drivers/pc80/pc: Clean up formatting of PS/2 related ASL code
[coreboot2.git] / util / msrtool / freebsd.c
blobf1afc9a01ed2bc16212a3a521520f864c5f04735
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <errno.h>
10 #include "msrtool.h"
12 static int msr_fd[MAX_CORES] = {-1, -1, -1, -1, -1, -1, -1, -1};
14 int freebsd_probe(const struct sysdef *system)
16 #ifdef __FreeBSD__
17 struct stat st;
19 return stat("/dev/cpuctl0", &st) == 0;
20 #else
21 return 0;
22 #endif
25 int freebsd_open(uint8_t cpu, enum SysModes mode)
27 #ifdef __FreeBSD__
28 int flags;
29 char devname[32];
31 if (cpu >= MAX_CORES)
32 return 0;
34 if (SYS_RDWR == mode)
35 flags = O_RDWR;
36 else if (SYS_WRONLY == mode)
37 flags = O_WRONLY;
38 else
39 flags = O_RDONLY;
41 snprintf(devname, sizeof(devname), "/dev/cpuctl%u", cpu);
42 msr_fd[cpu] = open(devname, flags);
43 if (msr_fd[cpu] < 0) {
44 perror(devname);
45 return 0;
47 return 1;
48 #else
49 return 0;
50 #endif
53 int freebsd_close(uint8_t cpu)
55 if (cpu >= MAX_CORES)
56 return 0;
58 if (msr_fd[cpu] != -1)
59 close(msr_fd[cpu]);
60 msr_fd[cpu] = -1;
61 return 1;
64 int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val)
66 #ifdef __FreeBSD__
67 cpuctl_msr_args_t args;
69 if (cpu >= MAX_CORES)
70 return 0;
72 if (msr_fd[cpu] < 0)
73 return 0;
75 args.msr = addr;
76 if (ioctl(msr_fd[cpu], CPUCTL_RDMSR, &args) < 0) {
77 perror("ioctl(CPUCTL_RDMSR)");
78 return 0;
81 val->hi = args.data >> 32;
82 val->lo = args.data & 0xffffffff;
83 return 1;
84 #else
85 return 0;
86 #endif