main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 & size_or_mask || size & size_or_mask) {
  268. pr_warning("mtrr: base or size exceeds the MTRR width\n");
  269. return -EINVAL;
  270. }
  271. error = -EINVAL;
  272. replace = -1;
  273. /* No CPU hotplug when we change MTRR entries */
  274. get_online_cpus();
  275. /* Search for existing MTRR */
  276. mutex_lock(&mtrr_mutex);
  277. for (i = 0; i < num_var_ranges; ++i) {
  278. mtrr_if->get(i, &lbase, &lsize, &ltype);
  279. if (!lsize || base > lbase + lsize - 1 ||
  280. base + size - 1 < lbase)
  281. continue;
  282. /*
  283. * At this point we know there is some kind of
  284. * overlap/enclosure
  285. */
  286. if (base < lbase || base + size - 1 > lbase + lsize - 1) {
  287. if (base <= lbase &&
  288. base + size - 1 >= lbase + lsize - 1) {
  289. /* New region encloses an existing region */
  290. if (type == ltype) {
  291. replace = replace == -1 ? i : -2;
  292. continue;
  293. } else if (types_compatible(type, ltype))
  294. continue;
  295. }
  296. pr_warning("mtrr: 0x%lx000,0x%lx000 overlaps existing"
  297. " 0x%lx000,0x%lx000\n", base, size, lbase,
  298. lsize);
  299. goto out;
  300. }
  301. /* New region is enclosed by an existing region */
  302. if (ltype != type) {
  303. if (types_compatible(type, ltype))
  304. continue;
  305. pr_warning("mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
  306. base, size, mtrr_attrib_to_str(ltype),
  307. mtrr_attrib_to_str(type));
  308. goto out;
  309. }
  310. if (increment)
  311. ++mtrr_usage_table[i];
  312. error = i;
  313. goto out;
  314. }
  315. /* Search for an empty MTRR */
  316. i = mtrr_if->get_free_region(base, size, replace);
  317. if (i >= 0) {
  318. set_mtrr(i, base, size, type);
  319. if (likely(replace < 0)) {
  320. mtrr_usage_table[i] = 1;
  321. } else {
  322. mtrr_usage_table[i] = mtrr_usage_table[replace];
  323. if (increment)
  324. mtrr_usage_table[i]++;
  325. if (unlikely(replace != i)) {
  326. set_mtrr(replace, 0, 0, 0);
  327. mtrr_usage_table[replace] = 0;
  328. }
  329. }
  330. } else {
  331. pr_info("mtrr: no more MTRRs available\n");
  332. }
  333. error = i;
  334. out:
  335. mutex_unlock(&mtrr_mutex);
  336. put_online_cpus();
  337. return error;
  338. }
  339. static int mtrr_check(unsigned long base, unsigned long size)
  340. {
  341. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
  342. pr_warning("mtrr: size and base must be multiples of 4 kiB\n");
  343. pr_debug("mtrr: size: 0x%lx base: 0x%lx\n", size, base);
  344. dump_stack();
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. /**
  350. * mtrr_add - Add a memory type region
  351. * @base: Physical base address of region
  352. * @size: Physical size of region
  353. * @type: Type of MTRR desired
  354. * @increment: If this is true do usage counting on the region
  355. *
  356. * Memory type region registers control the caching on newer Intel and
  357. * non Intel processors. This function allows drivers to request an
  358. * MTRR is added. The details and hardware specifics of each processor's
  359. * implementation are hidden from the caller, but nevertheless the
  360. * caller should expect to need to provide a power of two size on an
  361. * equivalent power of two boundary.
  362. *
  363. * If the region cannot be added either because all regions are in use
  364. * or the CPU cannot support it a negative value is returned. On success
  365. * the register number for this entry is returned, but should be treated
  366. * as a cookie only.
  367. *
  368. * On a multiprocessor machine the changes are made to all processors.
  369. * This is required on x86 by the Intel processors.
  370. *
  371. * The available types are
  372. *
  373. * %MTRR_TYPE_UNCACHABLE - No caching
  374. *
  375. * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
  376. *
  377. * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
  378. *
  379. * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
  380. *
  381. * BUGS: Needs a quiet flag for the cases where drivers do not mind
  382. * failures and do not wish system log messages to be sent.
  383. */
  384. int mtrr_add(unsigned long base, unsigned long size, unsigned int type,
  385. bool increment)
  386. {
  387. if (mtrr_check(base, size))
  388. return -EINVAL;
  389. return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
  390. increment);
  391. }
  392. EXPORT_SYMBOL(mtrr_add);
  393. /**
  394. * mtrr_del_page - delete a memory type region
  395. * @reg: Register returned by mtrr_add
  396. * @base: Physical base address
  397. * @size: Size of region
  398. *
  399. * If register is supplied then base and size are ignored. This is
  400. * how drivers should call it.
  401. *
  402. * Releases an MTRR region. If the usage count drops to zero the
  403. * register is freed and the region returns to default state.
  404. * On success the register is returned, on failure a negative error
  405. * code.
  406. */
  407. int mtrr_del_page(int reg, unsigned long base, unsigned long size)
  408. {
  409. int i, max;
  410. mtrr_type ltype;
  411. unsigned long lbase, lsize;
  412. int error = -EINVAL;
  413. if (!mtrr_if)
  414. return -ENXIO;
  415. max = num_var_ranges;
  416. /* No CPU hotplug when we change MTRR entries */
  417. get_online_cpus();
  418. mutex_lock(&mtrr_mutex);
  419. if (reg < 0) {
  420. /* Search for existing MTRR */
  421. for (i = 0; i < max; ++i) {
  422. mtrr_if->get(i, &lbase, &lsize, &ltype);
  423. if (lbase == base && lsize == size) {
  424. reg = i;
  425. break;
  426. }
  427. }
  428. if (reg < 0) {
  429. pr_debug("mtrr: no MTRR for %lx000,%lx000 found\n",
  430. base, size);
  431. goto out;
  432. }
  433. }
  434. if (reg >= max) {
  435. pr_warning("mtrr: register: %d too big\n", reg);
  436. goto out;
  437. }
  438. mtrr_if->get(reg, &lbase, &lsize, &ltype);
  439. if (lsize < 1) {
  440. pr_warning("mtrr: MTRR %d not used\n", reg);
  441. goto out;
  442. }
  443. if (mtrr_usage_table[reg] < 1) {
  444. pr_warning("mtrr: reg: %d has count=0\n", reg);
  445. goto out;
  446. }
  447. if (--mtrr_usage_table[reg] < 1)
  448. set_mtrr(reg, 0, 0, 0);
  449. error = reg;
  450. out:
  451. mutex_unlock(&mtrr_mutex);
  452. put_online_cpus();
  453. return error;
  454. }
  455. /**
  456. * mtrr_del - delete a memory type region
  457. * @reg: Register returned by mtrr_add
  458. * @base: Physical base address
  459. * @size: Size of region
  460. *
  461. * If register is supplied then base and size are ignored. This is
  462. * how drivers should call it.
  463. *
  464. * Releases an MTRR region. If the usage count drops to zero the
  465. * register is freed and the region returns to default state.
  466. * On success the register is returned, on failure a negative error
  467. * code.
  468. */
  469. int mtrr_del(int reg, unsigned long base, unsigned long size)
  470. {
  471. if (mtrr_check(base, size))
  472. return -EINVAL;
  473. return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
  474. }
  475. EXPORT_SYMBOL(mtrr_del);
  476. /*
  477. * HACK ALERT!
  478. * These should be called implicitly, but we can't yet until all the initcall
  479. * stuff is done...
  480. */
  481. static void __init init_ifs(void)
  482. {
  483. #ifndef CONFIG_X86_64
  484. amd_init_mtrr();
  485. cyrix_init_mtrr();
  486. centaur_init_mtrr();
  487. #endif
  488. }
  489. /* The suspend/resume methods are only for CPU without MTRR. CPU using generic
  490. * MTRR driver doesn't require this
  491. */
  492. struct mtrr_value {
  493. mtrr_type ltype;
  494. unsigned long lbase;
  495. unsigned long lsize;
  496. };
  497. static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
  498. static int mtrr_save(void)
  499. {
  500. int i;
  501. for (i = 0; i < num_var_ranges; i++) {
  502. mtrr_if->get(i, &mtrr_value[i].lbase,
  503. &mtrr_value[i].lsize,
  504. &mtrr_value[i].ltype);
  505. }
  506. return 0;
  507. }
  508. static void mtrr_restore(void)
  509. {
  510. int i;
  511. for (i = 0; i < num_var_ranges; i++) {
  512. if (mtrr_value[i].lsize) {
  513. set_mtrr(i, mtrr_value[i].lbase,
  514. mtrr_value[i].lsize,
  515. mtrr_value[i].ltype);
  516. }
  517. }
  518. }
  519. static struct syscore_ops mtrr_syscore_ops = {
  520. .suspend = mtrr_save,
  521. .resume = mtrr_restore,
  522. };
  523. int __initdata changed_by_mtrr_cleanup;
  524. /**
  525. * mtrr_bp_init - initialize mtrrs on the boot CPU
  526. *
  527. * This needs to be called early; before any of the other CPUs are
  528. * initialized (i.e. before smp_init()).
  529. *
  530. */
  531. void __init mtrr_bp_init(void)
  532. {
  533. u32 phys_addr;
  534. init_ifs();
  535. phys_addr = 32;
  536. if (cpu_has_mtrr) {
  537. mtrr_if = &generic_mtrr_ops;
  538. size_or_mask = 0xff000000; /* 36 bits */
  539. size_and_mask = 0x00f00000;
  540. phys_addr = 36;
  541. /*
  542. * This is an AMD specific MSR, but we assume(hope?) that
  543. * Intel will implement it to when they extend the address
  544. * bus of the Xeon.
  545. */
  546. if (cpuid_eax(0x80000000) >= 0x80000008) {
  547. phys_addr = cpuid_eax(0x80000008) & 0xff;
  548. /* CPUID workaround for Intel 0F33/0F34 CPU */
  549. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  550. boot_cpu_data.x86 == 0xF &&
  551. boot_cpu_data.x86_model == 0x3 &&
  552. (boot_cpu_data.x86_mask == 0x3 ||
  553. boot_cpu_data.x86_mask == 0x4))
  554. phys_addr = 36;
  555. size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
  556. size_and_mask = ~size_or_mask & 0xfffff00000ULL;
  557. } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
  558. boot_cpu_data.x86 == 6) {
  559. /*
  560. * VIA C* family have Intel style MTRRs,
  561. * but don't support PAE
  562. */
  563. size_or_mask = 0xfff00000; /* 32 bits */
  564. size_and_mask = 0;
  565. phys_addr = 32;
  566. }
  567. } else {
  568. switch (boot_cpu_data.x86_vendor) {
  569. case X86_VENDOR_AMD:
  570. if (cpu_has_k6_mtrr) {
  571. /* Pre-Athlon (K6) AMD CPU MTRRs */
  572. mtrr_if = mtrr_ops[X86_VENDOR_AMD];
  573. size_or_mask = 0xfff00000; /* 32 bits */
  574. size_and_mask = 0;
  575. }
  576. break;
  577. case X86_VENDOR_CENTAUR:
  578. if (cpu_has_centaur_mcr) {
  579. mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
  580. size_or_mask = 0xfff00000; /* 32 bits */
  581. size_and_mask = 0;
  582. }
  583. break;
  584. case X86_VENDOR_CYRIX:
  585. if (cpu_has_cyrix_arr) {
  586. mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
  587. size_or_mask = 0xfff00000; /* 32 bits */
  588. size_and_mask = 0;
  589. }
  590. break;
  591. default:
  592. break;
  593. }
  594. }
  595. if (mtrr_if) {
  596. set_num_var_ranges();
  597. init_table();
  598. if (use_intel()) {
  599. get_mtrr_state();
  600. if (mtrr_cleanup(phys_addr)) {
  601. changed_by_mtrr_cleanup = 1;
  602. mtrr_if->set_all();
  603. }
  604. }
  605. }
  606. }
  607. void mtrr_ap_init(void)
  608. {
  609. if (!use_intel() || mtrr_aps_delayed_init)
  610. return;
  611. /*
  612. * Ideally we should hold mtrr_mutex here to avoid mtrr entries
  613. * changed, but this routine will be called in cpu boot time,
  614. * holding the lock breaks it.
  615. *
  616. * This routine is called in two cases:
  617. *
  618. * 1. very earily time of software resume, when there absolutely
  619. * isn't mtrr entry changes;
  620. *
  621. * 2. cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug
  622. * lock to prevent mtrr entry changes
  623. */
  624. set_mtrr_from_inactive_cpu(~0U, 0, 0, 0);
  625. }
  626. /**
  627. * Save current fixed-range MTRR state of the BSP
  628. */
  629. void mtrr_save_state(void)
  630. {
  631. smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1);
  632. }
  633. void set_mtrr_aps_delayed_init(void)
  634. {
  635. if (!use_intel())
  636. return;
  637. mtrr_aps_delayed_init = true;
  638. }
  639. /*
  640. * Delayed MTRR initialization for all AP's
  641. */
  642. void mtrr_aps_init(void)
  643. {
  644. if (!use_intel())
  645. return;
  646. /*
  647. * Check if someone has requested the delay of AP MTRR initialization,
  648. * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
  649. * then we are done.
  650. */
  651. if (!mtrr_aps_delayed_init)
  652. return;
  653. set_mtrr(~0U, 0, 0, 0);
  654. mtrr_aps_delayed_init = false;
  655. }
  656. void mtrr_bp_restore(void)
  657. {
  658. if (!use_intel())
  659. return;
  660. mtrr_if->set_all();
  661. }
  662. static int __init mtrr_init_finialize(void)
  663. {
  664. if (!mtrr_if)
  665. return 0;
  666. if (use_intel()) {
  667. if (!changed_by_mtrr_cleanup)
  668. mtrr_state_warn();
  669. return 0;
  670. }
  671. /*
  672. * The CPU has no MTRR and seems to not support SMP. They have
  673. * specific drivers, we use a tricky method to support
  674. * suspend/resume for them.
  675. *
  676. * TBD: is there any system with such CPU which supports
  677. * suspend/resume? If no, we should remove the code.
  678. */
  679. register_syscore_ops(&mtrr_syscore_ops);
  680. return 0;
  681. }
  682. subsys_initcall(mtrr_init_finialize);