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/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_DSYNC)
  597. flags = _PAGE_CACHE_UC_MINUS;
  598. #ifdef CONFIG_X86_32
  599. /*
  600. * On the PPro and successors, the MTRRs are used to set
  601. * memory types for physical addresses outside main memory,
  602. * so blindly setting UC or PWT on those pages is wrong.
  603. * For Pentiums and earlier, the surround logic should disable
  604. * caching for the high addresses through the KEN pin, but
  605. * we maintain the tradition of paranoia in this code.
  606. */
  607. if (!pat_enabled &&
  608. !(boot_cpu_has(X86_FEATURE_MTRR) ||
  609. boot_cpu_has(X86_FEATURE_K6_MTRR) ||
  610. boot_cpu_has(X86_FEATURE_CYRIX_ARR) ||
  611. boot_cpu_has(X86_FEATURE_CENTAUR_MCR)) &&
  612. (pfn << PAGE_SHIFT) >= __pa(high_memory)) {
  613. flags = _PAGE_CACHE_UC;
  614. }
  615. #endif
  616. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  617. flags);
  618. return 1;
  619. }
  620. /*
  621. * Change the memory type for the physial address range in kernel identity
  622. * mapping space if that range is a part of identity map.
  623. */
  624. int kernel_map_sync_memtype(u64 base, unsigned long size, unsigned long flags)
  625. {
  626. unsigned long id_sz;
  627. if (base >= __pa(high_memory))
  628. return 0;
  629. id_sz = (__pa(high_memory) < base + size) ?
  630. __pa(high_memory) - base :
  631. size;
  632. if (ioremap_change_attr((unsigned long)__va(base), id_sz, flags) < 0) {
  633. printk(KERN_INFO
  634. "%s:%d ioremap_change_attr failed %s "
  635. "for %Lx-%Lx\n",
  636. current->comm, current->pid,
  637. cattr_name(flags),
  638. base, (unsigned long long)(base + size));
  639. return -EINVAL;
  640. }
  641. return 0;
  642. }
  643. /*
  644. * Internal interface to reserve a range of physical memory with prot.
  645. * Reserved non RAM regions only and after successful reserve_memtype,
  646. * this func also keeps identity mapping (if any) in sync with this new prot.
  647. */
  648. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  649. int strict_prot)
  650. {
  651. int is_ram = 0;
  652. int ret;
  653. unsigned long want_flags = (pgprot_val(*vma_prot) & _PAGE_CACHE_MASK);
  654. unsigned long flags = want_flags;
  655. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  656. /*
  657. * reserve_pfn_range() for RAM pages. We do not refcount to keep
  658. * track of number of mappings of RAM pages. We can assert that
  659. * the type requested matches the type of first page in the range.
  660. */
  661. if (is_ram) {
  662. if (!pat_enabled)
  663. return 0;
  664. flags = lookup_memtype(paddr);
  665. if (want_flags != flags) {
  666. printk(KERN_WARNING
  667. "%s:%d map pfn RAM range req %s for %Lx-%Lx, got %s\n",
  668. current->comm, current->pid,
  669. cattr_name(want_flags),
  670. (unsigned long long)paddr,
  671. (unsigned long long)(paddr + size),
  672. cattr_name(flags));
  673. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  674. (~_PAGE_CACHE_MASK)) |
  675. flags);
  676. }
  677. return 0;
  678. }
  679. ret = reserve_memtype(paddr, paddr + size, want_flags, &flags);
  680. if (ret)
  681. return ret;
  682. if (flags != want_flags) {
  683. if (strict_prot ||
  684. !is_new_memtype_allowed(paddr, size, want_flags, flags)) {
  685. free_memtype(paddr, paddr + size);
  686. printk(KERN_ERR "%s:%d map pfn expected mapping type %s"
  687. " for %Lx-%Lx, got %s\n",
  688. current->comm, current->pid,
  689. cattr_name(want_flags),
  690. (unsigned long long)paddr,
  691. (unsigned long long)(paddr + size),
  692. cattr_name(flags));
  693. return -EINVAL;
  694. }
  695. /*
  696. * We allow returning different type than the one requested in
  697. * non strict case.
  698. */
  699. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  700. (~_PAGE_CACHE_MASK)) |
  701. flags);
  702. }
  703. if (kernel_map_sync_memtype(paddr, size, flags) < 0) {
  704. free_memtype(paddr, paddr + size);
  705. return -EINVAL;
  706. }
  707. return 0;
  708. }
  709. /*
  710. * Internal interface to free a range of physical memory.
  711. * Frees non RAM regions only.
  712. */
  713. static void free_pfn_range(u64 paddr, unsigned long size)
  714. {
  715. int is_ram;
  716. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  717. if (is_ram == 0)
  718. free_memtype(paddr, paddr + size);
  719. }
  720. /*
  721. * track_pfn_vma_copy is called when vma that is covering the pfnmap gets
  722. * copied through copy_page_range().
  723. *
  724. * If the vma has a linear pfn mapping for the entire range, we get the prot
  725. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  726. */
  727. int track_pfn_vma_copy(struct vm_area_struct *vma)
  728. {
  729. resource_size_t paddr;
  730. unsigned long prot;
  731. unsigned long vma_size = vma->vm_end - vma->vm_start;
  732. pgprot_t pgprot;
  733. if (is_linear_pfn_mapping(vma)) {
  734. /*
  735. * reserve the whole chunk covered by vma. We need the
  736. * starting address and protection from pte.
  737. */
  738. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  739. WARN_ON_ONCE(1);
  740. return -EINVAL;
  741. }
  742. pgprot = __pgprot(prot);
  743. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  744. }
  745. return 0;
  746. }
  747. /*
  748. * track_pfn_vma_new is called when a _new_ pfn mapping is being established
  749. * for physical range indicated by pfn and size.
  750. *
  751. * prot is passed in as a parameter for the new mapping. If the vma has a
  752. * linear pfn mapping for the entire range reserve the entire vma range with
  753. * single reserve_pfn_range call.
  754. */
  755. int track_pfn_vma_new(struct vm_area_struct *vma, pgprot_t *prot,
  756. unsigned long pfn, unsigned long size)
  757. {
  758. unsigned long flags;
  759. resource_size_t paddr;
  760. unsigned long vma_size = vma->vm_end - vma->vm_start;
  761. if (is_linear_pfn_mapping(vma)) {
  762. /* reserve the whole chunk starting from vm_pgoff */
  763. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  764. return reserve_pfn_range(paddr, vma_size, prot, 0);
  765. }
  766. if (!pat_enabled)
  767. return 0;
  768. /* for vm_insert_pfn and friends, we set prot based on lookup */
  769. flags = lookup_memtype(pfn << PAGE_SHIFT);
  770. *prot = __pgprot((pgprot_val(vma->vm_page_prot) & (~_PAGE_CACHE_MASK)) |
  771. flags);
  772. return 0;
  773. }
  774. /*
  775. * untrack_pfn_vma is called while unmapping a pfnmap for a region.
  776. * untrack can be called for a specific region indicated by pfn and size or
  777. * can be for the entire vma (in which case size can be zero).
  778. */
  779. void untrack_pfn_vma(struct vm_area_struct *vma, unsigned long pfn,
  780. unsigned long size)
  781. {
  782. resource_size_t paddr;
  783. unsigned long vma_size = vma->vm_end - vma->vm_start;
  784. if (is_linear_pfn_mapping(vma)) {
  785. /* free the whole chunk starting from vm_pgoff */
  786. paddr = (resource_size_t)vma->vm_pgoff << PAGE_SHIFT;
  787. free_pfn_range(paddr, vma_size);
  788. return;
  789. }
  790. }
  791. pgprot_t pgprot_writecombine(pgprot_t prot)
  792. {
  793. if (pat_enabled)
  794. return __pgprot(pgprot_val(prot) | _PAGE_CACHE_WC);
  795. else
  796. return pgprot_noncached(prot);
  797. }
  798. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  799. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  800. /* get Nth element of the linked list */
  801. static struct memtype *memtype_get_idx(loff_t pos)
  802. {
  803. struct memtype *list_node, *print_entry;
  804. int i = 1;
  805. print_entry = kmalloc(sizeof(struct memtype), GFP_KERNEL);
  806. if (!print_entry)
  807. return NULL;
  808. spin_lock(&memtype_lock);
  809. list_for_each_entry(list_node, &memtype_list, nd) {
  810. if (pos == i) {
  811. *print_entry = *list_node;
  812. spin_unlock(&memtype_lock);
  813. return print_entry;
  814. }
  815. ++i;
  816. }
  817. spin_unlock(&memtype_lock);
  818. kfree(print_entry);
  819. return NULL;
  820. }
  821. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  822. {
  823. if (*pos == 0) {
  824. ++*pos;
  825. seq_printf(seq, "PAT memtype list:\n");
  826. }
  827. return memtype_get_idx(*pos);
  828. }
  829. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  830. {
  831. ++*pos;
  832. return memtype_get_idx(*pos);
  833. }
  834. static void memtype_seq_stop(struct seq_file *seq, void *v)
  835. {
  836. }
  837. static int memtype_seq_show(struct seq_file *seq, void *v)
  838. {
  839. struct memtype *print_entry = (struct memtype *)v;
  840. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  841. print_entry->start, print_entry->end);
  842. kfree(print_entry);
  843. return 0;
  844. }
  845. static const struct seq_operations memtype_seq_ops = {
  846. .start = memtype_seq_start,
  847. .next = memtype_seq_next,
  848. .stop = memtype_seq_stop,
  849. .show = memtype_seq_show,
  850. };
  851. static int memtype_seq_open(struct inode *inode, struct file *file)
  852. {
  853. return seq_open(file, &memtype_seq_ops);
  854. }
  855. static const struct file_operations memtype_fops = {
  856. .open = memtype_seq_open,
  857. .read = seq_read,
  858. .llseek = seq_lseek,
  859. .release = seq_release,
  860. };
  861. static int __init pat_memtype_list_init(void)
  862. {
  863. if (pat_enabled) {
  864. debugfs_create_file("pat_memtype_list", S_IRUSR,
  865. arch_debugfs_dir, NULL, &memtype_fops);
  866. }
  867. return 0;
  868. }
  869. late_initcall(pat_memtype_list_init);
  870. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */