main.c 23 KB

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