pat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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/seq_file.h>
  10. #include <linux/bootmem.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/gfp.h>
  15. #include <linux/mm.h>
  16. #include <linux/fs.h>
  17. #include <linux/rbtree.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/processor.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/fcntl.h>
  23. #include <asm/e820.h>
  24. #include <asm/mtrr.h>
  25. #include <asm/page.h>
  26. #include <asm/msr.h>
  27. #include <asm/pat.h>
  28. #include <asm/io.h>
  29. #ifdef CONFIG_X86_PAT
  30. int __read_mostly pat_enabled = 1;
  31. static inline void pat_disable(const char *reason)
  32. {
  33. pat_enabled = 0;
  34. printk(KERN_INFO "%s\n", reason);
  35. }
  36. static int __init nopat(char *str)
  37. {
  38. pat_disable("PAT support disabled.");
  39. return 0;
  40. }
  41. early_param("nopat", nopat);
  42. #else
  43. static inline void pat_disable(const char *reason)
  44. {
  45. (void)reason;
  46. }
  47. #endif
  48. static int debug_enable;
  49. static int __init pat_debug_setup(char *str)
  50. {
  51. debug_enable = 1;
  52. return 0;
  53. }
  54. __setup("debugpat", pat_debug_setup);
  55. #define dprintk(fmt, arg...) \
  56. do { if (debug_enable) printk(KERN_INFO fmt, ##arg); } while (0)
  57. static u64 __read_mostly boot_pat_state;
  58. enum {
  59. PAT_UC = 0, /* uncached */
  60. PAT_WC = 1, /* Write combining */
  61. PAT_WT = 4, /* Write Through */
  62. PAT_WP = 5, /* Write Protected */
  63. PAT_WB = 6, /* Write Back (default) */
  64. PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */
  65. };
  66. #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
  67. void pat_init(void)
  68. {
  69. u64 pat;
  70. if (!pat_enabled)
  71. return;
  72. if (!cpu_has_pat) {
  73. if (!boot_pat_state) {
  74. pat_disable("PAT not supported by CPU.");
  75. return;
  76. } else {
  77. /*
  78. * If this happens we are on a secondary CPU, but
  79. * switched to PAT on the boot CPU. We have no way to
  80. * undo PAT.
  81. */
  82. printk(KERN_ERR "PAT enabled, "
  83. "but not supported by secondary CPU\n");
  84. BUG();
  85. }
  86. }
  87. /* Set PWT to Write-Combining. All other bits stay the same */
  88. /*
  89. * PTE encoding used in Linux:
  90. * PAT
  91. * |PCD
  92. * ||PWT
  93. * |||
  94. * 000 WB _PAGE_CACHE_WB
  95. * 001 WC _PAGE_CACHE_WC
  96. * 010 UC- _PAGE_CACHE_UC_MINUS
  97. * 011 UC _PAGE_CACHE_UC
  98. * PAT bit unused
  99. */
  100. pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
  101. PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, UC);
  102. /* Boot CPU check */
  103. if (!boot_pat_state)
  104. rdmsrl(MSR_IA32_CR_PAT, boot_pat_state);
  105. wrmsrl(MSR_IA32_CR_PAT, pat);
  106. printk(KERN_INFO "x86 PAT enabled: cpu %d, old 0x%Lx, new 0x%Lx\n",
  107. smp_processor_id(), boot_pat_state, pat);
  108. }
  109. #undef PAT
  110. static char *cattr_name(unsigned long flags)
  111. {
  112. switch (flags & _PAGE_CACHE_MASK) {
  113. case _PAGE_CACHE_UC: return "uncached";
  114. case _PAGE_CACHE_UC_MINUS: return "uncached-minus";
  115. case _PAGE_CACHE_WB: return "write-back";
  116. case _PAGE_CACHE_WC: return "write-combining";
  117. default: return "broken";
  118. }
  119. }
  120. /*
  121. * The global memtype list keeps track of memory type for specific
  122. * physical memory areas. Conflicting memory types in different
  123. * mappings can cause CPU cache corruption. To avoid this we keep track.
  124. *
  125. * The list is sorted based on starting address and can contain multiple
  126. * entries for each address (this allows reference counting for overlapping
  127. * areas). All the aliases have the same cache attributes of course.
  128. * Zero attributes are represented as holes.
  129. *
  130. * The data structure is a list that is also organized as an rbtree
  131. * sorted on the start address of memtype range.
  132. *
  133. * memtype_lock protects both the linear list and rbtree.
  134. */
  135. struct memtype {
  136. u64 start;
  137. u64 end;
  138. unsigned long type;
  139. struct list_head nd;
  140. struct rb_node rb;
  141. };
  142. static struct rb_root memtype_rbroot = RB_ROOT;
  143. static LIST_HEAD(memtype_list);
  144. static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */
  145. static struct memtype *memtype_rb_search(struct rb_root *root, u64 start)
  146. {
  147. struct rb_node *node = root->rb_node;
  148. struct memtype *last_lower = NULL;
  149. while (node) {
  150. struct memtype *data = container_of(node, struct memtype, rb);
  151. if (data->start < start) {
  152. last_lower = data;
  153. node = node->rb_right;
  154. } else if (data->start > start) {
  155. node = node->rb_left;
  156. } else
  157. return data;
  158. }
  159. /* Will return NULL if there is no entry with its start <= start */
  160. return last_lower;
  161. }
  162. static void memtype_rb_insert(struct rb_root *root, struct memtype *data)
  163. {
  164. struct rb_node **new = &(root->rb_node);
  165. struct rb_node *parent = NULL;
  166. while (*new) {
  167. struct memtype *this = container_of(*new, struct memtype, rb);
  168. parent = *new;
  169. if (data->start <= this->start)
  170. new = &((*new)->rb_left);
  171. else if (data->start > this->start)
  172. new = &((*new)->rb_right);
  173. }
  174. rb_link_node(&data->rb, parent, new);
  175. rb_insert_color(&data->rb, root);
  176. }
  177. /*
  178. * Does intersection of PAT memory type and MTRR memory type and returns
  179. * the resulting memory type as PAT understands it.
  180. * (Type in pat and mtrr will not have same value)
  181. * The intersection is based on "Effective Memory Type" tables in IA-32
  182. * SDM vol 3a
  183. */
  184. static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type)
  185. {
  186. /*
  187. * Look for MTRR hint to get the effective type in case where PAT
  188. * request is for WB.
  189. */
  190. if (req_type == _PAGE_CACHE_WB) {
  191. u8 mtrr_type;
  192. mtrr_type = mtrr_type_lookup(start, end);
  193. if (mtrr_type != MTRR_TYPE_WRBACK)
  194. return _PAGE_CACHE_UC_MINUS;
  195. return _PAGE_CACHE_WB;
  196. }
  197. return req_type;
  198. }
  199. static int
  200. chk_conflict(struct memtype *new, struct memtype *entry, unsigned long *type)
  201. {
  202. if (new->type != entry->type) {
  203. if (type) {
  204. new->type = entry->type;
  205. *type = entry->type;
  206. } else
  207. goto conflict;
  208. }
  209. /* check overlaps with more than one entry in the list */
  210. list_for_each_entry_continue(entry, &memtype_list, nd) {
  211. if (new->end <= entry->start)
  212. break;
  213. else if (new->type != entry->type)
  214. goto conflict;
  215. }
  216. return 0;
  217. conflict:
  218. printk(KERN_INFO "%s:%d conflicting memory types "
  219. "%Lx-%Lx %s<->%s\n", current->comm, current->pid, new->start,
  220. new->end, cattr_name(new->type), cattr_name(entry->type));
  221. return -EBUSY;
  222. }
  223. static int pat_pagerange_is_ram(unsigned long start, unsigned long end)
  224. {
  225. int ram_page = 0, not_rampage = 0;
  226. unsigned long page_nr;
  227. for (page_nr = (start >> PAGE_SHIFT); page_nr < (end >> PAGE_SHIFT);
  228. ++page_nr) {
  229. /*
  230. * For legacy reasons, physical address range in the legacy ISA
  231. * region is tracked as non-RAM. This will allow users of
  232. * /dev/mem to map portions of legacy ISA region, even when
  233. * some of those portions are listed(or not even listed) with
  234. * different e820 types(RAM/reserved/..)
  235. */
  236. if (page_nr >= (ISA_END_ADDRESS >> PAGE_SHIFT) &&
  237. page_is_ram(page_nr))
  238. ram_page = 1;
  239. else
  240. not_rampage = 1;
  241. if (ram_page == not_rampage)
  242. return -1;
  243. }
  244. return ram_page;
  245. }
  246. /*
  247. * For RAM pages, mark the pages as non WB memory type using
  248. * PageNonWB (PG_arch_1). We allow only one set_memory_uc() or
  249. * set_memory_wc() on a RAM page at a time before marking it as WB again.
  250. * This is ok, because only one driver will be owning the page and
  251. * doing set_memory_*() calls.
  252. *
  253. * For now, we use PageNonWB to track that the RAM page is being mapped
  254. * as non WB. In future, we will have to use one more flag
  255. * (or some other mechanism in page_struct) to distinguish between
  256. * UC and WC mapping.
  257. */
  258. static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type,
  259. unsigned long *new_type)
  260. {
  261. struct page *page;
  262. u64 pfn, end_pfn;
  263. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  264. page = pfn_to_page(pfn);
  265. if (page_mapped(page) || PageNonWB(page))
  266. goto out;
  267. SetPageNonWB(page);
  268. }
  269. return 0;
  270. out:
  271. end_pfn = pfn;
  272. for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
  273. page = pfn_to_page(pfn);
  274. ClearPageNonWB(page);
  275. }
  276. return -EINVAL;
  277. }
  278. static int free_ram_pages_type(u64 start, u64 end)
  279. {
  280. struct page *page;
  281. u64 pfn, end_pfn;
  282. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  283. page = pfn_to_page(pfn);
  284. if (page_mapped(page) || !PageNonWB(page))
  285. goto out;
  286. ClearPageNonWB(page);
  287. }
  288. return 0;
  289. out:
  290. end_pfn = pfn;
  291. for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
  292. page = pfn_to_page(pfn);
  293. SetPageNonWB(page);
  294. }
  295. return -EINVAL;
  296. }
  297. /*
  298. * req_type typically has one of the:
  299. * - _PAGE_CACHE_WB
  300. * - _PAGE_CACHE_WC
  301. * - _PAGE_CACHE_UC_MINUS
  302. * - _PAGE_CACHE_UC
  303. *
  304. * req_type will have a special case value '-1', when requester want to inherit
  305. * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
  306. *
  307. * If new_type is NULL, function will return an error if it cannot reserve the
  308. * region with req_type. If new_type is non-NULL, function will return
  309. * available type in new_type in case of no error. In case of any error
  310. * it will return a negative return value.
  311. */
  312. int reserve_memtype(u64 start, u64 end, unsigned long req_type,
  313. unsigned long *new_type)
  314. {
  315. struct memtype *new, *entry;
  316. unsigned long actual_type;
  317. struct list_head *where;
  318. int is_range_ram;
  319. int err = 0;
  320. BUG_ON(start >= end); /* end is exclusive */
  321. if (!pat_enabled) {
  322. /* This is identical to page table setting without PAT */
  323. if (new_type) {
  324. if (req_type == -1)
  325. *new_type = _PAGE_CACHE_WB;
  326. else if (req_type == _PAGE_CACHE_WC)
  327. *new_type = _PAGE_CACHE_UC_MINUS;
  328. else
  329. *new_type = req_type & _PAGE_CACHE_MASK;
  330. }
  331. return 0;
  332. }
  333. /* Low ISA region is always mapped WB in page table. No need to track */
  334. if (is_ISA_range(start, end - 1)) {
  335. if (new_type)
  336. *new_type = _PAGE_CACHE_WB;
  337. return 0;
  338. }
  339. /*
  340. * Call mtrr_lookup to get the type hint. This is an
  341. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  342. * tools and ACPI tools). Use WB request for WB memory and use
  343. * UC_MINUS otherwise.
  344. */
  345. actual_type = pat_x_mtrr_type(start, end, req_type & _PAGE_CACHE_MASK);
  346. if (new_type)
  347. *new_type = actual_type;
  348. is_range_ram = pat_pagerange_is_ram(start, end);
  349. if (is_range_ram == 1)
  350. return reserve_ram_pages_type(start, end, req_type,
  351. new_type);
  352. else if (is_range_ram < 0)
  353. return -EINVAL;
  354. new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  355. if (!new)
  356. return -ENOMEM;
  357. new->start = start;
  358. new->end = end;
  359. new->type = actual_type;
  360. spin_lock(&memtype_lock);
  361. entry = memtype_rb_search(&memtype_rbroot, new->start);
  362. if (likely(entry != NULL)) {
  363. /* To work correctly with list_for_each_entry_continue */
  364. entry = list_entry(entry->nd.prev, struct memtype, nd);
  365. } else {
  366. entry = list_entry(&memtype_list, struct memtype, nd);
  367. }
  368. /* Search for existing mapping that overlaps the current range */
  369. where = NULL;
  370. list_for_each_entry_continue(entry, &memtype_list, nd) {
  371. if (end <= entry->start) {
  372. where = entry->nd.prev;
  373. break;
  374. } else if (start <= entry->start) { /* end > entry->start */
  375. err = chk_conflict(new, entry, new_type);
  376. if (!err) {
  377. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  378. entry->start, entry->end);
  379. where = entry->nd.prev;
  380. }
  381. break;
  382. } else if (start < entry->end) { /* start > entry->start */
  383. err = chk_conflict(new, entry, new_type);
  384. if (!err) {
  385. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  386. entry->start, entry->end);
  387. /*
  388. * Move to right position in the linked
  389. * list to add this new entry
  390. */
  391. list_for_each_entry_continue(entry,
  392. &memtype_list, nd) {
  393. if (start <= entry->start) {
  394. where = entry->nd.prev;
  395. break;
  396. }
  397. }
  398. }
  399. break;
  400. }
  401. }
  402. if (err) {
  403. printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
  404. "track %s, req %s\n",
  405. start, end, cattr_name(new->type), cattr_name(req_type));
  406. kfree(new);
  407. spin_unlock(&memtype_lock);
  408. return err;
  409. }
  410. if (where)
  411. list_add(&new->nd, where);
  412. else
  413. list_add_tail(&new->nd, &memtype_list);
  414. memtype_rb_insert(&memtype_rbroot, new);
  415. spin_unlock(&memtype_lock);
  416. dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
  417. start, end, cattr_name(new->type), cattr_name(req_type),
  418. new_type ? cattr_name(*new_type) : "-");
  419. return err;
  420. }
  421. int free_memtype(u64 start, u64 end)
  422. {
  423. struct memtype *entry, *saved_entry;
  424. int err = -EINVAL;
  425. int is_range_ram;
  426. if (!pat_enabled)
  427. return 0;
  428. /* Low ISA region is always mapped WB. No need to track */
  429. if (is_ISA_range(start, end - 1))
  430. return 0;
  431. is_range_ram = pat_pagerange_is_ram(start, end);
  432. if (is_range_ram == 1)
  433. return free_ram_pages_type(start, end);
  434. else if (is_range_ram < 0)
  435. return -EINVAL;
  436. spin_lock(&memtype_lock);
  437. entry = memtype_rb_search(&memtype_rbroot, start);
  438. if (unlikely(entry == NULL))
  439. goto unlock_ret;
  440. /*
  441. * Saved entry points to an entry with start same or less than what
  442. * we searched for. Now go through the list in both directions to look
  443. * for the entry that matches with both start and end, with list stored
  444. * in sorted start address
  445. */
  446. saved_entry = entry;
  447. list_for_each_entry(entry, &memtype_list, nd) {
  448. if (entry->start == start && entry->end == end) {
  449. rb_erase(&entry->rb, &memtype_rbroot);
  450. list_del(&entry->nd);
  451. kfree(entry);
  452. err = 0;
  453. break;
  454. } else if (entry->start > start) {
  455. break;
  456. }
  457. }
  458. if (!err)
  459. goto unlock_ret;
  460. entry = saved_entry;
  461. list_for_each_entry_reverse(entry, &memtype_list, nd) {
  462. if (entry->start == start && entry->end == end) {
  463. rb_erase(&entry->rb, &memtype_rbroot);
  464. list_del(&entry->nd);
  465. kfree(entry);
  466. err = 0;
  467. break;
  468. } else if (entry->start < start) {
  469. break;
  470. }
  471. }
  472. unlock_ret:
  473. spin_unlock(&memtype_lock);
  474. if (err) {
  475. printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
  476. current->comm, current->pid, start, end);
  477. }
  478. dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
  479. return err;
  480. }
  481. /**
  482. * io_reserve_memtype - Request a memory type mapping for a region of memory
  483. * @start: start (physical address) of the region
  484. * @end: end (physical address) of the region
  485. * @type: A pointer to memtype, with requested type. On success, requested
  486. * or any other compatible type that was available for the region is returned
  487. *
  488. * On success, returns 0
  489. * On failure, returns non-zero
  490. */
  491. int io_reserve_memtype(resource_size_t start, resource_size_t end,
  492. unsigned long *type)
  493. {
  494. unsigned long req_type = *type;
  495. unsigned long new_type;
  496. int ret;
  497. WARN_ON_ONCE(iomem_map_sanity_check(start, end - start));
  498. ret = reserve_memtype(start, end, req_type, &new_type);
  499. if (ret)
  500. goto out_err;
  501. if (!is_new_memtype_allowed(req_type, new_type))
  502. goto out_free;
  503. if (kernel_map_sync_memtype(start, end - start, new_type) < 0)
  504. goto out_free;
  505. *type = new_type;
  506. return 0;
  507. out_free:
  508. free_memtype(start, end);
  509. ret = -EBUSY;
  510. out_err:
  511. return ret;
  512. }
  513. /**
  514. * io_free_memtype - Release a memory type mapping for a region of memory
  515. * @start: start (physical address) of the region
  516. * @end: end (physical address) of the region
  517. */
  518. void io_free_memtype(resource_size_t start, resource_size_t end)
  519. {
  520. free_memtype(start, end);
  521. }
  522. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  523. unsigned long size, pgprot_t vma_prot)
  524. {
  525. return vma_prot;
  526. }
  527. #ifdef CONFIG_STRICT_DEVMEM
  528. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
  529. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  530. {
  531. return 1;
  532. }
  533. #else
  534. /* This check is needed to avoid cache aliasing when PAT is enabled */
  535. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  536. {
  537. u64 from = ((u64)pfn) << PAGE_SHIFT;
  538. u64 to = from + size;
  539. u64 cursor = from;
  540. if (!pat_enabled)
  541. return 1;
  542. while (cursor < to) {
  543. if (!devmem_is_allowed(pfn)) {
  544. printk(KERN_INFO
  545. "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  546. current->comm, from, to);
  547. return 0;
  548. }
  549. cursor += PAGE_SIZE;
  550. pfn++;
  551. }
  552. return 1;
  553. }
  554. #endif /* CONFIG_STRICT_DEVMEM */
  555. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  556. unsigned long size, pgprot_t *vma_prot)
  557. {
  558. unsigned long flags = _PAGE_CACHE_WB;
  559. if (!range_is_allowed(pfn, size))
  560. return 0;
  561. if (file->f_flags & O_SYNC) {
  562. flags = _PAGE_CACHE_UC_MINUS;
  563. }
  564. #ifdef CONFIG_X86_32
  565. /*
  566. * On the PPro and successors, the MTRRs are used to set
  567. * memory types for physical addresses outside main memory,
  568. * so blindly setting UC or PWT on those pages is wrong.
  569. * For Pentiums and earlier, the surround logic should disable
  570. * caching for the high addresses through the KEN pin, but
  571. * we maintain the tradition of paranoia in this code.
  572. */
  573. if (!pat_enabled &&
  574. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  575. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  576. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  577. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  578. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  579. flags = _PAGE_CACHE_UC;
  580. }
  581. #endif
  582. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  583. flags);
  584. return 1;
  585. }
  586. /*
  587. * Change the memory type for the physial address range in kernel identity
  588. * mapping space if that range is a part of identity map.
  589. */
  590. int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags)
  591. {
  592. unsigned long id_sz;
  593. if (base >= __pa(high_memory))
  594. return 0;
  595. id_sz = (__pa(high_memory) < base + size) ?
  596. __pa(high_memory) - base :
  597. size;
  598. if (ioremap_change_attr((unsigned long)__va(base), id_sz, flags) < 0) {
  599. printk(KERN_INFO
  600. "%s:%d ioremap_change_attr failed %s "
  601. "for %Lx-%Lx\n",
  602. current->comm, current->pid,
  603. cattr_name(flags),
  604. base, (unsigned long long)(base + size));
  605. return -EINVAL;
  606. }
  607. return 0;
  608. }
  609. /*
  610. * Internal interface to reserve a range of physical memory with prot.
  611. * Reserved non RAM regions only and after successful reserve_memtype,
  612. * this func also keeps identity mapping (if any) in sync with this new prot.
  613. */
  614. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  615. int strict_prot)
  616. {
  617. int is_ram = 0;
  618. int ret;
  619. unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
  620. unsigned long flags = want_flags;
  621. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  622. /*
  623. * reserve_pfn_range() doesn't support RAM pages. Maintain the current
  624. * behavior with RAM pages by returning success.
  625. */
  626. if (is_ram != 0)
  627. return 0;
  628. ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
  629. if (ret)
  630. return ret;
  631. if (flags != want_flags) {
  632. if (strict_prot || !is_new_memtype_allowed(want_flags, flags)) {
  633. free_memtype(paddr, paddr + size);
  634. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  635. " for %Lx-%Lx, got %s\n",
  636. current->comm, current->pid,
  637. cattr_name(want_flags),
  638. (unsigned long long)paddr,
  639. (unsigned long long)(paddr + size),
  640. cattr_name(flags));
  641. return -EINVAL;
  642. }
  643. /*
  644. * We allow returning different type than the one requested in
  645. * non strict case.
  646. */
  647. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  648. (~_PAGE_CACHE_MASK)) |
  649. flags);
  650. }
  651. if (kernel_map_sync_memtype(paddr, size, flags) < 0) {
  652. free_memtype(paddr, paddr + size);
  653. return -EINVAL;
  654. }
  655. return 0;
  656. }
  657. /*
  658. * Internal interface to free a range of physical memory.
  659. * Frees non RAM regions only.
  660. */
  661. static void free_pfn_range(u64 paddr, unsigned long size)
  662. {
  663. int is_ram;
  664. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  665. if (is_ram == 0)
  666. free_memtype(paddr, paddr + size);
  667. }
  668. /*
  669. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  670. * copied through copy_page_range().
  671. *
  672. * If the vma has a linear pfn mapping for the entire range, we get the prot
  673. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  674. */
  675. int track_pfn_vma_copy(struct vm_area_struct *vma)
  676. {
  677. resource_size_t paddr;
  678. unsigned long prot;
  679. unsigned long vma_size = vma->vm_end - vma->vm_start;
  680. pgprot_t pgprot;
  681. /*
  682. * For now, only handle remap_pfn_range() vmas where
  683. * is_linear_pfn_mapping() == TRUE. Handling of
  684. * vm_insert_pfn() is TBD.
  685. */
  686. if (is_linear_pfn_mapping(vma)) {
  687. /*
  688. * reserve the whole chunk covered by vma. We need the
  689. * starting address and protection from pte.
  690. */
  691. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  692. WARN_ON_ONCE(1);
  693. return -EINVAL;
  694. }
  695. pgprot = __pgprot(prot);
  696. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  697. }
  698. return 0;
  699. }
  700. /*
  701. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  702. * for physical range indicated by pfn and size.
  703. *
  704. * prot is passed in as a parameter for the new mapping. If the vma has a
  705. * linear pfn mapping for the entire range reserve the entire vma range with
  706. * single reserve_pfn_range call.
  707. */
  708. int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  709. unsigned long pfn, unsigned long size)
  710. {
  711. resource_size_t paddr;
  712. unsigned long vma_size = vma->vm_end - vma->vm_start;
  713. /*
  714. * For now, only handle remap_pfn_range() vmas where
  715. * is_linear_pfn_mapping() == TRUE. Handling of
  716. * vm_insert_pfn() is TBD.
  717. */
  718. if (is_linear_pfn_mapping(vma)) {
  719. /* reserve the whole chunk starting from vm_pgoff */
  720. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  721. return reserve_pfn_range(paddr, vma_size, prot, 0);
  722. }
  723. return 0;
  724. }
  725. /*
  726. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  727. * untrack can be called for a specific region indicated by pfn and size or
  728. * can be for the entire vma (in which case size can be zero).
  729. */
  730. void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  731. unsigned long size)
  732. {
  733. resource_size_t paddr;
  734. unsigned long vma_size = vma->vm_end - vma->vm_start;
  735. /*
  736. * For now, only handle remap_pfn_range() vmas where
  737. * is_linear_pfn_mapping() == TRUE. Handling of
  738. * vm_insert_pfn() is TBD.
  739. */
  740. if (is_linear_pfn_mapping(vma)) {
  741. /* free the whole chunk starting from vm_pgoff */
  742. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  743. free_pfn_range(paddr, vma_size);
  744. return;
  745. }
  746. }
  747. pgprot_t pgprot_writecombine(pgprot_t prot)
  748. {
  749. if (pat_enabled)
  750. return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
  751. else
  752. return pgprot_noncached(prot);
  753. }
  754. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  755. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  756. /* get Nth element of the linked list */
  757. static struct memtype *memtype_get_idx(loff_t pos)
  758. {
  759. struct memtype *list_node, *print_entry;
  760. int i = 1;
  761. print_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  762. if (!print_entry)
  763. return NULL;
  764. spin_lock(&memtype_lock);
  765. list_for_each_entry(list_node, &memtype_list, nd) {
  766. if (pos == i) {
  767. *print_entry = *list_node;
  768. spin_unlock(&memtype_lock);
  769. return print_entry;
  770. }
  771. ++i;
  772. }
  773. spin_unlock(&memtype_lock);
  774. kfree(print_entry);
  775. return NULL;
  776. }
  777. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  778. {
  779. if (*pos == 0) {
  780. ++*pos;
  781. seq_printf(seq, "PAT memtype list:\n");
  782. }
  783. return memtype_get_idx(*pos);
  784. }
  785. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  786. {
  787. ++*pos;
  788. return memtype_get_idx(*pos);
  789. }
  790. static void memtype_seq_stop(struct seq_file *seq, void *v)
  791. {
  792. }
  793. static int memtype_seq_show(struct seq_file *seq, void *v)
  794. {
  795. struct memtype *print_entry = (struct memtype *)v;
  796. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  797. print_entry->start, print_entry->end);
  798. kfree(print_entry);
  799. return 0;
  800. }
  801. static struct seq_operations memtype_seq_ops = {
  802. .start = memtype_seq_start,
  803. .next = memtype_seq_next,
  804. .stop = memtype_seq_stop,
  805. .show = memtype_seq_show,
  806. };
  807. static int memtype_seq_open(struct inode *inode, struct file *file)
  808. {
  809. return seq_open(file, &memtype_seq_ops);
  810. }
  811. static const struct file_operations memtype_fops = {
  812. .open = memtype_seq_open,
  813. .read = seq_read,
  814. .llseek = seq_lseek,
  815. .release = seq_release,
  816. };
  817. static int __init pat_memtype_list_init(void)
  818. {
  819. debugfs_create_file("pat_memtype_list", S_IRUSR, arch_debugfs_dir,
  820. NULL, &memtype_fops);
  821. return 0;
  822. }
  823. late_initcall(pat_memtype_list_init);
  824. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */