pat.c 25 KB

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