pat.c 15 KB

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