pat.c 20 KB

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