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