pat.c 20 KB

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