main.c 22 KB

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