pat.c 24 KB

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