generic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong
  3. * because MTRRs can span upto 40 bits (36bits on most modern x86)
  4. */
  5. #define DEBUG
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/mm.h>
  11. #include <asm/processor-flags.h>
  12. #include <asm/cpufeature.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/system.h>
  15. #include <asm/mtrr.h>
  16. #include <asm/msr.h>
  17. #include <asm/pat.h>
  18. #include "mtrr.h"
  19. struct fixed_range_block {
  20. int base_msr; /* start address of an MTRR block */
  21. int ranges; /* number of MTRRs in this block */
  22. };
  23. static struct fixed_range_block fixed_range_blocks[] = {
  24. { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */
  25. { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */
  26. { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */
  27. {}
  28. };
  29. static unsigned long smp_changes_mask;
  30. static int mtrr_state_set;
  31. u64 mtrr_tom2;
  32. struct mtrr_state_type mtrr_state;
  33. EXPORT_SYMBOL_GPL(mtrr_state);
  34. /*
  35. * BIOS is expected to clear MtrrFixDramModEn bit, see for example
  36. * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
  37. * Opteron Processors" (26094 Rev. 3.30 February 2006), section
  38. * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set
  39. * to 1 during BIOS initalization of the fixed MTRRs, then cleared to
  40. * 0 for operation."
  41. */
  42. static inline void k8_check_syscfg_dram_mod_en(void)
  43. {
  44. u32 lo, hi;
  45. if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
  46. (boot_cpu_data.x86 >= 0x0f)))
  47. return;
  48. rdmsr(MSR_K8_SYSCFG, lo, hi);
  49. if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) {
  50. printk(KERN_ERR FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]"
  51. " not cleared by BIOS, clearing this bit\n",
  52. smp_processor_id());
  53. lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY;
  54. mtrr_wrmsr(MSR_K8_SYSCFG, lo, hi);
  55. }
  56. }
  57. /*
  58. * Returns the effective MTRR type for the region
  59. * Error returns:
  60. * - 0xFE - when the range is "not entirely covered" by _any_ var range MTRR
  61. * - 0xFF - when MTRR is not enabled
  62. */
  63. u8 mtrr_type_lookup(u64 start, u64 end)
  64. {
  65. int i;
  66. u64 base, mask;
  67. u8 prev_match, curr_match;
  68. if (!mtrr_state_set)
  69. return 0xFF;
  70. if (!mtrr_state.enabled)
  71. return 0xFF;
  72. /* Make end inclusive end, instead of exclusive */
  73. end--;
  74. /* Look in fixed ranges. Just return the type as per start */
  75. if (mtrr_state.have_fixed && (start < 0x100000)) {
  76. int idx;
  77. if (start < 0x80000) {
  78. idx = 0;
  79. idx += (start >> 16);
  80. return mtrr_state.fixed_ranges[idx];
  81. } else if (start < 0xC0000) {
  82. idx = 1 * 8;
  83. idx += ((start - 0x80000) >> 14);
  84. return mtrr_state.fixed_ranges[idx];
  85. } else if (start < 0x1000000) {
  86. idx = 3 * 8;
  87. idx += ((start - 0xC0000) >> 12);
  88. return mtrr_state.fixed_ranges[idx];
  89. }
  90. }
  91. /*
  92. * Look in variable ranges
  93. * Look of multiple ranges matching this address and pick type
  94. * as per MTRR precedence
  95. */
  96. if (!(mtrr_state.enabled & 2))
  97. return mtrr_state.def_type;
  98. prev_match = 0xFF;
  99. for (i = 0; i < num_var_ranges; ++i) {
  100. unsigned short start_state, end_state;
  101. if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11)))
  102. continue;
  103. base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) +
  104. (mtrr_state.var_ranges[i].base_lo & PAGE_MASK);
  105. mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) +
  106. (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK);
  107. start_state = ((start & mask) == (base & mask));
  108. end_state = ((end & mask) == (base & mask));
  109. if (start_state != end_state)
  110. return 0xFE;
  111. if ((start & mask) != (base & mask))
  112. continue;
  113. curr_match = mtrr_state.var_ranges[i].base_lo & 0xff;
  114. if (prev_match == 0xFF) {
  115. prev_match = curr_match;
  116. continue;
  117. }
  118. if (prev_match == MTRR_TYPE_UNCACHABLE ||
  119. curr_match == MTRR_TYPE_UNCACHABLE) {
  120. return MTRR_TYPE_UNCACHABLE;
  121. }
  122. if ((prev_match == MTRR_TYPE_WRBACK &&
  123. curr_match == MTRR_TYPE_WRTHROUGH) ||
  124. (prev_match == MTRR_TYPE_WRTHROUGH &&
  125. curr_match == MTRR_TYPE_WRBACK)) {
  126. prev_match = MTRR_TYPE_WRTHROUGH;
  127. curr_match = MTRR_TYPE_WRTHROUGH;
  128. }
  129. if (prev_match != curr_match)
  130. return MTRR_TYPE_UNCACHABLE;
  131. }
  132. if (mtrr_tom2) {
  133. if (start >= (1ULL<<32) && (end < mtrr_tom2))
  134. return MTRR_TYPE_WRBACK;
  135. }
  136. if (prev_match != 0xFF)
  137. return prev_match;
  138. return mtrr_state.def_type;
  139. }
  140. /* Get the MSR pair relating to a var range */
  141. static void
  142. get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr)
  143. {
  144. rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
  145. rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
  146. }
  147. /* Fill the MSR pair relating to a var range */
  148. void fill_mtrr_var_range(unsigned int index,
  149. u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi)
  150. {
  151. struct mtrr_var_range *vr;
  152. vr = mtrr_state.var_ranges;
  153. vr[index].base_lo = base_lo;
  154. vr[index].base_hi = base_hi;
  155. vr[index].mask_lo = mask_lo;
  156. vr[index].mask_hi = mask_hi;
  157. }
  158. static void get_fixed_ranges(mtrr_type *frs)
  159. {
  160. unsigned int *p = (unsigned int *)frs;
  161. int i;
  162. k8_check_syscfg_dram_mod_en();
  163. rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]);
  164. for (i = 0; i < 2; i++)
  165. rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
  166. for (i = 0; i < 8; i++)
  167. rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
  168. }
  169. void mtrr_save_fixed_ranges(void *info)
  170. {
  171. if (cpu_has_mtrr)
  172. get_fixed_ranges(mtrr_state.fixed_ranges);
  173. }
  174. static unsigned __initdata last_fixed_start;
  175. static unsigned __initdata last_fixed_end;
  176. static mtrr_type __initdata last_fixed_type;
  177. static void __init print_fixed_last(void)
  178. {
  179. if (!last_fixed_end)
  180. return;
  181. pr_debug(" %05X-%05X %s\n", last_fixed_start,
  182. last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type));
  183. last_fixed_end = 0;
  184. }
  185. static void __init update_fixed_last(unsigned base, unsigned end,
  186. mtrr_type type)
  187. {
  188. last_fixed_start = base;
  189. last_fixed_end = end;
  190. last_fixed_type = type;
  191. }
  192. static void __init
  193. print_fixed(unsigned base, unsigned step, const mtrr_type *types)
  194. {
  195. unsigned i;
  196. for (i = 0; i < 8; ++i, ++types, base += step) {
  197. if (last_fixed_end == 0) {
  198. update_fixed_last(base, base + step, *types);
  199. continue;
  200. }
  201. if (last_fixed_end == base && last_fixed_type == *types) {
  202. last_fixed_end = base + step;
  203. continue;
  204. }
  205. /* new segments: gap or different type */
  206. print_fixed_last();
  207. update_fixed_last(base, base + step, *types);
  208. }
  209. }
  210. static void prepare_set(void);
  211. static void post_set(void);
  212. static void __init print_mtrr_state(void)
  213. {
  214. unsigned int i;
  215. int high_width;
  216. pr_debug("MTRR default type: %s\n",
  217. mtrr_attrib_to_str(mtrr_state.def_type));
  218. if (mtrr_state.have_fixed) {
  219. pr_debug("MTRR fixed ranges %sabled:\n",
  220. mtrr_state.enabled & 1 ? "en" : "dis");
  221. print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0);
  222. for (i = 0; i < 2; ++i)
  223. print_fixed(0x80000 + i * 0x20000, 0x04000,
  224. mtrr_state.fixed_ranges + (i + 1) * 8);
  225. for (i = 0; i < 8; ++i)
  226. print_fixed(0xC0000 + i * 0x08000, 0x01000,
  227. mtrr_state.fixed_ranges + (i + 3) * 8);
  228. /* tail */
  229. print_fixed_last();
  230. }
  231. pr_debug("MTRR variable ranges %sabled:\n",
  232. mtrr_state.enabled & 2 ? "en" : "dis");
  233. if (size_or_mask & 0xffffffffUL)
  234. high_width = ffs(size_or_mask & 0xffffffffUL) - 1;
  235. else
  236. high_width = ffs(size_or_mask>>32) + 32 - 1;
  237. high_width = (high_width - (32 - PAGE_SHIFT) + 3) / 4;
  238. for (i = 0; i < num_var_ranges; ++i) {
  239. if (mtrr_state.var_ranges[i].mask_lo & (1 << 11))
  240. pr_debug(" %u base %0*X%05X000 mask %0*X%05X000 %s\n",
  241. i,
  242. high_width,
  243. mtrr_state.var_ranges[i].base_hi,
  244. mtrr_state.var_ranges[i].base_lo >> 12,
  245. high_width,
  246. mtrr_state.var_ranges[i].mask_hi,
  247. mtrr_state.var_ranges[i].mask_lo >> 12,
  248. mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff));
  249. else
  250. pr_debug(" %u disabled\n", i);
  251. }
  252. if (mtrr_tom2)
  253. pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20);
  254. }
  255. /* Grab all of the MTRR state for this CPU into *state */
  256. void __init get_mtrr_state(void)
  257. {
  258. struct mtrr_var_range *vrs;
  259. unsigned long flags;
  260. unsigned lo, dummy;
  261. unsigned int i;
  262. vrs = mtrr_state.var_ranges;
  263. rdmsr(MSR_MTRRcap, lo, dummy);
  264. mtrr_state.have_fixed = (lo >> 8) & 1;
  265. for (i = 0; i < num_var_ranges; i++)
  266. get_mtrr_var_range(i, &vrs[i]);
  267. if (mtrr_state.have_fixed)
  268. get_fixed_ranges(mtrr_state.fixed_ranges);
  269. rdmsr(MSR_MTRRdefType, lo, dummy);
  270. mtrr_state.def_type = (lo & 0xff);
  271. mtrr_state.enabled = (lo & 0xc00) >> 10;
  272. if (amd_special_default_mtrr()) {
  273. unsigned low, high;
  274. /* TOP_MEM2 */
  275. rdmsr(MSR_K8_TOP_MEM2, low, high);
  276. mtrr_tom2 = high;
  277. mtrr_tom2 <<= 32;
  278. mtrr_tom2 |= low;
  279. mtrr_tom2 &= 0xffffff800000ULL;
  280. }
  281. print_mtrr_state();
  282. mtrr_state_set = 1;
  283. /* PAT setup for BP. We need to go through sync steps here */
  284. local_irq_save(flags);
  285. prepare_set();
  286. pat_init();
  287. post_set();
  288. local_irq_restore(flags);
  289. }
  290. /* Some BIOS's are messed up and don't set all MTRRs the same! */
  291. void __init mtrr_state_warn(void)
  292. {
  293. unsigned long mask = smp_changes_mask;
  294. if (!mask)
  295. return;
  296. if (mask & MTRR_CHANGE_MASK_FIXED)
  297. pr_warning("mtrr: your CPUs had inconsistent fixed MTRR settings\n");
  298. if (mask & MTRR_CHANGE_MASK_VARIABLE)
  299. pr_warning("mtrr: your CPUs had inconsistent variable MTRR settings\n");
  300. if (mask & MTRR_CHANGE_MASK_DEFTYPE)
  301. pr_warning("mtrr: your CPUs had inconsistent MTRRdefType settings\n");
  302. printk(KERN_INFO "mtrr: probably your BIOS does not setup all CPUs.\n");
  303. printk(KERN_INFO "mtrr: corrected configuration.\n");
  304. }
  305. /*
  306. * Doesn't attempt to pass an error out to MTRR users
  307. * because it's quite complicated in some cases and probably not
  308. * worth it because the best error handling is to ignore it.
  309. */
  310. void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b)
  311. {
  312. if (wrmsr_safe(msr, a, b) < 0) {
  313. printk(KERN_ERR
  314. "MTRR: CPU %u: Writing MSR %x to %x:%x failed\n",
  315. smp_processor_id(), msr, a, b);
  316. }
  317. }
  318. /**
  319. * set_fixed_range - checks & updates a fixed-range MTRR if it
  320. * differs from the value it should have
  321. * @msr: MSR address of the MTTR which should be checked and updated
  322. * @changed: pointer which indicates whether the MTRR needed to be changed
  323. * @msrwords: pointer to the MSR values which the MSR should have
  324. */
  325. static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords)
  326. {
  327. unsigned lo, hi;
  328. rdmsr(msr, lo, hi);
  329. if (lo != msrwords[0] || hi != msrwords[1]) {
  330. mtrr_wrmsr(msr, msrwords[0], msrwords[1]);
  331. *changed = true;
  332. }
  333. }
  334. /**
  335. * generic_get_free_region - Get a free MTRR.
  336. * @base: The starting (base) address of the region.
  337. * @size: The size (in bytes) of the region.
  338. * @replace_reg: mtrr index to be replaced; set to invalid value if none.
  339. *
  340. * Returns: The index of the region on success, else negative on error.
  341. */
  342. int
  343. generic_get_free_region(unsigned long base, unsigned long size, int replace_reg)
  344. {
  345. unsigned long lbase, lsize;
  346. mtrr_type ltype;
  347. int i, max;
  348. max = num_var_ranges;
  349. if (replace_reg >= 0 && replace_reg < max)
  350. return replace_reg;
  351. for (i = 0; i < max; ++i) {
  352. mtrr_if->get(i, &lbase, &lsize, &ltype);
  353. if (lsize == 0)
  354. return i;
  355. }
  356. return -ENOSPC;
  357. }
  358. static void generic_get_mtrr(unsigned int reg, unsigned long *base,
  359. unsigned long *size, mtrr_type *type)
  360. {
  361. unsigned int mask_lo, mask_hi, base_lo, base_hi;
  362. unsigned int tmp, hi;
  363. int cpu;
  364. /*
  365. * get_mtrr doesn't need to update mtrr_state, also it could be called
  366. * from any cpu, so try to print it out directly.
  367. */
  368. cpu = get_cpu();
  369. rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi);
  370. if ((mask_lo & 0x800) == 0) {
  371. /* Invalid (i.e. free) range */
  372. *base = 0;
  373. *size = 0;
  374. *type = 0;
  375. goto out_put_cpu;
  376. }
  377. rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi);
  378. /* Work out the shifted address mask: */
  379. tmp = mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT;
  380. mask_lo = size_or_mask | tmp;
  381. /* Expand tmp with high bits to all 1s: */
  382. hi = fls(tmp);
  383. if (hi > 0) {
  384. tmp |= ~((1<<(hi - 1)) - 1);
  385. if (tmp != mask_lo) {
  386. WARN_ONCE(1, KERN_INFO "mtrr: your BIOS has set up an incorrect mask, fixing it up.\n");
  387. mask_lo = tmp;
  388. }
  389. }
  390. /*
  391. * This works correctly if size is a power of two, i.e. a
  392. * contiguous range:
  393. */
  394. *size = -mask_lo;
  395. *base = base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;
  396. *type = base_lo & 0xff;
  397. out_put_cpu:
  398. put_cpu();
  399. }
  400. /**
  401. * set_fixed_ranges - checks & updates the fixed-range MTRRs if they
  402. * differ from the saved set
  403. * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges()
  404. */
  405. static int set_fixed_ranges(mtrr_type *frs)
  406. {
  407. unsigned long long *saved = (unsigned long long *)frs;
  408. bool changed = false;
  409. int block = -1, range;
  410. k8_check_syscfg_dram_mod_en();
  411. while (fixed_range_blocks[++block].ranges) {
  412. for (range = 0; range < fixed_range_blocks[block].ranges; range++)
  413. set_fixed_range(fixed_range_blocks[block].base_msr + range,
  414. &changed, (unsigned int *)saved++);
  415. }
  416. return changed;
  417. }
  418. /*
  419. * Set the MSR pair relating to a var range.
  420. * Returns true if changes are made.
  421. */
  422. static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr)
  423. {
  424. unsigned int lo, hi;
  425. bool changed = false;
  426. rdmsr(MTRRphysBase_MSR(index), lo, hi);
  427. if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL)
  428. || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
  429. (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
  430. mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi);
  431. changed = true;
  432. }
  433. rdmsr(MTRRphysMask_MSR(index), lo, hi);
  434. if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL)
  435. || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) !=
  436. (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) {
  437. mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi);
  438. changed = true;
  439. }
  440. return changed;
  441. }
  442. static u32 deftype_lo, deftype_hi;
  443. /**
  444. * set_mtrr_state - Set the MTRR state for this CPU.
  445. *
  446. * NOTE: The CPU must already be in a safe state for MTRR changes.
  447. * RETURNS: 0 if no changes made, else a mask indicating what was changed.
  448. */
  449. static unsigned long set_mtrr_state(void)
  450. {
  451. unsigned long change_mask = 0;
  452. unsigned int i;
  453. for (i = 0; i < num_var_ranges; i++) {
  454. if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i]))
  455. change_mask |= MTRR_CHANGE_MASK_VARIABLE;
  456. }
  457. if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges))
  458. change_mask |= MTRR_CHANGE_MASK_FIXED;
  459. /*
  460. * Set_mtrr_restore restores the old value of MTRRdefType,
  461. * so to set it we fiddle with the saved value:
  462. */
  463. if ((deftype_lo & 0xff) != mtrr_state.def_type
  464. || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) {
  465. deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type |
  466. (mtrr_state.enabled << 10);
  467. change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
  468. }
  469. return change_mask;
  470. }
  471. static unsigned long cr4;
  472. static DEFINE_SPINLOCK(set_atomicity_lock);
  473. /*
  474. * Since we are disabling the cache don't allow any interrupts,
  475. * they would run extremely slow and would only increase the pain.
  476. *
  477. * The caller must ensure that local interrupts are disabled and
  478. * are reenabled after post_set() has been called.
  479. */
  480. static void prepare_set(void) __acquires(set_atomicity_lock)
  481. {
  482. unsigned long cr0;
  483. /*
  484. * Note that this is not ideal
  485. * since the cache is only flushed/disabled for this CPU while the
  486. * MTRRs are changed, but changing this requires more invasive
  487. * changes to the way the kernel boots
  488. */
  489. spin_lock(&set_atomicity_lock);
  490. /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
  491. cr0 = read_cr0() | X86_CR0_CD;
  492. write_cr0(cr0);
  493. wbinvd();
  494. /* Save value of CR4 and clear Page Global Enable (bit 7) */
  495. if (cpu_has_pge) {
  496. cr4 = read_cr4();
  497. write_cr4(cr4 & ~X86_CR4_PGE);
  498. }
  499. /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */
  500. __flush_tlb();
  501. /* Save MTRR state */
  502. rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  503. /* Disable MTRRs, and set the default type to uncached */
  504. mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi);
  505. }
  506. static void post_set(void) __releases(set_atomicity_lock)
  507. {
  508. /* Flush TLBs (no need to flush caches - they are disabled) */
  509. __flush_tlb();
  510. /* Intel (P6) standard MTRRs */
  511. mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi);
  512. /* Enable caches */
  513. write_cr0(read_cr0() & 0xbfffffff);
  514. /* Restore value of CR4 */
  515. if (cpu_has_pge)
  516. write_cr4(cr4);
  517. spin_unlock(&set_atomicity_lock);
  518. }
  519. static void generic_set_all(void)
  520. {
  521. unsigned long mask, count;
  522. unsigned long flags;
  523. local_irq_save(flags);
  524. prepare_set();
  525. /* Actually set the state */
  526. mask = set_mtrr_state();
  527. /* also set PAT */
  528. pat_init();
  529. post_set();
  530. local_irq_restore(flags);
  531. /* Use the atomic bitops to update the global mask */
  532. for (count = 0; count < sizeof mask * 8; ++count) {
  533. if (mask & 0x01)
  534. set_bit(count, &smp_changes_mask);
  535. mask >>= 1;
  536. }
  537. }
  538. /**
  539. * generic_set_mtrr - set variable MTRR register on the local CPU.
  540. *
  541. * @reg: The register to set.
  542. * @base: The base address of the region.
  543. * @size: The size of the region. If this is 0 the region is disabled.
  544. * @type: The type of the region.
  545. *
  546. * Returns nothing.
  547. */
  548. static void generic_set_mtrr(unsigned int reg, unsigned long base,
  549. unsigned long size, mtrr_type type)
  550. {
  551. unsigned long flags;
  552. struct mtrr_var_range *vr;
  553. vr = &mtrr_state.var_ranges[reg];
  554. local_irq_save(flags);
  555. prepare_set();
  556. if (size == 0) {
  557. /*
  558. * The invalid bit is kept in the mask, so we simply
  559. * clear the relevant mask register to disable a range.
  560. */
  561. mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0);
  562. memset(vr, 0, sizeof(struct mtrr_var_range));
  563. } else {
  564. vr->base_lo = base << PAGE_SHIFT | type;
  565. vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT);
  566. vr->mask_lo = -size << PAGE_SHIFT | 0x800;
  567. vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT);
  568. mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi);
  569. mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi);
  570. }
  571. post_set();
  572. local_irq_restore(flags);
  573. }
  574. int generic_validate_add_page(unsigned long base, unsigned long size,
  575. unsigned int type)
  576. {
  577. unsigned long lbase, last;
  578. /*
  579. * For Intel PPro stepping <= 7
  580. * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF
  581. */
  582. if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 &&
  583. boot_cpu_data.x86_model == 1 &&
  584. boot_cpu_data.x86_mask <= 7) {
  585. if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) {
  586. pr_warning("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base);
  587. return -EINVAL;
  588. }
  589. if (!(base + size < 0x70000 || base > 0x7003F) &&
  590. (type == MTRR_TYPE_WRCOMB
  591. || type == MTRR_TYPE_WRBACK)) {
  592. pr_warning("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n");
  593. return -EINVAL;
  594. }
  595. }
  596. /*
  597. * Check upper bits of base and last are equal and lower bits are 0
  598. * for base and 1 for last
  599. */
  600. last = base + size - 1;
  601. for (lbase = base; !(lbase & 1) && (last & 1);
  602. lbase = lbase >> 1, last = last >> 1)
  603. ;
  604. if (lbase != last) {
  605. pr_warning("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size);
  606. return -EINVAL;
  607. }
  608. return 0;
  609. }
  610. static int generic_have_wrcomb(void)
  611. {
  612. unsigned long config, dummy;
  613. rdmsr(MSR_MTRRcap, config, dummy);
  614. return config & (1 << 10);
  615. }
  616. int positive_have_wrcomb(void)
  617. {
  618. return 1;
  619. }
  620. /*
  621. * Generic structure...
  622. */
  623. struct mtrr_ops generic_mtrr_ops = {
  624. .use_intel_if = 1,
  625. .set_all = generic_set_all,
  626. .get = generic_get_mtrr,
  627. .get_free_region = generic_get_free_region,
  628. .set = generic_set_mtrr,
  629. .validate_add_page = generic_validate_add_page,
  630. .have_wrcomb = generic_have_wrcomb,
  631. };