pat.c 14 KB

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