venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 10 | #include <linux/seq_file.h> |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 11 | #include <linux/bootmem.h> |
venkatesh.pallipadi@intel.com | fec0962 | 2008-07-18 16:08:14 -0700 | [diff] [blame] | 12 | #include <linux/debugfs.h> |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Ingo Molnar | 92b9af9 | 2009-02-28 14:09:27 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 15 | #include <linux/gfp.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include <linux/fs.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 18 | |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 19 | #include <asm/cacheflush.h> |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 20 | #include <asm/processor.h> |
| 21 | #include <asm/tlbflush.h> |
| 22 | #include <asm/pgtable.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 23 | #include <asm/fcntl.h> |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 24 | #include <asm/e820.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 25 | #include <asm/mtrr.h> |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 26 | #include <asm/page.h> |
| 27 | #include <asm/msr.h> |
| 28 | #include <asm/pat.h> |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 29 | #include <asm/io.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 30 | |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_X86_PAT |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 32 | int __read_mostly pat_enabled = 1; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 33 | |
Marcin Slusarz | 1ee4bd9 | 2009-04-10 22:47:17 +0200 | [diff] [blame] | 34 | static inline void pat_disable(const char *reason) |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 35 | { |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 36 | pat_enabled = 0; |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 37 | printk(KERN_INFO "%s\n", reason); |
| 38 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 39 | |
Andrew Morton | be524fb | 2008-05-29 00:01:28 -0700 | [diff] [blame] | 40 | static int __init nopat(char *str) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 41 | { |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 42 | pat_disable("PAT support disabled."); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | early_param("nopat", nopat); |
H. Peter Anvin | 75a0481 | 2009-01-22 16:17:05 -0800 | [diff] [blame] | 46 | #else |
| 47 | static inline void pat_disable(const char *reason) |
| 48 | { |
| 49 | (void)reason; |
| 50 | } |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 51 | #endif |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 52 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 53 | |
| 54 | static int debug_enable; |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 55 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 56 | static 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 Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 67 | static u64 __read_mostly boot_pat_state; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 68 | |
| 69 | enum { |
| 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 Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 78 | #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8)) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 79 | |
| 80 | void pat_init(void) |
| 81 | { |
| 82 | u64 pat; |
| 83 | |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 84 | if (!pat_enabled) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 85 | return; |
| 86 | |
H. Peter Anvin | 75a0481 | 2009-01-22 16:17:05 -0800 | [diff] [blame] | 87 | 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 Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 101 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 102 | |
| 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 Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 116 | 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 118 | |
| 119 | /* Boot CPU check */ |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 120 | if (!boot_pat_state) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 121 | rdmsrl(MSR_IA32_CR_PAT, boot_pat_state); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 122 | |
| 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 | |
| 130 | static char *cattr_name(unsigned long flags) |
| 131 | { |
| 132 | switch (flags & _PAGE_CACHE_MASK) { |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 133 | 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 138 | } |
| 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 | |
| 158 | struct memtype { |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 159 | u64 start; |
| 160 | u64 end; |
| 161 | unsigned long type; |
| 162 | struct list_head nd; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static LIST_HEAD(memtype_list); |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 166 | static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */ |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 167 | |
| 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 Dickins | 6cf514f | 2008-06-16 18:42:43 +0100 | [diff] [blame] | 175 | static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 176 | { |
Venki Pallipadi | c26421d | 2008-05-29 12:01:44 -0700 | [diff] [blame] | 177 | /* |
| 178 | * Look for MTRR hint to get the effective type in case where PAT |
| 179 | * request is for WB. |
| 180 | */ |
Andreas Herrmann | dd0c7c4 | 2008-06-18 15:38:57 +0200 | [diff] [blame] | 181 | if (req_type == _PAGE_CACHE_WB) { |
| 182 | u8 mtrr_type; |
| 183 | |
| 184 | mtrr_type = mtrr_type_lookup(start, end); |
Suresh Siddha | b6ff32d | 2009-04-09 14:26:51 -0700 | [diff] [blame] | 185 | if (mtrr_type != MTRR_TYPE_WRBACK) |
| 186 | return _PAGE_CACHE_UC_MINUS; |
| 187 | |
| 188 | return _PAGE_CACHE_WB; |
Andreas Herrmann | dd0c7c4 | 2008-06-18 15:38:57 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return req_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 194 | static int |
| 195 | chk_conflict(struct memtype *new, struct memtype *entry, unsigned long *type) |
Andreas Herrmann | 64fe44c | 2008-06-20 22:07:09 +0200 | [diff] [blame] | 196 | { |
| 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 Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 221 | static struct memtype *cached_entry; |
| 222 | static u64 cached_start; |
| 223 | |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 224 | static 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.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 251 | /* |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 252 | * 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 | */ |
| 263 | static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type, |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 264 | unsigned long *new_type) |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 265 | { |
| 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 | |
| 278 | out: |
| 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 | |
| 288 | static 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 | |
| 302 | out: |
| 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.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 312 | * 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 Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 321 | * 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.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 324 | * it will return a negative return value. |
| 325 | */ |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 326 | int reserve_memtype(u64 start, u64 end, unsigned long req_type, |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 327 | unsigned long *new_type) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 328 | { |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 329 | struct memtype *new, *entry; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 330 | unsigned long actual_type; |
Andreas Herrmann | f688726 | 2008-06-20 22:05:37 +0200 | [diff] [blame] | 331 | struct list_head *where; |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 332 | int is_range_ram; |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 333 | int err = 0; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 334 | |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 335 | BUG_ON(start >= end); /* end is exclusive */ |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame] | 336 | |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 337 | if (!pat_enabled) { |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 338 | /* This is identical to page table setting without PAT */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 339 | if (new_type) { |
| 340 | if (req_type == -1) |
| 341 | *new_type = _PAGE_CACHE_WB; |
Venkatesh Pallipadi | 5fc5174 | 2009-07-10 09:57:32 -0700 | [diff] [blame] | 342 | else if (req_type == _PAGE_CACHE_WC) |
| 343 | *new_type = _PAGE_CACHE_UC_MINUS; |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 344 | else |
| 345 | *new_type = req_type & _PAGE_CACHE_MASK; |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 346 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /* Low ISA region is always mapped WB in page table. No need to track */ |
Andreas Herrmann | bcc643d | 2008-06-20 21:58:46 +0200 | [diff] [blame] | 351 | if (is_ISA_range(start, end - 1)) { |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 352 | if (new_type) |
| 353 | *new_type = _PAGE_CACHE_WB; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Suresh Siddha | b6ff32d | 2009-04-09 14:26:51 -0700 | [diff] [blame] | 357 | /* |
| 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 364 | |
Suresh Siddha | 9597134 | 2009-01-13 10:21:30 -0800 | [diff] [blame] | 365 | if (new_type) |
| 366 | *new_type = actual_type; |
| 367 | |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 368 | is_range_ram = pat_pagerange_is_ram(start, end); |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 369 | if (is_range_ram == 1) |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 370 | return reserve_ram_pages_type(start, end, req_type, |
| 371 | new_type); |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 372 | else if (is_range_ram < 0) |
| 373 | return -EINVAL; |
| 374 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 375 | new = kmalloc(sizeof(struct memtype), GFP_KERNEL); |
| 376 | if (!new) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 377 | return -ENOMEM; |
| 378 | |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 379 | new->start = start; |
| 380 | new->end = end; |
| 381 | new->type = actual_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 382 | |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 383 | spin_lock(&memtype_lock); |
| 384 | |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 385 | 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 390 | /* Search for existing mapping that overlaps the current range */ |
Andreas Herrmann | f688726 | 2008-06-20 22:05:37 +0200 | [diff] [blame] | 391 | where = NULL; |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 392 | list_for_each_entry_continue(entry, &memtype_list, nd) { |
Andreas Herrmann | 33af903 | 2008-06-20 22:08:37 +0200 | [diff] [blame] | 393 | if (end <= entry->start) { |
Andreas Herrmann | f688726 | 2008-06-20 22:05:37 +0200 | [diff] [blame] | 394 | where = entry->nd.prev; |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 395 | cached_entry = list_entry(where, struct memtype, nd); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 396 | break; |
Andreas Herrmann | 33af903 | 2008-06-20 22:08:37 +0200 | [diff] [blame] | 397 | } else if (start <= entry->start) { /* end > entry->start */ |
Andreas Herrmann | 64fe44c | 2008-06-20 22:07:09 +0200 | [diff] [blame] | 398 | err = chk_conflict(new, entry, new_type); |
Andreas Herrmann | 33af903 | 2008-06-20 22:08:37 +0200 | [diff] [blame] | 399 | if (!err) { |
| 400 | dprintk("Overlap at 0x%Lx-0x%Lx\n", |
| 401 | entry->start, entry->end); |
| 402 | where = entry->nd.prev; |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 403 | cached_entry = list_entry(where, |
| 404 | struct memtype, nd); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 405 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 406 | break; |
Andreas Herrmann | 33af903 | 2008-06-20 22:08:37 +0200 | [diff] [blame] | 407 | } else if (start < entry->end) { /* start > entry->start */ |
Andreas Herrmann | 64fe44c | 2008-06-20 22:07:09 +0200 | [diff] [blame] | 408 | err = chk_conflict(new, entry, new_type); |
Andreas Herrmann | 33af903 | 2008-06-20 22:08:37 +0200 | [diff] [blame] | 409 | if (!err) { |
| 410 | dprintk("Overlap at 0x%Lx-0x%Lx\n", |
| 411 | entry->start, entry->end); |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 412 | 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 426 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (err) { |
Andreas Herrmann | 3e9c83b | 2008-06-20 22:04:02 +0200 | [diff] [blame] | 432 | 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 Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 435 | kfree(new); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 436 | spin_unlock(&memtype_lock); |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 437 | |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 438 | return err; |
| 439 | } |
| 440 | |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 441 | cached_start = start; |
| 442 | |
Andreas Herrmann | f688726 | 2008-06-20 22:05:37 +0200 | [diff] [blame] | 443 | if (where) |
| 444 | list_add(&new->nd, where); |
| 445 | else |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 446 | list_add_tail(&new->nd, &memtype_list); |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 447 | |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 448 | spin_unlock(&memtype_lock); |
Andreas Herrmann | 3e9c83b | 2008-06-20 22:04:02 +0200 | [diff] [blame] | 449 | |
| 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 454 | return err; |
| 455 | } |
| 456 | |
| 457 | int free_memtype(u64 start, u64 end) |
| 458 | { |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 459 | struct memtype *entry; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 460 | int err = -EINVAL; |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 461 | int is_range_ram; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 462 | |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame] | 463 | if (!pat_enabled) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 464 | return 0; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 465 | |
| 466 | /* Low ISA region is always mapped WB. No need to track */ |
Andreas Herrmann | bcc643d | 2008-06-20 21:58:46 +0200 | [diff] [blame] | 467 | if (is_ISA_range(start, end - 1)) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 468 | return 0; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 469 | |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 470 | is_range_ram = pat_pagerange_is_ram(start, end); |
Suresh Siddha | 9542ada | 2008-09-24 08:53:33 -0700 | [diff] [blame] | 471 | 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.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 476 | spin_lock(&memtype_lock); |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 477 | list_for_each_entry(entry, &memtype_list, nd) { |
| 478 | if (entry->start == start && entry->end == end) { |
Venki Pallipadi | 80c5e73 | 2008-08-19 16:28:01 -0700 | [diff] [blame] | 479 | if (cached_entry == entry || cached_start == start) |
| 480 | cached_entry = NULL; |
| 481 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 482 | list_del(&entry->nd); |
| 483 | kfree(entry); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 484 | err = 0; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | spin_unlock(&memtype_lock); |
| 489 | |
| 490 | if (err) { |
Ingo Molnar | 28eb559b | 2008-04-03 10:14:33 +0200 | [diff] [blame] | 491 | printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n", |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 492 | current->comm, current->pid, start, end); |
| 493 | } |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 494 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 495 | dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end); |
Ingo Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 496 | |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 497 | return err; |
| 498 | } |
| 499 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 500 | |
Venkatesh Pallipadi | 9fd126b | 2009-07-10 09:57:34 -0700 | [diff] [blame^] | 501 | /** |
| 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 | */ |
| 511 | int 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 | |
| 533 | out_free: |
| 534 | free_memtype(start, end); |
| 535 | ret = -EBUSY; |
| 536 | out_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 | */ |
| 545 | void io_free_memtype(resource_size_t start, resource_size_t end) |
| 546 | { |
| 547 | free_memtype(start, end); |
| 548 | } |
| 549 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 550 | pgprot_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 Molnar | d092633 | 2008-07-18 00:26:59 +0200 | [diff] [blame] | 556 | #ifdef CONFIG_STRICT_DEVMEM |
| 557 | /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/ |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 558 | static inline int range_is_allowed(unsigned long pfn, unsigned long size) |
| 559 | { |
| 560 | return 1; |
| 561 | } |
| 562 | #else |
Ravikiran G Thirumalai | 9e41bff | 2008-10-30 13:59:21 -0700 | [diff] [blame] | 563 | /* This check is needed to avoid cache aliasing when PAT is enabled */ |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 564 | static 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 Thirumalai | 9e41bff | 2008-10-30 13:59:21 -0700 | [diff] [blame] | 570 | if (!pat_enabled) |
| 571 | return 1; |
| 572 | |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 573 | 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 Molnar | d092633 | 2008-07-18 00:26:59 +0200 | [diff] [blame] | 585 | #endif /* CONFIG_STRICT_DEVMEM */ |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 586 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 587 | int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn, |
| 588 | unsigned long size, pgprot_t *vma_prot) |
| 589 | { |
Suresh Siddha | 0c3c8a1 | 2009-04-09 14:26:52 -0700 | [diff] [blame] | 590 | unsigned long flags = _PAGE_CACHE_WB; |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 591 | |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 592 | if (!range_is_allowed(pfn, size)) |
| 593 | return 0; |
| 594 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 595 | if (file->f_flags & O_SYNC) { |
venkatesh.pallipadi@intel.com | 28df82e | 2008-08-20 16:45:52 -0700 | [diff] [blame] | 596 | flags = _PAGE_CACHE_UC_MINUS; |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 597 | } |
| 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 Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 608 | if (!pat_enabled && |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 609 | !(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.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 614 | flags = _PAGE_CACHE_UC; |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 615 | } |
| 616 | #endif |
| 617 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 618 | *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) | |
| 619 | flags); |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 620 | return 1; |
| 621 | } |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 622 | |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 623 | /* |
Venkatesh Pallipadi | 7880f74 | 2009-02-24 17:35:13 -0800 | [diff] [blame] | 624 | * 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 | */ |
| 627 | int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags) |
| 628 | { |
| 629 | unsigned long id_sz; |
| 630 | |
Venkatesh Pallipadi | 5fc5174 | 2009-07-10 09:57:32 -0700 | [diff] [blame] | 631 | if (base >= __pa(high_memory)) |
Venkatesh Pallipadi | 7880f74 | 2009-02-24 17:35:13 -0800 | [diff] [blame] | 632 | 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 651 | * 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.com | cdecff6 | 2009-01-09 16:13:12 -0800 | [diff] [blame] | 655 | static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot, |
| 656 | int strict_prot) |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 657 | { |
| 658 | int is_ram = 0; |
Venkatesh Pallipadi | 7880f74 | 2009-02-24 17:35:13 -0800 | [diff] [blame] | 659 | int ret; |
venkatesh.pallipadi@intel.com | cdecff6 | 2009-01-09 16:13:12 -0800 | [diff] [blame] | 660 | unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK); |
Suresh Siddha | 0c3c8a1 | 2009-04-09 14:26:52 -0700 | [diff] [blame] | 661 | unsigned long flags = want_flags; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 662 | |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 663 | is_ram = pat_pagerange_is_ram(paddr, paddr + size); |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 664 | |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 665 | /* |
Pallipadi, Venkatesh | 4bb9c5c | 2009-03-12 17:45:27 -0700 | [diff] [blame] | 666 | * reserve_pfn_range() doesn't support RAM pages. Maintain the current |
| 667 | * behavior with RAM pages by returning success. |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 668 | */ |
| 669 | if (is_ram != 0) |
Pallipadi, Venkatesh | 4bb9c5c | 2009-03-12 17:45:27 -0700 | [diff] [blame] | 670 | return 0; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 671 | |
| 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.com | cdecff6 | 2009-01-09 16:13:12 -0800 | [diff] [blame] | 677 | 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Venkatesh Pallipadi | 7880f74 | 2009-02-24 17:35:13 -0800 | [diff] [blame] | 697 | if (kernel_map_sync_memtype(paddr, size, flags) < 0) { |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 698 | free_memtype(paddr, paddr + size); |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 699 | 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 | */ |
| 708 | static void free_pfn_range(u64 paddr, unsigned long size) |
| 709 | { |
| 710 | int is_ram; |
| 711 | |
Suresh Siddha | be03d9e | 2009-02-11 11:20:23 -0800 | [diff] [blame] | 712 | is_ram = pat_pagerange_is_ram(paddr, paddr + size); |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 713 | 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 723 | */ |
| 724 | int track_pfn_vma_copy(struct vm_area_struct *vma) |
| 725 | { |
H. Peter Anvin | c1c15b6 | 2008-12-23 10:10:40 -0800 | [diff] [blame] | 726 | resource_size_t paddr; |
venkatesh.pallipadi@intel.com | 982d789 | 2008-12-19 13:47:28 -0800 | [diff] [blame] | 727 | unsigned long prot; |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 728 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
venkatesh.pallipadi@intel.com | cdecff6 | 2009-01-09 16:13:12 -0800 | [diff] [blame] | 729 | pgprot_t pgprot; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 730 | |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 731 | /* |
| 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 736 | if (is_linear_pfn_mapping(vma)) { |
| 737 | /* |
venkatesh.pallipadi@intel.com | 982d789 | 2008-12-19 13:47:28 -0800 | [diff] [blame] | 738 | * reserve the whole chunk covered by vma. We need the |
| 739 | * starting address and protection from pte. |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 740 | */ |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 741 | if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) { |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 742 | WARN_ON_ONCE(1); |
venkatesh.pallipadi@intel.com | 982d789 | 2008-12-19 13:47:28 -0800 | [diff] [blame] | 743 | return -EINVAL; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 744 | } |
venkatesh.pallipadi@intel.com | cdecff6 | 2009-01-09 16:13:12 -0800 | [diff] [blame] | 745 | pgprot = __pgprot(prot); |
| 746 | return reserve_pfn_range(paddr, vma_size, &pgprot, 1); |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 747 | } |
| 748 | |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 749 | return 0; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 750 | } |
| 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 759 | */ |
venkatesh.pallipadi@intel.com | e4b866e | 2009-01-09 16:13:11 -0800 | [diff] [blame] | 760 | int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot, |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 761 | unsigned long pfn, unsigned long size) |
| 762 | { |
H. Peter Anvin | c1c15b6 | 2008-12-23 10:10:40 -0800 | [diff] [blame] | 763 | resource_size_t paddr; |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 764 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 765 | |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 766 | /* |
| 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 771 | if (is_linear_pfn_mapping(vma)) { |
| 772 | /* reserve the whole chunk starting from vm_pgoff */ |
H. Peter Anvin | c1c15b6 | 2008-12-23 10:10:40 -0800 | [diff] [blame] | 773 | paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT; |
venkatesh.pallipadi@intel.com | cdecff6 | 2009-01-09 16:13:12 -0800 | [diff] [blame] | 774 | return reserve_pfn_range(paddr, vma_size, prot, 0); |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 775 | } |
| 776 | |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 777 | return 0; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 778 | } |
| 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 | */ |
| 785 | void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn, |
| 786 | unsigned long size) |
| 787 | { |
H. Peter Anvin | c1c15b6 | 2008-12-23 10:10:40 -0800 | [diff] [blame] | 788 | resource_size_t paddr; |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 789 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 790 | |
Pallipadi, Venkatesh | 4b06504 | 2009-04-08 15:37:16 -0700 | [diff] [blame] | 791 | /* |
| 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.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 796 | if (is_linear_pfn_mapping(vma)) { |
| 797 | /* free the whole chunk starting from vm_pgoff */ |
H. Peter Anvin | c1c15b6 | 2008-12-23 10:10:40 -0800 | [diff] [blame] | 798 | paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT; |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 799 | free_pfn_range(paddr, vma_size); |
| 800 | return; |
| 801 | } |
venkatesh.pallipadi@intel.com | 5899329 | 2008-12-18 11:41:30 -0800 | [diff] [blame] | 802 | } |
| 803 | |
venkatesh.pallipadi@intel.com | 2520bd3 | 2008-12-18 11:41:32 -0800 | [diff] [blame] | 804 | pgprot_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 Molnar | 92b9af9 | 2009-02-28 14:09:27 +0100 | [diff] [blame] | 811 | EXPORT_SYMBOL_GPL(pgprot_writecombine); |
venkatesh.pallipadi@intel.com | 2520bd3 | 2008-12-18 11:41:32 -0800 | [diff] [blame] | 812 | |
Andreas Herrmann | 012f09e | 2008-08-06 16:23:08 +0200 | [diff] [blame] | 813 | #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT) |
venkatesh.pallipadi@intel.com | fec0962 | 2008-07-18 16:08:14 -0700 | [diff] [blame] | 814 | |
| 815 | /* get Nth element of the linked list */ |
| 816 | static 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 Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 836 | |
venkatesh.pallipadi@intel.com | fec0962 | 2008-07-18 16:08:14 -0700 | [diff] [blame] | 837 | return NULL; |
| 838 | } |
| 839 | |
| 840 | static 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 | |
| 850 | static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 851 | { |
| 852 | ++*pos; |
| 853 | return memtype_get_idx(*pos); |
| 854 | } |
| 855 | |
| 856 | static void memtype_seq_stop(struct seq_file *seq, void *v) |
| 857 | { |
| 858 | } |
| 859 | |
| 860 | static 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 Molnar | ad2cde1 | 2008-09-30 13:20:45 +0200 | [diff] [blame] | 867 | |
venkatesh.pallipadi@intel.com | fec0962 | 2008-07-18 16:08:14 -0700 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static 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 | |
| 878 | static int memtype_seq_open(struct inode *inode, struct file *file) |
| 879 | { |
| 880 | return seq_open(file, &memtype_seq_ops); |
| 881 | } |
| 882 | |
| 883 | static 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 | |
| 890 | static 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 | |
| 897 | late_initcall(pat_memtype_list_init); |
| 898 | |
Andreas Herrmann | 012f09e | 2008-08-06 16:23:08 +0200 | [diff] [blame] | 899 | #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */ |