generic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong
  4. * because MTRRs can span up to 40 bits (36bits on most modern x86)
  5. */
  6. #include <linux/export.h>
  7. #include <linux/init.h>
  8. #include <linux/io.h>
  9. #include <linux/mm.h>
  10. #include <asm/processor-flags.h>
  11. #include <asm/cpufeature.h>
  12. #include <asm/tlbflush.h>
  13. #include <asm/mtrr.h>
  14. #include <asm/msr.h>
  15. #include <asm/memtype.h>
  16. #include "mtrr.h"
  17. struct fixed_range_block {
  18. int base_msr; /* start address of an MTRR block */
  19. int ranges; /* number of MTRRs in this block */
  20. };
  21. static struct fixed_range_block fixed_range_blocks[] = {
  22. { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */
  23. { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */
  24. { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */
  25. {}
  26. };
  27. static unsigned long smp_changes_mask;
  28. static int mtrr_state_set;
  29. u64 mtrr_tom2;
  30. struct mtrr_state_type mtrr_state;
  31. EXPORT_SYMBOL_GPL(mtrr_state);
  32. /*
  33. * BIOS is expected to clear MtrrFixDramModEn bit, see for example
  34. * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
  35. * Opteron Processors" (26094 Rev. 3.30 February 2006), section
  36. * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
  37. * to 1 during BIOS initialization of the fixed MTRRs, then cleared to
  38. * 0 for operation."
  39. */
  40. static inline void k8_check_syscfg_dram_mod_en(void)
  41. {
  42. u32 lo, hi;
  43. if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
  44. (boot_cpu_data.x86 >= 0x0f)))
  45. return;
  46. rdmsr(MSR_AMD64_SYSCFG, lo, hi);
  47. if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
  48. pr_err(FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
  49. " not cleared by BIOS, clearing this bit\n",
  50. smp_processor_id());
  51. lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
  52. mtrr_wrmsr(MSR_AMD64_SYSCFG, lo, hi);
  53. }
  54. }
  55. /* Get the size of contiguous MTRR range */
  56. static u64 get_mtrr_size(u64 mask)
  57. {
  58. u64 size;
  59. mask >>= PAGE_SHIFT;
  60. mask |= size_or_mask;
  61. size = -mask;
  62. size <<= PAGE_SHIFT;
  63. return size;
  64. }
  65. /*
  66. * Check and return the effective type for MTRR-MTRR type overlap.
  67. * Returns 1 if the effective type is UNCACHEABLE, else returns 0
  68. */
  69. static int check_type_overlap(u8 *prev, u8 *curr)
  70. {
  71. if (*prev == MTRR_TYPE_UNCACHABLE || *curr == MTRR_TYPE_UNCACHABLE) {
  72. *prev = MTRR_TYPE_UNCACHABLE;
  73. *curr = MTRR_TYPE_UNCACHABLE;
  74. return 1;
  75. }
  76. if ((*prev == MTRR_TYPE_WRBACK && *curr == MTRR_TYPE_WRTHROUGH) ||
  77. (*prev == MTRR_TYPE_WRTHROUGH && *curr == MTRR_TYPE_WRBACK)) {
  78. *prev = MTRR_TYPE_WRTHROUGH;
  79. *curr = MTRR_TYPE_WRTHROUGH;
  80. }
  81. if (*prev != *curr) {
  82. *prev = MTRR_TYPE_UNCACHABLE;
  83. *curr = MTRR_TYPE_UNCACHABLE;
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. /**
  89. * mtrr_type_lookup_fixed - look up memory type in MTRR fixed entries
  90. *
  91. * Return the MTRR fixed memory type of 'start'.
  92. *
  93. * MTRR fixed entries are divided into the following ways:
  94. * 0x00000 - 0x7FFFF : This range is divided into eight 64KB sub-ranges
  95. * 0x80000 - 0xBFFFF : This range is divided into sixteen 16KB sub-ranges
  96. * 0xC0000 - 0xFFFFF : This range is divided into sixty-four 4KB sub-ranges
  97. *
  98. * Return Values:
  99. * MTRR_TYPE_(type) - Matched memory type
  100. * MTRR_TYPE_INVALID - Unmatched
  101. */
  102. static u8 mtrr_type_lookup_fixed(u64 start, u64 end)
  103. {
  104. int idx;
  105. if (start >= 0x100000)
  106. return MTRR_TYPE_INVALID;
  107. /* 0x0 - 0x7FFFF */
  108. if (start < 0x80000) {
  109. idx = 0;
  110. idx += (start >> 16);
  111. return mtrr_state.fixed_ranges[idx];
  112. /* 0x80000 - 0xBFFFF */
  113. } else if (start < 0xC0000) {
  114. idx = 1 * 8;
  115. idx += ((start - 0x80000) >> 14);
  116. return mtrr_state.fixed_ranges[idx];
  117. }
  118. /* 0xC0000 - 0xFFFFF */
  119. idx = 3 * 8;
  120. idx += ((start - 0xC0000) >> 12);
  121. return mtrr_state.fixed_ranges[idx];
  122. }
  123. /**
  124. * mtrr_type_lookup_variable - look up memory type in MTRR variable entries
  125. *
  126. * Return Value:
  127. * MTRR_TYPE_(type) - Matched memory type or default memory type (unmatched)
  128. *
  129. * Output Arguments:
  130. * repeat - Set to 1 when [start:end] spanned across MTRR range and type
  131. * returned corresponds only to [start:*partial_end]. Caller has
  132. * to lookup again for [*partial_end:end].
  133. *
  134. * uniform - Set to 1 when an MTRR covers the region uniformly, i.e. the
  135. * region is fully covered by a single MTRR entry or the default
  136. * type.
  137. */
  138. static u8 mtrr_type_lookup_variable(u64 start, u64 end, u64 *partial_end,
  139. int *repeat, u8 *uniform)
  140. {
  141. int i;
  142. u64 base, mask;
  143. u8 prev_match, curr_match;
  144. *repeat = 0;
  145. *uniform = 1;
  146. prev_match = MTRR_TYPE_INVALID;
  147. for (i = 0; i < num_var_ranges; ++i) {
  148. unsigned short start_state, end_state, inclusive;
  149. if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11)))
  150. continue;
  151. base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) +
  152. (mtrr_state.var_ranges[i].base_lo & PAGE_MASK);
  153. mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) +
  154. (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK);
  155. start_state = ((start & mask) == (base & mask));
  156. end_state = ((end & mask) == (base & mask));
  157. inclusive = ((start < base) && (end > base));
  158. if ((start_state != end_state) || inclusive) {
  159. /*
  160. * We have start:end spanning across an MTRR.
  161. * We split the region into either
  162. *
  163. * - start_state:1
  164. * (start:mtrr_end)(mtrr_end:end)
  165. * - end_state:1
  166. * (start:mtrr_start)(mtrr_start:end)
  167. * - inclusive:1
  168. * (start:mtrr_start)(mtrr_start:mtrr_end)(mtrr_end:end)
  169. *
  170. * depending on kind of overlap.
  171. *
  172. * Return the type of the first region and a pointer
  173. * to the start of next region so that caller will be
  174. * advised to lookup again after having adjusted start
  175. * and end.
  176. *
  177. * Note: This way we handle overlaps with multiple
  178. * entries and the default type properly.
  179. */
  180. if (start_state)
  181. *partial_end = base + get_mtrr_size(mask);
  182. else
  183. *partial_end = base;
  184. if (unlikely(*partial_end <= start)) {
  185. WARN_ON(1);
  186. *partial_end = start + PAGE_SIZE;
  187. }
  188. end = *partial_end - 1; /* end is inclusive */
  189. *repeat = 1;
  190. *uniform = 0;
  191. }
  192. if ((start & mask) != (base & mask))
  193. continue;
  194. curr_match = mtrr_state.var_ranges[i].base_lo & 0xff;
  195. if (prev_match == MTRR_TYPE_INVALID) {
  196. prev_match = curr_match;
  197. continue;
  198. }
  199. *uniform = 0;
  200. if (check_type_overlap(&prev_match, &curr_match))
  201. return curr_match;
  202. }
  203. if (prev_match != MTRR_TYPE_INVALID)
  204. return prev_match;
  205. return mtrr_state.def_type;
  206. }
  207. /**
  208. * mtrr_type_lookup - look up memory type in MTRR
  209. *
  210. * Return Values:
  211. * MTRR_TYPE_(type) - The effective MTRR type for the region
  212. * MTRR_TYPE_INVALID - MTRR is disabled
  213. *
  214. * Output Argument:
  215. * uniform - Set to 1 when an MTRR covers the region uniformly, i.e. the
  216. * region is fully covered by a single MTRR entry or the default
  217. * type.
  218. */
  219. u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
  220. {
  221. u8 type, prev_type, is_uniform = 1, dummy;
  222. int repeat;
  223. u64 partial_end;
  224. /* Make end inclusive instead of exclusive */
  225. end--;
  226. if (!mtrr_state_set)
  227. return MTRR_TYPE_INVALID;
  228. if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED))
  229. return MTRR_TYPE_INVALID;
  230. /*
  231. * Look up the fixed ranges first, which take priority over
  232. * the variable ranges.
  233. */
  234. if ((start < 0x100000) &&
  235. (mtrr_state.have_fixed) &&
  236. (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) {
  237. is_uniform = 0;
  238. type = mtrr_type_lookup_fixed(start, end);
  239. goto out;
  240. }
  241. /*
  242. * Look up the variable ranges. Look of multiple ranges matching
  243. * this address and pick type as per MTRR precedence.
  244. */
  245. type = mtrr_type_lookup_variable(start, end, &partial_end,
  246. &repeat, &is_uniform);
  247. /*
  248. * Common path is with repeat = 0.
  249. * However, we can have cases where [start:end] spans across some
  250. * MTRR ranges and/or the default type. Do repeated lookups for
  251. * that case here.
  252. */
  253. while (repeat) {
  254. prev_type = type;
  255. start = partial_end;
  256. is_uniform = 0;
  257. type = mtrr_type_lookup_variable(start, end, &partial_end,
  258. &repeat, &dummy);
  259. if (check_type_overlap(&prev_type, &type))
  260. goto out;
  261. }
  262. if (mtrr_tom2 && (start >= (1ULL<<32)) && (end < mtrr_tom2))
  263. type = MTRR_TYPE_WRBACK;
  264. out:
  265. *uniform = is_uniform;
  266. return type;
  267. }
  268. /* Get the MSR pair relating to a var range */
  269. static void
  270. get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
  271. {
  272. rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
  273. rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
  274. }
  275. /* Fill the MSR pair relating to a var range */
  276. void fill_mtrr_var_range(unsigned int index,
  277. u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
  278. {
  279. struct mtrr_var_range *vr;
  280. vr = mtrr_state.var_ranges;
  281. vr[index].base_lo = base_lo;
  282. vr[index].base_hi = base_hi;
  283. vr[index].mask_lo = mask_lo;
  284. vr[index].mask_hi = mask_hi;
  285. }
  286. static void get_fixed_ranges(mtrr_type *frs)
  287. {
  288. unsigned int *p = (unsigned int *)frs;
  289. int i;
  290. k8_check_syscfg_dram_mod_en();
  291. rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
  292. for (i = 0; i < 2; i++)
  293. rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
  294. for (i = 0; i < 8; i++)
  295. rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
  296. }
  297. void mtrr_save_fixed_ranges(void *info)
  298. {
  299. if (boot_cpu_has(X86_FEATURE_MTRR))
  300. get_fixed_ranges(mtrr_state.fixed_ranges);
  301. }
  302. static unsigned __initdata last_fixed_start;
  303. static unsigned __initdata last_fixed_end;
  304. static mtrr_type __initdata last_fixed_type;
  305. static void __init print_fixed_last(void)
  306. {
  307. if (!last_fixed_end)
  308. return;
  309. pr_debug(" %05X-%05X %s\n", last_fixed_start,
  310. last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
  311. last_fixed_end = 0;
  312. }
  313. static void __init update_fixed_last(unsigned base, unsigned end,
  314. mtrr_type type)
  315. {
  316. last_fixed_start = base;
  317. last_fixed_end = end;
  318. last_fixed_type = type;
  319. }
  320. static void __init
  321. print_fixed(unsigned base, unsigned step, const mtrr_type *types)
  322. {
  323. unsigned i;
  324. for (i = 0; i < 8; ++i, ++types, base += step) {
  325. if (last_fixed_end == 0) {
  326. update_fixed_last(base, base + step, *types);
  327. continue;
  328. }
  329. if (last_fixed_end == base && last_fixed_type == *types) {
  330. last_fixed_end = base + step;
  331. continue;
  332. }
  333. /* new segments: gap or different type */
  334. print_fixed_last();
  335. update_fixed_last(base, base + step, *types);
  336. }
  337. }
  338. static void prepare_set(void);
  339. static void post_set(void);
  340. static void __init print_mtrr_state(void)
  341. {
  342. unsigned int i;
  343. int high_width;
  344. pr_debug("MTRR default type: %s\n",
  345. mtrr_attrib_to_str(mtrr_state.def_type));
  346. if (mtrr_state.have_fixed) {
  347. pr_debug("MTRR fixed ranges %sabled:\n",
  348. ((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) &&
  349. (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ?
  350. "en" : "dis");
  351. print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
  352. for (i = 0; i < 2; ++i)
  353. print_fixed(0x80000 + i * 0x20000, 0x04000,
  354. mtrr_state.fixed_ranges + (i + 1) * 8);
  355. for (i = 0; i < 8; ++i)
  356. print_fixed(0xC0000 + i * 0x08000, 0x01000,
  357. mtrr_state.fixed_ranges + (i + 3) * 8);
  358. /* tail */
  359. print_fixed_last();
  360. }
  361. pr_debug("MTRR variable ranges %sabled:\n",
  362. mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis");
  363. high_width = (__ffs64(size_or_mask) - (32 - PAGE_SHIFT) + 3) / 4;
  364. for (i = 0; i < num_var_ranges; ++i) {
  365. if (mtrr_state.var_ranges[i].mask_lo & (1 << 11))
  366. pr_debug(" %u base %0*X%05X000 mask %0*X%05X000 %s\n",
  367. i,
  368. high_width,
  369. mtrr_state.var_ranges[i].base_hi,
  370. mtrr_state.var_ranges[i].base_lo >> 12,
  371. high_width,
  372. mtrr_state.var_ranges[i].mask_hi,
  373. mtrr_state.var_ranges[i].mask_lo >> 12,
  374. mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff));
  375. else
  376. pr_debug(" %u disabled\n", i);
  377. }
  378. if (mtrr_tom2)
  379. pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
  380. }
  381. /* PAT setup for BP. We need to go through sync steps here */
  382. void __init mtrr_bp_pat_init(void)
  383. {
  384. unsigned long flags;
  385. local_irq_save(flags);
  386. prepare_set();
  387. pat_init();
  388. post_set();
  389. local_irq_restore(flags);
  390. }
  391. /* Grab all of the MTRR state for this CPU into *state */
  392. bool __init get_mtrr_state(void)
  393. {
  394. struct mtrr_var_range *vrs;
  395. unsigned lo, dummy;
  396. unsigned int i;
  397. vrs = mtrr_state.var_ranges;
  398. rdmsr(MSR_MTRRcap, lo, dummy);
  399. mtrr_state.have_fixed = (lo >> 8) & 1;
  400. for (i = 0; i < num_var_ranges; i++)
  401. get_mtrr_var_range(i, &vrs[i]);
  402. if (mtrr_state.have_fixed)
  403. get_fixed_ranges(mtrr_state.fixed_ranges);
  404. rdmsr(MSR_MTRRdefType, lo, dummy);
  405. mtrr_state.def_type = (lo & 0xff);
  406. mtrr_state.enabled = (lo & 0xc00) >> 10;
  407. if (amd_special_default_mtrr()) {
  408. unsigned low, high;
  409. /* TOP_MEM2 */
  410. rdmsr(MSR_K8_TOP_MEM2, low, high);
  411. mtrr_tom2 = high;
  412. mtrr_tom2 <<= 32;
  413. mtrr_tom2 |= low;
  414. mtrr_tom2 &= 0xffffff800000ULL;
  415. }
  416. print_mtrr_state();
  417. mtrr_state_set = 1;
  418. return !!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED);
  419. }
  420. /* Some BIOS's are messed up and don't set all MTRRs the same! */
  421. void __init mtrr_state_warn(void)
  422. {
  423. unsigned long mask = smp_changes_mask;
  424. if (!mask)
  425. return;
  426. if (mask & MTRR_CHANGE_MASK_FIXED)
  427. pr_warn("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
  428. if (mask & MTRR_CHANGE_MASK_VARIABLE)
  429. pr_warn("mtrr: your CPUs had inconsistent variable MTRR settings\n");
  430. if (mask & MTRR_CHANGE_MASK_DEFTYPE)
  431. pr_warn("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
  432. pr_info("mtrr: probably your BIOS does not setup all CPUs.\n");
  433. pr_info("mtrr: corrected configuration.\n");
  434. }
  435. /*
  436. * Doesn't attempt to pass an error out to MTRR users
  437. * because it's quite complicated in some cases and probably not
  438. * worth it because the best error handling is to ignore it.
  439. */
  440. void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
  441. {
  442. if (wrmsr_safe(msr, a, b) < 0) {
  443. pr_err("MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
  444. smp_processor_id(), msr, a, b);
  445. }
  446. }
  447. /**
  448. * set_fixed_range - checks & updates a fixed-range MTRR if it
  449. * differs from the value it should have
  450. * @msr: MSR address of the MTTR which should be checked and updated
  451. * @changed: pointer which indicates whether the MTRR needed to be changed
  452. * @msrwords: pointer to the MSR values which the MSR should have
  453. */
  454. static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
  455. {
  456. unsigned lo, hi;
  457. rdmsr(msr, lo, hi);
  458. if (lo != msrwords[0] || hi != msrwords[1]) {
  459. mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
  460. *changed = true;
  461. }
  462. }
  463. /**
  464. * generic_get_free_region - Get a free MTRR.
  465. * @base: The starting (base) address of the region.
  466. * @size: The size (in bytes) of the region.
  467. * @replace_reg: mtrr index to be replaced; set to invalid value if none.
  468. *
  469. * Returns: The index of the region on success, else negative on error.
  470. */
  471. int
  472. generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
  473. {
  474. unsigned long lbase, lsize;
  475. mtrr_type ltype;
  476. int i, max;
  477. max = num_var_ranges;
  478. if (replace_reg >= 0 && replace_reg < max)
  479. return replace_reg;
  480. for (i = 0; i < max; ++i) {
  481. mtrr_if->get(i, &lbase, &lsize, &ltype);
  482. if (lsize == 0)
  483. return i;
  484. }
  485. return -ENOSPC;
  486. }
  487. static void generic_get_mtrr(unsigned int reg, unsigned long *base,
  488. unsigned long *size, mtrr_type *type)
  489. {
  490. u32 mask_lo, mask_hi, base_lo, base_hi;
  491. unsigned int hi;
  492. u64 tmp, mask;
  493. /*
  494. * get_mtrr doesn't need to update mtrr_state, also it could be called
  495. * from any cpu, so try to print it out directly.
  496. */
  497. get_cpu();
  498. rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
  499. if ((mask_lo & 0x800) == 0) {
  500. /* Invalid (i.e. free) range */
  501. *base = 0;
  502. *size = 0;
  503. *type = 0;
  504. goto out_put_cpu;
  505. }
  506. rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
  507. /* Work out the shifted address mask: */
  508. tmp = (u64)mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT;
  509. mask = size_or_mask | tmp;
  510. /* Expand tmp with high bits to all 1s: */
  511. hi = fls64(tmp);
  512. if (hi > 0) {
  513. tmp |= ~((1ULL<<(hi - 1)) - 1);
  514. if (tmp != mask) {
  515. pr_warn("mtrr: your BIOS has configured an incorrect mask, fixing it.\n");
  516. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  517. mask = tmp;
  518. }
  519. }
  520. /*
  521. * This works correctly if size is a power of two, i.e. a
  522. * contiguous range:
  523. */
  524. *size = -mask;
  525. *base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
  526. *type = base_lo & 0xff;
  527. out_put_cpu:
  528. put_cpu();
  529. }
  530. /**
  531. * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
  532. * differ from the saved set
  533. * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
  534. */
  535. static int set_fixed_ranges(mtrr_type *frs)
  536. {
  537. unsigned long long *saved = (unsigned long long *)frs;
  538. bool changed = false;
  539. int block = -1, range;
  540. k8_check_syscfg_dram_mod_en();
  541. while (fixed_range_blocks[++block].ranges) {
  542. for (range = 0; range < fixed_range_blocks[block].ranges; range++)
  543. set_fixed_range(fixed_range_blocks[block].base_msr + range,
  544. &changed, (unsigned int *)saved++);
  545. }
  546. return changed;
  547. }
  548. /*
  549. * Set the MSR pair relating to a var range.
  550. * Returns true if changes are made.
  551. */
  552. static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
  553. {
  554. unsigned int lo, hi;
  555. bool changed = false;
  556. rdmsr(MTRRphysBase_MSR(index), lo, hi);
  557. if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL)
  558. || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
  559. (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
  560. mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
  561. changed = true;
  562. }
  563. rdmsr(MTRRphysMask_MSR(index), lo, hi);
  564. if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL)
  565. || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
  566. (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
  567. mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
  568. changed = true;
  569. }
  570. return changed;
  571. }
  572. static u32 deftype_lo, deftype_hi;
  573. /**
  574. * set_mtrr_state - Set the MTRR state for this CPU.
  575. *
  576. * NOTE: The CPU must already be in a safe state for MTRR changes.
  577. * RETURNS: 0 if no changes made, else a mask indicating what was changed.
  578. */
  579. static unsigned long set_mtrr_state(void)
  580. {
  581. unsigned long change_mask = 0;
  582. unsigned int i;
  583. for (i = 0; i < num_var_ranges; i++) {
  584. if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
  585. change_mask |= MTRR_CHANGE_MASK_VARIABLE;
  586. }
  587. if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
  588. change_mask |= MTRR_CHANGE_MASK_FIXED;
  589. /*
  590. * Set_mtrr_restore restores the old value of MTRRdefType,
  591. * so to set it we fiddle with the saved value:
  592. */
  593. if ((deftype_lo & 0xff) != mtrr_state.def_type
  594. || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) {
  595. deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type |
  596. (mtrr_state.enabled << 10);
  597. change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
  598. }
  599. return change_mask;
  600. }
  601. static unsigned long cr4;
  602. static DEFINE_RAW_SPINLOCK(set_atomicity_lock);
  603. /*
  604. * Since we are disabling the cache don't allow any interrupts,
  605. * they would run extremely slow and would only increase the pain.
  606. *
  607. * The caller must ensure that local interrupts are disabled and
  608. * are reenabled after post_set() has been called.
  609. */
  610. static void prepare_set(void) __acquires(set_atomicity_lock)
  611. {
  612. unsigned long cr0;
  613. /*
  614. * Note that this is not ideal
  615. * since the cache is only flushed/disabled for this CPU while the
  616. * MTRRs are changed, but changing this requires more invasive
  617. * changes to the way the kernel boots
  618. */
  619. raw_spin_lock(&set_atomicity_lock);
  620. /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
  621. cr0 = read_cr0() | X86_CR0_CD;
  622. write_cr0(cr0);
  623. /*
  624. * Cache flushing is the most time-consuming step when programming
  625. * the MTRRs. Fortunately, as per the Intel Software Development
  626. * Manual, we can skip it if the processor supports cache self-
  627. * snooping.
  628. */
  629. if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
  630. wbinvd();
  631. /* Save value of CR4 and clear Page Global Enable (bit 7) */
  632. if (boot_cpu_has(X86_FEATURE_PGE)) {
  633. cr4 = __read_cr4();
  634. __write_cr4(cr4 & ~X86_CR4_PGE);
  635. }
  636. /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */
  637. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  638. flush_tlb_local();
  639. /* Save MTRR state */
  640. rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  641. /* Disable MTRRs, and set the default type to uncached */
  642. mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
  643. /* Again, only flush caches if we have to. */
  644. if (!static_cpu_has(X86_FEATURE_SELFSNOOP))
  645. wbinvd();
  646. }
  647. static void post_set(void) __releases(set_atomicity_lock)
  648. {
  649. /* Flush TLBs (no need to flush caches - they are disabled) */
  650. count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
  651. flush_tlb_local();
  652. /* Intel (P6) standard MTRRs */
  653. mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  654. /* Enable caches */
  655. write_cr0(read_cr0() & ~X86_CR0_CD);
  656. /* Restore value of CR4 */
  657. if (boot_cpu_has(X86_FEATURE_PGE))
  658. __write_cr4(cr4);
  659. raw_spin_unlock(&set_atomicity_lock);
  660. }
  661. static void generic_set_all(void)
  662. {
  663. unsigned long mask, count;
  664. unsigned long flags;
  665. local_irq_save(flags);
  666. prepare_set();
  667. /* Actually set the state */
  668. mask = set_mtrr_state();
  669. /* also set PAT */
  670. pat_init();
  671. post_set();
  672. local_irq_restore(flags);
  673. /* Use the atomic bitops to update the global mask */
  674. for (count = 0; count < sizeof(mask) * 8; ++count) {
  675. if (mask & 0x01)
  676. set_bit(count, &smp_changes_mask);
  677. mask >>= 1;
  678. }
  679. }
  680. /**
  681. * generic_set_mtrr - set variable MTRR register on the local CPU.
  682. *
  683. * @reg: The register to set.
  684. * @base: The base address of the region.
  685. * @size: The size of the region. If this is 0 the region is disabled.
  686. * @type: The type of the region.
  687. *
  688. * Returns nothing.
  689. */
  690. static void generic_set_mtrr(unsigned int reg, unsigned long base,
  691. unsigned long size, mtrr_type type)
  692. {
  693. unsigned long flags;
  694. struct mtrr_var_range *vr;
  695. vr = &mtrr_state.var_ranges[reg];
  696. local_irq_save(flags);
  697. prepare_set();
  698. if (size == 0) {
  699. /*
  700. * The invalid bit is kept in the mask, so we simply
  701. * clear the relevant mask register to disable a range.
  702. */
  703. mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
  704. memset(vr, 0, sizeof(struct mtrr_var_range));
  705. } else {
  706. vr->base_lo = base << PAGE_SHIFT | type;
  707. vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT);
  708. vr->mask_lo = -size << PAGE_SHIFT | 0x800;
  709. vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT);
  710. mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
  711. mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
  712. }
  713. post_set();
  714. local_irq_restore(flags);
  715. }
  716. int generic_validate_add_page(unsigned long base, unsigned long size,
  717. unsigned int type)
  718. {
  719. unsigned long lbase, last;
  720. /*
  721. * For Intel PPro stepping <= 7
  722. * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
  723. */
  724. if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 &&
  725. boot_cpu_data.x86_model == 1 &&
  726. boot_cpu_data.x86_stepping <= 7) {
  727. if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
  728. pr_warn("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
  729. return -EINVAL;
  730. }
  731. if (!(base + size < 0x70000 || base > 0x7003F) &&
  732. (type == MTRR_TYPE_WRCOMB
  733. || type == MTRR_TYPE_WRBACK)) {
  734. pr_warn("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
  735. return -EINVAL;
  736. }
  737. }
  738. /*
  739. * Check upper bits of base and last are equal and lower bits are 0
  740. * for base and 1 for last
  741. */
  742. last = base + size - 1;
  743. for (lbase = base; !(lbase & 1) && (last & 1);
  744. lbase = lbase >> 1, last = last >> 1)
  745. ;
  746. if (lbase != last) {
  747. pr_warn("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
  748. return -EINVAL;
  749. }
  750. return 0;
  751. }
  752. static int generic_have_wrcomb(void)
  753. {
  754. unsigned long config, dummy;
  755. rdmsr(MSR_MTRRcap, config, dummy);
  756. return config & (1 << 10);
  757. }
  758. int positive_have_wrcomb(void)
  759. {
  760. return 1;
  761. }
  762. /*
  763. * Generic structure...
  764. */
  765. const struct mtrr_ops generic_mtrr_ops = {
  766. .use_intel_if = 1,
  767. .set_all = generic_set_all,
  768. .get = generic_get_mtrr,
  769. .get_free_region = generic_get_free_region,
  770. .set = generic_set_mtrr,
  771. .validate_add_page = generic_validate_add_page,
  772. .have_wrcomb = generic_have_wrcomb,
  773. };