main.c 23 KB

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