blob: ac3a2b11eb38697a74608b08fbc29c97984d9d96 [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{
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700164 u8 mtrr_type;
165
Venki Pallipadic26421d2008-05-29 12:01:44 -0700166 /*
167 * We return the PAT request directly for types where PAT takes
168 * precedence with respect to MTRR and for UC_MINUS.
169 * Consistency checks with other PAT requests is done later
170 * while going through memtype list.
171 */
Hugh Dickins6cf514f2008-06-16 18:42:43 +0100172 if (req_type == _PAGE_CACHE_WC ||
173 req_type == _PAGE_CACHE_UC_MINUS ||
174 req_type == _PAGE_CACHE_UC)
175 return req_type;
Venki Pallipadic26421d2008-05-29 12:01:44 -0700176
177 /*
178 * Look for MTRR hint to get the effective type in case where PAT
179 * request is for WB.
180 */
181 mtrr_type = mtrr_type_lookup(start, end);
Hugh Dickins6cf514f2008-06-16 18:42:43 +0100182 if (mtrr_type == MTRR_TYPE_UNCACHABLE)
183 return _PAGE_CACHE_UC;
184 if (mtrr_type == MTRR_TYPE_WRCOMB)
185 return _PAGE_CACHE_WC;
186 return _PAGE_CACHE_WB;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700187}
188
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700189/*
190 * req_type typically has one of the:
191 * - _PAGE_CACHE_WB
192 * - _PAGE_CACHE_WC
193 * - _PAGE_CACHE_UC_MINUS
194 * - _PAGE_CACHE_UC
195 *
196 * req_type will have a special case value '-1', when requester want to inherit
197 * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
198 *
199 * If ret_type is NULL, function will return an error if it cannot reserve the
200 * region with req_type. If ret_type is non-null, function will return
201 * available type in ret_type in case of no error. In case of any error
202 * it will return a negative return value.
203 */
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700204int reserve_memtype(u64 start, u64 end, unsigned long req_type,
205 unsigned long *ret_type)
206{
207 struct memtype *new_entry = NULL;
208 struct memtype *parse;
209 unsigned long actual_type;
210 int err = 0;
211
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200212 /* Only track when pat_enabled */
213 if (!pat_enabled) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700214 /* This is identical to page table setting without PAT */
215 if (ret_type) {
216 if (req_type == -1) {
217 *ret_type = _PAGE_CACHE_WB;
218 } else {
Hugh Dickins6cf514f2008-06-16 18:42:43 +0100219 *ret_type = req_type & _PAGE_CACHE_MASK;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700220 }
221 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700222 return 0;
223 }
224
225 /* Low ISA region is always mapped WB in page table. No need to track */
226 if (start >= ISA_START_ADDRESS && (end - 1) <= ISA_END_ADDRESS) {
227 if (ret_type)
228 *ret_type = _PAGE_CACHE_WB;
229
230 return 0;
231 }
232
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700233 if (req_type == -1) {
234 /*
Venki Pallipadic26421d2008-05-29 12:01:44 -0700235 * Call mtrr_lookup to get the type hint. This is an
236 * optimization for /dev/mem mmap'ers into WB memory (BIOS
237 * tools and ACPI tools). Use WB request for WB memory and use
238 * UC_MINUS otherwise.
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700239 */
240 u8 mtrr_type = mtrr_type_lookup(start, end);
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700241
242 if (mtrr_type == MTRR_TYPE_WRBACK) {
243 req_type = _PAGE_CACHE_WB;
244 actual_type = _PAGE_CACHE_WB;
245 } else {
246 req_type = _PAGE_CACHE_UC_MINUS;
247 actual_type = _PAGE_CACHE_UC_MINUS;
248 }
249 } else {
250 req_type &= _PAGE_CACHE_MASK;
Hugh Dickins6cf514f2008-06-16 18:42:43 +0100251 actual_type = pat_x_mtrr_type(start, end, req_type);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700252 }
253
254 new_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
255 if (!new_entry)
256 return -ENOMEM;
257
258 new_entry->start = start;
259 new_entry->end = end;
260 new_entry->type = actual_type;
261
262 if (ret_type)
263 *ret_type = actual_type;
264
265 spin_lock(&memtype_lock);
266
267 /* Search for existing mapping that overlaps the current range */
268 list_for_each_entry(parse, &memtype_list, nd) {
269 struct memtype *saved_ptr;
270
271 if (parse->start >= end) {
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700272 dprintk("New Entry\n");
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700273 list_add(&new_entry->nd, parse->nd.prev);
274 new_entry = NULL;
275 break;
276 }
277
278 if (start <= parse->start && end >= parse->start) {
279 if (actual_type != parse->type && ret_type) {
280 actual_type = parse->type;
281 *ret_type = actual_type;
282 new_entry->type = actual_type;
283 }
284
285 if (actual_type != parse->type) {
286 printk(
287 KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
288 current->comm, current->pid,
289 start, end,
290 cattr_name(actual_type),
291 cattr_name(parse->type));
292 err = -EBUSY;
293 break;
294 }
295
296 saved_ptr = parse;
297 /*
298 * Check to see whether the request overlaps more
299 * than one entry in the list
300 */
301 list_for_each_entry_continue(parse, &memtype_list, nd) {
302 if (end <= parse->start) {
303 break;
304 }
305
306 if (actual_type != parse->type) {
307 printk(
308 KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
309 current->comm, current->pid,
310 start, end,
311 cattr_name(actual_type),
312 cattr_name(parse->type));
313 err = -EBUSY;
314 break;
315 }
316 }
317
318 if (err) {
319 break;
320 }
321
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700322 dprintk("Overlap at 0x%Lx-0x%Lx\n",
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700323 saved_ptr->start, saved_ptr->end);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700324 /* No conflict. Go ahead and add this new entry */
325 list_add(&new_entry->nd, saved_ptr->nd.prev);
326 new_entry = NULL;
327 break;
328 }
329
330 if (start < parse->end) {
331 if (actual_type != parse->type && ret_type) {
332 actual_type = parse->type;
333 *ret_type = actual_type;
334 new_entry->type = actual_type;
335 }
336
337 if (actual_type != parse->type) {
338 printk(
339 KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
340 current->comm, current->pid,
341 start, end,
342 cattr_name(actual_type),
343 cattr_name(parse->type));
344 err = -EBUSY;
345 break;
346 }
347
348 saved_ptr = parse;
349 /*
350 * Check to see whether the request overlaps more
351 * than one entry in the list
352 */
353 list_for_each_entry_continue(parse, &memtype_list, nd) {
354 if (end <= parse->start) {
355 break;
356 }
357
358 if (actual_type != parse->type) {
359 printk(
360 KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
361 current->comm, current->pid,
362 start, end,
363 cattr_name(actual_type),
364 cattr_name(parse->type));
365 err = -EBUSY;
366 break;
367 }
368 }
369
370 if (err) {
371 break;
372 }
373
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700374 dprintk("Overlap at 0x%Lx-0x%Lx\n",
Linus Torvalds86cf02f2008-04-27 11:59:30 -0700375 saved_ptr->start, saved_ptr->end);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700376 /* No conflict. Go ahead and add this new entry */
377 list_add(&new_entry->nd, &saved_ptr->nd);
378 new_entry = NULL;
379 break;
380 }
381 }
382
383 if (err) {
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200384 printk(KERN_INFO
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700385 "reserve_memtype failed 0x%Lx-0x%Lx, track %s, req %s\n",
386 start, end, cattr_name(new_entry->type),
387 cattr_name(req_type));
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700388 kfree(new_entry);
389 spin_unlock(&memtype_lock);
390 return err;
391 }
392
393 if (new_entry) {
394 /* No conflict. Not yet added to the list. Add to the tail */
395 list_add_tail(&new_entry->nd, &memtype_list);
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700396 dprintk("New Entry\n");
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200397 }
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700398
399 if (ret_type) {
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700400 dprintk(
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700401 "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
402 start, end, cattr_name(actual_type),
403 cattr_name(req_type), cattr_name(*ret_type));
404 } else {
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700405 dprintk(
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700406 "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s\n",
407 start, end, cattr_name(actual_type),
408 cattr_name(req_type));
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700409 }
410
411 spin_unlock(&memtype_lock);
412 return err;
413}
414
415int free_memtype(u64 start, u64 end)
416{
417 struct memtype *ml;
418 int err = -EINVAL;
419
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200420 /* Only track when pat_enabled */
421 if (!pat_enabled) {
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700422 return 0;
423 }
424
425 /* Low ISA region is always mapped WB. No need to track */
426 if (start >= ISA_START_ADDRESS && end <= ISA_END_ADDRESS) {
427 return 0;
428 }
429
430 spin_lock(&memtype_lock);
431 list_for_each_entry(ml, &memtype_list, nd) {
432 if (ml->start == start && ml->end == end) {
433 list_del(&ml->nd);
434 kfree(ml);
435 err = 0;
436 break;
437 }
438 }
439 spin_unlock(&memtype_lock);
440
441 if (err) {
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200442 printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700443 current->comm, current->pid, start, end);
444 }
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700445
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700446 dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700447 return err;
448}
449
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700450
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700451/*
452 * /dev/mem mmap interface. The memtype used for mapping varies:
453 * - Use UC for mappings with O_SYNC flag
454 * - Without O_SYNC flag, if there is any conflict in reserve_memtype,
455 * inherit the memtype from existing mapping.
456 * - Else use UC_MINUS memtype (for backward compatibility with existing
457 * X drivers.
458 */
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700459pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
460 unsigned long size, pgprot_t vma_prot)
461{
462 return vma_prot;
463}
464
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700465#ifdef CONFIG_NONPROMISC_DEVMEM
466/* This check is done in drivers/char/mem.c in case of NONPROMISC_DEVMEM*/
467static inline int range_is_allowed(unsigned long pfn, unsigned long size)
468{
469 return 1;
470}
471#else
472static inline int range_is_allowed(unsigned long pfn, unsigned long size)
473{
474 u64 from = ((u64)pfn) << PAGE_SHIFT;
475 u64 to = from + size;
476 u64 cursor = from;
477
478 while (cursor < to) {
479 if (!devmem_is_allowed(pfn)) {
480 printk(KERN_INFO
481 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
482 current->comm, from, to);
483 return 0;
484 }
485 cursor += PAGE_SIZE;
486 pfn++;
487 }
488 return 1;
489}
490#endif /* CONFIG_NONPROMISC_DEVMEM */
491
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700492int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
493 unsigned long size, pgprot_t *vma_prot)
494{
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700495 u64 offset = ((u64) pfn) << PAGE_SHIFT;
496 unsigned long flags = _PAGE_CACHE_UC_MINUS;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700497 int retval;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700498
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700499 if (!range_is_allowed(pfn, size))
500 return 0;
501
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700502 if (file->f_flags & O_SYNC) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700503 flags = _PAGE_CACHE_UC;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700504 }
505
506#ifdef CONFIG_X86_32
507 /*
508 * On the PPro and successors, the MTRRs are used to set
509 * memory types for physical addresses outside main memory,
510 * so blindly setting UC or PWT on those pages is wrong.
511 * For Pentiums and earlier, the surround logic should disable
512 * caching for the high addresses through the KEN pin, but
513 * we maintain the tradition of paranoia in this code.
514 */
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200515 if (!pat_enabled &&
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200516 !(boot_cpu_has(X86_FEATURE_MTRR) ||
517 boot_cpu_has(X86_FEATURE_K6_MTRR) ||
518 boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
519 boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
520 (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700521 flags = _PAGE_CACHE_UC;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700522 }
523#endif
524
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700525 /*
526 * With O_SYNC, we can only take UC mapping. Fail if we cannot.
527 * Without O_SYNC, we want to get
528 * - WB for WB-able memory and no other conflicting mappings
529 * - UC_MINUS for non-WB-able memory with no other conflicting mappings
530 * - Inherit from confliting mappings otherwise
531 */
532 if (flags != _PAGE_CACHE_UC_MINUS) {
533 retval = reserve_memtype(offset, offset + size, flags, NULL);
534 } else {
Ingo Molnarf022bfd2008-03-21 15:42:28 +0100535 retval = reserve_memtype(offset, offset + size, -1, &flags);
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700536 }
537
538 if (retval < 0)
539 return 0;
540
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700541 if (pfn <= max_pfn_mapped &&
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200542 ioremap_change_attr((unsigned long)__va(offset), size, flags) < 0) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700543 free_memtype(offset, offset + size);
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200544 printk(KERN_INFO
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700545 "%s:%d /dev/mem ioremap_change_attr failed %s for %Lx-%Lx\n",
546 current->comm, current->pid,
547 cattr_name(flags),
Pranith Kumarafc85342008-05-12 14:52:26 +0530548 offset, (unsigned long long)(offset + size));
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700549 return 0;
550 }
551
552 *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
553 flags);
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700554 return 1;
555}
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700556
557void map_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
558{
559 u64 addr = (u64)pfn << PAGE_SHIFT;
560 unsigned long flags;
561 unsigned long want_flags = (pgprot_val(vma_prot) & _PAGE_CACHE_MASK);
562
563 reserve_memtype(addr, addr + size, want_flags, &flags);
564 if (flags != want_flags) {
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200565 printk(KERN_INFO
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700566 "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n",
567 current->comm, current->pid,
568 cattr_name(want_flags),
Pranith Kumarafc85342008-05-12 14:52:26 +0530569 addr, (unsigned long long)(addr + size),
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700570 cattr_name(flags));
571 }
572}
573
574void unmap_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
575{
576 u64 addr = (u64)pfn << PAGE_SHIFT;
577
578 free_memtype(addr, addr + size);
579}