blob: 49dcd9652ec8a58062d38725dae4f7f1d4c55ddc [file] [log] [blame]
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -07001/*
2 * Handle caching attributes in page tables (PAT)
3 *
4 * Authors: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Suresh B Siddha <suresh.b.siddha@intel.com>
6 *
7 * Loosely based on earlier PAT patchset from Eric Biederman and Andi Kleen.
8 */
9
10#include <linux/mm.h>
11#include <linux/kernel.h>
12#include <linux/gfp.h>
13#include <linux/fs.h>
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -070014#include <linux/bootmem.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070015
16#include <asm/msr.h>
17#include <asm/tlbflush.h>
18#include <asm/processor.h>
Venki Pallipadi0124cec2008-04-26 11:32:12 -070019#include <asm/page.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070020#include <asm/pgtable.h>
21#include <asm/pat.h>
22#include <asm/e820.h>
23#include <asm/cacheflush.h>
24#include <asm/fcntl.h>
25#include <asm/mtrr.h>
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -070026#include <asm/io.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070027
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020028#ifdef CONFIG_X86_PAT
Andreas Herrmann499f8f82008-06-10 16:06:21 +020029int __read_mostly pat_enabled = 1;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070030
Avi Kivity31f4d872008-05-14 12:20:32 +030031void __cpuinit pat_disable(char *reason)
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020032{
Andreas Herrmann499f8f82008-06-10 16:06:21 +020033 pat_enabled = 0;
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020034 printk(KERN_INFO "%s\n", reason);
35}
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070036
Andrew Mortonbe524fb2008-05-29 00:01:28 -070037static int __init nopat(char *str)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070038{
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020039 pat_disable("PAT support disabled.");
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070040 return 0;
41}
42early_param("nopat", nopat);
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020043#endif
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070044
Venki Pallipadi77b52b42008-05-05 19:09:10 -070045
46static int debug_enable;
47static int __init pat_debug_setup(char *str)
48{
49 debug_enable = 1;
50 return 0;
51}
52__setup("debugpat", pat_debug_setup);
53
54#define dprintk(fmt, arg...) \
55 do { if (debug_enable) printk(KERN_INFO fmt, ##arg); } while (0)
56
57
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020058static u64 __read_mostly boot_pat_state;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070059
60enum {
61 PAT_UC = 0, /* uncached */
62 PAT_WC = 1, /* Write combining */
63 PAT_WT = 4, /* Write Through */
64 PAT_WP = 5, /* Write Protected */
65 PAT_WB = 6, /* Write Back (default) */
66 PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */
67};
68
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +020069#define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070070
71void pat_init(void)
72{
73 u64 pat;
74
Andreas Herrmann499f8f82008-06-10 16:06:21 +020075 if (!pat_enabled)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070076 return;
77
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020078 /* Paranoia check. */
Andreas Herrmann97cfab62008-06-10 16:05:18 +020079 if (!cpu_has_pat && boot_pat_state) {
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020080 /*
Andreas Herrmann97cfab62008-06-10 16:05:18 +020081 * If this happens we are on a secondary CPU, but
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020082 * switched to PAT on the boot CPU. We have no way to
83 * undo PAT.
Andreas Herrmann97cfab62008-06-10 16:05:18 +020084 */
85 printk(KERN_ERR "PAT enabled, "
86 "but not supported by secondary CPU\n");
87 BUG();
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020088 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070089
90 /* Set PWT to Write-Combining. All other bits stay the same */
91 /*
92 * PTE encoding used in Linux:
93 * PAT
94 * |PCD
95 * ||PWT
96 * |||
97 * 000 WB _PAGE_CACHE_WB
98 * 001 WC _PAGE_CACHE_WC
99 * 010 UC- _PAGE_CACHE_UC_MINUS
100 * 011 UC _PAGE_CACHE_UC
101 * PAT bit unused
102 */
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200103 pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
104 PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, UC);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700105
106 /* Boot CPU check */
Thomas Gleixner8d4a4302008-05-08 09:18:43 +0200107 if (!boot_pat_state)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700108 rdmsrl(MSR_IA32_CR_PAT, boot_pat_state);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700109
110 wrmsrl(MSR_IA32_CR_PAT, pat);
111 printk(KERN_INFO "x86 PAT enabled: cpu %d, old 0x%Lx, new 0x%Lx\n",
112 smp_processor_id(), boot_pat_state, pat);
113}
114
115#undef PAT
116
117static char *cattr_name(unsigned long flags)
118{
119 switch (flags & _PAGE_CACHE_MASK) {
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200120 case _PAGE_CACHE_UC: return "uncached";
121 case _PAGE_CACHE_UC_MINUS: return "uncached-minus";
122 case _PAGE_CACHE_WB: return "write-back";
123 case _PAGE_CACHE_WC: return "write-combining";
124 default: return "broken";
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700125 }
126}
127
128/*
129 * The global memtype list keeps track of memory type for specific
130 * physical memory areas. Conflicting memory types in different
131 * mappings can cause CPU cache corruption. To avoid this we keep track.
132 *
133 * The list is sorted based on starting address and can contain multiple
134 * entries for each address (this allows reference counting for overlapping
135 * areas). All the aliases have the same cache attributes of course.
136 * Zero attributes are represented as holes.
137 *
138 * Currently the data structure is a list because the number of mappings
139 * are expected to be relatively small. If this should be a problem
140 * it could be changed to a rbtree or similar.
141 *
142 * memtype_lock protects the whole list.
143 */
144
145struct memtype {
146 u64 start;
147 u64 end;
148 unsigned long type;
149 struct list_head nd;
150};
151
152static LIST_HEAD(memtype_list);
153static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */
154
155/*
156 * Does intersection of PAT memory type and MTRR memory type and returns
157 * the resulting memory type as PAT understands it.
158 * (Type in pat and mtrr will not have same value)
159 * The intersection is based on "Effective Memory Type" tables in IA-32
160 * SDM vol 3a
161 */
Hugh Dickins6cf514f2008-06-16 18:42:43 +0100162static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700163{
Venki Pallipadic26421d2008-05-29 12:01:44 -0700164 /*
165 * Look for MTRR hint to get the effective type in case where PAT
166 * request is for WB.
167 */
Andreas Herrmanndd0c7c42008-06-18 15:38:57 +0200168 if (req_type == _PAGE_CACHE_WB) {
169 u8 mtrr_type;
170
171 mtrr_type = mtrr_type_lookup(start, end);
172 if (mtrr_type == MTRR_TYPE_UNCACHABLE)
173 return _PAGE_CACHE_UC;
174 if (mtrr_type == MTRR_TYPE_WRCOMB)
175 return _PAGE_CACHE_WC;
176 }
177
178 return req_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700179}
180
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700181/*
182 * req_type typically has one of the:
183 * - _PAGE_CACHE_WB
184 * - _PAGE_CACHE_WC
185 * - _PAGE_CACHE_UC_MINUS
186 * - _PAGE_CACHE_UC
187 *
188 * req_type will have a special case value '-1', when requester want to inherit
189 * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
190 *
Andreas Herrmannac979912008-06-20 22:01:49 +0200191 * If new_type is NULL, function will return an error if it cannot reserve the
192 * region with req_type. If new_type is non-NULL, function will return
193 * available type in new_type in case of no error. In case of any error
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700194 * it will return a negative return value.
195 */
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700196int reserve_memtype(u64 start, u64 end, unsigned long req_type,
Andreas Herrmannac979912008-06-20 22:01:49 +0200197 unsigned long *new_type)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700198{
Andreas Herrmannac979912008-06-20 22:01:49 +0200199 struct memtype *new, *entry;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700200 unsigned long actual_type;
Andreas Herrmannf6887262008-06-20 22:05:37 +0200201 struct list_head *where;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700202 int err = 0;
203
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200204 BUG_ON(start >= end); /* end is exclusive */
205
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200206 if (!pat_enabled) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700207 /* This is identical to page table setting without PAT */
Andreas Herrmannac979912008-06-20 22:01:49 +0200208 if (new_type) {
209 if (req_type == -1)
210 *new_type = _PAGE_CACHE_WB;
211 else
212 *new_type = req_type & _PAGE_CACHE_MASK;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700213 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700214 return 0;
215 }
216
217 /* Low ISA region is always mapped WB in page table. No need to track */
Andreas Herrmannbcc643d2008-06-20 21:58:46 +0200218 if (is_ISA_range(start, end - 1)) {
Andreas Herrmannac979912008-06-20 22:01:49 +0200219 if (new_type)
220 *new_type = _PAGE_CACHE_WB;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700221 return 0;
222 }
223
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700224 if (req_type == -1) {
225 /*
Venki Pallipadic26421d2008-05-29 12:01:44 -0700226 * Call mtrr_lookup to get the type hint. This is an
227 * optimization for /dev/mem mmap'ers into WB memory (BIOS
228 * tools and ACPI tools). Use WB request for WB memory and use
229 * UC_MINUS otherwise.
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700230 */
231 u8 mtrr_type = mtrr_type_lookup(start, end);
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700232
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200233 if (mtrr_type == MTRR_TYPE_WRBACK)
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700234 actual_type = _PAGE_CACHE_WB;
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200235 else
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700236 actual_type = _PAGE_CACHE_UC_MINUS;
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200237 } else
238 actual_type = pat_x_mtrr_type(start, end,
239 req_type & _PAGE_CACHE_MASK);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700240
Andreas Herrmannac979912008-06-20 22:01:49 +0200241 new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
242 if (!new)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700243 return -ENOMEM;
244
Andreas Herrmannac979912008-06-20 22:01:49 +0200245 new->start = start;
246 new->end = end;
247 new->type = actual_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700248
Andreas Herrmannac979912008-06-20 22:01:49 +0200249 if (new_type)
250 *new_type = actual_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700251
252 spin_lock(&memtype_lock);
253
254 /* Search for existing mapping that overlaps the current range */
Andreas Herrmannf6887262008-06-20 22:05:37 +0200255 where = NULL;
Andreas Herrmannac979912008-06-20 22:01:49 +0200256 list_for_each_entry(entry, &memtype_list, nd) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700257 struct memtype *saved_ptr;
258
Andreas Herrmannac979912008-06-20 22:01:49 +0200259 if (entry->start >= end) {
Andreas Herrmannf6887262008-06-20 22:05:37 +0200260 where = entry->nd.prev;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700261 break;
262 }
263
Andreas Herrmannac979912008-06-20 22:01:49 +0200264 if (start <= entry->start && end >= entry->start) {
265 if (actual_type != entry->type && new_type) {
266 actual_type = entry->type;
267 *new_type = actual_type;
268 new->type = actual_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700269 }
270
Andreas Herrmannac979912008-06-20 22:01:49 +0200271 if (actual_type != entry->type) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700272 err = -EBUSY;
273 break;
274 }
275
Andreas Herrmannac979912008-06-20 22:01:49 +0200276 saved_ptr = entry;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700277 /*
278 * Check to see whether the request overlaps more
279 * than one entry in the list
280 */
Andreas Herrmannac979912008-06-20 22:01:49 +0200281 list_for_each_entry_continue(entry, &memtype_list, nd) {
282 if (end <= entry->start) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700283 break;
284 }
285
Andreas Herrmannac979912008-06-20 22:01:49 +0200286 if (actual_type != entry->type) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700287 err = -EBUSY;
288 break;
289 }
290 }
291
292 if (err) {
293 break;
294 }
295
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700296 dprintk("Overlap at 0x%Lx-0x%Lx\n",
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700297 saved_ptr->start, saved_ptr->end);
Andreas Herrmannf6887262008-06-20 22:05:37 +0200298 where = saved_ptr->nd.prev;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700299 break;
300 }
301
Andreas Herrmannac979912008-06-20 22:01:49 +0200302 if (start < entry->end) {
303 if (actual_type != entry->type && new_type) {
304 actual_type = entry->type;
305 *new_type = actual_type;
306 new->type = actual_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700307 }
308
Andreas Herrmannac979912008-06-20 22:01:49 +0200309 if (actual_type != entry->type) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700310 err = -EBUSY;
311 break;
312 }
313
Andreas Herrmannac979912008-06-20 22:01:49 +0200314 saved_ptr = entry;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700315 /*
316 * Check to see whether the request overlaps more
317 * than one entry in the list
318 */
Andreas Herrmannac979912008-06-20 22:01:49 +0200319 list_for_each_entry_continue(entry, &memtype_list, nd) {
320 if (end <= entry->start) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700321 break;
322 }
323
Andreas Herrmannac979912008-06-20 22:01:49 +0200324 if (actual_type != entry->type) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700325 err = -EBUSY;
326 break;
327 }
328 }
329
330 if (err) {
331 break;
332 }
333
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700334 dprintk("Overlap at 0x%Lx-0x%Lx\n",
Linus Torvalds86cf02f2008-04-27 11:59:30 -0700335 saved_ptr->start, saved_ptr->end);
Andreas Herrmannf6887262008-06-20 22:05:37 +0200336 where = &saved_ptr->nd;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700337 break;
338 }
339 }
340
341 if (err) {
Andreas Herrmann3e9c83b2008-06-20 22:04:02 +0200342 printk(KERN_INFO "%s:%d conflicting memory types "
343 "%Lx-%Lx %s<->%s\n", current->comm, current->pid, start,
344 end, cattr_name(new->type), cattr_name(entry->type));
345 printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
346 "track %s, req %s\n",
347 start, end, cattr_name(new->type), cattr_name(req_type));
Andreas Herrmannac979912008-06-20 22:01:49 +0200348 kfree(new);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700349 spin_unlock(&memtype_lock);
350 return err;
351 }
352
Andreas Herrmannf6887262008-06-20 22:05:37 +0200353 if (where)
354 list_add(&new->nd, where);
355 else
Andreas Herrmannac979912008-06-20 22:01:49 +0200356 list_add_tail(&new->nd, &memtype_list);
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700357
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700358 spin_unlock(&memtype_lock);
Andreas Herrmann3e9c83b2008-06-20 22:04:02 +0200359
360 dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
361 start, end, cattr_name(new->type), cattr_name(req_type),
362 new_type ? cattr_name(*new_type) : "-");
363
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700364 return err;
365}
366
367int free_memtype(u64 start, u64 end)
368{
Andreas Herrmannac979912008-06-20 22:01:49 +0200369 struct memtype *entry;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700370 int err = -EINVAL;
371
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200372 if (!pat_enabled)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700373 return 0;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700374
375 /* Low ISA region is always mapped WB. No need to track */
Andreas Herrmannbcc643d2008-06-20 21:58:46 +0200376 if (is_ISA_range(start, end - 1))
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700377 return 0;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700378
379 spin_lock(&memtype_lock);
Andreas Herrmannac979912008-06-20 22:01:49 +0200380 list_for_each_entry(entry, &memtype_list, nd) {
381 if (entry->start == start && entry->end == end) {
382 list_del(&entry->nd);
383 kfree(entry);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700384 err = 0;
385 break;
386 }
387 }
388 spin_unlock(&memtype_lock);
389
390 if (err) {
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200391 printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700392 current->comm, current->pid, start, end);
393 }
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700394
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700395 dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700396 return err;
397}
398
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700399
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700400/*
401 * /dev/mem mmap interface. The memtype used for mapping varies:
402 * - Use UC for mappings with O_SYNC flag
403 * - Without O_SYNC flag, if there is any conflict in reserve_memtype,
404 * inherit the memtype from existing mapping.
405 * - Else use UC_MINUS memtype (for backward compatibility with existing
406 * X drivers.
407 */
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700408pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
409 unsigned long size, pgprot_t vma_prot)
410{
411 return vma_prot;
412}
413
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700414#ifdef CONFIG_NONPROMISC_DEVMEM
415/* This check is done in drivers/char/mem.c in case of NONPROMISC_DEVMEM*/
416static inline int range_is_allowed(unsigned long pfn, unsigned long size)
417{
418 return 1;
419}
420#else
421static inline int range_is_allowed(unsigned long pfn, unsigned long size)
422{
423 u64 from = ((u64)pfn) << PAGE_SHIFT;
424 u64 to = from + size;
425 u64 cursor = from;
426
427 while (cursor < to) {
428 if (!devmem_is_allowed(pfn)) {
429 printk(KERN_INFO
430 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
431 current->comm, from, to);
432 return 0;
433 }
434 cursor += PAGE_SIZE;
435 pfn++;
436 }
437 return 1;
438}
439#endif /* CONFIG_NONPROMISC_DEVMEM */
440
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700441int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
442 unsigned long size, pgprot_t *vma_prot)
443{
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700444 u64 offset = ((u64) pfn) << PAGE_SHIFT;
445 unsigned long flags = _PAGE_CACHE_UC_MINUS;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700446 int retval;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700447
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700448 if (!range_is_allowed(pfn, size))
449 return 0;
450
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700451 if (file->f_flags & O_SYNC) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700452 flags = _PAGE_CACHE_UC;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700453 }
454
455#ifdef CONFIG_X86_32
456 /*
457 * On the PPro and successors, the MTRRs are used to set
458 * memory types for physical addresses outside main memory,
459 * so blindly setting UC or PWT on those pages is wrong.
460 * For Pentiums and earlier, the surround logic should disable
461 * caching for the high addresses through the KEN pin, but
462 * we maintain the tradition of paranoia in this code.
463 */
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200464 if (!pat_enabled &&
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200465 !(boot_cpu_has(X86_FEATURE_MTRR) ||
466 boot_cpu_has(X86_FEATURE_K6_MTRR) ||
467 boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
468 boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
469 (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700470 flags = _PAGE_CACHE_UC;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700471 }
472#endif
473
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700474 /*
475 * With O_SYNC, we can only take UC mapping. Fail if we cannot.
476 * Without O_SYNC, we want to get
477 * - WB for WB-able memory and no other conflicting mappings
478 * - UC_MINUS for non-WB-able memory with no other conflicting mappings
479 * - Inherit from confliting mappings otherwise
480 */
481 if (flags != _PAGE_CACHE_UC_MINUS) {
482 retval = reserve_memtype(offset, offset + size, flags, NULL);
483 } else {
Ingo Molnarf022bfd2008-03-21 15:42:28 +0100484 retval = reserve_memtype(offset, offset + size, -1, &flags);
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700485 }
486
487 if (retval < 0)
488 return 0;
489
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700490 if (pfn <= max_pfn_mapped &&
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200491 ioremap_change_attr((unsigned long)__va(offset), size, flags) < 0) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700492 free_memtype(offset, offset + size);
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200493 printk(KERN_INFO
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700494 "%s:%d /dev/mem ioremap_change_attr failed %s for %Lx-%Lx\n",
495 current->comm, current->pid,
496 cattr_name(flags),
Pranith Kumarafc85342008-05-12 14:52:26 +0530497 offset, (unsigned long long)(offset + size));
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700498 return 0;
499 }
500
501 *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
502 flags);
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700503 return 1;
504}
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700505
506void map_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
507{
508 u64 addr = (u64)pfn << PAGE_SHIFT;
509 unsigned long flags;
510 unsigned long want_flags = (pgprot_val(vma_prot) & _PAGE_CACHE_MASK);
511
512 reserve_memtype(addr, addr + size, want_flags, &flags);
513 if (flags != want_flags) {
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200514 printk(KERN_INFO
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700515 "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n",
516 current->comm, current->pid,
517 cattr_name(want_flags),
Pranith Kumarafc85342008-05-12 14:52:26 +0530518 addr, (unsigned long long)(addr + size),
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700519 cattr_name(flags));
520 }
521}
522
523void unmap_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
524{
525 u64 addr = (u64)pfn << PAGE_SHIFT;
526
527 free_memtype(addr, addr + size);
528}