generic.c 21 KB

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