pat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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/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(unsigned long start, unsigned long 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. * Caller must hold memtype_lock for atomicity.
  165. */
  166. static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type,
  167. unsigned long *new_type)
  168. {
  169. struct page *page;
  170. u64 pfn;
  171. if (req_type == _PAGE_CACHE_UC) {
  172. /* We do not support strong UC */
  173. WARN_ON_ONCE(1);
  174. req_type = _PAGE_CACHE_UC_MINUS;
  175. }
  176. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  177. unsigned long type;
  178. page = pfn_to_page(pfn);
  179. type = get_page_memtype(page);
  180. if (type != -1) {
  181. printk(KERN_INFO "reserve_ram_pages_type failed "
  182. "0x%Lx-0x%Lx, track 0x%lx, req 0x%lx\n",
  183. start, end, type, req_type);
  184. if (new_type)
  185. *new_type = type;
  186. return -EBUSY;
  187. }
  188. }
  189. if (new_type)
  190. *new_type = req_type;
  191. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  192. page = pfn_to_page(pfn);
  193. set_page_memtype(page, req_type);
  194. }
  195. return 0;
  196. }
  197. static int free_ram_pages_type(u64 start, u64 end)
  198. {
  199. struct page *page;
  200. u64 pfn;
  201. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  202. page = pfn_to_page(pfn);
  203. set_page_memtype(page, -1);
  204. }
  205. return 0;
  206. }
  207. /*
  208. * req_type typically has one of the:
  209. * - _PAGE_CACHE_WB
  210. * - _PAGE_CACHE_WC
  211. * - _PAGE_CACHE_UC_MINUS
  212. * - _PAGE_CACHE_UC
  213. *
  214. * If new_type is NULL, function will return an error if it cannot reserve the
  215. * region with req_type. If new_type is non-NULL, function will return
  216. * available type in new_type in case of no error. In case of any error
  217. * it will return a negative return value.
  218. */
  219. int reserve_memtype(u64 start, u64 end, unsigned long req_type,
  220. unsigned long *new_type)
  221. {
  222. struct memtype *new;
  223. unsigned long actual_type;
  224. int is_range_ram;
  225. int err = 0;
  226. BUG_ON(start >= end); /* end is exclusive */
  227. if (!pat_enabled) {
  228. /* This is identical to page table setting without PAT */
  229. if (new_type) {
  230. if (req_type == _PAGE_CACHE_WC)
  231. *new_type = _PAGE_CACHE_UC_MINUS;
  232. else
  233. *new_type = req_type & _PAGE_CACHE_MASK;
  234. }
  235. return 0;
  236. }
  237. /* Low ISA region is always mapped WB in page table. No need to track */
  238. if (x86_platform.is_untracked_pat_range(start, end)) {
  239. if (new_type)
  240. *new_type = _PAGE_CACHE_WB;
  241. return 0;
  242. }
  243. /*
  244. * Call mtrr_lookup to get the type hint. This is an
  245. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  246. * tools and ACPI tools). Use WB request for WB memory and use
  247. * UC_MINUS otherwise.
  248. */
  249. actual_type = pat_x_mtrr_type(start, end, req_type & _PAGE_CACHE_MASK);
  250. if (new_type)
  251. *new_type = actual_type;
  252. is_range_ram = pat_pagerange_is_ram(start, end);
  253. if (is_range_ram == 1) {
  254. spin_lock(&memtype_lock);
  255. err = reserve_ram_pages_type(start, end, req_type, new_type);
  256. spin_unlock(&memtype_lock);
  257. return err;
  258. } else if (is_range_ram < 0) {
  259. return -EINVAL;
  260. }
  261. new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  262. if (!new)
  263. return -ENOMEM;
  264. new->start = start;
  265. new->end = end;
  266. new->type = actual_type;
  267. spin_lock(&memtype_lock);
  268. err = rbt_memtype_check_insert(new, new_type);
  269. if (err) {
  270. printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
  271. "track %s, req %s\n",
  272. start, end, cattr_name(new->type), cattr_name(req_type));
  273. kfree(new);
  274. spin_unlock(&memtype_lock);
  275. return err;
  276. }
  277. spin_unlock(&memtype_lock);
  278. dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
  279. start, end, cattr_name(new->type), cattr_name(req_type),
  280. new_type ? cattr_name(*new_type) : "-");
  281. return err;
  282. }
  283. int free_memtype(u64 start, u64 end)
  284. {
  285. int err = -EINVAL;
  286. int is_range_ram;
  287. if (!pat_enabled)
  288. return 0;
  289. /* Low ISA region is always mapped WB. No need to track */
  290. if (x86_platform.is_untracked_pat_range(start, end))
  291. return 0;
  292. is_range_ram = pat_pagerange_is_ram(start, end);
  293. if (is_range_ram == 1) {
  294. spin_lock(&memtype_lock);
  295. err = free_ram_pages_type(start, end);
  296. spin_unlock(&memtype_lock);
  297. return err;
  298. } else if (is_range_ram < 0) {
  299. return -EINVAL;
  300. }
  301. spin_lock(&memtype_lock);
  302. err = rbt_memtype_erase(start, end);
  303. spin_unlock(&memtype_lock);
  304. if (err) {
  305. printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
  306. current->comm, current->pid, start, end);
  307. }
  308. dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
  309. return err;
  310. }
  311. /**
  312. * lookup_memtype - Looksup the memory type for a physical address
  313. * @paddr: physical address of which memory type needs to be looked up
  314. *
  315. * Only to be called when PAT is enabled
  316. *
  317. * Returns _PAGE_CACHE_WB, _PAGE_CACHE_WC, _PAGE_CACHE_UC_MINUS or
  318. * _PAGE_CACHE_UC
  319. */
  320. static unsigned long lookup_memtype(u64 paddr)
  321. {
  322. int rettype = _PAGE_CACHE_WB;
  323. struct memtype *entry;
  324. if (x86_platform.is_untracked_pat_range(paddr, paddr + PAGE_SIZE))
  325. return rettype;
  326. if (pat_pagerange_is_ram(paddr, paddr + PAGE_SIZE)) {
  327. struct page *page;
  328. spin_lock(&memtype_lock);
  329. page = pfn_to_page(paddr >> PAGE_SHIFT);
  330. rettype = get_page_memtype(page);
  331. spin_unlock(&memtype_lock);
  332. /*
  333. * -1 from get_page_memtype() implies RAM page is in its
  334. * default state and not reserved, and hence of type WB
  335. */
  336. if (rettype == -1)
  337. rettype = _PAGE_CACHE_WB;
  338. return rettype;
  339. }
  340. spin_lock(&memtype_lock);
  341. entry = rbt_memtype_lookup(paddr);
  342. if (entry != NULL)
  343. rettype = entry->type;
  344. else
  345. rettype = _PAGE_CACHE_UC_MINUS;
  346. spin_unlock(&memtype_lock);
  347. return rettype;
  348. }
  349. /**
  350. * io_reserve_memtype - Request a memory type mapping for a region of memory
  351. * @start: start (physical address) of the region
  352. * @end: end (physical address) of the region
  353. * @type: A pointer to memtype, with requested type. On success, requested
  354. * or any other compatible type that was available for the region is returned
  355. *
  356. * On success, returns 0
  357. * On failure, returns non-zero
  358. */
  359. int io_reserve_memtype(resource_size_t start, resource_size_t end,
  360. unsigned long *type)
  361. {
  362. resource_size_t size = end - start;
  363. unsigned long req_type = *type;
  364. unsigned long new_type;
  365. int ret;
  366. WARN_ON_ONCE(iomem_map_sanity_check(start, size));
  367. ret = reserve_memtype(start, end, req_type, &new_type);
  368. if (ret)
  369. goto out_err;
  370. if (!is_new_memtype_allowed(start, size, req_type, new_type))
  371. goto out_free;
  372. if (kernel_map_sync_memtype(start, size, new_type) < 0)
  373. goto out_free;
  374. *type = new_type;
  375. return 0;
  376. out_free:
  377. free_memtype(start, end);
  378. ret = -EBUSY;
  379. out_err:
  380. return ret;
  381. }
  382. /**
  383. * io_free_memtype - Release a memory type mapping for a region of memory
  384. * @start: start (physical address) of the region
  385. * @end: end (physical address) of the region
  386. */
  387. void io_free_memtype(resource_size_t start, resource_size_t end)
  388. {
  389. free_memtype(start, end);
  390. }
  391. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  392. unsigned long size, pgprot_t vma_prot)
  393. {
  394. return vma_prot;
  395. }
  396. #ifdef CONFIG_STRICT_DEVMEM
  397. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
  398. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  399. {
  400. return 1;
  401. }
  402. #else
  403. /* This check is needed to avoid cache aliasing when PAT is enabled */
  404. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  405. {
  406. u64 from = ((u64)pfn) << PAGE_SHIFT;
  407. u64 to = from + size;
  408. u64 cursor = from;
  409. if (!pat_enabled)
  410. return 1;
  411. while (cursor < to) {
  412. if (!devmem_is_allowed(pfn)) {
  413. printk(KERN_INFO
  414. "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  415. current->comm, from, to);
  416. return 0;
  417. }
  418. cursor += PAGE_SIZE;
  419. pfn++;
  420. }
  421. return 1;
  422. }
  423. #endif /* CONFIG_STRICT_DEVMEM */
  424. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  425. unsigned long size, pgprot_t *vma_prot)
  426. {
  427. unsigned long flags = _PAGE_CACHE_WB;
  428. if (!range_is_allowed(pfn, size))
  429. return 0;
  430. if (file->f_flags & O_DSYNC)
  431. flags = _PAGE_CACHE_UC_MINUS;
  432. #ifdef CONFIG_X86_32
  433. /*
  434. * On the PPro and successors, the MTRRs are used to set
  435. * memory types for physical addresses outside main memory,
  436. * so blindly setting UC or PWT on those pages is wrong.
  437. * For Pentiums and earlier, the surround logic should disable
  438. * caching for the high addresses through the KEN pin, but
  439. * we maintain the tradition of paranoia in this code.
  440. */
  441. if (!pat_enabled &&
  442. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  443. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  444. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  445. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  446. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  447. flags = _PAGE_CACHE_UC;
  448. }
  449. #endif
  450. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  451. flags);
  452. return 1;
  453. }
  454. /*
  455. * Change the memory type for the physial address range in kernel identity
  456. * mapping space if that range is a part of identity map.
  457. */
  458. int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags)
  459. {
  460. unsigned long id_sz;
  461. if (base >= __pa(high_memory))
  462. return 0;
  463. id_sz = (__pa(high_memory) < base + size) ?
  464. __pa(high_memory) - base :
  465. size;
  466. if (ioremap_change_attr((unsigned long)__va(base), id_sz, flags) < 0) {
  467. printk(KERN_INFO
  468. "%s:%d ioremap_change_attr failed %s "
  469. "for %Lx-%Lx\n",
  470. current->comm, current->pid,
  471. cattr_name(flags),
  472. base, (unsigned long long)(base + size));
  473. return -EINVAL;
  474. }
  475. return 0;
  476. }
  477. /*
  478. * Internal interface to reserve a range of physical memory with prot.
  479. * Reserved non RAM regions only and after successful reserve_memtype,
  480. * this func also keeps identity mapping (if any) in sync with this new prot.
  481. */
  482. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  483. int strict_prot)
  484. {
  485. int is_ram = 0;
  486. int ret;
  487. unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
  488. unsigned long flags = want_flags;
  489. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  490. /*
  491. * reserve_pfn_range() for RAM pages. We do not refcount to keep
  492. * track of number of mappings of RAM pages. We can assert that
  493. * the type requested matches the type of first page in the range.
  494. */
  495. if (is_ram) {
  496. if (!pat_enabled)
  497. return 0;
  498. flags = lookup_memtype(paddr);
  499. if (want_flags != flags) {
  500. printk(KERN_WARNING
  501. "%s:%d map pfn RAM range req %s for %Lx-%Lx, got %s\n",
  502. current->comm, current->pid,
  503. cattr_name(want_flags),
  504. (unsigned long long)paddr,
  505. (unsigned long long)(paddr + size),
  506. cattr_name(flags));
  507. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  508. (~_PAGE_CACHE_MASK)) |
  509. flags);
  510. }
  511. return 0;
  512. }
  513. ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
  514. if (ret)
  515. return ret;
  516. if (flags != want_flags) {
  517. if (strict_prot ||
  518. !is_new_memtype_allowed(paddr, size, want_flags, flags)) {
  519. free_memtype(paddr, paddr + size);
  520. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  521. " for %Lx-%Lx, got %s\n",
  522. current->comm, current->pid,
  523. cattr_name(want_flags),
  524. (unsigned long long)paddr,
  525. (unsigned long long)(paddr + size),
  526. cattr_name(flags));
  527. return -EINVAL;
  528. }
  529. /*
  530. * We allow returning different type than the one requested in
  531. * non strict case.
  532. */
  533. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  534. (~_PAGE_CACHE_MASK)) |
  535. flags);
  536. }
  537. if (kernel_map_sync_memtype(paddr, size, flags) < 0) {
  538. free_memtype(paddr, paddr + size);
  539. return -EINVAL;
  540. }
  541. return 0;
  542. }
  543. /*
  544. * Internal interface to free a range of physical memory.
  545. * Frees non RAM regions only.
  546. */
  547. static void free_pfn_range(u64 paddr, unsigned long size)
  548. {
  549. int is_ram;
  550. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  551. if (is_ram == 0)
  552. free_memtype(paddr, paddr + size);
  553. }
  554. /*
  555. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  556. * copied through copy_page_range().
  557. *
  558. * If the vma has a linear pfn mapping for the entire range, we get the prot
  559. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  560. */
  561. int track_pfn_vma_copy(struct vm_area_struct *vma)
  562. {
  563. resource_size_t paddr;
  564. unsigned long prot;
  565. unsigned long vma_size = vma->vm_end - vma->vm_start;
  566. pgprot_t pgprot;
  567. if (is_linear_pfn_mapping(vma)) {
  568. /*
  569. * reserve the whole chunk covered by vma. We need the
  570. * starting address and protection from pte.
  571. */
  572. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  573. WARN_ON_ONCE(1);
  574. return -EINVAL;
  575. }
  576. pgprot = __pgprot(prot);
  577. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  578. }
  579. return 0;
  580. }
  581. /*
  582. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  583. * for physical range indicated by pfn and size.
  584. *
  585. * prot is passed in as a parameter for the new mapping. If the vma has a
  586. * linear pfn mapping for the entire range reserve the entire vma range with
  587. * single reserve_pfn_range call.
  588. */
  589. int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  590. unsigned long pfn, unsigned long size)
  591. {
  592. unsigned long flags;
  593. resource_size_t paddr;
  594. unsigned long vma_size = vma->vm_end - vma->vm_start;
  595. if (is_linear_pfn_mapping(vma)) {
  596. /* reserve the whole chunk starting from vm_pgoff */
  597. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  598. return reserve_pfn_range(paddr, vma_size, prot, 0);
  599. }
  600. if (!pat_enabled)
  601. return 0;
  602. /* for vm_insert_pfn and friends, we set prot based on lookup */
  603. flags = lookup_memtype(pfn << PAGE_SHIFT);
  604. *prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
  605. flags);
  606. return 0;
  607. }
  608. /*
  609. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  610. * untrack can be called for a specific region indicated by pfn and size or
  611. * can be for the entire vma (in which case size can be zero).
  612. */
  613. void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  614. unsigned long size)
  615. {
  616. resource_size_t paddr;
  617. unsigned long vma_size = vma->vm_end - vma->vm_start;
  618. if (is_linear_pfn_mapping(vma)) {
  619. /* free the whole chunk starting from vm_pgoff */
  620. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  621. free_pfn_range(paddr, vma_size);
  622. return;
  623. }
  624. }
  625. pgprot_t pgprot_writecombine(pgprot_t prot)
  626. {
  627. if (pat_enabled)
  628. return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
  629. else
  630. return pgprot_noncached(prot);
  631. }
  632. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  633. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  634. static struct memtype *memtype_get_idx(loff_t pos)
  635. {
  636. struct memtype *print_entry;
  637. int ret;
  638. print_entry = kzalloc(sizeof(struct memtype), GFP_KERNEL);
  639. if (!print_entry)
  640. return NULL;
  641. spin_lock(&memtype_lock);
  642. ret = rbt_memtype_copy_nth_element(print_entry, pos);
  643. spin_unlock(&memtype_lock);
  644. if (!ret) {
  645. return print_entry;
  646. } else {
  647. kfree(print_entry);
  648. return NULL;
  649. }
  650. }
  651. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  652. {
  653. if (*pos == 0) {
  654. ++*pos;
  655. seq_printf(seq, "PAT memtype list:\n");
  656. }
  657. return memtype_get_idx(*pos);
  658. }
  659. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  660. {
  661. ++*pos;
  662. return memtype_get_idx(*pos);
  663. }
  664. static void memtype_seq_stop(struct seq_file *seq, void *v)
  665. {
  666. }
  667. static int memtype_seq_show(struct seq_file *seq, void *v)
  668. {
  669. struct memtype *print_entry = (struct memtype *)v;
  670. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  671. print_entry->start, print_entry->end);
  672. kfree(print_entry);
  673. return 0;
  674. }
  675. static const struct seq_operations memtype_seq_ops = {
  676. .start = memtype_seq_start,
  677. .next = memtype_seq_next,
  678. .stop = memtype_seq_stop,
  679. .show = memtype_seq_show,
  680. };
  681. static int memtype_seq_open(struct inode *inode, struct file *file)
  682. {
  683. return seq_open(file, &memtype_seq_ops);
  684. }
  685. static const struct file_operations memtype_fops = {
  686. .open = memtype_seq_open,
  687. .read = seq_read,
  688. .llseek = seq_lseek,
  689. .release = seq_release,
  690. };
  691. static int __init pat_memtype_list_init(void)
  692. {
  693. if (pat_enabled) {
  694. debugfs_create_file("pat_memtype_list", S_IRUSR,
  695. arch_debugfs_dir, NULL, &memtype_fops);
  696. }
  697. return 0;
  698. }
  699. late_initcall(pat_memtype_list_init);
  700. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */