mtrr.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 [email protected]
  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 <[email protected]>
  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. #include <linux/types.h> /* FIXME: kvm_para.h needs this */
  27. #include <linux/stop_machine.h>
  28. #include <linux/kvm_para.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/export.h>
  31. #include <linux/mutex.h>
  32. #include <linux/init.h>
  33. #include <linux/sort.h>
  34. #include <linux/cpu.h>
  35. #include <linux/pci.h>
  36. #include <linux/smp.h>
  37. #include <linux/syscore_ops.h>
  38. #include <linux/rcupdate.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/memtype.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("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("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) || is_cpu(HYGON))
  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("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("your processor doesn't support write-combining\n");
  279. return -ENOSYS;
  280. }
  281. if (!size) {
  282. pr_warn("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("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. cpus_read_lock();
  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("0x%lx000,0x%lx000 overlaps existing 0x%lx000,0x%lx000\n", base, size, lbase,
  316. lsize);
  317. goto out;
  318. }
  319. /* New region is enclosed by an existing region */
  320. if (ltype != type) {
  321. if (types_compatible(type, ltype))
  322. continue;
  323. pr_warn("type mismatch for %lx000,%lx000 old: %s new: %s\n",
  324. base, size, mtrr_attrib_to_str(ltype),
  325. mtrr_attrib_to_str(type));
  326. goto out;
  327. }
  328. if (increment)
  329. ++mtrr_usage_table[i];
  330. error = i;
  331. goto out;
  332. }
  333. /* Search for an empty MTRR */
  334. i = mtrr_if->get_free_region(base, size, replace);
  335. if (i >= 0) {
  336. set_mtrr_cpuslocked(i, base, size, type);
  337. if (likely(replace < 0)) {
  338. mtrr_usage_table[i] = 1;
  339. } else {
  340. mtrr_usage_table[i] = mtrr_usage_table[replace];
  341. if (increment)
  342. mtrr_usage_table[i]++;
  343. if (unlikely(replace != i)) {
  344. set_mtrr_cpuslocked(replace, 0, 0, 0);
  345. mtrr_usage_table[replace] = 0;
  346. }
  347. }
  348. } else {
  349. pr_info("no more MTRRs available\n");
  350. }
  351. error = i;
  352. out:
  353. mutex_unlock(&mtrr_mutex);
  354. cpus_read_unlock();
  355. return error;
  356. }
  357. static int mtrr_check(unsigned long base, unsigned long size)
  358. {
  359. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  360. pr_warn("size and base must be multiples of 4 kiB\n");
  361. pr_debug("size: 0x%lx base: 0x%lx\n", size, base);
  362. dump_stack();
  363. return -1;
  364. }
  365. return 0;
  366. }
  367. /**
  368. * mtrr_add - Add a memory type region
  369. * @base: Physical base address of region
  370. * @size: Physical size of region
  371. * @type: Type of MTRR desired
  372. * @increment: If this is true do usage counting on the region
  373. *
  374. * Memory type region registers control the caching on newer Intel and
  375. * non Intel processors. This function allows drivers to request an
  376. * MTRR is added. The details and hardware specifics of each processor's
  377. * implementation are hidden from the caller, but nevertheless the
  378. * caller should expect to need to provide a power of two size on an
  379. * equivalent power of two boundary.
  380. *
  381. * If the region cannot be added either because all regions are in use
  382. * or the CPU cannot support it a negative value is returned. On success
  383. * the register number for this entry is returned, but should be treated
  384. * as a cookie only.
  385. *
  386. * On a multiprocessor machine the changes are made to all processors.
  387. * This is required on x86 by the Intel processors.
  388. *
  389. * The available types are
  390. *
  391. * %MTRR_TYPE_UNCACHABLE - No caching
  392. *
  393. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  394. *
  395. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  396. *
  397. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  398. *
  399. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  400. * failures and do not wish system log messages to be sent.
  401. */
  402. int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  403. bool increment)
  404. {
  405. if (!mtrr_enabled())
  406. return -ENODEV;
  407. if (mtrr_check(base, size))
  408. return -EINVAL;
  409. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  410. increment);
  411. }
  412. /**
  413. * mtrr_del_page - delete a memory type region
  414. * @reg: Register returned by mtrr_add
  415. * @base: Physical base address
  416. * @size: Size of region
  417. *
  418. * If register is supplied then base and size are ignored. This is
  419. * how drivers should call it.
  420. *
  421. * Releases an MTRR region. If the usage count drops to zero the
  422. * register is freed and the region returns to default state.
  423. * On success the register is returned, on failure a negative error
  424. * code.
  425. */
  426. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  427. {
  428. int i, max;
  429. mtrr_type ltype;
  430. unsigned long lbase, lsize;
  431. int error = -EINVAL;
  432. if (!mtrr_enabled())
  433. return -ENODEV;
  434. max = num_var_ranges;
  435. /* No CPU hotplug when we change MTRR entries */
  436. cpus_read_lock();
  437. mutex_lock(&mtrr_mutex);
  438. if (reg < 0) {
  439. /* Search for existing MTRR */
  440. for (i = 0; i < max; ++i) {
  441. mtrr_if->get(i, &lbase, &lsize, &ltype);
  442. if (lbase == base && lsize == size) {
  443. reg = i;
  444. break;
  445. }
  446. }
  447. if (reg < 0) {
  448. pr_debug("no MTRR for %lx000,%lx000 found\n",
  449. base, size);
  450. goto out;
  451. }
  452. }
  453. if (reg >= max) {
  454. pr_warn("register: %d too big\n", reg);
  455. goto out;
  456. }
  457. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  458. if (lsize < 1) {
  459. pr_warn("MTRR %d not used\n", reg);
  460. goto out;
  461. }
  462. if (mtrr_usage_table[reg] < 1) {
  463. pr_warn("reg: %d has count=0\n", reg);
  464. goto out;
  465. }
  466. if (--mtrr_usage_table[reg] < 1)
  467. set_mtrr_cpuslocked(reg, 0, 0, 0);
  468. error = reg;
  469. out:
  470. mutex_unlock(&mtrr_mutex);
  471. cpus_read_unlock();
  472. return error;
  473. }
  474. /**
  475. * mtrr_del - delete a memory type region
  476. * @reg: Register returned by mtrr_add
  477. * @base: Physical base address
  478. * @size: Size of region
  479. *
  480. * If register is supplied then base and size are ignored. This is
  481. * how drivers should call it.
  482. *
  483. * Releases an MTRR region. If the usage count drops to zero the
  484. * register is freed and the region returns to default state.
  485. * On success the register is returned, on failure a negative error
  486. * code.
  487. */
  488. int mtrr_del(int reg, unsigned long base, unsigned long size)
  489. {
  490. if (!mtrr_enabled())
  491. return -ENODEV;
  492. if (mtrr_check(base, size))
  493. return -EINVAL;
  494. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  495. }
  496. /**
  497. * arch_phys_wc_add - add a WC MTRR and handle errors if PAT is unavailable
  498. * @base: Physical base address
  499. * @size: Size of region
  500. *
  501. * If PAT is available, this does nothing. If PAT is unavailable, it
  502. * attempts to add a WC MTRR covering size bytes starting at base and
  503. * logs an error if this fails.
  504. *
  505. * The called should provide a power of two size on an equivalent
  506. * power of two boundary.
  507. *
  508. * Drivers must store the return value to pass to mtrr_del_wc_if_needed,
  509. * but drivers should not try to interpret that return value.
  510. */
  511. int arch_phys_wc_add(unsigned long base, unsigned long size)
  512. {
  513. int ret;
  514. if (pat_enabled() || !mtrr_enabled())
  515. return 0; /* Success! (We don't need to do anything.) */
  516. ret = mtrr_add(base, size, MTRR_TYPE_WRCOMB, true);
  517. if (ret < 0) {
  518. pr_warn("Failed to add WC MTRR for [%p-%p]; performance may suffer.",
  519. (void *)base, (void *)(base + size - 1));
  520. return ret;
  521. }
  522. return ret + MTRR_TO_PHYS_WC_OFFSET;
  523. }
  524. EXPORT_SYMBOL(arch_phys_wc_add);
  525. /*
  526. * arch_phys_wc_del - undoes arch_phys_wc_add
  527. * @handle: Return value from arch_phys_wc_add
  528. *
  529. * This cleans up after mtrr_add_wc_if_needed.
  530. *
  531. * The API guarantees that mtrr_del_wc_if_needed(error code) and
  532. * mtrr_del_wc_if_needed(0) do nothing.
  533. */
  534. void arch_phys_wc_del(int handle)
  535. {
  536. if (handle >= 1) {
  537. WARN_ON(handle < MTRR_TO_PHYS_WC_OFFSET);
  538. mtrr_del(handle - MTRR_TO_PHYS_WC_OFFSET, 0, 0);
  539. }
  540. }
  541. EXPORT_SYMBOL(arch_phys_wc_del);
  542. /*
  543. * arch_phys_wc_index - translates arch_phys_wc_add's return value
  544. * @handle: Return value from arch_phys_wc_add
  545. *
  546. * This will turn the return value from arch_phys_wc_add into an mtrr
  547. * index suitable for debugging.
  548. *
  549. * Note: There is no legitimate use for this function, except possibly
  550. * in printk line. Alas there is an illegitimate use in some ancient
  551. * drm ioctls.
  552. */
  553. int arch_phys_wc_index(int handle)
  554. {
  555. if (handle < MTRR_TO_PHYS_WC_OFFSET)
  556. return -1;
  557. else
  558. return handle - MTRR_TO_PHYS_WC_OFFSET;
  559. }
  560. EXPORT_SYMBOL_GPL(arch_phys_wc_index);
  561. /*
  562. * HACK ALERT!
  563. * These should be called implicitly, but we can't yet until all the initcall
  564. * stuff is done...
  565. */
  566. static void __init init_ifs(void)
  567. {
  568. #ifndef CONFIG_X86_64
  569. amd_init_mtrr();
  570. cyrix_init_mtrr();
  571. centaur_init_mtrr();
  572. #endif
  573. }
  574. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  575. * MTRR driver doesn't require this
  576. */
  577. struct mtrr_value {
  578. mtrr_type ltype;
  579. unsigned long lbase;
  580. unsigned long lsize;
  581. };
  582. static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
  583. static int mtrr_save(void)
  584. {
  585. int i;
  586. for (i = 0; i < num_var_ranges; i++) {
  587. mtrr_if->get(i, &mtrr_value[i].lbase,
  588. &mtrr_value[i].lsize,
  589. &mtrr_value[i].ltype);
  590. }
  591. return 0;
  592. }
  593. static void mtrr_restore(void)
  594. {
  595. int i;
  596. for (i = 0; i < num_var_ranges; i++) {
  597. if (mtrr_value[i].lsize) {
  598. set_mtrr(i, mtrr_value[i].lbase,
  599. mtrr_value[i].lsize,
  600. mtrr_value[i].ltype);
  601. }
  602. }
  603. }
  604. static struct syscore_ops mtrr_syscore_ops = {
  605. .suspend = mtrr_save,
  606. .resume = mtrr_restore,
  607. };
  608. int __initdata changed_by_mtrr_cleanup;
  609. #define SIZE_OR_MASK_BITS(n) (~((1ULL << ((n) - PAGE_SHIFT)) - 1))
  610. /**
  611. * mtrr_bp_init - initialize mtrrs on the boot CPU
  612. *
  613. * This needs to be called early; before any of the other CPUs are
  614. * initialized (i.e. before smp_init()).
  615. *
  616. */
  617. void __init mtrr_bp_init(void)
  618. {
  619. u32 phys_addr;
  620. init_ifs();
  621. phys_addr = 32;
  622. if (boot_cpu_has(X86_FEATURE_MTRR)) {
  623. mtrr_if = &generic_mtrr_ops;
  624. size_or_mask = SIZE_OR_MASK_BITS(36);
  625. size_and_mask = 0x00f00000;
  626. phys_addr = 36;
  627. /*
  628. * This is an AMD specific MSR, but we assume(hope?) that
  629. * Intel will implement it too when they extend the address
  630. * bus of the Xeon.
  631. */
  632. if (cpuid_eax(0x80000000) >= 0x80000008) {
  633. phys_addr = cpuid_eax(0x80000008) & 0xff;
  634. /* CPUID workaround for Intel 0F33/0F34 CPU */
  635. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  636. boot_cpu_data.x86 == 0xF &&
  637. boot_cpu_data.x86_model == 0x3 &&
  638. (boot_cpu_data.x86_stepping == 0x3 ||
  639. boot_cpu_data.x86_stepping == 0x4))
  640. phys_addr = 36;
  641. size_or_mask = SIZE_OR_MASK_BITS(phys_addr);
  642. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  643. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  644. boot_cpu_data.x86 == 6) {
  645. /*
  646. * VIA C* family have Intel style MTRRs,
  647. * but don't support PAE
  648. */
  649. size_or_mask = SIZE_OR_MASK_BITS(32);
  650. size_and_mask = 0;
  651. phys_addr = 32;
  652. }
  653. } else {
  654. switch (boot_cpu_data.x86_vendor) {
  655. case X86_VENDOR_AMD:
  656. if (cpu_feature_enabled(X86_FEATURE_K6_MTRR)) {
  657. /* Pre-Athlon (K6) AMD CPU MTRRs */
  658. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  659. size_or_mask = SIZE_OR_MASK_BITS(32);
  660. size_and_mask = 0;
  661. }
  662. break;
  663. case X86_VENDOR_CENTAUR:
  664. if (cpu_feature_enabled(X86_FEATURE_CENTAUR_MCR)) {
  665. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  666. size_or_mask = SIZE_OR_MASK_BITS(32);
  667. size_and_mask = 0;
  668. }
  669. break;
  670. case X86_VENDOR_CYRIX:
  671. if (cpu_feature_enabled(X86_FEATURE_CYRIX_ARR)) {
  672. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  673. size_or_mask = SIZE_OR_MASK_BITS(32);
  674. size_and_mask = 0;
  675. }
  676. break;
  677. default:
  678. break;
  679. }
  680. }
  681. if (mtrr_if) {
  682. __mtrr_enabled = true;
  683. set_num_var_ranges();
  684. init_table();
  685. if (use_intel()) {
  686. /* BIOS may override */
  687. __mtrr_enabled = get_mtrr_state();
  688. if (mtrr_enabled())
  689. mtrr_bp_pat_init();
  690. if (mtrr_cleanup(phys_addr)) {
  691. changed_by_mtrr_cleanup = 1;
  692. mtrr_if->set_all();
  693. }
  694. }
  695. }
  696. if (!mtrr_enabled()) {
  697. pr_info("Disabled\n");
  698. /*
  699. * PAT initialization relies on MTRR's rendezvous handler.
  700. * Skip PAT init until the handler can initialize both
  701. * features independently.
  702. */
  703. pat_disable("MTRRs disabled, skipping PAT initialization too.");
  704. }
  705. }
  706. void mtrr_ap_init(void)
  707. {
  708. if (!mtrr_enabled())
  709. return;
  710. if (!use_intel() || mtrr_aps_delayed_init)
  711. return;
  712. /*
  713. * Ideally we should hold mtrr_mutex here to avoid mtrr entries
  714. * changed, but this routine will be called in cpu boot time,
  715. * holding the lock breaks it.
  716. *
  717. * This routine is called in two cases:
  718. *
  719. * 1. very early time of software resume, when there absolutely
  720. * isn't mtrr entry changes;
  721. *
  722. * 2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
  723. * lock to prevent mtrr entry changes
  724. */
  725. set_mtrr_from_inactive_cpu(~0U, 0, 0, 0);
  726. }
  727. /**
  728. * mtrr_save_state - Save current fixed-range MTRR state of the first
  729. * 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);