main.c 22 KB

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