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