mtrr.h revision 1.2 1 /* $NetBSD: mtrr.h,v 1.2 2003/07/28 10:29:00 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef _X86_MTRR_H_
40 #define _X86_MTRR_H_
41
42 #define MTRR_I686_FIXED_IDX64K 0
43 #define MTRR_I686_FIXED_IDX16K 1
44 #define MTRR_I686_FIXED_IDX4K 3
45
46 #define MTRR_I686_NVAR 8
47
48 #define MTRR_I686_64K_START 0x00000
49 #define MTRR_I686_16K_START 0x80000
50 #define MTRR_I686_4K_START 0xc0000
51
52 #define MTRR_I686_NFIXED_64K 1
53 #define MTRR_I686_NFIXED_16K 2
54 #define MTRR_I686_NFIXED_4K 8
55 #define MTRR_I686_NFIXED 11
56 #define MTRR_I686_NFIXED_SOFT_64K (MTRR_I686_NFIXED_64K * 8)
57 #define MTRR_I686_NFIXED_SOFT_16K (MTRR_I686_NFIXED_16K * 8)
58 #define MTRR_I686_NFIXED_SOFT_4K (MTRR_I686_NFIXED_4K * 8)
59 #define MTRR_I686_NFIXED_SOFT (MTRR_I686_NFIXED * 8)
60
61 #define MTRR_I686_ENABLE_MASK 0x0800
62 #define MTRR_I686_FIXED_ENABLE_MASK 0x0400
63
64 #define MTRR_I686_CAP_VCNT_MASK 0x00ff
65 #define MTRR_I686_CAP_FIX_MASK 0x0100
66 #define MTRR_I686_CAP_WC_MASK 0x0400
67
68 #define MTRR_TYPE_UC 0
69 #define MTRR_TYPE_WC 1
70 #define MTRR_TYPE_UNDEF1 2
71 #define MTRR_TYPE_UNDEF2 3
72 #define MTRR_TYPE_WT 4
73 #define MTRR_TYPE_WP 5
74 #define MTRR_TYPE_WB 6
75
76 struct mtrr_state {
77 uint32_t msraddr;
78 uint64_t msrval;
79 };
80
81 #define MTRR_PRIVATE 0x0001 /* 'own' range, reset at exit */
82 #define MTRR_FIXED 0x0002 /* use fixed range mtrr */
83 #define MTRR_VALID 0x0004 /* entry is valid */
84
85 #define MTRR_CANTSET MTRR_FIXED
86
87 #define MTRR_I686_MASK_VALID (1 << 11)
88
89 /*
90 * AMD K6 MTRRs.
91 *
92 * There are two of these MTRR-like registers in the UWCRR.
93 */
94
95 #define MTRR_K6_ADDR_SHIFT 17
96 #define MTRR_K6_ADDR (0x7fffU << MTRR_K6_ADDR_SHIFT)
97 #define MTRR_K6_MASK_SHIFT 2
98 #define MTRR_K6_MASK (0x7fffU << MTRR_K6_MASK_SHIFT)
99 #define MTRR_K6_WC (1U << 1) /* write-combine */
100 #define MTRR_K6_UC (1U << 0) /* uncached */
101
102 #define MTRR_K6_NVAR 2
103
104 #ifdef _KERNEL
105
106 #define mtrr_base_value(mtrrp) \
107 (((uint64_t)(mtrrp)->base) | ((uint64_t)(mtrrp)->type))
108 #define mtrr_mask_value(mtrrp) \
109 ((~((mtrrp)->len - 1) & 0x0000000ffffff000LL))
110
111
112 #define mtrr_len(val) \
113 ((~((val) & 0x0000000ffffff000LL)+1) & 0x0000000ffffff000LL)
114 #define mtrr_base(val) ((val) & 0x0000000ffffff000LL)
115 #define mtrr_type(val) ((uint8_t)((val) & 0x00000000000000ffLL))
116 #define mtrr_valid(val) (((val) & MTRR_I686_MASK_VALID) != 0)
117
118 struct proc;
119 struct mtrr;
120
121 void i686_mtrr_init_first(void);
122 void k6_mtrr_init_first(void);
123
124 struct mtrr_funcs {
125 void (*init_cpu)(struct cpu_info *ci);
126 void (*reload_cpu)(struct cpu_info *ci);
127 void (*clean)(struct proc *p);
128 int (*set)(struct mtrr *, int *n, struct proc *p, int flags);
129 int (*get)(struct mtrr *, int *n, struct proc *p, int flags);
130 void (*commit)(void);
131 void (*dump)(const char *tag);
132 };
133
134 extern struct mtrr_funcs i686_mtrr_funcs;
135 extern struct mtrr_funcs k6_mtrr_funcs;
136 extern struct mtrr_funcs *mtrr_funcs;
137
138 #define mtrr_init_cpu(ci) mtrr_funcs->init_cpu(ci)
139 #define mtrr_reload_cpu(ci) mtrr_funcs->reload_cpu(ci)
140 #define mtrr_clean(p) mtrr_funcs->clean(p)
141 #define mtrr_set(mp,n,p,f) mtrr_funcs->set(mp,n,p,f)
142 #define mtrr_get(mp,n,p,f) mtrr_funcs->get(mp,n,p,f)
143 #define mtrr_dump(s) mtrr_funcs->dump(s)
144 #define mtrr_commit() mtrr_funcs->commit()
145
146 #define MTRR_GETSET_USER 0x0001
147 #define MTRR_GETSET_KERNEL 0x0002
148
149 #endif /* _KERNEL */
150
151 struct mtrr {
152 uint64_t base; /* physical base address */
153 uint64_t len;
154 uint8_t type;
155 int flags;
156 pid_t owner; /* valid if MTRR_PRIVATE set in flags */
157 };
158
159 #endif /* _X86_MTRR_H_ */
160