main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 <asm/e820.h>
  33. #include <asm/mtrr.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/processor.h>
  36. #include <asm/msr.h>
  37. #include <asm/kvm_para.h>
  38. #include "mtrr.h"
  39. u32 num_var_ranges = 0;
  40. unsigned int mtrr_usage_table[MAX_VAR_RANGES];
  41. static DEFINE_MUTEX(mtrr_mutex);
  42. u64 size_or_mask, size_and_mask;
  43. static struct mtrr_ops * mtrr_ops[X86_VENDOR_NUM] = {};
  44. struct mtrr_ops * mtrr_if = NULL;
  45. static void set_mtrr(unsigned int reg, unsigned long base,
  46. unsigned long size, mtrr_type type);
  47. void set_mtrr_ops(struct mtrr_ops * ops)
  48. {
  49. if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
  50. mtrr_ops[ops->vendor] = ops;
  51. }
  52. /* Returns non-zero if we have the write-combining memory type */
  53. static int have_wrcomb(void)
  54. {
  55. struct pci_dev *dev;
  56. u8 rev;
  57. if ((dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) != NULL) {
  58. /* ServerWorks LE chipsets < rev 6 have problems with write-combining
  59. Don't allow it and leave room for other chipsets to be tagged */
  60. if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
  61. dev->device == PCI_DEVICE_ID_SERVERWORKS_LE) {
  62. pci_read_config_byte(dev, PCI_CLASS_REVISION, &rev);
  63. if (rev <= 5) {
  64. printk(KERN_INFO "mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
  65. pci_dev_put(dev);
  66. return 0;
  67. }
  68. }
  69. /* Intel 450NX errata # 23. Non ascending cacheline evictions to
  70. write combining memory may resulting in data corruption */
  71. if (dev->vendor == PCI_VENDOR_ID_INTEL &&
  72. dev->device == PCI_DEVICE_ID_INTEL_82451NX) {
  73. printk(KERN_INFO "mtrr: Intel 450NX MMC detected. Write-combining disabled.\n");
  74. pci_dev_put(dev);
  75. return 0;
  76. }
  77. pci_dev_put(dev);
  78. }
  79. return (mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0);
  80. }
  81. /* This function returns the number of variable MTRRs */
  82. static void __init set_num_var_ranges(void)
  83. {
  84. unsigned long config = 0, dummy;
  85. if (use_intel()) {
  86. rdmsr(MTRRcap_MSR, config, dummy);
  87. } else if (is_cpu(AMD))
  88. config = 2;
  89. else if (is_cpu(CYRIX) || is_cpu(CENTAUR))
  90. config = 8;
  91. num_var_ranges = config & 0xff;
  92. }
  93. static void __init init_table(void)
  94. {
  95. int i, max;
  96. max = num_var_ranges;
  97. for (i = 0; i < max; i++)
  98. mtrr_usage_table[i] = 1;
  99. }
  100. struct set_mtrr_data {
  101. atomic_t count;
  102. atomic_t gate;
  103. unsigned long smp_base;
  104. unsigned long smp_size;
  105. unsigned int smp_reg;
  106. mtrr_type smp_type;
  107. };
  108. static void ipi_handler(void *info)
  109. /* [SUMMARY] Synchronisation handler. Executed by "other" CPUs.
  110. [RETURNS] Nothing.
  111. */
  112. {
  113. #ifdef CONFIG_SMP
  114. struct set_mtrr_data *data = info;
  115. unsigned long flags;
  116. local_irq_save(flags);
  117. atomic_dec(&data->count);
  118. while(!atomic_read(&data->gate))
  119. cpu_relax();
  120. /* The master has cleared me to execute */
  121. if (data->smp_reg != ~0U)
  122. mtrr_if->set(data->smp_reg, data->smp_base,
  123. data->smp_size, data->smp_type);
  124. else
  125. mtrr_if->set_all();
  126. atomic_dec(&data->count);
  127. while(atomic_read(&data->gate))
  128. cpu_relax();
  129. atomic_dec(&data->count);
  130. local_irq_restore(flags);
  131. #endif
  132. }
  133. static inline int types_compatible(mtrr_type type1, mtrr_type type2) {
  134. return type1 == MTRR_TYPE_UNCACHABLE ||
  135. type2 == MTRR_TYPE_UNCACHABLE ||
  136. (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
  137. (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
  138. }
  139. /**
  140. * set_mtrr - update mtrrs on all processors
  141. * @reg: mtrr in question
  142. * @base: mtrr base
  143. * @size: mtrr size
  144. * @type: mtrr type
  145. *
  146. * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
  147. *
  148. * 1. Send IPI to do the following:
  149. * 2. Disable Interrupts
  150. * 3. Wait for all procs to do so
  151. * 4. Enter no-fill cache mode
  152. * 5. Flush caches
  153. * 6. Clear PGE bit
  154. * 7. Flush all TLBs
  155. * 8. Disable all range registers
  156. * 9. Update the MTRRs
  157. * 10. Enable all range registers
  158. * 11. Flush all TLBs and caches again
  159. * 12. Enter normal cache mode and reenable caching
  160. * 13. Set PGE
  161. * 14. Wait for buddies to catch up
  162. * 15. Enable interrupts.
  163. *
  164. * What does that mean for us? Well, first we set data.count to the number
  165. * of CPUs. As each CPU disables interrupts, it'll decrement it once. We wait
  166. * until it hits 0 and proceed. We set the data.gate flag and reset data.count.
  167. * Meanwhile, they are waiting for that flag to be set. Once it's set, each
  168. * CPU goes through the transition of updating MTRRs. The CPU vendors may each do it
  169. * differently, so we call mtrr_if->set() callback and let them take care of it.
  170. * When they're done, they again decrement data->count and wait for data.gate to
  171. * be reset.
  172. * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag.
  173. * Everyone then enables interrupts and we all continue on.
  174. *
  175. * Note that the mechanism is the same for UP systems, too; all the SMP stuff
  176. * becomes nops.
  177. */
  178. static void set_mtrr(unsigned int reg, unsigned long base,
  179. unsigned long size, mtrr_type type)
  180. {
  181. struct set_mtrr_data data;
  182. unsigned long flags;
  183. data.smp_reg = reg;
  184. data.smp_base = base;
  185. data.smp_size = size;
  186. data.smp_type = type;
  187. atomic_set(&data.count, num_booting_cpus() - 1);
  188. /* make sure data.count is visible before unleashing other CPUs */
  189. smp_wmb();
  190. atomic_set(&data.gate,0);
  191. /* Start the ball rolling on other CPUs */
  192. if (smp_call_function(ipi_handler, &data, 1, 0) != 0)
  193. panic("mtrr: timed out waiting for other CPUs\n");
  194. local_irq_save(flags);
  195. while(atomic_read(&data.count))
  196. cpu_relax();
  197. /* ok, reset count and toggle gate */
  198. atomic_set(&data.count, num_booting_cpus() - 1);
  199. smp_wmb();
  200. atomic_set(&data.gate,1);
  201. /* do our MTRR business */
  202. /* HACK!
  203. * We use this same function to initialize the mtrrs on boot.
  204. * The state of the boot cpu's mtrrs has been saved, and we want
  205. * to replicate across all the APs.
  206. * If we're doing that @reg is set to something special...
  207. */
  208. if (reg != ~0U)
  209. mtrr_if->set(reg,base,size,type);
  210. /* wait for the others */
  211. while(atomic_read(&data.count))
  212. cpu_relax();
  213. atomic_set(&data.count, num_booting_cpus() - 1);
  214. smp_wmb();
  215. atomic_set(&data.gate,0);
  216. /*
  217. * Wait here for everyone to have seen the gate change
  218. * So we're the last ones to touch 'data'
  219. */
  220. while(atomic_read(&data.count))
  221. cpu_relax();
  222. local_irq_restore(flags);
  223. }
  224. /**
  225. * mtrr_add_page - Add a memory type region
  226. * @base: Physical base address of region in pages (in units of 4 kB!)
  227. * @size: Physical size of region in pages (4 kB)
  228. * @type: Type of MTRR desired
  229. * @increment: If this is true do usage counting on the region
  230. *
  231. * Memory type region registers control the caching on newer Intel and
  232. * non Intel processors. This function allows drivers to request an
  233. * MTRR is added. The details and hardware specifics of each processor's
  234. * implementation are hidden from the caller, but nevertheless the
  235. * caller should expect to need to provide a power of two size on an
  236. * equivalent power of two boundary.
  237. *
  238. * If the region cannot be added either because all regions are in use
  239. * or the CPU cannot support it a negative value is returned. On success
  240. * the register number for this entry is returned, but should be treated
  241. * as a cookie only.
  242. *
  243. * On a multiprocessor machine the changes are made to all processors.
  244. * This is required on x86 by the Intel processors.
  245. *
  246. * The available types are
  247. *
  248. * %MTRR_TYPE_UNCACHABLE - No caching
  249. *
  250. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  251. *
  252. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  253. *
  254. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  255. *
  256. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  257. * failures and do not wish system log messages to be sent.
  258. */
  259. int mtrr_add_page(unsigned long base, unsigned long size,
  260. unsigned int type, bool increment)
  261. {
  262. int i, replace, error;
  263. mtrr_type ltype;
  264. unsigned long lbase, lsize;
  265. if (!mtrr_if)
  266. return -ENXIO;
  267. if ((error = mtrr_if->validate_add_page(base,size,type)))
  268. return error;
  269. if (type >= MTRR_NUM_TYPES) {
  270. printk(KERN_WARNING "mtrr: type: %u invalid\n", type);
  271. return -EINVAL;
  272. }
  273. /* If the type is WC, check that this processor supports it */
  274. if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
  275. printk(KERN_WARNING
  276. "mtrr: your processor doesn't support write-combining\n");
  277. return -ENOSYS;
  278. }
  279. if (!size) {
  280. printk(KERN_WARNING "mtrr: zero sized request\n");
  281. return -EINVAL;
  282. }
  283. if (base & size_or_mask || size & size_or_mask) {
  284. printk(KERN_WARNING "mtrr: base or size exceeds the MTRR width\n");
  285. return -EINVAL;
  286. }
  287. error = -EINVAL;
  288. replace = -1;
  289. /* No CPU hotplug when we change MTRR entries */
  290. get_online_cpus();
  291. /* Search for existing MTRR */
  292. mutex_lock(&mtrr_mutex);
  293. for (i = 0; i < num_var_ranges; ++i) {
  294. mtrr_if->get(i, &lbase, &lsize, &ltype);
  295. if (!lsize || base > lbase + lsize - 1 || base + size - 1 < lbase)
  296. continue;
  297. /* At this point we know there is some kind of overlap/enclosure */
  298. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  299. if (base <= lbase && base + size - 1 >= lbase + lsize - 1) {
  300. /* New region encloses an existing region */
  301. if (type == ltype) {
  302. replace = replace == -1 ? i : -2;
  303. continue;
  304. }
  305. else if (types_compatible(type, ltype))
  306. continue;
  307. }
  308. printk(KERN_WARNING
  309. "mtrr: 0x%lx000,0x%lx000 overlaps existing"
  310. " 0x%lx000,0x%lx000\n", base, size, lbase,
  311. lsize);
  312. goto out;
  313. }
  314. /* New region is enclosed by an existing region */
  315. if (ltype != type) {
  316. if (types_compatible(type, ltype))
  317. continue;
  318. printk (KERN_WARNING "mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
  319. base, size, mtrr_attrib_to_str(ltype),
  320. mtrr_attrib_to_str(type));
  321. goto out;
  322. }
  323. if (increment)
  324. ++mtrr_usage_table[i];
  325. error = i;
  326. goto out;
  327. }
  328. /* Search for an empty MTRR */
  329. i = mtrr_if->get_free_region(base, size, replace);
  330. if (i >= 0) {
  331. set_mtrr(i, base, size, type);
  332. if (likely(replace < 0)) {
  333. mtrr_usage_table[i] = 1;
  334. } else {
  335. mtrr_usage_table[i] = mtrr_usage_table[replace];
  336. if (increment)
  337. mtrr_usage_table[i]++;
  338. if (unlikely(replace != i)) {
  339. set_mtrr(replace, 0, 0, 0);
  340. mtrr_usage_table[replace] = 0;
  341. }
  342. }
  343. } else
  344. printk(KERN_INFO "mtrr: no more MTRRs available\n");
  345. error = i;
  346. out:
  347. mutex_unlock(&mtrr_mutex);
  348. put_online_cpus();
  349. return error;
  350. }
  351. static int mtrr_check(unsigned long base, unsigned long size)
  352. {
  353. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  354. printk(KERN_WARNING
  355. "mtrr: size and base must be multiples of 4 kiB\n");
  356. printk(KERN_DEBUG
  357. "mtrr: size: 0x%lx base: 0x%lx\n", size, base);
  358. dump_stack();
  359. return -1;
  360. }
  361. return 0;
  362. }
  363. /**
  364. * mtrr_add - Add a memory type region
  365. * @base: Physical base address of region
  366. * @size: Physical size of region
  367. * @type: Type of MTRR desired
  368. * @increment: If this is true do usage counting on the region
  369. *
  370. * Memory type region registers control the caching on newer Intel and
  371. * non Intel processors. This function allows drivers to request an
  372. * MTRR is added. The details and hardware specifics of each processor's
  373. * implementation are hidden from the caller, but nevertheless the
  374. * caller should expect to need to provide a power of two size on an
  375. * equivalent power of two boundary.
  376. *
  377. * If the region cannot be added either because all regions are in use
  378. * or the CPU cannot support it a negative value is returned. On success
  379. * the register number for this entry is returned, but should be treated
  380. * as a cookie only.
  381. *
  382. * On a multiprocessor machine the changes are made to all processors.
  383. * This is required on x86 by the Intel processors.
  384. *
  385. * The available types are
  386. *
  387. * %MTRR_TYPE_UNCACHABLE - No caching
  388. *
  389. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  390. *
  391. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  392. *
  393. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  394. *
  395. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  396. * failures and do not wish system log messages to be sent.
  397. */
  398. int
  399. mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  400. bool increment)
  401. {
  402. if (mtrr_check(base, size))
  403. return -EINVAL;
  404. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  405. increment);
  406. }
  407. /**
  408. * mtrr_del_page - delete a memory type region
  409. * @reg: Register returned by mtrr_add
  410. * @base: Physical base address
  411. * @size: Size of region
  412. *
  413. * If register is supplied then base and size are ignored. This is
  414. * how drivers should call it.
  415. *
  416. * Releases an MTRR region. If the usage count drops to zero the
  417. * register is freed and the region returns to default state.
  418. * On success the register is returned, on failure a negative error
  419. * code.
  420. */
  421. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  422. {
  423. int i, max;
  424. mtrr_type ltype;
  425. unsigned long lbase, lsize;
  426. int error = -EINVAL;
  427. if (!mtrr_if)
  428. return -ENXIO;
  429. max = num_var_ranges;
  430. /* No CPU hotplug when we change MTRR entries */
  431. get_online_cpus();
  432. mutex_lock(&mtrr_mutex);
  433. if (reg < 0) {
  434. /* Search for existing MTRR */
  435. for (i = 0; i < max; ++i) {
  436. mtrr_if->get(i, &lbase, &lsize, &ltype);
  437. if (lbase == base && lsize == size) {
  438. reg = i;
  439. break;
  440. }
  441. }
  442. if (reg < 0) {
  443. printk(KERN_DEBUG "mtrr: no MTRR for %lx000,%lx000 found\n", base,
  444. size);
  445. goto out;
  446. }
  447. }
  448. if (reg >= max) {
  449. printk(KERN_WARNING "mtrr: register: %d too big\n", reg);
  450. goto out;
  451. }
  452. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  453. if (lsize < 1) {
  454. printk(KERN_WARNING "mtrr: MTRR %d not used\n", reg);
  455. goto out;
  456. }
  457. if (mtrr_usage_table[reg] < 1) {
  458. printk(KERN_WARNING "mtrr: reg: %d has count=0\n", reg);
  459. goto out;
  460. }
  461. if (--mtrr_usage_table[reg] < 1)
  462. set_mtrr(reg, 0, 0, 0);
  463. error = reg;
  464. out:
  465. mutex_unlock(&mtrr_mutex);
  466. put_online_cpus();
  467. return error;
  468. }
  469. /**
  470. * mtrr_del - delete a memory type region
  471. * @reg: Register returned by mtrr_add
  472. * @base: Physical base address
  473. * @size: Size of region
  474. *
  475. * If register is supplied then base and size are ignored. This is
  476. * how drivers should call it.
  477. *
  478. * Releases an MTRR region. If the usage count drops to zero the
  479. * register is freed and the region returns to default state.
  480. * On success the register is returned, on failure a negative error
  481. * code.
  482. */
  483. int
  484. mtrr_del(int reg, unsigned long base, unsigned long size)
  485. {
  486. if (mtrr_check(base, size))
  487. return -EINVAL;
  488. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  489. }
  490. EXPORT_SYMBOL(mtrr_add);
  491. EXPORT_SYMBOL(mtrr_del);
  492. /* HACK ALERT!
  493. * These should be called implicitly, but we can't yet until all the initcall
  494. * stuff is done...
  495. */
  496. static void __init init_ifs(void)
  497. {
  498. #ifndef CONFIG_X86_64
  499. amd_init_mtrr();
  500. cyrix_init_mtrr();
  501. centaur_init_mtrr();
  502. #endif
  503. }
  504. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  505. * MTRR driver doesn't require this
  506. */
  507. struct mtrr_value {
  508. mtrr_type ltype;
  509. unsigned long lbase;
  510. unsigned long lsize;
  511. };
  512. static struct mtrr_value mtrr_state[MAX_VAR_RANGES];
  513. static int mtrr_save(struct sys_device * sysdev, pm_message_t state)
  514. {
  515. int i;
  516. for (i = 0; i < num_var_ranges; i++) {
  517. mtrr_if->get(i,
  518. &mtrr_state[i].lbase,
  519. &mtrr_state[i].lsize,
  520. &mtrr_state[i].ltype);
  521. }
  522. return 0;
  523. }
  524. static int mtrr_restore(struct sys_device * sysdev)
  525. {
  526. int i;
  527. for (i = 0; i < num_var_ranges; i++) {
  528. if (mtrr_state[i].lsize)
  529. set_mtrr(i,
  530. mtrr_state[i].lbase,
  531. mtrr_state[i].lsize,
  532. mtrr_state[i].ltype);
  533. }
  534. return 0;
  535. }
  536. static struct sysdev_driver mtrr_sysdev_driver = {
  537. .suspend = mtrr_save,
  538. .resume = mtrr_restore,
  539. };
  540. static int disable_mtrr_trim;
  541. static int __init disable_mtrr_trim_setup(char *str)
  542. {
  543. disable_mtrr_trim = 1;
  544. return 0;
  545. }
  546. early_param("disable_mtrr_trim", disable_mtrr_trim_setup);
  547. /*
  548. * Newer AMD K8s and later CPUs have a special magic MSR way to force WB
  549. * for memory >4GB. Check for that here.
  550. * Note this won't check if the MTRRs < 4GB where the magic bit doesn't
  551. * apply to are wrong, but so far we don't know of any such case in the wild.
  552. */
  553. #define Tom2Enabled (1U << 21)
  554. #define Tom2ForceMemTypeWB (1U << 22)
  555. int __init amd_special_default_mtrr(void)
  556. {
  557. u32 l, h;
  558. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  559. return 0;
  560. if (boot_cpu_data.x86 < 0xf || boot_cpu_data.x86 > 0x11)
  561. return 0;
  562. /* In case some hypervisor doesn't pass SYSCFG through */
  563. if (rdmsr_safe(MSR_K8_SYSCFG, &l, &h) < 0)
  564. return 0;
  565. /*
  566. * Memory between 4GB and top of mem is forced WB by this magic bit.
  567. * Reserved before K8RevF, but should be zero there.
  568. */
  569. if ((l & (Tom2Enabled | Tom2ForceMemTypeWB)) ==
  570. (Tom2Enabled | Tom2ForceMemTypeWB))
  571. return 1;
  572. return 0;
  573. }
  574. /**
  575. * mtrr_trim_uncached_memory - trim RAM not covered by MTRRs
  576. * @end_pfn: ending page frame number
  577. *
  578. * Some buggy BIOSes don't setup the MTRRs properly for systems with certain
  579. * memory configurations. This routine checks that the highest MTRR matches
  580. * the end of memory, to make sure the MTRRs having a write back type cover
  581. * all of the memory the kernel is intending to use. If not, it'll trim any
  582. * memory off the end by adjusting end_pfn, removing it from the kernel's
  583. * allocation pools, warning the user with an obnoxious message.
  584. */
  585. int __init mtrr_trim_uncached_memory(unsigned long end_pfn)
  586. {
  587. unsigned long i, base, size, highest_pfn = 0, def, dummy;
  588. mtrr_type type;
  589. u64 trim_start, trim_size;
  590. /*
  591. * Make sure we only trim uncachable memory on machines that
  592. * support the Intel MTRR architecture:
  593. */
  594. if (!is_cpu(INTEL) || disable_mtrr_trim)
  595. return 0;
  596. rdmsr(MTRRdefType_MSR, def, dummy);
  597. def &= 0xff;
  598. if (def != MTRR_TYPE_UNCACHABLE)
  599. return 0;
  600. if (amd_special_default_mtrr())
  601. return 0;
  602. /* Find highest cached pfn */
  603. for (i = 0; i < num_var_ranges; i++) {
  604. mtrr_if->get(i, &base, &size, &type);
  605. if (type != MTRR_TYPE_WRBACK)
  606. continue;
  607. if (highest_pfn < base + size)
  608. highest_pfn = base + size;
  609. }
  610. /* kvm/qemu doesn't have mtrr set right, don't trim them all */
  611. if (!highest_pfn) {
  612. if (!kvm_para_available()) {
  613. printk(KERN_WARNING
  614. "WARNING: strange, CPU MTRRs all blank?\n");
  615. WARN_ON(1);
  616. }
  617. return 0;
  618. }
  619. if (highest_pfn < end_pfn) {
  620. printk(KERN_WARNING "WARNING: BIOS bug: CPU MTRRs don't cover"
  621. " all of memory, losing %luMB of RAM.\n",
  622. (end_pfn - highest_pfn) >> (20 - PAGE_SHIFT));
  623. WARN_ON(1);
  624. printk(KERN_INFO "update e820 for mtrr\n");
  625. trim_start = highest_pfn;
  626. trim_start <<= PAGE_SHIFT;
  627. trim_size = end_pfn;
  628. trim_size <<= PAGE_SHIFT;
  629. trim_size -= trim_start;
  630. update_memory_range(trim_start, trim_size, E820_RAM,
  631. E820_RESERVED);
  632. update_e820();
  633. return 1;
  634. }
  635. return 0;
  636. }
  637. /**
  638. * mtrr_bp_init - initialize mtrrs on the boot CPU
  639. *
  640. * This needs to be called early; before any of the other CPUs are
  641. * initialized (i.e. before smp_init()).
  642. *
  643. */
  644. void __init mtrr_bp_init(void)
  645. {
  646. init_ifs();
  647. if (cpu_has_mtrr) {
  648. mtrr_if = &generic_mtrr_ops;
  649. size_or_mask = 0xff000000; /* 36 bits */
  650. size_and_mask = 0x00f00000;
  651. /* This is an AMD specific MSR, but we assume(hope?) that
  652. Intel will implement it to when they extend the address
  653. bus of the Xeon. */
  654. if (cpuid_eax(0x80000000) >= 0x80000008) {
  655. u32 phys_addr;
  656. phys_addr = cpuid_eax(0x80000008) & 0xff;
  657. /* CPUID workaround for Intel 0F33/0F34 CPU */
  658. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  659. boot_cpu_data.x86 == 0xF &&
  660. boot_cpu_data.x86_model == 0x3 &&
  661. (boot_cpu_data.x86_mask == 0x3 ||
  662. boot_cpu_data.x86_mask == 0x4))
  663. phys_addr = 36;
  664. size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  665. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  666. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  667. boot_cpu_data.x86 == 6) {
  668. /* VIA C* family have Intel style MTRRs, but
  669. don't support PAE */
  670. size_or_mask = 0xfff00000; /* 32 bits */
  671. size_and_mask = 0;
  672. }
  673. } else {
  674. switch (boot_cpu_data.x86_vendor) {
  675. case X86_VENDOR_AMD:
  676. if (cpu_has_k6_mtrr) {
  677. /* Pre-Athlon (K6) AMD CPU MTRRs */
  678. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  679. size_or_mask = 0xfff00000; /* 32 bits */
  680. size_and_mask = 0;
  681. }
  682. break;
  683. case X86_VENDOR_CENTAUR:
  684. if (cpu_has_centaur_mcr) {
  685. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  686. size_or_mask = 0xfff00000; /* 32 bits */
  687. size_and_mask = 0;
  688. }
  689. break;
  690. case X86_VENDOR_CYRIX:
  691. if (cpu_has_cyrix_arr) {
  692. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  693. size_or_mask = 0xfff00000; /* 32 bits */
  694. size_and_mask = 0;
  695. }
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. if (mtrr_if) {
  702. set_num_var_ranges();
  703. init_table();
  704. if (use_intel())
  705. get_mtrr_state();
  706. }
  707. }
  708. void mtrr_ap_init(void)
  709. {
  710. unsigned long flags;
  711. if (!mtrr_if || !use_intel())
  712. return;
  713. /*
  714. * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
  715. * but this routine will be called in cpu boot time, holding the lock
  716. * breaks it. This routine is called in two cases: 1.very earily time
  717. * of software resume, when there absolutely isn't mtrr entry changes;
  718. * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
  719. * prevent mtrr entry changes
  720. */
  721. local_irq_save(flags);
  722. mtrr_if->set_all();
  723. local_irq_restore(flags);
  724. }
  725. /**
  726. * Save current fixed-range MTRR state of the BSP
  727. */
  728. void mtrr_save_state(void)
  729. {
  730. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1, 1);
  731. }
  732. static int __init mtrr_init_finialize(void)
  733. {
  734. if (!mtrr_if)
  735. return 0;
  736. if (use_intel())
  737. mtrr_state_warn();
  738. else {
  739. /* The CPUs haven't MTRR and seem to not support SMP. They have
  740. * specific drivers, we use a tricky method to support
  741. * suspend/resume for them.
  742. * TBD: is there any system with such CPU which supports
  743. * suspend/resume? if no, we should remove the code.
  744. */
  745. sysdev_driver_register(&cpu_sysdev_class,
  746. &mtrr_sysdev_driver);
  747. }
  748. return 0;
  749. }
  750. subsys_initcall(mtrr_init_finialize);