cpuctl.c revision 275960
1181430Sstas/*-
2181430Sstas * Copyright (c) 2006-2008 Stanislav Sedov <stas@FreeBSD.org>
3181430Sstas * All rights reserved.
4181430Sstas *
5181430Sstas * Redistribution and use in source and binary forms, with or without
6181430Sstas * modification, are permitted provided that the following conditions
7181430Sstas * are met:
8181430Sstas * 1. Redistributions of source code must retain the above copyright
9181430Sstas *    notice, this list of conditions and the following disclaimer.
10181430Sstas * 2. Redistributions in binary form must reproduce the above copyright
11181430Sstas *    notice, this list of conditions and the following disclaimer in the
12181430Sstas *    documentation and/or other materials provided with the distribution.
13181430Sstas *
14181430Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15181430Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16181430Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17181430Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18181430Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19181430Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20181430Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21181430Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22181430Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23181430Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24181430Sstas * SUCH DAMAGE.
25181430Sstas *
26181430Sstas */
27181430Sstas
28181430Sstas#include <sys/cdefs.h>
29181430Sstas__FBSDID("$FreeBSD: head/sys/dev/cpuctl/cpuctl.c 275960 2014-12-20 16:40:49Z kib $");
30181430Sstas
31181430Sstas#include <sys/param.h>
32181430Sstas#include <sys/systm.h>
33181430Sstas#include <sys/conf.h>
34181430Sstas#include <sys/fcntl.h>
35181430Sstas#include <sys/ioccom.h>
36181430Sstas#include <sys/malloc.h>
37181430Sstas#include <sys/module.h>
38181430Sstas#include <sys/mutex.h>
39181430Sstas#include <sys/priv.h>
40181430Sstas#include <sys/proc.h>
41181430Sstas#include <sys/queue.h>
42181430Sstas#include <sys/sched.h>
43181430Sstas#include <sys/kernel.h>
44181430Sstas#include <sys/sysctl.h>
45181430Sstas#include <sys/uio.h>
46181430Sstas#include <sys/pcpu.h>
47181430Sstas#include <sys/smp.h>
48181430Sstas#include <sys/pmckern.h>
49181430Sstas#include <sys/cpuctl.h>
50181430Sstas
51181430Sstas#include <machine/cpufunc.h>
52181430Sstas#include <machine/md_var.h>
53181430Sstas#include <machine/specialreg.h>
54181430Sstas
55181430Sstasstatic d_open_t cpuctl_open;
56181430Sstasstatic d_ioctl_t cpuctl_ioctl;
57181430Sstas
58181430Sstas#define	CPUCTL_VERSION 1
59181430Sstas
60181430Sstas#ifdef DEBUG
61181430Sstas# define	DPRINTF(format,...) printf(format, __VA_ARGS__);
62181430Sstas#else
63181430Sstas# define	DPRINTF(...)
64181430Sstas#endif
65181430Sstas
66275960Skib#define	UCODE_SIZE_MAX	(32 * 1024)
67181430Sstas
68181430Sstasstatic int cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd,
69181430Sstas    struct thread *td);
70267814Skibstatic void cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data,
71181430Sstas    struct thread *td);
72267814Skibstatic void cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
73267651Sattilio    struct thread *td);
74181430Sstasstatic int cpuctl_do_update(int cpu, cpuctl_update_args_t *data,
75181430Sstas    struct thread *td);
76181430Sstasstatic int update_intel(int cpu, cpuctl_update_args_t *args,
77181430Sstas    struct thread *td);
78181430Sstasstatic int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
79228436Sfabientstatic int update_via(int cpu, cpuctl_update_args_t *args,
80228436Sfabient    struct thread *td);
81181430Sstas
82181430Sstasstatic struct cdev **cpuctl_devs;
83181430Sstasstatic MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
84181430Sstas
85181430Sstasstatic struct cdevsw cpuctl_cdevsw = {
86181430Sstas        .d_version =    D_VERSION,
87181430Sstas        .d_open =       cpuctl_open,
88181430Sstas        .d_ioctl =      cpuctl_ioctl,
89181430Sstas        .d_name =       "cpuctl",
90181430Sstas};
91181430Sstas
92181430Sstas/*
93181430Sstas * This function checks if specified cpu enabled or not.
94181430Sstas */
95181430Sstasstatic int
96181430Sstascpu_enabled(int cpu)
97181430Sstas{
98181430Sstas
99181430Sstas	return (pmc_cpu_is_disabled(cpu) == 0);
100181430Sstas}
101181430Sstas
102181430Sstas/*
103181430Sstas * Check if the current thread is bound to a specific cpu.
104181430Sstas */
105181430Sstasstatic int
106181430Sstascpu_sched_is_bound(struct thread *td)
107181430Sstas{
108181430Sstas	int ret;
109181430Sstas
110181430Sstas	thread_lock(td);
111181430Sstas	ret = sched_is_bound(td);
112181430Sstas	thread_unlock(td);
113181430Sstas	return (ret);
114181430Sstas}
115181430Sstas
116181430Sstas/*
117181430Sstas * Switch to target cpu to run.
118181430Sstas */
119181430Sstasstatic void
120181430Sstasset_cpu(int cpu, struct thread *td)
121181430Sstas{
122181430Sstas
123181430Sstas	KASSERT(cpu >= 0 && cpu < mp_ncpus && cpu_enabled(cpu),
124181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
125181430Sstas	thread_lock(td);
126181430Sstas	sched_bind(td, cpu);
127181430Sstas	thread_unlock(td);
128181430Sstas	KASSERT(td->td_oncpu == cpu,
129181430Sstas	    ("[cpuctl,%d]: cannot bind to target cpu %d", __LINE__, cpu));
130181430Sstas}
131181430Sstas
132181430Sstasstatic void
133181430Sstasrestore_cpu(int oldcpu, int is_bound, struct thread *td)
134181430Sstas{
135181430Sstas
136181430Sstas	KASSERT(oldcpu >= 0 && oldcpu < mp_ncpus && cpu_enabled(oldcpu),
137181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
138181430Sstas	thread_lock(td);
139181430Sstas	if (is_bound == 0)
140181430Sstas		sched_unbind(td);
141181430Sstas	else
142181430Sstas		sched_bind(td, oldcpu);
143181430Sstas	thread_unlock(td);
144181430Sstas}
145181430Sstas
146181430Sstasint
147181430Sstascpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
148181430Sstas	int flags, struct thread *td)
149181430Sstas{
150181430Sstas	int ret;
151183397Sed	int cpu = dev2unit(dev);
152181430Sstas
153181430Sstas	if (cpu >= mp_ncpus || !cpu_enabled(cpu)) {
154181430Sstas		DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
155181430Sstas		return (ENXIO);
156181430Sstas	}
157181430Sstas	/* Require write flag for "write" requests. */
158181430Sstas	if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) &&
159181430Sstas	    ((flags & FWRITE) == 0))
160181430Sstas		return (EPERM);
161181430Sstas	switch (cmd) {
162181430Sstas	case CPUCTL_RDMSR:
163181430Sstas		ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
164181430Sstas		break;
165195189Sstas	case CPUCTL_MSRSBIT:
166195189Sstas	case CPUCTL_MSRCBIT:
167181430Sstas	case CPUCTL_WRMSR:
168181430Sstas		ret = priv_check(td, PRIV_CPUCTL_WRMSR);
169181430Sstas		if (ret != 0)
170181430Sstas			goto fail;
171181430Sstas		ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
172181430Sstas		break;
173181430Sstas	case CPUCTL_CPUID:
174267814Skib		cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
175267814Skib		ret = 0;
176181430Sstas		break;
177181430Sstas	case CPUCTL_UPDATE:
178181430Sstas		ret = priv_check(td, PRIV_CPUCTL_UPDATE);
179181430Sstas		if (ret != 0)
180181430Sstas			goto fail;
181181430Sstas		ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
182181430Sstas		break;
183267651Sattilio	case CPUCTL_CPUID_COUNT:
184267814Skib		cpuctl_do_cpuid_count(cpu, (cpuctl_cpuid_count_args_t *)data,
185267814Skib		    td);
186267814Skib		ret = 0;
187267651Sattilio		break;
188181430Sstas	default:
189181430Sstas		ret = EINVAL;
190181430Sstas		break;
191181430Sstas	}
192181430Sstasfail:
193181430Sstas	return (ret);
194181430Sstas}
195181430Sstas
196181430Sstas/*
197181430Sstas * Actually perform cpuid operation.
198181430Sstas */
199267814Skibstatic void
200267673Skibcpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
201267673Skib    struct thread *td)
202181430Sstas{
203181430Sstas	int is_bound = 0;
204181430Sstas	int oldcpu;
205181430Sstas
206181430Sstas	KASSERT(cpu >= 0 && cpu < mp_ncpus,
207181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
208181430Sstas
209181430Sstas	/* Explicitly clear cpuid data to avoid returning stale info. */
210181430Sstas	bzero(data->data, sizeof(data->data));
211267651Sattilio	DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n",
212267651Sattilio	    __LINE__, data->level, data->level_type, cpu);
213181430Sstas	oldcpu = td->td_oncpu;
214181430Sstas	is_bound = cpu_sched_is_bound(td);
215181430Sstas	set_cpu(cpu, td);
216267651Sattilio	cpuid_count(data->level, data->level_type, data->data);
217181430Sstas	restore_cpu(oldcpu, is_bound, td);
218181430Sstas}
219181430Sstas
220267814Skibstatic void
221267651Sattiliocpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
222267651Sattilio{
223267673Skib	cpuctl_cpuid_count_args_t cdata;
224267651Sattilio
225267673Skib	cdata.level = data->level;
226267651Sattilio	/* Override the level type. */
227267673Skib	cdata.level_type = 0;
228267814Skib	cpuctl_do_cpuid_count(cpu, &cdata, td);
229267673Skib	bcopy(cdata.data, data->data, sizeof(data->data)); /* Ignore error */
230267651Sattilio}
231267651Sattilio
232181430Sstas/*
233181430Sstas * Actually perform MSR operations.
234181430Sstas */
235181430Sstasstatic int
236181430Sstascpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
237181430Sstas{
238195189Sstas	uint64_t reg;
239181430Sstas	int is_bound = 0;
240181430Sstas	int oldcpu;
241181430Sstas	int ret;
242181430Sstas
243181430Sstas	KASSERT(cpu >= 0 && cpu < mp_ncpus,
244181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
245181430Sstas
246181430Sstas	/*
247181430Sstas	 * Explicitly clear cpuid data to avoid returning stale
248181430Sstas	 * info
249181430Sstas	 */
250181430Sstas	DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
251181430Sstas	    data->msr, cpu);
252181430Sstas	oldcpu = td->td_oncpu;
253181430Sstas	is_bound = cpu_sched_is_bound(td);
254181430Sstas	set_cpu(cpu, td);
255195081Sstas	if (cmd == CPUCTL_RDMSR) {
256195081Sstas		data->data = 0;
257195081Sstas		ret = rdmsr_safe(data->msr, &data->data);
258195189Sstas	} else if (cmd == CPUCTL_WRMSR) {
259195081Sstas		ret = wrmsr_safe(data->msr, data->data);
260195189Sstas	} else if (cmd == CPUCTL_MSRSBIT) {
261195189Sstas		critical_enter();
262195189Sstas		ret = rdmsr_safe(data->msr, &reg);
263195189Sstas		if (ret == 0)
264195189Sstas			ret = wrmsr_safe(data->msr, reg | data->data);
265195189Sstas		critical_exit();
266195189Sstas	} else if (cmd == CPUCTL_MSRCBIT) {
267195189Sstas		critical_enter();
268195189Sstas		ret = rdmsr_safe(data->msr, &reg);
269195189Sstas		if (ret == 0)
270195189Sstas			ret = wrmsr_safe(data->msr, reg & ~data->data);
271195189Sstas		critical_exit();
272195189Sstas	} else
273195189Sstas		panic("[cpuctl,%d]: unknown operation requested: %lu", __LINE__, cmd);
274181430Sstas	restore_cpu(oldcpu, is_bound, td);
275181430Sstas	return (ret);
276181430Sstas}
277181430Sstas
278181430Sstas/*
279181430Sstas * Actually perform microcode update.
280181430Sstas */
281181430Sstasstatic int
282181430Sstascpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
283181430Sstas{
284181430Sstas	cpuctl_cpuid_args_t args = {
285181430Sstas		.level = 0,
286181430Sstas	};
287181430Sstas	char vendor[13];
288181430Sstas	int ret;
289181430Sstas
290181430Sstas	KASSERT(cpu >= 0 && cpu < mp_ncpus,
291181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
292181430Sstas	DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
293181430Sstas
294267814Skib	cpuctl_do_cpuid(cpu, &args, td);
295181430Sstas	((uint32_t *)vendor)[0] = args.data[1];
296181430Sstas	((uint32_t *)vendor)[1] = args.data[3];
297181430Sstas	((uint32_t *)vendor)[2] = args.data[2];
298181430Sstas	vendor[12] = '\0';
299181430Sstas	if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
300181430Sstas		ret = update_intel(cpu, data, td);
301228436Sfabient	else if(strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
302181430Sstas		ret = update_amd(cpu, data, td);
303228436Sfabient	else if(strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID)) == 0)
304228436Sfabient		ret = update_via(cpu, data, td);
305181430Sstas	else
306181430Sstas		ret = ENXIO;
307181430Sstas	return (ret);
308181430Sstas}
309181430Sstas
310181430Sstasstatic int
311181430Sstasupdate_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
312181430Sstas{
313255439Skib	void *ptr;
314181430Sstas	uint64_t rev0, rev1;
315181430Sstas	uint32_t tmp[4];
316255439Skib	int is_bound;
317181430Sstas	int oldcpu;
318181430Sstas	int ret;
319181430Sstas
320181430Sstas	if (args->size == 0 || args->data == NULL) {
321181430Sstas		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
322181430Sstas		return (EINVAL);
323181430Sstas	}
324181430Sstas	if (args->size > UCODE_SIZE_MAX) {
325181430Sstas		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
326181430Sstas		return (EINVAL);
327181430Sstas	}
328181430Sstas
329181430Sstas	/*
330255439Skib	 * 16 byte alignment required.  Rely on the fact that
331255439Skib	 * malloc(9) always returns the pointer aligned at least on
332255439Skib	 * the size of the allocation.
333181430Sstas	 */
334181430Sstas	ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
335181430Sstas	if (copyin(args->data, ptr, args->size) != 0) {
336181430Sstas		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
337181430Sstas		    __LINE__, args->data, ptr, args->size);
338181430Sstas		ret = EFAULT;
339181430Sstas		goto fail;
340181430Sstas	}
341181430Sstas	oldcpu = td->td_oncpu;
342181430Sstas	is_bound = cpu_sched_is_bound(td);
343181430Sstas	set_cpu(cpu, td);
344181430Sstas	critical_enter();
345252592Srpaulo	rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
346181430Sstas
347181430Sstas	/*
348181430Sstas	 * Perform update.
349181430Sstas	 */
350181430Sstas	wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
351181430Sstas	wrmsr_safe(MSR_BIOS_SIGN, 0);
352181430Sstas
353181430Sstas	/*
354181430Sstas	 * Serialize instruction flow.
355181430Sstas	 */
356181430Sstas	do_cpuid(0, tmp);
357181430Sstas	critical_exit();
358252592Srpaulo	rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
359181430Sstas	restore_cpu(oldcpu, is_bound, td);
360181430Sstas	if (rev1 > rev0)
361181430Sstas		ret = 0;
362181430Sstas	else
363181430Sstas		ret = EEXIST;
364181430Sstasfail:
365254191Skib	free(ptr, M_CPUCTL);
366181430Sstas	return (ret);
367181430Sstas}
368181430Sstas
369181430Sstasstatic int
370181430Sstasupdate_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
371181430Sstas{
372181430Sstas	void *ptr = NULL;
373181430Sstas	uint32_t tmp[4];
374181430Sstas	int is_bound = 0;
375181430Sstas	int oldcpu;
376181430Sstas	int ret;
377181430Sstas
378181430Sstas	if (args->size == 0 || args->data == NULL) {
379181430Sstas		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
380181430Sstas		return (EINVAL);
381181430Sstas	}
382181430Sstas	if (args->size > UCODE_SIZE_MAX) {
383181430Sstas		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
384181430Sstas		return (EINVAL);
385181430Sstas	}
386181430Sstas	/*
387181430Sstas	 * XXX Might not require contignous address space - needs check
388181430Sstas	 */
389181430Sstas	ptr = contigmalloc(args->size, M_CPUCTL, 0, 0, 0xffffffff, 16, 0);
390181430Sstas	if (ptr == NULL) {
391181430Sstas		DPRINTF("[cpuctl,%d]: cannot allocate %zd bytes of memory",
392181430Sstas		    __LINE__, args->size);
393181430Sstas		return (ENOMEM);
394181430Sstas	}
395181430Sstas	if (copyin(args->data, ptr, args->size) != 0) {
396181430Sstas		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
397181430Sstas		    __LINE__, args->data, ptr, args->size);
398181430Sstas		ret = EFAULT;
399181430Sstas		goto fail;
400181430Sstas	}
401181430Sstas	oldcpu = td->td_oncpu;
402181430Sstas	is_bound = cpu_sched_is_bound(td);
403181430Sstas	set_cpu(cpu, td);
404181430Sstas	critical_enter();
405181430Sstas
406181430Sstas	/*
407181430Sstas	 * Perform update.
408181430Sstas	 */
409195081Sstas	wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)ptr);
410181430Sstas
411181430Sstas	/*
412181430Sstas	 * Serialize instruction flow.
413181430Sstas	 */
414181430Sstas	do_cpuid(0, tmp);
415181430Sstas	critical_exit();
416181430Sstas	restore_cpu(oldcpu, is_bound, td);
417181430Sstas	ret = 0;
418181430Sstasfail:
419181430Sstas	if (ptr != NULL)
420181430Sstas		contigfree(ptr, args->size, M_CPUCTL);
421181430Sstas	return (ret);
422181430Sstas}
423181430Sstas
424228436Sfabientstatic int
425228436Sfabientupdate_via(int cpu, cpuctl_update_args_t *args, struct thread *td)
426228436Sfabient{
427255439Skib	void *ptr;
428228436Sfabient	uint64_t rev0, rev1, res;
429228436Sfabient	uint32_t tmp[4];
430255439Skib	int is_bound;
431228436Sfabient	int oldcpu;
432228436Sfabient	int ret;
433228436Sfabient
434228436Sfabient	if (args->size == 0 || args->data == NULL) {
435228436Sfabient		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
436228436Sfabient		return (EINVAL);
437228436Sfabient	}
438228436Sfabient	if (args->size > UCODE_SIZE_MAX) {
439228436Sfabient		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
440228436Sfabient		return (EINVAL);
441228436Sfabient	}
442228436Sfabient
443228436Sfabient	/*
444228436Sfabient	 * 4 byte alignment required.
445228436Sfabient	 */
446255439Skib	ptr = malloc(args->size, M_CPUCTL, M_WAITOK);
447228436Sfabient	if (copyin(args->data, ptr, args->size) != 0) {
448228436Sfabient		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
449228436Sfabient		    __LINE__, args->data, ptr, args->size);
450228436Sfabient		ret = EFAULT;
451228436Sfabient		goto fail;
452228436Sfabient	}
453228436Sfabient	oldcpu = td->td_oncpu;
454228436Sfabient	is_bound = cpu_sched_is_bound(td);
455228436Sfabient	set_cpu(cpu, td);
456228436Sfabient	critical_enter();
457252592Srpaulo	rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
458228436Sfabient
459228436Sfabient	/*
460228436Sfabient	 * Perform update.
461228436Sfabient	 */
462228436Sfabient	wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
463228436Sfabient	do_cpuid(1, tmp);
464228436Sfabient
465228436Sfabient	/*
466228436Sfabient	 * Result are in low byte of MSR FCR5:
467228436Sfabient	 * 0x00: No update has been attempted since RESET.
468228436Sfabient	 * 0x01: The last attempted update was successful.
469228436Sfabient	 * 0x02: The last attempted update was unsuccessful due to a bad
470228436Sfabient	 *       environment. No update was loaded and any preexisting
471228436Sfabient	 *       patches are still active.
472228436Sfabient	 * 0x03: The last attempted update was not applicable to this processor.
473228436Sfabient	 *       No update was loaded and any preexisting patches are still
474228436Sfabient	 *       active.
475228436Sfabient	 * 0x04: The last attempted update was not successful due to an invalid
476228436Sfabient	 *       update data block. No update was loaded and any preexisting
477228436Sfabient	 *       patches are still active
478228436Sfabient	 */
479228436Sfabient	rdmsr_safe(0x1205, &res);
480228436Sfabient	res &= 0xff;
481228436Sfabient	critical_exit();
482228436Sfabient	rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
483228436Sfabient	restore_cpu(oldcpu, is_bound, td);
484228436Sfabient
485228436Sfabient	DPRINTF("[cpu,%d]: rev0=%x rev1=%x res=%x\n", __LINE__,
486228436Sfabient	    (unsigned)(rev0 >> 32), (unsigned)(rev1 >> 32), (unsigned)res);
487228436Sfabient
488228436Sfabient	if (res != 0x01)
489228436Sfabient		ret = EINVAL;
490228436Sfabient	else
491228436Sfabient		ret = 0;
492228436Sfabientfail:
493254191Skib	free(ptr, M_CPUCTL);
494228436Sfabient	return (ret);
495228436Sfabient}
496228436Sfabient
497181430Sstasint
498181430Sstascpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
499181430Sstas{
500181430Sstas	int ret = 0;
501181430Sstas	int cpu;
502181430Sstas
503183397Sed	cpu = dev2unit(dev);
504181430Sstas	if (cpu >= mp_ncpus || !cpu_enabled(cpu)) {
505181430Sstas		DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
506181430Sstas		    cpu);
507181430Sstas		return (ENXIO);
508181430Sstas	}
509181430Sstas	if (flags & FWRITE)
510181430Sstas		ret = securelevel_gt(td->td_ucred, 0);
511181430Sstas	return (ret);
512181430Sstas}
513181430Sstas
514181430Sstasstatic int
515181430Sstascpuctl_modevent(module_t mod __unused, int type, void *data __unused)
516181430Sstas{
517181430Sstas	int cpu;
518181430Sstas
519181430Sstas	switch(type) {
520181430Sstas	case MOD_LOAD:
521181430Sstas		if ((cpu_feature & CPUID_MSR) == 0) {
522181430Sstas			if (bootverbose)
523181430Sstas				printf("cpuctl: not available.\n");
524181430Sstas			return (ENODEV);
525181430Sstas		}
526181430Sstas		if (bootverbose)
527181430Sstas			printf("cpuctl: access to MSR registers/cpuid info.\n");
528263080Skib		cpuctl_devs = malloc(sizeof(*cpuctl_devs) * mp_ncpus, M_CPUCTL,
529263080Skib		    M_WAITOK | M_ZERO);
530181430Sstas		for (cpu = 0; cpu < mp_ncpus; cpu++)
531181430Sstas			if (cpu_enabled(cpu))
532181430Sstas				cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
533181430Sstas				    UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
534181430Sstas		break;
535181430Sstas	case MOD_UNLOAD:
536181430Sstas		for (cpu = 0; cpu < mp_ncpus; cpu++) {
537181430Sstas			if (cpuctl_devs[cpu] != NULL)
538181430Sstas				destroy_dev(cpuctl_devs[cpu]);
539181430Sstas		}
540181430Sstas		free(cpuctl_devs, M_CPUCTL);
541181430Sstas		break;
542181430Sstas	case MOD_SHUTDOWN:
543181430Sstas		break;
544181430Sstas	default:
545181430Sstas		return (EOPNOTSUPP);
546181430Sstas        }
547181430Sstas	return (0);
548181430Sstas}
549181430Sstas
550181430SstasDEV_MODULE(cpuctl, cpuctl_modevent, NULL);
551181430SstasMODULE_VERSION(cpuctl, CPUCTL_VERSION);
552