pat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. #include "mm_internal.h"
  32. #ifdef CONFIG_X86_PAT
  33. int __read_mostly pat_enabled = 1;
  34. static inline void pat_disable(const char *reason)
  35. {
  36. pat_enabled = 0;
  37. printk(KERN_INFO "%s\n", reason);
  38. }
  39. static int __init nopat(char *str)
  40. {
  41. pat_disable("PAT support disabled.");
  42. return 0;
  43. }
  44. early_param("nopat", nopat);
  45. #else
  46. static inline void pat_disable(const char *reason)
  47. {
  48. (void)reason;
  49. }
  50. #endif
  51. int pat_debug_enable;
  52. static int __init pat_debug_setup(char *str)
  53. {
  54. pat_debug_enable = 1;
  55. return 0;
  56. }
  57. __setup("debugpat", pat_debug_setup);
  58. static u64 __read_mostly boot_pat_state;
  59. #ifdef CONFIG_X86_PAT
  60. /*
  61. * X86 PAT uses page flags WC and Uncached together to keep track of
  62. * memory type of pages that have backing page struct. X86 PAT supports 3
  63. * different memory types, _PAGE_CACHE_MODE_WB, _PAGE_CACHE_MODE_WC and
  64. * _PAGE_CACHE_MODE_UC_MINUS and fourth state where page's memory type has not
  65. * been changed from its default (value of -1 used to denote this).
  66. * Note we do not support _PAGE_CACHE_MODE_UC here.
  67. */
  68. #define _PGMT_DEFAULT 0
  69. #define _PGMT_WC (1UL << PG_arch_1)
  70. #define _PGMT_UC_MINUS (1UL << PG_uncached)
  71. #define _PGMT_WB (1UL << PG_uncached | 1UL << PG_arch_1)
  72. #define _PGMT_MASK (1UL << PG_uncached | 1UL << PG_arch_1)
  73. #define _PGMT_CLEAR_MASK (~_PGMT_MASK)
  74. static inline enum page_cache_mode get_page_memtype(struct page *pg)
  75. {
  76. unsigned long pg_flags = pg->flags & _PGMT_MASK;
  77. if (pg_flags == _PGMT_DEFAULT)
  78. return -1;
  79. else if (pg_flags == _PGMT_WC)
  80. return _PAGE_CACHE_MODE_WC;
  81. else if (pg_flags == _PGMT_UC_MINUS)
  82. return _PAGE_CACHE_MODE_UC_MINUS;
  83. else
  84. return _PAGE_CACHE_MODE_WB;
  85. }
  86. static inline void set_page_memtype(struct page *pg,
  87. enum page_cache_mode memtype)
  88. {
  89. unsigned long memtype_flags;
  90. unsigned long old_flags;
  91. unsigned long new_flags;
  92. switch (memtype) {
  93. case _PAGE_CACHE_MODE_WC:
  94. memtype_flags = _PGMT_WC;
  95. break;
  96. case _PAGE_CACHE_MODE_UC_MINUS:
  97. memtype_flags = _PGMT_UC_MINUS;
  98. break;
  99. case _PAGE_CACHE_MODE_WB:
  100. memtype_flags = _PGMT_WB;
  101. break;
  102. default:
  103. memtype_flags = _PGMT_DEFAULT;
  104. break;
  105. }
  106. do {
  107. old_flags = pg->flags;
  108. new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags;
  109. } while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags);
  110. }
  111. #else
  112. static inline enum page_cache_mode get_page_memtype(struct page *pg)
  113. {
  114. return -1;
  115. }
  116. static inline void set_page_memtype(struct page *pg,
  117. enum page_cache_mode memtype)
  118. {
  119. }
  120. #endif
  121. enum {
  122. PAT_UC = 0, /* uncached */
  123. PAT_WC = 1, /* Write combining */
  124. PAT_WT = 4, /* Write Through */
  125. PAT_WP = 5, /* Write Protected */
  126. PAT_WB = 6, /* Write Back (default) */
  127. PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */
  128. };
  129. #define CM(c) (_PAGE_CACHE_MODE_ ## c)
  130. static enum page_cache_mode pat_get_cache_mode(unsigned pat_val, char *msg)
  131. {
  132. enum page_cache_mode cache;
  133. char *cache_mode;
  134. switch (pat_val) {
  135. case PAT_UC: cache = CM(UC); cache_mode = "UC "; break;
  136. case PAT_WC: cache = CM(WC); cache_mode = "WC "; break;
  137. case PAT_WT: cache = CM(WT); cache_mode = "WT "; break;
  138. case PAT_WP: cache = CM(WP); cache_mode = "WP "; break;
  139. case PAT_WB: cache = CM(WB); cache_mode = "WB "; break;
  140. case PAT_UC_MINUS: cache = CM(UC_MINUS); cache_mode = "UC- "; break;
  141. default: cache = CM(WB); cache_mode = "WB "; break;
  142. }
  143. memcpy(msg, cache_mode, 4);
  144. return cache;
  145. }
  146. #undef CM
  147. /*
  148. * Update the cache mode to pgprot translation tables according to PAT
  149. * configuration.
  150. * Using lower indices is preferred, so we start with highest index.
  151. */
  152. void pat_init_cache_modes(void)
  153. {
  154. int i;
  155. enum page_cache_mode cache;
  156. char pat_msg[33];
  157. u64 pat;
  158. rdmsrl(MSR_IA32_CR_PAT, pat);
  159. pat_msg[32] = 0;
  160. for (i = 7; i >= 0; i--) {
  161. cache = pat_get_cache_mode((pat >> (i * 8)) & 7,
  162. pat_msg + 4 * i);
  163. update_cache_mode_entry(i, cache);
  164. }
  165. pr_info("PAT configuration [0-7]: %s\n", pat_msg);
  166. }
  167. #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
  168. void pat_init(void)
  169. {
  170. u64 pat;
  171. bool boot_cpu = !boot_pat_state;
  172. if (!pat_enabled)
  173. return;
  174. if (!cpu_has_pat) {
  175. if (!boot_pat_state) {
  176. pat_disable("PAT not supported by CPU.");
  177. return;
  178. } else {
  179. /*
  180. * If this happens we are on a secondary CPU, but
  181. * switched to PAT on the boot CPU. We have no way to
  182. * undo PAT.
  183. */
  184. printk(KERN_ERR "PAT enabled, "
  185. "but not supported by secondary CPU\n");
  186. BUG();
  187. }
  188. }
  189. /* Set PWT to Write-Combining. All other bits stay the same */
  190. /*
  191. * PTE encoding used in Linux:
  192. * PAT
  193. * |PCD
  194. * ||PWT
  195. * |||
  196. * 000 WB _PAGE_CACHE_WB
  197. * 001 WC _PAGE_CACHE_WC
  198. * 010 UC- _PAGE_CACHE_UC_MINUS
  199. * 011 UC _PAGE_CACHE_UC
  200. * PAT bit unused
  201. */
  202. pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
  203. PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, UC);
  204. /* Boot CPU check */
  205. if (!boot_pat_state) {
  206. rdmsrl(MSR_IA32_CR_PAT, boot_pat_state);
  207. if (!boot_pat_state) {
  208. pat_disable("PAT read returns always zero, disabled.");
  209. return;
  210. }
  211. }
  212. wrmsrl(MSR_IA32_CR_PAT, pat);
  213. if (boot_cpu)
  214. pat_init_cache_modes();
  215. }
  216. #undef PAT
  217. static DEFINE_SPINLOCK(memtype_lock); /* protects memtype accesses */
  218. /*
  219. * Does intersection of PAT memory type and MTRR memory type and returns
  220. * the resulting memory type as PAT understands it.
  221. * (Type in pat and mtrr will not have same value)
  222. * The intersection is based on "Effective Memory Type" tables in IA-32
  223. * SDM vol 3a
  224. */
  225. static unsigned long pat_x_mtrr_type(u64 start, u64 end,
  226. enum page_cache_mode req_type)
  227. {
  228. /*
  229. * Look for MTRR hint to get the effective type in case where PAT
  230. * request is for WB.
  231. */
  232. if (req_type == _PAGE_CACHE_MODE_WB) {
  233. u8 mtrr_type;
  234. mtrr_type = mtrr_type_lookup(start, end);
  235. if (mtrr_type != MTRR_TYPE_WRBACK)
  236. return _PAGE_CACHE_MODE_UC_MINUS;
  237. return _PAGE_CACHE_MODE_WB;
  238. }
  239. return req_type;
  240. }
  241. struct pagerange_state {
  242. unsigned long cur_pfn;
  243. int ram;
  244. int not_ram;
  245. };
  246. static int
  247. pagerange_is_ram_callback(unsigned long initial_pfn, unsigned long total_nr_pages, void *arg)
  248. {
  249. struct pagerange_state *state = arg;
  250. state->not_ram |= initial_pfn > state->cur_pfn;
  251. state->ram |= total_nr_pages > 0;
  252. state->cur_pfn = initial_pfn + total_nr_pages;
  253. return state->ram && state->not_ram;
  254. }
  255. static int pat_pagerange_is_ram(resource_size_t start, resource_size_t end)
  256. {
  257. int ret = 0;
  258. unsigned long start_pfn = start >> PAGE_SHIFT;
  259. unsigned long end_pfn = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  260. struct pagerange_state state = {start_pfn, 0, 0};
  261. /*
  262. * For legacy reasons, physical address range in the legacy ISA
  263. * region is tracked as non-RAM. This will allow users of
  264. * /dev/mem to map portions of legacy ISA region, even when
  265. * some of those portions are listed(or not even listed) with
  266. * different e820 types(RAM/reserved/..)
  267. */
  268. if (start_pfn < ISA_END_ADDRESS >> PAGE_SHIFT)
  269. start_pfn = ISA_END_ADDRESS >> PAGE_SHIFT;
  270. if (start_pfn < end_pfn) {
  271. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn,
  272. &state, pagerange_is_ram_callback);
  273. }
  274. return (ret > 0) ? -1 : (state.ram ? 1 : 0);
  275. }
  276. /*
  277. * For RAM pages, we use page flags to mark the pages with appropriate type.
  278. * Here we do two pass:
  279. * - Find the memtype of all the pages in the range, look for any conflicts
  280. * - In case of no conflicts, set the new memtype for pages in the range
  281. */
  282. static int reserve_ram_pages_type(u64 start, u64 end,
  283. enum page_cache_mode req_type,
  284. enum page_cache_mode *new_type)
  285. {
  286. struct page *page;
  287. u64 pfn;
  288. if (req_type == _PAGE_CACHE_MODE_UC) {
  289. /* We do not support strong UC */
  290. WARN_ON_ONCE(1);
  291. req_type = _PAGE_CACHE_MODE_UC_MINUS;
  292. }
  293. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  294. enum page_cache_mode type;
  295. page = pfn_to_page(pfn);
  296. type = get_page_memtype(page);
  297. if (type != -1) {
  298. pr_info("reserve_ram_pages_type failed [mem %#010Lx-%#010Lx], track 0x%x, req 0x%x\n",
  299. start, end - 1, type, req_type);
  300. if (new_type)
  301. *new_type = type;
  302. return -EBUSY;
  303. }
  304. }
  305. if (new_type)
  306. *new_type = req_type;
  307. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  308. page = pfn_to_page(pfn);
  309. set_page_memtype(page, req_type);
  310. }
  311. return 0;
  312. }
  313. static int free_ram_pages_type(u64 start, u64 end)
  314. {
  315. struct page *page;
  316. u64 pfn;
  317. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  318. page = pfn_to_page(pfn);
  319. set_page_memtype(page, -1);
  320. }
  321. return 0;
  322. }
  323. /*
  324. * req_type typically has one of the:
  325. * - _PAGE_CACHE_MODE_WB
  326. * - _PAGE_CACHE_MODE_WC
  327. * - _PAGE_CACHE_MODE_UC_MINUS
  328. * - _PAGE_CACHE_MODE_UC
  329. *
  330. * If new_type is NULL, function will return an error if it cannot reserve the
  331. * region with req_type. If new_type is non-NULL, function will return
  332. * available type in new_type in case of no error. In case of any error
  333. * it will return a negative return value.
  334. */
  335. int reserve_memtype(u64 start, u64 end, enum page_cache_mode req_type,
  336. enum page_cache_mode *new_type)
  337. {
  338. struct memtype *new;
  339. enum page_cache_mode actual_type;
  340. int is_range_ram;
  341. int err = 0;
  342. BUG_ON(start >= end); /* end is exclusive */
  343. if (!pat_enabled) {
  344. /* This is identical to page table setting without PAT */
  345. if (new_type) {
  346. if (req_type == _PAGE_CACHE_MODE_WC)
  347. *new_type = _PAGE_CACHE_MODE_UC_MINUS;
  348. else
  349. *new_type = req_type;
  350. }
  351. return 0;
  352. }
  353. /* Low ISA region is always mapped WB in page table. No need to track */
  354. if (x86_platform.is_untracked_pat_range(start, end)) {
  355. if (new_type)
  356. *new_type = _PAGE_CACHE_MODE_WB;
  357. return 0;
  358. }
  359. /*
  360. * Call mtrr_lookup to get the type hint. This is an
  361. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  362. * tools and ACPI tools). Use WB request for WB memory and use
  363. * UC_MINUS otherwise.
  364. */
  365. actual_type = pat_x_mtrr_type(start, end, req_type);
  366. if (new_type)
  367. *new_type = actual_type;
  368. is_range_ram = pat_pagerange_is_ram(start, end);
  369. if (is_range_ram == 1) {
  370. err = reserve_ram_pages_type(start, end, req_type, new_type);
  371. return err;
  372. } else if (is_range_ram < 0) {
  373. return -EINVAL;
  374. }
  375. new = kzalloc(sizeof(struct memtype), GFP_KERNEL);
  376. if (!new)
  377. return -ENOMEM;
  378. new->start = start;
  379. new->end = end;
  380. new->type = actual_type;
  381. spin_lock(&memtype_lock);
  382. err = rbt_memtype_check_insert(new, new_type);
  383. if (err) {
  384. printk(KERN_INFO "reserve_memtype failed [mem %#010Lx-%#010Lx], track %s, req %s\n",
  385. start, end - 1,
  386. cattr_name(new->type), cattr_name(req_type));
  387. kfree(new);
  388. spin_unlock(&memtype_lock);
  389. return err;
  390. }
  391. spin_unlock(&memtype_lock);
  392. dprintk("reserve_memtype added [mem %#010Lx-%#010Lx], track %s, req %s, ret %s\n",
  393. start, end - 1, cattr_name(new->type), cattr_name(req_type),
  394. new_type ? cattr_name(*new_type) : "-");
  395. return err;
  396. }
  397. int free_memtype(u64 start, u64 end)
  398. {
  399. int err = -EINVAL;
  400. int is_range_ram;
  401. struct memtype *entry;
  402. if (!pat_enabled)
  403. return 0;
  404. /* Low ISA region is always mapped WB. No need to track */
  405. if (x86_platform.is_untracked_pat_range(start, end))
  406. return 0;
  407. is_range_ram = pat_pagerange_is_ram(start, end);
  408. if (is_range_ram == 1) {
  409. err = free_ram_pages_type(start, end);
  410. return err;
  411. } else if (is_range_ram < 0) {
  412. return -EINVAL;
  413. }
  414. spin_lock(&memtype_lock);
  415. entry = rbt_memtype_erase(start, end);
  416. spin_unlock(&memtype_lock);
  417. if (!entry) {
  418. printk(KERN_INFO "%s:%d freeing invalid memtype [mem %#010Lx-%#010Lx]\n",
  419. current->comm, current->pid, start, end - 1);
  420. return -EINVAL;
  421. }
  422. kfree(entry);
  423. dprintk("free_memtype request [mem %#010Lx-%#010Lx]\n", start, end - 1);
  424. return 0;
  425. }
  426. /**
  427. * lookup_memtype - Looksup the memory type for a physical address
  428. * @paddr: physical address of which memory type needs to be looked up
  429. *
  430. * Only to be called when PAT is enabled
  431. *
  432. * Returns _PAGE_CACHE_MODE_WB, _PAGE_CACHE_MODE_WC, _PAGE_CACHE_MODE_UC_MINUS
  433. * or _PAGE_CACHE_MODE_UC
  434. */
  435. static enum page_cache_mode lookup_memtype(u64 paddr)
  436. {
  437. enum page_cache_mode rettype = _PAGE_CACHE_MODE_WB;
  438. struct memtype *entry;
  439. if (x86_platform.is_untracked_pat_range(paddr, paddr + PAGE_SIZE))
  440. return rettype;
  441. if (pat_pagerange_is_ram(paddr, paddr + PAGE_SIZE)) {
  442. struct page *page;
  443. page = pfn_to_page(paddr >> PAGE_SHIFT);
  444. rettype = get_page_memtype(page);
  445. /*
  446. * -1 from get_page_memtype() implies RAM page is in its
  447. * default state and not reserved, and hence of type WB
  448. */
  449. if (rettype == -1)
  450. rettype = _PAGE_CACHE_MODE_WB;
  451. return rettype;
  452. }
  453. spin_lock(&memtype_lock);
  454. entry = rbt_memtype_lookup(paddr);
  455. if (entry != NULL)
  456. rettype = entry->type;
  457. else
  458. rettype = _PAGE_CACHE_MODE_UC_MINUS;
  459. spin_unlock(&memtype_lock);
  460. return rettype;
  461. }
  462. /**
  463. * io_reserve_memtype - Request a memory type mapping for a region of memory
  464. * @start: start (physical address) of the region
  465. * @end: end (physical address) of the region
  466. * @type: A pointer to memtype, with requested type. On success, requested
  467. * or any other compatible type that was available for the region is returned
  468. *
  469. * On success, returns 0
  470. * On failure, returns non-zero
  471. */
  472. int io_reserve_memtype(resource_size_t start, resource_size_t end,
  473. enum page_cache_mode *type)
  474. {
  475. resource_size_t size = end - start;
  476. enum page_cache_mode req_type = *type;
  477. enum page_cache_mode new_type;
  478. int ret;
  479. WARN_ON_ONCE(iomem_map_sanity_check(start, size));
  480. ret = reserve_memtype(start, end, req_type, &new_type);
  481. if (ret)
  482. goto out_err;
  483. if (!is_new_memtype_allowed(start, size, req_type, new_type))
  484. goto out_free;
  485. if (kernel_map_sync_memtype(start, size, new_type) < 0)
  486. goto out_free;
  487. *type = new_type;
  488. return 0;
  489. out_free:
  490. free_memtype(start, end);
  491. ret = -EBUSY;
  492. out_err:
  493. return ret;
  494. }
  495. /**
  496. * io_free_memtype - Release a memory type mapping for a region of memory
  497. * @start: start (physical address) of the region
  498. * @end: end (physical address) of the region
  499. */
  500. void io_free_memtype(resource_size_t start, resource_size_t end)
  501. {
  502. free_memtype(start, end);
  503. }
  504. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  505. unsigned long size, pgprot_t vma_prot)
  506. {
  507. return vma_prot;
  508. }
  509. #ifdef CONFIG_STRICT_DEVMEM
  510. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM */
  511. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  512. {
  513. return 1;
  514. }
  515. #else
  516. /* This check is needed to avoid cache aliasing when PAT is enabled */
  517. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  518. {
  519. u64 from = ((u64)pfn) << PAGE_SHIFT;
  520. u64 to = from + size;
  521. u64 cursor = from;
  522. if (!pat_enabled)
  523. return 1;
  524. while (cursor < to) {
  525. if (!devmem_is_allowed(pfn)) {
  526. printk(KERN_INFO "Program %s tried to access /dev/mem between [mem %#010Lx-%#010Lx], PAT prevents it\n",
  527. current->comm, from, to - 1);
  528. return 0;
  529. }
  530. cursor += PAGE_SIZE;
  531. pfn++;
  532. }
  533. return 1;
  534. }
  535. #endif /* CONFIG_STRICT_DEVMEM */
  536. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  537. unsigned long size, pgprot_t *vma_prot)
  538. {
  539. enum page_cache_mode pcm = _PAGE_CACHE_MODE_WB;
  540. if (!range_is_allowed(pfn, size))
  541. return 0;
  542. if (file->f_flags & O_DSYNC)
  543. pcm = _PAGE_CACHE_MODE_UC_MINUS;
  544. #ifdef CONFIG_X86_32
  545. /*
  546. * On the PPro and successors, the MTRRs are used to set
  547. * memory types for physical addresses outside main memory,
  548. * so blindly setting UC or PWT on those pages is wrong.
  549. * For Pentiums and earlier, the surround logic should disable
  550. * caching for the high addresses through the KEN pin, but
  551. * we maintain the tradition of paranoia in this code.
  552. */
  553. if (!pat_enabled &&
  554. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  555. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  556. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  557. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  558. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  559. pcm = _PAGE_CACHE_MODE_UC;
  560. }
  561. #endif
  562. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  563. cachemode2protval(pcm));
  564. return 1;
  565. }
  566. /*
  567. * Change the memory type for the physial address range in kernel identity
  568. * mapping space if that range is a part of identity map.
  569. */
  570. int kernel_map_sync_memtype(u64 base, unsigned long size,
  571. enum page_cache_mode pcm)
  572. {
  573. unsigned long id_sz;
  574. if (base > __pa(high_memory-1))
  575. return 0;
  576. /*
  577. * some areas in the middle of the kernel identity range
  578. * are not mapped, like the PCI space.
  579. */
  580. if (!page_is_ram(base >> PAGE_SHIFT))
  581. return 0;
  582. id_sz = (__pa(high_memory-1) <= base + size) ?
  583. __pa(high_memory) - base :
  584. size;
  585. if (ioremap_change_attr((unsigned long)__va(base), id_sz, pcm) < 0) {
  586. printk(KERN_INFO "%s:%d ioremap_change_attr failed %s "
  587. "for [mem %#010Lx-%#010Lx]\n",
  588. current->comm, current->pid,
  589. cattr_name(pcm),
  590. base, (unsigned long long)(base + size-1));
  591. return -EINVAL;
  592. }
  593. return 0;
  594. }
  595. /*
  596. * Internal interface to reserve a range of physical memory with prot.
  597. * Reserved non RAM regions only and after successful reserve_memtype,
  598. * this func also keeps identity mapping (if any) in sync with this new prot.
  599. */
  600. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  601. int strict_prot)
  602. {
  603. int is_ram = 0;
  604. int ret;
  605. enum page_cache_mode want_pcm = pgprot2cachemode(*vma_prot);
  606. enum page_cache_mode pcm = want_pcm;
  607. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  608. /*
  609. * reserve_pfn_range() for RAM pages. We do not refcount to keep
  610. * track of number of mappings of RAM pages. We can assert that
  611. * the type requested matches the type of first page in the range.
  612. */
  613. if (is_ram) {
  614. if (!pat_enabled)
  615. return 0;
  616. pcm = lookup_memtype(paddr);
  617. if (want_pcm != pcm) {
  618. printk(KERN_WARNING "%s:%d map pfn RAM range req %s for [mem %#010Lx-%#010Lx], got %s\n",
  619. current->comm, current->pid,
  620. cattr_name(want_pcm),
  621. (unsigned long long)paddr,
  622. (unsigned long long)(paddr + size - 1),
  623. cattr_name(pcm));
  624. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  625. (~_PAGE_CACHE_MASK)) |
  626. cachemode2protval(pcm));
  627. }
  628. return 0;
  629. }
  630. ret = reserve_memtype(paddr, paddr + size, want_pcm, &pcm);
  631. if (ret)
  632. return ret;
  633. if (pcm != want_pcm) {
  634. if (strict_prot ||
  635. !is_new_memtype_allowed(paddr, size, want_pcm, pcm)) {
  636. free_memtype(paddr, paddr + size);
  637. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  638. " for [mem %#010Lx-%#010Lx], got %s\n",
  639. current->comm, current->pid,
  640. cattr_name(want_pcm),
  641. (unsigned long long)paddr,
  642. (unsigned long long)(paddr + size - 1),
  643. cattr_name(pcm));
  644. return -EINVAL;
  645. }
  646. /*
  647. * We allow returning different type than the one requested in
  648. * non strict case.
  649. */
  650. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  651. (~_PAGE_CACHE_MASK)) |
  652. cachemode2protval(pcm));
  653. }
  654. if (kernel_map_sync_memtype(paddr, size, pcm) < 0) {
  655. free_memtype(paddr, paddr + size);
  656. return -EINVAL;
  657. }
  658. return 0;
  659. }
  660. /*
  661. * Internal interface to free a range of physical memory.
  662. * Frees non RAM regions only.
  663. */
  664. static void free_pfn_range(u64 paddr, unsigned long size)
  665. {
  666. int is_ram;
  667. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  668. if (is_ram == 0)
  669. free_memtype(paddr, paddr + size);
  670. }
  671. /*
  672. * track_pfn_copy is called when vma that is covering the pfnmap gets
  673. * copied through copy_page_range().
  674. *
  675. * If the vma has a linear pfn mapping for the entire range, we get the prot
  676. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  677. */
  678. int track_pfn_copy(struct vm_area_struct *vma)
  679. {
  680. resource_size_t paddr;
  681. unsigned long prot;
  682. unsigned long vma_size = vma->vm_end - vma->vm_start;
  683. pgprot_t pgprot;
  684. if (vma->vm_flags & VM_PAT) {
  685. /*
  686. * reserve the whole chunk covered by vma. We need the
  687. * starting address and protection from pte.
  688. */
  689. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  690. WARN_ON_ONCE(1);
  691. return -EINVAL;
  692. }
  693. pgprot = __pgprot(prot);
  694. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  695. }
  696. return 0;
  697. }
  698. /*
  699. * prot is passed in as a parameter for the new mapping. If the vma has a
  700. * linear pfn mapping for the entire range reserve the entire vma range with
  701. * single reserve_pfn_range call.
  702. */
  703. int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  704. unsigned long pfn, unsigned long addr, unsigned long size)
  705. {
  706. resource_size_t paddr = (resource_size_t)pfn << PAGE_SHIFT;
  707. enum page_cache_mode pcm;
  708. /* reserve the whole chunk starting from paddr */
  709. if (addr == vma->vm_start && size == (vma->vm_end - vma->vm_start)) {
  710. int ret;
  711. ret = reserve_pfn_range(paddr, size, prot, 0);
  712. if (!ret)
  713. vma->vm_flags |= VM_PAT;
  714. return ret;
  715. }
  716. if (!pat_enabled)
  717. return 0;
  718. /*
  719. * For anything smaller than the vma size we set prot based on the
  720. * lookup.
  721. */
  722. pcm = lookup_memtype(paddr);
  723. /* Check memtype for the remaining pages */
  724. while (size > PAGE_SIZE) {
  725. size -= PAGE_SIZE;
  726. paddr += PAGE_SIZE;
  727. if (pcm != lookup_memtype(paddr))
  728. return -EINVAL;
  729. }
  730. *prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
  731. cachemode2protval(pcm));
  732. return 0;
  733. }
  734. int track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
  735. unsigned long pfn)
  736. {
  737. enum page_cache_mode pcm;
  738. if (!pat_enabled)
  739. return 0;
  740. /* Set prot based on lookup */
  741. pcm = lookup_memtype((resource_size_t)pfn << PAGE_SHIFT);
  742. *prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
  743. cachemode2protval(pcm));
  744. return 0;
  745. }
  746. /*
  747. * untrack_pfn is called while unmapping a pfnmap for a region.
  748. * untrack can be called for a specific region indicated by pfn and size or
  749. * can be for the entire vma (in which case pfn, size are zero).
  750. */
  751. void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
  752. unsigned long size)
  753. {
  754. resource_size_t paddr;
  755. unsigned long prot;
  756. if (!(vma->vm_flags & VM_PAT))
  757. return;
  758. /* free the chunk starting from pfn or the whole chunk */
  759. paddr = (resource_size_t)pfn << PAGE_SHIFT;
  760. if (!paddr && !size) {
  761. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  762. WARN_ON_ONCE(1);
  763. return;
  764. }
  765. size = vma->vm_end - vma->vm_start;
  766. }
  767. free_pfn_range(paddr, size);
  768. vma->vm_flags &= ~VM_PAT;
  769. }
  770. pgprot_t pgprot_writecombine(pgprot_t prot)
  771. {
  772. if (pat_enabled)
  773. return __pgprot(pgprot_val(prot) |
  774. cachemode2protval(_PAGE_CACHE_MODE_WC));
  775. else
  776. return pgprot_noncached(prot);
  777. }
  778. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  779. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  780. static struct memtype *memtype_get_idx(loff_t pos)
  781. {
  782. struct memtype *print_entry;
  783. int ret;
  784. print_entry = kzalloc(sizeof(struct memtype), GFP_KERNEL);
  785. if (!print_entry)
  786. return NULL;
  787. spin_lock(&memtype_lock);
  788. ret = rbt_memtype_copy_nth_element(print_entry, pos);
  789. spin_unlock(&memtype_lock);
  790. if (!ret) {
  791. return print_entry;
  792. } else {
  793. kfree(print_entry);
  794. return NULL;
  795. }
  796. }
  797. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  798. {
  799. if (*pos == 0) {
  800. ++*pos;
  801. seq_puts(seq, "PAT memtype list:\n");
  802. }
  803. return memtype_get_idx(*pos);
  804. }
  805. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  806. {
  807. ++*pos;
  808. return memtype_get_idx(*pos);
  809. }
  810. static void memtype_seq_stop(struct seq_file *seq, void *v)
  811. {
  812. }
  813. static int memtype_seq_show(struct seq_file *seq, void *v)
  814. {
  815. struct memtype *print_entry = (struct memtype *)v;
  816. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  817. print_entry->start, print_entry->end);
  818. kfree(print_entry);
  819. return 0;
  820. }
  821. static const struct seq_operations memtype_seq_ops = {
  822. .start = memtype_seq_start,
  823. .next = memtype_seq_next,
  824. .stop = memtype_seq_stop,
  825. .show = memtype_seq_show,
  826. };
  827. static int memtype_seq_open(struct inode *inode, struct file *file)
  828. {
  829. return seq_open(file, &memtype_seq_ops);
  830. }
  831. static const struct file_operations memtype_fops = {
  832. .open = memtype_seq_open,
  833. .read = seq_read,
  834. .llseek = seq_lseek,
  835. .release = seq_release,
  836. };
  837. static int __init pat_memtype_list_init(void)
  838. {
  839. if (pat_enabled) {
  840. debugfs_create_file("pat_memtype_list", S_IRUSR,
  841. arch_debugfs_dir, NULL, &memtype_fops);
  842. }
  843. return 0;
  844. }
  845. late_initcall(pat_memtype_list_init);
  846. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */