pat.c 29 KB

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