pat.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. /*
  192. * For RAM pages, mark the pages as non WB memory type using
  193. * PageNonWB (PG_arch_1). We allow only one set_memory_uc() or
  194. * set_memory_wc() on a RAM page at a time before marking it as WB again.
  195. * This is ok, because only one driver will be owning the page and
  196. * doing set_memory_*() calls.
  197. *
  198. * For now, we use PageNonWB to track that the RAM page is being mapped
  199. * as non WB. In future, we will have to use one more flag
  200. * (or some other mechanism in page_struct) to distinguish between
  201. * UC and WC mapping.
  202. */
  203. static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type,
  204. unsigned long *new_type)
  205. {
  206. struct page *page;
  207. u64 pfn, end_pfn;
  208. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  209. page = pfn_to_page(pfn);
  210. if (page_mapped(page) || PageNonWB(page))
  211. goto out;
  212. SetPageNonWB(page);
  213. }
  214. return 0;
  215. out:
  216. end_pfn = pfn;
  217. for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
  218. page = pfn_to_page(pfn);
  219. ClearPageNonWB(page);
  220. }
  221. return -EINVAL;
  222. }
  223. static int free_ram_pages_type(u64 start, u64 end)
  224. {
  225. struct page *page;
  226. u64 pfn, end_pfn;
  227. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  228. page = pfn_to_page(pfn);
  229. if (page_mapped(page) || !PageNonWB(page))
  230. goto out;
  231. ClearPageNonWB(page);
  232. }
  233. return 0;
  234. out:
  235. end_pfn = pfn;
  236. for (pfn = (start >> PAGE_SHIFT); pfn < end_pfn; ++pfn) {
  237. page = pfn_to_page(pfn);
  238. SetPageNonWB(page);
  239. }
  240. return -EINVAL;
  241. }
  242. /*
  243. * req_type typically has one of the:
  244. * - _PAGE_CACHE_WB
  245. * - _PAGE_CACHE_WC
  246. * - _PAGE_CACHE_UC_MINUS
  247. * - _PAGE_CACHE_UC
  248. *
  249. * req_type will have a special case value '-1', when requester want to inherit
  250. * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
  251. *
  252. * If new_type is NULL, function will return an error if it cannot reserve the
  253. * region with req_type. If new_type is non-NULL, function will return
  254. * available type in new_type in case of no error. In case of any error
  255. * it will return a negative return value.
  256. */
  257. int reserve_memtype(u64 start, u64 end, unsigned long req_type,
  258. unsigned long *new_type)
  259. {
  260. struct memtype *new, *entry;
  261. unsigned long actual_type;
  262. struct list_head *where;
  263. int is_range_ram;
  264. int err = 0;
  265. BUG_ON(start >= end); /* end is exclusive */
  266. if (!pat_enabled) {
  267. /* This is identical to page table setting without PAT */
  268. if (new_type) {
  269. if (req_type == -1)
  270. *new_type = _PAGE_CACHE_WB;
  271. else
  272. *new_type = req_type & _PAGE_CACHE_MASK;
  273. }
  274. return 0;
  275. }
  276. /* Low ISA region is always mapped WB in page table. No need to track */
  277. if (is_ISA_range(start, end - 1)) {
  278. if (new_type)
  279. *new_type = _PAGE_CACHE_WB;
  280. return 0;
  281. }
  282. if (req_type == -1) {
  283. /*
  284. * Call mtrr_lookup to get the type hint. This is an
  285. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  286. * tools and ACPI tools). Use WB request for WB memory and use
  287. * UC_MINUS otherwise.
  288. */
  289. u8 mtrr_type = mtrr_type_lookup(start, end);
  290. if (mtrr_type == MTRR_TYPE_WRBACK)
  291. actual_type = _PAGE_CACHE_WB;
  292. else
  293. actual_type = _PAGE_CACHE_UC_MINUS;
  294. } else {
  295. actual_type = pat_x_mtrr_type(start, end,
  296. req_type & _PAGE_CACHE_MASK);
  297. }
  298. is_range_ram = pagerange_is_ram(start, end);
  299. if (is_range_ram == 1)
  300. return reserve_ram_pages_type(start, end, req_type, new_type);
  301. else if (is_range_ram < 0)
  302. return -EINVAL;
  303. new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  304. if (!new)
  305. return -ENOMEM;
  306. new->start = start;
  307. new->end = end;
  308. new->type = actual_type;
  309. if (new_type)
  310. *new_type = actual_type;
  311. spin_lock(&memtype_lock);
  312. if (cached_entry && start >= cached_start)
  313. entry = cached_entry;
  314. else
  315. entry = list_entry(&memtype_list, struct memtype, nd);
  316. /* Search for existing mapping that overlaps the current range */
  317. where = NULL;
  318. list_for_each_entry_continue(entry, &memtype_list, nd) {
  319. if (end <= entry->start) {
  320. where = entry->nd.prev;
  321. cached_entry = list_entry(where, struct memtype, nd);
  322. break;
  323. } else if (start <= entry->start) { /* end > entry->start */
  324. err = chk_conflict(new, entry, new_type);
  325. if (!err) {
  326. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  327. entry->start, entry->end);
  328. where = entry->nd.prev;
  329. cached_entry = list_entry(where,
  330. struct memtype, nd);
  331. }
  332. break;
  333. } else if (start < entry->end) { /* start > entry->start */
  334. err = chk_conflict(new, entry, new_type);
  335. if (!err) {
  336. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  337. entry->start, entry->end);
  338. cached_entry = list_entry(entry->nd.prev,
  339. struct memtype, nd);
  340. /*
  341. * Move to right position in the linked
  342. * list to add this new entry
  343. */
  344. list_for_each_entry_continue(entry,
  345. &memtype_list, nd) {
  346. if (start <= entry->start) {
  347. where = entry->nd.prev;
  348. break;
  349. }
  350. }
  351. }
  352. break;
  353. }
  354. }
  355. if (err) {
  356. printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
  357. "track %s, req %s\n",
  358. start, end, cattr_name(new->type), cattr_name(req_type));
  359. kfree(new);
  360. spin_unlock(&memtype_lock);
  361. return err;
  362. }
  363. cached_start = start;
  364. if (where)
  365. list_add(&new->nd, where);
  366. else
  367. list_add_tail(&new->nd, &memtype_list);
  368. spin_unlock(&memtype_lock);
  369. dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
  370. start, end, cattr_name(new->type), cattr_name(req_type),
  371. new_type ? cattr_name(*new_type) : "-");
  372. return err;
  373. }
  374. int free_memtype(u64 start, u64 end)
  375. {
  376. struct memtype *entry;
  377. int err = -EINVAL;
  378. int is_range_ram;
  379. if (!pat_enabled)
  380. return 0;
  381. /* Low ISA region is always mapped WB. No need to track */
  382. if (is_ISA_range(start, end - 1))
  383. return 0;
  384. is_range_ram = pagerange_is_ram(start, end);
  385. if (is_range_ram == 1)
  386. return free_ram_pages_type(start, end);
  387. else if (is_range_ram < 0)
  388. return -EINVAL;
  389. spin_lock(&memtype_lock);
  390. list_for_each_entry(entry, &memtype_list, nd) {
  391. if (entry->start == start && entry->end == end) {
  392. if (cached_entry == entry || cached_start == start)
  393. cached_entry = NULL;
  394. list_del(&entry->nd);
  395. kfree(entry);
  396. err = 0;
  397. break;
  398. }
  399. }
  400. spin_unlock(&memtype_lock);
  401. if (err) {
  402. printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
  403. current->comm, current->pid, start, end);
  404. }
  405. dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
  406. return err;
  407. }
  408. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  409. unsigned long size, pgprot_t vma_prot)
  410. {
  411. return vma_prot;
  412. }
  413. #ifdef CONFIG_STRICT_DEVMEM
  414. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
  415. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  416. {
  417. return 1;
  418. }
  419. #else
  420. /* This check is needed to avoid cache aliasing when PAT is enabled */
  421. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  422. {
  423. u64 from = ((u64)pfn) << PAGE_SHIFT;
  424. u64 to = from + size;
  425. u64 cursor = from;
  426. if (!pat_enabled)
  427. return 1;
  428. while (cursor < to) {
  429. if (!devmem_is_allowed(pfn)) {
  430. printk(KERN_INFO
  431. "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  432. current->comm, from, to);
  433. return 0;
  434. }
  435. cursor += PAGE_SIZE;
  436. pfn++;
  437. }
  438. return 1;
  439. }
  440. #endif /* CONFIG_STRICT_DEVMEM */
  441. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  442. unsigned long size, pgprot_t *vma_prot)
  443. {
  444. u64 offset = ((u64) pfn) << PAGE_SHIFT;
  445. unsigned long flags = -1;
  446. int retval;
  447. if (!range_is_allowed(pfn, size))
  448. return 0;
  449. if (file->f_flags & O_SYNC) {
  450. flags = _PAGE_CACHE_UC_MINUS;
  451. }
  452. #ifdef CONFIG_X86_32
  453. /*
  454. * On the PPro and successors, the MTRRs are used to set
  455. * memory types for physical addresses outside main memory,
  456. * so blindly setting UC or PWT on those pages is wrong.
  457. * For Pentiums and earlier, the surround logic should disable
  458. * caching for the high addresses through the KEN pin, but
  459. * we maintain the tradition of paranoia in this code.
  460. */
  461. if (!pat_enabled &&
  462. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  463. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  464. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  465. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  466. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  467. flags = _PAGE_CACHE_UC;
  468. }
  469. #endif
  470. /*
  471. * With O_SYNC, we can only take UC_MINUS mapping. Fail if we cannot.
  472. *
  473. * Without O_SYNC, we want to get
  474. * - WB for WB-able memory and no other conflicting mappings
  475. * - UC_MINUS for non-WB-able memory with no other conflicting mappings
  476. * - Inherit from confliting mappings otherwise
  477. */
  478. if (flags != -1) {
  479. retval = reserve_memtype(offset, offset + size, flags, NULL);
  480. } else {
  481. retval = reserve_memtype(offset, offset + size, -1, &flags);
  482. }
  483. if (retval < 0)
  484. return 0;
  485. if (((pfn < max_low_pfn_mapped) ||
  486. (pfn >= (1UL<<(32 - PAGE_SHIFT)) && pfn < max_pfn_mapped)) &&
  487. ioremap_change_attr((unsigned long)__va(offset), size, flags) < 0) {
  488. free_memtype(offset, offset + size);
  489. printk(KERN_INFO
  490. "%s:%d /dev/mem ioremap_change_attr failed %s for %Lx-%Lx\n",
  491. current->comm, current->pid,
  492. cattr_name(flags),
  493. offset, (unsigned long long)(offset + size));
  494. return 0;
  495. }
  496. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  497. flags);
  498. return 1;
  499. }
  500. void map_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
  501. {
  502. unsigned long want_flags = (pgprot_val(vma_prot) & _PAGE_CACHE_MASK);
  503. u64 addr = (u64)pfn << PAGE_SHIFT;
  504. unsigned long flags;
  505. reserve_memtype(addr, addr + size, want_flags, &flags);
  506. if (flags != want_flags) {
  507. printk(KERN_INFO
  508. "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n",
  509. current->comm, current->pid,
  510. cattr_name(want_flags),
  511. addr, (unsigned long long)(addr + size),
  512. cattr_name(flags));
  513. }
  514. }
  515. void unmap_devmem(unsigned long pfn, unsigned long size, pgprot_t vma_prot)
  516. {
  517. u64 addr = (u64)pfn << PAGE_SHIFT;
  518. free_memtype(addr, addr + size);
  519. }
  520. /*
  521. * Internal interface to reserve a range of physical memory with prot.
  522. * Reserved non RAM regions only and after successful reserve_memtype,
  523. * this func also keeps identity mapping (if any) in sync with this new prot.
  524. */
  525. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  526. int strict_prot)
  527. {
  528. int is_ram = 0;
  529. int id_sz, ret;
  530. unsigned long flags;
  531. unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
  532. is_ram = pagerange_is_ram(paddr, paddr + size);
  533. if (is_ram != 0) {
  534. /*
  535. * For mapping RAM pages, drivers need to call
  536. * set_memory_[uc|wc|wb] directly, for reserve and free, before
  537. * setting up the PTE.
  538. */
  539. WARN_ON_ONCE(1);
  540. return 0;
  541. }
  542. ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
  543. if (ret)
  544. return ret;
  545. if (flags != want_flags) {
  546. if (strict_prot || !is_new_memtype_allowed(want_flags, flags)) {
  547. free_memtype(paddr, paddr + size);
  548. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  549. " for %Lx-%Lx, got %s\n",
  550. current->comm, current->pid,
  551. cattr_name(want_flags),
  552. (unsigned long long)paddr,
  553. (unsigned long long)(paddr + size),
  554. cattr_name(flags));
  555. return -EINVAL;
  556. }
  557. /*
  558. * We allow returning different type than the one requested in
  559. * non strict case.
  560. */
  561. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  562. (~_PAGE_CACHE_MASK)) |
  563. flags);
  564. }
  565. /* Need to keep identity mapping in sync */
  566. if (paddr >= __pa(high_memory))
  567. return 0;
  568. id_sz = (__pa(high_memory) < paddr + size) ?
  569. __pa(high_memory) - paddr :
  570. size;
  571. if (ioremap_change_attr((unsigned long)__va(paddr), id_sz, flags) < 0) {
  572. free_memtype(paddr, paddr + size);
  573. printk(KERN_ERR
  574. "%s:%d reserve_pfn_range ioremap_change_attr failed %s "
  575. "for %Lx-%Lx\n",
  576. current->comm, current->pid,
  577. cattr_name(flags),
  578. (unsigned long long)paddr,
  579. (unsigned long long)(paddr + size));
  580. return -EINVAL;
  581. }
  582. return 0;
  583. }
  584. /*
  585. * Internal interface to free a range of physical memory.
  586. * Frees non RAM regions only.
  587. */
  588. static void free_pfn_range(u64 paddr, unsigned long size)
  589. {
  590. int is_ram;
  591. is_ram = pagerange_is_ram(paddr, paddr + size);
  592. if (is_ram == 0)
  593. free_memtype(paddr, paddr + size);
  594. }
  595. /*
  596. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  597. * copied through copy_page_range().
  598. *
  599. * If the vma has a linear pfn mapping for the entire range, we get the prot
  600. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  601. * Otherwise, we reserve the entire vma range, my ging through the PTEs page
  602. * by page to get physical address and protection.
  603. */
  604. int track_pfn_vma_copy(struct vm_area_struct *vma)
  605. {
  606. int retval = 0;
  607. unsigned long i, j;
  608. resource_size_t paddr;
  609. unsigned long prot;
  610. unsigned long vma_start = vma->vm_start;
  611. unsigned long vma_end = vma->vm_end;
  612. unsigned long vma_size = vma_end - vma_start;
  613. pgprot_t pgprot;
  614. if (!pat_enabled)
  615. return 0;
  616. if (is_linear_pfn_mapping(vma)) {
  617. /*
  618. * reserve the whole chunk covered by vma. We need the
  619. * starting address and protection from pte.
  620. */
  621. if (follow_phys(vma, vma_start, 0, &prot, &paddr)) {
  622. WARN_ON_ONCE(1);
  623. return -EINVAL;
  624. }
  625. pgprot = __pgprot(prot);
  626. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  627. }
  628. /* reserve entire vma page by page, using pfn and prot from pte */
  629. for (i = 0; i < vma_size; i += PAGE_SIZE) {
  630. if (follow_phys(vma, vma_start + i, 0, &prot, &paddr))
  631. continue;
  632. pgprot = __pgprot(prot);
  633. retval = reserve_pfn_range(paddr, PAGE_SIZE, &pgprot, 1);
  634. if (retval)
  635. goto cleanup_ret;
  636. }
  637. return 0;
  638. cleanup_ret:
  639. /* Reserve error: Cleanup partial reservation and return error */
  640. for (j = 0; j < i; j += PAGE_SIZE) {
  641. if (follow_phys(vma, vma_start + j, 0, &prot, &paddr))
  642. continue;
  643. free_pfn_range(paddr, PAGE_SIZE);
  644. }
  645. return retval;
  646. }
  647. /*
  648. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  649. * for physical range indicated by pfn and size.
  650. *
  651. * prot is passed in as a parameter for the new mapping. If the vma has a
  652. * linear pfn mapping for the entire range reserve the entire vma range with
  653. * single reserve_pfn_range call.
  654. * Otherwise, we look t the pfn and size and reserve only the specified range
  655. * page by page.
  656. *
  657. * Note that this function can be called with caller trying to map only a
  658. * subrange/page inside the vma.
  659. */
  660. int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  661. unsigned long pfn, unsigned long size)
  662. {
  663. int retval = 0;
  664. unsigned long i, j;
  665. resource_size_t base_paddr;
  666. resource_size_t paddr;
  667. unsigned long vma_start = vma->vm_start;
  668. unsigned long vma_end = vma->vm_end;
  669. unsigned long vma_size = vma_end - vma_start;
  670. if (!pat_enabled)
  671. return 0;
  672. if (is_linear_pfn_mapping(vma)) {
  673. /* reserve the whole chunk starting from vm_pgoff */
  674. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  675. return reserve_pfn_range(paddr, vma_size, prot, 0);
  676. }
  677. /* reserve page by page using pfn and size */
  678. base_paddr = (resource_size_t)pfn << PAGE_SHIFT;
  679. for (i = 0; i < size; i += PAGE_SIZE) {
  680. paddr = base_paddr + i;
  681. retval = reserve_pfn_range(paddr, PAGE_SIZE, prot, 0);
  682. if (retval)
  683. goto cleanup_ret;
  684. }
  685. return 0;
  686. cleanup_ret:
  687. /* Reserve error: Cleanup partial reservation and return error */
  688. for (j = 0; j < i; j += PAGE_SIZE) {
  689. paddr = base_paddr + j;
  690. free_pfn_range(paddr, PAGE_SIZE);
  691. }
  692. return retval;
  693. }
  694. /*
  695. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  696. * untrack can be called for a specific region indicated by pfn and size or
  697. * can be for the entire vma (in which case size can be zero).
  698. */
  699. void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  700. unsigned long size)
  701. {
  702. unsigned long i;
  703. resource_size_t paddr;
  704. unsigned long prot;
  705. unsigned long vma_start = vma->vm_start;
  706. unsigned long vma_end = vma->vm_end;
  707. unsigned long vma_size = vma_end - vma_start;
  708. if (!pat_enabled)
  709. return;
  710. if (is_linear_pfn_mapping(vma)) {
  711. /* free the whole chunk starting from vm_pgoff */
  712. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  713. free_pfn_range(paddr, vma_size);
  714. return;
  715. }
  716. if (size != 0 && size != vma_size) {
  717. /* free page by page, using pfn and size */
  718. paddr = (resource_size_t)pfn << PAGE_SHIFT;
  719. for (i = 0; i < size; i += PAGE_SIZE) {
  720. paddr = paddr + i;
  721. free_pfn_range(paddr, PAGE_SIZE);
  722. }
  723. } else {
  724. /* free entire vma, page by page, using the pfn from pte */
  725. for (i = 0; i < vma_size; i += PAGE_SIZE) {
  726. if (follow_phys(vma, vma_start + i, 0, &prot, &paddr))
  727. continue;
  728. free_pfn_range(paddr, PAGE_SIZE);
  729. }
  730. }
  731. }
  732. pgprot_t pgprot_writecombine(pgprot_t prot)
  733. {
  734. if (pat_enabled)
  735. return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
  736. else
  737. return pgprot_noncached(prot);
  738. }
  739. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  740. /* get Nth element of the linked list */
  741. static struct memtype *memtype_get_idx(loff_t pos)
  742. {
  743. struct memtype *list_node, *print_entry;
  744. int i = 1;
  745. print_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  746. if (!print_entry)
  747. return NULL;
  748. spin_lock(&memtype_lock);
  749. list_for_each_entry(list_node, &memtype_list, nd) {
  750. if (pos == i) {
  751. *print_entry = *list_node;
  752. spin_unlock(&memtype_lock);
  753. return print_entry;
  754. }
  755. ++i;
  756. }
  757. spin_unlock(&memtype_lock);
  758. kfree(print_entry);
  759. return NULL;
  760. }
  761. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  762. {
  763. if (*pos == 0) {
  764. ++*pos;
  765. seq_printf(seq, "PAT memtype list:\n");
  766. }
  767. return memtype_get_idx(*pos);
  768. }
  769. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  770. {
  771. ++*pos;
  772. return memtype_get_idx(*pos);
  773. }
  774. static void memtype_seq_stop(struct seq_file *seq, void *v)
  775. {
  776. }
  777. static int memtype_seq_show(struct seq_file *seq, void *v)
  778. {
  779. struct memtype *print_entry = (struct memtype *)v;
  780. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  781. print_entry->start, print_entry->end);
  782. kfree(print_entry);
  783. return 0;
  784. }
  785. static struct seq_operations memtype_seq_ops = {
  786. .start = memtype_seq_start,
  787. .next = memtype_seq_next,
  788. .stop = memtype_seq_stop,
  789. .show = memtype_seq_show,
  790. };
  791. static int memtype_seq_open(struct inode *inode, struct file *file)
  792. {
  793. return seq_open(file, &memtype_seq_ops);
  794. }
  795. static const struct file_operations memtype_fops = {
  796. .open = memtype_seq_open,
  797. .read = seq_read,
  798. .llseek = seq_lseek,
  799. .release = seq_release,
  800. };
  801. static int __init pat_memtype_list_init(void)
  802. {
  803. debugfs_create_file("pat_memtype_list", S_IRUSR, arch_debugfs_dir,
  804. NULL, &memtype_fops);
  805. return 0;
  806. }
  807. late_initcall(pat_memtype_list_init);
  808. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */