main.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /* Generic MTRR (Memory Type Range Register) driver.
  2. Copyright (C) 1997-2000 Richard Gooch
  3. Copyright (c) 2002 Patrick Mochel
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. Richard Gooch may be reached by email at rgooch@atnf.csiro.au
  16. The postal address is:
  17. Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
  18. Source: "Pentium Pro Family Developer's Manual, Volume 3:
  19. Operating System Writer's Guide" (Intel document number 242692),
  20. section 11.11.7
  21. This was cleaned and made readable by Patrick Mochel <mochel@osdl.org>
  22. on 6-7 March 2002.
  23. Source: Intel Architecture Software Developers Manual, Volume 3:
  24. System Programming Guide; Section 9.11. (1997 edition - PPro).
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/pci.h>
  29. #include <linux/smp.h>
  30. #include <linux/cpu.h>
  31. #include <linux/mutex.h>
  32. #include <linux/sort.h>
  33. #include <asm/e820.h>
  34. #include <asm/mtrr.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/processor.h>
  37. #include <asm/msr.h>
  38. #include <asm/kvm_para.h>
  39. #include "mtrr.h"
  40. u32 num_var_ranges = 0;
  41. unsigned int mtrr_usage_table[MAX_VAR_RANGES];
  42. static DEFINE_MUTEX(mtrr_mutex);
  43. u64 size_or_mask, size_and_mask;
  44. static struct mtrr_ops * mtrr_ops[X86_VENDOR_NUM] = {};
  45. struct mtrr_ops * mtrr_if = NULL;
  46. static void set_mtrr(unsigned int reg, unsigned long base,
  47. unsigned long size, mtrr_type type);
  48. void set_mtrr_ops(struct mtrr_ops * ops)
  49. {
  50. if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
  51. mtrr_ops[ops->vendor] = ops;
  52. }
  53. /* Returns non-zero if we have the write-combining memory type */
  54. static int have_wrcomb(void)
  55. {
  56. struct pci_dev *dev;
  57. u8 rev;
  58. if ((dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) != NULL) {
  59. /* ServerWorks LE chipsets < rev 6 have problems with write-combining
  60. Don't allow it and leave room for other chipsets to be tagged */
  61. if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
  62. dev->device == PCI_DEVICE_ID_SERVERWORKS_LE) {
  63. pci_read_config_byte(dev, PCI_CLASS_REVISION, &rev);
  64. if (rev <= 5) {
  65. printk(KERN_INFO "mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
  66. pci_dev_put(dev);
  67. return 0;
  68. }
  69. }
  70. /* Intel 450NX errata # 23. Non ascending cacheline evictions to
  71. write combining memory may resulting in data corruption */
  72. if (dev->vendor == PCI_VENDOR_ID_INTEL &&
  73. dev->device == PCI_DEVICE_ID_INTEL_82451NX) {
  74. printk(KERN_INFO "mtrr: Intel 450NX MMC detected. Write-combining disabled.\n");
  75. pci_dev_put(dev);
  76. return 0;
  77. }
  78. pci_dev_put(dev);
  79. }
  80. return (mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0);
  81. }
  82. /* This function returns the number of variable MTRRs */
  83. static void __init set_num_var_ranges(void)
  84. {
  85. unsigned long config = 0, dummy;
  86. if (use_intel()) {
  87. rdmsr(MTRRcap_MSR, config, dummy);
  88. } else if (is_cpu(AMD))
  89. config = 2;
  90. else if (is_cpu(CYRIX) || is_cpu(CENTAUR))
  91. config = 8;
  92. num_var_ranges = config & 0xff;
  93. }
  94. static void __init init_table(void)
  95. {
  96. int i, max;
  97. max = num_var_ranges;
  98. for (i = 0; i < max; i++)
  99. mtrr_usage_table[i] = 1;
  100. }
  101. struct set_mtrr_data {
  102. atomic_t count;
  103. atomic_t gate;
  104. unsigned long smp_base;
  105. unsigned long smp_size;
  106. unsigned int smp_reg;
  107. mtrr_type smp_type;
  108. };
  109. static void ipi_handler(void *info)
  110. /* [SUMMARY] Synchronisation handler. Executed by "other" CPUs.
  111. [RETURNS] Nothing.
  112. */
  113. {
  114. #ifdef CONFIG_SMP
  115. struct set_mtrr_data *data = info;
  116. unsigned long flags;
  117. local_irq_save(flags);
  118. atomic_dec(&data->count);
  119. while(!atomic_read(&data->gate))
  120. cpu_relax();
  121. /* The master has cleared me to execute */
  122. if (data->smp_reg != ~0U)
  123. mtrr_if->set(data->smp_reg, data->smp_base,
  124. data->smp_size, data->smp_type);
  125. else
  126. mtrr_if->set_all();
  127. atomic_dec(&data->count);
  128. while(atomic_read(&data->gate))
  129. cpu_relax();
  130. atomic_dec(&data->count);
  131. local_irq_restore(flags);
  132. #endif
  133. }
  134. static inline int types_compatible(mtrr_type type1, mtrr_type type2) {
  135. return type1 == MTRR_TYPE_UNCACHABLE ||
  136. type2 == MTRR_TYPE_UNCACHABLE ||
  137. (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
  138. (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
  139. }
  140. /**
  141. * set_mtrr - update mtrrs on all processors
  142. * @reg: mtrr in question
  143. * @base: mtrr base
  144. * @size: mtrr size
  145. * @type: mtrr type
  146. *
  147. * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
  148. *
  149. * 1. Send IPI to do the following:
  150. * 2. Disable Interrupts
  151. * 3. Wait for all procs to do so
  152. * 4. Enter no-fill cache mode
  153. * 5. Flush caches
  154. * 6. Clear PGE bit
  155. * 7. Flush all TLBs
  156. * 8. Disable all range registers
  157. * 9. Update the MTRRs
  158. * 10. Enable all range registers
  159. * 11. Flush all TLBs and caches again
  160. * 12. Enter normal cache mode and reenable caching
  161. * 13. Set PGE
  162. * 14. Wait for buddies to catch up
  163. * 15. Enable interrupts.
  164. *
  165. * What does that mean for us? Well, first we set data.count to the number
  166. * of CPUs. As each CPU disables interrupts, it'll decrement it once. We wait
  167. * until it hits 0 and proceed. We set the data.gate flag and reset data.count.
  168. * Meanwhile, they are waiting for that flag to be set. Once it's set, each
  169. * CPU goes through the transition of updating MTRRs. The CPU vendors may each do it
  170. * differently, so we call mtrr_if->set() callback and let them take care of it.
  171. * When they're done, they again decrement data->count and wait for data.gate to
  172. * be reset.
  173. * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag.
  174. * Everyone then enables interrupts and we all continue on.
  175. *
  176. * Note that the mechanism is the same for UP systems, too; all the SMP stuff
  177. * becomes nops.
  178. */
  179. static void set_mtrr(unsigned int reg, unsigned long base,
  180. unsigned long size, mtrr_type type)
  181. {
  182. struct set_mtrr_data data;
  183. unsigned long flags;
  184. data.smp_reg = reg;
  185. data.smp_base = base;
  186. data.smp_size = size;
  187. data.smp_type = type;
  188. atomic_set(&data.count, num_booting_cpus() - 1);
  189. /* make sure data.count is visible before unleashing other CPUs */
  190. smp_wmb();
  191. atomic_set(&data.gate,0);
  192. /* Start the ball rolling on other CPUs */
  193. if (smp_call_function(ipi_handler, &data, 1, 0) != 0)
  194. panic("mtrr: timed out waiting for other CPUs\n");
  195. local_irq_save(flags);
  196. while(atomic_read(&data.count))
  197. cpu_relax();
  198. /* ok, reset count and toggle gate */
  199. atomic_set(&data.count, num_booting_cpus() - 1);
  200. smp_wmb();
  201. atomic_set(&data.gate,1);
  202. /* do our MTRR business */
  203. /* HACK!
  204. * We use this same function to initialize the mtrrs on boot.
  205. * The state of the boot cpu's mtrrs has been saved, and we want
  206. * to replicate across all the APs.
  207. * If we're doing that @reg is set to something special...
  208. */
  209. if (reg != ~0U)
  210. mtrr_if->set(reg,base,size,type);
  211. /* wait for the others */
  212. while(atomic_read(&data.count))
  213. cpu_relax();
  214. atomic_set(&data.count, num_booting_cpus() - 1);
  215. smp_wmb();
  216. atomic_set(&data.gate,0);
  217. /*
  218. * Wait here for everyone to have seen the gate change
  219. * So we're the last ones to touch 'data'
  220. */
  221. while(atomic_read(&data.count))
  222. cpu_relax();
  223. local_irq_restore(flags);
  224. }
  225. /**
  226. * mtrr_add_page - Add a memory type region
  227. * @base: Physical base address of region in pages (in units of 4 kB!)
  228. * @size: Physical size of region in pages (4 kB)
  229. * @type: Type of MTRR desired
  230. * @increment: If this is true do usage counting on the region
  231. *
  232. * Memory type region registers control the caching on newer Intel and
  233. * non Intel processors. This function allows drivers to request an
  234. * MTRR is added. The details and hardware specifics of each processor's
  235. * implementation are hidden from the caller, but nevertheless the
  236. * caller should expect to need to provide a power of two size on an
  237. * equivalent power of two boundary.
  238. *
  239. * If the region cannot be added either because all regions are in use
  240. * or the CPU cannot support it a negative value is returned. On success
  241. * the register number for this entry is returned, but should be treated
  242. * as a cookie only.
  243. *
  244. * On a multiprocessor machine the changes are made to all processors.
  245. * This is required on x86 by the Intel processors.
  246. *
  247. * The available types are
  248. *
  249. * %MTRR_TYPE_UNCACHABLE - No caching
  250. *
  251. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  252. *
  253. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  254. *
  255. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  256. *
  257. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  258. * failures and do not wish system log messages to be sent.
  259. */
  260. int mtrr_add_page(unsigned long base, unsigned long size,
  261. unsigned int type, bool increment)
  262. {
  263. int i, replace, error;
  264. mtrr_type ltype;
  265. unsigned long lbase, lsize;
  266. if (!mtrr_if)
  267. return -ENXIO;
  268. if ((error = mtrr_if->validate_add_page(base,size,type)))
  269. return error;
  270. if (type >= MTRR_NUM_TYPES) {
  271. printk(KERN_WARNING "mtrr: type: %u invalid\n", type);
  272. return -EINVAL;
  273. }
  274. /* If the type is WC, check that this processor supports it */
  275. if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
  276. printk(KERN_WARNING
  277. "mtrr: your processor doesn't support write-combining\n");
  278. return -ENOSYS;
  279. }
  280. if (!size) {
  281. printk(KERN_WARNING "mtrr: zero sized request\n");
  282. return -EINVAL;
  283. }
  284. if (base & size_or_mask || size & size_or_mask) {
  285. printk(KERN_WARNING "mtrr: base or size exceeds the MTRR width\n");
  286. return -EINVAL;
  287. }
  288. error = -EINVAL;
  289. replace = -1;
  290. /* No CPU hotplug when we change MTRR entries */
  291. get_online_cpus();
  292. /* Search for existing MTRR */
  293. mutex_lock(&mtrr_mutex);
  294. for (i = 0; i < num_var_ranges; ++i) {
  295. mtrr_if->get(i, &lbase, &lsize, &ltype);
  296. if (!lsize || base > lbase + lsize - 1 || base + size - 1 < lbase)
  297. continue;
  298. /* At this point we know there is some kind of overlap/enclosure */
  299. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  300. if (base <= lbase && base + size - 1 >= lbase + lsize - 1) {
  301. /* New region encloses an existing region */
  302. if (type == ltype) {
  303. replace = replace == -1 ? i : -2;
  304. continue;
  305. }
  306. else if (types_compatible(type, ltype))
  307. continue;
  308. }
  309. printk(KERN_WARNING
  310. "mtrr: 0x%lx000,0x%lx000 overlaps existing"
  311. " 0x%lx000,0x%lx000\n", base, size, lbase,
  312. lsize);
  313. goto out;
  314. }
  315. /* New region is enclosed by an existing region */
  316. if (ltype != type) {
  317. if (types_compatible(type, ltype))
  318. continue;
  319. printk (KERN_WARNING "mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
  320. base, size, mtrr_attrib_to_str(ltype),
  321. mtrr_attrib_to_str(type));
  322. goto out;
  323. }
  324. if (increment)
  325. ++mtrr_usage_table[i];
  326. error = i;
  327. goto out;
  328. }
  329. /* Search for an empty MTRR */
  330. i = mtrr_if->get_free_region(base, size, replace);
  331. if (i >= 0) {
  332. set_mtrr(i, base, size, type);
  333. if (likely(replace < 0)) {
  334. mtrr_usage_table[i] = 1;
  335. } else {
  336. mtrr_usage_table[i] = mtrr_usage_table[replace];
  337. if (increment)
  338. mtrr_usage_table[i]++;
  339. if (unlikely(replace != i)) {
  340. set_mtrr(replace, 0, 0, 0);
  341. mtrr_usage_table[replace] = 0;
  342. }
  343. }
  344. } else
  345. printk(KERN_INFO "mtrr: no more MTRRs available\n");
  346. error = i;
  347. out:
  348. mutex_unlock(&mtrr_mutex);
  349. put_online_cpus();
  350. return error;
  351. }
  352. static int mtrr_check(unsigned long base, unsigned long size)
  353. {
  354. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  355. printk(KERN_WARNING
  356. "mtrr: size and base must be multiples of 4 kiB\n");
  357. printk(KERN_DEBUG
  358. "mtrr: size: 0x%lx base: 0x%lx\n", size, base);
  359. dump_stack();
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. /**
  365. * mtrr_add - Add a memory type region
  366. * @base: Physical base address of region
  367. * @size: Physical size of region
  368. * @type: Type of MTRR desired
  369. * @increment: If this is true do usage counting on the region
  370. *
  371. * Memory type region registers control the caching on newer Intel and
  372. * non Intel processors. This function allows drivers to request an
  373. * MTRR is added. The details and hardware specifics of each processor's
  374. * implementation are hidden from the caller, but nevertheless the
  375. * caller should expect to need to provide a power of two size on an
  376. * equivalent power of two boundary.
  377. *
  378. * If the region cannot be added either because all regions are in use
  379. * or the CPU cannot support it a negative value is returned. On success
  380. * the register number for this entry is returned, but should be treated
  381. * as a cookie only.
  382. *
  383. * On a multiprocessor machine the changes are made to all processors.
  384. * This is required on x86 by the Intel processors.
  385. *
  386. * The available types are
  387. *
  388. * %MTRR_TYPE_UNCACHABLE - No caching
  389. *
  390. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  391. *
  392. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  393. *
  394. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  395. *
  396. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  397. * failures and do not wish system log messages to be sent.
  398. */
  399. int
  400. mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  401. bool increment)
  402. {
  403. if (mtrr_check(base, size))
  404. return -EINVAL;
  405. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  406. increment);
  407. }
  408. /**
  409. * mtrr_del_page - delete a memory type region
  410. * @reg: Register returned by mtrr_add
  411. * @base: Physical base address
  412. * @size: Size of region
  413. *
  414. * If register is supplied then base and size are ignored. This is
  415. * how drivers should call it.
  416. *
  417. * Releases an MTRR region. If the usage count drops to zero the
  418. * register is freed and the region returns to default state.
  419. * On success the register is returned, on failure a negative error
  420. * code.
  421. */
  422. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  423. {
  424. int i, max;
  425. mtrr_type ltype;
  426. unsigned long lbase, lsize;
  427. int error = -EINVAL;
  428. if (!mtrr_if)
  429. return -ENXIO;
  430. max = num_var_ranges;
  431. /* No CPU hotplug when we change MTRR entries */
  432. get_online_cpus();
  433. mutex_lock(&mtrr_mutex);
  434. if (reg < 0) {
  435. /* Search for existing MTRR */
  436. for (i = 0; i < max; ++i) {
  437. mtrr_if->get(i, &lbase, &lsize, &ltype);
  438. if (lbase == base && lsize == size) {
  439. reg = i;
  440. break;
  441. }
  442. }
  443. if (reg < 0) {
  444. printk(KERN_DEBUG "mtrr: no MTRR for %lx000,%lx000 found\n", base,
  445. size);
  446. goto out;
  447. }
  448. }
  449. if (reg >= max) {
  450. printk(KERN_WARNING "mtrr: register: %d too big\n", reg);
  451. goto out;
  452. }
  453. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  454. if (lsize < 1) {
  455. printk(KERN_WARNING "mtrr: MTRR %d not used\n", reg);
  456. goto out;
  457. }
  458. if (mtrr_usage_table[reg] < 1) {
  459. printk(KERN_WARNING "mtrr: reg: %d has count=0\n", reg);
  460. goto out;
  461. }
  462. if (--mtrr_usage_table[reg] < 1)
  463. set_mtrr(reg, 0, 0, 0);
  464. error = reg;
  465. out:
  466. mutex_unlock(&mtrr_mutex);
  467. put_online_cpus();
  468. return error;
  469. }
  470. /**
  471. * mtrr_del - delete a memory type region
  472. * @reg: Register returned by mtrr_add
  473. * @base: Physical base address
  474. * @size: Size of region
  475. *
  476. * If register is supplied then base and size are ignored. This is
  477. * how drivers should call it.
  478. *
  479. * Releases an MTRR region. If the usage count drops to zero the
  480. * register is freed and the region returns to default state.
  481. * On success the register is returned, on failure a negative error
  482. * code.
  483. */
  484. int
  485. mtrr_del(int reg, unsigned long base, unsigned long size)
  486. {
  487. if (mtrr_check(base, size))
  488. return -EINVAL;
  489. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  490. }
  491. EXPORT_SYMBOL(mtrr_add);
  492. EXPORT_SYMBOL(mtrr_del);
  493. /* HACK ALERT!
  494. * These should be called implicitly, but we can't yet until all the initcall
  495. * stuff is done...
  496. */
  497. static void __init init_ifs(void)
  498. {
  499. #ifndef CONFIG_X86_64
  500. amd_init_mtrr();
  501. cyrix_init_mtrr();
  502. centaur_init_mtrr();
  503. #endif
  504. }
  505. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  506. * MTRR driver doesn't require this
  507. */
  508. struct mtrr_value {
  509. mtrr_type ltype;
  510. unsigned long lbase;
  511. unsigned long lsize;
  512. };
  513. static struct mtrr_value mtrr_state[MAX_VAR_RANGES];
  514. static int mtrr_save(struct sys_device * sysdev, pm_message_t state)
  515. {
  516. int i;
  517. for (i = 0; i < num_var_ranges; i++) {
  518. mtrr_if->get(i,
  519. &mtrr_state[i].lbase,
  520. &mtrr_state[i].lsize,
  521. &mtrr_state[i].ltype);
  522. }
  523. return 0;
  524. }
  525. static int mtrr_restore(struct sys_device * sysdev)
  526. {
  527. int i;
  528. for (i = 0; i < num_var_ranges; i++) {
  529. if (mtrr_state[i].lsize)
  530. set_mtrr(i,
  531. mtrr_state[i].lbase,
  532. mtrr_state[i].lsize,
  533. mtrr_state[i].ltype);
  534. }
  535. return 0;
  536. }
  537. static struct sysdev_driver mtrr_sysdev_driver = {
  538. .suspend = mtrr_save,
  539. .resume = mtrr_restore,
  540. };
  541. #ifdef CONFIG_MTRR_SANITIZER
  542. static int enable_mtrr_cleanup __initdata = CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT;
  543. #else
  544. static int enable_mtrr_cleanup __initdata = -1;
  545. #endif
  546. static int __init disable_mtrr_cleanup_setup(char *str)
  547. {
  548. if (enable_mtrr_cleanup != -1)
  549. enable_mtrr_cleanup = 0;
  550. return 0;
  551. }
  552. early_param("disable_mtrr_cleanup", disable_mtrr_cleanup_setup);
  553. static int __init enable_mtrr_cleanup_setup(char *str)
  554. {
  555. if (enable_mtrr_cleanup != -1)
  556. enable_mtrr_cleanup = 1;
  557. return 0;
  558. }
  559. early_param("enble_mtrr_cleanup", enable_mtrr_cleanup_setup);
  560. /* should be related to MTRR_VAR_RANGES nums */
  561. #define RANGE_NUM 256
  562. struct res_range {
  563. unsigned long start;
  564. unsigned long end;
  565. };
  566. static int __init
  567. add_range(struct res_range *range, int nr_range, unsigned long start,
  568. unsigned long end)
  569. {
  570. /* out of slots */
  571. if (nr_range >= RANGE_NUM)
  572. return nr_range;
  573. range[nr_range].start = start;
  574. range[nr_range].end = end;
  575. nr_range++;
  576. return nr_range;
  577. }
  578. static int __init
  579. add_range_with_merge(struct res_range *range, int nr_range, unsigned long start,
  580. unsigned long end)
  581. {
  582. int i;
  583. /* try to merge it with old one */
  584. for (i = 0; i < nr_range; i++) {
  585. unsigned long final_start, final_end;
  586. unsigned long common_start, common_end;
  587. if (!range[i].end)
  588. continue;
  589. common_start = max(range[i].start, start);
  590. common_end = min(range[i].end, end);
  591. if (common_start > common_end + 1)
  592. continue;
  593. final_start = min(range[i].start, start);
  594. final_end = max(range[i].end, end);
  595. range[i].start = final_start;
  596. range[i].end = final_end;
  597. return nr_range;
  598. }
  599. /* need to add that */
  600. return add_range(range, nr_range, start, end);
  601. }
  602. static void __init
  603. subtract_range(struct res_range *range, unsigned long start, unsigned long end)
  604. {
  605. int i, j;
  606. for (j = 0; j < RANGE_NUM; j++) {
  607. if (!range[j].end)
  608. continue;
  609. if (start <= range[j].start && end >= range[j].end) {
  610. range[j].start = 0;
  611. range[j].end = 0;
  612. continue;
  613. }
  614. if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
  615. range[j].start = end + 1;
  616. continue;
  617. }
  618. if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
  619. range[j].end = start - 1;
  620. continue;
  621. }
  622. if (start > range[j].start && end < range[j].end) {
  623. /* find the new spare */
  624. for (i = 0; i < RANGE_NUM; i++) {
  625. if (range[i].end == 0)
  626. break;
  627. }
  628. if (i < RANGE_NUM) {
  629. range[i].end = range[j].end;
  630. range[i].start = end + 1;
  631. } else {
  632. printk(KERN_ERR "run of slot in ranges\n");
  633. }
  634. range[j].end = start - 1;
  635. continue;
  636. }
  637. }
  638. }
  639. static int __init cmp_range(const void *x1, const void *x2)
  640. {
  641. const struct res_range *r1 = x1;
  642. const struct res_range *r2 = x2;
  643. long start1, start2;
  644. start1 = r1->start;
  645. start2 = r2->start;
  646. return start1 - start2;
  647. }
  648. struct var_mtrr_state {
  649. unsigned long range_startk;
  650. unsigned long range_sizek;
  651. unsigned long chunk_sizek;
  652. unsigned long gran_sizek;
  653. unsigned int reg;
  654. unsigned int address_bits;
  655. };
  656. static void __init
  657. set_var_mtrr(unsigned int reg, unsigned long basek, unsigned long sizek,
  658. unsigned char type, unsigned address_bits)
  659. {
  660. u32 base_lo, base_hi, mask_lo, mask_hi;
  661. u64 base, mask;
  662. if (!sizek) {
  663. fill_mtrr_var_range(reg, 0, 0, 0, 0);
  664. return;
  665. }
  666. mask = (1ULL << address_bits) - 1;
  667. mask &= ~((((u64)sizek) << 10) - 1);
  668. base = ((u64)basek) << 10;
  669. base |= type;
  670. mask |= 0x800;
  671. base_lo = base & ((1ULL<<32) - 1);
  672. base_hi = base >> 32;
  673. mask_lo = mask & ((1ULL<<32) - 1);
  674. mask_hi = mask >> 32;
  675. fill_mtrr_var_range(reg, base_lo, base_hi, mask_lo, mask_hi);
  676. }
  677. static unsigned int __init
  678. range_to_mtrr(unsigned int reg, unsigned long range_startk,
  679. unsigned long range_sizek, unsigned char type,
  680. unsigned address_bits)
  681. {
  682. if (!range_sizek || (reg >= num_var_ranges))
  683. return reg;
  684. while (range_sizek) {
  685. unsigned long max_align, align;
  686. unsigned long sizek;
  687. /* Compute the maximum size I can make a range */
  688. if (range_startk)
  689. max_align = ffs(range_startk) - 1;
  690. else
  691. max_align = 32;
  692. align = fls(range_sizek) - 1;
  693. if (align > max_align)
  694. align = max_align;
  695. sizek = 1 << align;
  696. printk(KERN_INFO "Setting variable MTRR %d, base: %ldMB, range: %ldMB, type %s\n",
  697. reg, range_startk >> 10, sizek >> 10,
  698. (type == MTRR_TYPE_UNCACHABLE)?"UC":
  699. ((type == MTRR_TYPE_WRBACK)?"WB":"Other")
  700. );
  701. set_var_mtrr(reg++, range_startk, sizek, type, address_bits);
  702. range_startk += sizek;
  703. range_sizek -= sizek;
  704. if (reg >= num_var_ranges)
  705. break;
  706. }
  707. return reg;
  708. }
  709. static void __init
  710. range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigned long basek)
  711. {
  712. unsigned long hole_basek, hole_sizek;
  713. unsigned long range0_basek, range0_sizek;
  714. unsigned long range_basek, range_sizek;
  715. unsigned long chunk_sizek;
  716. unsigned long gran_sizek;
  717. hole_basek = 0;
  718. hole_sizek = 0;
  719. chunk_sizek = state->chunk_sizek;
  720. gran_sizek = state->gran_sizek;
  721. /* align with gran size, prevent small block used up MTRRs */
  722. range_basek = ALIGN(state->range_startk, gran_sizek);
  723. if ((range_basek > basek) && basek)
  724. return;
  725. range_sizek = ALIGN(state->range_sizek - (range_basek - state->range_startk), gran_sizek);
  726. while (range_basek + range_sizek > (state->range_startk + state->range_sizek)) {
  727. range_sizek -= gran_sizek;
  728. if (!range_sizek)
  729. return;
  730. }
  731. state->range_startk = range_basek;
  732. state->range_sizek = range_sizek;
  733. /* try to append some small hole */
  734. range0_basek = state->range_startk;
  735. range0_sizek = ALIGN(state->range_sizek, chunk_sizek);
  736. if (range0_sizek == state->range_sizek) {
  737. printk(KERN_INFO "rangeX: %016lx - %016lx\n", range0_basek<<10, (range0_basek + state->range_sizek)<<10);
  738. state->reg = range_to_mtrr(state->reg, range0_basek,
  739. state->range_sizek, MTRR_TYPE_WRBACK, state->address_bits);
  740. return;
  741. } else if (basek) {
  742. while (range0_basek + range0_sizek - chunk_sizek > basek) {
  743. range0_sizek -= chunk_sizek;
  744. if (!range0_sizek)
  745. break;
  746. }
  747. }
  748. if (range0_sizek > chunk_sizek)
  749. range0_sizek -= chunk_sizek;
  750. printk(KERN_INFO "range0: %016lx - %016lx\n", range0_basek<<10, (range0_basek + range0_sizek)<<10);
  751. state->reg = range_to_mtrr(state->reg, range0_basek,
  752. range0_sizek, MTRR_TYPE_WRBACK, state->address_bits);
  753. range_basek = range0_basek + range0_sizek;
  754. range_sizek = chunk_sizek;
  755. if ((range_sizek - (state->range_sizek - range0_sizek) < (chunk_sizek >> 1)) &&
  756. (range_basek + range_sizek <= basek)) {
  757. hole_sizek = range_sizek - (state->range_sizek - range0_sizek);
  758. hole_basek = range_basek + range_sizek - hole_sizek;
  759. } else
  760. range_sizek = state->range_sizek - range0_sizek;
  761. printk(KERN_INFO "range: %016lx - %016lx\n", range_basek<<10, (range_basek + range_sizek)<<10);
  762. state->reg = range_to_mtrr(state->reg, range_basek,
  763. range_sizek, MTRR_TYPE_WRBACK, state->address_bits);
  764. if (hole_sizek) {
  765. printk(KERN_INFO "hole: %016lx - %016lx\n", hole_basek<<10, (hole_basek + hole_sizek)<<10);
  766. state->reg = range_to_mtrr(state->reg, hole_basek,
  767. hole_sizek, MTRR_TYPE_UNCACHABLE, state->address_bits);
  768. }
  769. }
  770. static void __init
  771. set_var_mtrr_range(struct var_mtrr_state *state, unsigned long base_pfn,
  772. unsigned long size_pfn)
  773. {
  774. unsigned long basek, sizek;
  775. if (state->reg >= num_var_ranges)
  776. return;
  777. basek = base_pfn << (PAGE_SHIFT - 10);
  778. sizek = size_pfn << (PAGE_SHIFT - 10);
  779. /* See if I can merge with the last range */
  780. if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
  781. unsigned long endk = basek + sizek;
  782. state->range_sizek = endk - state->range_startk;
  783. return;
  784. }
  785. /* Write the range mtrrs */
  786. if (state->range_sizek != 0) {
  787. range_to_mtrr_with_hole(state, basek);
  788. state->range_startk = 0;
  789. state->range_sizek = 0;
  790. }
  791. /* Allocate an msr */
  792. state->range_startk = basek;
  793. state->range_sizek = sizek;
  794. }
  795. /* mininum size of mtrr block that can take hole */
  796. static u64 mtrr_chunk_size __initdata = (256ULL<<20);
  797. static int __init parse_mtrr_chunk_size_opt(char *p)
  798. {
  799. if (!p)
  800. return -EINVAL;
  801. mtrr_chunk_size = memparse(p, &p);
  802. return 0;
  803. }
  804. early_param("mtrr_chunk_size", parse_mtrr_chunk_size_opt);
  805. /* granity of mtrr of block */
  806. static u64 mtrr_gran_size __initdata = (1ULL<<20);
  807. static int __init parse_mtrr_gran_size_opt(char *p)
  808. {
  809. if (!p)
  810. return -EINVAL;
  811. mtrr_gran_size = memparse(p, &p);
  812. return 0;
  813. }
  814. early_param("mtrr_gran_size", parse_mtrr_gran_size_opt);
  815. static void __init
  816. x86_setup_var_mtrrs(struct res_range *range, int nr_range,
  817. unsigned address_bits)
  818. {
  819. struct var_mtrr_state var_state;
  820. int i;
  821. var_state.range_startk = 0;
  822. var_state.range_sizek = 0;
  823. var_state.reg = 0;
  824. var_state.address_bits = address_bits;
  825. var_state.chunk_sizek = mtrr_chunk_size >> 10;
  826. var_state.gran_sizek = mtrr_gran_size >> 10;
  827. /* Write the range etc */
  828. for (i = 0; i < nr_range; i++)
  829. set_var_mtrr_range(&var_state, range[i].start, range[i].end - range[i].start + 1);
  830. /* Write the last range */
  831. range_to_mtrr_with_hole(&var_state, 0);
  832. printk(KERN_INFO "DONE variable MTRRs\n");
  833. /* Clear out the extra MTRR's */
  834. while (var_state.reg < num_var_ranges) {
  835. set_var_mtrr(var_state.reg, 0, 0, 0, var_state.address_bits);
  836. var_state.reg++;
  837. }
  838. }
  839. static int __init
  840. x86_get_mtrr_mem_range(struct res_range *range, int nr_range,
  841. unsigned long extra_remove_base,
  842. unsigned long extra_remove_size)
  843. {
  844. unsigned long i, base, size;
  845. mtrr_type type;
  846. for (i = 0; i < num_var_ranges; i++) {
  847. mtrr_if->get(i, &base, &size, &type);
  848. if (type != MTRR_TYPE_WRBACK)
  849. continue;
  850. nr_range = add_range_with_merge(range, nr_range, base, base + size - 1);
  851. }
  852. printk(KERN_INFO "After WB checking\n");
  853. for (i = 0; i < nr_range; i++)
  854. printk(KERN_INFO "MTRR MAP PFN: %016lx - %016lx\n", range[i].start, range[i].end + 1);
  855. /* take out UC ranges */
  856. for (i = 0; i < num_var_ranges; i++) {
  857. mtrr_if->get(i, &base, &size, &type);
  858. if (type != MTRR_TYPE_UNCACHABLE)
  859. continue;
  860. if (!size)
  861. continue;
  862. subtract_range(range, base, base + size - 1);
  863. }
  864. if (extra_remove_size)
  865. subtract_range(range, extra_remove_base, extra_remove_base + extra_remove_size - 1);
  866. /* get new range num */
  867. nr_range = 0;
  868. for (i = 0; i < RANGE_NUM; i++) {
  869. if (!range[i].end)
  870. continue;
  871. nr_range++;
  872. }
  873. printk(KERN_INFO "After UC checking\n");
  874. for (i = 0; i < nr_range; i++)
  875. printk(KERN_INFO "MTRR MAP PFN: %016lx - %016lx\n", range[i].start, range[i].end + 1);
  876. /* sort the ranges */
  877. sort(range, nr_range, sizeof(struct res_range), cmp_range, NULL);
  878. printk(KERN_INFO "After sorting\n");
  879. for (i = 0; i < nr_range; i++)
  880. printk(KERN_INFO "MTRR MAP PFN: %016lx - %016lx\n", range[i].start, range[i].end + 1);
  881. return nr_range;
  882. }
  883. static int __init mtrr_cleanup(unsigned address_bits)
  884. {
  885. unsigned long extra_remove_base, extra_remove_size;
  886. unsigned long i, base, size, def, dummy;
  887. struct res_range range[RANGE_NUM];
  888. mtrr_type type;
  889. int nr_range;
  890. /* extra one for all 0 */
  891. int num[MTRR_NUM_TYPES + 1];
  892. if (!is_cpu(INTEL) || enable_mtrr_cleanup < 1)
  893. return 0;
  894. rdmsr(MTRRdefType_MSR, def, dummy);
  895. def &= 0xff;
  896. if (def != MTRR_TYPE_UNCACHABLE)
  897. return 0;
  898. /* check entries number */
  899. memset(num, 0, sizeof(num));
  900. for (i = 0; i < num_var_ranges; i++) {
  901. mtrr_if->get(i, &base, &size, &type);
  902. if (type >= MTRR_NUM_TYPES)
  903. continue;
  904. if (!size)
  905. type = MTRR_NUM_TYPES;
  906. num[type]++;
  907. }
  908. /* check if we got UC entries */
  909. if (!num[MTRR_TYPE_UNCACHABLE])
  910. return 0;
  911. /* check if we only had WB and UC */
  912. if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
  913. num_var_ranges - num[MTRR_NUM_TYPES])
  914. return 0;
  915. memset(range, 0, sizeof(range));
  916. extra_remove_size = 0;
  917. if (mtrr_tom2) {
  918. extra_remove_base = 1 << (32 - PAGE_SHIFT);
  919. extra_remove_size = (mtrr_tom2>>PAGE_SHIFT) - extra_remove_base;
  920. }
  921. nr_range = x86_get_mtrr_mem_range(range, 0, extra_remove_base, extra_remove_size);
  922. /* convert ranges to var ranges state */
  923. x86_setup_var_mtrrs(range, nr_range, address_bits);
  924. return 1;
  925. }
  926. static int disable_mtrr_trim;
  927. static int __init disable_mtrr_trim_setup(char *str)
  928. {
  929. disable_mtrr_trim = 1;
  930. return 0;
  931. }
  932. early_param("disable_mtrr_trim", disable_mtrr_trim_setup);
  933. /*
  934. * Newer AMD K8s and later CPUs have a special magic MSR way to force WB
  935. * for memory >4GB. Check for that here.
  936. * Note this won't check if the MTRRs < 4GB where the magic bit doesn't
  937. * apply to are wrong, but so far we don't know of any such case in the wild.
  938. */
  939. #define Tom2Enabled (1U << 21)
  940. #define Tom2ForceMemTypeWB (1U << 22)
  941. int __init amd_special_default_mtrr(void)
  942. {
  943. u32 l, h;
  944. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  945. return 0;
  946. if (boot_cpu_data.x86 < 0xf || boot_cpu_data.x86 > 0x11)
  947. return 0;
  948. /* In case some hypervisor doesn't pass SYSCFG through */
  949. if (rdmsr_safe(MSR_K8_SYSCFG, &l, &h) < 0)
  950. return 0;
  951. /*
  952. * Memory between 4GB and top of mem is forced WB by this magic bit.
  953. * Reserved before K8RevF, but should be zero there.
  954. */
  955. if ((l & (Tom2Enabled | Tom2ForceMemTypeWB)) ==
  956. (Tom2Enabled | Tom2ForceMemTypeWB))
  957. return 1;
  958. return 0;
  959. }
  960. static u64 __init real_trim_memory(unsigned long start_pfn, unsigned long limit_pfn)
  961. {
  962. u64 trim_start, trim_size;
  963. trim_start = start_pfn;
  964. trim_start <<= PAGE_SHIFT;
  965. trim_size = limit_pfn;
  966. trim_size <<= PAGE_SHIFT;
  967. trim_size -= trim_start;
  968. return update_memory_range(trim_start, trim_size, E820_RAM,
  969. E820_RESERVED);
  970. }
  971. /**
  972. * mtrr_trim_uncached_memory - trim RAM not covered by MTRRs
  973. * @end_pfn: ending page frame number
  974. *
  975. * Some buggy BIOSes don't setup the MTRRs properly for systems with certain
  976. * memory configurations. This routine checks that the highest MTRR matches
  977. * the end of memory, to make sure the MTRRs having a write back type cover
  978. * all of the memory the kernel is intending to use. If not, it'll trim any
  979. * memory off the end by adjusting end_pfn, removing it from the kernel's
  980. * allocation pools, warning the user with an obnoxious message.
  981. */
  982. int __init mtrr_trim_uncached_memory(unsigned long end_pfn)
  983. {
  984. unsigned long i, base, size, highest_pfn = 0, def, dummy;
  985. mtrr_type type;
  986. struct res_range range[RANGE_NUM];
  987. int nr_range;
  988. u64 total_real_trim_size;
  989. /* extra one for all 0 */
  990. int num[MTRR_NUM_TYPES + 1];
  991. /*
  992. * Make sure we only trim uncachable memory on machines that
  993. * support the Intel MTRR architecture:
  994. */
  995. if (!is_cpu(INTEL) || disable_mtrr_trim)
  996. return 0;
  997. rdmsr(MTRRdefType_MSR, def, dummy);
  998. def &= 0xff;
  999. if (def != MTRR_TYPE_UNCACHABLE)
  1000. return 0;
  1001. /* Find highest cached pfn */
  1002. for (i = 0; i < num_var_ranges; i++) {
  1003. mtrr_if->get(i, &base, &size, &type);
  1004. if (type != MTRR_TYPE_WRBACK)
  1005. continue;
  1006. if (highest_pfn < base + size)
  1007. highest_pfn = base + size;
  1008. }
  1009. /* kvm/qemu doesn't have mtrr set right, don't trim them all */
  1010. if (!highest_pfn) {
  1011. if (!kvm_para_available()) {
  1012. printk(KERN_WARNING
  1013. "WARNING: strange, CPU MTRRs all blank?\n");
  1014. WARN_ON(1);
  1015. }
  1016. return 0;
  1017. }
  1018. /* check entries number */
  1019. memset(num, 0, sizeof(num));
  1020. for (i = 0; i < num_var_ranges; i++) {
  1021. mtrr_if->get(i, &base, &size, &type);
  1022. if (type >= MTRR_NUM_TYPES)
  1023. continue;
  1024. if (!size)
  1025. type = MTRR_NUM_TYPES;
  1026. num[type]++;
  1027. }
  1028. /* no entry for WB? */
  1029. if (!num[MTRR_TYPE_WRBACK])
  1030. return 0;
  1031. /* check if we only had WB and UC */
  1032. if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
  1033. num_var_ranges - num[MTRR_NUM_TYPES])
  1034. return 0;
  1035. memset(range, 0, sizeof(range));
  1036. nr_range = 0;
  1037. if (mtrr_tom2) {
  1038. range[nr_range].start = (1ULL<<(32 - PAGE_SHIFT));
  1039. range[nr_range].end = (mtrr_tom2 >> PAGE_SHIFT) - 1;
  1040. if (highest_pfn < range[nr_range].end + 1)
  1041. highest_pfn = range[nr_range].end + 1;
  1042. nr_range++;
  1043. }
  1044. nr_range = x86_get_mtrr_mem_range(range, nr_range, 0, 0);
  1045. total_real_trim_size = 0;
  1046. /* check the head */
  1047. if (range[0].start)
  1048. total_real_trim_size += real_trim_memory(0, range[0].start);
  1049. /* check the holes */
  1050. for (i = 0; i < nr_range - 1; i++) {
  1051. if (range[i].end + 1 < range[i+1].start)
  1052. total_real_trim_size += real_trim_memory(range[i].end + 1, range[i+1].start);
  1053. }
  1054. /* check the top */
  1055. i = nr_range - 1;
  1056. if (range[i].end + 1 < end_pfn)
  1057. total_real_trim_size += real_trim_memory(range[i].end + 1, end_pfn);
  1058. if (total_real_trim_size) {
  1059. printk(KERN_WARNING "WARNING: BIOS bug: CPU MTRRs don't cover"
  1060. " all of memory, losing %lluMB of RAM.\n",
  1061. total_real_trim_size >> 20);
  1062. if (enable_mtrr_cleanup < 1)
  1063. WARN_ON(1);
  1064. printk(KERN_INFO "update e820 for mtrr\n");
  1065. update_e820();
  1066. return 1;
  1067. }
  1068. return 0;
  1069. }
  1070. /**
  1071. * mtrr_bp_init - initialize mtrrs on the boot CPU
  1072. *
  1073. * This needs to be called early; before any of the other CPUs are
  1074. * initialized (i.e. before smp_init()).
  1075. *
  1076. */
  1077. void __init mtrr_bp_init(void)
  1078. {
  1079. u32 phys_addr;
  1080. init_ifs();
  1081. phys_addr = 32;
  1082. if (cpu_has_mtrr) {
  1083. mtrr_if = &generic_mtrr_ops;
  1084. size_or_mask = 0xff000000; /* 36 bits */
  1085. size_and_mask = 0x00f00000;
  1086. phys_addr = 36;
  1087. /* This is an AMD specific MSR, but we assume(hope?) that
  1088. Intel will implement it to when they extend the address
  1089. bus of the Xeon. */
  1090. if (cpuid_eax(0x80000000) >= 0x80000008) {
  1091. phys_addr = cpuid_eax(0x80000008) & 0xff;
  1092. /* CPUID workaround for Intel 0F33/0F34 CPU */
  1093. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1094. boot_cpu_data.x86 == 0xF &&
  1095. boot_cpu_data.x86_model == 0x3 &&
  1096. (boot_cpu_data.x86_mask == 0x3 ||
  1097. boot_cpu_data.x86_mask == 0x4))
  1098. phys_addr = 36;
  1099. size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  1100. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  1101. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  1102. boot_cpu_data.x86 == 6) {
  1103. /* VIA C* family have Intel style MTRRs, but
  1104. don't support PAE */
  1105. size_or_mask = 0xfff00000; /* 32 bits */
  1106. size_and_mask = 0;
  1107. phys_addr = 32;
  1108. }
  1109. } else {
  1110. switch (boot_cpu_data.x86_vendor) {
  1111. case X86_VENDOR_AMD:
  1112. if (cpu_has_k6_mtrr) {
  1113. /* Pre-Athlon (K6) AMD CPU MTRRs */
  1114. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  1115. size_or_mask = 0xfff00000; /* 32 bits */
  1116. size_and_mask = 0;
  1117. }
  1118. break;
  1119. case X86_VENDOR_CENTAUR:
  1120. if (cpu_has_centaur_mcr) {
  1121. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  1122. size_or_mask = 0xfff00000; /* 32 bits */
  1123. size_and_mask = 0;
  1124. }
  1125. break;
  1126. case X86_VENDOR_CYRIX:
  1127. if (cpu_has_cyrix_arr) {
  1128. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  1129. size_or_mask = 0xfff00000; /* 32 bits */
  1130. size_and_mask = 0;
  1131. }
  1132. break;
  1133. default:
  1134. break;
  1135. }
  1136. }
  1137. if (mtrr_if) {
  1138. set_num_var_ranges();
  1139. init_table();
  1140. if (use_intel()) {
  1141. get_mtrr_state();
  1142. if (mtrr_cleanup(phys_addr))
  1143. mtrr_if->set_all();
  1144. }
  1145. }
  1146. }
  1147. void mtrr_ap_init(void)
  1148. {
  1149. unsigned long flags;
  1150. if (!mtrr_if || !use_intel())
  1151. return;
  1152. /*
  1153. * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
  1154. * but this routine will be called in cpu boot time, holding the lock
  1155. * breaks it. This routine is called in two cases: 1.very earily time
  1156. * of software resume, when there absolutely isn't mtrr entry changes;
  1157. * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
  1158. * prevent mtrr entry changes
  1159. */
  1160. local_irq_save(flags);
  1161. mtrr_if->set_all();
  1162. local_irq_restore(flags);
  1163. }
  1164. /**
  1165. * Save current fixed-range MTRR state of the BSP
  1166. */
  1167. void mtrr_save_state(void)
  1168. {
  1169. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1, 1);
  1170. }
  1171. static int __init mtrr_init_finialize(void)
  1172. {
  1173. if (!mtrr_if)
  1174. return 0;
  1175. if (use_intel()) {
  1176. if (enable_mtrr_cleanup < 1)
  1177. mtrr_state_warn();
  1178. } else {
  1179. /* The CPUs haven't MTRR and seem to not support SMP. They have
  1180. * specific drivers, we use a tricky method to support
  1181. * suspend/resume for them.
  1182. * TBD: is there any system with such CPU which supports
  1183. * suspend/resume? if no, we should remove the code.
  1184. */
  1185. sysdev_driver_register(&cpu_sysdev_class,
  1186. &mtrr_sysdev_driver);
  1187. }
  1188. return 0;
  1189. }
  1190. subsys_initcall(mtrr_init_finialize);