pat.c 25 KB

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