2 * Handle caching attributes in page tables (PAT)
4 * Authors: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Suresh B Siddha <suresh.b.siddha@intel.com>
7 * Loosely based on earlier PAT patchset from Eric Biederman and Andi Kleen.
11 #include <linux/kernel.h>
12 #include <linux/gfp.h>
14 #include <linux/bootmem.h>
17 #include <asm/tlbflush.h>
18 #include <asm/processor.h>
20 #include <asm/pgtable.h>
23 #include <asm/cacheflush.h>
24 #include <asm/fcntl.h>
29 int __read_mostly pat_enabled
= 1;
31 void __cpuinit
pat_disable(char *reason
)
34 printk(KERN_INFO
"%s\n", reason
);
37 static int __init
nopat(char *str
)
39 pat_disable("PAT support disabled.");
42 early_param("nopat", nopat
);
46 static int debug_enable
;
47 static int __init
pat_debug_setup(char *str
)
52 __setup("debugpat", pat_debug_setup
);
54 #define dprintk(fmt, arg...) \
55 do { if (debug_enable) printk(KERN_INFO fmt, ##arg); } while (0)
58 static u64 __read_mostly boot_pat_state
;
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 */
69 #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
79 if (!cpu_has_pat
&& boot_pat_state
) {
81 * If this happens we are on a secondary CPU, but
82 * switched to PAT on the boot CPU. We have no way to
85 printk(KERN_ERR
"PAT enabled, "
86 "but not supported by secondary CPU\n");
90 /* Set PWT to Write-Combining. All other bits stay the same */
92 * PTE encoding used in Linux:
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
103 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
);
108 rdmsrl(MSR_IA32_CR_PAT
, boot_pat_state
);
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
);
117 static char *cattr_name(unsigned long flags
)
119 switch (flags
& _PAGE_CACHE_MASK
) {
120 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";
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.
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.
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.
142 * memtype_lock protects the whole list.
152 static LIST_HEAD(memtype_list
);
153 static DEFINE_SPINLOCK(memtype_lock
); /* protects memtype list */
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
162 static unsigned long pat_x_mtrr_type(u64 start
, u64 end
, unsigned long req_type
)
165 * Look for MTRR hint to get the effective type in case where PAT
168 if (req_type
== _PAGE_CACHE_WB
) {
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
;
181 static int chk_conflict(struct memtype
*new, struct memtype
*entry
,
184 if (new->type
!= entry
->type
) {
186 new->type
= entry
->type
;
192 /* check overlaps with more than one entry in the list */
193 list_for_each_entry_continue(entry
, &memtype_list
, nd
) {
194 if (new->end
<= entry
->start
)
196 else if (new->type
!= entry
->type
)
202 printk(KERN_INFO
"%s:%d conflicting memory types "
203 "%Lx-%Lx %s<->%s\n", current
->comm
, current
->pid
, new->start
,
204 new->end
, cattr_name(new->type
), cattr_name(entry
->type
));
209 * req_type typically has one of the:
212 * - _PAGE_CACHE_UC_MINUS
215 * req_type will have a special case value '-1', when requester want to inherit
216 * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
218 * If new_type is NULL, function will return an error if it cannot reserve the
219 * region with req_type. If new_type is non-NULL, function will return
220 * available type in new_type in case of no error. In case of any error
221 * it will return a negative return value.
223 int reserve_memtype(u64 start
, u64 end
, unsigned long req_type
,
224 unsigned long *new_type
)
226 struct memtype
*new, *entry
;
227 unsigned long actual_type
;
228 struct list_head
*where
;
231 BUG_ON(start
>= end
); /* end is exclusive */
234 /* This is identical to page table setting without PAT */
237 *new_type
= _PAGE_CACHE_WB
;
239 *new_type
= req_type
& _PAGE_CACHE_MASK
;
244 /* Low ISA region is always mapped WB in page table. No need to track */
245 if (is_ISA_range(start
, end
- 1)) {
247 *new_type
= _PAGE_CACHE_WB
;
251 if (req_type
== -1) {
253 * Call mtrr_lookup to get the type hint. This is an
254 * optimization for /dev/mem mmap'ers into WB memory (BIOS
255 * tools and ACPI tools). Use WB request for WB memory and use
256 * UC_MINUS otherwise.
258 u8 mtrr_type
= mtrr_type_lookup(start
, end
);
260 if (mtrr_type
== MTRR_TYPE_WRBACK
)
261 actual_type
= _PAGE_CACHE_WB
;
263 actual_type
= _PAGE_CACHE_UC_MINUS
;
265 actual_type
= pat_x_mtrr_type(start
, end
,
266 req_type
& _PAGE_CACHE_MASK
);
268 new = kmalloc(sizeof(struct memtype
), GFP_KERNEL
);
274 new->type
= actual_type
;
277 *new_type
= actual_type
;
279 spin_lock(&memtype_lock
);
281 /* Search for existing mapping that overlaps the current range */
283 list_for_each_entry(entry
, &memtype_list
, nd
) {
284 if (entry
->start
>= end
) {
285 where
= entry
->nd
.prev
;
289 if (start
<= entry
->start
&& end
>= entry
->start
) {
290 err
= chk_conflict(new, entry
, new_type
);
295 dprintk("Overlap at 0x%Lx-0x%Lx\n",
296 entry
->start
, entry
->end
);
297 where
= entry
->nd
.prev
;
301 if (start
< entry
->end
) {
302 err
= chk_conflict(new, entry
, new_type
);
307 dprintk("Overlap at 0x%Lx-0x%Lx\n",
308 entry
->start
, entry
->end
);
315 printk(KERN_INFO
"reserve_memtype failed 0x%Lx-0x%Lx, "
316 "track %s, req %s\n",
317 start
, end
, cattr_name(new->type
), cattr_name(req_type
));
319 spin_unlock(&memtype_lock
);
324 list_add(&new->nd
, where
);
326 list_add_tail(&new->nd
, &memtype_list
);
328 spin_unlock(&memtype_lock
);
330 dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
331 start
, end
, cattr_name(new->type
), cattr_name(req_type
),
332 new_type
? cattr_name(*new_type
) : "-");
337 int free_memtype(u64 start
, u64 end
)
339 struct memtype
*entry
;
345 /* Low ISA region is always mapped WB. No need to track */
346 if (is_ISA_range(start
, end
- 1))
349 spin_lock(&memtype_lock
);
350 list_for_each_entry(entry
, &memtype_list
, nd
) {
351 if (entry
->start
== start
&& entry
->end
== end
) {
352 list_del(&entry
->nd
);
358 spin_unlock(&memtype_lock
);
361 printk(KERN_INFO
"%s:%d freeing invalid memtype %Lx-%Lx\n",
362 current
->comm
, current
->pid
, start
, end
);
365 dprintk("free_memtype request 0x%Lx-0x%Lx\n", start
, end
);
371 * /dev/mem mmap interface. The memtype used for mapping varies:
372 * - Use UC for mappings with O_SYNC flag
373 * - Without O_SYNC flag, if there is any conflict in reserve_memtype,
374 * inherit the memtype from existing mapping.
375 * - Else use UC_MINUS memtype (for backward compatibility with existing
378 pgprot_t
phys_mem_access_prot(struct file
*file
, unsigned long pfn
,
379 unsigned long size
, pgprot_t vma_prot
)
384 #ifdef CONFIG_NONPROMISC_DEVMEM
385 /* This check is done in drivers/char/mem.c in case of NONPROMISC_DEVMEM*/
386 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
391 static inline int range_is_allowed(unsigned long pfn
, unsigned long size
)
393 u64 from
= ((u64
)pfn
) << PAGE_SHIFT
;
394 u64 to
= from
+ size
;
397 while (cursor
< to
) {
398 if (!devmem_is_allowed(pfn
)) {
400 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
401 current
->comm
, from
, to
);
409 #endif /* CONFIG_NONPROMISC_DEVMEM */
411 int phys_mem_access_prot_allowed(struct file
*file
, unsigned long pfn
,
412 unsigned long size
, pgprot_t
*vma_prot
)
414 u64 offset
= ((u64
) pfn
) << PAGE_SHIFT
;
415 unsigned long flags
= _PAGE_CACHE_UC_MINUS
;
418 if (!range_is_allowed(pfn
, size
))
421 if (file
->f_flags
& O_SYNC
) {
422 flags
= _PAGE_CACHE_UC
;
427 * On the PPro and successors, the MTRRs are used to set
428 * memory types for physical addresses outside main memory,
429 * so blindly setting UC or PWT on those pages is wrong.
430 * For Pentiums and earlier, the surround logic should disable
431 * caching for the high addresses through the KEN pin, but
432 * we maintain the tradition of paranoia in this code.
435 !(boot_cpu_has(X86_FEATURE_MTRR
) ||
436 boot_cpu_has(X86_FEATURE_K6_MTRR
) ||
437 boot_cpu_has(X86_FEATURE_CYRIX_ARR
) ||
438 boot_cpu_has(X86_FEATURE_CENTAUR_MCR
)) &&
439 (pfn
<< PAGE_SHIFT
) >= __pa(high_memory
)) {
440 flags
= _PAGE_CACHE_UC
;
445 * With O_SYNC, we can only take UC mapping. Fail if we cannot.
446 * Without O_SYNC, we want to get
447 * - WB for WB-able memory and no other conflicting mappings
448 * - UC_MINUS for non-WB-able memory with no other conflicting mappings
449 * - Inherit from confliting mappings otherwise
451 if (flags
!= _PAGE_CACHE_UC_MINUS
) {
452 retval
= reserve_memtype(offset
, offset
+ size
, flags
, NULL
);
454 retval
= reserve_memtype(offset
, offset
+ size
, -1, &flags
);
460 if (pfn
<= max_pfn_mapped
&&
461 ioremap_change_attr((unsigned long)__va(offset
), size
, flags
) < 0) {
462 free_memtype(offset
, offset
+ size
);
464 "%s:%d /dev/mem ioremap_change_attr failed %s for %Lx-%Lx\n",
465 current
->comm
, current
->pid
,
467 offset
, (unsigned long long)(offset
+ size
));
471 *vma_prot
= __pgprot((pgprot_val(*vma_prot
) & ~_PAGE_CACHE_MASK
) |
476 void map_devmem(unsigned long pfn
, unsigned long size
, pgprot_t vma_prot
)
478 u64 addr
= (u64
)pfn
<< PAGE_SHIFT
;
480 unsigned long want_flags
= (pgprot_val(vma_prot
) & _PAGE_CACHE_MASK
);
482 reserve_memtype(addr
, addr
+ size
, want_flags
, &flags
);
483 if (flags
!= want_flags
) {
485 "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n",
486 current
->comm
, current
->pid
,
487 cattr_name(want_flags
),
488 addr
, (unsigned long long)(addr
+ size
),
493 void unmap_devmem(unsigned long pfn
, unsigned long size
, pgprot_t vma_prot
)
495 u64 addr
= (u64
)pfn
<< PAGE_SHIFT
;
497 free_memtype(addr
, addr
+ size
);