pat.c 27 KB

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