main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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[MTRR_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, 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_value[MTRR_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_value[i].lbase,
  520. &mtrr_value[i].lsize,
  521. &mtrr_value[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_value[i].lsize)
  530. set_mtrr(i,
  531. mtrr_value[i].lbase,
  532. mtrr_value[i].lsize,
  533. mtrr_value[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. int __initdata changed_by_mtrr_cleanup;
  542. /**
  543. * mtrr_bp_init - initialize mtrrs on the boot CPU
  544. *
  545. * This needs to be called early; before any of the other CPUs are
  546. * initialized (i.e. before smp_init()).
  547. *
  548. */
  549. void __init mtrr_bp_init(void)
  550. {
  551. u32 phys_addr;
  552. init_ifs();
  553. phys_addr = 32;
  554. if (cpu_has_mtrr) {
  555. mtrr_if = &generic_mtrr_ops;
  556. size_or_mask = 0xff000000; /* 36 bits */
  557. size_and_mask = 0x00f00000;
  558. phys_addr = 36;
  559. /* This is an AMD specific MSR, but we assume(hope?) that
  560. Intel will implement it to when they extend the address
  561. bus of the Xeon. */
  562. if (cpuid_eax(0x80000000) >= 0x80000008) {
  563. phys_addr = cpuid_eax(0x80000008) & 0xff;
  564. /* CPUID workaround for Intel 0F33/0F34 CPU */
  565. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  566. boot_cpu_data.x86 == 0xF &&
  567. boot_cpu_data.x86_model == 0x3 &&
  568. (boot_cpu_data.x86_mask == 0x3 ||
  569. boot_cpu_data.x86_mask == 0x4))
  570. phys_addr = 36;
  571. size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  572. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  573. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  574. boot_cpu_data.x86 == 6) {
  575. /* VIA C* family have Intel style MTRRs, but
  576. don't support PAE */
  577. size_or_mask = 0xfff00000; /* 32 bits */
  578. size_and_mask = 0;
  579. phys_addr = 32;
  580. }
  581. } else {
  582. switch (boot_cpu_data.x86_vendor) {
  583. case X86_VENDOR_AMD:
  584. if (cpu_has_k6_mtrr) {
  585. /* Pre-Athlon (K6) AMD CPU MTRRs */
  586. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  587. size_or_mask = 0xfff00000; /* 32 bits */
  588. size_and_mask = 0;
  589. }
  590. break;
  591. case X86_VENDOR_CENTAUR:
  592. if (cpu_has_centaur_mcr) {
  593. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  594. size_or_mask = 0xfff00000; /* 32 bits */
  595. size_and_mask = 0;
  596. }
  597. break;
  598. case X86_VENDOR_CYRIX:
  599. if (cpu_has_cyrix_arr) {
  600. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  601. size_or_mask = 0xfff00000; /* 32 bits */
  602. size_and_mask = 0;
  603. }
  604. break;
  605. default:
  606. break;
  607. }
  608. }
  609. if (mtrr_if) {
  610. set_num_var_ranges();
  611. init_table();
  612. if (use_intel()) {
  613. get_mtrr_state();
  614. if (mtrr_cleanup(phys_addr)) {
  615. changed_by_mtrr_cleanup = 1;
  616. mtrr_if->set_all();
  617. }
  618. }
  619. }
  620. }
  621. void mtrr_ap_init(void)
  622. {
  623. unsigned long flags;
  624. if (!mtrr_if || !use_intel())
  625. return;
  626. /*
  627. * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
  628. * but this routine will be called in cpu boot time, holding the lock
  629. * breaks it. This routine is called in two cases: 1.very earily time
  630. * of software resume, when there absolutely isn't mtrr entry changes;
  631. * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
  632. * prevent mtrr entry changes
  633. */
  634. local_irq_save(flags);
  635. mtrr_if->set_all();
  636. local_irq_restore(flags);
  637. }
  638. /**
  639. * Save current fixed-range MTRR state of the BSP
  640. */
  641. void mtrr_save_state(void)
  642. {
  643. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1);
  644. }
  645. static int __init mtrr_init_finialize(void)
  646. {
  647. if (!mtrr_if)
  648. return 0;
  649. if (use_intel()) {
  650. if (!changed_by_mtrr_cleanup)
  651. mtrr_state_warn();
  652. } else {
  653. /* The CPUs haven't MTRR and seem to not support SMP. They have
  654. * specific drivers, we use a tricky method to support
  655. * suspend/resume for them.
  656. * TBD: is there any system with such CPU which supports
  657. * suspend/resume? if no, we should remove the code.
  658. */
  659. sysdev_driver_register(&cpu_sysdev_class,
  660. &mtrr_sysdev_driver);
  661. }
  662. return 0;
  663. }
  664. subsys_initcall(mtrr_init_finialize);