main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. #ifdef CONFIG_SMP
  125. struct set_mtrr_data *data = info;
  126. /*
  127. * We use this same function to initialize the mtrrs during boot,
  128. * resume, runtime cpu online and on an explicit request to set a
  129. * specific MTRR.
  130. *
  131. * During boot or suspend, the state of the boot cpu's mtrrs has been
  132. * saved, and we want to replicate that across all the cpus that come
  133. * online (either at the end of boot or resume or during a runtime cpu
  134. * online). If we're doing that, @reg is set to something special and on
  135. * all the cpu's we do mtrr_if->set_all() (On the logical cpu that
  136. * started the boot/resume sequence, this might be a duplicate
  137. * set_all()).
  138. */
  139. if (data->smp_reg != ~0U) {
  140. mtrr_if->set(data->smp_reg, data->smp_base,
  141. data->smp_size, data->smp_type);
  142. } else if (mtrr_aps_delayed_init || !cpu_online(smp_processor_id())) {
  143. mtrr_if->set_all();
  144. }
  145. #endif
  146. return 0;
  147. }
  148. static inline int types_compatible(mtrr_type type1, mtrr_type type2)
  149. {
  150. return type1 == MTRR_TYPE_UNCACHABLE ||
  151. type2 == MTRR_TYPE_UNCACHABLE ||
  152. (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
  153. (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
  154. }
  155. /**
  156. * set_mtrr - update mtrrs on all processors
  157. * @reg: mtrr in question
  158. * @base: mtrr base
  159. * @size: mtrr size
  160. * @type: mtrr type
  161. *
  162. * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
  163. *
  164. * 1. Queue work to do the following on all processors:
  165. * 2. Disable Interrupts
  166. * 3. Wait for all procs to do so
  167. * 4. Enter no-fill cache mode
  168. * 5. Flush caches
  169. * 6. Clear PGE bit
  170. * 7. Flush all TLBs
  171. * 8. Disable all range registers
  172. * 9. Update the MTRRs
  173. * 10. Enable all range registers
  174. * 11. Flush all TLBs and caches again
  175. * 12. Enter normal cache mode and reenable caching
  176. * 13. Set PGE
  177. * 14. Wait for buddies to catch up
  178. * 15. Enable interrupts.
  179. *
  180. * What does that mean for us? Well, stop_machine() will ensure that
  181. * the rendezvous handler is started on each CPU. And in lockstep they
  182. * do the state transition of disabling interrupts, updating MTRR's
  183. * (the CPU vendors may each do it differently, so we call mtrr_if->set()
  184. * callback and let them take care of it.) and enabling interrupts.
  185. *
  186. * Note that the mechanism is the same for UP systems, too; all the SMP stuff
  187. * becomes nops.
  188. */
  189. static void
  190. set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type type)
  191. {
  192. struct set_mtrr_data data = { .smp_reg = reg,
  193. .smp_base = base,
  194. .smp_size = size,
  195. .smp_type = type
  196. };
  197. stop_machine(mtrr_rendezvous_handler, &data, cpu_online_mask);
  198. }
  199. static void set_mtrr_from_inactive_cpu(unsigned int reg, unsigned long base,
  200. unsigned long size, mtrr_type type)
  201. {
  202. struct set_mtrr_data data = { .smp_reg = reg,
  203. .smp_base = base,
  204. .smp_size = size,
  205. .smp_type = type
  206. };
  207. stop_machine_from_inactive_cpu(mtrr_rendezvous_handler, &data,
  208. cpu_callout_mask);
  209. }
  210. /**
  211. * mtrr_add_page - Add a memory type region
  212. * @base: Physical base address of region in pages (in units of 4 kB!)
  213. * @size: Physical size of region in pages (4 kB)
  214. * @type: Type of MTRR desired
  215. * @increment: If this is true do usage counting on the region
  216. *
  217. * Memory type region registers control the caching on newer Intel and
  218. * non Intel processors. This function allows drivers to request an
  219. * MTRR is added. The details and hardware specifics of each processor's
  220. * implementation are hidden from the caller, but nevertheless the
  221. * caller should expect to need to provide a power of two size on an
  222. * equivalent power of two boundary.
  223. *
  224. * If the region cannot be added either because all regions are in use
  225. * or the CPU cannot support it a negative value is returned. On success
  226. * the register number for this entry is returned, but should be treated
  227. * as a cookie only.
  228. *
  229. * On a multiprocessor machine the changes are made to all processors.
  230. * This is required on x86 by the Intel processors.
  231. *
  232. * The available types are
  233. *
  234. * %MTRR_TYPE_UNCACHABLE - No caching
  235. *
  236. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  237. *
  238. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  239. *
  240. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  241. *
  242. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  243. * failures and do not wish system log messages to be sent.
  244. */
  245. int mtrr_add_page(unsigned long base, unsigned long size,
  246. unsigned int type, bool increment)
  247. {
  248. unsigned long lbase, lsize;
  249. int i, replace, error;
  250. mtrr_type ltype;
  251. if (!mtrr_if)
  252. return -ENXIO;
  253. error = mtrr_if->validate_add_page(base, size, type);
  254. if (error)
  255. return error;
  256. if (type >= MTRR_NUM_TYPES) {
  257. pr_warning("mtrr: type: %u invalid\n", type);
  258. return -EINVAL;
  259. }
  260. /* If the type is WC, check that this processor supports it */
  261. if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
  262. pr_warning("mtrr: your processor doesn't support write-combining\n");
  263. return -ENOSYS;
  264. }
  265. if (!size) {
  266. pr_warning("mtrr: zero sized request\n");
  267. return -EINVAL;
  268. }
  269. if (base & size_or_mask || size & size_or_mask) {
  270. pr_warning("mtrr: base or size exceeds the MTRR width\n");
  271. return -EINVAL;
  272. }
  273. error = -EINVAL;
  274. replace = -1;
  275. /* No CPU hotplug when we change MTRR entries */
  276. get_online_cpus();
  277. /* Search for existing MTRR */
  278. mutex_lock(&mtrr_mutex);
  279. for (i = 0; i < num_var_ranges; ++i) {
  280. mtrr_if->get(i, &lbase, &lsize, &ltype);
  281. if (!lsize || base > lbase + lsize - 1 ||
  282. base + size - 1 < lbase)
  283. continue;
  284. /*
  285. * At this point we know there is some kind of
  286. * overlap/enclosure
  287. */
  288. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  289. if (base <= lbase &&
  290. base + size - 1 >= lbase + lsize - 1) {
  291. /* New region encloses an existing region */
  292. if (type == ltype) {
  293. replace = replace == -1 ? i : -2;
  294. continue;
  295. } else if (types_compatible(type, ltype))
  296. continue;
  297. }
  298. pr_warning("mtrr: 0x%lx000,0x%lx000 overlaps existing"
  299. " 0x%lx000,0x%lx000\n", base, size, lbase,
  300. lsize);
  301. goto out;
  302. }
  303. /* New region is enclosed by an existing region */
  304. if (ltype != type) {
  305. if (types_compatible(type, ltype))
  306. continue;
  307. pr_warning("mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
  308. base, size, mtrr_attrib_to_str(ltype),
  309. mtrr_attrib_to_str(type));
  310. goto out;
  311. }
  312. if (increment)
  313. ++mtrr_usage_table[i];
  314. error = i;
  315. goto out;
  316. }
  317. /* Search for an empty MTRR */
  318. i = mtrr_if->get_free_region(base, size, replace);
  319. if (i >= 0) {
  320. set_mtrr(i, base, size, type);
  321. if (likely(replace < 0)) {
  322. mtrr_usage_table[i] = 1;
  323. } else {
  324. mtrr_usage_table[i] = mtrr_usage_table[replace];
  325. if (increment)
  326. mtrr_usage_table[i]++;
  327. if (unlikely(replace != i)) {
  328. set_mtrr(replace, 0, 0, 0);
  329. mtrr_usage_table[replace] = 0;
  330. }
  331. }
  332. } else {
  333. pr_info("mtrr: no more MTRRs available\n");
  334. }
  335. error = i;
  336. out:
  337. mutex_unlock(&mtrr_mutex);
  338. put_online_cpus();
  339. return error;
  340. }
  341. static int mtrr_check(unsigned long base, unsigned long size)
  342. {
  343. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  344. pr_warning("mtrr: size and base must be multiples of 4 kiB\n");
  345. pr_debug("mtrr: size: 0x%lx base: 0x%lx\n", size, base);
  346. dump_stack();
  347. return -1;
  348. }
  349. return 0;
  350. }
  351. /**
  352. * mtrr_add - Add a memory type region
  353. * @base: Physical base address of region
  354. * @size: Physical size of region
  355. * @type: Type of MTRR desired
  356. * @increment: If this is true do usage counting on the region
  357. *
  358. * Memory type region registers control the caching on newer Intel and
  359. * non Intel processors. This function allows drivers to request an
  360. * MTRR is added. The details and hardware specifics of each processor's
  361. * implementation are hidden from the caller, but nevertheless the
  362. * caller should expect to need to provide a power of two size on an
  363. * equivalent power of two boundary.
  364. *
  365. * If the region cannot be added either because all regions are in use
  366. * or the CPU cannot support it a negative value is returned. On success
  367. * the register number for this entry is returned, but should be treated
  368. * as a cookie only.
  369. *
  370. * On a multiprocessor machine the changes are made to all processors.
  371. * This is required on x86 by the Intel processors.
  372. *
  373. * The available types are
  374. *
  375. * %MTRR_TYPE_UNCACHABLE - No caching
  376. *
  377. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  378. *
  379. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  380. *
  381. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  382. *
  383. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  384. * failures and do not wish system log messages to be sent.
  385. */
  386. int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  387. bool increment)
  388. {
  389. if (mtrr_check(base, size))
  390. return -EINVAL;
  391. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  392. increment);
  393. }
  394. EXPORT_SYMBOL(mtrr_add);
  395. /**
  396. * mtrr_del_page - delete a memory type region
  397. * @reg: Register returned by mtrr_add
  398. * @base: Physical base address
  399. * @size: Size of region
  400. *
  401. * If register is supplied then base and size are ignored. This is
  402. * how drivers should call it.
  403. *
  404. * Releases an MTRR region. If the usage count drops to zero the
  405. * register is freed and the region returns to default state.
  406. * On success the register is returned, on failure a negative error
  407. * code.
  408. */
  409. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  410. {
  411. int i, max;
  412. mtrr_type ltype;
  413. unsigned long lbase, lsize;
  414. int error = -EINVAL;
  415. if (!mtrr_if)
  416. return -ENXIO;
  417. max = num_var_ranges;
  418. /* No CPU hotplug when we change MTRR entries */
  419. get_online_cpus();
  420. mutex_lock(&mtrr_mutex);
  421. if (reg < 0) {
  422. /* Search for existing MTRR */
  423. for (i = 0; i < max; ++i) {
  424. mtrr_if->get(i, &lbase, &lsize, &ltype);
  425. if (lbase == base && lsize == size) {
  426. reg = i;
  427. break;
  428. }
  429. }
  430. if (reg < 0) {
  431. pr_debug("mtrr: no MTRR for %lx000,%lx000 found\n",
  432. base, size);
  433. goto out;
  434. }
  435. }
  436. if (reg >= max) {
  437. pr_warning("mtrr: register: %d too big\n", reg);
  438. goto out;
  439. }
  440. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  441. if (lsize < 1) {
  442. pr_warning("mtrr: MTRR %d not used\n", reg);
  443. goto out;
  444. }
  445. if (mtrr_usage_table[reg] < 1) {
  446. pr_warning("mtrr: reg: %d has count=0\n", reg);
  447. goto out;
  448. }
  449. if (--mtrr_usage_table[reg] < 1)
  450. set_mtrr(reg, 0, 0, 0);
  451. error = reg;
  452. out:
  453. mutex_unlock(&mtrr_mutex);
  454. put_online_cpus();
  455. return error;
  456. }
  457. /**
  458. * mtrr_del - delete a memory type region
  459. * @reg: Register returned by mtrr_add
  460. * @base: Physical base address
  461. * @size: Size of region
  462. *
  463. * If register is supplied then base and size are ignored. This is
  464. * how drivers should call it.
  465. *
  466. * Releases an MTRR region. If the usage count drops to zero the
  467. * register is freed and the region returns to default state.
  468. * On success the register is returned, on failure a negative error
  469. * code.
  470. */
  471. int mtrr_del(int reg, unsigned long base, unsigned long size)
  472. {
  473. if (mtrr_check(base, size))
  474. return -EINVAL;
  475. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  476. }
  477. EXPORT_SYMBOL(mtrr_del);
  478. /*
  479. * HACK ALERT!
  480. * These should be called implicitly, but we can't yet until all the initcall
  481. * stuff is done...
  482. */
  483. static void __init init_ifs(void)
  484. {
  485. #ifndef CONFIG_X86_64
  486. amd_init_mtrr();
  487. cyrix_init_mtrr();
  488. centaur_init_mtrr();
  489. #endif
  490. }
  491. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  492. * MTRR driver doesn't require this
  493. */
  494. struct mtrr_value {
  495. mtrr_type ltype;
  496. unsigned long lbase;
  497. unsigned long lsize;
  498. };
  499. static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
  500. static int mtrr_save(void)
  501. {
  502. int i;
  503. for (i = 0; i < num_var_ranges; i++) {
  504. mtrr_if->get(i, &mtrr_value[i].lbase,
  505. &mtrr_value[i].lsize,
  506. &mtrr_value[i].ltype);
  507. }
  508. return 0;
  509. }
  510. static void mtrr_restore(void)
  511. {
  512. int i;
  513. for (i = 0; i < num_var_ranges; i++) {
  514. if (mtrr_value[i].lsize) {
  515. set_mtrr(i, mtrr_value[i].lbase,
  516. mtrr_value[i].lsize,
  517. mtrr_value[i].ltype);
  518. }
  519. }
  520. }
  521. static struct syscore_ops mtrr_syscore_ops = {
  522. .suspend = mtrr_save,
  523. .resume = mtrr_restore,
  524. };
  525. int __initdata changed_by_mtrr_cleanup;
  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 = 0xff000000; /* 36 bits */
  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 to 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 = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  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 = 0xfff00000; /* 32 bits */
  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 = 0xfff00000; /* 32 bits */
  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 = 0xfff00000; /* 32 bits */
  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 = 0xfff00000; /* 32 bits */
  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 BSP
  630. */
  631. void mtrr_save_state(void)
  632. {
  633. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1);
  634. }
  635. void set_mtrr_aps_delayed_init(void)
  636. {
  637. if (!use_intel())
  638. return;
  639. mtrr_aps_delayed_init = true;
  640. }
  641. /*
  642. * Delayed MTRR initialization for all AP's
  643. */
  644. void mtrr_aps_init(void)
  645. {
  646. if (!use_intel())
  647. return;
  648. /*
  649. * Check if someone has requested the delay of AP MTRR initialization,
  650. * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
  651. * then we are done.
  652. */
  653. if (!mtrr_aps_delayed_init)
  654. return;
  655. set_mtrr(~0U, 0, 0, 0);
  656. mtrr_aps_delayed_init = false;
  657. }
  658. void mtrr_bp_restore(void)
  659. {
  660. if (!use_intel())
  661. return;
  662. mtrr_if->set_all();
  663. }
  664. static int __init mtrr_init_finialize(void)
  665. {
  666. if (!mtrr_if)
  667. return 0;
  668. if (use_intel()) {
  669. if (!changed_by_mtrr_cleanup)
  670. mtrr_state_warn();
  671. return 0;
  672. }
  673. /*
  674. * The CPU has no MTRR and seems to not support SMP. They have
  675. * specific drivers, we use a tricky method to support
  676. * suspend/resume for them.
  677. *
  678. * TBD: is there any system with such CPU which supports
  679. * suspend/resume? If no, we should remove the code.
  680. */
  681. register_syscore_ops(&mtrr_syscore_ops);
  682. return 0;
  683. }
  684. subsys_initcall(mtrr_init_finialize);