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