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 (10 * 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_update(int cpu, cpuctl_update_args_t *data,
74 static int update_intel(int cpu, cpuctl_update_args_t *args,
76 static int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
78 static struct cdev **cpuctl_devs;
79 static MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
81 static struct cdevsw cpuctl_cdevsw = {
82 .d_version = D_VERSION,
83 .d_open = cpuctl_open,
84 .d_ioctl = cpuctl_ioctl,
89 * This function checks if specified cpu enabled or not.
95 return (pmc_cpu_is_disabled(cpu) == 0);
99 * Check if the current thread is bound to a specific cpu.
102 cpu_sched_is_bound(struct thread *td)
107 ret = sched_is_bound(td);
113 * Switch to target cpu to run.
116 set_cpu(int cpu, struct thread *td)
119 KASSERT(cpu >= 0 && cpu < mp_ncpus && cpu_enabled(cpu),
120 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
124 KASSERT(td->td_oncpu == cpu,
125 ("[cpuctl,%d]: cannot bind to target cpu %d", __LINE__, cpu));
129 restore_cpu(int oldcpu, int is_bound, struct thread *td)
132 KASSERT(oldcpu >= 0 && oldcpu < mp_ncpus && cpu_enabled(oldcpu),
133 ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
138 sched_bind(td, oldcpu);
143 cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
144 int flags, struct thread *td)
147 int cpu = dev2unit(dev);
149 if (cpu >= mp_ncpus || !cpu_enabled(cpu)) {
150 DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
153 /* Require write flag for "write" requests. */
154 if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) &&
155 ((flags & FWRITE) == 0))
159 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
164 ret = priv_check(td, PRIV_CPUCTL_WRMSR);
167 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
170 ret = cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
173 ret = priv_check(td, PRIV_CPUCTL_UPDATE);
176 ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
187 * Actually perform cpuid operation.
190 cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
195 KASSERT(cpu >= 0 && cpu < mp_ncpus,
196 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
198 /* Explicitly clear cpuid data to avoid returning stale info. */
199 bzero(data->data, sizeof(data->data));
200 DPRINTF("[cpuctl,%d]: retriving cpuid level %#0x for %d cpu\n",
201 __LINE__, data->level, cpu);
202 oldcpu = td->td_oncpu;
203 is_bound = cpu_sched_is_bound(td);
205 do_cpuid(data->level, data->data);
206 restore_cpu(oldcpu, is_bound, td);
211 * Actually perform MSR operations.
214 cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
221 KASSERT(cpu >= 0 && cpu < mp_ncpus,
222 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
225 * Explicitly clear cpuid data to avoid returning stale
228 DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
230 oldcpu = td->td_oncpu;
231 is_bound = cpu_sched_is_bound(td);
233 if (cmd == CPUCTL_RDMSR) {
235 ret = rdmsr_safe(data->msr, &data->data);
236 } else if (cmd == CPUCTL_WRMSR) {
237 ret = wrmsr_safe(data->msr, data->data);
238 } else if (cmd == CPUCTL_MSRSBIT) {
240 ret = rdmsr_safe(data->msr, ®);
242 ret = wrmsr_safe(data->msr, reg | data->data);
244 } else if (cmd == CPUCTL_MSRCBIT) {
246 ret = rdmsr_safe(data->msr, ®);
248 ret = wrmsr_safe(data->msr, reg & ~data->data);
251 panic("[cpuctl,%d]: unknown operation requested: %lu", __LINE__, cmd);
252 restore_cpu(oldcpu, is_bound, td);
257 * Actually perform microcode update.
260 cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
262 cpuctl_cpuid_args_t args = {
268 KASSERT(cpu >= 0 && cpu < mp_ncpus,
269 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
270 DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
272 ret = cpuctl_do_cpuid(cpu, &args, td);
274 DPRINTF("[cpuctl,%d]: cannot retrive cpuid info for cpu %d",
278 ((uint32_t *)vendor)[0] = args.data[1];
279 ((uint32_t *)vendor)[1] = args.data[3];
280 ((uint32_t *)vendor)[2] = args.data[2];
282 if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
283 ret = update_intel(cpu, data, td);
284 else if(strncmp(vendor, INTEL_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
285 ret = update_amd(cpu, data, td);
292 update_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
301 if (args->size == 0 || args->data == NULL) {
302 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
305 if (args->size > UCODE_SIZE_MAX) {
306 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
311 * 16 byte alignment required.
313 ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
314 ptr = (void *)(16 + ((intptr_t)ptr & ~0xf));
315 if (copyin(args->data, ptr, args->size) != 0) {
316 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
317 __LINE__, args->data, ptr, args->size);
321 oldcpu = td->td_oncpu;
322 is_bound = cpu_sched_is_bound(td);
325 rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current micorcode revision. */
330 wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
331 wrmsr_safe(MSR_BIOS_SIGN, 0);
334 * Serialize instruction flow.
338 rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new micorcode revision. */
339 restore_cpu(oldcpu, is_bound, td);
346 contigfree(ptr, args->size, M_CPUCTL);
351 update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
359 if (args->size == 0 || args->data == NULL) {
360 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
363 if (args->size > UCODE_SIZE_MAX) {
364 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
368 * XXX Might not require contignous address space - needs check
370 ptr = contigmalloc(args->size, M_CPUCTL, 0, 0, 0xffffffff, 16, 0);
372 DPRINTF("[cpuctl,%d]: cannot allocate %zd bytes of memory",
373 __LINE__, args->size);
376 if (copyin(args->data, ptr, args->size) != 0) {
377 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
378 __LINE__, args->data, ptr, args->size);
382 oldcpu = td->td_oncpu;
383 is_bound = cpu_sched_is_bound(td);
390 wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)ptr);
393 * Serialize instruction flow.
397 restore_cpu(oldcpu, is_bound, td);
401 contigfree(ptr, args->size, M_CPUCTL);
406 cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
412 if (cpu >= mp_ncpus || !cpu_enabled(cpu)) {
413 DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
418 ret = securelevel_gt(td->td_ucred, 0);
423 cpuctl_modevent(module_t mod __unused, int type, void *data __unused)
429 if ((cpu_feature & CPUID_MSR) == 0) {
431 printf("cpuctl: not available.\n");
435 printf("cpuctl: access to MSR registers/cpuid info.\n");
436 cpuctl_devs = (struct cdev **)malloc(sizeof(void *) * mp_ncpus,
437 M_CPUCTL, M_WAITOK | M_ZERO);
438 if (cpuctl_devs == NULL) {
439 DPRINTF("[cpuctl,%d]: cannot allocate memory\n",
443 for (cpu = 0; cpu < mp_ncpus; cpu++)
444 if (cpu_enabled(cpu))
445 cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
446 UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
449 for (cpu = 0; cpu < mp_ncpus; cpu++) {
450 if (cpuctl_devs[cpu] != NULL)
451 destroy_dev(cpuctl_devs[cpu]);
453 free(cpuctl_devs, M_CPUCTL);
463 DEV_MODULE(cpuctl, cpuctl_modevent, NULL);
464 MODULE_VERSION(cpuctl, CPUCTL_VERSION);