pat.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. int arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size)
  617. {
  618. enum page_cache_mode type = _PAGE_CACHE_MODE_WC;
  619. return io_reserve_memtype(start, start + size, &type);
  620. }
  621. EXPORT_SYMBOL(arch_io_reserve_memtype_wc);
  622. void arch_io_free_memtype_wc(resource_size_t start, resource_size_t size)
  623. {
  624. io_free_memtype(start, start + size);
  625. }
  626. EXPORT_SYMBOL(arch_io_free_memtype_wc);
  627. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  628. unsigned long size, pgprot_t vma_prot)
  629. {
  630. return vma_prot;
  631. }
  632. #ifdef CONFIG_STRICT_DEVMEM
  633. /* This check is done in drivers/char/mem.c in case of STRICT_DEVMEM */
  634. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  635. {
  636. return 1;
  637. }
  638. #else
  639. /* This check is needed to avoid cache aliasing when PAT is enabled */
  640. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  641. {
  642. u64 from = ((u64)pfn) << PAGE_SHIFT;
  643. u64 to = from + size;
  644. u64 cursor = from;
  645. if (!pat_enabled())
  646. return 1;
  647. while (cursor < to) {
  648. if (!devmem_is_allowed(pfn))
  649. return 0;
  650. cursor += PAGE_SIZE;
  651. pfn++;
  652. }
  653. return 1;
  654. }
  655. #endif /* CONFIG_STRICT_DEVMEM */
  656. int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
  657. unsigned long size, pgprot_t *vma_prot)
  658. {
  659. enum page_cache_mode pcm = _PAGE_CACHE_MODE_WB;
  660. if (!range_is_allowed(pfn, size))
  661. return 0;
  662. if (file->f_flags & O_DSYNC)
  663. pcm = _PAGE_CACHE_MODE_UC_MINUS;
  664. *vma_prot = __pgprot((pgprot_val(*vma_prot) & ~_PAGE_CACHE_MASK) |
  665. cachemode2protval(pcm));
  666. return 1;
  667. }
  668. /*
  669. * Change the memory type for the physial address range in kernel identity
  670. * mapping space if that range is a part of identity map.
  671. */
  672. int kernel_map_sync_memtype(u64 base, unsigned long size,
  673. enum page_cache_mode pcm)
  674. {
  675. unsigned long id_sz;
  676. if (base > __pa(high_memory-1))
  677. return 0;
  678. /*
  679. * some areas in the middle of the kernel identity range
  680. * are not mapped, like the PCI space.
  681. */
  682. if (!page_is_ram(base >> PAGE_SHIFT))
  683. return 0;
  684. id_sz = (__pa(high_memory-1) <= base + size) ?
  685. __pa(high_memory) - base :
  686. size;
  687. if (ioremap_change_attr((unsigned long)__va(base), id_sz, pcm) < 0) {
  688. pr_info("x86/PAT: %s:%d ioremap_change_attr failed %s for [mem %#010Lx-%#010Lx]\n",
  689. current->comm, current->pid,
  690. cattr_name(pcm),
  691. base, (unsigned long long)(base + size-1));
  692. return -EINVAL;
  693. }
  694. return 0;
  695. }
  696. /*
  697. * Internal interface to reserve a range of physical memory with prot.
  698. * Reserved non RAM regions only and after successful reserve_memtype,
  699. * this func also keeps identity mapping (if any) in sync with this new prot.
  700. */
  701. static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,
  702. int strict_prot)
  703. {
  704. int is_ram = 0;
  705. int ret;
  706. enum page_cache_mode want_pcm = pgprot2cachemode(*vma_prot);
  707. enum page_cache_mode pcm = want_pcm;
  708. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  709. /*
  710. * reserve_pfn_range() for RAM pages. We do not refcount to keep
  711. * track of number of mappings of RAM pages. We can assert that
  712. * the type requested matches the type of first page in the range.
  713. */
  714. if (is_ram) {
  715. if (!pat_enabled())
  716. return 0;
  717. pcm = lookup_memtype(paddr);
  718. if (want_pcm != pcm) {
  719. pr_warn("x86/PAT: %s:%d map pfn RAM range req %s for [mem %#010Lx-%#010Lx], got %s\n",
  720. current->comm, current->pid,
  721. cattr_name(want_pcm),
  722. (unsigned long long)paddr,
  723. (unsigned long long)(paddr + size - 1),
  724. cattr_name(pcm));
  725. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  726. (~_PAGE_CACHE_MASK)) |
  727. cachemode2protval(pcm));
  728. }
  729. return 0;
  730. }
  731. ret = reserve_memtype(paddr, paddr + size, want_pcm, &pcm);
  732. if (ret)
  733. return ret;
  734. if (pcm != want_pcm) {
  735. if (strict_prot ||
  736. !is_new_memtype_allowed(paddr, size, want_pcm, pcm)) {
  737. free_memtype(paddr, paddr + size);
  738. pr_err("x86/PAT: %s:%d map pfn expected mapping type %s for [mem %#010Lx-%#010Lx], got %s\n",
  739. current->comm, current->pid,
  740. cattr_name(want_pcm),
  741. (unsigned long long)paddr,
  742. (unsigned long long)(paddr + size - 1),
  743. cattr_name(pcm));
  744. return -EINVAL;
  745. }
  746. /*
  747. * We allow returning different type than the one requested in
  748. * non strict case.
  749. */
  750. *vma_prot = __pgprot((pgprot_val(*vma_prot) &
  751. (~_PAGE_CACHE_MASK)) |
  752. cachemode2protval(pcm));
  753. }
  754. if (kernel_map_sync_memtype(paddr, size, pcm) < 0) {
  755. free_memtype(paddr, paddr + size);
  756. return -EINVAL;
  757. }
  758. return 0;
  759. }
  760. /*
  761. * Internal interface to free a range of physical memory.
  762. * Frees non RAM regions only.
  763. */
  764. static void free_pfn_range(u64 paddr, unsigned long size)
  765. {
  766. int is_ram;
  767. is_ram = pat_pagerange_is_ram(paddr, paddr + size);
  768. if (is_ram == 0)
  769. free_memtype(paddr, paddr + size);
  770. }
  771. /*
  772. * track_pfn_copy is called when vma that is covering the pfnmap gets
  773. * copied through copy_page_range().
  774. *
  775. * If the vma has a linear pfn mapping for the entire range, we get the prot
  776. * from pte and reserve the entire vma range with single reserve_pfn_range call.
  777. */
  778. int track_pfn_copy(struct vm_area_struct *vma)
  779. {
  780. resource_size_t paddr;
  781. unsigned long prot;
  782. unsigned long vma_size = vma->vm_end - vma->vm_start;
  783. pgprot_t pgprot;
  784. if (vma->vm_flags & VM_PAT) {
  785. /*
  786. * reserve the whole chunk covered by vma. We need the
  787. * starting address and protection from pte.
  788. */
  789. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  790. WARN_ON_ONCE(1);
  791. return -EINVAL;
  792. }
  793. pgprot = __pgprot(prot);
  794. return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
  795. }
  796. return 0;
  797. }
  798. /*
  799. * prot is passed in as a parameter for the new mapping. If the vma has
  800. * a linear pfn mapping for the entire range, or no vma is provided,
  801. * reserve the entire pfn + size range with single reserve_pfn_range
  802. * call.
  803. */
  804. int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,
  805. unsigned long pfn, unsigned long addr, unsigned long size)
  806. {
  807. resource_size_t paddr = (resource_size_t)pfn << PAGE_SHIFT;
  808. enum page_cache_mode pcm;
  809. /* reserve the whole chunk starting from paddr */
  810. if (!vma || (addr == vma->vm_start
  811. && size == (vma->vm_end - vma->vm_start))) {
  812. int ret;
  813. ret = reserve_pfn_range(paddr, size, prot, 0);
  814. if (ret == 0 && vma)
  815. vma->vm_flags |= VM_PAT;
  816. return ret;
  817. }
  818. if (!pat_enabled())
  819. return 0;
  820. /*
  821. * For anything smaller than the vma size we set prot based on the
  822. * lookup.
  823. */
  824. pcm = lookup_memtype(paddr);
  825. /* Check memtype for the remaining pages */
  826. while (size > PAGE_SIZE) {
  827. size -= PAGE_SIZE;
  828. paddr += PAGE_SIZE;
  829. if (pcm != lookup_memtype(paddr))
  830. return -EINVAL;
  831. }
  832. *prot = __pgprot((pgprot_val(*prot) & (~_PAGE_CACHE_MASK)) |
  833. cachemode2protval(pcm));
  834. return 0;
  835. }
  836. void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn)
  837. {
  838. enum page_cache_mode pcm;
  839. if (!pat_enabled())
  840. return;
  841. /* Set prot based on lookup */
  842. pcm = lookup_memtype(pfn_t_to_phys(pfn));
  843. *prot = __pgprot((pgprot_val(*prot) & (~_PAGE_CACHE_MASK)) |
  844. cachemode2protval(pcm));
  845. }
  846. /*
  847. * untrack_pfn is called while unmapping a pfnmap for a region.
  848. * untrack can be called for a specific region indicated by pfn and size or
  849. * can be for the entire vma (in which case pfn, size are zero).
  850. */
  851. void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
  852. unsigned long size)
  853. {
  854. resource_size_t paddr;
  855. unsigned long prot;
  856. if (vma && !(vma->vm_flags & VM_PAT))
  857. return;
  858. /* free the chunk starting from pfn or the whole chunk */
  859. paddr = (resource_size_t)pfn << PAGE_SHIFT;
  860. if (!paddr && !size) {
  861. if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
  862. WARN_ON_ONCE(1);
  863. return;
  864. }
  865. size = vma->vm_end - vma->vm_start;
  866. }
  867. free_pfn_range(paddr, size);
  868. if (vma)
  869. vma->vm_flags &= ~VM_PAT;
  870. }
  871. /*
  872. * untrack_pfn_moved is called, while mremapping a pfnmap for a new region,
  873. * with the old vma after its pfnmap page table has been removed. The new
  874. * vma has a new pfnmap to the same pfn & cache type with VM_PAT set.
  875. */
  876. void untrack_pfn_moved(struct vm_area_struct *vma)
  877. {
  878. vma->vm_flags &= ~VM_PAT;
  879. }
  880. pgprot_t pgprot_writecombine(pgprot_t prot)
  881. {
  882. return __pgprot(pgprot_val(prot) |
  883. cachemode2protval(_PAGE_CACHE_MODE_WC));
  884. }
  885. EXPORT_SYMBOL_GPL(pgprot_writecombine);
  886. pgprot_t pgprot_writethrough(pgprot_t prot)
  887. {
  888. return __pgprot(pgprot_val(prot) |
  889. cachemode2protval(_PAGE_CACHE_MODE_WT));
  890. }
  891. EXPORT_SYMBOL_GPL(pgprot_writethrough);
  892. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_PAT)
  893. static struct memtype *memtype_get_idx(loff_t pos)
  894. {
  895. struct memtype *print_entry;
  896. int ret;
  897. print_entry = kzalloc(sizeof(struct memtype), GFP_KERNEL);
  898. if (!print_entry)
  899. return NULL;
  900. spin_lock(&memtype_lock);
  901. ret = rbt_memtype_copy_nth_element(print_entry, pos);
  902. spin_unlock(&memtype_lock);
  903. if (!ret) {
  904. return print_entry;
  905. } else {
  906. kfree(print_entry);
  907. return NULL;
  908. }
  909. }
  910. static void *memtype_seq_start(struct seq_file *seq, loff_t *pos)
  911. {
  912. if (*pos == 0) {
  913. ++*pos;
  914. seq_puts(seq, "PAT memtype list:\n");
  915. }
  916. return memtype_get_idx(*pos);
  917. }
  918. static void *memtype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  919. {
  920. ++*pos;
  921. return memtype_get_idx(*pos);
  922. }
  923. static void memtype_seq_stop(struct seq_file *seq, void *v)
  924. {
  925. }
  926. static int memtype_seq_show(struct seq_file *seq, void *v)
  927. {
  928. struct memtype *print_entry = (struct memtype *)v;
  929. seq_printf(seq, "%s @ 0x%Lx-0x%Lx\n", cattr_name(print_entry->type),
  930. print_entry->start, print_entry->end);
  931. kfree(print_entry);
  932. return 0;
  933. }
  934. static const struct seq_operations memtype_seq_ops = {
  935. .start = memtype_seq_start,
  936. .next = memtype_seq_next,
  937. .stop = memtype_seq_stop,
  938. .show = memtype_seq_show,
  939. };
  940. static int memtype_seq_open(struct inode *inode, struct file *file)
  941. {
  942. return seq_open(file, &memtype_seq_ops);
  943. }
  944. static const struct file_operations memtype_fops = {
  945. .open = memtype_seq_open,
  946. .read = seq_read,
  947. .llseek = seq_lseek,
  948. .release = seq_release,
  949. };
  950. static int __init pat_memtype_list_init(void)
  951. {
  952. if (pat_enabled()) {
  953. debugfs_create_file("pat_memtype_list", S_IRUSR,
  954. arch_debugfs_dir, NULL, &memtype_fops);
  955. }
  956. return 0;
  957. }
  958. late_initcall(pat_memtype_list_init);
  959. #endif /* CONFIG_DEBUG_FS && CONFIG_X86_PAT */