main.c 21 KB

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