mtrr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 <linux/rcupdate.h>
  40. #include <asm/cpufeature.h>
  41. #include <asm/e820/api.h>
  42. #include <asm/mtrr.h>
  43. #include <asm/msr.h>
  44. #include <asm/pat.h>
  45. #include "mtrr.h"
  46. /* arch_phys_wc_add returns an MTRR register index plus this offset. */
  47. #define MTRR_TO_PHYS_WC_OFFSET 1000
  48. u32 num_var_ranges;
  49. static bool __mtrr_enabled;
  50. static bool mtrr_enabled(void)
  51. {
  52. return __mtrr_enabled;
  53. }
  54. unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
  55. static DEFINE_MUTEX(mtrr_mutex);
  56. u64 size_or_mask, size_and_mask;
  57. static bool mtrr_aps_delayed_init;
  58. static const struct mtrr_ops *mtrr_ops[X86_VENDOR_NUM] __ro_after_init;
  59. const struct mtrr_ops *mtrr_if;
  60. static void set_mtrr(unsigned int reg, unsigned long base,
  61. unsigned long size, mtrr_type type);
  62. void __init set_mtrr_ops(const struct mtrr_ops *ops)
  63. {
  64. if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
  65. mtrr_ops[ops->vendor] = ops;
  66. }
  67. /* Returns non-zero if we have the write-combining memory type */
  68. static int have_wrcomb(void)
  69. {
  70. struct pci_dev *dev;
  71. dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL);
  72. if (dev != NULL) {
  73. /*
  74. * ServerWorks LE chipsets < rev 6 have problems with
  75. * write-combining. Don't allow it and leave room for other
  76. * chipsets to be tagged
  77. */
  78. if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
  79. dev->device == PCI_DEVICE_ID_SERVERWORKS_LE &&
  80. dev->revision <= 5) {
  81. pr_info("Serverworks LE rev < 6 detected. Write-combining disabled.\n");
  82. pci_dev_put(dev);
  83. return 0;
  84. }
  85. /*
  86. * Intel 450NX errata # 23. Non ascending cacheline evictions to
  87. * write combining memory may resulting in data corruption
  88. */
  89. if (dev->vendor == PCI_VENDOR_ID_INTEL &&
  90. dev->device == PCI_DEVICE_ID_INTEL_82451NX) {
  91. pr_info("Intel 450NX MMC detected. Write-combining disabled.\n");
  92. pci_dev_put(dev);
  93. return 0;
  94. }
  95. pci_dev_put(dev);
  96. }
  97. return mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0;
  98. }
  99. /* This function returns the number of variable MTRRs */
  100. static void __init set_num_var_ranges(void)
  101. {
  102. unsigned long config = 0, dummy;
  103. if (use_intel())
  104. rdmsr(MSR_MTRRcap, config, dummy);
  105. else if (is_cpu(AMD) || is_cpu(HYGON))
  106. config = 2;
  107. else if (is_cpu(CYRIX) || is_cpu(CENTAUR))
  108. config = 8;
  109. num_var_ranges = config & 0xff;
  110. }
  111. static void __init init_table(void)
  112. {
  113. int i, max;
  114. max = num_var_ranges;
  115. for (i = 0; i < max; i++)
  116. mtrr_usage_table[i] = 1;
  117. }
  118. struct set_mtrr_data {
  119. unsigned long smp_base;
  120. unsigned long smp_size;
  121. unsigned int smp_reg;
  122. mtrr_type smp_type;
  123. };
  124. /**
  125. * mtrr_rendezvous_handler - Work done in the synchronization handler. Executed
  126. * by all the CPUs.
  127. * @info: pointer to mtrr configuration data
  128. *
  129. * Returns nothing.
  130. */
  131. static int mtrr_rendezvous_handler(void *info)
  132. {
  133. struct set_mtrr_data *data = info;
  134. /*
  135. * We use this same function to initialize the mtrrs during boot,
  136. * resume, runtime cpu online and on an explicit request to set a
  137. * specific MTRR.
  138. *
  139. * During boot or suspend, the state of the boot cpu's mtrrs has been
  140. * saved, and we want to replicate that across all the cpus that come
  141. * online (either at the end of boot or resume or during a runtime cpu
  142. * online). If we're doing that, @reg is set to something special and on
  143. * all the cpu's we do mtrr_if->set_all() (On the logical cpu that
  144. * started the boot/resume sequence, this might be a duplicate
  145. * set_all()).
  146. */
  147. if (data->smp_reg != ~0U) {
  148. mtrr_if->set(data->smp_reg, data->smp_base,
  149. data->smp_size, data->smp_type);
  150. } else if (mtrr_aps_delayed_init || !cpu_online(smp_processor_id())) {
  151. mtrr_if->set_all();
  152. }
  153. return 0;
  154. }
  155. static inline int types_compatible(mtrr_type type1, mtrr_type type2)
  156. {
  157. return type1 == MTRR_TYPE_UNCACHABLE ||
  158. type2 == MTRR_TYPE_UNCACHABLE ||
  159. (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
  160. (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
  161. }
  162. /**
  163. * set_mtrr - update mtrrs on all processors
  164. * @reg: mtrr in question
  165. * @base: mtrr base
  166. * @size: mtrr size
  167. * @type: mtrr type
  168. *
  169. * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
  170. *
  171. * 1. Queue work to do the following on all processors:
  172. * 2. Disable Interrupts
  173. * 3. Wait for all procs to do so
  174. * 4. Enter no-fill cache mode
  175. * 5. Flush caches
  176. * 6. Clear PGE bit
  177. * 7. Flush all TLBs
  178. * 8. Disable all range registers
  179. * 9. Update the MTRRs
  180. * 10. Enable all range registers
  181. * 11. Flush all TLBs and caches again
  182. * 12. Enter normal cache mode and reenable caching
  183. * 13. Set PGE
  184. * 14. Wait for buddies to catch up
  185. * 15. Enable interrupts.
  186. *
  187. * What does that mean for us? Well, stop_machine() will ensure that
  188. * the rendezvous handler is started on each CPU. And in lockstep they
  189. * do the state transition of disabling interrupts, updating MTRR's
  190. * (the CPU vendors may each do it differently, so we call mtrr_if->set()
  191. * callback and let them take care of it.) and enabling interrupts.
  192. *
  193. * Note that the mechanism is the same for UP systems, too; all the SMP stuff
  194. * becomes nops.
  195. */
  196. static void
  197. set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type type)
  198. {
  199. struct set_mtrr_data data = { .smp_reg = reg,
  200. .smp_base = base,
  201. .smp_size = size,
  202. .smp_type = type
  203. };
  204. stop_machine(mtrr_rendezvous_handler, &data, cpu_online_mask);
  205. }
  206. static void set_mtrr_cpuslocked(unsigned int reg, unsigned long base,
  207. unsigned long size, mtrr_type type)
  208. {
  209. struct set_mtrr_data data = { .smp_reg = reg,
  210. .smp_base = base,
  211. .smp_size = size,
  212. .smp_type = type
  213. };
  214. stop_machine_cpuslocked(mtrr_rendezvous_handler, &data, cpu_online_mask);
  215. }
  216. static void set_mtrr_from_inactive_cpu(unsigned int reg, unsigned long base,
  217. unsigned long size, mtrr_type type)
  218. {
  219. struct set_mtrr_data data = { .smp_reg = reg,
  220. .smp_base = base,
  221. .smp_size = size,
  222. .smp_type = type
  223. };
  224. stop_machine_from_inactive_cpu(mtrr_rendezvous_handler, &data,
  225. cpu_callout_mask);
  226. }
  227. /**
  228. * mtrr_add_page - Add a memory type region
  229. * @base: Physical base address of region in pages (in units of 4 kB!)
  230. * @size: Physical size of region in pages (4 kB)
  231. * @type: Type of MTRR desired
  232. * @increment: If this is true do usage counting on the region
  233. *
  234. * Memory type region registers control the caching on newer Intel and
  235. * non Intel processors. This function allows drivers to request an
  236. * MTRR is added. The details and hardware specifics of each processor's
  237. * implementation are hidden from the caller, but nevertheless the
  238. * caller should expect to need to provide a power of two size on an
  239. * equivalent power of two boundary.
  240. *
  241. * If the region cannot be added either because all regions are in use
  242. * or the CPU cannot support it a negative value is returned. On success
  243. * the register number for this entry is returned, but should be treated
  244. * as a cookie only.
  245. *
  246. * On a multiprocessor machine the changes are made to all processors.
  247. * This is required on x86 by the Intel processors.
  248. *
  249. * The available types are
  250. *
  251. * %MTRR_TYPE_UNCACHABLE - No caching
  252. *
  253. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  254. *
  255. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  256. *
  257. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  258. *
  259. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  260. * failures and do not wish system log messages to be sent.
  261. */
  262. int mtrr_add_page(unsigned long base, unsigned long size,
  263. unsigned int type, bool increment)
  264. {
  265. unsigned long lbase, lsize;
  266. int i, replace, error;
  267. mtrr_type ltype;
  268. if (!mtrr_enabled())
  269. return -ENXIO;
  270. error = mtrr_if->validate_add_page(base, size, type);
  271. if (error)
  272. return error;
  273. if (type >= MTRR_NUM_TYPES) {
  274. pr_warn("type: %u invalid\n", type);
  275. return -EINVAL;
  276. }
  277. /* If the type is WC, check that this processor supports it */
  278. if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
  279. pr_warn("your processor doesn't support write-combining\n");
  280. return -ENOSYS;
  281. }
  282. if (!size) {
  283. pr_warn("zero sized request\n");
  284. return -EINVAL;
  285. }
  286. if ((base | (base + size - 1)) >>
  287. (boot_cpu_data.x86_phys_bits - PAGE_SHIFT)) {
  288. pr_warn("base or size exceeds the MTRR width\n");
  289. return -EINVAL;
  290. }
  291. error = -EINVAL;
  292. replace = -1;
  293. /* No CPU hotplug when we change MTRR entries */
  294. get_online_cpus();
  295. /* Search for existing MTRR */
  296. mutex_lock(&mtrr_mutex);
  297. for (i = 0; i < num_var_ranges; ++i) {
  298. mtrr_if->get(i, &lbase, &lsize, &ltype);
  299. if (!lsize || base > lbase + lsize - 1 ||
  300. base + size - 1 < lbase)
  301. continue;
  302. /*
  303. * At this point we know there is some kind of
  304. * overlap/enclosure
  305. */
  306. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  307. if (base <= lbase &&
  308. base + size - 1 >= lbase + lsize - 1) {
  309. /* New region encloses an existing region */
  310. if (type == ltype) {
  311. replace = replace == -1 ? i : -2;
  312. continue;
  313. } else if (types_compatible(type, ltype))
  314. continue;
  315. }
  316. pr_warn("0x%lx000,0x%lx000 overlaps existing 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("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("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("size and base must be multiples of 4 kiB\n");
  362. pr_debug("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("no MTRR for %lx000,%lx000 found\n",
  450. base, size);
  451. goto out;
  452. }
  453. }
  454. if (reg >= max) {
  455. pr_warn("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 %d not used\n", reg);
  461. goto out;
  462. }
  463. if (mtrr_usage_table[reg] < 1) {
  464. pr_warn("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("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. rcu_cpu_starting(smp_processor_id());
  714. /*
  715. * Ideally we should hold mtrr_mutex here to avoid mtrr entries
  716. * changed, but this routine will be called in cpu boot time,
  717. * holding the lock breaks it.
  718. *
  719. * This routine is called in two cases:
  720. *
  721. * 1. very earily time of software resume, when there absolutely
  722. * isn't mtrr entry changes;
  723. *
  724. * 2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
  725. * lock to prevent mtrr entry changes
  726. */
  727. set_mtrr_from_inactive_cpu(~0U, 0, 0, 0);
  728. }
  729. /**
  730. * Save current fixed-range MTRR state of the first cpu in cpu_online_mask.
  731. */
  732. void mtrr_save_state(void)
  733. {
  734. int first_cpu;
  735. if (!mtrr_enabled())
  736. return;
  737. first_cpu = cpumask_first(cpu_online_mask);
  738. smp_call_function_single(first_cpu, mtrr_save_fixed_ranges, NULL, 1);
  739. }
  740. void set_mtrr_aps_delayed_init(void)
  741. {
  742. if (!mtrr_enabled())
  743. return;
  744. if (!use_intel())
  745. return;
  746. mtrr_aps_delayed_init = true;
  747. }
  748. /*
  749. * Delayed MTRR initialization for all AP's
  750. */
  751. void mtrr_aps_init(void)
  752. {
  753. if (!use_intel() || !mtrr_enabled())
  754. return;
  755. /*
  756. * Check if someone has requested the delay of AP MTRR initialization,
  757. * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
  758. * then we are done.
  759. */
  760. if (!mtrr_aps_delayed_init)
  761. return;
  762. set_mtrr(~0U, 0, 0, 0);
  763. mtrr_aps_delayed_init = false;
  764. }
  765. void mtrr_bp_restore(void)
  766. {
  767. if (!use_intel() || !mtrr_enabled())
  768. return;
  769. mtrr_if->set_all();
  770. }
  771. static int __init mtrr_init_finialize(void)
  772. {
  773. if (!mtrr_enabled())
  774. return 0;
  775. if (use_intel()) {
  776. if (!changed_by_mtrr_cleanup)
  777. mtrr_state_warn();
  778. return 0;
  779. }
  780. /*
  781. * The CPU has no MTRR and seems to not support SMP. They have
  782. * specific drivers, we use a tricky method to support
  783. * suspend/resume for them.
  784. *
  785. * TBD: is there any system with such CPU which supports
  786. * suspend/resume? If no, we should remove the code.
  787. */
  788. register_syscore_ops(&mtrr_syscore_ops);
  789. return 0;
  790. }
  791. subsys_initcall(mtrr_init_finialize);