pat.c 9.7 KB

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