main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 <asm/pat.h>
  44. #include "mtrr.h"
  45. /* arch_phys_wc_add returns an MTRR register index plus this offset. */
  46. #define MTRR_TO_PHYS_WC_OFFSET 1000
  47. u32 num_var_ranges;
  48. unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
  49. static DEFINE_MUTEX(mtrr_mutex);
  50. u64 size_or_mask, size_and_mask;
  51. static bool mtrr_aps_delayed_init;
  52. static const struct mtrr_ops *mtrr_ops[X86_VENDOR_NUM];
  53. const struct mtrr_ops *mtrr_if;
  54. static void set_mtrr(unsigned int reg, unsigned long base,
  55. unsigned long size, mtrr_type type);
  56. void set_mtrr_ops(const struct mtrr_ops *ops)
  57. {
  58. if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
  59. mtrr_ops[ops->vendor] = ops;
  60. }
  61. /* Returns non-zero if we have the write-combining memory type */
  62. static int have_wrcomb(void)
  63. {
  64. struct pci_dev *dev;
  65. dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL);
  66. if (dev != NULL) {
  67. /*
  68. * ServerWorks LE chipsets < rev 6 have problems with
  69. * write-combining. Don't allow it and leave room for other
  70. * chipsets to be tagged
  71. */
  72. if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
  73. dev->device == PCI_DEVICE_ID_SERVERWORKS_LE &&
  74. dev->revision <= 5) {
  75. pr_info("mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
  76. pci_dev_put(dev);
  77. return 0;
  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. unsigned long smp_base;
  114. unsigned long smp_size;
  115. unsigned int smp_reg;
  116. mtrr_type smp_type;
  117. };
  118. /**
  119. * mtrr_rendezvous_handler - Work done in the synchronization handler. Executed
  120. * by all the CPUs.
  121. * @info: pointer to mtrr configuration data
  122. *
  123. * Returns nothing.
  124. */
  125. static int mtrr_rendezvous_handler(void *info)
  126. {
  127. struct set_mtrr_data *data = info;
  128. /*
  129. * We use this same function to initialize the mtrrs during boot,
  130. * resume, runtime cpu online and on an explicit request to set a
  131. * specific MTRR.
  132. *
  133. * During boot or suspend, the state of the boot cpu's mtrrs has been
  134. * saved, and we want to replicate that across all the cpus that come
  135. * online (either at the end of boot or resume or during a runtime cpu
  136. * online). If we're doing that, @reg is set to something special and on
  137. * all the cpu's we do mtrr_if->set_all() (On the logical cpu that
  138. * started the boot/resume sequence, this might be a duplicate
  139. * set_all()).
  140. */
  141. if (data->smp_reg != ~0U) {
  142. mtrr_if->set(data->smp_reg, data->smp_base,
  143. data->smp_size, data->smp_type);
  144. } else if (mtrr_aps_delayed_init || !cpu_online(smp_processor_id())) {
  145. mtrr_if->set_all();
  146. }
  147. return 0;
  148. }
  149. static inline int types_compatible(mtrr_type type1, mtrr_type type2)
  150. {
  151. return type1 == MTRR_TYPE_UNCACHABLE ||
  152. type2 == MTRR_TYPE_UNCACHABLE ||
  153. (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
  154. (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
  155. }
  156. /**
  157. * set_mtrr - update mtrrs on all processors
  158. * @reg: mtrr in question
  159. * @base: mtrr base
  160. * @size: mtrr size
  161. * @type: mtrr type
  162. *
  163. * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
  164. *
  165. * 1. Queue work to do the following on all processors:
  166. * 2. Disable Interrupts
  167. * 3. Wait for all procs to do so
  168. * 4. Enter no-fill cache mode
  169. * 5. Flush caches
  170. * 6. Clear PGE bit
  171. * 7. Flush all TLBs
  172. * 8. Disable all range registers
  173. * 9. Update the MTRRs
  174. * 10. Enable all range registers
  175. * 11. Flush all TLBs and caches again
  176. * 12. Enter normal cache mode and reenable caching
  177. * 13. Set PGE
  178. * 14. Wait for buddies to catch up
  179. * 15. Enable interrupts.
  180. *
  181. * What does that mean for us? Well, stop_machine() will ensure that
  182. * the rendezvous handler is started on each CPU. And in lockstep they
  183. * do the state transition of disabling interrupts, updating MTRR's
  184. * (the CPU vendors may each do it differently, so we call mtrr_if->set()
  185. * callback and let them take care of it.) and enabling interrupts.
  186. *
  187. * Note that the mechanism is the same for UP systems, too; all the SMP stuff
  188. * becomes nops.
  189. */
  190. static void
  191. set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type type)
  192. {
  193. struct set_mtrr_data data = { .smp_reg = reg,
  194. .smp_base = base,
  195. .smp_size = size,
  196. .smp_type = type
  197. };
  198. stop_machine(mtrr_rendezvous_handler, &data, cpu_online_mask);
  199. }
  200. static void set_mtrr_from_inactive_cpu(unsigned int reg, unsigned long base,
  201. unsigned long size, mtrr_type type)
  202. {
  203. struct set_mtrr_data data = { .smp_reg = reg,
  204. .smp_base = base,
  205. .smp_size = size,
  206. .smp_type = type
  207. };
  208. stop_machine_from_inactive_cpu(mtrr_rendezvous_handler, &data,
  209. cpu_callout_mask);
  210. }
  211. /**
  212. * mtrr_add_page - Add a memory type region
  213. * @base: Physical base address of region in pages (in units of 4 kB!)
  214. * @size: Physical size of region in pages (4 kB)
  215. * @type: Type of MTRR desired
  216. * @increment: If this is true do usage counting on the region
  217. *
  218. * Memory type region registers control the caching on newer Intel and
  219. * non Intel processors. This function allows drivers to request an
  220. * MTRR is added. The details and hardware specifics of each processor's
  221. * implementation are hidden from the caller, but nevertheless the
  222. * caller should expect to need to provide a power of two size on an
  223. * equivalent power of two boundary.
  224. *
  225. * If the region cannot be added either because all regions are in use
  226. * or the CPU cannot support it a negative value is returned. On success
  227. * the register number for this entry is returned, but should be treated
  228. * as a cookie only.
  229. *
  230. * On a multiprocessor machine the changes are made to all processors.
  231. * This is required on x86 by the Intel processors.
  232. *
  233. * The available types are
  234. *
  235. * %MTRR_TYPE_UNCACHABLE - No caching
  236. *
  237. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  238. *
  239. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  240. *
  241. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  242. *
  243. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  244. * failures and do not wish system log messages to be sent.
  245. */
  246. int mtrr_add_page(unsigned long base, unsigned long size,
  247. unsigned int type, bool increment)
  248. {
  249. unsigned long lbase, lsize;
  250. int i, replace, error;
  251. mtrr_type ltype;
  252. if (!mtrr_if)
  253. return -ENXIO;
  254. error = mtrr_if->validate_add_page(base, size, type);
  255. if (error)
  256. return error;
  257. if (type >= MTRR_NUM_TYPES) {
  258. pr_warning("mtrr: type: %u invalid\n", type);
  259. return -EINVAL;
  260. }
  261. /* If the type is WC, check that this processor supports it */
  262. if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
  263. pr_warning("mtrr: your processor doesn't support write-combining\n");
  264. return -ENOSYS;
  265. }
  266. if (!size) {
  267. pr_warning("mtrr: zero sized request\n");
  268. return -EINVAL;
  269. }
  270. if ((base | (base + size - 1)) >>
  271. (boot_cpu_data.x86_phys_bits - PAGE_SHIFT)) {
  272. pr_warning("mtrr: base or size exceeds the MTRR width\n");
  273. return -EINVAL;
  274. }
  275. error = -EINVAL;
  276. replace = -1;
  277. /* No CPU hotplug when we change MTRR entries */
  278. get_online_cpus();
  279. /* Search for existing MTRR */
  280. mutex_lock(&mtrr_mutex);
  281. for (i = 0; i < num_var_ranges; ++i) {
  282. mtrr_if->get(i, &lbase, &lsize, &ltype);
  283. if (!lsize || base > lbase + lsize - 1 ||
  284. base + size - 1 < lbase)
  285. continue;
  286. /*
  287. * At this point we know there is some kind of
  288. * overlap/enclosure
  289. */
  290. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  291. if (base <= lbase &&
  292. base + size - 1 >= lbase + lsize - 1) {
  293. /* New region encloses an existing region */
  294. if (type == ltype) {
  295. replace = replace == -1 ? i : -2;
  296. continue;
  297. } else if (types_compatible(type, ltype))
  298. continue;
  299. }
  300. pr_warning("mtrr: 0x%lx000,0x%lx000 overlaps existing"
  301. " 0x%lx000,0x%lx000\n", base, size, lbase,
  302. lsize);
  303. goto out;
  304. }
  305. /* New region is enclosed by an existing region */
  306. if (ltype != type) {
  307. if (types_compatible(type, ltype))
  308. continue;
  309. pr_warning("mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
  310. base, size, mtrr_attrib_to_str(ltype),
  311. mtrr_attrib_to_str(type));
  312. goto out;
  313. }
  314. if (increment)
  315. ++mtrr_usage_table[i];
  316. error = i;
  317. goto out;
  318. }
  319. /* Search for an empty MTRR */
  320. i = mtrr_if->get_free_region(base, size, replace);
  321. if (i >= 0) {
  322. set_mtrr(i, base, size, type);
  323. if (likely(replace < 0)) {
  324. mtrr_usage_table[i] = 1;
  325. } else {
  326. mtrr_usage_table[i] = mtrr_usage_table[replace];
  327. if (increment)
  328. mtrr_usage_table[i]++;
  329. if (unlikely(replace != i)) {
  330. set_mtrr(replace, 0, 0, 0);
  331. mtrr_usage_table[replace] = 0;
  332. }
  333. }
  334. } else {
  335. pr_info("mtrr: no more MTRRs available\n");
  336. }
  337. error = i;
  338. out:
  339. mutex_unlock(&mtrr_mutex);
  340. put_online_cpus();
  341. return error;
  342. }
  343. static int mtrr_check(unsigned long base, unsigned long size)
  344. {
  345. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  346. pr_warning("mtrr: size and base must be multiples of 4 kiB\n");
  347. pr_debug("mtrr: size: 0x%lx base: 0x%lx\n", size, base);
  348. dump_stack();
  349. return -1;
  350. }
  351. return 0;
  352. }
  353. /**
  354. * mtrr_add - Add a memory type region
  355. * @base: Physical base address of region
  356. * @size: Physical size of region
  357. * @type: Type of MTRR desired
  358. * @increment: If this is true do usage counting on the region
  359. *
  360. * Memory type region registers control the caching on newer Intel and
  361. * non Intel processors. This function allows drivers to request an
  362. * MTRR is added. The details and hardware specifics of each processor's
  363. * implementation are hidden from the caller, but nevertheless the
  364. * caller should expect to need to provide a power of two size on an
  365. * equivalent power of two boundary.
  366. *
  367. * If the region cannot be added either because all regions are in use
  368. * or the CPU cannot support it a negative value is returned. On success
  369. * the register number for this entry is returned, but should be treated
  370. * as a cookie only.
  371. *
  372. * On a multiprocessor machine the changes are made to all processors.
  373. * This is required on x86 by the Intel processors.
  374. *
  375. * The available types are
  376. *
  377. * %MTRR_TYPE_UNCACHABLE - No caching
  378. *
  379. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  380. *
  381. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  382. *
  383. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  384. *
  385. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  386. * failures and do not wish system log messages to be sent.
  387. */
  388. int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  389. bool increment)
  390. {
  391. if (mtrr_check(base, size))
  392. return -EINVAL;
  393. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  394. increment);
  395. }
  396. EXPORT_SYMBOL(mtrr_add);
  397. /**
  398. * mtrr_del_page - delete a memory type region
  399. * @reg: Register returned by mtrr_add
  400. * @base: Physical base address
  401. * @size: Size of region
  402. *
  403. * If register is supplied then base and size are ignored. This is
  404. * how drivers should call it.
  405. *
  406. * Releases an MTRR region. If the usage count drops to zero the
  407. * register is freed and the region returns to default state.
  408. * On success the register is returned, on failure a negative error
  409. * code.
  410. */
  411. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  412. {
  413. int i, max;
  414. mtrr_type ltype;
  415. unsigned long lbase, lsize;
  416. int error = -EINVAL;
  417. if (!mtrr_if)
  418. return -ENXIO;
  419. max = num_var_ranges;
  420. /* No CPU hotplug when we change MTRR entries */
  421. get_online_cpus();
  422. mutex_lock(&mtrr_mutex);
  423. if (reg < 0) {
  424. /* Search for existing MTRR */
  425. for (i = 0; i < max; ++i) {
  426. mtrr_if->get(i, &lbase, &lsize, &ltype);
  427. if (lbase == base && lsize == size) {
  428. reg = i;
  429. break;
  430. }
  431. }
  432. if (reg < 0) {
  433. pr_debug("mtrr: no MTRR for %lx000,%lx000 found\n",
  434. base, size);
  435. goto out;
  436. }
  437. }
  438. if (reg >= max) {
  439. pr_warning("mtrr: register: %d too big\n", reg);
  440. goto out;
  441. }
  442. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  443. if (lsize < 1) {
  444. pr_warning("mtrr: MTRR %d not used\n", reg);
  445. goto out;
  446. }
  447. if (mtrr_usage_table[reg] < 1) {
  448. pr_warning("mtrr: reg: %d has count=0\n", reg);
  449. goto out;
  450. }
  451. if (--mtrr_usage_table[reg] < 1)
  452. set_mtrr(reg, 0, 0, 0);
  453. error = reg;
  454. out:
  455. mutex_unlock(&mtrr_mutex);
  456. put_online_cpus();
  457. return error;
  458. }
  459. /**
  460. * mtrr_del - delete a memory type region
  461. * @reg: Register returned by mtrr_add
  462. * @base: Physical base address
  463. * @size: Size of region
  464. *
  465. * If register is supplied then base and size are ignored. This is
  466. * how drivers should call it.
  467. *
  468. * Releases an MTRR region. If the usage count drops to zero the
  469. * register is freed and the region returns to default state.
  470. * On success the register is returned, on failure a negative error
  471. * code.
  472. */
  473. int mtrr_del(int reg, unsigned long base, unsigned long size)
  474. {
  475. if (mtrr_check(base, size))
  476. return -EINVAL;
  477. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  478. }
  479. EXPORT_SYMBOL(mtrr_del);
  480. /**
  481. * arch_phys_wc_add - add a WC MTRR and handle errors if PAT is unavailable
  482. * @base: Physical base address
  483. * @size: Size of region
  484. *
  485. * If PAT is available, this does nothing. If PAT is unavailable, it
  486. * attempts to add a WC MTRR covering size bytes starting at base and
  487. * logs an error if this fails.
  488. *
  489. * Drivers must store the return value to pass to mtrr_del_wc_if_needed,
  490. * but drivers should not try to interpret that return value.
  491. */
  492. int arch_phys_wc_add(unsigned long base, unsigned long size)
  493. {
  494. int ret;
  495. if (pat_enabled)
  496. return 0; /* Success! (We don't need to do anything.) */
  497. ret = mtrr_add(base, size, MTRR_TYPE_WRCOMB, true);
  498. if (ret < 0) {
  499. pr_warn("Failed to add WC MTRR for [%p-%p]; performance may suffer.",
  500. (void *)base, (void *)(base + size - 1));
  501. return ret;
  502. }
  503. return ret + MTRR_TO_PHYS_WC_OFFSET;
  504. }
  505. EXPORT_SYMBOL(arch_phys_wc_add);
  506. /*
  507. * arch_phys_wc_del - undoes arch_phys_wc_add
  508. * @handle: Return value from arch_phys_wc_add
  509. *
  510. * This cleans up after mtrr_add_wc_if_needed.
  511. *
  512. * The API guarantees that mtrr_del_wc_if_needed(error code) and
  513. * mtrr_del_wc_if_needed(0) do nothing.
  514. */
  515. void arch_phys_wc_del(int handle)
  516. {
  517. if (handle >= 1) {
  518. WARN_ON(handle < MTRR_TO_PHYS_WC_OFFSET);
  519. mtrr_del(handle - MTRR_TO_PHYS_WC_OFFSET, 0, 0);
  520. }
  521. }
  522. EXPORT_SYMBOL(arch_phys_wc_del);
  523. /*
  524. * phys_wc_to_mtrr_index - translates arch_phys_wc_add's return value
  525. * @handle: Return value from arch_phys_wc_add
  526. *
  527. * This will turn the return value from arch_phys_wc_add into an mtrr
  528. * index suitable for debugging.
  529. *
  530. * Note: There is no legitimate use for this function, except possibly
  531. * in printk line. Alas there is an illegitimate use in some ancient
  532. * drm ioctls.
  533. */
  534. int phys_wc_to_mtrr_index(int handle)
  535. {
  536. if (handle < MTRR_TO_PHYS_WC_OFFSET)
  537. return -1;
  538. else
  539. return handle - MTRR_TO_PHYS_WC_OFFSET;
  540. }
  541. EXPORT_SYMBOL_GPL(phys_wc_to_mtrr_index);
  542. /*
  543. * HACK ALERT!
  544. * These should be called implicitly, but we can't yet until all the initcall
  545. * stuff is done...
  546. */
  547. static void __init init_ifs(void)
  548. {
  549. #ifndef CONFIG_X86_64
  550. amd_init_mtrr();
  551. cyrix_init_mtrr();
  552. centaur_init_mtrr();
  553. #endif
  554. }
  555. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  556. * MTRR driver doesn't require this
  557. */
  558. struct mtrr_value {
  559. mtrr_type ltype;
  560. unsigned long lbase;
  561. unsigned long lsize;
  562. };
  563. static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
  564. static int mtrr_save(void)
  565. {
  566. int i;
  567. for (i = 0; i < num_var_ranges; i++) {
  568. mtrr_if->get(i, &mtrr_value[i].lbase,
  569. &mtrr_value[i].lsize,
  570. &mtrr_value[i].ltype);
  571. }
  572. return 0;
  573. }
  574. static void mtrr_restore(void)
  575. {
  576. int i;
  577. for (i = 0; i < num_var_ranges; i++) {
  578. if (mtrr_value[i].lsize) {
  579. set_mtrr(i, mtrr_value[i].lbase,
  580. mtrr_value[i].lsize,
  581. mtrr_value[i].ltype);
  582. }
  583. }
  584. }
  585. static struct syscore_ops mtrr_syscore_ops = {
  586. .suspend = mtrr_save,
  587. .resume = mtrr_restore,
  588. };
  589. int __initdata changed_by_mtrr_cleanup;
  590. #define SIZE_OR_MASK_BITS(n) (~((1ULL << ((n) - PAGE_SHIFT)) - 1))
  591. /**
  592. * mtrr_bp_init - initialize mtrrs on the boot CPU
  593. *
  594. * This needs to be called early; before any of the other CPUs are
  595. * initialized (i.e. before smp_init()).
  596. *
  597. */
  598. void __init mtrr_bp_init(void)
  599. {
  600. u32 phys_addr;
  601. init_ifs();
  602. phys_addr = 32;
  603. if (cpu_has_mtrr) {
  604. mtrr_if = &generic_mtrr_ops;
  605. size_or_mask = SIZE_OR_MASK_BITS(36);
  606. size_and_mask = 0x00f00000;
  607. phys_addr = 36;
  608. /*
  609. * This is an AMD specific MSR, but we assume(hope?) that
  610. * Intel will implement it too when they extend the address
  611. * bus of the Xeon.
  612. */
  613. if (cpuid_eax(0x80000000) >= 0x80000008) {
  614. phys_addr = cpuid_eax(0x80000008) & 0xff;
  615. /* CPUID workaround for Intel 0F33/0F34 CPU */
  616. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  617. boot_cpu_data.x86 == 0xF &&
  618. boot_cpu_data.x86_model == 0x3 &&
  619. (boot_cpu_data.x86_mask == 0x3 ||
  620. boot_cpu_data.x86_mask == 0x4))
  621. phys_addr = 36;
  622. size_or_mask = SIZE_OR_MASK_BITS(phys_addr);
  623. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  624. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  625. boot_cpu_data.x86 == 6) {
  626. /*
  627. * VIA C* family have Intel style MTRRs,
  628. * but don't support PAE
  629. */
  630. size_or_mask = SIZE_OR_MASK_BITS(32);
  631. size_and_mask = 0;
  632. phys_addr = 32;
  633. }
  634. } else {
  635. switch (boot_cpu_data.x86_vendor) {
  636. case X86_VENDOR_AMD:
  637. if (cpu_has_k6_mtrr) {
  638. /* Pre-Athlon (K6) AMD CPU MTRRs */
  639. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  640. size_or_mask = SIZE_OR_MASK_BITS(32);
  641. size_and_mask = 0;
  642. }
  643. break;
  644. case X86_VENDOR_CENTAUR:
  645. if (cpu_has_centaur_mcr) {
  646. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  647. size_or_mask = SIZE_OR_MASK_BITS(32);
  648. size_and_mask = 0;
  649. }
  650. break;
  651. case X86_VENDOR_CYRIX:
  652. if (cpu_has_cyrix_arr) {
  653. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  654. size_or_mask = SIZE_OR_MASK_BITS(32);
  655. size_and_mask = 0;
  656. }
  657. break;
  658. default:
  659. break;
  660. }
  661. }
  662. if (mtrr_if) {
  663. set_num_var_ranges();
  664. init_table();
  665. if (use_intel()) {
  666. get_mtrr_state();
  667. if (mtrr_cleanup(phys_addr)) {
  668. changed_by_mtrr_cleanup = 1;
  669. mtrr_if->set_all();
  670. }
  671. }
  672. }
  673. }
  674. void mtrr_ap_init(void)
  675. {
  676. if (!use_intel() || mtrr_aps_delayed_init)
  677. return;
  678. /*
  679. * Ideally we should hold mtrr_mutex here to avoid mtrr entries
  680. * changed, but this routine will be called in cpu boot time,
  681. * holding the lock breaks it.
  682. *
  683. * This routine is called in two cases:
  684. *
  685. * 1. very earily time of software resume, when there absolutely
  686. * isn't mtrr entry changes;
  687. *
  688. * 2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
  689. * lock to prevent mtrr entry changes
  690. */
  691. set_mtrr_from_inactive_cpu(~0U, 0, 0, 0);
  692. }
  693. /**
  694. * Save current fixed-range MTRR state of the first cpu in cpu_online_mask.
  695. */
  696. void mtrr_save_state(void)
  697. {
  698. int first_cpu;
  699. get_online_cpus();
  700. first_cpu = cpumask_first(cpu_online_mask);
  701. smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
  702. put_online_cpus();
  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);