main.c 20 KB

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