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 | |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/gfp.h> |
| 13 | #include <linux/fs.h> |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 14 | #include <linux/bootmem.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 15 | |
| 16 | #include <asm/msr.h> |
| 17 | #include <asm/tlbflush.h> |
| 18 | #include <asm/processor.h> |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 19 | #include <asm/page.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 20 | #include <asm/pgtable.h> |
| 21 | #include <asm/pat.h> |
| 22 | #include <asm/e820.h> |
| 23 | #include <asm/cacheflush.h> |
| 24 | #include <asm/fcntl.h> |
| 25 | #include <asm/mtrr.h> |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 26 | #include <asm/io.h> |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 27 | |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 28 | #ifdef CONFIG_X86_PAT |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 29 | int __read_mostly pat_enabled = 1; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 30 | |
Avi Kivity | 31f4d87 | 2008-05-14 12:20:32 +0300 | [diff] [blame] | 31 | void __cpuinit pat_disable(char *reason) |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 32 | { |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 33 | pat_enabled = 0; |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 34 | printk(KERN_INFO "%s\n", reason); |
| 35 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 36 | |
Andrew Morton | be524fb | 2008-05-29 00:01:28 -0700 | [diff] [blame] | 37 | static int __init nopat(char *str) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 38 | { |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 39 | pat_disable("PAT support disabled."); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | early_param("nopat", nopat); |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 43 | #endif |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 44 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 45 | |
| 46 | static int debug_enable; |
| 47 | static int __init pat_debug_setup(char *str) |
| 48 | { |
| 49 | debug_enable = 1; |
| 50 | return 0; |
| 51 | } |
| 52 | __setup("debugpat", pat_debug_setup); |
| 53 | |
| 54 | #define dprintk(fmt, arg...) \ |
| 55 | do { if (debug_enable) printk(KERN_INFO fmt, ##arg); } while (0) |
| 56 | |
| 57 | |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 58 | static u64 __read_mostly boot_pat_state; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 59 | |
| 60 | enum { |
| 61 | PAT_UC = 0, /* uncached */ |
| 62 | PAT_WC = 1, /* Write combining */ |
| 63 | PAT_WT = 4, /* Write Through */ |
| 64 | PAT_WP = 5, /* Write Protected */ |
| 65 | PAT_WB = 6, /* Write Back (default) */ |
| 66 | PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */ |
| 67 | }; |
| 68 | |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 69 | #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8)) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 70 | |
| 71 | void pat_init(void) |
| 72 | { |
| 73 | u64 pat; |
| 74 | |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 75 | if (!pat_enabled) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 76 | return; |
| 77 | |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 78 | /* Paranoia check. */ |
Andreas Herrmann | 97cfab6 | 2008-06-10 16:05:18 +0200 | [diff] [blame] | 79 | if (!cpu_has_pat && boot_pat_state) { |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 80 | /* |
Andreas Herrmann | 97cfab6 | 2008-06-10 16:05:18 +0200 | [diff] [blame] | 81 | * If this happens we are on a secondary CPU, but |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 82 | * switched to PAT on the boot CPU. We have no way to |
| 83 | * undo PAT. |
Andreas Herrmann | 97cfab6 | 2008-06-10 16:05:18 +0200 | [diff] [blame] | 84 | */ |
| 85 | printk(KERN_ERR "PAT enabled, " |
| 86 | "but not supported by secondary CPU\n"); |
| 87 | BUG(); |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 88 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 89 | |
| 90 | /* Set PWT to Write-Combining. All other bits stay the same */ |
| 91 | /* |
| 92 | * PTE encoding used in Linux: |
| 93 | * PAT |
| 94 | * |PCD |
| 95 | * ||PWT |
| 96 | * ||| |
| 97 | * 000 WB _PAGE_CACHE_WB |
| 98 | * 001 WC _PAGE_CACHE_WC |
| 99 | * 010 UC- _PAGE_CACHE_UC_MINUS |
| 100 | * 011 UC _PAGE_CACHE_UC |
| 101 | * PAT bit unused |
| 102 | */ |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 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); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 105 | |
| 106 | /* Boot CPU check */ |
Thomas Gleixner | 8d4a430 | 2008-05-08 09:18:43 +0200 | [diff] [blame] | 107 | if (!boot_pat_state) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 108 | rdmsrl(MSR_IA32_CR_PAT, boot_pat_state); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 109 | |
| 110 | wrmsrl(MSR_IA32_CR_PAT, pat); |
| 111 | printk(KERN_INFO "x86 PAT enabled: cpu %d, old 0x%Lx, new 0x%Lx\n", |
| 112 | smp_processor_id(), boot_pat_state, pat); |
| 113 | } |
| 114 | |
| 115 | #undef PAT |
| 116 | |
| 117 | static char *cattr_name(unsigned long flags) |
| 118 | { |
| 119 | switch (flags & _PAGE_CACHE_MASK) { |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 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"; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * The global memtype list keeps track of memory type for specific |
| 130 | * physical memory areas. Conflicting memory types in different |
| 131 | * mappings can cause CPU cache corruption. To avoid this we keep track. |
| 132 | * |
| 133 | * The list is sorted based on starting address and can contain multiple |
| 134 | * entries for each address (this allows reference counting for overlapping |
| 135 | * areas). All the aliases have the same cache attributes of course. |
| 136 | * Zero attributes are represented as holes. |
| 137 | * |
| 138 | * Currently the data structure is a list because the number of mappings |
| 139 | * are expected to be relatively small. If this should be a problem |
| 140 | * it could be changed to a rbtree or similar. |
| 141 | * |
| 142 | * memtype_lock protects the whole list. |
| 143 | */ |
| 144 | |
| 145 | struct memtype { |
| 146 | u64 start; |
| 147 | u64 end; |
| 148 | unsigned long type; |
| 149 | struct list_head nd; |
| 150 | }; |
| 151 | |
| 152 | static LIST_HEAD(memtype_list); |
| 153 | static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */ |
| 154 | |
| 155 | /* |
| 156 | * Does intersection of PAT memory type and MTRR memory type and returns |
| 157 | * the resulting memory type as PAT understands it. |
| 158 | * (Type in pat and mtrr will not have same value) |
| 159 | * The intersection is based on "Effective Memory Type" tables in IA-32 |
| 160 | * SDM vol 3a |
| 161 | */ |
Hugh Dickins | 6cf514f | 2008-06-16 18:42:43 +0100 | [diff] [blame] | 162 | 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] | 163 | { |
Venki Pallipadi | c26421d | 2008-05-29 12:01:44 -0700 | [diff] [blame] | 164 | /* |
| 165 | * Look for MTRR hint to get the effective type in case where PAT |
| 166 | * request is for WB. |
| 167 | */ |
Andreas Herrmann | dd0c7c4 | 2008-06-18 15:38:57 +0200 | [diff] [blame] | 168 | if (req_type == _PAGE_CACHE_WB) { |
| 169 | u8 mtrr_type; |
| 170 | |
| 171 | mtrr_type = mtrr_type_lookup(start, end); |
| 172 | if (mtrr_type == MTRR_TYPE_UNCACHABLE) |
| 173 | return _PAGE_CACHE_UC; |
| 174 | if (mtrr_type == MTRR_TYPE_WRCOMB) |
| 175 | return _PAGE_CACHE_WC; |
| 176 | } |
| 177 | |
| 178 | return req_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 179 | } |
| 180 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 181 | /* |
| 182 | * req_type typically has one of the: |
| 183 | * - _PAGE_CACHE_WB |
| 184 | * - _PAGE_CACHE_WC |
| 185 | * - _PAGE_CACHE_UC_MINUS |
| 186 | * - _PAGE_CACHE_UC |
| 187 | * |
| 188 | * req_type will have a special case value '-1', when requester want to inherit |
| 189 | * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS. |
| 190 | * |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 191 | * If new_type is NULL, function will return an error if it cannot reserve the |
| 192 | * region with req_type. If new_type is non-NULL, function will return |
| 193 | * available type in new_type in case of no error. In case of any error |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 194 | * it will return a negative return value. |
| 195 | */ |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 196 | int reserve_memtype(u64 start, u64 end, unsigned long req_type, |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 197 | unsigned long *new_type) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 198 | { |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 199 | struct memtype *new, *entry; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 200 | unsigned long actual_type; |
| 201 | int err = 0; |
| 202 | |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame^] | 203 | BUG_ON(start >= end); /* end is exclusive */ |
| 204 | |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 205 | if (!pat_enabled) { |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 206 | /* This is identical to page table setting without PAT */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 207 | if (new_type) { |
| 208 | if (req_type == -1) |
| 209 | *new_type = _PAGE_CACHE_WB; |
| 210 | else |
| 211 | *new_type = req_type & _PAGE_CACHE_MASK; |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 212 | } |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* 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] | 217 | if (is_ISA_range(start, end - 1)) { |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 218 | if (new_type) |
| 219 | *new_type = _PAGE_CACHE_WB; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 223 | if (req_type == -1) { |
| 224 | /* |
Venki Pallipadi | c26421d | 2008-05-29 12:01:44 -0700 | [diff] [blame] | 225 | * Call mtrr_lookup to get the type hint. This is an |
| 226 | * optimization for /dev/mem mmap'ers into WB memory (BIOS |
| 227 | * tools and ACPI tools). Use WB request for WB memory and use |
| 228 | * UC_MINUS otherwise. |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 229 | */ |
| 230 | u8 mtrr_type = mtrr_type_lookup(start, end); |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 231 | |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame^] | 232 | if (mtrr_type == MTRR_TYPE_WRBACK) |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 233 | actual_type = _PAGE_CACHE_WB; |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame^] | 234 | else |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 235 | actual_type = _PAGE_CACHE_UC_MINUS; |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame^] | 236 | } else |
| 237 | actual_type = pat_x_mtrr_type(start, end, |
| 238 | req_type & _PAGE_CACHE_MASK); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 239 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 240 | new = kmalloc(sizeof(struct memtype), GFP_KERNEL); |
| 241 | if (!new) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 242 | return -ENOMEM; |
| 243 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 244 | new->start = start; |
| 245 | new->end = end; |
| 246 | new->type = actual_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 247 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 248 | if (new_type) |
| 249 | *new_type = actual_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 250 | |
| 251 | spin_lock(&memtype_lock); |
| 252 | |
| 253 | /* Search for existing mapping that overlaps the current range */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 254 | list_for_each_entry(entry, &memtype_list, nd) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 255 | struct memtype *saved_ptr; |
| 256 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 257 | if (entry->start >= end) { |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 258 | dprintk("New Entry\n"); |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 259 | list_add(&new->nd, entry->nd.prev); |
| 260 | new = NULL; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 261 | break; |
| 262 | } |
| 263 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 264 | if (start <= entry->start && end >= entry->start) { |
| 265 | if (actual_type != entry->type && new_type) { |
| 266 | actual_type = entry->type; |
| 267 | *new_type = actual_type; |
| 268 | new->type = actual_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 271 | if (actual_type != entry->type) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 272 | printk( |
| 273 | KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n", |
| 274 | current->comm, current->pid, |
| 275 | start, end, |
| 276 | cattr_name(actual_type), |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 277 | cattr_name(entry->type)); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 278 | err = -EBUSY; |
| 279 | break; |
| 280 | } |
| 281 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 282 | saved_ptr = entry; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 283 | /* |
| 284 | * Check to see whether the request overlaps more |
| 285 | * than one entry in the list |
| 286 | */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 287 | list_for_each_entry_continue(entry, &memtype_list, nd) { |
| 288 | if (end <= entry->start) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 292 | if (actual_type != entry->type) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 293 | printk( |
| 294 | KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n", |
| 295 | current->comm, current->pid, |
| 296 | start, end, |
| 297 | cattr_name(actual_type), |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 298 | cattr_name(entry->type)); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 299 | err = -EBUSY; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (err) { |
| 305 | break; |
| 306 | } |
| 307 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 308 | dprintk("Overlap at 0x%Lx-0x%Lx\n", |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 309 | saved_ptr->start, saved_ptr->end); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 310 | /* No conflict. Go ahead and add this new entry */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 311 | list_add(&new->nd, saved_ptr->nd.prev); |
| 312 | new = NULL; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 313 | break; |
| 314 | } |
| 315 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 316 | if (start < entry->end) { |
| 317 | if (actual_type != entry->type && new_type) { |
| 318 | actual_type = entry->type; |
| 319 | *new_type = actual_type; |
| 320 | new->type = actual_type; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 323 | if (actual_type != entry->type) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 324 | printk( |
| 325 | KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n", |
| 326 | current->comm, current->pid, |
| 327 | start, end, |
| 328 | cattr_name(actual_type), |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 329 | cattr_name(entry->type)); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 330 | err = -EBUSY; |
| 331 | break; |
| 332 | } |
| 333 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 334 | saved_ptr = entry; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 335 | /* |
| 336 | * Check to see whether the request overlaps more |
| 337 | * than one entry in the list |
| 338 | */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 339 | list_for_each_entry_continue(entry, &memtype_list, nd) { |
| 340 | if (end <= entry->start) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 344 | if (actual_type != entry->type) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 345 | printk( |
| 346 | KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n", |
| 347 | current->comm, current->pid, |
| 348 | start, end, |
| 349 | cattr_name(actual_type), |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 350 | cattr_name(entry->type)); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 351 | err = -EBUSY; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (err) { |
| 357 | break; |
| 358 | } |
| 359 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 360 | dprintk("Overlap at 0x%Lx-0x%Lx\n", |
Linus Torvalds | 86cf02f | 2008-04-27 11:59:30 -0700 | [diff] [blame] | 361 | saved_ptr->start, saved_ptr->end); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 362 | /* No conflict. Go ahead and add this new entry */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 363 | list_add(&new->nd, &saved_ptr->nd); |
| 364 | new = NULL; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (err) { |
Ingo Molnar | 28eb559b | 2008-04-03 10:14:33 +0200 | [diff] [blame] | 370 | printk(KERN_INFO |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 371 | "reserve_memtype failed 0x%Lx-0x%Lx, track %s, req %s\n", |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 372 | start, end, cattr_name(new->type), |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 373 | cattr_name(req_type)); |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 374 | kfree(new); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 375 | spin_unlock(&memtype_lock); |
| 376 | return err; |
| 377 | } |
| 378 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 379 | if (new) { |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 380 | /* No conflict. Not yet added to the list. Add to the tail */ |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 381 | list_add_tail(&new->nd, &memtype_list); |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 382 | dprintk("New Entry\n"); |
Ingo Molnar | 28eb559b | 2008-04-03 10:14:33 +0200 | [diff] [blame] | 383 | } |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 384 | |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 385 | if (new_type) { |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 386 | dprintk( |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 387 | "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n", |
| 388 | start, end, cattr_name(actual_type), |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 389 | cattr_name(req_type), cattr_name(*new_type)); |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 390 | } else { |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 391 | dprintk( |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 392 | "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s\n", |
| 393 | start, end, cattr_name(actual_type), |
| 394 | cattr_name(req_type)); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | spin_unlock(&memtype_lock); |
| 398 | return err; |
| 399 | } |
| 400 | |
| 401 | int free_memtype(u64 start, u64 end) |
| 402 | { |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 403 | struct memtype *entry; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 404 | int err = -EINVAL; |
| 405 | |
Andreas Herrmann | 69e26be | 2008-06-20 22:03:06 +0200 | [diff] [blame^] | 406 | if (!pat_enabled) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 407 | return 0; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 408 | |
| 409 | /* Low ISA region is always mapped WB. No need to track */ |
Andreas Herrmann | bcc643d | 2008-06-20 21:58:46 +0200 | [diff] [blame] | 410 | if (is_ISA_range(start, end - 1)) |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 411 | return 0; |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 412 | |
| 413 | spin_lock(&memtype_lock); |
Andreas Herrmann | ac97991 | 2008-06-20 22:01:49 +0200 | [diff] [blame] | 414 | list_for_each_entry(entry, &memtype_list, nd) { |
| 415 | if (entry->start == start && entry->end == end) { |
| 416 | list_del(&entry->nd); |
| 417 | kfree(entry); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 418 | err = 0; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | spin_unlock(&memtype_lock); |
| 423 | |
| 424 | if (err) { |
Ingo Molnar | 28eb559b | 2008-04-03 10:14:33 +0200 | [diff] [blame] | 425 | 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] | 426 | current->comm, current->pid, start, end); |
| 427 | } |
venkatesh.pallipadi@intel.com | 6997ab4 | 2008-03-18 17:00:25 -0700 | [diff] [blame] | 428 | |
Venki Pallipadi | 77b52b4 | 2008-05-05 19:09:10 -0700 | [diff] [blame] | 429 | dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end); |
venkatesh.pallipadi@intel.com | 2e5d9c8 | 2008-03-18 17:00:14 -0700 | [diff] [blame] | 430 | return err; |
| 431 | } |
| 432 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 433 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 434 | /* |
| 435 | * /dev/mem mmap interface. The memtype used for mapping varies: |
| 436 | * - Use UC for mappings with O_SYNC flag |
| 437 | * - Without O_SYNC flag, if there is any conflict in reserve_memtype, |
| 438 | * inherit the memtype from existing mapping. |
| 439 | * - Else use UC_MINUS memtype (for backward compatibility with existing |
| 440 | * X drivers. |
| 441 | */ |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 442 | pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, |
| 443 | unsigned long size, pgprot_t vma_prot) |
| 444 | { |
| 445 | return vma_prot; |
| 446 | } |
| 447 | |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 448 | #ifdef CONFIG_NONPROMISC_DEVMEM |
| 449 | /* This check is done in drivers/char/mem.c in case of NONPROMISC_DEVMEM*/ |
| 450 | static inline int range_is_allowed(unsigned long pfn, unsigned long size) |
| 451 | { |
| 452 | return 1; |
| 453 | } |
| 454 | #else |
| 455 | static inline int range_is_allowed(unsigned long pfn, unsigned long size) |
| 456 | { |
| 457 | u64 from = ((u64)pfn) << PAGE_SHIFT; |
| 458 | u64 to = from + size; |
| 459 | u64 cursor = from; |
| 460 | |
| 461 | while (cursor < to) { |
| 462 | if (!devmem_is_allowed(pfn)) { |
| 463 | printk(KERN_INFO |
| 464 | "Program %s tried to access /dev/mem between %Lx->%Lx.\n", |
| 465 | current->comm, from, to); |
| 466 | return 0; |
| 467 | } |
| 468 | cursor += PAGE_SIZE; |
| 469 | pfn++; |
| 470 | } |
| 471 | return 1; |
| 472 | } |
| 473 | #endif /* CONFIG_NONPROMISC_DEVMEM */ |
| 474 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 475 | int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn, |
| 476 | unsigned long size, pgprot_t *vma_prot) |
| 477 | { |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 478 | u64 offset = ((u64) pfn) << PAGE_SHIFT; |
| 479 | unsigned long flags = _PAGE_CACHE_UC_MINUS; |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 480 | int retval; |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 481 | |
Venki Pallipadi | 0124cec | 2008-04-26 11:32:12 -0700 | [diff] [blame] | 482 | if (!range_is_allowed(pfn, size)) |
| 483 | return 0; |
| 484 | |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 485 | if (file->f_flags & O_SYNC) { |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 486 | flags = _PAGE_CACHE_UC; |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | #ifdef CONFIG_X86_32 |
| 490 | /* |
| 491 | * On the PPro and successors, the MTRRs are used to set |
| 492 | * memory types for physical addresses outside main memory, |
| 493 | * so blindly setting UC or PWT on those pages is wrong. |
| 494 | * For Pentiums and earlier, the surround logic should disable |
| 495 | * caching for the high addresses through the KEN pin, but |
| 496 | * we maintain the tradition of paranoia in this code. |
| 497 | */ |
Andreas Herrmann | 499f8f8 | 2008-06-10 16:06:21 +0200 | [diff] [blame] | 498 | if (!pat_enabled && |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 499 | !(boot_cpu_has(X86_FEATURE_MTRR) || |
| 500 | boot_cpu_has(X86_FEATURE_K6_MTRR) || |
| 501 | boot_cpu_has(X86_FEATURE_CYRIX_ARR) || |
| 502 | boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) && |
| 503 | (pfn << PAGE_SHIFT) >= __pa(high_memory)) { |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 504 | flags = _PAGE_CACHE_UC; |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 505 | } |
| 506 | #endif |
| 507 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 508 | /* |
| 509 | * With O_SYNC, we can only take UC mapping. Fail if we cannot. |
| 510 | * Without O_SYNC, we want to get |
| 511 | * - WB for WB-able memory and no other conflicting mappings |
| 512 | * - UC_MINUS for non-WB-able memory with no other conflicting mappings |
| 513 | * - Inherit from confliting mappings otherwise |
| 514 | */ |
| 515 | if (flags != _PAGE_CACHE_UC_MINUS) { |
| 516 | retval = reserve_memtype(offset, offset + size, flags, NULL); |
| 517 | } else { |
Ingo Molnar | f022bfd | 2008-03-21 15:42:28 +0100 | [diff] [blame] | 518 | retval = reserve_memtype(offset, offset + size, -1, &flags); |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (retval < 0) |
| 522 | return 0; |
| 523 | |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 524 | if (pfn <= max_pfn_mapped && |
Andreas Herrmann | cd7a4e9 | 2008-06-10 16:05:39 +0200 | [diff] [blame] | 525 | ioremap_change_attr((unsigned long)__va(offset), size, flags) < 0) { |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 526 | free_memtype(offset, offset + size); |
Ingo Molnar | 28eb559b | 2008-04-03 10:14:33 +0200 | [diff] [blame] | 527 | printk(KERN_INFO |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 528 | "%s:%d /dev/mem ioremap_change_attr failed %s for %Lx-%Lx\n", |
| 529 | current->comm, current->pid, |
| 530 | cattr_name(flags), |
Pranith Kumar | afc8534 | 2008-05-12 14:52:26 +0530 | [diff] [blame] | 531 | offset, (unsigned long long)(offset + size)); |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) | |
| 536 | flags); |
venkatesh.pallipadi@intel.com | f0970c1 | 2008-03-18 17:00:20 -0700 | [diff] [blame] | 537 | return 1; |
| 538 | } |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 539 | |
| 540 | void map_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot) |
| 541 | { |
| 542 | u64 addr = (u64)pfn << PAGE_SHIFT; |
| 543 | unsigned long flags; |
| 544 | unsigned long want_flags = (pgprot_val(vma_prot) & _PAGE_CACHE_MASK); |
| 545 | |
| 546 | reserve_memtype(addr, addr + size, want_flags, &flags); |
| 547 | if (flags != want_flags) { |
Ingo Molnar | 28eb559b | 2008-04-03 10:14:33 +0200 | [diff] [blame] | 548 | printk(KERN_INFO |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 549 | "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n", |
| 550 | current->comm, current->pid, |
| 551 | cattr_name(want_flags), |
Pranith Kumar | afc8534 | 2008-05-12 14:52:26 +0530 | [diff] [blame] | 552 | addr, (unsigned long long)(addr + size), |
venkatesh.pallipadi@intel.com | e7f260a | 2008-03-18 17:00:21 -0700 | [diff] [blame] | 553 | cattr_name(flags)); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void unmap_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot) |
| 558 | { |
| 559 | u64 addr = (u64)pfn << PAGE_SHIFT; |
| 560 | |
| 561 | free_memtype(addr, addr + size); |
| 562 | } |