main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. 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];
  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 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_warning("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_warning("mtrr: your processor doesn't support write-combining\n");
  269. return -ENOSYS;
  270. }
  271. if (!size) {
  272. pr_warning("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_warning("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_warning("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_warning("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_warning("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. EXPORT_SYMBOL(mtrr_add);
  404. /**
  405. * mtrr_del_page - delete a memory type region
  406. * @reg: Register returned by mtrr_add
  407. * @base: Physical base address
  408. * @size: Size of region
  409. *
  410. * If register is supplied then base and size are ignored. This is
  411. * how drivers should call it.
  412. *
  413. * Releases an MTRR region. If the usage count drops to zero the
  414. * register is freed and the region returns to default state.
  415. * On success the register is returned, on failure a negative error
  416. * code.
  417. */
  418. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  419. {
  420. int i, max;
  421. mtrr_type ltype;
  422. unsigned long lbase, lsize;
  423. int error = -EINVAL;
  424. if (!mtrr_enabled())
  425. return -ENODEV;
  426. max = num_var_ranges;
  427. /* No CPU hotplug when we change MTRR entries */
  428. get_online_cpus();
  429. mutex_lock(&mtrr_mutex);
  430. if (reg < 0) {
  431. /* Search for existing MTRR */
  432. for (i = 0; i < max; ++i) {
  433. mtrr_if->get(i, &lbase, &lsize, &ltype);
  434. if (lbase == base && lsize == size) {
  435. reg = i;
  436. break;
  437. }
  438. }
  439. if (reg < 0) {
  440. pr_debug("mtrr: no MTRR for %lx000,%lx000 found\n",
  441. base, size);
  442. goto out;
  443. }
  444. }
  445. if (reg >= max) {
  446. pr_warning("mtrr: register: %d too big\n", reg);
  447. goto out;
  448. }
  449. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  450. if (lsize < 1) {
  451. pr_warning("mtrr: MTRR %d not used\n", reg);
  452. goto out;
  453. }
  454. if (mtrr_usage_table[reg] < 1) {
  455. pr_warning("mtrr: reg: %d has count=0\n", reg);
  456. goto out;
  457. }
  458. if (--mtrr_usage_table[reg] < 1)
  459. set_mtrr(reg, 0, 0, 0);
  460. error = reg;
  461. out:
  462. mutex_unlock(&mtrr_mutex);
  463. put_online_cpus();
  464. return error;
  465. }
  466. /**
  467. * mtrr_del - delete a memory type region
  468. * @reg: Register returned by mtrr_add
  469. * @base: Physical base address
  470. * @size: Size of region
  471. *
  472. * If register is supplied then base and size are ignored. This is
  473. * how drivers should call it.
  474. *
  475. * Releases an MTRR region. If the usage count drops to zero the
  476. * register is freed and the region returns to default state.
  477. * On success the register is returned, on failure a negative error
  478. * code.
  479. */
  480. int mtrr_del(int reg, unsigned long base, unsigned long size)
  481. {
  482. if (!mtrr_enabled())
  483. return -ENODEV;
  484. if (mtrr_check(base, size))
  485. return -EINVAL;
  486. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  487. }
  488. EXPORT_SYMBOL(mtrr_del);
  489. /**
  490. * arch_phys_wc_add - add a WC MTRR and handle errors if PAT is unavailable
  491. * @base: Physical base address
  492. * @size: Size of region
  493. *
  494. * If PAT is available, this does nothing. If PAT is unavailable, it
  495. * attempts to add a WC MTRR covering size bytes starting at base and
  496. * logs an error if this fails.
  497. *
  498. * The called should provide a power of two size on an equivalent
  499. * power of two boundary.
  500. *
  501. * Drivers must store the return value to pass to mtrr_del_wc_if_needed,
  502. * but drivers should not try to interpret that return value.
  503. */
  504. int arch_phys_wc_add(unsigned long base, unsigned long size)
  505. {
  506. int ret;
  507. if (pat_enabled() || !mtrr_enabled())
  508. return 0; /* Success! (We don't need to do anything.) */
  509. ret = mtrr_add(base, size, MTRR_TYPE_WRCOMB, true);
  510. if (ret < 0) {
  511. pr_warn("Failed to add WC MTRR for [%p-%p]; performance may suffer.",
  512. (void *)base, (void *)(base + size - 1));
  513. return ret;
  514. }
  515. return ret + MTRR_TO_PHYS_WC_OFFSET;
  516. }
  517. EXPORT_SYMBOL(arch_phys_wc_add);
  518. /*
  519. * arch_phys_wc_del - undoes arch_phys_wc_add
  520. * @handle: Return value from arch_phys_wc_add
  521. *
  522. * This cleans up after mtrr_add_wc_if_needed.
  523. *
  524. * The API guarantees that mtrr_del_wc_if_needed(error code) and
  525. * mtrr_del_wc_if_needed(0) do nothing.
  526. */
  527. void arch_phys_wc_del(int handle)
  528. {
  529. if (handle >= 1) {
  530. WARN_ON(handle < MTRR_TO_PHYS_WC_OFFSET);
  531. mtrr_del(handle - MTRR_TO_PHYS_WC_OFFSET, 0, 0);
  532. }
  533. }
  534. EXPORT_SYMBOL(arch_phys_wc_del);
  535. /*
  536. * arch_phys_wc_index - translates arch_phys_wc_add's return value
  537. * @handle: Return value from arch_phys_wc_add
  538. *
  539. * This will turn the return value from arch_phys_wc_add into an mtrr
  540. * index suitable for debugging.
  541. *
  542. * Note: There is no legitimate use for this function, except possibly
  543. * in printk line. Alas there is an illegitimate use in some ancient
  544. * drm ioctls.
  545. */
  546. int arch_phys_wc_index(int handle)
  547. {
  548. if (handle < MTRR_TO_PHYS_WC_OFFSET)
  549. return -1;
  550. else
  551. return handle - MTRR_TO_PHYS_WC_OFFSET;
  552. }
  553. EXPORT_SYMBOL_GPL(arch_phys_wc_index);
  554. /*
  555. * HACK ALERT!
  556. * These should be called implicitly, but we can't yet until all the initcall
  557. * stuff is done...
  558. */
  559. static void __init init_ifs(void)
  560. {
  561. #ifndef CONFIG_X86_64
  562. amd_init_mtrr();
  563. cyrix_init_mtrr();
  564. centaur_init_mtrr();
  565. #endif
  566. }
  567. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  568. * MTRR driver doesn't require this
  569. */
  570. struct mtrr_value {
  571. mtrr_type ltype;
  572. unsigned long lbase;
  573. unsigned long lsize;
  574. };
  575. static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
  576. static int mtrr_save(void)
  577. {
  578. int i;
  579. for (i = 0; i < num_var_ranges; i++) {
  580. mtrr_if->get(i, &mtrr_value[i].lbase,
  581. &mtrr_value[i].lsize,
  582. &mtrr_value[i].ltype);
  583. }
  584. return 0;
  585. }
  586. static void mtrr_restore(void)
  587. {
  588. int i;
  589. for (i = 0; i < num_var_ranges; i++) {
  590. if (mtrr_value[i].lsize) {
  591. set_mtrr(i, mtrr_value[i].lbase,
  592. mtrr_value[i].lsize,
  593. mtrr_value[i].ltype);
  594. }
  595. }
  596. }
  597. static struct syscore_ops mtrr_syscore_ops = {
  598. .suspend = mtrr_save,
  599. .resume = mtrr_restore,
  600. };
  601. int __initdata changed_by_mtrr_cleanup;
  602. #define SIZE_OR_MASK_BITS(n) (~((1ULL << ((n) - PAGE_SHIFT)) - 1))
  603. /**
  604. * mtrr_bp_init - initialize mtrrs on the boot CPU
  605. *
  606. * This needs to be called early; before any of the other CPUs are
  607. * initialized (i.e. before smp_init()).
  608. *
  609. */
  610. void __init mtrr_bp_init(void)
  611. {
  612. u32 phys_addr;
  613. init_ifs();
  614. phys_addr = 32;
  615. if (cpu_has_mtrr) {
  616. mtrr_if = &generic_mtrr_ops;
  617. size_or_mask = SIZE_OR_MASK_BITS(36);
  618. size_and_mask = 0x00f00000;
  619. phys_addr = 36;
  620. /*
  621. * This is an AMD specific MSR, but we assume(hope?) that
  622. * Intel will implement it too when they extend the address
  623. * bus of the Xeon.
  624. */
  625. if (cpuid_eax(0x80000000) >= 0x80000008) {
  626. phys_addr = cpuid_eax(0x80000008) & 0xff;
  627. /* CPUID workaround for Intel 0F33/0F34 CPU */
  628. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  629. boot_cpu_data.x86 == 0xF &&
  630. boot_cpu_data.x86_model == 0x3 &&
  631. (boot_cpu_data.x86_mask == 0x3 ||
  632. boot_cpu_data.x86_mask == 0x4))
  633. phys_addr = 36;
  634. size_or_mask = SIZE_OR_MASK_BITS(phys_addr);
  635. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  636. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  637. boot_cpu_data.x86 == 6) {
  638. /*
  639. * VIA C* family have Intel style MTRRs,
  640. * but don't support PAE
  641. */
  642. size_or_mask = SIZE_OR_MASK_BITS(32);
  643. size_and_mask = 0;
  644. phys_addr = 32;
  645. }
  646. } else {
  647. switch (boot_cpu_data.x86_vendor) {
  648. case X86_VENDOR_AMD:
  649. if (cpu_feature_enabled(X86_FEATURE_K6_MTRR)) {
  650. /* Pre-Athlon (K6) AMD CPU MTRRs */
  651. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  652. size_or_mask = SIZE_OR_MASK_BITS(32);
  653. size_and_mask = 0;
  654. }
  655. break;
  656. case X86_VENDOR_CENTAUR:
  657. if (cpu_feature_enabled(X86_FEATURE_CENTAUR_MCR)) {
  658. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  659. size_or_mask = SIZE_OR_MASK_BITS(32);
  660. size_and_mask = 0;
  661. }
  662. break;
  663. case X86_VENDOR_CYRIX:
  664. if (cpu_feature_enabled(X86_FEATURE_CYRIX_ARR)) {
  665. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  666. size_or_mask = SIZE_OR_MASK_BITS(32);
  667. size_and_mask = 0;
  668. }
  669. break;
  670. default:
  671. break;
  672. }
  673. }
  674. if (mtrr_if) {
  675. __mtrr_enabled = true;
  676. set_num_var_ranges();
  677. init_table();
  678. if (use_intel()) {
  679. /* BIOS may override */
  680. __mtrr_enabled = get_mtrr_state();
  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. void mtrr_ap_init(void)
  691. {
  692. if (!mtrr_enabled())
  693. return;
  694. if (!use_intel() || mtrr_aps_delayed_init)
  695. return;
  696. /*
  697. * Ideally we should hold mtrr_mutex here to avoid mtrr entries
  698. * changed, but this routine will be called in cpu boot time,
  699. * holding the lock breaks it.
  700. *
  701. * This routine is called in two cases:
  702. *
  703. * 1. very earily time of software resume, when there absolutely
  704. * isn't mtrr entry changes;
  705. *
  706. * 2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
  707. * lock to prevent mtrr entry changes
  708. */
  709. set_mtrr_from_inactive_cpu(~0U, 0, 0, 0);
  710. }
  711. /**
  712. * Save current fixed-range MTRR state of the first cpu in cpu_online_mask.
  713. */
  714. void mtrr_save_state(void)
  715. {
  716. int first_cpu;
  717. if (!mtrr_enabled())
  718. return;
  719. get_online_cpus();
  720. first_cpu = cpumask_first(cpu_online_mask);
  721. smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
  722. put_online_cpus();
  723. }
  724. void set_mtrr_aps_delayed_init(void)
  725. {
  726. if (!mtrr_enabled())
  727. return;
  728. if (!use_intel())
  729. return;
  730. mtrr_aps_delayed_init = true;
  731. }
  732. /*
  733. * Delayed MTRR initialization for all AP's
  734. */
  735. void mtrr_aps_init(void)
  736. {
  737. if (!use_intel() || !mtrr_enabled())
  738. return;
  739. /*
  740. * Check if someone has requested the delay of AP MTRR initialization,
  741. * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
  742. * then we are done.
  743. */
  744. if (!mtrr_aps_delayed_init)
  745. return;
  746. set_mtrr(~0U, 0, 0, 0);
  747. mtrr_aps_delayed_init = false;
  748. }
  749. void mtrr_bp_restore(void)
  750. {
  751. if (!use_intel() || !mtrr_enabled())
  752. return;
  753. mtrr_if->set_all();
  754. }
  755. static int __init mtrr_init_finialize(void)
  756. {
  757. if (!mtrr_enabled())
  758. return 0;
  759. if (use_intel()) {
  760. if (!changed_by_mtrr_cleanup)
  761. mtrr_state_warn();
  762. return 0;
  763. }
  764. /*
  765. * The CPU has no MTRR and seems to not support SMP. They have
  766. * specific drivers, we use a tricky method to support
  767. * suspend/resume for them.
  768. *
  769. * TBD: is there any system with such CPU which supports
  770. * suspend/resume? If no, we should remove the code.
  771. */
  772. register_syscore_ops(&mtrr_syscore_ops);
  773. return 0;
  774. }
  775. subsys_initcall(mtrr_init_finialize);