main.c 23 KB

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