pat.c 28 KB

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