pat.c 28 KB

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