2 * Copyright (c) 2006-2008 Stanislav Sedov <stas@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/fcntl.h>
35 #include <sys/ioccom.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/sched.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
48 #include <sys/pmckern.h>
49 #include <sys/cpuctl.h>
51 #include <machine/cpufunc.h>
52 #include <machine/md_var.h>
53 #include <machine/specialreg.h>
55 static d_open_t cpuctl_open;
56 static d_ioctl_t cpuctl_ioctl;
58 #define CPUCTL_VERSION 1
61 # define DPRINTF(format,...) printf(format, __VA_ARGS__);
66 #define UCODE_SIZE_MAX (4 * 1024 * 1024)
68 static int cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd,
70 static int cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data,
72 static int cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
74 static int cpuctl_do_update(int cpu, cpuctl_update_args_t *data,
76 static int update_intel(int cpu, cpuctl_update_args_t *args,
78 static int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
79 static int update_via(int cpu, cpuctl_update_args_t *args,
82 static struct cdev **cpuctl_devs;
83 static MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
85 static struct cdevsw cpuctl_cdevsw = {
86 .d_version = D_VERSION,
87 .d_open = cpuctl_open,
88 .d_ioctl = cpuctl_ioctl,
93 * This function checks if specified cpu enabled or not.
99 return (pmc_cpu_is_disabled(cpu) == 0);
103 * Check if the current thread is bound to a specific cpu.
106 cpu_sched_is_bound(struct thread *td)
111 ret = sched_is_bound(td);
117 * Switch to target cpu to run.
120 set_cpu(int cpu, struct thread *td)
123 KASSERT(cpu >= 0 && cpu <= mp_maxid && cpu_enabled(cpu),
124 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
128 KASSERT(td->td_oncpu == cpu,
129 ("[cpuctl,%d]: cannot bind to target cpu %d on cpu %d", __LINE__, cpu, td->td_oncpu));
133 restore_cpu(int oldcpu, int is_bound, struct thread *td)
136 KASSERT(oldcpu >= 0 && oldcpu <= mp_maxid && cpu_enabled(oldcpu),
137 ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
142 sched_bind(td, oldcpu);
147 cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
148 int flags, struct thread *td)
151 int cpu = dev2unit(dev);
153 if (cpu > mp_maxid || !cpu_enabled(cpu)) {
154 DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
157 /* Require write flag for "write" requests. */
158 if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) &&
159 ((flags & FWRITE) == 0))
163 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
168 ret = priv_check(td, PRIV_CPUCTL_WRMSR);
171 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
174 ret = cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
177 ret = priv_check(td, PRIV_CPUCTL_UPDATE);
180 ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
182 case CPUCTL_CPUID_COUNT:
183 ret = cpuctl_do_cpuid_count(cpu,
184 (cpuctl_cpuid_count_args_t *)data, td);
195 * Actually perform cpuid operation.
198 cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
204 KASSERT(cpu >= 0 && cpu <= mp_maxid,
205 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
207 /* Explicitly clear cpuid data to avoid returning stale info. */
208 bzero(data->data, sizeof(data->data));
209 DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n",
210 __LINE__, data->level, data->level_type, cpu);
215 oldcpu = td->td_oncpu;
216 is_bound = cpu_sched_is_bound(td);
218 cpuid_count(data->level, data->level_type, data->data);
219 restore_cpu(oldcpu, is_bound, td);
224 cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
226 cpuctl_cpuid_count_args_t cdata;
229 cdata.level = data->level;
230 /* Override the level type. */
231 cdata.level_type = 0;
232 error = cpuctl_do_cpuid_count(cpu, &cdata, td);
233 bcopy(cdata.data, data->data, sizeof(data->data)); /* Ignore error */
238 * Actually perform MSR operations.
241 cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
248 KASSERT(cpu >= 0 && cpu <= mp_maxid,
249 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
252 * Explicitly clear cpuid data to avoid returning stale
255 DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
258 if ((cpu_feature & CPUID_MSR) == 0)
261 oldcpu = td->td_oncpu;
262 is_bound = cpu_sched_is_bound(td);
264 if (cmd == CPUCTL_RDMSR) {
266 ret = rdmsr_safe(data->msr, &data->data);
267 } else if (cmd == CPUCTL_WRMSR) {
268 ret = wrmsr_safe(data->msr, data->data);
269 } else if (cmd == CPUCTL_MSRSBIT) {
271 ret = rdmsr_safe(data->msr, ®);
273 ret = wrmsr_safe(data->msr, reg | data->data);
275 } else if (cmd == CPUCTL_MSRCBIT) {
277 ret = rdmsr_safe(data->msr, ®);
279 ret = wrmsr_safe(data->msr, reg & ~data->data);
282 panic("[cpuctl,%d]: unknown operation requested: %lu", __LINE__, cmd);
283 restore_cpu(oldcpu, is_bound, td);
288 * Actually perform microcode update.
291 cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
293 cpuctl_cpuid_args_t args = {
299 KASSERT(cpu >= 0 && cpu <= mp_maxid,
300 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
301 DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
303 ret = cpuctl_do_cpuid(cpu, &args, td);
306 ((uint32_t *)vendor)[0] = args.data[1];
307 ((uint32_t *)vendor)[1] = args.data[3];
308 ((uint32_t *)vendor)[2] = args.data[2];
310 if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
311 ret = update_intel(cpu, data, td);
312 else if(strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
313 ret = update_amd(cpu, data, td);
314 else if(strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID)) == 0)
315 ret = update_via(cpu, data, td);
322 update_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
331 if (args->size == 0 || args->data == NULL) {
332 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
335 if (args->size > UCODE_SIZE_MAX) {
336 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
341 * 16 byte alignment required. Rely on the fact that
342 * malloc(9) always returns the pointer aligned at least on
343 * the size of the allocation.
345 ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
346 if (copyin(args->data, ptr, args->size) != 0) {
347 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
348 __LINE__, args->data, ptr, args->size);
352 oldcpu = td->td_oncpu;
353 is_bound = cpu_sched_is_bound(td);
356 rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
361 wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
362 wrmsr_safe(MSR_BIOS_SIGN, 0);
365 * Serialize instruction flow.
369 rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
370 restore_cpu(oldcpu, is_bound, td);
381 * NB: MSR 0xc0010020, MSR_K8_UCODE_UPDATE, is not documented by AMD.
382 * Coreboot, illumos and Linux source code was used to understand
386 amd_ucode_wrmsr(void *ucode_ptr)
390 wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)ucode_ptr);
395 update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
400 if (args->size == 0 || args->data == NULL) {
401 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
404 if (args->size > UCODE_SIZE_MAX) {
405 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
410 * 16 byte alignment required. Rely on the fact that
411 * malloc(9) always returns the pointer aligned at least on
412 * the size of the allocation.
414 ptr = malloc(args->size + 16, M_CPUCTL, M_ZERO | M_WAITOK);
415 if (copyin(args->data, ptr, args->size) != 0) {
416 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
417 __LINE__, args->data, ptr, args->size);
421 smp_rendezvous(NULL, amd_ucode_wrmsr, NULL, ptr);
429 update_via(int cpu, cpuctl_update_args_t *args, struct thread *td)
432 uint64_t rev0, rev1, res;
438 if (args->size == 0 || args->data == NULL) {
439 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
442 if (args->size > UCODE_SIZE_MAX) {
443 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
448 * 4 byte alignment required.
450 ptr = malloc(args->size, M_CPUCTL, M_WAITOK);
451 if (copyin(args->data, ptr, args->size) != 0) {
452 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
453 __LINE__, args->data, ptr, args->size);
457 oldcpu = td->td_oncpu;
458 is_bound = cpu_sched_is_bound(td);
461 rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
466 wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
470 * Result are in low byte of MSR FCR5:
471 * 0x00: No update has been attempted since RESET.
472 * 0x01: The last attempted update was successful.
473 * 0x02: The last attempted update was unsuccessful due to a bad
474 * environment. No update was loaded and any preexisting
475 * patches are still active.
476 * 0x03: The last attempted update was not applicable to this processor.
477 * No update was loaded and any preexisting patches are still
479 * 0x04: The last attempted update was not successful due to an invalid
480 * update data block. No update was loaded and any preexisting
481 * patches are still active
483 rdmsr_safe(0x1205, &res);
486 rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
487 restore_cpu(oldcpu, is_bound, td);
489 DPRINTF("[cpu,%d]: rev0=%x rev1=%x res=%x\n", __LINE__,
490 (unsigned)(rev0 >> 32), (unsigned)(rev1 >> 32), (unsigned)res);
502 cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
508 if (cpu > mp_maxid || !cpu_enabled(cpu)) {
509 DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
514 ret = securelevel_gt(td->td_ucred, 0);
519 cpuctl_modevent(module_t mod __unused, int type, void *data __unused)
526 printf("cpuctl: access to MSR registers/cpuid info.\n");
527 cpuctl_devs = malloc(sizeof(*cpuctl_devs) * (mp_maxid + 1), M_CPUCTL,
530 if (cpu_enabled(cpu))
531 cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
532 UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
536 if (cpuctl_devs[cpu] != NULL)
537 destroy_dev(cpuctl_devs[cpu]);
539 free(cpuctl_devs, M_CPUCTL);
549 DEV_MODULE(cpuctl, cpuctl_modevent, NULL);
550 MODULE_VERSION(cpuctl, CPUCTL_VERSION);