main.c 19 KB

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