xref: /minix3/sys/arch/x86/include/cpu_msr.h (revision 1cd76c751364e6270e8d5a0daebc6d3d169baf4d)
1*1cd76c75SBen Gras /* $NetBSD: cpu_msr.h,v 1.7 2009/10/05 23:59:31 rmind Exp $ */
2*1cd76c75SBen Gras 
3*1cd76c75SBen Gras /*-
4*1cd76c75SBen Gras  * Copyright (c) 2007 Juan Romero Pardines.
5*1cd76c75SBen Gras  * All rights reserved.
6*1cd76c75SBen Gras  *
7*1cd76c75SBen Gras  * Redistribution and use in source and binary forms, with or without
8*1cd76c75SBen Gras  * modification, are permitted provided that the following conditions
9*1cd76c75SBen Gras  * are met:
10*1cd76c75SBen Gras  * 1. Redistributions of source code must retain the above copyright
11*1cd76c75SBen Gras  *    notice, this list of conditions and the following disclaimer.
12*1cd76c75SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*1cd76c75SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*1cd76c75SBen Gras  *    documentation and/or other materials provided with the distribution.
15*1cd76c75SBen Gras  *
16*1cd76c75SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*1cd76c75SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*1cd76c75SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*1cd76c75SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*1cd76c75SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*1cd76c75SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*1cd76c75SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*1cd76c75SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*1cd76c75SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*1cd76c75SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*1cd76c75SBen Gras  */
27*1cd76c75SBen Gras 
28*1cd76c75SBen Gras #ifndef _X86_CPU_MSR_H
29*1cd76c75SBen Gras #define _X86_CPU_MSR_H
30*1cd76c75SBen Gras 
31*1cd76c75SBen Gras #include <sys/param.h>
32*1cd76c75SBen Gras #include <sys/types.h>
33*1cd76c75SBen Gras 
34*1cd76c75SBen Gras #ifdef _KERNEL
35*1cd76c75SBen Gras 
36*1cd76c75SBen Gras struct msr_rw_info {
37*1cd76c75SBen Gras 	int		msr_read;
38*1cd76c75SBen Gras 	int		msr_type;
39*1cd76c75SBen Gras 	uint64_t	msr_value;
40*1cd76c75SBen Gras 	uint64_t	msr_mask;
41*1cd76c75SBen Gras };
42*1cd76c75SBen Gras 
43*1cd76c75SBen Gras static inline void
x86_msr_xcall(void * arg1,void * arg2)44*1cd76c75SBen Gras x86_msr_xcall(void *arg1, void *arg2)
45*1cd76c75SBen Gras {
46*1cd76c75SBen Gras 	struct msr_rw_info *msrdat = arg1;
47*1cd76c75SBen Gras 	uint64_t msr = 0;
48*1cd76c75SBen Gras 
49*1cd76c75SBen Gras 	KASSERT(msrdat->msr_type != 0);
50*1cd76c75SBen Gras 
51*1cd76c75SBen Gras 	/* Read the MSR requested and apply the mask if defined. */
52*1cd76c75SBen Gras 	if (msrdat->msr_read) {
53*1cd76c75SBen Gras 		msr = rdmsr(msrdat->msr_type);
54*1cd76c75SBen Gras 		if (msrdat->msr_mask) {
55*1cd76c75SBen Gras 			msr &= ~msrdat->msr_mask;
56*1cd76c75SBen Gras 		}
57*1cd76c75SBen Gras 	}
58*1cd76c75SBen Gras 	/* Assign (or extract, on read) the value and perform the write. */
59*1cd76c75SBen Gras 	msr |= msrdat->msr_value;
60*1cd76c75SBen Gras 	wrmsr(msrdat->msr_type, msr);
61*1cd76c75SBen Gras }
62*1cd76c75SBen Gras 
63*1cd76c75SBen Gras #endif /* ! _KERNEL */
64*1cd76c75SBen Gras #endif /* ! _X86_CPU_MSR_H */
65