pat.c 15 KB

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