pat.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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/pfn_t.h>
  14. #include <linux/slab.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. #include "pat_internal.h"
  31. #include "mm_internal.h"
  32. #undef pr_fmt
  33. #define pr_fmt(fmt) "" fmt
  34. static bool __read_mostly boot_cpu_done;
  35. static bool __read_mostly pat_disabled = !IS_ENABLED(CONFIG_X86_PAT);
  36. static bool __read_mostly pat_initialized;
  37. static bool __read_mostly init_cm_done;
  38. void pat_disable(const char *reason)
  39. {
  40. if (pat_disabled)
  41. return;
  42. if (boot_cpu_done) {
  43. WARN_ONCE(1, "x86/PAT: PAT cannot be disabled after initialization\n");
  44. return;
  45. }
  46. pat_disabled = true;
  47. pr_info("x86/PAT: %s\n", reason);
  48. }
  49. static int __init nopat(char *str)
  50. {
  51. pat_disable("PAT support disabled.");
  52. return 0;
  53. }
  54. early_param("nopat", nopat);
  55. bool pat_enabled(void)
  56. {
  57. return pat_initialized;
  58. }
  59. EXPORT_SYMBOL_GPL(pat_enabled);
  60. int pat_debug_enable;
  61. static int __init pat_debug_setup(char *str)
  62. {
  63. pat_debug_enable = 1;
  64. return 0;
  65. }
  66. __setup("debugpat", pat_debug_setup);
  67. #ifdef CONFIG_X86_PAT
  68. /*
  69. * X86 PAT uses page flags arch_1 and uncached together to keep track of
  70. * memory type of pages that have backing page struct.
  71. *
  72. * X86 PAT supports 4 different memory types:
  73. * - _PAGE_CACHE_MODE_WB
  74. * - _PAGE_CACHE_MODE_WC
  75. * - _PAGE_CACHE_MODE_UC_MINUS
  76. * - _PAGE_CACHE_MODE_WT
  77. *
  78. * _PAGE_CACHE_MODE_WB is the default type.
  79. */
  80. #define _PGMT_WB 0
  81. #define _PGMT_WC (1UL << PG_arch_1)
  82. #define _PGMT_UC_MINUS (1UL << PG_uncached)
  83. #define _PGMT_WT (1UL << PG_uncached | 1UL << PG_arch_1)
  84. #define _PGMT_MASK (1UL << PG_uncached | 1UL << PG_arch_1)
  85. #define _PGMT_CLEAR_MASK (~_PGMT_MASK)
  86. static inline enum page_cache_mode get_page_memtype(struct page *pg)
  87. {
  88. unsigned long pg_flags = pg->flags & _PGMT_MASK;
  89. if (pg_flags == _PGMT_WB)
  90. return _PAGE_CACHE_MODE_WB;
  91. else if (pg_flags == _PGMT_WC)
  92. return _PAGE_CACHE_MODE_WC;
  93. else if (pg_flags == _PGMT_UC_MINUS)
  94. return _PAGE_CACHE_MODE_UC_MINUS;
  95. else
  96. return _PAGE_CACHE_MODE_WT;
  97. }
  98. static inline void set_page_memtype(struct page *pg,
  99. enum page_cache_mode memtype)
  100. {
  101. unsigned long memtype_flags;
  102. unsigned long old_flags;
  103. unsigned long new_flags;
  104. switch (memtype) {
  105. case _PAGE_CACHE_MODE_WC:
  106. memtype_flags = _PGMT_WC;
  107. break;
  108. case _PAGE_CACHE_MODE_UC_MINUS:
  109. memtype_flags = _PGMT_UC_MINUS;
  110. break;
  111. case _PAGE_CACHE_MODE_WT:
  112. memtype_flags = _PGMT_WT;
  113. break;
  114. case _PAGE_CACHE_MODE_WB:
  115. default:
  116. memtype_flags = _PGMT_WB;
  117. break;
  118. }
  119. do {
  120. old_flags = pg->flags;
  121. new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags;
  122. } while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags);
  123. }
  124. #else
  125. static inline enum page_cache_mode get_page_memtype(struct page *pg)
  126. {
  127. return -1;
  128. }
  129. static inline void set_page_memtype(struct page *pg,
  130. enum page_cache_mode memtype)
  131. {
  132. }
  133. #endif
  134. enum {
  135. PAT_UC = 0, /* uncached */
  136. PAT_WC = 1, /* Write combining */
  137. PAT_WT = 4, /* Write Through */
  138. PAT_WP = 5, /* Write Protected */
  139. PAT_WB = 6, /* Write Back (default) */
  140. PAT_UC_MINUS = 7, /* UC, but can be overridden by MTRR */
  141. };
  142. #define CM(c) (_PAGE_CACHE_MODE_ ## c)
  143. static enum page_cache_mode pat_get_cache_mode(unsigned pat_val, char *msg)
  144. {
  145. enum page_cache_mode cache;
  146. char *cache_mode;
  147. switch (pat_val) {
  148. case PAT_UC: cache = CM(UC); cache_mode = "UC "; break;
  149. case PAT_WC: cache = CM(WC); cache_mode = "WC "; break;
  150. case PAT_WT: cache = CM(WT); cache_mode = "WT "; break;
  151. case PAT_WP: cache = CM(WP); cache_mode = "WP "; break;
  152. case PAT_WB: cache = CM(WB); cache_mode = "WB "; break;
  153. case PAT_UC_MINUS: cache = CM(UC_MINUS); cache_mode = "UC- "; break;
  154. default: cache = CM(WB); cache_mode = "WB "; break;
  155. }
  156. memcpy(msg, cache_mode, 4);
  157. return cache;
  158. }
  159. #undef CM
  160. /*
  161. * Update the cache mode to pgprot translation tables according to PAT
  162. * configuration.
  163. * Using lower indices is preferred, so we start with highest index.
  164. */
  165. static void __init_cache_modes(u64 pat)
  166. {
  167. enum page_cache_mode cache;
  168. char pat_msg[33];
  169. int i;
  170. pat_msg[32] = 0;
  171. for (i = 7; i >= 0; i--) {
  172. cache = pat_get_cache_mode((pat >> (i * 8)) & 7,
  173. pat_msg + 4 * i);
  174. update_cache_mode_entry(i, cache);
  175. }
  176. pr_info("x86/PAT: Configuration [0-7]: %s\n", pat_msg);
  177. init_cm_done = true;
  178. }
  179. #define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
  180. static void pat_bsp_init(u64 pat)
  181. {
  182. u64 tmp_pat;
  183. if (!boot_cpu_has(X86_FEATURE_PAT)) {
  184. pat_disable("PAT not supported by CPU.");
  185. return;
  186. }
  187. rdmsrl(MSR_IA32_CR_PAT, tmp_pat);
  188. if (!tmp_pat) {
  189. pat_disable("PAT MSR is 0, disabled.");
  190. return;
  191. }
  192. wrmsrl(MSR_IA32_CR_PAT, pat);
  193. pat_initialized = true;
  194. __init_cache_modes(pat);
  195. }
  196. static void pat_ap_init(u64 pat)
  197. {
  198. if (!boot_cpu_has(X86_FEATURE_PAT)) {
  199. /*
  200. * If this happens we are on a secondary CPU, but switched to
  201. * PAT on the boot CPU. We have no way to undo PAT.
  202. */
  203. panic("x86/PAT: PAT enabled, but not supported by secondary CPU\n");
  204. }
  205. wrmsrl(MSR_IA32_CR_PAT, pat);
  206. }
  207. void init_cache_modes(void)
  208. {
  209. u64 pat = 0;
  210. if (init_cm_done)
  211. return;
  212. if (boot_cpu_has(X86_FEATURE_PAT)) {
  213. /*
  214. * CPU supports PAT. Set PAT table to be consistent with
  215. * PAT MSR. This case supports "nopat" boot option, and
  216. * virtual machine environments which support PAT without
  217. * MTRRs. In specific, Xen has unique setup to PAT MSR.
  218. *
  219. * If PAT MSR returns 0, it is considered invalid and emulates
  220. * as No PAT.
  221. */
  222. rdmsrl(MSR_IA32_CR_PAT, pat);
  223. }
  224. if (!pat) {
  225. /*
  226. * No PAT. Emulate the PAT table that corresponds to the two
  227. * cache bits, PWT (Write Through) and PCD (Cache Disable).
  228. * This setup is also the same as the BIOS default setup.
  229. *
  230. * PTE encoding:
  231. *
  232. * PCD
  233. * |PWT PAT
  234. * || slot
  235. * 00 0 WB : _PAGE_CACHE_MODE_WB
  236. * 01 1 WT : _PAGE_CACHE_MODE_WT
  237. * 10 2 UC-: _PAGE_CACHE_MODE_UC_MINUS
  238. * 11 3 UC : _PAGE_CACHE_MODE_UC
  239. *
  240. * NOTE: When WC or WP is used, it is redirected to UC- per
  241. * the default setup in __cachemode2pte_tbl[].
  242. */
  243. pat = PAT(0, WB) | PAT(1, WT) | PAT(2, UC_MINUS) | PAT(3, UC) |
  244. PAT(4, WB) | PAT(5, WT) | PAT(6, UC_MINUS) | PAT(7, UC);
  245. }
  246. __init_cache_modes(pat);
  247. }
  248. /**
  249. * pat_init - Initialize PAT MSR and PAT table
  250. *
  251. * This function initializes PAT MSR and PAT table with an OS-defined value
  252. * to enable additional cache attributes, WC and WT.
  253. *
  254. * This function must be called on all CPUs using the specific sequence of
  255. * operations defined in Intel SDM. mtrr_rendezvous_handler() provides this
  256. * procedure for PAT.
  257. */
  258. void pat_init(void)
  259. {
  260. u64 pat;
  261. struct cpuinfo_x86 *c = &boot_cpu_data;
  262. if (pat_disabled)
  263. return;
  264. if ((c->x86_vendor == X86_VENDOR_INTEL) &&
  265. (((c->x86 == 0x6) && (c->x86_model <= 0xd)) ||
  266. ((c->x86 == 0xf) && (c->x86_model <= 0x6)))) {
  267. /*
  268. * PAT support with the lower four entries. Intel Pentium 2,
  269. * 3, M, and 4 are affected by PAT errata, which makes the
  270. * upper four entries unusable. To be on the safe side, we don't
  271. * use those.
  272. *
  273. * PTE encoding:
  274. * PAT
  275. * |PCD
  276. * ||PWT PAT
  277. * ||| slot
  278. * 000 0 WB : _PAGE_CACHE_MODE_WB
  279. * 001 1 WC : _PAGE_CACHE_MODE_WC
  280. * 010 2 UC-: _PAGE_CACHE_MODE_UC_MINUS
  281. * 011 3 UC : _PAGE_CACHE_MODE_UC
  282. * PAT bit unused
  283. *
  284. * NOTE: When WT or WP is used, it is redirected to UC- per
  285. * the default setup in __cachemode2pte_tbl[].
  286. */
  287. pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
  288. PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, UC);
  289. } else {
  290. /*
  291. * Full PAT support. We put WT in slot 7 to improve
  292. * robustness in the presence of errata that might cause
  293. * the high PAT bit to be ignored. This way, a buggy slot 7
  294. * access will hit slot 3, and slot 3 is UC, so at worst
  295. * we lose performance without causing a correctness issue.
  296. * Pentium 4 erratum N46 is an example for such an erratum,
  297. * although we try not to use PAT at all on affected CPUs.
  298. *
  299. * PTE encoding:
  300. * PAT
  301. * |PCD
  302. * ||PWT PAT
  303. * ||| slot
  304. * 000 0 WB : _PAGE_CACHE_MODE_WB
  305. * 001 1 WC : _PAGE_CACHE_MODE_WC
  306. * 010 2 UC-: _PAGE_CACHE_MODE_UC_MINUS
  307. * 011 3 UC : _PAGE_CACHE_MODE_UC
  308. * 100 4 WB : Reserved
  309. * 101 5 WC : Reserved
  310. * 110 6 UC-: Reserved
  311. * 111 7 WT : _PAGE_CACHE_MODE_WT
  312. *
  313. * The reserved slots are unused, but mapped to their
  314. * corresponding types in the presence of PAT errata.
  315. */
  316. pat = PAT(0, WB) | PAT(1, WC) | PAT(2, UC_MINUS) | PAT(3, UC) |
  317. PAT(4, WB) | PAT(5, WC) | PAT(6, UC_MINUS) | PAT(7, WT);
  318. }
  319. if (!boot_cpu_done) {
  320. pat_bsp_init(pat);
  321. boot_cpu_done = true;
  322. } else {
  323. pat_ap_init(pat);
  324. }
  325. }
  326. #undef PAT
  327. static DEFINE_SPINLOCK(memtype_lock); /* protects memtype accesses */
  328. /*
  329. * Does intersection of PAT memory type and MTRR memory type and returns
  330. * the resulting memory type as PAT understands it.
  331. * (Type in pat and mtrr will not have same value)
  332. * The intersection is based on "Effective Memory Type" tables in IA-32
  333. * SDM vol 3a
  334. */
  335. static unsigned long pat_x_mtrr_type(u64 start, u64 end,
  336. enum page_cache_mode req_type)
  337. {
  338. /*
  339. * Look for MTRR hint to get the effective type in case where PAT
  340. * request is for WB.
  341. */
  342. if (req_type == _PAGE_CACHE_MODE_WB) {
  343. u8 mtrr_type, uniform;
  344. mtrr_type = mtrr_type_lookup(start, end, &uniform);
  345. if (mtrr_type != MTRR_TYPE_WRBACK)
  346. return _PAGE_CACHE_MODE_UC_MINUS;
  347. return _PAGE_CACHE_MODE_WB;
  348. }
  349. return req_type;
  350. }
  351. struct pagerange_state {
  352. unsigned long cur_pfn;
  353. int ram;
  354. int not_ram;
  355. };
  356. static int
  357. pagerange_is_ram_callback(unsigned long initial_pfn, unsigned long total_nr_pages, void *arg)
  358. {
  359. struct pagerange_state *state = arg;
  360. state->not_ram |= initial_pfn > state->cur_pfn;
  361. state->ram |= total_nr_pages > 0;
  362. state->cur_pfn = initial_pfn + total_nr_pages;
  363. return state->ram && state->not_ram;
  364. }
  365. static int pat_pagerange_is_ram(resource_size_t start, resource_size_t end)
  366. {
  367. int ret = 0;
  368. unsigned long start_pfn = start >> PAGE_SHIFT;
  369. unsigned long end_pfn = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  370. struct pagerange_state state = {start_pfn, 0, 0};
  371. /*
  372. * For legacy reasons, physical address range in the legacy ISA
  373. * region is tracked as non-RAM. This will allow users of
  374. * /dev/mem to map portions of legacy ISA region, even when
  375. * some of those portions are listed(or not even listed) with
  376. * different e820 types(RAM/reserved/..)
  377. */
  378. if (start_pfn < ISA_END_ADDRESS >> PAGE_SHIFT)
  379. start_pfn = ISA_END_ADDRESS >> PAGE_SHIFT;
  380. if (start_pfn < end_pfn) {
  381. ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn,
  382. &state, pagerange_is_ram_callback);
  383. }
  384. return (ret > 0) ? -1 : (state.ram ? 1 : 0);
  385. }
  386. /*
  387. * For RAM pages, we use page flags to mark the pages with appropriate type.
  388. * The page flags are limited to four types, WB (default), WC, WT and UC-.
  389. * WP request fails with -EINVAL, and UC gets redirected to UC-. Setting
  390. * a new memory type is only allowed for a page mapped with the default WB
  391. * type.
  392. *
  393. * Here we do two passes:
  394. * - Find the memtype of all the pages in the range, look for any conflicts.
  395. * - In case of no conflicts, set the new memtype for pages in the range.
  396. */
  397. static int reserve_ram_pages_type(u64 start, u64 end,
  398. enum page_cache_mode req_type,
  399. enum page_cache_mode *new_type)
  400. {
  401. struct page *page;
  402. u64 pfn;
  403. if (req_type == _PAGE_CACHE_MODE_WP) {
  404. if (new_type)
  405. *new_type = _PAGE_CACHE_MODE_UC_MINUS;
  406. return -EINVAL;
  407. }
  408. if (req_type == _PAGE_CACHE_MODE_UC) {
  409. /* We do not support strong UC */
  410. WARN_ON_ONCE(1);
  411. req_type = _PAGE_CACHE_MODE_UC_MINUS;
  412. }
  413. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  414. enum page_cache_mode type;
  415. page = pfn_to_page(pfn);
  416. type = get_page_memtype(page);
  417. if (type != _PAGE_CACHE_MODE_WB) {
  418. pr_info("x86/PAT: reserve_ram_pages_type failed [mem %#010Lx-%#010Lx], track 0x%x, req 0x%x\n",
  419. start, end - 1, type, req_type);
  420. if (new_type)
  421. *new_type = type;
  422. return -EBUSY;
  423. }
  424. }
  425. if (new_type)
  426. *new_type = req_type;
  427. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  428. page = pfn_to_page(pfn);
  429. set_page_memtype(page, req_type);
  430. }
  431. return 0;
  432. }
  433. static int free_ram_pages_type(u64 start, u64 end)
  434. {
  435. struct page *page;
  436. u64 pfn;
  437. for (pfn = (start >> PAGE_SHIFT); pfn < (end >> PAGE_SHIFT); ++pfn) {
  438. page = pfn_to_page(pfn);
  439. set_page_memtype(page, _PAGE_CACHE_MODE_WB);
  440. }
  441. return 0;
  442. }
  443. /*
  444. * req_type typically has one of the:
  445. * - _PAGE_CACHE_MODE_WB
  446. * - _PAGE_CACHE_MODE_WC
  447. * - _PAGE_CACHE_MODE_UC_MINUS
  448. * - _PAGE_CACHE_MODE_UC
  449. * - _PAGE_CACHE_MODE_WT
  450. *
  451. * If new_type is NULL, function will return an error if it cannot reserve the
  452. * region with req_type. If new_type is non-NULL, function will return
  453. * available type in new_type in case of no error. In case of any error
  454. * it will return a negative return value.
  455. */
  456. int reserve_memtype(u64 start, u64 end, enum page_cache_mode req_type,
  457. enum page_cache_mode *new_type)
  458. {
  459. struct memtype *new;
  460. enum page_cache_mode actual_type;
  461. int is_range_ram;
  462. int err = 0;
  463. BUG_ON(start >= end); /* end is exclusive */
  464. if (!pat_enabled()) {
  465. /* This is identical to page table setting without PAT */
  466. if (new_type)
  467. *new_type = req_type;
  468. return 0;
  469. }
  470. /* Low ISA region is always mapped WB in page table. No need to track */
  471. if (x86_platform.is_untracked_pat_range(start, end)) {
  472. if (new_type)
  473. *new_type = _PAGE_CACHE_MODE_WB;
  474. return 0;
  475. }
  476. /*
  477. * Call mtrr_lookup to get the type hint. This is an
  478. * optimization for /dev/mem mmap'ers into WB memory (BIOS
  479. * tools and ACPI tools). Use WB request for WB memory and use
  480. * UC_MINUS otherwise.
  481. */
  482. actual_type = pat_x_mtrr_type(start, end, req_type);
  483. if (new_type)
  484. *new_type = actual_type;
  485. is_range_ram = pat_pagerange_is_ram(start, end);
  486. if (is_range_ram == 1) {
  487. err = reserve_ram_pages_type(start, end, req_type, new_type);
  488. return err;
  489. } else if (is_range_ram < 0) {
  490. return -EINVAL;
  491. }
  492. new = kzalloc(sizeof(struct memtype), GFP_KERNEL);
  493. if (!new)
  494. return -ENOMEM;
  495. new->start = start;
  496. new->end = end;
  497. new->type = actual_type;
  498. spin_lock(&memtype_lock);
  499. err = rbt_memtype_check_insert(new, new_type);
  500. if (err) {
  501. pr_info("x86/PAT: reserve_memtype failed [mem %#010Lx-%#010Lx], track %s, req %s\n",
  502. start, end - 1,
  503. cattr_name(new->type), cattr_name(req_type));
  504. kfree(new);
  505. spin_unlock(&memtype_lock);
  506. return err;
  507. }
  508. spin_unlock(&memtype_lock);
  509. dprintk("reserve_memtype added [mem %#010Lx-%#010Lx], track %s, req %s, ret %s\n",
  510. start, end - 1, cattr_name(new->type), cattr_name(req_type),
  511. new_type ? cattr_name(*new_type) : "-");
  512. return err;
  513. }
  514. int free_memtype(u64 start, u64 end)
  515. {
  516. int err = -EINVAL;
  517. int is_range_ram;
  518. struct memtype *entry;
  519. if (!pat_enabled())
  520. return 0;
  521. /* Low ISA region is always mapped WB. No need to track */
  522. if (x86_platform.is_untracked_pat_range(start, end))
  523. return 0;
  524. is_range_ram = pat_pagerange_is_ram(start, end);
  525. if (is_range_ram == 1) {
  526. err = free_ram_pages_type(start, end);
  527. return err;
  528. } else if (is_range_ram < 0) {
  529. return -EINVAL;
  530. }
  531. spin_lock(&memtype_lock);
  532. entry = rbt_memtype_erase(start, end);
  533. spin_unlock(&memtype_lock);
  534. if (IS_ERR(entry)) {
  535. pr_info("x86/PAT: %s:%d freeing invalid memtype [mem %#010Lx-%#010Lx]\n",
  536. current->comm, current->pid, start, end - 1);
  537. return -EINVAL;
  538. }
  539. kfree(entry);
  540. dprintk("free_memtype request [mem %#010Lx-%#010Lx]\n", start, end - 1);
  541. return 0;
  542. }
  543. /**
  544. * lookup_memtype - Looksup the memory type for a physical address
  545. * @paddr: physical address of which memory type needs to be looked up
  546. *
  547. * Only to be called when PAT is enabled
  548. *
  549. * Returns _PAGE_CACHE_MODE_WB, _PAGE_CACHE_MODE_WC, _PAGE_CACHE_MODE_UC_MINUS
  550. * or _PAGE_CACHE_MODE_WT.
  551. */
  552. static enum page_cache_mode lookup_memtype(u64 paddr)
  553. {
  554. enum page_cache_mode rettype = _PAGE_CACHE_MODE_WB;
  555. struct memtype *entry;
  556. if (x86_platform.is_untracked_pat_range(paddr, paddr + PAGE_SIZE))
  557. return rettype;
  558. if (pat_pagerange_is_ram(paddr, paddr + PAGE_SIZE)) {
  559. struct page *page;
  560. page = pfn_to_page(paddr >> PAGE_SHIFT);
  561. return get_page_memtype(page);
  562. }
  563. spin_lock(&memtype_lock);
  564. entry = rbt_memtype_lookup(paddr);
  565. if (entry != NULL)
  566. rettype = entry->type;
  567. else
  568. rettype = _PAGE_CACHE_MODE_UC_MINUS;
  569. spin_unlock(&memtype_lock);
  570. return rettype;
  571. }
  572. /**
  573. * io_reserve_memtype - Request a memory type mapping for a region of memory
  574. * @start: start (physical address) of the region
  575. * @end: end (physical address) of the region
  576. * @type: A pointer to memtype, with requested type. On success, requested
  577. * or any other compatible type that was available for the region is returned
  578. *
  579. * On success, returns 0
  580. * On failure, returns non-zero
  581. */
  582. int io_reserve_memtype(resource_size_t start, resource_size_t end,
  583. enum page_cache_mode *type)
  584. {
  585. resource_size_t size = end - start;
  586. enum page_cache_mode req_type = *type;
  587. enum page_cache_mode new_type;
  588. int ret;
  589. WARN_ON_ONCE(iomem_map_sanity_check(start, size));
  590. ret = reserve_memtype(start, end, req_type, &new_type);
  591. if (ret)
  592. goto out_err;
  593. if (!is_new_memtype_allowed(start, size, req_type, new_type))
  594. goto out_free;
  595. if (kernel_map_sync_memtype(start, size, new_type) < 0)
  596. goto out_free;
  597. *type = new_type;
  598. return 0;
  599. out_free:
  600. free_memtype(start, end);
  601. ret = -EBUSY;
  602. out_err:
  603. return ret;
  604. }
  605. /**
  606. * io_free_memtype - Release a memory type mapping for a region of memory
  607. * @start: start (physical address) of the region
  608. * @end: end (physical address) of the region
  609. */
  610. void io_free_memtype(resource_size_t start, resource_size_t end)
  611. {
  612. free_memtype(start, end);
  613. }
  614. int arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size)
  615. {
  616. enum page_cache_mode type = _PAGE_CACHE_MODE_WC;
  617. return io_reserve_memtype(start, start + size, &type);
  618. }
  619. EXPORT_SYMBOL(arch_io_reserve_memtype_wc);
  620. void arch_io_free_memtype_wc(resource_size_t start, resource_size_t size)
  621. {
  622. io_free_memtype(start, start + size);
  623. }
  624. EXPORT_SYMBOL(arch_io_free_memtype_wc);
  625. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  626. unsigned long size, pgprot_t vma_prot)
  627. {
  628. return vma_prot;
  629. }
  630. #ifdef CONFIG_STRICT_DEVMEM
  631. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM */
  632. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  633. {
  634. return 1;
  635. }
  636. #else
  637. /* This check is needed to avoid cache aliasing when PAT is enabled */
  638. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  639. {
  640. u64 from = ((u64)pfn) << PAGE_SHIFT;
  641. u64 to = from + size;
  642. u64 cursor = from;
  643. if (!pat_enabled())
  644. return 1;
  645. while (cursor < to) {
  646. if (!devmem_is_allowed(pfn))
  647. return 0;
  648. cursor += PAGE_SIZE;
  649. pfn++;
  650. }
  651. return 1;
  652. }
  653. #endif /* CONFIG_STRICT_DEVMEM */
  654. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  655. unsigned long size, pgprot_t *vma_prot)
  656. {
  657. enum page_cache_mode pcm = _PAGE_CACHE_MODE_WB;
  658. if (!range_is_allowed(pfn, size))
  659. return 0;
  660. if (file->f_flags & O_DSYNC)
  661. pcm = _PAGE_CACHE_MODE_UC_MINUS;
  662. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  663. cachemode2protval(pcm));
  664. return 1;
  665. }
  666. /*
  667. * Change the memory type for the physial address range in kernel identity
  668. * mapping space if that range is a part of identity map.
  669. */
  670. int kernel_map_sync_memtype(u64 base, unsigned long size,
  671. enum page_cache_mode pcm)
  672. {
  673. unsigned long id_sz;
  674. if (base > __pa(high_memory-1))
  675. return 0;
  676. /*
  677. * some areas in the middle of the kernel identity range
  678. * are not mapped, like the PCI space.
  679. */
  680. if (!page_is_ram(base >> PAGE_SHIFT))
  681. return 0;
  682. id_sz = (__pa(high_memory-1) <= base + size) ?
  683. __pa(high_memory) - base :
  684. size;
  685. if (ioremap_change_attr((unsigned long)__va(base), id_sz, pcm) < 0) {
  686. pr_info("x86/PAT: %s:%d ioremap_change_attr failed %s for [mem %#010Lx-%#010Lx]\n",
  687. current->comm, current->pid,
  688. cattr_name(pcm),
  689. base, (unsigned long long)(base + size-1));
  690. return -EINVAL;
  691. }
  692. return 0;
  693. }
  694. /*
  695. * Internal interface to reserve a range of physical memory with prot.
  696. * Reserved non RAM regions only and after successful reserve_memtype,
  697. * this func also keeps identity mapping (if any) in sync with this new prot.
  698. */
  699. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  700. int strict_prot)
  701. {
  702. int is_ram = 0;
  703. int ret;
  704. enum page_cache_mode want_pcm = pgprot2cachemode(*vma_prot);
  705. enum page_cache_mode pcm = want_pcm;
  706. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  707. /*
  708. * reserve_pfn_range() for RAM pages. We do not refcount to keep
  709. * track of number of mappings of RAM pages. We can assert that
  710. * the type requested matches the type of first page in the range.
  711. */
  712. if (is_ram) {
  713. if (!pat_enabled())
  714. return 0;
  715. pcm = lookup_memtype(paddr);
  716. if (want_pcm != pcm) {
  717. pr_warn("x86/PAT: %s:%d map pfn RAM range req %s for [mem %#010Lx-%#010Lx], got %s\n",
  718. current->comm, current->pid,
  719. cattr_name(want_pcm),
  720. (unsigned long long)paddr,
  721. (unsigned long long)(paddr + size - 1),
  722. cattr_name(pcm));
  723. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  724. (~_PAGE_CACHE_MASK)) |
  725. cachemode2protval(pcm));
  726. }
  727. return 0;
  728. }
  729. ret = reserve_memtype(paddr, paddr + size, want_pcm, &pcm);
  730. if (ret)
  731. return ret;
  732. if (pcm != want_pcm) {
  733. if (strict_prot ||
  734. !is_new_memtype_allowed(paddr, size, want_pcm, pcm)) {
  735. free_memtype(paddr, paddr + size);
  736. pr_err("x86/PAT: %s:%d map pfn expected mapping type %s for [mem %#010Lx-%#010Lx], got %s\n",
  737. current->comm, current->pid,
  738. cattr_name(want_pcm),
  739. (unsigned long long)paddr,
  740. (unsigned long long)(paddr + size - 1),
  741. cattr_name(pcm));
  742. return -EINVAL;
  743. }
  744. /*
  745. * We allow returning different type than the one requested in
  746. * non strict case.
  747. */
  748. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  749. (~_PAGE_CACHE_MASK)) |
  750. cachemode2protval(pcm));
  751. }
  752. if (kernel_map_sync_memtype(paddr, size, pcm) < 0) {
  753. free_memtype(paddr, paddr + size);
  754. return -EINVAL;
  755. }
  756. return 0;
  757. }
  758. /*
  759. * Internal interface to free a range of physical memory.
  760. * Frees non RAM regions only.
  761. */
  762. static void free_pfn_range(u64 paddr, unsigned long size)
  763. {
  764. int is_ram;
  765. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  766. if (is_ram == 0)
  767. free_memtype(paddr, paddr + size);
  768. }
  769. /*
  770. * track_pfn_copy is called when vma that is covering the pfnmap gets
  771. * copied through copy_page_range().
  772. *
  773. * If the vma has a linear pfn mapping for the entire range, we get the prot
  774. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  775. */
  776. int track_pfn_copy(struct vm_area_struct *vma)
  777. {
  778. resource_size_t paddr;
  779. unsigned long prot;
  780. unsigned long vma_size = vma->vm_end - vma->vm_start;
  781. pgprot_t pgprot;
  782. if (vma->vm_flags & VM_PAT) {
  783. /*
  784. * reserve the whole chunk covered by vma. We need the
  785. * starting address and protection from pte.
  786. */
  787. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  788. WARN_ON_ONCE(1);
  789. return -EINVAL;
  790. }
  791. pgprot = __pgprot(prot);
  792. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  793. }
  794. return 0;
  795. }
  796. /*
  797. * prot is passed in as a parameter for the new mapping. If the vma has
  798. * a linear pfn mapping for the entire range, or no vma is provided,
  799. * reserve the entire pfn + size range with single reserve_pfn_range
  800. * call.
  801. */
  802. int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  803. unsigned long pfn, unsigned long addr, unsigned long size)
  804. {
  805. resource_size_t paddr = (resource_size_t)pfn << PAGE_SHIFT;
  806. enum page_cache_mode pcm;
  807. /* reserve the whole chunk starting from paddr */
  808. if (!vma || (addr == vma->vm_start
  809. && size == (vma->vm_end - vma->vm_start))) {
  810. int ret;
  811. ret = reserve_pfn_range(paddr, size, prot, 0);
  812. if (ret == 0 && vma)
  813. vma->vm_flags |= VM_PAT;
  814. return ret;
  815. }
  816. if (!pat_enabled())
  817. return 0;
  818. /*
  819. * For anything smaller than the vma size we set prot based on the
  820. * lookup.
  821. */
  822. pcm = lookup_memtype(paddr);
  823. /* Check memtype for the remaining pages */
  824. while (size > PAGE_SIZE) {
  825. size -= PAGE_SIZE;
  826. paddr += PAGE_SIZE;
  827. if (pcm != lookup_memtype(paddr))
  828. return -EINVAL;
  829. }
  830. *prot = __pgprot((pgprot_val(*prot) & (~_PAGE_CACHE_MASK)) |
  831. cachemode2protval(pcm));
  832. return 0;
  833. }
  834. int track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
  835. pfn_t pfn)
  836. {
  837. enum page_cache_mode pcm;
  838. if (!pat_enabled())
  839. return 0;
  840. /* Set prot based on lookup */
  841. pcm = lookup_memtype(pfn_t_to_phys(pfn));
  842. *prot = __pgprot((pgprot_val(*prot) & (~_PAGE_CACHE_MASK)) |
  843. cachemode2protval(pcm));
  844. return 0;
  845. }
  846. /*
  847. * untrack_pfn is called while unmapping a pfnmap for a region.
  848. * untrack can be called for a specific region indicated by pfn and size or
  849. * can be for the entire vma (in which case pfn, size are zero).
  850. */
  851. void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
  852. unsigned long size)
  853. {
  854. resource_size_t paddr;
  855. unsigned long prot;
  856. if (vma && !(vma->vm_flags & VM_PAT))
  857. return;
  858. /* free the chunk starting from pfn or the whole chunk */
  859. paddr = (resource_size_t)pfn << PAGE_SHIFT;
  860. if (!paddr && !size) {
  861. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  862. WARN_ON_ONCE(1);
  863. return;
  864. }
  865. size = vma->vm_end - vma->vm_start;
  866. }
  867. free_pfn_range(paddr, size);
  868. if (vma)
  869. vma->vm_flags &= ~VM_PAT;
  870. }
  871. /*
  872. * untrack_pfn_moved is called, while mremapping a pfnmap for a new region,
  873. * with the old vma after its pfnmap page table has been removed. The new
  874. * vma has a new pfnmap to the same pfn & cache type with VM_PAT set.
  875. */
  876. void untrack_pfn_moved(struct vm_area_struct *vma)
  877. {
  878. vma->vm_flags &= ~VM_PAT;
  879. }
  880. pgprot_t pgprot_writecombine(pgprot_t prot)
  881. {
  882. return __pgprot(pgprot_val(prot) |
  883. cachemode2protval(_PAGE_CACHE_MODE_WC));
  884. }
  885. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  886. pgprot_t pgprot_writethrough(pgprot_t prot)
  887. {
  888. return __pgprot(pgprot_val(prot) |
  889. cachemode2protval(_PAGE_CACHE_MODE_WT));
  890. }
  891. EXPORT_SYMBOL_GPL(pgprot_writethrough);
  892. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  893. static struct memtype *memtype_get_idx(loff_t pos)
  894. {
  895. struct memtype *print_entry;
  896. int ret;
  897. print_entry = kzalloc(sizeof(struct memtype), GFP_KERNEL);
  898. if (!print_entry)
  899. return NULL;
  900. spin_lock(&memtype_lock);
  901. ret = rbt_memtype_copy_nth_element(print_entry, pos);
  902. spin_unlock(&memtype_lock);
  903. if (!ret) {
  904. return print_entry;
  905. } else {
  906. kfree(print_entry);
  907. return NULL;
  908. }
  909. }
  910. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  911. {
  912. if (*pos == 0) {
  913. ++*pos;
  914. seq_puts(seq, "PAT memtype list:\n");
  915. }
  916. return memtype_get_idx(*pos);
  917. }
  918. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  919. {
  920. ++*pos;
  921. return memtype_get_idx(*pos);
  922. }
  923. static void memtype_seq_stop(struct seq_file *seq, void *v)
  924. {
  925. }
  926. static int memtype_seq_show(struct seq_file *seq, void *v)
  927. {
  928. struct memtype *print_entry = (struct memtype *)v;
  929. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  930. print_entry->start, print_entry->end);
  931. kfree(print_entry);
  932. return 0;
  933. }
  934. static const struct seq_operations memtype_seq_ops = {
  935. .start = memtype_seq_start,
  936. .next = memtype_seq_next,
  937. .stop = memtype_seq_stop,
  938. .show = memtype_seq_show,
  939. };
  940. static int memtype_seq_open(struct inode *inode, struct file *file)
  941. {
  942. return seq_open(file, &memtype_seq_ops);
  943. }
  944. static const struct file_operations memtype_fops = {
  945. .open = memtype_seq_open,
  946. .read = seq_read,
  947. .llseek = seq_lseek,
  948. .release = seq_release,
  949. };
  950. static int __init pat_memtype_list_init(void)
  951. {
  952. if (pat_enabled()) {
  953. debugfs_create_file("pat_memtype_list", S_IRUSR,
  954. arch_debugfs_dir, NULL, &memtype_fops);
  955. }
  956. return 0;
  957. }
  958. late_initcall(pat_memtype_list_init);
  959. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */