pat.c 25 KB

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