pat.c 24 KB

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