pat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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/gfp.h>
  14. #include <linux/mm.h>
  15. #include <linux/fs.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/processor.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/fcntl.h>
  21. #include <asm/e820.h>
  22. #include <asm/mtrr.h>
  23. #include <asm/page.h>
  24. #include <asm/msr.h>
  25. #include <asm/pat.h>
  26. #include <asm/io.h>
  27. #ifdef CONFIG_X86_PAT
  28. int __read_mostly pat_enabled = 1;
  29. void __cpuinit pat_disable(const char *reason)
  30. {
  31. pat_enabled = 0;
  32. printk(KERN_INFO "%s\n", reason);
  33. }
  34. static int __init nopat(char *str)
  35. {
  36. pat_disable("PAT support disabled.");
  37. return 0;
  38. }
  39. early_param("nopat", nopat);
  40. #else
  41. static inline void pat_disable(const char *reason)
  42. {
  43. (void)reason;
  44. }
  45. #endif
  46. static int debug_enable;
  47. static int __init pat_debug_setup(char *str)
  48. {
  49. debug_enable = 1;
  50. return 0;
  51. }
  52. __setup("debugpat", pat_debug_setup);
  53. #define dprintk(fmt, arg...) \
  54. do { if (debug_enable) printk(KERN_INFO fmt, ##arg); } while (0)
  55. static u64 __read_mostly boot_pat_state;
  56. enum {
  57. PAT_UC = 0, /* uncached */
  58. PAT_WC = 1, /* Write combining */
  59. PAT_WT = 4, /* Write Through */
  60. PAT_WP = 5, /* Write Protected */
  61. PAT_WB = 6, /* Write Back (default) */
  62. PAT_UC_MINUS = 7, /* UC, but can be overriden by MTRR */
  63. };
  64. #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
  65. void pat_init(void)
  66. {
  67. u64 pat;
  68. if (!pat_enabled)
  69. return;
  70. if (!cpu_has_pat) {
  71. if (!boot_pat_state) {
  72. pat_disable("PAT not supported by CPU.");
  73. return;
  74. } else {
  75. /*
  76. * If this happens we are on a secondary CPU, but
  77. * switched to PAT on the boot CPU. We have no way to
  78. * undo PAT.
  79. */
  80. printk(KERN_ERR "PAT enabled, "
  81. "but not supported by secondary CPU\n");
  82. BUG();
  83. }
  84. }
  85. /* Set PWT to Write-Combining. All other bits stay the same */
  86. /*
  87. * PTE encoding used in Linux:
  88. * PAT
  89. * |PCD
  90. * ||PWT
  91. * |||
  92. * 000 WB _PAGE_CACHE_WB
  93. * 001 WC _PAGE_CACHE_WC
  94. * 010 UC- _PAGE_CACHE_UC_MINUS
  95. * 011 UC _PAGE_CACHE_UC
  96. * PAT bit unused
  97. */
  98. pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
  99. PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, UC);
  100. /* Boot CPU check */
  101. if (!boot_pat_state)
  102. rdmsrl(MSR_IA32_CR_PAT, boot_pat_state);
  103. wrmsrl(MSR_IA32_CR_PAT, pat);
  104. printk(KERN_INFO "x86 PAT enabled: cpu %d, old 0x%Lx, new 0x%Lx\n",
  105. smp_processor_id(), boot_pat_state, pat);
  106. }
  107. #undef PAT
  108. static char *cattr_name(unsigned long flags)
  109. {
  110. switch (flags & _PAGE_CACHE_MASK) {
  111. case _PAGE_CACHE_UC: return "uncached";
  112. case _PAGE_CACHE_UC_MINUS: return "uncached-minus";
  113. case _PAGE_CACHE_WB: return "write-back";
  114. case _PAGE_CACHE_WC: return "write-combining";
  115. default: return "broken";
  116. }
  117. }
  118. /*
  119. * The global memtype list keeps track of memory type for specific
  120. * physical memory areas. Conflicting memory types in different
  121. * mappings can cause CPU cache corruption. To avoid this we keep track.
  122. *
  123. * The list is sorted based on starting address and can contain multiple
  124. * entries for each address (this allows reference counting for overlapping
  125. * areas). All the aliases have the same cache attributes of course.
  126. * Zero attributes are represented as holes.
  127. *
  128. * Currently the data structure is a list because the number of mappings
  129. * are expected to be relatively small. If this should be a problem
  130. * it could be changed to a rbtree or similar.
  131. *
  132. * memtype_lock protects the whole list.
  133. */
  134. struct memtype {
  135. u64 start;
  136. u64 end;
  137. unsigned long type;
  138. struct list_head nd;
  139. };
  140. static LIST_HEAD(memtype_list);
  141. static DEFINE_SPINLOCK(memtype_lock); /* protects memtype list */
  142. /*
  143. * Does intersection of PAT memory type and MTRR memory type and returns
  144. * the resulting memory type as PAT understands it.
  145. * (Type in pat and mtrr will not have same value)
  146. * The intersection is based on "Effective Memory Type" tables in IA-32
  147. * SDM vol 3a
  148. */
  149. static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type)
  150. {
  151. /*
  152. * Look for MTRR hint to get the effective type in case where PAT
  153. * request is for WB.
  154. */
  155. if (req_type == _PAGE_CACHE_WB) {
  156. u8 mtrr_type;
  157. mtrr_type = mtrr_type_lookup(start, end);
  158. if (mtrr_type == MTRR_TYPE_UNCACHABLE)
  159. return _PAGE_CACHE_UC;
  160. if (mtrr_type == MTRR_TYPE_WRCOMB)
  161. return _PAGE_CACHE_WC;
  162. }
  163. return req_type;
  164. }
  165. static int
  166. chk_conflict(struct memtype *new, struct memtype *entry, unsigned long *type)
  167. {
  168. if (new->type != entry->type) {
  169. if (type) {
  170. new->type = entry->type;
  171. *type = entry->type;
  172. } else
  173. goto conflict;
  174. }
  175. /* check overlaps with more than one entry in the list */
  176. list_for_each_entry_continue(entry, &memtype_list, nd) {
  177. if (new->end <= entry->start)
  178. break;
  179. else if (new->type != entry->type)
  180. goto conflict;
  181. }
  182. return 0;
  183. conflict:
  184. printk(KERN_INFO "%s:%d conflicting memory types "
  185. "%Lx-%Lx %s<->%s\n", current->comm, current->pid, new->start,
  186. new->end, cattr_name(new->type), cattr_name(entry->type));
  187. return -EBUSY;
  188. }
  189. static struct memtype *cached_entry;
  190. static u64 cached_start;
  191. static int pat_pagerange_is_ram(unsigned long start, unsigned long end)
  192. {
  193. int ram_page = 0, not_rampage = 0;
  194. unsigned long page_nr;
  195. for (page_nr = (start >> PAGE_SHIFT); page_nr < (end >> PAGE_SHIFT);
  196. ++page_nr) {
  197. /*
  198. * For legacy reasons, physical address range in the legacy ISA
  199. * region is tracked as non-RAM. This will allow users of
  200. * /dev/mem to map portions of legacy ISA region, even when
  201. * some of those portions are listed(or not even listed) with
  202. * different e820 types(RAM/reserved/..)
  203. */
  204. if (page_nr >= (ISA_END_ADDRESS >> PAGE_SHIFT) &&
  205. page_is_ram(page_nr))
  206. ram_page = 1;
  207. else
  208. not_rampage = 1;
  209. if (ram_page == not_rampage)
  210. return -1;
  211. }
  212. return ram_page;
  213. }
  214. /*
  215. * For RAM pages, mark the pages as non WB memory type using
  216. * PageNonWB (PG_arch_1). We allow only one set_memory_uc() or
  217. * set_memory_wc() on a RAM page at a time before marking it as WB again.
  218. * This is ok, because only one driver will be owning the page and
  219. * doing set_memory_*() calls.
  220. *
  221. * For now, we use PageNonWB to track that the RAM page is being mapped
  222. * as non WB. In future, we will have to use one more flag
  223. * (or some other mechanism in page_struct) to distinguish between
  224. * UC and WC mapping.
  225. */
  226. static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type,
  227. unsigned long *new_type)
  228. {
  229. struct page *page;
  230. u64 pfn, end_pfn;
  231. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  232. page = pfn_to_page(pfn);
  233. if (page_mapped(page) || PageNonWB(page))
  234. goto out;
  235. SetPageNonWB(page);
  236. }
  237. return 0;
  238. out:
  239. end_pfn = pfn;
  240. for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
  241. page = pfn_to_page(pfn);
  242. ClearPageNonWB(page);
  243. }
  244. return -EINVAL;
  245. }
  246. static int free_ram_pages_type(u64 start, u64 end)
  247. {
  248. struct page *page;
  249. u64 pfn, end_pfn;
  250. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  251. page = pfn_to_page(pfn);
  252. if (page_mapped(page) || !PageNonWB(page))
  253. goto out;
  254. ClearPageNonWB(page);
  255. }
  256. return 0;
  257. out:
  258. end_pfn = pfn;
  259. for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
  260. page = pfn_to_page(pfn);
  261. SetPageNonWB(page);
  262. }
  263. return -EINVAL;
  264. }
  265. /*
  266. * req_type typically has one of the:
  267. * - _PAGE_CACHE_WB
  268. * - _PAGE_CACHE_WC
  269. * - _PAGE_CACHE_UC_MINUS
  270. * - _PAGE_CACHE_UC
  271. *
  272. * req_type will have a special case value '-1', when requester want to inherit
  273. * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
  274. *
  275. * If new_type is NULL, function will return an error if it cannot reserve the
  276. * region with req_type. If new_type is non-NULL, function will return
  277. * available type in new_type in case of no error. In case of any error
  278. * it will return a negative return value.
  279. */
  280. int reserve_memtype(u64 start, u64 end, unsigned long req_type,
  281. unsigned long *new_type)
  282. {
  283. struct memtype *new, *entry;
  284. unsigned long actual_type;
  285. struct list_head *where;
  286. int is_range_ram;
  287. int err = 0;
  288. BUG_ON(start >= end); /* end is exclusive */
  289. if (!pat_enabled) {
  290. /* This is identical to page table setting without PAT */
  291. if (new_type) {
  292. if (req_type == -1)
  293. *new_type = _PAGE_CACHE_WB;
  294. else
  295. *new_type = req_type & _PAGE_CACHE_MASK;
  296. }
  297. return 0;
  298. }
  299. /* Low ISA region is always mapped WB in page table. No need to track */
  300. if (is_ISA_range(start, end - 1)) {
  301. if (new_type)
  302. *new_type = _PAGE_CACHE_WB;
  303. return 0;
  304. }
  305. if (req_type == -1) {
  306. /*
  307. * Call mtrr_lookup to get the type hint. This is an
  308. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  309. * tools and ACPI tools). Use WB request for WB memory and use
  310. * UC_MINUS otherwise.
  311. */
  312. u8 mtrr_type = mtrr_type_lookup(start, end);
  313. if (mtrr_type == MTRR_TYPE_WRBACK)
  314. actual_type = _PAGE_CACHE_WB;
  315. else
  316. actual_type = _PAGE_CACHE_UC_MINUS;
  317. } else {
  318. actual_type = pat_x_mtrr_type(start, end,
  319. req_type & _PAGE_CACHE_MASK);
  320. }
  321. if (new_type)
  322. *new_type = actual_type;
  323. is_range_ram = pat_pagerange_is_ram(start, end);
  324. if (is_range_ram == 1)
  325. return reserve_ram_pages_type(start, end, req_type,
  326. new_type);
  327. else if (is_range_ram < 0)
  328. return -EINVAL;
  329. new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  330. if (!new)
  331. return -ENOMEM;
  332. new->start = start;
  333. new->end = end;
  334. new->type = actual_type;
  335. spin_lock(&memtype_lock);
  336. if (cached_entry && start >= cached_start)
  337. entry = cached_entry;
  338. else
  339. entry = list_entry(&memtype_list, struct memtype, nd);
  340. /* Search for existing mapping that overlaps the current range */
  341. where = NULL;
  342. list_for_each_entry_continue(entry, &memtype_list, nd) {
  343. if (end <= entry->start) {
  344. where = entry->nd.prev;
  345. cached_entry = list_entry(where, struct memtype, nd);
  346. break;
  347. } else if (start <= entry->start) { /* end > entry->start */
  348. err = chk_conflict(new, entry, new_type);
  349. if (!err) {
  350. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  351. entry->start, entry->end);
  352. where = entry->nd.prev;
  353. cached_entry = list_entry(where,
  354. struct memtype, nd);
  355. }
  356. break;
  357. } else if (start < entry->end) { /* start > entry->start */
  358. err = chk_conflict(new, entry, new_type);
  359. if (!err) {
  360. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  361. entry->start, entry->end);
  362. cached_entry = list_entry(entry->nd.prev,
  363. struct memtype, nd);
  364. /*
  365. * Move to right position in the linked
  366. * list to add this new entry
  367. */
  368. list_for_each_entry_continue(entry,
  369. &memtype_list, nd) {
  370. if (start <= entry->start) {
  371. where = entry->nd.prev;
  372. break;
  373. }
  374. }
  375. }
  376. break;
  377. }
  378. }
  379. if (err) {
  380. printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
  381. "track %s, req %s\n",
  382. start, end, cattr_name(new->type), cattr_name(req_type));
  383. kfree(new);
  384. spin_unlock(&memtype_lock);
  385. return err;
  386. }
  387. cached_start = start;
  388. if (where)
  389. list_add(&new->nd, where);
  390. else
  391. list_add_tail(&new->nd, &memtype_list);
  392. spin_unlock(&memtype_lock);
  393. dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
  394. start, end, cattr_name(new->type), cattr_name(req_type),
  395. new_type ? cattr_name(*new_type) : "-");
  396. return err;
  397. }
  398. int free_memtype(u64 start, u64 end)
  399. {
  400. struct memtype *entry;
  401. int err = -EINVAL;
  402. int is_range_ram;
  403. if (!pat_enabled)
  404. return 0;
  405. /* Low ISA region is always mapped WB. No need to track */
  406. if (is_ISA_range(start, end - 1))
  407. return 0;
  408. is_range_ram = pat_pagerange_is_ram(start, end);
  409. if (is_range_ram == 1)
  410. return free_ram_pages_type(start, end);
  411. else if (is_range_ram < 0)
  412. return -EINVAL;
  413. spin_lock(&memtype_lock);
  414. list_for_each_entry(entry, &memtype_list, nd) {
  415. if (entry->start == start && entry->end == end) {
  416. if (cached_entry == entry || cached_start == start)
  417. cached_entry = NULL;
  418. list_del(&entry->nd);
  419. kfree(entry);
  420. err = 0;
  421. break;
  422. }
  423. }
  424. spin_unlock(&memtype_lock);
  425. if (err) {
  426. printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
  427. current->comm, current->pid, start, end);
  428. }
  429. dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
  430. return err;
  431. }
  432. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  433. unsigned long size, pgprot_t vma_prot)
  434. {
  435. return vma_prot;
  436. }
  437. #ifdef CONFIG_STRICT_DEVMEM
  438. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
  439. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  440. {
  441. return 1;
  442. }
  443. #else
  444. /* This check is needed to avoid cache aliasing when PAT is enabled */
  445. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  446. {
  447. u64 from = ((u64)pfn) << PAGE_SHIFT;
  448. u64 to = from + size;
  449. u64 cursor = from;
  450. if (!pat_enabled)
  451. return 1;
  452. while (cursor < to) {
  453. if (!devmem_is_allowed(pfn)) {
  454. printk(KERN_INFO
  455. "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  456. current->comm, from, to);
  457. return 0;
  458. }
  459. cursor += PAGE_SIZE;
  460. pfn++;
  461. }
  462. return 1;
  463. }
  464. #endif /* CONFIG_STRICT_DEVMEM */
  465. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  466. unsigned long size, pgprot_t *vma_prot)
  467. {
  468. u64 offset = ((u64) pfn) << PAGE_SHIFT;
  469. unsigned long flags = -1;
  470. int retval;
  471. if (!range_is_allowed(pfn, size))
  472. return 0;
  473. if (file->f_flags & O_SYNC) {
  474. flags = _PAGE_CACHE_UC_MINUS;
  475. }
  476. #ifdef CONFIG_X86_32
  477. /*
  478. * On the PPro and successors, the MTRRs are used to set
  479. * memory types for physical addresses outside main memory,
  480. * so blindly setting UC or PWT on those pages is wrong.
  481. * For Pentiums and earlier, the surround logic should disable
  482. * caching for the high addresses through the KEN pin, but
  483. * we maintain the tradition of paranoia in this code.
  484. */
  485. if (!pat_enabled &&
  486. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  487. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  488. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  489. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  490. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  491. flags = _PAGE_CACHE_UC;
  492. }
  493. #endif
  494. /*
  495. * With O_SYNC, we can only take UC_MINUS mapping. Fail if we cannot.
  496. *
  497. * Without O_SYNC, we want to get
  498. * - WB for WB-able memory and no other conflicting mappings
  499. * - UC_MINUS for non-WB-able memory with no other conflicting mappings
  500. * - Inherit from confliting mappings otherwise
  501. */
  502. if (flags != -1) {
  503. retval = reserve_memtype(offset, offset + size, flags, NULL);
  504. } else {
  505. retval = reserve_memtype(offset, offset + size, -1, &flags);
  506. }
  507. if (retval < 0)
  508. return 0;
  509. if (((pfn < max_low_pfn_mapped) ||
  510. (pfn >= (1UL<<(32 - PAGE_SHIFT)) && pfn < max_pfn_mapped)) &&
  511. ioremap_change_attr((unsigned long)__va(offset), size, flags) < 0) {
  512. free_memtype(offset, offset + size);
  513. printk(KERN_INFO
  514. "%s:%d /dev/mem ioremap_change_attr failed %s for %Lx-%Lx\n",
  515. current->comm, current->pid,
  516. cattr_name(flags),
  517. offset, (unsigned long long)(offset + size));
  518. return 0;
  519. }
  520. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  521. flags);
  522. return 1;
  523. }
  524. void map_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
  525. {
  526. unsigned long want_flags = (pgprot_val(vma_prot) & _PAGE_CACHE_MASK);
  527. u64 addr = (u64)pfn << PAGE_SHIFT;
  528. unsigned long flags;
  529. reserve_memtype(addr, addr + size, want_flags, &flags);
  530. if (flags != want_flags) {
  531. printk(KERN_INFO
  532. "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n",
  533. current->comm, current->pid,
  534. cattr_name(want_flags),
  535. addr, (unsigned long long)(addr + size),
  536. cattr_name(flags));
  537. }
  538. }
  539. void unmap_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
  540. {
  541. u64 addr = (u64)pfn << PAGE_SHIFT;
  542. free_memtype(addr, addr + size);
  543. }
  544. /*
  545. * Change the memory type for the physial address range in kernel identity
  546. * mapping space if that range is a part of identity map.
  547. */
  548. int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags)
  549. {
  550. unsigned long id_sz;
  551. if (!pat_enabled || base >= __pa(high_memory))
  552. return 0;
  553. id_sz = (__pa(high_memory) < base + size) ?
  554. __pa(high_memory) - base :
  555. size;
  556. if (ioremap_change_attr((unsigned long)__va(base), id_sz, flags) < 0) {
  557. printk(KERN_INFO
  558. "%s:%d ioremap_change_attr failed %s "
  559. "for %Lx-%Lx\n",
  560. current->comm, current->pid,
  561. cattr_name(flags),
  562. base, (unsigned long long)(base + size));
  563. return -EINVAL;
  564. }
  565. return 0;
  566. }
  567. /*
  568. * Internal interface to reserve a range of physical memory with prot.
  569. * Reserved non RAM regions only and after successful reserve_memtype,
  570. * this func also keeps identity mapping (if any) in sync with this new prot.
  571. */
  572. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  573. int strict_prot)
  574. {
  575. int is_ram = 0;
  576. int ret;
  577. unsigned long flags;
  578. unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
  579. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  580. /*
  581. * reserve_pfn_range() doesn't support RAM pages.
  582. */
  583. if (is_ram != 0)
  584. return -EINVAL;
  585. ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
  586. if (ret)
  587. return ret;
  588. if (flags != want_flags) {
  589. if (strict_prot || !is_new_memtype_allowed(want_flags, flags)) {
  590. free_memtype(paddr, paddr + size);
  591. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  592. " for %Lx-%Lx, got %s\n",
  593. current->comm, current->pid,
  594. cattr_name(want_flags),
  595. (unsigned long long)paddr,
  596. (unsigned long long)(paddr + size),
  597. cattr_name(flags));
  598. return -EINVAL;
  599. }
  600. /*
  601. * We allow returning different type than the one requested in
  602. * non strict case.
  603. */
  604. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  605. (~_PAGE_CACHE_MASK)) |
  606. flags);
  607. }
  608. if (kernel_map_sync_memtype(paddr, size, flags) < 0) {
  609. free_memtype(paddr, paddr + size);
  610. return -EINVAL;
  611. }
  612. return 0;
  613. }
  614. /*
  615. * Internal interface to free a range of physical memory.
  616. * Frees non RAM regions only.
  617. */
  618. static void free_pfn_range(u64 paddr, unsigned long size)
  619. {
  620. int is_ram;
  621. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  622. if (is_ram == 0)
  623. free_memtype(paddr, paddr + size);
  624. }
  625. /*
  626. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  627. * copied through copy_page_range().
  628. *
  629. * If the vma has a linear pfn mapping for the entire range, we get the prot
  630. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  631. * Otherwise, we reserve the entire vma range, my ging through the PTEs page
  632. * by page to get physical address and protection.
  633. */
  634. int track_pfn_vma_copy(struct vm_area_struct *vma)
  635. {
  636. int retval = 0;
  637. unsigned long i, j;
  638. resource_size_t paddr;
  639. unsigned long prot;
  640. unsigned long vma_start = vma->vm_start;
  641. unsigned long vma_end = vma->vm_end;
  642. unsigned long vma_size = vma_end - vma_start;
  643. pgprot_t pgprot;
  644. if (!pat_enabled)
  645. return 0;
  646. if (is_linear_pfn_mapping(vma)) {
  647. /*
  648. * reserve the whole chunk covered by vma. We need the
  649. * starting address and protection from pte.
  650. */
  651. if (follow_phys(vma, vma_start, 0, &prot, &paddr)) {
  652. WARN_ON_ONCE(1);
  653. return -EINVAL;
  654. }
  655. pgprot = __pgprot(prot);
  656. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  657. }
  658. /* reserve entire vma page by page, using pfn and prot from pte */
  659. for (i = 0; i < vma_size; i += PAGE_SIZE) {
  660. if (follow_phys(vma, vma_start + i, 0, &prot, &paddr))
  661. continue;
  662. pgprot = __pgprot(prot);
  663. retval = reserve_pfn_range(paddr, PAGE_SIZE, &pgprot, 1);
  664. if (retval)
  665. goto cleanup_ret;
  666. }
  667. return 0;
  668. cleanup_ret:
  669. /* Reserve error: Cleanup partial reservation and return error */
  670. for (j = 0; j < i; j += PAGE_SIZE) {
  671. if (follow_phys(vma, vma_start + j, 0, &prot, &paddr))
  672. continue;
  673. free_pfn_range(paddr, PAGE_SIZE);
  674. }
  675. return retval;
  676. }
  677. /*
  678. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  679. * for physical range indicated by pfn and size.
  680. *
  681. * prot is passed in as a parameter for the new mapping. If the vma has a
  682. * linear pfn mapping for the entire range reserve the entire vma range with
  683. * single reserve_pfn_range call.
  684. * Otherwise, we look t the pfn and size and reserve only the specified range
  685. * page by page.
  686. *
  687. * Note that this function can be called with caller trying to map only a
  688. * subrange/page inside the vma.
  689. */
  690. int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  691. unsigned long pfn, unsigned long size)
  692. {
  693. int retval = 0;
  694. unsigned long i, j;
  695. resource_size_t base_paddr;
  696. resource_size_t paddr;
  697. unsigned long vma_start = vma->vm_start;
  698. unsigned long vma_end = vma->vm_end;
  699. unsigned long vma_size = vma_end - vma_start;
  700. if (!pat_enabled)
  701. return 0;
  702. if (is_linear_pfn_mapping(vma)) {
  703. /* reserve the whole chunk starting from vm_pgoff */
  704. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  705. return reserve_pfn_range(paddr, vma_size, prot, 0);
  706. }
  707. /* reserve page by page using pfn and size */
  708. base_paddr = (resource_size_t)pfn << PAGE_SHIFT;
  709. for (i = 0; i < size; i += PAGE_SIZE) {
  710. paddr = base_paddr + i;
  711. retval = reserve_pfn_range(paddr, PAGE_SIZE, prot, 0);
  712. if (retval)
  713. goto cleanup_ret;
  714. }
  715. return 0;
  716. cleanup_ret:
  717. /* Reserve error: Cleanup partial reservation and return error */
  718. for (j = 0; j < i; j += PAGE_SIZE) {
  719. paddr = base_paddr + j;
  720. free_pfn_range(paddr, PAGE_SIZE);
  721. }
  722. return retval;
  723. }
  724. /*
  725. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  726. * untrack can be called for a specific region indicated by pfn and size or
  727. * can be for the entire vma (in which case size can be zero).
  728. */
  729. void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  730. unsigned long size)
  731. {
  732. unsigned long i;
  733. resource_size_t paddr;
  734. unsigned long prot;
  735. unsigned long vma_start = vma->vm_start;
  736. unsigned long vma_end = vma->vm_end;
  737. unsigned long vma_size = vma_end - vma_start;
  738. if (!pat_enabled)
  739. return;
  740. if (is_linear_pfn_mapping(vma)) {
  741. /* free the whole chunk starting from vm_pgoff */
  742. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  743. free_pfn_range(paddr, vma_size);
  744. return;
  745. }
  746. if (size != 0 && size != vma_size) {
  747. /* free page by page, using pfn and size */
  748. paddr = (resource_size_t)pfn << PAGE_SHIFT;
  749. for (i = 0; i < size; i += PAGE_SIZE) {
  750. paddr = paddr + i;
  751. free_pfn_range(paddr, PAGE_SIZE);
  752. }
  753. } else {
  754. /* free entire vma, page by page, using the pfn from pte */
  755. for (i = 0; i < vma_size; i += PAGE_SIZE) {
  756. if (follow_phys(vma, vma_start + i, 0, &prot, &paddr))
  757. continue;
  758. free_pfn_range(paddr, PAGE_SIZE);
  759. }
  760. }
  761. }
  762. pgprot_t pgprot_writecombine(pgprot_t prot)
  763. {
  764. if (pat_enabled)
  765. return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
  766. else
  767. return pgprot_noncached(prot);
  768. }
  769. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  770. /* get Nth element of the linked list */
  771. static struct memtype *memtype_get_idx(loff_t pos)
  772. {
  773. struct memtype *list_node, *print_entry;
  774. int i = 1;
  775. print_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  776. if (!print_entry)
  777. return NULL;
  778. spin_lock(&memtype_lock);
  779. list_for_each_entry(list_node, &memtype_list, nd) {
  780. if (pos == i) {
  781. *print_entry = *list_node;
  782. spin_unlock(&memtype_lock);
  783. return print_entry;
  784. }
  785. ++i;
  786. }
  787. spin_unlock(&memtype_lock);
  788. kfree(print_entry);
  789. return NULL;
  790. }
  791. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  792. {
  793. if (*pos == 0) {
  794. ++*pos;
  795. seq_printf(seq, "PAT memtype list:\n");
  796. }
  797. return memtype_get_idx(*pos);
  798. }
  799. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  800. {
  801. ++*pos;
  802. return memtype_get_idx(*pos);
  803. }
  804. static void memtype_seq_stop(struct seq_file *seq, void *v)
  805. {
  806. }
  807. static int memtype_seq_show(struct seq_file *seq, void *v)
  808. {
  809. struct memtype *print_entry = (struct memtype *)v;
  810. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  811. print_entry->start, print_entry->end);
  812. kfree(print_entry);
  813. return 0;
  814. }
  815. static struct seq_operations memtype_seq_ops = {
  816. .start = memtype_seq_start,
  817. .next = memtype_seq_next,
  818. .stop = memtype_seq_stop,
  819. .show = memtype_seq_show,
  820. };
  821. static int memtype_seq_open(struct inode *inode, struct file *file)
  822. {
  823. return seq_open(file, &memtype_seq_ops);
  824. }
  825. static const struct file_operations memtype_fops = {
  826. .open = memtype_seq_open,
  827. .read = seq_read,
  828. .llseek = seq_lseek,
  829. .release = seq_release,
  830. };
  831. static int __init pat_memtype_list_init(void)
  832. {
  833. debugfs_create_file("pat_memtype_list", S_IRUSR, arch_debugfs_dir,
  834. NULL, &memtype_fops);
  835. return 0;
  836. }
  837. late_initcall(pat_memtype_list_init);
  838. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */