generic.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 <linux/cc_platform.h>
  11. #include <asm/processor-flags.h>
  12. #include <asm/cacheinfo.h>
  13. #include <asm/cpufeature.h>
  14. #include <asm/hypervisor.h>
  15. #include <asm/mshyperv.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/msr.h>
  19. #include <asm/memtype.h>
  20. #include "mtrr.h"
  21. struct fixed_range_block {
  22. int base_msr; /* start address of an MTRR block */
  23. int ranges; /* number of MTRRs in this block */
  24. };
  25. static struct fixed_range_block fixed_range_blocks[] = {
  26. { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */
  27. { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */
  28. { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */
  29. {}
  30. };
  31. struct cache_map {
  32. u64 start;
  33. u64 end;
  34. u64 flags;
  35. u64 type:8;
  36. u64 fixed:1;
  37. };
  38. bool mtrr_debug;
  39. static int __init mtrr_param_setup(char *str)
  40. {
  41. int rc = 0;
  42. if (!str)
  43. return -EINVAL;
  44. if (!strcmp(str, "debug"))
  45. mtrr_debug = true;
  46. else
  47. rc = -EINVAL;
  48. return rc;
  49. }
  50. early_param("mtrr", mtrr_param_setup);
  51. /*
  52. * CACHE_MAP_MAX is the maximum number of memory ranges in cache_map, where
  53. * no 2 adjacent ranges have the same cache mode (those would be merged).
  54. * The number is based on the worst case:
  55. * - no two adjacent fixed MTRRs share the same cache mode
  56. * - one variable MTRR is spanning a huge area with mode WB
  57. * - 255 variable MTRRs with mode UC all overlap with the WB MTRR, creating 2
  58. * additional ranges each (result like "ababababa...aba" with a = WB, b = UC),
  59. * accounting for MTRR_MAX_VAR_RANGES * 2 - 1 range entries
  60. * - a TOP_MEM2 area (even with overlapping an UC MTRR can't add 2 range entries
  61. * to the possible maximum, as it always starts at 4GB, thus it can't be in
  62. * the middle of that MTRR, unless that MTRR starts at 0, which would remove
  63. * the initial "a" from the "abababa" pattern above)
  64. * The map won't contain ranges with no matching MTRR (those fall back to the
  65. * default cache mode).
  66. */
  67. #define CACHE_MAP_MAX (MTRR_NUM_FIXED_RANGES + MTRR_MAX_VAR_RANGES * 2)
  68. static struct cache_map init_cache_map[CACHE_MAP_MAX] __initdata;
  69. static struct cache_map *cache_map __refdata = init_cache_map;
  70. static unsigned int cache_map_size = CACHE_MAP_MAX;
  71. static unsigned int cache_map_n;
  72. static unsigned int cache_map_fixed;
  73. static unsigned long smp_changes_mask;
  74. static int mtrr_state_set;
  75. u64 mtrr_tom2;
  76. struct mtrr_state_type mtrr_state;
  77. EXPORT_SYMBOL_GPL(mtrr_state);
  78. /* Reserved bits in the high portion of the MTRRphysBaseN MSR. */
  79. u32 phys_hi_rsvd;
  80. /*
  81. * BIOS is expected to clear MtrrFixDramModEn bit, see for example
  82. * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
  83. * Opteron Processors" (26094 Rev. 3.30 February 2006), section
  84. * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
  85. * to 1 during BIOS initialization of the fixed MTRRs, then cleared to
  86. * 0 for operation."
  87. */
  88. static inline void k8_check_syscfg_dram_mod_en(void)
  89. {
  90. u32 lo, hi;
  91. if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
  92. (boot_cpu_data.x86 >= 0x0f)))
  93. return;
  94. if (cc_platform_has(CC_ATTR_HOST_SEV_SNP))
  95. return;
  96. rdmsr(MSR_AMD64_SYSCFG, lo, hi);
  97. if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
  98. pr_err(FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
  99. " not cleared by BIOS, clearing this bit\n",
  100. smp_processor_id());
  101. lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
  102. mtrr_wrmsr(MSR_AMD64_SYSCFG, lo, hi);
  103. }
  104. }
  105. /* Get the size of contiguous MTRR range */
  106. static u64 get_mtrr_size(u64 mask)
  107. {
  108. u64 size;
  109. mask |= (u64)phys_hi_rsvd << 32;
  110. size = -mask;
  111. return size;
  112. }
  113. static u8 get_var_mtrr_state(unsigned int reg, u64 *start, u64 *size)
  114. {
  115. struct mtrr_var_range *mtrr = mtrr_state.var_ranges + reg;
  116. if (!(mtrr->mask_lo & MTRR_PHYSMASK_V))
  117. return MTRR_TYPE_INVALID;
  118. *start = (((u64)mtrr->base_hi) << 32) + (mtrr->base_lo & PAGE_MASK);
  119. *size = get_mtrr_size((((u64)mtrr->mask_hi) << 32) +
  120. (mtrr->mask_lo & PAGE_MASK));
  121. return mtrr->base_lo & MTRR_PHYSBASE_TYPE;
  122. }
  123. static u8 get_effective_type(u8 type1, u8 type2)
  124. {
  125. if (type1 == MTRR_TYPE_UNCACHABLE || type2 == MTRR_TYPE_UNCACHABLE)
  126. return MTRR_TYPE_UNCACHABLE;
  127. if ((type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH) ||
  128. (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK))
  129. return MTRR_TYPE_WRTHROUGH;
  130. if (type1 != type2)
  131. return MTRR_TYPE_UNCACHABLE;
  132. return type1;
  133. }
  134. static void rm_map_entry_at(int idx)
  135. {
  136. cache_map_n--;
  137. if (cache_map_n > idx) {
  138. memmove(cache_map + idx, cache_map + idx + 1,
  139. sizeof(*cache_map) * (cache_map_n - idx));
  140. }
  141. }
  142. /*
  143. * Add an entry into cache_map at a specific index. Merges adjacent entries if
  144. * appropriate. Return the number of merges for correcting the scan index
  145. * (this is needed as merging will reduce the number of entries, which will
  146. * result in skipping entries in future iterations if the scan index isn't
  147. * corrected).
  148. * Note that the corrected index can never go below -1 (resulting in being 0 in
  149. * the next scan iteration), as "2" is returned only if the current index is
  150. * larger than zero.
  151. */
  152. static int add_map_entry_at(u64 start, u64 end, u8 type, int idx)
  153. {
  154. bool merge_prev = false, merge_next = false;
  155. if (start >= end)
  156. return 0;
  157. if (idx > 0) {
  158. struct cache_map *prev = cache_map + idx - 1;
  159. if (!prev->fixed && start == prev->end && type == prev->type)
  160. merge_prev = true;
  161. }
  162. if (idx < cache_map_n) {
  163. struct cache_map *next = cache_map + idx;
  164. if (!next->fixed && end == next->start && type == next->type)
  165. merge_next = true;
  166. }
  167. if (merge_prev && merge_next) {
  168. cache_map[idx - 1].end = cache_map[idx].end;
  169. rm_map_entry_at(idx);
  170. return 2;
  171. }
  172. if (merge_prev) {
  173. cache_map[idx - 1].end = end;
  174. return 1;
  175. }
  176. if (merge_next) {
  177. cache_map[idx].start = start;
  178. return 1;
  179. }
  180. /* Sanity check: the array should NEVER be too small! */
  181. if (cache_map_n == cache_map_size) {
  182. WARN(1, "MTRR cache mode memory map exhausted!\n");
  183. cache_map_n = cache_map_fixed;
  184. return 0;
  185. }
  186. if (cache_map_n > idx) {
  187. memmove(cache_map + idx + 1, cache_map + idx,
  188. sizeof(*cache_map) * (cache_map_n - idx));
  189. }
  190. cache_map[idx].start = start;
  191. cache_map[idx].end = end;
  192. cache_map[idx].type = type;
  193. cache_map[idx].fixed = 0;
  194. cache_map_n++;
  195. return 0;
  196. }
  197. /* Clear a part of an entry. Return 1 if start of entry is still valid. */
  198. static int clr_map_range_at(u64 start, u64 end, int idx)
  199. {
  200. int ret = start != cache_map[idx].start;
  201. u64 tmp;
  202. if (start == cache_map[idx].start && end == cache_map[idx].end) {
  203. rm_map_entry_at(idx);
  204. } else if (start == cache_map[idx].start) {
  205. cache_map[idx].start = end;
  206. } else if (end == cache_map[idx].end) {
  207. cache_map[idx].end = start;
  208. } else {
  209. tmp = cache_map[idx].end;
  210. cache_map[idx].end = start;
  211. add_map_entry_at(end, tmp, cache_map[idx].type, idx + 1);
  212. }
  213. return ret;
  214. }
  215. /*
  216. * Add MTRR to the map. The current map is scanned and each part of the MTRR
  217. * either overlapping with an existing entry or with a hole in the map is
  218. * handled separately.
  219. */
  220. static void add_map_entry(u64 start, u64 end, u8 type)
  221. {
  222. u8 new_type, old_type;
  223. u64 tmp;
  224. int i;
  225. for (i = 0; i < cache_map_n && start < end; i++) {
  226. if (start >= cache_map[i].end)
  227. continue;
  228. if (start < cache_map[i].start) {
  229. /* Region start has no overlap. */
  230. tmp = min(end, cache_map[i].start);
  231. i -= add_map_entry_at(start, tmp, type, i);
  232. start = tmp;
  233. continue;
  234. }
  235. new_type = get_effective_type(type, cache_map[i].type);
  236. old_type = cache_map[i].type;
  237. if (cache_map[i].fixed || new_type == old_type) {
  238. /* Cut off start of new entry. */
  239. start = cache_map[i].end;
  240. continue;
  241. }
  242. /* Handle only overlapping part of region. */
  243. tmp = min(end, cache_map[i].end);
  244. i += clr_map_range_at(start, tmp, i);
  245. i -= add_map_entry_at(start, tmp, new_type, i);
  246. start = tmp;
  247. }
  248. /* Add rest of region after last map entry (rest might be empty). */
  249. add_map_entry_at(start, end, type, i);
  250. }
  251. /* Add variable MTRRs to cache map. */
  252. static void map_add_var(void)
  253. {
  254. u64 start, size;
  255. unsigned int i;
  256. u8 type;
  257. /*
  258. * Add AMD TOP_MEM2 area. Can't be added in mtrr_build_map(), as it
  259. * needs to be added again when rebuilding the map due to potentially
  260. * having moved as a result of variable MTRRs for memory below 4GB.
  261. */
  262. if (mtrr_tom2) {
  263. add_map_entry(BIT_ULL(32), mtrr_tom2, MTRR_TYPE_WRBACK);
  264. cache_map[cache_map_n - 1].fixed = 1;
  265. }
  266. for (i = 0; i < num_var_ranges; i++) {
  267. type = get_var_mtrr_state(i, &start, &size);
  268. if (type != MTRR_TYPE_INVALID)
  269. add_map_entry(start, start + size, type);
  270. }
  271. }
  272. /*
  273. * Rebuild map by replacing variable entries. Needs to be called when MTRR
  274. * registers are being changed after boot, as such changes could include
  275. * removals of registers, which are complicated to handle without rebuild of
  276. * the map.
  277. */
  278. void generic_rebuild_map(void)
  279. {
  280. if (mtrr_if != &generic_mtrr_ops)
  281. return;
  282. cache_map_n = cache_map_fixed;
  283. map_add_var();
  284. }
  285. static unsigned int __init get_cache_map_size(void)
  286. {
  287. return cache_map_fixed + 2 * num_var_ranges + (mtrr_tom2 != 0);
  288. }
  289. /* Build the cache_map containing the cache modes per memory range. */
  290. void __init mtrr_build_map(void)
  291. {
  292. u64 start, end, size;
  293. unsigned int i;
  294. u8 type;
  295. /* Add fixed MTRRs, optimize for adjacent entries with same type. */
  296. if (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED) {
  297. /*
  298. * Start with 64k size fixed entries, preset 1st one (hence the
  299. * loop below is starting with index 1).
  300. */
  301. start = 0;
  302. end = size = 0x10000;
  303. type = mtrr_state.fixed_ranges[0];
  304. for (i = 1; i < MTRR_NUM_FIXED_RANGES; i++) {
  305. /* 8 64k entries, then 16 16k ones, rest 4k. */
  306. if (i == 8 || i == 24)
  307. size >>= 2;
  308. if (mtrr_state.fixed_ranges[i] != type) {
  309. add_map_entry(start, end, type);
  310. start = end;
  311. type = mtrr_state.fixed_ranges[i];
  312. }
  313. end += size;
  314. }
  315. add_map_entry(start, end, type);
  316. }
  317. /* Mark fixed, they take precedence. */
  318. for (i = 0; i < cache_map_n; i++)
  319. cache_map[i].fixed = 1;
  320. cache_map_fixed = cache_map_n;
  321. map_add_var();
  322. pr_info("MTRR map: %u entries (%u fixed + %u variable; max %u), built from %u variable MTRRs\n",
  323. cache_map_n, cache_map_fixed, cache_map_n - cache_map_fixed,
  324. get_cache_map_size(), num_var_ranges + (mtrr_tom2 != 0));
  325. if (mtrr_debug) {
  326. for (i = 0; i < cache_map_n; i++) {
  327. pr_info("%3u: %016llx-%016llx %s\n", i,
  328. cache_map[i].start, cache_map[i].end - 1,
  329. mtrr_attrib_to_str(cache_map[i].type));
  330. }
  331. }
  332. }
  333. /* Copy the cache_map from __initdata memory to dynamically allocated one. */
  334. void __init mtrr_copy_map(void)
  335. {
  336. unsigned int new_size = get_cache_map_size();
  337. if (!mtrr_state.enabled || !new_size) {
  338. cache_map = NULL;
  339. return;
  340. }
  341. mutex_lock(&mtrr_mutex);
  342. cache_map = kcalloc(new_size, sizeof(*cache_map), GFP_KERNEL);
  343. if (cache_map) {
  344. memmove(cache_map, init_cache_map,
  345. cache_map_n * sizeof(*cache_map));
  346. cache_map_size = new_size;
  347. } else {
  348. mtrr_state.enabled = 0;
  349. pr_err("MTRRs disabled due to allocation failure for lookup map.\n");
  350. }
  351. mutex_unlock(&mtrr_mutex);
  352. }
  353. /**
  354. * guest_force_mtrr_state - set static MTRR state for a guest
  355. *
  356. * Used to set MTRR state via different means (e.g. with data obtained from
  357. * a hypervisor).
  358. * Is allowed only for special cases when running virtualized. Must be called
  359. * from the x86_init.hyper.init_platform() hook. It can be called only once.
  360. * The MTRR state can't be changed afterwards. To ensure that, X86_FEATURE_MTRR
  361. * is cleared.
  362. *
  363. * @var: MTRR variable range array to use
  364. * @num_var: length of the @var array
  365. * @def_type: default caching type
  366. */
  367. void guest_force_mtrr_state(struct mtrr_var_range *var, unsigned int num_var,
  368. mtrr_type def_type)
  369. {
  370. unsigned int i;
  371. /* Only allowed to be called once before mtrr_bp_init(). */
  372. if (WARN_ON_ONCE(mtrr_state_set))
  373. return;
  374. /* Only allowed when running virtualized. */
  375. if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
  376. return;
  377. /*
  378. * Only allowed for special virtualization cases:
  379. * - when running as Hyper-V, SEV-SNP guest using vTOM
  380. * - when running as Xen PV guest
  381. * - when running as SEV-SNP or TDX guest to avoid unnecessary
  382. * VMM communication/Virtualization exceptions (#VC, #VE)
  383. */
  384. if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) &&
  385. !hv_is_isolation_supported() &&
  386. !cpu_feature_enabled(X86_FEATURE_XENPV) &&
  387. !cpu_feature_enabled(X86_FEATURE_TDX_GUEST))
  388. return;
  389. /* Disable MTRR in order to disable MTRR modifications. */
  390. setup_clear_cpu_cap(X86_FEATURE_MTRR);
  391. if (var) {
  392. if (num_var > MTRR_MAX_VAR_RANGES) {
  393. pr_warn("Trying to overwrite MTRR state with %u variable entries\n",
  394. num_var);
  395. num_var = MTRR_MAX_VAR_RANGES;
  396. }
  397. for (i = 0; i < num_var; i++)
  398. mtrr_state.var_ranges[i] = var[i];
  399. num_var_ranges = num_var;
  400. }
  401. mtrr_state.def_type = def_type;
  402. mtrr_state.enabled |= MTRR_STATE_MTRR_ENABLED;
  403. mtrr_state_set = 1;
  404. }
  405. static u8 type_merge(u8 type, u8 new_type, u8 *uniform)
  406. {
  407. u8 effective_type;
  408. if (type == MTRR_TYPE_INVALID)
  409. return new_type;
  410. effective_type = get_effective_type(type, new_type);
  411. if (type != effective_type)
  412. *uniform = 0;
  413. return effective_type;
  414. }
  415. /**
  416. * mtrr_type_lookup - look up memory type in MTRR
  417. *
  418. * @start: Begin of the physical address range
  419. * @end: End of the physical address range
  420. * @uniform: output argument:
  421. * - 1: the returned MTRR type is valid for the whole region
  422. * - 0: otherwise
  423. *
  424. * Return Values:
  425. * MTRR_TYPE_(type) - The effective MTRR type for the region
  426. * MTRR_TYPE_INVALID - MTRR is disabled
  427. */
  428. u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
  429. {
  430. u8 type = MTRR_TYPE_INVALID;
  431. unsigned int i;
  432. if (!mtrr_state_set) {
  433. /* Uniformity is unknown. */
  434. *uniform = 0;
  435. return MTRR_TYPE_UNCACHABLE;
  436. }
  437. *uniform = 1;
  438. if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED))
  439. return MTRR_TYPE_UNCACHABLE;
  440. for (i = 0; i < cache_map_n && start < end; i++) {
  441. /* Region after current map entry? -> continue with next one. */
  442. if (start >= cache_map[i].end)
  443. continue;
  444. /* Start of region not covered by current map entry? */
  445. if (start < cache_map[i].start) {
  446. /* At least some part of region has default type. */
  447. type = type_merge(type, mtrr_state.def_type, uniform);
  448. /* End of region not covered, too? -> lookup done. */
  449. if (end <= cache_map[i].start)
  450. return type;
  451. }
  452. /* At least part of region covered by map entry. */
  453. type = type_merge(type, cache_map[i].type, uniform);
  454. start = cache_map[i].end;
  455. }
  456. /* End of region past last entry in map? -> use default type. */
  457. if (start < end)
  458. type = type_merge(type, mtrr_state.def_type, uniform);
  459. return type;
  460. }
  461. /* Get the MSR pair relating to a var range */
  462. static void
  463. get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
  464. {
  465. rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
  466. rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
  467. }
  468. /* Fill the MSR pair relating to a var range */
  469. void fill_mtrr_var_range(unsigned int index,
  470. u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
  471. {
  472. struct mtrr_var_range *vr;
  473. vr = mtrr_state.var_ranges;
  474. vr[index].base_lo = base_lo;
  475. vr[index].base_hi = base_hi;
  476. vr[index].mask_lo = mask_lo;
  477. vr[index].mask_hi = mask_hi;
  478. }
  479. static void get_fixed_ranges(mtrr_type *frs)
  480. {
  481. unsigned int *p = (unsigned int *)frs;
  482. int i;
  483. k8_check_syscfg_dram_mod_en();
  484. rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
  485. for (i = 0; i < 2; i++)
  486. rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
  487. for (i = 0; i < 8; i++)
  488. rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
  489. }
  490. void mtrr_save_fixed_ranges(void *info)
  491. {
  492. if (mtrr_state.have_fixed)
  493. get_fixed_ranges(mtrr_state.fixed_ranges);
  494. }
  495. static unsigned __initdata last_fixed_start;
  496. static unsigned __initdata last_fixed_end;
  497. static mtrr_type __initdata last_fixed_type;
  498. static void __init print_fixed_last(void)
  499. {
  500. if (!last_fixed_end)
  501. return;
  502. pr_info(" %05X-%05X %s\n", last_fixed_start,
  503. last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
  504. last_fixed_end = 0;
  505. }
  506. static void __init update_fixed_last(unsigned base, unsigned end,
  507. mtrr_type type)
  508. {
  509. last_fixed_start = base;
  510. last_fixed_end = end;
  511. last_fixed_type = type;
  512. }
  513. static void __init
  514. print_fixed(unsigned base, unsigned step, const mtrr_type *types)
  515. {
  516. unsigned i;
  517. for (i = 0; i < 8; ++i, ++types, base += step) {
  518. if (last_fixed_end == 0) {
  519. update_fixed_last(base, base + step, *types);
  520. continue;
  521. }
  522. if (last_fixed_end == base && last_fixed_type == *types) {
  523. last_fixed_end = base + step;
  524. continue;
  525. }
  526. /* new segments: gap or different type */
  527. print_fixed_last();
  528. update_fixed_last(base, base + step, *types);
  529. }
  530. }
  531. static void __init print_mtrr_state(void)
  532. {
  533. unsigned int i;
  534. int high_width;
  535. pr_info("MTRR default type: %s\n",
  536. mtrr_attrib_to_str(mtrr_state.def_type));
  537. if (mtrr_state.have_fixed) {
  538. pr_info("MTRR fixed ranges %sabled:\n",
  539. ((mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED) &&
  540. (mtrr_state.enabled & MTRR_STATE_MTRR_FIXED_ENABLED)) ?
  541. "en" : "dis");
  542. print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
  543. for (i = 0; i < 2; ++i)
  544. print_fixed(0x80000 + i * 0x20000, 0x04000,
  545. mtrr_state.fixed_ranges + (i + 1) * 8);
  546. for (i = 0; i < 8; ++i)
  547. print_fixed(0xC0000 + i * 0x08000, 0x01000,
  548. mtrr_state.fixed_ranges + (i + 3) * 8);
  549. /* tail */
  550. print_fixed_last();
  551. }
  552. pr_info("MTRR variable ranges %sabled:\n",
  553. mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED ? "en" : "dis");
  554. high_width = (boot_cpu_data.x86_phys_bits - (32 - PAGE_SHIFT) + 3) / 4;
  555. for (i = 0; i < num_var_ranges; ++i) {
  556. if (mtrr_state.var_ranges[i].mask_lo & MTRR_PHYSMASK_V)
  557. pr_info(" %u base %0*X%05X000 mask %0*X%05X000 %s\n",
  558. i,
  559. high_width,
  560. mtrr_state.var_ranges[i].base_hi,
  561. mtrr_state.var_ranges[i].base_lo >> 12,
  562. high_width,
  563. mtrr_state.var_ranges[i].mask_hi,
  564. mtrr_state.var_ranges[i].mask_lo >> 12,
  565. mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo &
  566. MTRR_PHYSBASE_TYPE));
  567. else
  568. pr_info(" %u disabled\n", i);
  569. }
  570. if (mtrr_tom2)
  571. pr_info("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
  572. }
  573. /* Grab all of the MTRR state for this CPU into *state */
  574. bool __init get_mtrr_state(void)
  575. {
  576. struct mtrr_var_range *vrs;
  577. unsigned lo, dummy;
  578. unsigned int i;
  579. vrs = mtrr_state.var_ranges;
  580. rdmsr(MSR_MTRRcap, lo, dummy);
  581. mtrr_state.have_fixed = lo & MTRR_CAP_FIX;
  582. for (i = 0; i < num_var_ranges; i++)
  583. get_mtrr_var_range(i, &vrs[i]);
  584. if (mtrr_state.have_fixed)
  585. get_fixed_ranges(mtrr_state.fixed_ranges);
  586. rdmsr(MSR_MTRRdefType, lo, dummy);
  587. mtrr_state.def_type = lo & MTRR_DEF_TYPE_TYPE;
  588. mtrr_state.enabled = (lo & MTRR_DEF_TYPE_ENABLE) >> MTRR_STATE_SHIFT;
  589. if (amd_special_default_mtrr()) {
  590. unsigned low, high;
  591. /* TOP_MEM2 */
  592. rdmsr(MSR_K8_TOP_MEM2, low, high);
  593. mtrr_tom2 = high;
  594. mtrr_tom2 <<= 32;
  595. mtrr_tom2 |= low;
  596. mtrr_tom2 &= 0xffffff800000ULL;
  597. }
  598. if (mtrr_debug)
  599. print_mtrr_state();
  600. mtrr_state_set = 1;
  601. return !!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED);
  602. }
  603. /* Some BIOS's are messed up and don't set all MTRRs the same! */
  604. void __init mtrr_state_warn(void)
  605. {
  606. unsigned long mask = smp_changes_mask;
  607. if (!mask)
  608. return;
  609. if (mask & MTRR_CHANGE_MASK_FIXED)
  610. pr_warn("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
  611. if (mask & MTRR_CHANGE_MASK_VARIABLE)
  612. pr_warn("mtrr: your CPUs had inconsistent variable MTRR settings\n");
  613. if (mask & MTRR_CHANGE_MASK_DEFTYPE)
  614. pr_warn("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
  615. pr_info("mtrr: probably your BIOS does not setup all CPUs.\n");
  616. pr_info("mtrr: corrected configuration.\n");
  617. }
  618. /*
  619. * Doesn't attempt to pass an error out to MTRR users
  620. * because it's quite complicated in some cases and probably not
  621. * worth it because the best error handling is to ignore it.
  622. */
  623. void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
  624. {
  625. if (wrmsr_safe(msr, a, b) < 0) {
  626. pr_err("MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
  627. smp_processor_id(), msr, a, b);
  628. }
  629. }
  630. /**
  631. * set_fixed_range - checks & updates a fixed-range MTRR if it
  632. * differs from the value it should have
  633. * @msr: MSR address of the MTTR which should be checked and updated
  634. * @changed: pointer which indicates whether the MTRR needed to be changed
  635. * @msrwords: pointer to the MSR values which the MSR should have
  636. */
  637. static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
  638. {
  639. unsigned lo, hi;
  640. rdmsr(msr, lo, hi);
  641. if (lo != msrwords[0] || hi != msrwords[1]) {
  642. mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
  643. *changed = true;
  644. }
  645. }
  646. /**
  647. * generic_get_free_region - Get a free MTRR.
  648. * @base: The starting (base) address of the region.
  649. * @size: The size (in bytes) of the region.
  650. * @replace_reg: mtrr index to be replaced; set to invalid value if none.
  651. *
  652. * Returns: The index of the region on success, else negative on error.
  653. */
  654. int
  655. generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
  656. {
  657. unsigned long lbase, lsize;
  658. mtrr_type ltype;
  659. int i, max;
  660. max = num_var_ranges;
  661. if (replace_reg >= 0 && replace_reg < max)
  662. return replace_reg;
  663. for (i = 0; i < max; ++i) {
  664. mtrr_if->get(i, &lbase, &lsize, &ltype);
  665. if (lsize == 0)
  666. return i;
  667. }
  668. return -ENOSPC;
  669. }
  670. static void generic_get_mtrr(unsigned int reg, unsigned long *base,
  671. unsigned long *size, mtrr_type *type)
  672. {
  673. u32 mask_lo, mask_hi, base_lo, base_hi;
  674. unsigned int hi;
  675. u64 tmp, mask;
  676. /*
  677. * get_mtrr doesn't need to update mtrr_state, also it could be called
  678. * from any cpu, so try to print it out directly.
  679. */
  680. get_cpu();
  681. rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
  682. if (!(mask_lo & MTRR_PHYSMASK_V)) {
  683. /* Invalid (i.e. free) range */
  684. *base = 0;
  685. *size = 0;
  686. *type = 0;
  687. goto out_put_cpu;
  688. }
  689. rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
  690. /* Work out the shifted address mask: */
  691. tmp = (u64)mask_hi << 32 | (mask_lo & PAGE_MASK);
  692. mask = (u64)phys_hi_rsvd << 32 | tmp;
  693. /* Expand tmp with high bits to all 1s: */
  694. hi = fls64(tmp);
  695. if (hi > 0) {
  696. tmp |= ~((1ULL<<(hi - 1)) - 1);
  697. if (tmp != mask) {
  698. pr_warn("mtrr: your BIOS has configured an incorrect mask, fixing it.\n");
  699. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  700. mask = tmp;
  701. }
  702. }
  703. /*
  704. * This works correctly if size is a power of two, i.e. a
  705. * contiguous range:
  706. */
  707. *size = -mask >> PAGE_SHIFT;
  708. *base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
  709. *type = base_lo & MTRR_PHYSBASE_TYPE;
  710. out_put_cpu:
  711. put_cpu();
  712. }
  713. /**
  714. * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
  715. * differ from the saved set
  716. * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
  717. */
  718. static int set_fixed_ranges(mtrr_type *frs)
  719. {
  720. unsigned long long *saved = (unsigned long long *)frs;
  721. bool changed = false;
  722. int block = -1, range;
  723. k8_check_syscfg_dram_mod_en();
  724. while (fixed_range_blocks[++block].ranges) {
  725. for (range = 0; range < fixed_range_blocks[block].ranges; range++)
  726. set_fixed_range(fixed_range_blocks[block].base_msr + range,
  727. &changed, (unsigned int *)saved++);
  728. }
  729. return changed;
  730. }
  731. /*
  732. * Set the MSR pair relating to a var range.
  733. * Returns true if changes are made.
  734. */
  735. static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
  736. {
  737. unsigned int lo, hi;
  738. bool changed = false;
  739. rdmsr(MTRRphysBase_MSR(index), lo, hi);
  740. if ((vr->base_lo & ~MTRR_PHYSBASE_RSVD) != (lo & ~MTRR_PHYSBASE_RSVD)
  741. || (vr->base_hi & ~phys_hi_rsvd) != (hi & ~phys_hi_rsvd)) {
  742. mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
  743. changed = true;
  744. }
  745. rdmsr(MTRRphysMask_MSR(index), lo, hi);
  746. if ((vr->mask_lo & ~MTRR_PHYSMASK_RSVD) != (lo & ~MTRR_PHYSMASK_RSVD)
  747. || (vr->mask_hi & ~phys_hi_rsvd) != (hi & ~phys_hi_rsvd)) {
  748. mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
  749. changed = true;
  750. }
  751. return changed;
  752. }
  753. static u32 deftype_lo, deftype_hi;
  754. /**
  755. * set_mtrr_state - Set the MTRR state for this CPU.
  756. *
  757. * NOTE: The CPU must already be in a safe state for MTRR changes, including
  758. * measures that only a single CPU can be active in set_mtrr_state() in
  759. * order to not be subject to races for usage of deftype_lo. This is
  760. * accomplished by taking cache_disable_lock.
  761. * RETURNS: 0 if no changes made, else a mask indicating what was changed.
  762. */
  763. static unsigned long set_mtrr_state(void)
  764. {
  765. unsigned long change_mask = 0;
  766. unsigned int i;
  767. for (i = 0; i < num_var_ranges; i++) {
  768. if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
  769. change_mask |= MTRR_CHANGE_MASK_VARIABLE;
  770. }
  771. if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
  772. change_mask |= MTRR_CHANGE_MASK_FIXED;
  773. /*
  774. * Set_mtrr_restore restores the old value of MTRRdefType,
  775. * so to set it we fiddle with the saved value:
  776. */
  777. if ((deftype_lo & MTRR_DEF_TYPE_TYPE) != mtrr_state.def_type ||
  778. ((deftype_lo & MTRR_DEF_TYPE_ENABLE) >> MTRR_STATE_SHIFT) != mtrr_state.enabled) {
  779. deftype_lo = (deftype_lo & MTRR_DEF_TYPE_DISABLE) |
  780. mtrr_state.def_type |
  781. (mtrr_state.enabled << MTRR_STATE_SHIFT);
  782. change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
  783. }
  784. return change_mask;
  785. }
  786. void mtrr_disable(void)
  787. {
  788. /* Save MTRR state */
  789. rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  790. /* Disable MTRRs, and set the default type to uncached */
  791. mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & MTRR_DEF_TYPE_DISABLE, deftype_hi);
  792. }
  793. void mtrr_enable(void)
  794. {
  795. /* Intel (P6) standard MTRRs */
  796. mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  797. }
  798. void mtrr_generic_set_state(void)
  799. {
  800. unsigned long mask, count;
  801. /* Actually set the state */
  802. mask = set_mtrr_state();
  803. /* Use the atomic bitops to update the global mask */
  804. for (count = 0; count < sizeof(mask) * 8; ++count) {
  805. if (mask & 0x01)
  806. set_bit(count, &smp_changes_mask);
  807. mask >>= 1;
  808. }
  809. }
  810. /**
  811. * generic_set_mtrr - set variable MTRR register on the local CPU.
  812. *
  813. * @reg: The register to set.
  814. * @base: The base address of the region.
  815. * @size: The size of the region. If this is 0 the region is disabled.
  816. * @type: The type of the region.
  817. *
  818. * Returns nothing.
  819. */
  820. static void generic_set_mtrr(unsigned int reg, unsigned long base,
  821. unsigned long size, mtrr_type type)
  822. {
  823. unsigned long flags;
  824. struct mtrr_var_range *vr;
  825. vr = &mtrr_state.var_ranges[reg];
  826. local_irq_save(flags);
  827. cache_disable();
  828. if (size == 0) {
  829. /*
  830. * The invalid bit is kept in the mask, so we simply
  831. * clear the relevant mask register to disable a range.
  832. */
  833. mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
  834. memset(vr, 0, sizeof(struct mtrr_var_range));
  835. } else {
  836. vr->base_lo = base << PAGE_SHIFT | type;
  837. vr->base_hi = (base >> (32 - PAGE_SHIFT)) & ~phys_hi_rsvd;
  838. vr->mask_lo = -size << PAGE_SHIFT | MTRR_PHYSMASK_V;
  839. vr->mask_hi = (-size >> (32 - PAGE_SHIFT)) & ~phys_hi_rsvd;
  840. mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
  841. mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
  842. }
  843. cache_enable();
  844. local_irq_restore(flags);
  845. }
  846. int generic_validate_add_page(unsigned long base, unsigned long size,
  847. unsigned int type)
  848. {
  849. unsigned long lbase, last;
  850. /*
  851. * For Intel PPro stepping <= 7
  852. * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
  853. */
  854. if (mtrr_if == &generic_mtrr_ops && boot_cpu_data.x86 == 6 &&
  855. boot_cpu_data.x86_model == 1 &&
  856. boot_cpu_data.x86_stepping <= 7) {
  857. if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
  858. pr_warn("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
  859. return -EINVAL;
  860. }
  861. if (!(base + size < 0x70000 || base > 0x7003F) &&
  862. (type == MTRR_TYPE_WRCOMB
  863. || type == MTRR_TYPE_WRBACK)) {
  864. pr_warn("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
  865. return -EINVAL;
  866. }
  867. }
  868. /*
  869. * Check upper bits of base and last are equal and lower bits are 0
  870. * for base and 1 for last
  871. */
  872. last = base + size - 1;
  873. for (lbase = base; !(lbase & 1) && (last & 1);
  874. lbase = lbase >> 1, last = last >> 1)
  875. ;
  876. if (lbase != last) {
  877. pr_warn("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
  878. return -EINVAL;
  879. }
  880. return 0;
  881. }
  882. static int generic_have_wrcomb(void)
  883. {
  884. unsigned long config, dummy;
  885. rdmsr(MSR_MTRRcap, config, dummy);
  886. return config & MTRR_CAP_WC;
  887. }
  888. int positive_have_wrcomb(void)
  889. {
  890. return 1;
  891. }
  892. /*
  893. * Generic structure...
  894. */
  895. const struct mtrr_ops generic_mtrr_ops = {
  896. .get = generic_get_mtrr,
  897. .get_free_region = generic_get_free_region,
  898. .set = generic_set_mtrr,
  899. .validate_add_page = generic_validate_add_page,
  900. .have_wrcomb = generic_have_wrcomb,
  901. };