generic.c 18 KB

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