main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. /* make sure data.count is visible before unleashing other CPUs */
  197. smp_wmb();
  198. atomic_set(&data.gate,0);
  199. /* Start the ball rolling on other CPUs */
  200. if (smp_call_function(ipi_handler, &data, 1, 0) != 0)
  201. panic("mtrr: timed out waiting for other CPUs\n");
  202. local_irq_save(flags);
  203. while(atomic_read(&data.count))
  204. cpu_relax();
  205. /* ok, reset count and toggle gate */
  206. atomic_set(&data.count, num_booting_cpus() - 1);
  207. smp_wmb();
  208. atomic_set(&data.gate,1);
  209. /* do our MTRR business */
  210. /* HACK!
  211. * We use this same function to initialize the mtrrs on boot.
  212. * The state of the boot cpu's mtrrs has been saved, and we want
  213. * to replicate across all the APs.
  214. * If we're doing that @reg is set to something special...
  215. */
  216. if (reg != ~0U)
  217. mtrr_if->set(reg,base,size,type);
  218. /* wait for the others */
  219. while(atomic_read(&data.count))
  220. cpu_relax();
  221. atomic_set(&data.count, num_booting_cpus() - 1);
  222. smp_wmb();
  223. atomic_set(&data.gate,0);
  224. /*
  225. * Wait here for everyone to have seen the gate change
  226. * So we're the last ones to touch 'data'
  227. */
  228. while(atomic_read(&data.count))
  229. cpu_relax();
  230. local_irq_restore(flags);
  231. }
  232. /**
  233. * mtrr_add_page - Add a memory type region
  234. * @base: Physical base address of region in pages (in units of 4 kB!)
  235. * @size: Physical size of region in pages (4 kB)
  236. * @type: Type of MTRR desired
  237. * @increment: If this is true do usage counting on the region
  238. *
  239. * Memory type region registers control the caching on newer Intel and
  240. * non Intel processors. This function allows drivers to request an
  241. * MTRR is added. The details and hardware specifics of each processor's
  242. * implementation are hidden from the caller, but nevertheless the
  243. * caller should expect to need to provide a power of two size on an
  244. * equivalent power of two boundary.
  245. *
  246. * If the region cannot be added either because all regions are in use
  247. * or the CPU cannot support it a negative value is returned. On success
  248. * the register number for this entry is returned, but should be treated
  249. * as a cookie only.
  250. *
  251. * On a multiprocessor machine the changes are made to all processors.
  252. * This is required on x86 by the Intel processors.
  253. *
  254. * The available types are
  255. *
  256. * %MTRR_TYPE_UNCACHABLE - No caching
  257. *
  258. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  259. *
  260. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  261. *
  262. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  263. *
  264. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  265. * failures and do not wish system log messages to be sent.
  266. */
  267. int mtrr_add_page(unsigned long base, unsigned long size,
  268. unsigned int type, char increment)
  269. {
  270. int i, replace, error;
  271. mtrr_type ltype;
  272. unsigned long lbase, lsize;
  273. if (!mtrr_if)
  274. return -ENXIO;
  275. if ((error = mtrr_if->validate_add_page(base,size,type)))
  276. return error;
  277. if (type >= MTRR_NUM_TYPES) {
  278. printk(KERN_WARNING "mtrr: type: %u invalid\n", type);
  279. return -EINVAL;
  280. }
  281. /* If the type is WC, check that this processor supports it */
  282. if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
  283. printk(KERN_WARNING
  284. "mtrr: your processor doesn't support write-combining\n");
  285. return -ENOSYS;
  286. }
  287. if (!size) {
  288. printk(KERN_WARNING "mtrr: zero sized request\n");
  289. return -EINVAL;
  290. }
  291. if (base & size_or_mask || size & size_or_mask) {
  292. printk(KERN_WARNING "mtrr: base or size exceeds the MTRR width\n");
  293. return -EINVAL;
  294. }
  295. error = -EINVAL;
  296. replace = -1;
  297. /* No CPU hotplug when we change MTRR entries */
  298. lock_cpu_hotplug();
  299. /* Search for existing MTRR */
  300. mutex_lock(&mtrr_mutex);
  301. for (i = 0; i < num_var_ranges; ++i) {
  302. mtrr_if->get(i, &lbase, &lsize, &ltype);
  303. if (!lsize || base > lbase + lsize - 1 || base + size - 1 < lbase)
  304. continue;
  305. /* At this point we know there is some kind of overlap/enclosure */
  306. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  307. if (base <= lbase && base + size - 1 >= lbase + lsize - 1) {
  308. /* New region encloses an existing region */
  309. if (type == ltype) {
  310. replace = replace == -1 ? i : -2;
  311. continue;
  312. }
  313. else if (types_compatible(type, ltype))
  314. continue;
  315. }
  316. printk(KERN_WARNING
  317. "mtrr: 0x%lx000,0x%lx000 overlaps existing"
  318. " 0x%lx000,0x%lx000\n", base, size, lbase,
  319. lsize);
  320. goto out;
  321. }
  322. /* New region is enclosed by an existing region */
  323. if (ltype != type) {
  324. if (types_compatible(type, ltype))
  325. continue;
  326. printk (KERN_WARNING "mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
  327. base, size, mtrr_attrib_to_str(ltype),
  328. mtrr_attrib_to_str(type));
  329. goto out;
  330. }
  331. if (increment)
  332. ++usage_table[i];
  333. error = i;
  334. goto out;
  335. }
  336. /* Search for an empty MTRR */
  337. i = mtrr_if->get_free_region(base, size, replace);
  338. if (i >= 0) {
  339. set_mtrr(i, base, size, type);
  340. if (likely(replace < 0))
  341. usage_table[i] = 1;
  342. else {
  343. usage_table[i] = usage_table[replace] + !!increment;
  344. if (unlikely(replace != i)) {
  345. set_mtrr(replace, 0, 0, 0);
  346. usage_table[replace] = 0;
  347. }
  348. }
  349. } else
  350. printk(KERN_INFO "mtrr: no more MTRRs available\n");
  351. error = i;
  352. out:
  353. mutex_unlock(&mtrr_mutex);
  354. unlock_cpu_hotplug();
  355. return error;
  356. }
  357. static int mtrr_check(unsigned long base, unsigned long size)
  358. {
  359. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  360. printk(KERN_WARNING
  361. "mtrr: size and base must be multiples of 4 kiB\n");
  362. printk(KERN_DEBUG
  363. "mtrr: size: 0x%lx base: 0x%lx\n", size, base);
  364. dump_stack();
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. /**
  370. * mtrr_add - Add a memory type region
  371. * @base: Physical base address of region
  372. * @size: Physical size of region
  373. * @type: Type of MTRR desired
  374. * @increment: If this is true do usage counting on the region
  375. *
  376. * Memory type region registers control the caching on newer Intel and
  377. * non Intel processors. This function allows drivers to request an
  378. * MTRR is added. The details and hardware specifics of each processor's
  379. * implementation are hidden from the caller, but nevertheless the
  380. * caller should expect to need to provide a power of two size on an
  381. * equivalent power of two boundary.
  382. *
  383. * If the region cannot be added either because all regions are in use
  384. * or the CPU cannot support it a negative value is returned. On success
  385. * the register number for this entry is returned, but should be treated
  386. * as a cookie only.
  387. *
  388. * On a multiprocessor machine the changes are made to all processors.
  389. * This is required on x86 by the Intel processors.
  390. *
  391. * The available types are
  392. *
  393. * %MTRR_TYPE_UNCACHABLE - No caching
  394. *
  395. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  396. *
  397. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  398. *
  399. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  400. *
  401. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  402. * failures and do not wish system log messages to be sent.
  403. */
  404. int
  405. mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  406. char increment)
  407. {
  408. if (mtrr_check(base, size))
  409. return -EINVAL;
  410. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  411. increment);
  412. }
  413. /**
  414. * mtrr_del_page - delete a memory type region
  415. * @reg: Register returned by mtrr_add
  416. * @base: Physical base address
  417. * @size: Size of region
  418. *
  419. * If register is supplied then base and size are ignored. This is
  420. * how drivers should call it.
  421. *
  422. * Releases an MTRR region. If the usage count drops to zero the
  423. * register is freed and the region returns to default state.
  424. * On success the register is returned, on failure a negative error
  425. * code.
  426. */
  427. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  428. {
  429. int i, max;
  430. mtrr_type ltype;
  431. unsigned long lbase, lsize;
  432. int error = -EINVAL;
  433. if (!mtrr_if)
  434. return -ENXIO;
  435. max = num_var_ranges;
  436. /* No CPU hotplug when we change MTRR entries */
  437. lock_cpu_hotplug();
  438. mutex_lock(&mtrr_mutex);
  439. if (reg < 0) {
  440. /* Search for existing MTRR */
  441. for (i = 0; i < max; ++i) {
  442. mtrr_if->get(i, &lbase, &lsize, &ltype);
  443. if (lbase == base && lsize == size) {
  444. reg = i;
  445. break;
  446. }
  447. }
  448. if (reg < 0) {
  449. printk(KERN_DEBUG "mtrr: no MTRR for %lx000,%lx000 found\n", base,
  450. size);
  451. goto out;
  452. }
  453. }
  454. if (reg >= max) {
  455. printk(KERN_WARNING "mtrr: register: %d too big\n", reg);
  456. goto out;
  457. }
  458. if (is_cpu(CYRIX) && !use_intel()) {
  459. if ((reg == 3) && arr3_protected) {
  460. printk(KERN_WARNING "mtrr: ARR3 cannot be changed\n");
  461. goto out;
  462. }
  463. }
  464. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  465. if (lsize < 1) {
  466. printk(KERN_WARNING "mtrr: MTRR %d not used\n", reg);
  467. goto out;
  468. }
  469. if (usage_table[reg] < 1) {
  470. printk(KERN_WARNING "mtrr: reg: %d has count=0\n", reg);
  471. goto out;
  472. }
  473. if (--usage_table[reg] < 1)
  474. set_mtrr(reg, 0, 0, 0);
  475. error = reg;
  476. out:
  477. mutex_unlock(&mtrr_mutex);
  478. unlock_cpu_hotplug();
  479. return error;
  480. }
  481. /**
  482. * mtrr_del - delete a memory type region
  483. * @reg: Register returned by mtrr_add
  484. * @base: Physical base address
  485. * @size: Size of region
  486. *
  487. * If register is supplied then base and size are ignored. This is
  488. * how drivers should call it.
  489. *
  490. * Releases an MTRR region. If the usage count drops to zero the
  491. * register is freed and the region returns to default state.
  492. * On success the register is returned, on failure a negative error
  493. * code.
  494. */
  495. int
  496. mtrr_del(int reg, unsigned long base, unsigned long size)
  497. {
  498. if (mtrr_check(base, size))
  499. return -EINVAL;
  500. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  501. }
  502. EXPORT_SYMBOL(mtrr_add);
  503. EXPORT_SYMBOL(mtrr_del);
  504. /* HACK ALERT!
  505. * These should be called implicitly, but we can't yet until all the initcall
  506. * stuff is done...
  507. */
  508. extern void amd_init_mtrr(void);
  509. extern void cyrix_init_mtrr(void);
  510. extern void centaur_init_mtrr(void);
  511. static void __init init_ifs(void)
  512. {
  513. #ifndef CONFIG_X86_64
  514. amd_init_mtrr();
  515. cyrix_init_mtrr();
  516. centaur_init_mtrr();
  517. #endif
  518. }
  519. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  520. * MTRR driver doesn't require this
  521. */
  522. struct mtrr_value {
  523. mtrr_type ltype;
  524. unsigned long lbase;
  525. unsigned long lsize;
  526. };
  527. static struct mtrr_value * mtrr_state;
  528. static int mtrr_save(struct sys_device * sysdev, pm_message_t state)
  529. {
  530. int i;
  531. int size = num_var_ranges * sizeof(struct mtrr_value);
  532. mtrr_state = kzalloc(size,GFP_ATOMIC);
  533. if (!mtrr_state)
  534. return -ENOMEM;
  535. for (i = 0; i < num_var_ranges; i++) {
  536. mtrr_if->get(i,
  537. &mtrr_state[i].lbase,
  538. &mtrr_state[i].lsize,
  539. &mtrr_state[i].ltype);
  540. }
  541. return 0;
  542. }
  543. static int mtrr_restore(struct sys_device * sysdev)
  544. {
  545. int i;
  546. for (i = 0; i < num_var_ranges; i++) {
  547. if (mtrr_state[i].lsize)
  548. set_mtrr(i,
  549. mtrr_state[i].lbase,
  550. mtrr_state[i].lsize,
  551. mtrr_state[i].ltype);
  552. }
  553. kfree(mtrr_state);
  554. return 0;
  555. }
  556. static struct sysdev_driver mtrr_sysdev_driver = {
  557. .suspend = mtrr_save,
  558. .resume = mtrr_restore,
  559. };
  560. /**
  561. * mtrr_bp_init - initialize mtrrs on the boot CPU
  562. *
  563. * This needs to be called early; before any of the other CPUs are
  564. * initialized (i.e. before smp_init()).
  565. *
  566. */
  567. void __init mtrr_bp_init(void)
  568. {
  569. init_ifs();
  570. if (cpu_has_mtrr) {
  571. mtrr_if = &generic_mtrr_ops;
  572. size_or_mask = 0xff000000; /* 36 bits */
  573. size_and_mask = 0x00f00000;
  574. /* This is an AMD specific MSR, but we assume(hope?) that
  575. Intel will implement it to when they extend the address
  576. bus of the Xeon. */
  577. if (cpuid_eax(0x80000000) >= 0x80000008) {
  578. u32 phys_addr;
  579. phys_addr = cpuid_eax(0x80000008) & 0xff;
  580. /* CPUID workaround for Intel 0F33/0F34 CPU */
  581. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  582. boot_cpu_data.x86 == 0xF &&
  583. boot_cpu_data.x86_model == 0x3 &&
  584. (boot_cpu_data.x86_mask == 0x3 ||
  585. boot_cpu_data.x86_mask == 0x4))
  586. phys_addr = 36;
  587. size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  588. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  589. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  590. boot_cpu_data.x86 == 6) {
  591. /* VIA C* family have Intel style MTRRs, but
  592. don't support PAE */
  593. size_or_mask = 0xfff00000; /* 32 bits */
  594. size_and_mask = 0;
  595. }
  596. } else {
  597. switch (boot_cpu_data.x86_vendor) {
  598. case X86_VENDOR_AMD:
  599. if (cpu_has_k6_mtrr) {
  600. /* Pre-Athlon (K6) AMD CPU MTRRs */
  601. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  602. size_or_mask = 0xfff00000; /* 32 bits */
  603. size_and_mask = 0;
  604. }
  605. break;
  606. case X86_VENDOR_CENTAUR:
  607. if (cpu_has_centaur_mcr) {
  608. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  609. size_or_mask = 0xfff00000; /* 32 bits */
  610. size_and_mask = 0;
  611. }
  612. break;
  613. case X86_VENDOR_CYRIX:
  614. if (cpu_has_cyrix_arr) {
  615. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  616. size_or_mask = 0xfff00000; /* 32 bits */
  617. size_and_mask = 0;
  618. }
  619. break;
  620. default:
  621. break;
  622. }
  623. }
  624. if (mtrr_if) {
  625. set_num_var_ranges();
  626. init_table();
  627. if (use_intel())
  628. get_mtrr_state();
  629. }
  630. }
  631. void mtrr_ap_init(void)
  632. {
  633. unsigned long flags;
  634. if (!mtrr_if || !use_intel())
  635. return;
  636. /*
  637. * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
  638. * but this routine will be called in cpu boot time, holding the lock
  639. * breaks it. This routine is called in two cases: 1.very earily time
  640. * of software resume, when there absolutely isn't mtrr entry changes;
  641. * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
  642. * prevent mtrr entry changes
  643. */
  644. local_irq_save(flags);
  645. mtrr_if->set_all();
  646. local_irq_restore(flags);
  647. }
  648. /**
  649. * Save current fixed-range MTRR state of the BSP
  650. */
  651. void mtrr_save_state(void)
  652. {
  653. int cpu = get_cpu();
  654. if (cpu == 0)
  655. mtrr_save_fixed_ranges(NULL);
  656. else
  657. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1, 1);
  658. put_cpu();
  659. }
  660. static int __init mtrr_init_finialize(void)
  661. {
  662. if (!mtrr_if)
  663. return 0;
  664. if (use_intel())
  665. mtrr_state_warn();
  666. else {
  667. /* The CPUs haven't MTRR and seemes not support SMP. They have
  668. * specific drivers, we use a tricky method to support
  669. * suspend/resume for them.
  670. * TBD: is there any system with such CPU which supports
  671. * suspend/resume? if no, we should remove the code.
  672. */
  673. sysdev_driver_register(&cpu_sysdev_class,
  674. &mtrr_sysdev_driver);
  675. }
  676. return 0;
  677. }
  678. subsys_initcall(mtrr_init_finialize);