main.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. #ifdef CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT
  543. static int enable_mtrr_cleanup __initdata = 1;
  544. #else
  545. static int enable_mtrr_cleanup __initdata;
  546. #endif
  547. #else
  548. static int enable_mtrr_cleanup __initdata = -1;
  549. #endif
  550. static int __init disable_mtrr_cleanup_setup(char *str)
  551. {
  552. if (enable_mtrr_cleanup != -1)
  553. enable_mtrr_cleanup = 0;
  554. return 0;
  555. }
  556. early_param("disable_mtrr_cleanup", disable_mtrr_cleanup_setup);
  557. static int __init enable_mtrr_cleanup_setup(char *str)
  558. {
  559. if (enable_mtrr_cleanup != -1)
  560. enable_mtrr_cleanup = 1;
  561. return 0;
  562. }
  563. early_param("enble_mtrr_cleanup", enable_mtrr_cleanup_setup);
  564. #define RANGE_NUM 256
  565. struct res_range {
  566. unsigned long start;
  567. unsigned long end;
  568. };
  569. static int __init add_range(struct res_range *range, int nr_range, unsigned long start,
  570. unsigned long end, int merge)
  571. {
  572. int i;
  573. if (!merge)
  574. goto addit;
  575. /* try to merge it with old one */
  576. for (i = 0; i < nr_range; i++) {
  577. unsigned long final_start, final_end;
  578. unsigned long common_start, common_end;
  579. if (!range[i].end)
  580. continue;
  581. common_start = max(range[i].start, start);
  582. common_end = min(range[i].end, end);
  583. if (common_start > common_end + 1)
  584. continue;
  585. final_start = min(range[i].start, start);
  586. final_end = max(range[i].end, end);
  587. range[i].start = final_start;
  588. range[i].end = final_end;
  589. return nr_range;
  590. }
  591. addit:
  592. /* need to add that */
  593. if (nr_range >= RANGE_NUM)
  594. return nr_range;
  595. range[nr_range].start = start;
  596. range[nr_range].end = end;
  597. nr_range++;
  598. return nr_range;
  599. }
  600. static void __init subtract_range(struct res_range *range, unsigned long start,
  601. unsigned long end)
  602. {
  603. int i;
  604. int j;
  605. for (j = 0; j < RANGE_NUM; j++) {
  606. if (!range[j].end)
  607. continue;
  608. if (start <= range[j].start && end >= range[j].end) {
  609. range[j].start = 0;
  610. range[j].end = 0;
  611. continue;
  612. }
  613. if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
  614. range[j].start = end + 1;
  615. continue;
  616. }
  617. if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
  618. range[j].end = start - 1;
  619. continue;
  620. }
  621. if (start > range[j].start && end < range[j].end) {
  622. /* find the new spare */
  623. for (i = 0; i < RANGE_NUM; i++) {
  624. if (range[i].end == 0)
  625. break;
  626. }
  627. if (i < RANGE_NUM) {
  628. range[i].end = range[j].end;
  629. range[i].start = end + 1;
  630. } else {
  631. printk(KERN_ERR "run of slot in ranges\n");
  632. }
  633. range[j].end = start - 1;
  634. continue;
  635. }
  636. }
  637. }
  638. static int __init cmp_range(const void *x1, const void *x2)
  639. {
  640. const struct res_range *r1 = x1;
  641. const struct res_range *r2 = x2;
  642. long start1, start2;
  643. start1 = r1->start;
  644. start2 = r2->start;
  645. return start1 - start2;
  646. }
  647. struct var_mtrr_state {
  648. unsigned long range_startk, range_sizek;
  649. unsigned long chunk_sizek;
  650. unsigned long gran_sizek;
  651. unsigned int reg;
  652. unsigned address_bits;
  653. };
  654. static void __init set_var_mtrr(
  655. unsigned int reg, unsigned long basek, unsigned long sizek,
  656. unsigned char type, unsigned address_bits)
  657. {
  658. u32 base_lo, base_hi, mask_lo, mask_hi;
  659. unsigned address_mask_high;
  660. if (!sizek) {
  661. fill_mtrr_var_range(reg, 0, 0, 0, 0);
  662. return;
  663. }
  664. address_mask_high = ((1u << (address_bits - 32u)) - 1u);
  665. base_hi = basek >> 22;
  666. base_lo = basek << 10;
  667. if (sizek < 4*1024*1024) {
  668. mask_hi = address_mask_high;
  669. mask_lo = ~((sizek << 10) - 1);
  670. } else {
  671. mask_hi = address_mask_high & (~((sizek >> 22) - 1));
  672. mask_lo = 0;
  673. }
  674. base_lo |= type;
  675. mask_lo |= 0x800;
  676. fill_mtrr_var_range(reg, base_lo, base_hi, mask_lo, mask_hi);
  677. }
  678. static unsigned int __init range_to_mtrr(unsigned int reg,
  679. unsigned long range_startk, unsigned long range_sizek,
  680. unsigned char type, 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 range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigned long basek)
  710. {
  711. unsigned long hole_basek, hole_sizek;
  712. unsigned long range0_basek, range0_sizek;
  713. unsigned long range_basek, range_sizek;
  714. unsigned long chunk_sizek;
  715. unsigned long gran_sizek;
  716. hole_basek = 0;
  717. hole_sizek = 0;
  718. chunk_sizek = state->chunk_sizek;
  719. gran_sizek = state->gran_sizek;
  720. /* align with gran size, prevent small block used up MTRRs */
  721. range_basek = ALIGN(state->range_startk, gran_sizek);
  722. if ((range_basek > basek) && basek)
  723. return;
  724. range_sizek = ALIGN(state->range_sizek - (range_basek - state->range_startk), gran_sizek);
  725. while (range_basek + range_sizek > (state->range_startk + state->range_sizek)) {
  726. range_sizek -= gran_sizek;
  727. if (!range_sizek)
  728. return;
  729. }
  730. state->range_startk = range_basek;
  731. state->range_sizek = range_sizek;
  732. /* try to append some small hole */
  733. range0_basek = state->range_startk;
  734. range0_sizek = ALIGN(state->range_sizek, chunk_sizek);
  735. if ((range0_sizek == state->range_sizek) ||
  736. ((range0_basek + range0_sizek - chunk_sizek > basek) && basek)) {
  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. }
  742. range0_sizek -= chunk_sizek;
  743. printk(KERN_INFO "range0: %016lx - %016lx\n", range0_basek<<10, (range0_basek + range0_sizek)<<10);
  744. state->reg = range_to_mtrr(state->reg, range0_basek,
  745. range0_sizek, MTRR_TYPE_WRBACK, state->address_bits);
  746. range_basek = range0_basek + range0_sizek;
  747. range_sizek = chunk_sizek;
  748. if (range_sizek - (state->range_sizek - range0_sizek) < (chunk_sizek >> 1)) {
  749. hole_sizek = range_sizek - (state->range_sizek - range0_sizek);
  750. hole_basek = range_basek + range_sizek - hole_sizek;
  751. } else
  752. range_sizek = state->range_sizek - range0_sizek;
  753. printk(KERN_INFO "range: %016lx - %016lx\n", range_basek<<10, (range_basek + range_sizek)<<10);
  754. state->reg = range_to_mtrr(state->reg, range_basek,
  755. range_sizek, MTRR_TYPE_WRBACK, state->address_bits);
  756. if (hole_sizek) {
  757. printk(KERN_INFO "hole: %016lx - %016lx\n", hole_basek<<10, (hole_basek + hole_sizek)<<10);
  758. state->reg = range_to_mtrr(state->reg, hole_basek,
  759. hole_sizek, MTRR_TYPE_UNCACHABLE, state->address_bits);
  760. }
  761. }
  762. static void __init set_var_mtrr_range(struct var_mtrr_state *state, unsigned long base_pfn, unsigned long size_pfn)
  763. {
  764. unsigned long basek, sizek;
  765. if (state->reg >= num_var_ranges)
  766. return;
  767. basek = base_pfn << (PAGE_SHIFT - 10);
  768. sizek = size_pfn << (PAGE_SHIFT - 10);
  769. /* See if I can merge with the last range */
  770. if ((basek <= 1024) || (state->range_startk + state->range_sizek == basek)) {
  771. unsigned long endk = basek + sizek;
  772. state->range_sizek = endk - state->range_startk;
  773. return;
  774. }
  775. /* Write the range mtrrs */
  776. if (state->range_sizek != 0) {
  777. range_to_mtrr_with_hole(state, basek);
  778. state->range_startk = 0;
  779. state->range_sizek = 0;
  780. }
  781. /* Allocate an msr */
  782. state->range_startk = basek;
  783. state->range_sizek = sizek;
  784. }
  785. /* mininum size of mtrr block that can take hole */
  786. static u64 mtrr_chunk_size __initdata = (256ULL<<20);
  787. static int __init parse_mtrr_chunk_size_opt(char *p)
  788. {
  789. if (!p)
  790. return -EINVAL;
  791. mtrr_chunk_size = memparse(p, &p);
  792. return 0;
  793. }
  794. early_param("mtrr_chunk_size", parse_mtrr_chunk_size_opt);
  795. /* granity of mtrr of block */
  796. static u64 mtrr_gran_size __initdata = (64ULL<<20);
  797. static int __init parse_mtrr_gran_size_opt(char *p)
  798. {
  799. if (!p)
  800. return -EINVAL;
  801. mtrr_gran_size = memparse(p, &p);
  802. return 0;
  803. }
  804. early_param("mtrr_gran_size", parse_mtrr_gran_size_opt);
  805. static void __init x86_setup_var_mtrrs(struct res_range *range, int nr_range, unsigned address_bits)
  806. {
  807. struct var_mtrr_state var_state;
  808. int i;
  809. var_state.range_startk = 0;
  810. var_state.range_sizek = 0;
  811. var_state.reg = 0;
  812. var_state.address_bits = address_bits;
  813. var_state.chunk_sizek = mtrr_chunk_size >> 10;
  814. var_state.gran_sizek = mtrr_gran_size >> 10;
  815. /* Write the range etc */
  816. for (i = 0; i < nr_range; i++)
  817. set_var_mtrr_range(&var_state, range[i].start, range[i].end - range[i].start + 1);
  818. /* Write the last range */
  819. range_to_mtrr_with_hole(&var_state, 0);
  820. printk(KERN_INFO "DONE variable MTRRs\n");
  821. /* Clear out the extra MTRR's */
  822. while (var_state.reg < num_var_ranges)
  823. set_var_mtrr(var_state.reg++, 0, 0, 0, var_state.address_bits);
  824. }
  825. static int __init x86_get_mtrr_mem_range(struct res_range *range, int nr_range, unsigned long extra_remove_base, unsigned long extra_remove_size)
  826. {
  827. unsigned long i, base, size;
  828. mtrr_type type;
  829. for (i = 0; i < num_var_ranges; i++) {
  830. mtrr_if->get(i, &base, &size, &type);
  831. if (type != MTRR_TYPE_WRBACK)
  832. continue;
  833. nr_range = add_range(range, nr_range, base, base + size - 1, 1);
  834. }
  835. printk(KERN_INFO "After WB checking\n");
  836. for (i = 0; i < nr_range; i++)
  837. printk(KERN_INFO "MTRR MAP PFN: %016lx - %016lx\n", range[i].start, range[i].end + 1);
  838. /* take out UC ranges */
  839. for (i = 0; i < num_var_ranges; i++) {
  840. mtrr_if->get(i, &base, &size, &type);
  841. if (type != MTRR_TYPE_UNCACHABLE)
  842. continue;
  843. if (!size)
  844. continue;
  845. subtract_range(range, base, base + size - 1);
  846. }
  847. if (extra_remove_size)
  848. subtract_range(range, extra_remove_base, extra_remove_base + extra_remove_size - 1);
  849. /* get new range num */
  850. nr_range = 0;
  851. for (i = 0; i < RANGE_NUM; i++) {
  852. if (!range[i].end)
  853. continue;
  854. nr_range++;
  855. }
  856. printk(KERN_INFO "After UC checking\n");
  857. for (i = 0; i < nr_range; i++)
  858. printk(KERN_INFO "MTRR MAP PFN: %016lx - %016lx\n", range[i].start, range[i].end + 1);
  859. /* sort the ranges */
  860. sort(range, nr_range, sizeof(struct res_range), cmp_range, NULL);
  861. printk(KERN_INFO "After sorting\n");
  862. for (i = 0; i < nr_range; i++)
  863. printk(KERN_INFO "MTRR MAP PFN: %016lx - %016lx\n", range[i].start, range[i].end + 1);
  864. return nr_range;
  865. }
  866. static int __init mtrr_cleanup(unsigned address_bits)
  867. {
  868. unsigned long i, base, size, def, dummy;
  869. mtrr_type type;
  870. struct res_range range[RANGE_NUM];
  871. int nr_range;
  872. unsigned long extra_remove_base, extra_remove_size;
  873. /* extra one for all 0 */
  874. int num[MTRR_NUM_TYPES + 1];
  875. if (!is_cpu(INTEL) || enable_mtrr_cleanup < 1)
  876. return 0;
  877. rdmsr(MTRRdefType_MSR, def, dummy);
  878. def &= 0xff;
  879. if (def != MTRR_TYPE_UNCACHABLE)
  880. return 0;
  881. /* check entries number */
  882. memset(num, 0, sizeof(num));
  883. for (i = 0; i < num_var_ranges; i++) {
  884. mtrr_if->get(i, &base, &size, &type);
  885. if (type >= MTRR_NUM_TYPES)
  886. continue;
  887. if (!size)
  888. type = MTRR_NUM_TYPES;
  889. num[type]++;
  890. }
  891. /* check if we got UC entries */
  892. if (!num[MTRR_TYPE_UNCACHABLE])
  893. return 0;
  894. /* check if we only had WB and UC */
  895. if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
  896. num_var_ranges - num[MTRR_NUM_TYPES])
  897. return 0;
  898. memset(range, 0, sizeof(range));
  899. extra_remove_size = 0;
  900. if (mtrr_tom2) {
  901. extra_remove_base = 1 << (32 - PAGE_SHIFT);
  902. extra_remove_size = (mtrr_tom2>>PAGE_SHIFT) - extra_remove_base;
  903. }
  904. nr_range = x86_get_mtrr_mem_range(range, 0, extra_remove_base, extra_remove_size);
  905. /* convert ranges to var ranges state */
  906. x86_setup_var_mtrrs(range, nr_range, address_bits);
  907. return 1;
  908. }
  909. static int disable_mtrr_trim;
  910. static int __init disable_mtrr_trim_setup(char *str)
  911. {
  912. disable_mtrr_trim = 1;
  913. return 0;
  914. }
  915. early_param("disable_mtrr_trim", disable_mtrr_trim_setup);
  916. /*
  917. * Newer AMD K8s and later CPUs have a special magic MSR way to force WB
  918. * for memory >4GB. Check for that here.
  919. * Note this won't check if the MTRRs < 4GB where the magic bit doesn't
  920. * apply to are wrong, but so far we don't know of any such case in the wild.
  921. */
  922. #define Tom2Enabled (1U << 21)
  923. #define Tom2ForceMemTypeWB (1U << 22)
  924. int __init amd_special_default_mtrr(void)
  925. {
  926. u32 l, h;
  927. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  928. return 0;
  929. if (boot_cpu_data.x86 < 0xf || boot_cpu_data.x86 > 0x11)
  930. return 0;
  931. /* In case some hypervisor doesn't pass SYSCFG through */
  932. if (rdmsr_safe(MSR_K8_SYSCFG, &l, &h) < 0)
  933. return 0;
  934. /*
  935. * Memory between 4GB and top of mem is forced WB by this magic bit.
  936. * Reserved before K8RevF, but should be zero there.
  937. */
  938. if ((l & (Tom2Enabled | Tom2ForceMemTypeWB)) ==
  939. (Tom2Enabled | Tom2ForceMemTypeWB))
  940. return 1;
  941. return 0;
  942. }
  943. static u64 __init real_trim_memory(unsigned long start_pfn, unsigned long limit_pfn)
  944. {
  945. u64 trim_start, trim_size;
  946. trim_start = start_pfn;
  947. trim_start <<= PAGE_SHIFT;
  948. trim_size = limit_pfn;
  949. trim_size <<= PAGE_SHIFT;
  950. trim_size -= trim_start;
  951. return update_memory_range(trim_start, trim_size, E820_RAM,
  952. E820_RESERVED);
  953. }
  954. /**
  955. * mtrr_trim_uncached_memory - trim RAM not covered by MTRRs
  956. * @end_pfn: ending page frame number
  957. *
  958. * Some buggy BIOSes don't setup the MTRRs properly for systems with certain
  959. * memory configurations. This routine checks that the highest MTRR matches
  960. * the end of memory, to make sure the MTRRs having a write back type cover
  961. * all of the memory the kernel is intending to use. If not, it'll trim any
  962. * memory off the end by adjusting end_pfn, removing it from the kernel's
  963. * allocation pools, warning the user with an obnoxious message.
  964. */
  965. int __init mtrr_trim_uncached_memory(unsigned long end_pfn)
  966. {
  967. unsigned long i, base, size, highest_pfn = 0, def, dummy;
  968. mtrr_type type;
  969. struct res_range range[RANGE_NUM];
  970. int nr_range;
  971. u64 total_real_trim_size;
  972. /* extra one for all 0 */
  973. int num[MTRR_NUM_TYPES + 1];
  974. /*
  975. * Make sure we only trim uncachable memory on machines that
  976. * support the Intel MTRR architecture:
  977. */
  978. if (!is_cpu(INTEL) || disable_mtrr_trim)
  979. return 0;
  980. rdmsr(MTRRdefType_MSR, def, dummy);
  981. def &= 0xff;
  982. if (def != MTRR_TYPE_UNCACHABLE)
  983. return 0;
  984. /* Find highest cached pfn */
  985. for (i = 0; i < num_var_ranges; i++) {
  986. mtrr_if->get(i, &base, &size, &type);
  987. if (type != MTRR_TYPE_WRBACK)
  988. continue;
  989. if (highest_pfn < base + size)
  990. highest_pfn = base + size;
  991. }
  992. /* kvm/qemu doesn't have mtrr set right, don't trim them all */
  993. if (!highest_pfn) {
  994. if (!kvm_para_available()) {
  995. printk(KERN_WARNING
  996. "WARNING: strange, CPU MTRRs all blank?\n");
  997. WARN_ON(1);
  998. }
  999. return 0;
  1000. }
  1001. /* check entries number */
  1002. memset(num, 0, sizeof(num));
  1003. for (i = 0; i < num_var_ranges; i++) {
  1004. mtrr_if->get(i, &base, &size, &type);
  1005. if (type >= MTRR_NUM_TYPES)
  1006. continue;
  1007. if (!size)
  1008. type = MTRR_NUM_TYPES;
  1009. num[type]++;
  1010. }
  1011. /* no entry for WB? */
  1012. if (!num[MTRR_TYPE_WRBACK])
  1013. return 0;
  1014. /* check if we only had WB and UC */
  1015. if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
  1016. num_var_ranges - num[MTRR_NUM_TYPES])
  1017. return 0;
  1018. memset(range, 0, sizeof(range));
  1019. nr_range = 0;
  1020. if (mtrr_tom2) {
  1021. range[nr_range].start = (1ULL<<(32 - PAGE_SHIFT));
  1022. range[nr_range].end = (mtrr_tom2 >> PAGE_SHIFT) - 1;
  1023. if (highest_pfn < range[nr_range].end + 1)
  1024. highest_pfn = range[nr_range].end + 1;
  1025. nr_range++;
  1026. }
  1027. nr_range = x86_get_mtrr_mem_range(range, nr_range, 0, 0);
  1028. total_real_trim_size = 0;
  1029. /* check the head */
  1030. if (range[0].start)
  1031. total_real_trim_size += real_trim_memory(0, range[0].start);
  1032. /* check the holes */
  1033. for (i = 0; i < nr_range - 1; i++) {
  1034. if (range[i].end + 1 < range[i+1].start)
  1035. total_real_trim_size += real_trim_memory(range[i].end + 1, range[i+1].start);
  1036. }
  1037. /* check the top */
  1038. i = nr_range - 1;
  1039. if (range[i].end + 1 < end_pfn)
  1040. total_real_trim_size += real_trim_memory(range[i].end + 1, end_pfn);
  1041. if (total_real_trim_size) {
  1042. printk(KERN_WARNING "WARNING: BIOS bug: CPU MTRRs don't cover"
  1043. " all of memory, losing %lluMB of RAM.\n",
  1044. total_real_trim_size >> 20);
  1045. if (enable_mtrr_cleanup < 1)
  1046. WARN_ON(1);
  1047. printk(KERN_INFO "update e820 for mtrr\n");
  1048. update_e820();
  1049. return 1;
  1050. }
  1051. return 0;
  1052. }
  1053. /**
  1054. * mtrr_bp_init - initialize mtrrs on the boot CPU
  1055. *
  1056. * This needs to be called early; before any of the other CPUs are
  1057. * initialized (i.e. before smp_init()).
  1058. *
  1059. */
  1060. void __init mtrr_bp_init(void)
  1061. {
  1062. u32 phys_addr;
  1063. init_ifs();
  1064. phys_addr = 32;
  1065. if (cpu_has_mtrr) {
  1066. mtrr_if = &generic_mtrr_ops;
  1067. size_or_mask = 0xff000000; /* 36 bits */
  1068. size_and_mask = 0x00f00000;
  1069. phys_addr = 36;
  1070. /* This is an AMD specific MSR, but we assume(hope?) that
  1071. Intel will implement it to when they extend the address
  1072. bus of the Xeon. */
  1073. if (cpuid_eax(0x80000000) >= 0x80000008) {
  1074. phys_addr = cpuid_eax(0x80000008) & 0xff;
  1075. /* CPUID workaround for Intel 0F33/0F34 CPU */
  1076. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1077. boot_cpu_data.x86 == 0xF &&
  1078. boot_cpu_data.x86_model == 0x3 &&
  1079. (boot_cpu_data.x86_mask == 0x3 ||
  1080. boot_cpu_data.x86_mask == 0x4))
  1081. phys_addr = 36;
  1082. size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  1083. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  1084. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  1085. boot_cpu_data.x86 == 6) {
  1086. /* VIA C* family have Intel style MTRRs, but
  1087. don't support PAE */
  1088. size_or_mask = 0xfff00000; /* 32 bits */
  1089. size_and_mask = 0;
  1090. phys_addr = 32;
  1091. }
  1092. } else {
  1093. switch (boot_cpu_data.x86_vendor) {
  1094. case X86_VENDOR_AMD:
  1095. if (cpu_has_k6_mtrr) {
  1096. /* Pre-Athlon (K6) AMD CPU MTRRs */
  1097. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  1098. size_or_mask = 0xfff00000; /* 32 bits */
  1099. size_and_mask = 0;
  1100. }
  1101. break;
  1102. case X86_VENDOR_CENTAUR:
  1103. if (cpu_has_centaur_mcr) {
  1104. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  1105. size_or_mask = 0xfff00000; /* 32 bits */
  1106. size_and_mask = 0;
  1107. }
  1108. break;
  1109. case X86_VENDOR_CYRIX:
  1110. if (cpu_has_cyrix_arr) {
  1111. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  1112. size_or_mask = 0xfff00000; /* 32 bits */
  1113. size_and_mask = 0;
  1114. }
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. }
  1120. if (mtrr_if) {
  1121. set_num_var_ranges();
  1122. init_table();
  1123. if (use_intel()) {
  1124. get_mtrr_state();
  1125. if (mtrr_cleanup(phys_addr))
  1126. mtrr_if->set_all();
  1127. }
  1128. }
  1129. }
  1130. void mtrr_ap_init(void)
  1131. {
  1132. unsigned long flags;
  1133. if (!mtrr_if || !use_intel())
  1134. return;
  1135. /*
  1136. * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
  1137. * but this routine will be called in cpu boot time, holding the lock
  1138. * breaks it. This routine is called in two cases: 1.very earily time
  1139. * of software resume, when there absolutely isn't mtrr entry changes;
  1140. * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
  1141. * prevent mtrr entry changes
  1142. */
  1143. local_irq_save(flags);
  1144. mtrr_if->set_all();
  1145. local_irq_restore(flags);
  1146. }
  1147. /**
  1148. * Save current fixed-range MTRR state of the BSP
  1149. */
  1150. void mtrr_save_state(void)
  1151. {
  1152. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1, 1);
  1153. }
  1154. static int __init mtrr_init_finialize(void)
  1155. {
  1156. if (!mtrr_if)
  1157. return 0;
  1158. if (use_intel()) {
  1159. if (enable_mtrr_cleanup < 1)
  1160. mtrr_state_warn();
  1161. } else {
  1162. /* The CPUs haven't MTRR and seem to not support SMP. They have
  1163. * specific drivers, we use a tricky method to support
  1164. * suspend/resume for them.
  1165. * TBD: is there any system with such CPU which supports
  1166. * suspend/resume? if no, we should remove the code.
  1167. */
  1168. sysdev_driver_register(&cpu_sysdev_class,
  1169. &mtrr_sysdev_driver);
  1170. }
  1171. return 0;
  1172. }
  1173. subsys_initcall(mtrr_init_finialize);