blob: 82d097ce3091cdd25686ed4badf4484901587f4d [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
Ingo Molnarad2cde12008-09-30 13:20:45 +020010#include <linux/seq_file.h>
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -070011#include <linux/bootmem.h>
venkatesh.pallipadi@intel.comfec09622008-07-18 16:08:14 -070012#include <linux/debugfs.h>
Ingo Molnarad2cde12008-09-30 13:20:45 +020013#include <linux/kernel.h>
Ingo Molnar92b9af92009-02-28 14:09:27 +010014#include <linux/module.h>
Ingo Molnarad2cde12008-09-30 13:20:45 +020015#include <linux/gfp.h>
16#include <linux/mm.h>
17#include <linux/fs.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070018
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070019#include <asm/cacheflush.h>
Ingo Molnarad2cde12008-09-30 13:20:45 +020020#include <asm/processor.h>
21#include <asm/tlbflush.h>
22#include <asm/pgtable.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070023#include <asm/fcntl.h>
Ingo Molnarad2cde12008-09-30 13:20:45 +020024#include <asm/e820.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070025#include <asm/mtrr.h>
Ingo Molnarad2cde12008-09-30 13:20:45 +020026#include <asm/page.h>
27#include <asm/msr.h>
28#include <asm/pat.h>
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -070029#include <asm/io.h>
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070030
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020031#ifdef CONFIG_X86_PAT
Andreas Herrmann499f8f82008-06-10 16:06:21 +020032int __read_mostly pat_enabled = 1;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070033
Marcin Slusarz1ee4bd92009-04-10 22:47:17 +020034static inline void pat_disable(const char *reason)
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020035{
Andreas Herrmann499f8f82008-06-10 16:06:21 +020036 pat_enabled = 0;
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020037 printk(KERN_INFO "%s\n", reason);
38}
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070039
Andrew Mortonbe524fb2008-05-29 00:01:28 -070040static int __init nopat(char *str)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070041{
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020042 pat_disable("PAT support disabled.");
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070043 return 0;
44}
45early_param("nopat", nopat);
H. Peter Anvin75a04812009-01-22 16:17:05 -080046#else
47static inline void pat_disable(const char *reason)
48{
49 (void)reason;
50}
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020051#endif
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070052
Venki Pallipadi77b52b42008-05-05 19:09:10 -070053
54static int debug_enable;
Ingo Molnarad2cde12008-09-30 13:20:45 +020055
Venki Pallipadi77b52b42008-05-05 19:09:10 -070056static int __init pat_debug_setup(char *str)
57{
58 debug_enable = 1;
59 return 0;
60}
61__setup("debugpat", pat_debug_setup);
62
63#define dprintk(fmt, arg...) \
64 do { if (debug_enable) printk(KERN_INFO fmt, ##arg); } while (0)
65
66
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020067static u64 __read_mostly boot_pat_state;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070068
69enum {
70 PAT_UC = 0, /* uncached */
71 PAT_WC = 1, /* Write combining */
72 PAT_WT = 4, /* Write Through */
73 PAT_WP = 5, /* Write Protected */
74 PAT_WB = 6, /* Write Back (default) */
75 PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */
76};
77
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +020078#define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070079
80void pat_init(void)
81{
82 u64 pat;
83
Andreas Herrmann499f8f82008-06-10 16:06:21 +020084 if (!pat_enabled)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -070085 return;
86
H. Peter Anvin75a04812009-01-22 16:17:05 -080087 if (!cpu_has_pat) {
88 if (!boot_pat_state) {
89 pat_disable("PAT not supported by CPU.");
90 return;
91 } else {
92 /*
93 * If this happens we are on a secondary CPU, but
94 * switched to PAT on the boot CPU. We have no way to
95 * undo PAT.
96 */
97 printk(KERN_ERR "PAT enabled, "
98 "but not supported by secondary CPU\n");
99 BUG();
100 }
Thomas Gleixner8d4a4302008-05-08 09:18:43 +0200101 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700102
103 /* Set PWT to Write-Combining. All other bits stay the same */
104 /*
105 * PTE encoding used in Linux:
106 * PAT
107 * |PCD
108 * ||PWT
109 * |||
110 * 000 WB _PAGE_CACHE_WB
111 * 001 WC _PAGE_CACHE_WC
112 * 010 UC- _PAGE_CACHE_UC_MINUS
113 * 011 UC _PAGE_CACHE_UC
114 * PAT bit unused
115 */
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200116 pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
117 PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, UC);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700118
119 /* Boot CPU check */
Thomas Gleixner8d4a4302008-05-08 09:18:43 +0200120 if (!boot_pat_state)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700121 rdmsrl(MSR_IA32_CR_PAT, boot_pat_state);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700122
123 wrmsrl(MSR_IA32_CR_PAT, pat);
124 printk(KERN_INFO "x86 PAT enabled: cpu %d, old 0x%Lx, new 0x%Lx\n",
125 smp_processor_id(), boot_pat_state, pat);
126}
127
128#undef PAT
129
130static char *cattr_name(unsigned long flags)
131{
132 switch (flags & _PAGE_CACHE_MASK) {
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200133 case _PAGE_CACHE_UC: return "uncached";
134 case _PAGE_CACHE_UC_MINUS: return "uncached-minus";
135 case _PAGE_CACHE_WB: return "write-back";
136 case _PAGE_CACHE_WC: return "write-combining";
137 default: return "broken";
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700138 }
139}
140
141/*
142 * The global memtype list keeps track of memory type for specific
143 * physical memory areas. Conflicting memory types in different
144 * mappings can cause CPU cache corruption. To avoid this we keep track.
145 *
146 * The list is sorted based on starting address and can contain multiple
147 * entries for each address (this allows reference counting for overlapping
148 * areas). All the aliases have the same cache attributes of course.
149 * Zero attributes are represented as holes.
150 *
151 * Currently the data structure is a list because the number of mappings
152 * are expected to be relatively small. If this should be a problem
153 * it could be changed to a rbtree or similar.
154 *
155 * memtype_lock protects the whole list.
156 */
157
158struct memtype {
Ingo Molnarad2cde12008-09-30 13:20:45 +0200159 u64 start;
160 u64 end;
161 unsigned long type;
162 struct list_head nd;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700163};
164
165static LIST_HEAD(memtype_list);
Ingo Molnarad2cde12008-09-30 13:20:45 +0200166static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700167
168/*
169 * Does intersection of PAT memory type and MTRR memory type and returns
170 * the resulting memory type as PAT understands it.
171 * (Type in pat and mtrr will not have same value)
172 * The intersection is based on "Effective Memory Type" tables in IA-32
173 * SDM vol 3a
174 */
Hugh Dickins6cf514f2008-06-16 18:42:43 +0100175static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700176{
Venki Pallipadic26421d2008-05-29 12:01:44 -0700177 /*
178 * Look for MTRR hint to get the effective type in case where PAT
179 * request is for WB.
180 */
Andreas Herrmanndd0c7c42008-06-18 15:38:57 +0200181 if (req_type == _PAGE_CACHE_WB) {
182 u8 mtrr_type;
183
184 mtrr_type = mtrr_type_lookup(start, end);
Suresh Siddhab6ff32d2009-04-09 14:26:51 -0700185 if (mtrr_type != MTRR_TYPE_WRBACK)
186 return _PAGE_CACHE_UC_MINUS;
187
188 return _PAGE_CACHE_WB;
Andreas Herrmanndd0c7c42008-06-18 15:38:57 +0200189 }
190
191 return req_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700192}
193
Ingo Molnarad2cde12008-09-30 13:20:45 +0200194static int
195chk_conflict(struct memtype *new, struct memtype *entry, unsigned long *type)
Andreas Herrmann64fe44c2008-06-20 22:07:09 +0200196{
197 if (new->type != entry->type) {
198 if (type) {
199 new->type = entry->type;
200 *type = entry->type;
201 } else
202 goto conflict;
203 }
204
205 /* check overlaps with more than one entry in the list */
206 list_for_each_entry_continue(entry, &memtype_list, nd) {
207 if (new->end <= entry->start)
208 break;
209 else if (new->type != entry->type)
210 goto conflict;
211 }
212 return 0;
213
214 conflict:
215 printk(KERN_INFO "%s:%d conflicting memory types "
216 "%Lx-%Lx %s<->%s\n", current->comm, current->pid, new->start,
217 new->end, cattr_name(new->type), cattr_name(entry->type));
218 return -EBUSY;
219}
220
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700221static struct memtype *cached_entry;
222static u64 cached_start;
223
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800224static int pat_pagerange_is_ram(unsigned long start, unsigned long end)
225{
226 int ram_page = 0, not_rampage = 0;
227 unsigned long page_nr;
228
229 for (page_nr = (start >> PAGE_SHIFT); page_nr < (end >> PAGE_SHIFT);
230 ++page_nr) {
231 /*
232 * For legacy reasons, physical address range in the legacy ISA
233 * region is tracked as non-RAM. This will allow users of
234 * /dev/mem to map portions of legacy ISA region, even when
235 * some of those portions are listed(or not even listed) with
236 * different e820 types(RAM/reserved/..)
237 */
238 if (page_nr >= (ISA_END_ADDRESS >> PAGE_SHIFT) &&
239 page_is_ram(page_nr))
240 ram_page = 1;
241 else
242 not_rampage = 1;
243
244 if (ram_page == not_rampage)
245 return -1;
246 }
247
248 return ram_page;
249}
250
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700251/*
Suresh Siddha9542ada2008-09-24 08:53:33 -0700252 * For RAM pages, mark the pages as non WB memory type using
253 * PageNonWB (PG_arch_1). We allow only one set_memory_uc() or
254 * set_memory_wc() on a RAM page at a time before marking it as WB again.
255 * This is ok, because only one driver will be owning the page and
256 * doing set_memory_*() calls.
257 *
258 * For now, we use PageNonWB to track that the RAM page is being mapped
259 * as non WB. In future, we will have to use one more flag
260 * (or some other mechanism in page_struct) to distinguish between
261 * UC and WC mapping.
262 */
263static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type,
Ingo Molnarad2cde12008-09-30 13:20:45 +0200264 unsigned long *new_type)
Suresh Siddha9542ada2008-09-24 08:53:33 -0700265{
266 struct page *page;
267 u64 pfn, end_pfn;
268
269 for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
270 page = pfn_to_page(pfn);
271 if (page_mapped(page) || PageNonWB(page))
272 goto out;
273
274 SetPageNonWB(page);
275 }
276 return 0;
277
278out:
279 end_pfn = pfn;
280 for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
281 page = pfn_to_page(pfn);
282 ClearPageNonWB(page);
283 }
284
285 return -EINVAL;
286}
287
288static int free_ram_pages_type(u64 start, u64 end)
289{
290 struct page *page;
291 u64 pfn, end_pfn;
292
293 for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
294 page = pfn_to_page(pfn);
295 if (page_mapped(page) || !PageNonWB(page))
296 goto out;
297
298 ClearPageNonWB(page);
299 }
300 return 0;
301
302out:
303 end_pfn = pfn;
304 for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
305 page = pfn_to_page(pfn);
306 SetPageNonWB(page);
307 }
308 return -EINVAL;
309}
310
311/*
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700312 * req_type typically has one of the:
313 * - _PAGE_CACHE_WB
314 * - _PAGE_CACHE_WC
315 * - _PAGE_CACHE_UC_MINUS
316 * - _PAGE_CACHE_UC
317 *
318 * req_type will have a special case value '-1', when requester want to inherit
319 * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
320 *
Andreas Herrmannac979912008-06-20 22:01:49 +0200321 * If new_type is NULL, function will return an error if it cannot reserve the
322 * region with req_type. If new_type is non-NULL, function will return
323 * available type in new_type in case of no error. In case of any error
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700324 * it will return a negative return value.
325 */
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700326int reserve_memtype(u64 start, u64 end, unsigned long req_type,
Ingo Molnarad2cde12008-09-30 13:20:45 +0200327 unsigned long *new_type)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700328{
Andreas Herrmannac979912008-06-20 22:01:49 +0200329 struct memtype *new, *entry;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700330 unsigned long actual_type;
Andreas Herrmannf6887262008-06-20 22:05:37 +0200331 struct list_head *where;
Suresh Siddha9542ada2008-09-24 08:53:33 -0700332 int is_range_ram;
Ingo Molnarad2cde12008-09-30 13:20:45 +0200333 int err = 0;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700334
Ingo Molnarad2cde12008-09-30 13:20:45 +0200335 BUG_ON(start >= end); /* end is exclusive */
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200336
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200337 if (!pat_enabled) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700338 /* This is identical to page table setting without PAT */
Andreas Herrmannac979912008-06-20 22:01:49 +0200339 if (new_type) {
340 if (req_type == -1)
341 *new_type = _PAGE_CACHE_WB;
Venkatesh Pallipadi5fc51742009-07-10 09:57:32 -0700342 else if (req_type == _PAGE_CACHE_WC)
343 *new_type = _PAGE_CACHE_UC_MINUS;
Andreas Herrmannac979912008-06-20 22:01:49 +0200344 else
345 *new_type = req_type & _PAGE_CACHE_MASK;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700346 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700347 return 0;
348 }
349
350 /* Low ISA region is always mapped WB in page table. No need to track */
Andreas Herrmannbcc643d2008-06-20 21:58:46 +0200351 if (is_ISA_range(start, end - 1)) {
Andreas Herrmannac979912008-06-20 22:01:49 +0200352 if (new_type)
353 *new_type = _PAGE_CACHE_WB;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700354 return 0;
355 }
356
Suresh Siddhab6ff32d2009-04-09 14:26:51 -0700357 /*
358 * Call mtrr_lookup to get the type hint. This is an
359 * optimization for /dev/mem mmap'ers into WB memory (BIOS
360 * tools and ACPI tools). Use WB request for WB memory and use
361 * UC_MINUS otherwise.
362 */
363 actual_type = pat_x_mtrr_type(start, end, req_type & _PAGE_CACHE_MASK);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700364
Suresh Siddha95971342009-01-13 10:21:30 -0800365 if (new_type)
366 *new_type = actual_type;
367
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800368 is_range_ram = pat_pagerange_is_ram(start, end);
Suresh Siddha9542ada2008-09-24 08:53:33 -0700369 if (is_range_ram == 1)
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800370 return reserve_ram_pages_type(start, end, req_type,
371 new_type);
Suresh Siddha9542ada2008-09-24 08:53:33 -0700372 else if (is_range_ram < 0)
373 return -EINVAL;
374
Andreas Herrmannac979912008-06-20 22:01:49 +0200375 new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
376 if (!new)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700377 return -ENOMEM;
378
Ingo Molnarad2cde12008-09-30 13:20:45 +0200379 new->start = start;
380 new->end = end;
381 new->type = actual_type;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700382
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700383 spin_lock(&memtype_lock);
384
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700385 if (cached_entry && start >= cached_start)
386 entry = cached_entry;
387 else
388 entry = list_entry(&memtype_list, struct memtype, nd);
389
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700390 /* Search for existing mapping that overlaps the current range */
Andreas Herrmannf6887262008-06-20 22:05:37 +0200391 where = NULL;
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700392 list_for_each_entry_continue(entry, &memtype_list, nd) {
Andreas Herrmann33af9032008-06-20 22:08:37 +0200393 if (end <= entry->start) {
Andreas Herrmannf6887262008-06-20 22:05:37 +0200394 where = entry->nd.prev;
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700395 cached_entry = list_entry(where, struct memtype, nd);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700396 break;
Andreas Herrmann33af9032008-06-20 22:08:37 +0200397 } else if (start <= entry->start) { /* end > entry->start */
Andreas Herrmann64fe44c2008-06-20 22:07:09 +0200398 err = chk_conflict(new, entry, new_type);
Andreas Herrmann33af9032008-06-20 22:08:37 +0200399 if (!err) {
400 dprintk("Overlap at 0x%Lx-0x%Lx\n",
401 entry->start, entry->end);
402 where = entry->nd.prev;
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700403 cached_entry = list_entry(where,
404 struct memtype, nd);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700405 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700406 break;
Andreas Herrmann33af9032008-06-20 22:08:37 +0200407 } else if (start < entry->end) { /* start > entry->start */
Andreas Herrmann64fe44c2008-06-20 22:07:09 +0200408 err = chk_conflict(new, entry, new_type);
Andreas Herrmann33af9032008-06-20 22:08:37 +0200409 if (!err) {
410 dprintk("Overlap at 0x%Lx-0x%Lx\n",
411 entry->start, entry->end);
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700412 cached_entry = list_entry(entry->nd.prev,
413 struct memtype, nd);
414
415 /*
416 * Move to right position in the linked
417 * list to add this new entry
418 */
419 list_for_each_entry_continue(entry,
420 &memtype_list, nd) {
421 if (start <= entry->start) {
422 where = entry->nd.prev;
423 break;
424 }
425 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700426 }
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700427 break;
428 }
429 }
430
431 if (err) {
Andreas Herrmann3e9c83b2008-06-20 22:04:02 +0200432 printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
433 "track %s, req %s\n",
434 start, end, cattr_name(new->type), cattr_name(req_type));
Andreas Herrmannac979912008-06-20 22:01:49 +0200435 kfree(new);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700436 spin_unlock(&memtype_lock);
Ingo Molnarad2cde12008-09-30 13:20:45 +0200437
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700438 return err;
439 }
440
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700441 cached_start = start;
442
Andreas Herrmannf6887262008-06-20 22:05:37 +0200443 if (where)
444 list_add(&new->nd, where);
445 else
Andreas Herrmannac979912008-06-20 22:01:49 +0200446 list_add_tail(&new->nd, &memtype_list);
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700447
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700448 spin_unlock(&memtype_lock);
Andreas Herrmann3e9c83b2008-06-20 22:04:02 +0200449
450 dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
451 start, end, cattr_name(new->type), cattr_name(req_type),
452 new_type ? cattr_name(*new_type) : "-");
453
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700454 return err;
455}
456
457int free_memtype(u64 start, u64 end)
458{
Andreas Herrmannac979912008-06-20 22:01:49 +0200459 struct memtype *entry;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700460 int err = -EINVAL;
Suresh Siddha9542ada2008-09-24 08:53:33 -0700461 int is_range_ram;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700462
Andreas Herrmann69e26be2008-06-20 22:03:06 +0200463 if (!pat_enabled)
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700464 return 0;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700465
466 /* Low ISA region is always mapped WB. No need to track */
Andreas Herrmannbcc643d2008-06-20 21:58:46 +0200467 if (is_ISA_range(start, end - 1))
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700468 return 0;
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700469
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800470 is_range_ram = pat_pagerange_is_ram(start, end);
Suresh Siddha9542ada2008-09-24 08:53:33 -0700471 if (is_range_ram == 1)
472 return free_ram_pages_type(start, end);
473 else if (is_range_ram < 0)
474 return -EINVAL;
475
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700476 spin_lock(&memtype_lock);
Andreas Herrmannac979912008-06-20 22:01:49 +0200477 list_for_each_entry(entry, &memtype_list, nd) {
478 if (entry->start == start && entry->end == end) {
Venki Pallipadi80c5e732008-08-19 16:28:01 -0700479 if (cached_entry == entry || cached_start == start)
480 cached_entry = NULL;
481
Andreas Herrmannac979912008-06-20 22:01:49 +0200482 list_del(&entry->nd);
483 kfree(entry);
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700484 err = 0;
485 break;
486 }
487 }
488 spin_unlock(&memtype_lock);
489
490 if (err) {
Ingo Molnar28eb559b2008-04-03 10:14:33 +0200491 printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700492 current->comm, current->pid, start, end);
493 }
venkatesh.pallipadi@intel.com6997ab42008-03-18 17:00:25 -0700494
Venki Pallipadi77b52b42008-05-05 19:09:10 -0700495 dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
Ingo Molnarad2cde12008-09-30 13:20:45 +0200496
venkatesh.pallipadi@intel.com2e5d9c82008-03-18 17:00:14 -0700497 return err;
498}
499
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700500
Venkatesh Pallipadi9fd126b2009-07-10 09:57:34 -0700501/**
502 * io_reserve_memtype - Request a memory type mapping for a region of memory
503 * @start: start (physical address) of the region
504 * @end: end (physical address) of the region
505 * @type: A pointer to memtype, with requested type. On success, requested
506 * or any other compatible type that was available for the region is returned
507 *
508 * On success, returns 0
509 * On failure, returns non-zero
510 */
511int io_reserve_memtype(resource_size_t start, resource_size_t end,
512 unsigned long *type)
513{
514 unsigned long req_type = *type;
515 unsigned long new_type;
516 int ret;
517
518 WARN_ON_ONCE(iomem_map_sanity_check(start, end - start));
519
520 ret = reserve_memtype(start, end, req_type, &new_type);
521 if (ret)
522 goto out_err;
523
524 if (!is_new_memtype_allowed(req_type, new_type))
525 goto out_free;
526
527 if (kernel_map_sync_memtype(start, end - start, new_type) < 0)
528 goto out_free;
529
530 *type = new_type;
531 return 0;
532
533out_free:
534 free_memtype(start, end);
535 ret = -EBUSY;
536out_err:
537 return ret;
538}
539
540/**
541 * io_free_memtype - Release a memory type mapping for a region of memory
542 * @start: start (physical address) of the region
543 * @end: end (physical address) of the region
544 */
545void io_free_memtype(resource_size_t start, resource_size_t end)
546{
547 free_memtype(start, end);
548}
549
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700550pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
551 unsigned long size, pgprot_t vma_prot)
552{
553 return vma_prot;
554}
555
Ingo Molnard0926332008-07-18 00:26:59 +0200556#ifdef CONFIG_STRICT_DEVMEM
557/* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700558static inline int range_is_allowed(unsigned long pfn, unsigned long size)
559{
560 return 1;
561}
562#else
Ravikiran G Thirumalai9e41bff2008-10-30 13:59:21 -0700563/* This check is needed to avoid cache aliasing when PAT is enabled */
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700564static inline int range_is_allowed(unsigned long pfn, unsigned long size)
565{
566 u64 from = ((u64)pfn) << PAGE_SHIFT;
567 u64 to = from + size;
568 u64 cursor = from;
569
Ravikiran G Thirumalai9e41bff2008-10-30 13:59:21 -0700570 if (!pat_enabled)
571 return 1;
572
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700573 while (cursor < to) {
574 if (!devmem_is_allowed(pfn)) {
575 printk(KERN_INFO
576 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
577 current->comm, from, to);
578 return 0;
579 }
580 cursor += PAGE_SIZE;
581 pfn++;
582 }
583 return 1;
584}
Ingo Molnard0926332008-07-18 00:26:59 +0200585#endif /* CONFIG_STRICT_DEVMEM */
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700586
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700587int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
588 unsigned long size, pgprot_t *vma_prot)
589{
Suresh Siddha0c3c8a12009-04-09 14:26:52 -0700590 unsigned long flags = _PAGE_CACHE_WB;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700591
Venki Pallipadi0124cec2008-04-26 11:32:12 -0700592 if (!range_is_allowed(pfn, size))
593 return 0;
594
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700595 if (file->f_flags & O_SYNC) {
venkatesh.pallipadi@intel.com28df82e2008-08-20 16:45:52 -0700596 flags = _PAGE_CACHE_UC_MINUS;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700597 }
598
599#ifdef CONFIG_X86_32
600 /*
601 * On the PPro and successors, the MTRRs are used to set
602 * memory types for physical addresses outside main memory,
603 * so blindly setting UC or PWT on those pages is wrong.
604 * For Pentiums and earlier, the surround logic should disable
605 * caching for the high addresses through the KEN pin, but
606 * we maintain the tradition of paranoia in this code.
607 */
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200608 if (!pat_enabled &&
Andreas Herrmanncd7a4e92008-06-10 16:05:39 +0200609 !(boot_cpu_has(X86_FEATURE_MTRR) ||
610 boot_cpu_has(X86_FEATURE_K6_MTRR) ||
611 boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
612 boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
613 (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700614 flags = _PAGE_CACHE_UC;
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700615 }
616#endif
617
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700618 *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
619 flags);
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700620 return 1;
621}
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700622
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800623/*
Venkatesh Pallipadi7880f742009-02-24 17:35:13 -0800624 * Change the memory type for the physial address range in kernel identity
625 * mapping space if that range is a part of identity map.
626 */
627int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags)
628{
629 unsigned long id_sz;
630
Venkatesh Pallipadi5fc51742009-07-10 09:57:32 -0700631 if (base >= __pa(high_memory))
Venkatesh Pallipadi7880f742009-02-24 17:35:13 -0800632 return 0;
633
634 id_sz = (__pa(high_memory) < base + size) ?
635 __pa(high_memory) - base :
636 size;
637
638 if (ioremap_change_attr((unsigned long)__va(base), id_sz, flags) < 0) {
639 printk(KERN_INFO
640 "%s:%d ioremap_change_attr failed %s "
641 "for %Lx-%Lx\n",
642 current->comm, current->pid,
643 cattr_name(flags),
644 base, (unsigned long long)(base + size));
645 return -EINVAL;
646 }
647 return 0;
648}
649
650/*
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800651 * Internal interface to reserve a range of physical memory with prot.
652 * Reserved non RAM regions only and after successful reserve_memtype,
653 * this func also keeps identity mapping (if any) in sync with this new prot.
654 */
venkatesh.pallipadi@intel.comcdecff62009-01-09 16:13:12 -0800655static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
656 int strict_prot)
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800657{
658 int is_ram = 0;
Venkatesh Pallipadi7880f742009-02-24 17:35:13 -0800659 int ret;
venkatesh.pallipadi@intel.comcdecff62009-01-09 16:13:12 -0800660 unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
Suresh Siddha0c3c8a12009-04-09 14:26:52 -0700661 unsigned long flags = want_flags;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800662
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800663 is_ram = pat_pagerange_is_ram(paddr, paddr + size);
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800664
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800665 /*
Pallipadi, Venkatesh4bb9c5c2009-03-12 17:45:27 -0700666 * reserve_pfn_range() doesn't support RAM pages. Maintain the current
667 * behavior with RAM pages by returning success.
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800668 */
669 if (is_ram != 0)
Pallipadi, Venkatesh4bb9c5c2009-03-12 17:45:27 -0700670 return 0;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800671
672 ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
673 if (ret)
674 return ret;
675
676 if (flags != want_flags) {
venkatesh.pallipadi@intel.comcdecff62009-01-09 16:13:12 -0800677 if (strict_prot || !is_new_memtype_allowed(want_flags, flags)) {
678 free_memtype(paddr, paddr + size);
679 printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
680 " for %Lx-%Lx, got %s\n",
681 current->comm, current->pid,
682 cattr_name(want_flags),
683 (unsigned long long)paddr,
684 (unsigned long long)(paddr + size),
685 cattr_name(flags));
686 return -EINVAL;
687 }
688 /*
689 * We allow returning different type than the one requested in
690 * non strict case.
691 */
692 *vma_prot = __pgprot((pgprot_val(*vma_prot) &
693 (~_PAGE_CACHE_MASK)) |
694 flags);
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800695 }
696
Venkatesh Pallipadi7880f742009-02-24 17:35:13 -0800697 if (kernel_map_sync_memtype(paddr, size, flags) < 0) {
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800698 free_memtype(paddr, paddr + size);
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800699 return -EINVAL;
700 }
701 return 0;
702}
703
704/*
705 * Internal interface to free a range of physical memory.
706 * Frees non RAM regions only.
707 */
708static void free_pfn_range(u64 paddr, unsigned long size)
709{
710 int is_ram;
711
Suresh Siddhabe03d9e2009-02-11 11:20:23 -0800712 is_ram = pat_pagerange_is_ram(paddr, paddr + size);
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800713 if (is_ram == 0)
714 free_memtype(paddr, paddr + size);
715}
716
717/*
718 * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
719 * copied through copy_page_range().
720 *
721 * If the vma has a linear pfn mapping for the entire range, we get the prot
722 * from pte and reserve the entire vma range with single reserve_pfn_range call.
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800723 */
724int track_pfn_vma_copy(struct vm_area_struct *vma)
725{
H. Peter Anvinc1c15b62008-12-23 10:10:40 -0800726 resource_size_t paddr;
venkatesh.pallipadi@intel.com982d7892008-12-19 13:47:28 -0800727 unsigned long prot;
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700728 unsigned long vma_size = vma->vm_end - vma->vm_start;
venkatesh.pallipadi@intel.comcdecff62009-01-09 16:13:12 -0800729 pgprot_t pgprot;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800730
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700731 /*
732 * For now, only handle remap_pfn_range() vmas where
733 * is_linear_pfn_mapping() == TRUE. Handling of
734 * vm_insert_pfn() is TBD.
735 */
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800736 if (is_linear_pfn_mapping(vma)) {
737 /*
venkatesh.pallipadi@intel.com982d7892008-12-19 13:47:28 -0800738 * reserve the whole chunk covered by vma. We need the
739 * starting address and protection from pte.
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800740 */
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700741 if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800742 WARN_ON_ONCE(1);
venkatesh.pallipadi@intel.com982d7892008-12-19 13:47:28 -0800743 return -EINVAL;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800744 }
venkatesh.pallipadi@intel.comcdecff62009-01-09 16:13:12 -0800745 pgprot = __pgprot(prot);
746 return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800747 }
748
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800749 return 0;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800750}
751
752/*
753 * track_pfn_vma_new is called when a _new_ pfn mapping is being established
754 * for physical range indicated by pfn and size.
755 *
756 * prot is passed in as a parameter for the new mapping. If the vma has a
757 * linear pfn mapping for the entire range reserve the entire vma range with
758 * single reserve_pfn_range call.
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800759 */
venkatesh.pallipadi@intel.come4b866e2009-01-09 16:13:11 -0800760int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800761 unsigned long pfn, unsigned long size)
762{
H. Peter Anvinc1c15b62008-12-23 10:10:40 -0800763 resource_size_t paddr;
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700764 unsigned long vma_size = vma->vm_end - vma->vm_start;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800765
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700766 /*
767 * For now, only handle remap_pfn_range() vmas where
768 * is_linear_pfn_mapping() == TRUE. Handling of
769 * vm_insert_pfn() is TBD.
770 */
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800771 if (is_linear_pfn_mapping(vma)) {
772 /* reserve the whole chunk starting from vm_pgoff */
H. Peter Anvinc1c15b62008-12-23 10:10:40 -0800773 paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
venkatesh.pallipadi@intel.comcdecff62009-01-09 16:13:12 -0800774 return reserve_pfn_range(paddr, vma_size, prot, 0);
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800775 }
776
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800777 return 0;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800778}
779
780/*
781 * untrack_pfn_vma is called while unmapping a pfnmap for a region.
782 * untrack can be called for a specific region indicated by pfn and size or
783 * can be for the entire vma (in which case size can be zero).
784 */
785void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
786 unsigned long size)
787{
H. Peter Anvinc1c15b62008-12-23 10:10:40 -0800788 resource_size_t paddr;
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700789 unsigned long vma_size = vma->vm_end - vma->vm_start;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800790
Pallipadi, Venkatesh4b065042009-04-08 15:37:16 -0700791 /*
792 * For now, only handle remap_pfn_range() vmas where
793 * is_linear_pfn_mapping() == TRUE. Handling of
794 * vm_insert_pfn() is TBD.
795 */
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800796 if (is_linear_pfn_mapping(vma)) {
797 /* free the whole chunk starting from vm_pgoff */
H. Peter Anvinc1c15b62008-12-23 10:10:40 -0800798 paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800799 free_pfn_range(paddr, vma_size);
800 return;
801 }
venkatesh.pallipadi@intel.com58993292008-12-18 11:41:30 -0800802}
803
venkatesh.pallipadi@intel.com2520bd32008-12-18 11:41:32 -0800804pgprot_t pgprot_writecombine(pgprot_t prot)
805{
806 if (pat_enabled)
807 return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
808 else
809 return pgprot_noncached(prot);
810}
Ingo Molnar92b9af92009-02-28 14:09:27 +0100811EXPORT_SYMBOL_GPL(pgprot_writecombine);
venkatesh.pallipadi@intel.com2520bd32008-12-18 11:41:32 -0800812
Andreas Herrmann012f09e2008-08-06 16:23:08 +0200813#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
venkatesh.pallipadi@intel.comfec09622008-07-18 16:08:14 -0700814
815/* get Nth element of the linked list */
816static struct memtype *memtype_get_idx(loff_t pos)
817{
818 struct memtype *list_node, *print_entry;
819 int i = 1;
820
821 print_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
822 if (!print_entry)
823 return NULL;
824
825 spin_lock(&memtype_lock);
826 list_for_each_entry(list_node, &memtype_list, nd) {
827 if (pos == i) {
828 *print_entry = *list_node;
829 spin_unlock(&memtype_lock);
830 return print_entry;
831 }
832 ++i;
833 }
834 spin_unlock(&memtype_lock);
835 kfree(print_entry);
Ingo Molnarad2cde12008-09-30 13:20:45 +0200836
venkatesh.pallipadi@intel.comfec09622008-07-18 16:08:14 -0700837 return NULL;
838}
839
840static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
841{
842 if (*pos == 0) {
843 ++*pos;
844 seq_printf(seq, "PAT memtype list:\n");
845 }
846
847 return memtype_get_idx(*pos);
848}
849
850static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
851{
852 ++*pos;
853 return memtype_get_idx(*pos);
854}
855
856static void memtype_seq_stop(struct seq_file *seq, void *v)
857{
858}
859
860static int memtype_seq_show(struct seq_file *seq, void *v)
861{
862 struct memtype *print_entry = (struct memtype *)v;
863
864 seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
865 print_entry->start, print_entry->end);
866 kfree(print_entry);
Ingo Molnarad2cde12008-09-30 13:20:45 +0200867
venkatesh.pallipadi@intel.comfec09622008-07-18 16:08:14 -0700868 return 0;
869}
870
871static struct seq_operations memtype_seq_ops = {
872 .start = memtype_seq_start,
873 .next = memtype_seq_next,
874 .stop = memtype_seq_stop,
875 .show = memtype_seq_show,
876};
877
878static int memtype_seq_open(struct inode *inode, struct file *file)
879{
880 return seq_open(file, &memtype_seq_ops);
881}
882
883static const struct file_operations memtype_fops = {
884 .open = memtype_seq_open,
885 .read = seq_read,
886 .llseek = seq_lseek,
887 .release = seq_release,
888};
889
890static int __init pat_memtype_list_init(void)
891{
892 debugfs_create_file("pat_memtype_list", S_IRUSR, arch_debugfs_dir,
893 NULL, &memtype_fops);
894 return 0;
895}
896
897late_initcall(pat_memtype_list_init);
898
Andreas Herrmann012f09e2008-08-06 16:23:08 +0200899#endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */