pat.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. * req_type will have a special case value '-1', when requester want to inherit
  306. * the memory type from mtrr (if WB), existing PAT, defaulting to UC_MINUS.
  307. *
  308. * If new_type is NULL, function will return an error if it cannot reserve the
  309. * region with req_type. If new_type is non-NULL, function will return
  310. * available type in new_type in case of no error. In case of any error
  311. * it will return a negative return value.
  312. */
  313. int reserve_memtype(u64 start, u64 end, unsigned long req_type,
  314. unsigned long *new_type)
  315. {
  316. struct memtype *new, *entry;
  317. unsigned long actual_type;
  318. struct list_head *where;
  319. int is_range_ram;
  320. int err = 0;
  321. BUG_ON(start >= end); /* end is exclusive */
  322. if (!pat_enabled) {
  323. /* This is identical to page table setting without PAT */
  324. if (new_type) {
  325. if (req_type == -1)
  326. *new_type = _PAGE_CACHE_WB;
  327. else if (req_type == _PAGE_CACHE_WC)
  328. *new_type = _PAGE_CACHE_UC_MINUS;
  329. else
  330. *new_type = req_type & _PAGE_CACHE_MASK;
  331. }
  332. return 0;
  333. }
  334. /* Low ISA region is always mapped WB in page table. No need to track */
  335. if (x86_platform.is_untracked_pat_range(start, end)) {
  336. if (new_type)
  337. *new_type = _PAGE_CACHE_WB;
  338. return 0;
  339. }
  340. /*
  341. * Call mtrr_lookup to get the type hint. This is an
  342. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  343. * tools and ACPI tools). Use WB request for WB memory and use
  344. * UC_MINUS otherwise.
  345. */
  346. actual_type = pat_x_mtrr_type(start, end, req_type & _PAGE_CACHE_MASK);
  347. if (new_type)
  348. *new_type = actual_type;
  349. is_range_ram = pat_pagerange_is_ram(start, end);
  350. if (is_range_ram == 1) {
  351. spin_lock(&memtype_lock);
  352. err = reserve_ram_pages_type(start, end, req_type, new_type);
  353. spin_unlock(&memtype_lock);
  354. return err;
  355. } else if (is_range_ram < 0) {
  356. return -EINVAL;
  357. }
  358. new = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  359. if (!new)
  360. return -ENOMEM;
  361. new->start = start;
  362. new->end = end;
  363. new->type = actual_type;
  364. spin_lock(&memtype_lock);
  365. /* Search for existing mapping that overlaps the current range */
  366. where = NULL;
  367. list_for_each_entry(entry, &memtype_list, nd) {
  368. if (end <= entry->start) {
  369. where = entry->nd.prev;
  370. break;
  371. } else if (start <= entry->start) { /* end > entry->start */
  372. err = chk_conflict(new, entry, new_type);
  373. if (!err) {
  374. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  375. entry->start, entry->end);
  376. where = entry->nd.prev;
  377. }
  378. break;
  379. } else if (start < entry->end) { /* start > entry->start */
  380. err = chk_conflict(new, entry, new_type);
  381. if (!err) {
  382. dprintk("Overlap at 0x%Lx-0x%Lx\n",
  383. entry->start, entry->end);
  384. /*
  385. * Move to right position in the linked
  386. * list to add this new entry
  387. */
  388. list_for_each_entry_continue(entry,
  389. &memtype_list, nd) {
  390. if (start <= entry->start) {
  391. where = entry->nd.prev;
  392. break;
  393. }
  394. }
  395. }
  396. break;
  397. }
  398. }
  399. if (err) {
  400. printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
  401. "track %s, req %s\n",
  402. start, end, cattr_name(new->type), cattr_name(req_type));
  403. kfree(new);
  404. spin_unlock(&memtype_lock);
  405. return err;
  406. }
  407. if (where)
  408. list_add(&new->nd, where);
  409. else
  410. list_add_tail(&new->nd, &memtype_list);
  411. memtype_rb_insert(&memtype_rbroot, new);
  412. spin_unlock(&memtype_lock);
  413. dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
  414. start, end, cattr_name(new->type), cattr_name(req_type),
  415. new_type ? cattr_name(*new_type) : "-");
  416. return err;
  417. }
  418. int free_memtype(u64 start, u64 end)
  419. {
  420. struct memtype *entry, *saved_entry;
  421. int err = -EINVAL;
  422. int is_range_ram;
  423. if (!pat_enabled)
  424. return 0;
  425. /* Low ISA region is always mapped WB. No need to track */
  426. if (x86_platform.is_untracked_pat_range(start, end))
  427. return 0;
  428. is_range_ram = pat_pagerange_is_ram(start, end);
  429. if (is_range_ram == 1) {
  430. spin_lock(&memtype_lock);
  431. err = free_ram_pages_type(start, end);
  432. spin_unlock(&memtype_lock);
  433. return err;
  434. } else if (is_range_ram < 0) {
  435. return -EINVAL;
  436. }
  437. spin_lock(&memtype_lock);
  438. entry = memtype_rb_search(&memtype_rbroot, start);
  439. if (unlikely(entry == NULL))
  440. goto unlock_ret;
  441. /*
  442. * Saved entry points to an entry with start same or less than what
  443. * we searched for. Now go through the list in both directions to look
  444. * for the entry that matches with both start and end, with list stored
  445. * in sorted start address
  446. */
  447. saved_entry = entry;
  448. list_for_each_entry_from(entry, &memtype_list, nd) {
  449. if (entry->start == start && entry->end == end) {
  450. rb_erase(&entry->rb, &memtype_rbroot);
  451. list_del(&entry->nd);
  452. kfree(entry);
  453. err = 0;
  454. break;
  455. } else if (entry->start > start) {
  456. break;
  457. }
  458. }
  459. if (!err)
  460. goto unlock_ret;
  461. entry = saved_entry;
  462. list_for_each_entry_reverse(entry, &memtype_list, nd) {
  463. if (entry->start == start && entry->end == end) {
  464. rb_erase(&entry->rb, &memtype_rbroot);
  465. list_del(&entry->nd);
  466. kfree(entry);
  467. err = 0;
  468. break;
  469. } else if (entry->start < start) {
  470. break;
  471. }
  472. }
  473. unlock_ret:
  474. spin_unlock(&memtype_lock);
  475. if (err) {
  476. printk(KERN_INFO "%s:%d freeing invalid memtype %Lx-%Lx\n",
  477. current->comm, current->pid, start, end);
  478. }
  479. dprintk("free_memtype request 0x%Lx-0x%Lx\n", start, end);
  480. return err;
  481. }
  482. /**
  483. * lookup_memtype - Looksup the memory type for a physical address
  484. * @paddr: physical address of which memory type needs to be looked up
  485. *
  486. * Only to be called when PAT is enabled
  487. *
  488. * Returns _PAGE_CACHE_WB, _PAGE_CACHE_WC, _PAGE_CACHE_UC_MINUS or
  489. * _PAGE_CACHE_UC
  490. */
  491. static unsigned long lookup_memtype(u64 paddr)
  492. {
  493. int rettype = _PAGE_CACHE_WB;
  494. struct memtype *entry;
  495. if (x86_platform.is_untracked_pat_range(paddr, paddr + PAGE_SIZE))
  496. return rettype;
  497. if (pat_pagerange_is_ram(paddr, paddr + PAGE_SIZE)) {
  498. struct page *page;
  499. spin_lock(&memtype_lock);
  500. page = pfn_to_page(paddr >> PAGE_SHIFT);
  501. rettype = get_page_memtype(page);
  502. spin_unlock(&memtype_lock);
  503. /*
  504. * -1 from get_page_memtype() implies RAM page is in its
  505. * default state and not reserved, and hence of type WB
  506. */
  507. if (rettype == -1)
  508. rettype = _PAGE_CACHE_WB;
  509. return rettype;
  510. }
  511. spin_lock(&memtype_lock);
  512. entry = memtype_rb_search(&memtype_rbroot, paddr);
  513. if (entry != NULL)
  514. rettype = entry->type;
  515. else
  516. rettype = _PAGE_CACHE_UC_MINUS;
  517. spin_unlock(&memtype_lock);
  518. return rettype;
  519. }
  520. /**
  521. * io_reserve_memtype - Request a memory type mapping for a region of memory
  522. * @start: start (physical address) of the region
  523. * @end: end (physical address) of the region
  524. * @type: A pointer to memtype, with requested type. On success, requested
  525. * or any other compatible type that was available for the region is returned
  526. *
  527. * On success, returns 0
  528. * On failure, returns non-zero
  529. */
  530. int io_reserve_memtype(resource_size_t start, resource_size_t end,
  531. unsigned long *type)
  532. {
  533. resource_size_t size = end - start;
  534. unsigned long req_type = *type;
  535. unsigned long new_type;
  536. int ret;
  537. WARN_ON_ONCE(iomem_map_sanity_check(start, size));
  538. ret = reserve_memtype(start, end, req_type, &new_type);
  539. if (ret)
  540. goto out_err;
  541. if (!is_new_memtype_allowed(start, size, req_type, new_type))
  542. goto out_free;
  543. if (kernel_map_sync_memtype(start, size, new_type) < 0)
  544. goto out_free;
  545. *type = new_type;
  546. return 0;
  547. out_free:
  548. free_memtype(start, end);
  549. ret = -EBUSY;
  550. out_err:
  551. return ret;
  552. }
  553. /**
  554. * io_free_memtype - Release a memory type mapping for a region of memory
  555. * @start: start (physical address) of the region
  556. * @end: end (physical address) of the region
  557. */
  558. void io_free_memtype(resource_size_t start, resource_size_t end)
  559. {
  560. free_memtype(start, end);
  561. }
  562. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  563. unsigned long size, pgprot_t vma_prot)
  564. {
  565. return vma_prot;
  566. }
  567. #ifdef CONFIG_STRICT_DEVMEM
  568. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM*/
  569. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  570. {
  571. return 1;
  572. }
  573. #else
  574. /* This check is needed to avoid cache aliasing when PAT is enabled */
  575. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  576. {
  577. u64 from = ((u64)pfn) << PAGE_SHIFT;
  578. u64 to = from + size;
  579. u64 cursor = from;
  580. if (!pat_enabled)
  581. return 1;
  582. while (cursor < to) {
  583. if (!devmem_is_allowed(pfn)) {
  584. printk(KERN_INFO
  585. "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  586. current->comm, from, to);
  587. return 0;
  588. }
  589. cursor += PAGE_SIZE;
  590. pfn++;
  591. }
  592. return 1;
  593. }
  594. #endif /* CONFIG_STRICT_DEVMEM */
  595. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  596. unsigned long size, pgprot_t *vma_prot)
  597. {
  598. unsigned long flags = _PAGE_CACHE_WB;
  599. if (!range_is_allowed(pfn, size))
  600. return 0;
  601. if (file->f_flags & O_SYNC) {
  602. flags = _PAGE_CACHE_UC_MINUS;
  603. }
  604. #ifdef CONFIG_X86_32
  605. /*
  606. * On the PPro and successors, the MTRRs are used to set
  607. * memory types for physical addresses outside main memory,
  608. * so blindly setting UC or PWT on those pages is wrong.
  609. * For Pentiums and earlier, the surround logic should disable
  610. * caching for the high addresses through the KEN pin, but
  611. * we maintain the tradition of paranoia in this code.
  612. */
  613. if (!pat_enabled &&
  614. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  615. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  616. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  617. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  618. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  619. flags = _PAGE_CACHE_UC;
  620. }
  621. #endif
  622. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  623. flags);
  624. return 1;
  625. }
  626. /*
  627. * Change the memory type for the physial address range in kernel identity
  628. * mapping space if that range is a part of identity map.
  629. */
  630. int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags)
  631. {
  632. unsigned long id_sz;
  633. if (base >= __pa(high_memory))
  634. return 0;
  635. id_sz = (__pa(high_memory) < base + size) ?
  636. __pa(high_memory) - base :
  637. size;
  638. if (ioremap_change_attr((unsigned long)__va(base), id_sz, flags) < 0) {
  639. printk(KERN_INFO
  640. "%s:%d ioremap_change_attr failed %s "
  641. "for %Lx-%Lx\n",
  642. current->comm, current->pid,
  643. cattr_name(flags),
  644. base, (unsigned long long)(base + size));
  645. return -EINVAL;
  646. }
  647. return 0;
  648. }
  649. /*
  650. * Internal interface to reserve a range of physical memory with prot.
  651. * Reserved non RAM regions only and after successful reserve_memtype,
  652. * this func also keeps identity mapping (if any) in sync with this new prot.
  653. */
  654. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  655. int strict_prot)
  656. {
  657. int is_ram = 0;
  658. int ret;
  659. unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
  660. unsigned long flags = want_flags;
  661. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  662. /*
  663. * reserve_pfn_range() for RAM pages. We do not refcount to keep
  664. * track of number of mappings of RAM pages. We can assert that
  665. * the type requested matches the type of first page in the range.
  666. */
  667. if (is_ram) {
  668. if (!pat_enabled)
  669. return 0;
  670. flags = lookup_memtype(paddr);
  671. if (want_flags != flags) {
  672. printk(KERN_WARNING
  673. "%s:%d map pfn RAM range req %s for %Lx-%Lx, got %s\n",
  674. current->comm, current->pid,
  675. cattr_name(want_flags),
  676. (unsigned long long)paddr,
  677. (unsigned long long)(paddr + size),
  678. cattr_name(flags));
  679. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  680. (~_PAGE_CACHE_MASK)) |
  681. flags);
  682. }
  683. return 0;
  684. }
  685. ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
  686. if (ret)
  687. return ret;
  688. if (flags != want_flags) {
  689. if (strict_prot ||
  690. !is_new_memtype_allowed(paddr, size, want_flags, flags)) {
  691. free_memtype(paddr, paddr + size);
  692. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  693. " for %Lx-%Lx, got %s\n",
  694. current->comm, current->pid,
  695. cattr_name(want_flags),
  696. (unsigned long long)paddr,
  697. (unsigned long long)(paddr + size),
  698. cattr_name(flags));
  699. return -EINVAL;
  700. }
  701. /*
  702. * We allow returning different type than the one requested in
  703. * non strict case.
  704. */
  705. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  706. (~_PAGE_CACHE_MASK)) |
  707. flags);
  708. }
  709. if (kernel_map_sync_memtype(paddr, size, flags) < 0) {
  710. free_memtype(paddr, paddr + size);
  711. return -EINVAL;
  712. }
  713. return 0;
  714. }
  715. /*
  716. * Internal interface to free a range of physical memory.
  717. * Frees non RAM regions only.
  718. */
  719. static void free_pfn_range(u64 paddr, unsigned long size)
  720. {
  721. int is_ram;
  722. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  723. if (is_ram == 0)
  724. free_memtype(paddr, paddr + size);
  725. }
  726. /*
  727. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  728. * copied through copy_page_range().
  729. *
  730. * If the vma has a linear pfn mapping for the entire range, we get the prot
  731. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  732. */
  733. int track_pfn_vma_copy(struct vm_area_struct *vma)
  734. {
  735. resource_size_t paddr;
  736. unsigned long prot;
  737. unsigned long vma_size = vma->vm_end - vma->vm_start;
  738. pgprot_t pgprot;
  739. if (is_linear_pfn_mapping(vma)) {
  740. /*
  741. * reserve the whole chunk covered by vma. We need the
  742. * starting address and protection from pte.
  743. */
  744. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  745. WARN_ON_ONCE(1);
  746. return -EINVAL;
  747. }
  748. pgprot = __pgprot(prot);
  749. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  750. }
  751. return 0;
  752. }
  753. /*
  754. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  755. * for physical range indicated by pfn and size.
  756. *
  757. * prot is passed in as a parameter for the new mapping. If the vma has a
  758. * linear pfn mapping for the entire range reserve the entire vma range with
  759. * single reserve_pfn_range call.
  760. */
  761. int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  762. unsigned long pfn, unsigned long size)
  763. {
  764. unsigned long flags;
  765. resource_size_t paddr;
  766. unsigned long vma_size = vma->vm_end - vma->vm_start;
  767. if (is_linear_pfn_mapping(vma)) {
  768. /* reserve the whole chunk starting from vm_pgoff */
  769. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  770. return reserve_pfn_range(paddr, vma_size, prot, 0);
  771. }
  772. if (!pat_enabled)
  773. return 0;
  774. /* for vm_insert_pfn and friends, we set prot based on lookup */
  775. flags = lookup_memtype(pfn << PAGE_SHIFT);
  776. *prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
  777. flags);
  778. return 0;
  779. }
  780. /*
  781. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  782. * untrack can be called for a specific region indicated by pfn and size or
  783. * can be for the entire vma (in which case size can be zero).
  784. */
  785. void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  786. unsigned long size)
  787. {
  788. resource_size_t paddr;
  789. unsigned long vma_size = vma->vm_end - vma->vm_start;
  790. if (is_linear_pfn_mapping(vma)) {
  791. /* free the whole chunk starting from vm_pgoff */
  792. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  793. free_pfn_range(paddr, vma_size);
  794. return;
  795. }
  796. }
  797. pgprot_t pgprot_writecombine(pgprot_t prot)
  798. {
  799. if (pat_enabled)
  800. return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
  801. else
  802. return pgprot_noncached(prot);
  803. }
  804. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  805. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  806. /* get Nth element of the linked list */
  807. static struct memtype *memtype_get_idx(loff_t pos)
  808. {
  809. struct memtype *list_node, *print_entry;
  810. int i = 1;
  811. print_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  812. if (!print_entry)
  813. return NULL;
  814. spin_lock(&memtype_lock);
  815. list_for_each_entry(list_node, &memtype_list, nd) {
  816. if (pos == i) {
  817. *print_entry = *list_node;
  818. spin_unlock(&memtype_lock);
  819. return print_entry;
  820. }
  821. ++i;
  822. }
  823. spin_unlock(&memtype_lock);
  824. kfree(print_entry);
  825. return NULL;
  826. }
  827. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  828. {
  829. if (*pos == 0) {
  830. ++*pos;
  831. seq_printf(seq, "PAT memtype list:\n");
  832. }
  833. return memtype_get_idx(*pos);
  834. }
  835. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  836. {
  837. ++*pos;
  838. return memtype_get_idx(*pos);
  839. }
  840. static void memtype_seq_stop(struct seq_file *seq, void *v)
  841. {
  842. }
  843. static int memtype_seq_show(struct seq_file *seq, void *v)
  844. {
  845. struct memtype *print_entry = (struct memtype *)v;
  846. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  847. print_entry->start, print_entry->end);
  848. kfree(print_entry);
  849. return 0;
  850. }
  851. static const struct seq_operations memtype_seq_ops = {
  852. .start = memtype_seq_start,
  853. .next = memtype_seq_next,
  854. .stop = memtype_seq_stop,
  855. .show = memtype_seq_show,
  856. };
  857. static int memtype_seq_open(struct inode *inode, struct file *file)
  858. {
  859. return seq_open(file, &memtype_seq_ops);
  860. }
  861. static const struct file_operations memtype_fops = {
  862. .open = memtype_seq_open,
  863. .read = seq_read,
  864. .llseek = seq_lseek,
  865. .release = seq_release,
  866. };
  867. static int __init pat_memtype_list_init(void)
  868. {
  869. if (pat_enabled) {
  870. debugfs_create_file("pat_memtype_list", S_IRUSR,
  871. arch_debugfs_dir, NULL, &memtype_fops);
  872. }
  873. return 0;
  874. }
  875. late_initcall(pat_memtype_list_init);
  876. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */