pat.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 <asm/msr.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/processor.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/pat.h>
  18. #include <asm/e820.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/fcntl.h>
  21. #include <asm/mtrr.h>
  22. int pat_wc_enabled = 1;
  23. static u64 __read_mostly boot_pat_state;
  24. static int nopat(char *str)
  25. {
  26. pat_wc_enabled = 0;
  27. printk(KERN_INFO "x86: PAT support disabled.\n");
  28. return 0;
  29. }
  30. early_param("nopat", nopat);
  31. static int pat_known_cpu(void)
  32. {
  33. if (!pat_wc_enabled)
  34. return 0;
  35. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  36. (boot_cpu_data.x86 == 0xF ||
  37. (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model >= 15))) {
  38. if (cpu_has_pat) {
  39. return 1;
  40. }
  41. }
  42. pat_wc_enabled = 0;
  43. printk(KERN_INFO "CPU and/or kernel does not support PAT.\n");
  44. return 0;
  45. }
  46. enum {
  47. PAT_UC = 0, /* uncached */
  48. PAT_WC = 1, /* Write combining */
  49. PAT_WT = 4, /* Write Through */
  50. PAT_WP = 5, /* Write Protected */
  51. PAT_WB = 6, /* Write Back (default) */
  52. PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */
  53. };
  54. #define PAT(x,y) ((u64)PAT_ ## y << ((x)*8))
  55. void pat_init(void)
  56. {
  57. u64 pat;
  58. #ifndef CONFIG_X86_PAT
  59. nopat(NULL);
  60. #endif
  61. /* Boot CPU enables PAT based on CPU feature */
  62. if (!smp_processor_id() && !pat_known_cpu())
  63. return;
  64. /* APs enable PAT iff boot CPU has enabled it before */
  65. if (smp_processor_id() && !pat_wc_enabled)
  66. return;
  67. /* Set PWT to Write-Combining. All other bits stay the same */
  68. /*
  69. * PTE encoding used in Linux:
  70. * PAT
  71. * |PCD
  72. * ||PWT
  73. * |||
  74. * 000 WB _PAGE_CACHE_WB
  75. * 001 WC _PAGE_CACHE_WC
  76. * 010 UC- _PAGE_CACHE_UC_MINUS
  77. * 011 UC _PAGE_CACHE_UC
  78. * PAT bit unused
  79. */
  80. pat = PAT(0,WB) | PAT(1,WC) | PAT(2,UC_MINUS) | PAT(3,UC) |
  81. PAT(4,WB) | PAT(5,WC) | PAT(6,UC_MINUS) | PAT(7,UC);
  82. /* Boot CPU check */
  83. if (!smp_processor_id()) {
  84. rdmsrl(MSR_IA32_CR_PAT, boot_pat_state);
  85. }
  86. wrmsrl(MSR_IA32_CR_PAT, pat);
  87. printk(KERN_INFO "x86 PAT enabled: cpu %d, old 0x%Lx, new 0x%Lx\n",
  88. smp_processor_id(), boot_pat_state, pat);
  89. }
  90. #undef PAT
  91. static char *cattr_name(unsigned long flags)
  92. {
  93. switch (flags & _PAGE_CACHE_MASK) {
  94. case _PAGE_CACHE_UC: return "uncached";
  95. case _PAGE_CACHE_UC_MINUS: return "uncached-minus";
  96. case _PAGE_CACHE_WB: return "write-back";
  97. case _PAGE_CACHE_WC: return "write-combining";
  98. default: return "broken";
  99. }
  100. }
  101. /*
  102. * The global memtype list keeps track of memory type for specific
  103. * physical memory areas. Conflicting memory types in different
  104. * mappings can cause CPU cache corruption. To avoid this we keep track.
  105. *
  106. * The list is sorted based on starting address and can contain multiple
  107. * entries for each address (this allows reference counting for overlapping
  108. * areas). All the aliases have the same cache attributes of course.
  109. * Zero attributes are represented as holes.
  110. *
  111. * Currently the data structure is a list because the number of mappings
  112. * are expected to be relatively small. If this should be a problem
  113. * it could be changed to a rbtree or similar.
  114. *
  115. * memtype_lock protects the whole list.
  116. */
  117. struct memtype {
  118. u64 start;
  119. u64 end;
  120. unsigned long type;
  121. struct list_head nd;
  122. };
  123. static LIST_HEAD(memtype_list);
  124. static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */
  125. /*
  126. * Does intersection of PAT memory type and MTRR memory type and returns
  127. * the resulting memory type as PAT understands it.
  128. * (Type in pat and mtrr will not have same value)
  129. * The intersection is based on "Effective Memory Type" tables in IA-32
  130. * SDM vol 3a
  131. */
  132. static int pat_x_mtrr_type(u64 start, u64 end, unsigned long prot,
  133. unsigned long *ret_prot)
  134. {
  135. unsigned long pat_type;
  136. u8 mtrr_type;
  137. mtrr_type = mtrr_type_lookup(start, end);
  138. if (mtrr_type == 0xFF) { /* MTRR not enabled */
  139. *ret_prot = prot;
  140. return 0;
  141. }
  142. if (mtrr_type == 0xFE) { /* MTRR match error */
  143. *ret_prot = _PAGE_CACHE_UC;
  144. return -1;
  145. }
  146. if (mtrr_type != MTRR_TYPE_UNCACHABLE &&
  147. mtrr_type != MTRR_TYPE_WRBACK &&
  148. mtrr_type != MTRR_TYPE_WRCOMB) { /* MTRR type unhandled */
  149. *ret_prot = _PAGE_CACHE_UC;
  150. return -1;
  151. }
  152. pat_type = prot & _PAGE_CACHE_MASK;
  153. prot &= (~_PAGE_CACHE_MASK);
  154. /* Currently doing intersection by hand. Optimize it later. */
  155. if (pat_type == _PAGE_CACHE_WC) {
  156. *ret_prot = prot | _PAGE_CACHE_WC;
  157. } else if (pat_type == _PAGE_CACHE_UC_MINUS) {
  158. *ret_prot = prot | _PAGE_CACHE_UC_MINUS;
  159. } else if (pat_type == _PAGE_CACHE_UC ||
  160. mtrr_type == MTRR_TYPE_UNCACHABLE) {
  161. *ret_prot = prot | _PAGE_CACHE_UC;
  162. } else if (mtrr_type == MTRR_TYPE_WRCOMB) {
  163. *ret_prot = prot | _PAGE_CACHE_WC;
  164. } else {
  165. *ret_prot = prot | _PAGE_CACHE_WB;
  166. }
  167. return 0;
  168. }
  169. int reserve_memtype(u64 start, u64 end, unsigned long req_type,
  170. unsigned long *ret_type)
  171. {
  172. struct memtype *new_entry = NULL;
  173. struct memtype *parse;
  174. unsigned long actual_type;
  175. int err = 0;
  176. /* Only track when pat_wc_enabled */
  177. if (!pat_wc_enabled) {
  178. if (ret_type)
  179. *ret_type = req_type;
  180. return 0;
  181. }
  182. /* Low ISA region is always mapped WB in page table. No need to track */
  183. if (start >= ISA_START_ADDRESS && (end - 1) <= ISA_END_ADDRESS) {
  184. if (ret_type)
  185. *ret_type = _PAGE_CACHE_WB;
  186. return 0;
  187. }
  188. req_type &= _PAGE_CACHE_MASK;
  189. err = pat_x_mtrr_type(start, end, req_type, &actual_type);
  190. if (err) {
  191. if (ret_type)
  192. *ret_type = actual_type;
  193. return -EINVAL;
  194. }
  195. new_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  196. if (!new_entry)
  197. return -ENOMEM;
  198. new_entry->start = start;
  199. new_entry->end = end;
  200. new_entry->type = actual_type;
  201. if (ret_type)
  202. *ret_type = actual_type;
  203. spin_lock(&memtype_lock);
  204. /* Search for existing mapping that overlaps the current range */
  205. list_for_each_entry(parse, &memtype_list, nd) {
  206. struct memtype *saved_ptr;
  207. if (parse->start >= end) {
  208. printk("New Entry\n");
  209. list_add(&new_entry->nd, parse->nd.prev);
  210. new_entry = NULL;
  211. break;
  212. }
  213. if (start <= parse->start && end >= parse->start) {
  214. if (actual_type != parse->type && ret_type) {
  215. actual_type = parse->type;
  216. *ret_type = actual_type;
  217. new_entry->type = actual_type;
  218. }
  219. if (actual_type != parse->type) {
  220. printk(
  221. KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
  222. current->comm, current->pid,
  223. start, end,
  224. cattr_name(actual_type),
  225. cattr_name(parse->type));
  226. err = -EBUSY;
  227. break;
  228. }
  229. saved_ptr = parse;
  230. /*
  231. * Check to see whether the request overlaps more
  232. * than one entry in the list
  233. */
  234. list_for_each_entry_continue(parse, &memtype_list, nd) {
  235. if (end <= parse->start) {
  236. break;
  237. }
  238. if (actual_type != parse->type) {
  239. printk(
  240. KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
  241. current->comm, current->pid,
  242. start, end,
  243. cattr_name(actual_type),
  244. cattr_name(parse->type));
  245. err = -EBUSY;
  246. break;
  247. }
  248. }
  249. if (err) {
  250. break;
  251. }
  252. printk("Overlap at 0x%Lx-0x%Lx\n",
  253. saved_ptr->start, saved_ptr->end);
  254. /* No conflict. Go ahead and add this new entry */
  255. list_add(&new_entry->nd, saved_ptr->nd.prev);
  256. new_entry = NULL;
  257. break;
  258. }
  259. if (start < parse->end) {
  260. if (actual_type != parse->type && ret_type) {
  261. actual_type = parse->type;
  262. *ret_type = actual_type;
  263. new_entry->type = actual_type;
  264. }
  265. if (actual_type != parse->type) {
  266. printk(
  267. KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
  268. current->comm, current->pid,
  269. start, end,
  270. cattr_name(actual_type),
  271. cattr_name(parse->type));
  272. err = -EBUSY;
  273. break;
  274. }
  275. saved_ptr = parse;
  276. /*
  277. * Check to see whether the request overlaps more
  278. * than one entry in the list
  279. */
  280. list_for_each_entry_continue(parse, &memtype_list, nd) {
  281. if (end <= parse->start) {
  282. break;
  283. }
  284. if (actual_type != parse->type) {
  285. printk(
  286. KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
  287. current->comm, current->pid,
  288. start, end,
  289. cattr_name(actual_type),
  290. cattr_name(parse->type));
  291. err = -EBUSY;
  292. break;
  293. }
  294. }
  295. if (err) {
  296. break;
  297. }
  298. printk("Overlap at 0x%Lx-0x%Lx\n",
  299. saved_ptr->start, saved_ptr->end);
  300. /* No conflict. Go ahead and add this new entry */
  301. list_add(&new_entry->nd, &saved_ptr->nd);
  302. new_entry = NULL;
  303. break;
  304. }
  305. }
  306. if (err) {
  307. printk(
  308. "reserve_memtype failed 0x%Lx-0x%Lx, track %s, req %s\n",
  309. start, end, cattr_name(new_entry->type),
  310. cattr_name(req_type));
  311. kfree(new_entry);
  312. spin_unlock(&memtype_lock);
  313. return err;
  314. }
  315. if (new_entry) {
  316. /* No conflict. Not yet added to the list. Add to the tail */
  317. list_add_tail(&new_entry->nd, &memtype_list);
  318. printk("New Entry\n");
  319. }
  320. if (ret_type) {
  321. printk(
  322. "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
  323. start, end, cattr_name(actual_type),
  324. cattr_name(req_type), cattr_name(*ret_type));
  325. } else {
  326. printk(
  327. "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s\n",
  328. start, end, cattr_name(actual_type),
  329. cattr_name(req_type));
  330. }
  331. spin_unlock(&memtype_lock);
  332. return err;
  333. }
  334. int free_memtype(u64 start, u64 end)
  335. {
  336. struct memtype *ml;
  337. int err = -EINVAL;
  338. /* Only track when pat_wc_enabled */
  339. if (!pat_wc_enabled) {
  340. return 0;
  341. }
  342. /* Low ISA region is always mapped WB. No need to track */
  343. if (start >= ISA_START_ADDRESS && end <= ISA_END_ADDRESS) {
  344. return 0;
  345. }
  346. spin_lock(&memtype_lock);
  347. list_for_each_entry(ml, &memtype_list, nd) {
  348. if (ml->start == start && ml->end == end) {
  349. list_del(&ml->nd);
  350. kfree(ml);
  351. err = 0;
  352. break;
  353. }
  354. }
  355. spin_unlock(&memtype_lock);
  356. if (err) {
  357. printk(KERN_DEBUG "%s:%d freeing invalid memtype %Lx-%Lx\n",
  358. current->comm, current->pid, start, end);
  359. }
  360. printk( "free_memtype request 0x%Lx-0x%Lx\n", start, end);
  361. return err;
  362. }