pat.c 20 KB

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