pat.c 25 KB

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