pat.c 25 KB

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