main.c 20 KB

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