pat.c 24 KB

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