pat.c 14 KB

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