main.c 23 KB

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