main.c 20 KB

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