pat.c 21 KB

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