i386: Move CPU ID and APIC ID maps from mp_machdep.c to lapic.c
[dragonfly.git] / sys / platform / pc32 / i386 / i686_mem.c
1 /*-
2  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/i386/i686_mem.c,v 1.31 2009/03/17 00:48:11 jkim
27  */
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/memrange.h>
35 #include <sys/sysctl.h>
36 #include <sys/thread2.h>
37
38 #include <machine/cputypes.h>
39 #include <machine/md_var.h>
40 #include <machine/specialreg.h>
41
42 /*
43  * i686 memory range operations
44  *
45  * This code will probably be impenetrable without reference to the
46  * Intel Pentium Pro documentation.
47  */
48
49 static char *mem_owner_bios = "BIOS";
50
51 #define MR686_FIXMTRR   (1<<0)
52
53 #define mrwithin(mr, a)                                                 \
54         (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
55 #define mroverlap(mra, mrb)                                             \
56         (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
57
58 #define mrvalid(base, len)                                              \
59         ((!(base & ((1 << 12) - 1))) && /* base is multiple of 4k */    \
60             ((len) >= (1 << 12)) &&     /* length is >= 4k */           \
61             powerof2((len)) &&          /* ... and power of two */      \
62             !((base) & ((len) - 1)))    /* range is not discontiuous */
63
64 #define mrcopyflags(curr, new)                                          \
65         (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
66
67 static int mtrrs_disabled;
68 TUNABLE_INT("machdep.disable_mtrrs", &mtrrs_disabled);
69 SYSCTL_INT(_machdep, OID_AUTO, disable_mtrrs, CTLFLAG_RD,
70     &mtrrs_disabled, 0, "Disable i686 MTRRs.");
71
72 static void     i686_mrinit(struct mem_range_softc *sc);
73 static int      i686_mrset(struct mem_range_softc *sc,
74                     struct mem_range_desc *mrd, int *arg);
75 static void     i686_mrAPinit(struct mem_range_softc *sc);
76 static void     i686_mrreinit(struct mem_range_softc *sc);
77
78 static struct mem_range_ops i686_mrops = {
79         i686_mrinit,
80         i686_mrset,
81         i686_mrAPinit,
82         i686_mrreinit
83 };
84
85 /* XXX for AP startup hook */
86 static u_int64_t mtrrcap, mtrrdef;
87
88 /* The bitmask for the PhysBase and PhysMask fields of the variable MTRRs. */
89 static u_int64_t mtrr_physmask;
90
91 static struct mem_range_desc *mem_range_match(struct mem_range_softc *sc,
92                     struct mem_range_desc *mrd);
93 static void     i686_mrfetch(struct mem_range_softc *sc);
94 static int      i686_mtrrtype(int flags);
95 static int      i686_mrt2mtrr(int flags, int oldval);
96 static int      i686_mtrrconflict(int flag1, int flag2);
97 static void     i686_mrstore(struct mem_range_softc *sc);
98 static void     i686_mrstoreone(void *arg);
99 #ifdef SMP
100 static void     i686_mrstoreone_cpusync(void *arg);
101 static void     i686_mrAPinit_cpusync(void *arg);
102 #endif
103 static struct mem_range_desc *i686_mtrrfixsearch(struct mem_range_softc *sc,
104                     u_int64_t addr);
105 static int      i686_mrsetlow(struct mem_range_softc *sc,
106                     struct mem_range_desc *mrd, int *arg);
107 static int      i686_mrsetvariable(struct mem_range_softc *sc,
108                     struct mem_range_desc *mrd, int *arg);
109
110 /* i686 MTRR type to memory range type conversion */
111 static int i686_mtrrtomrt[] = {
112         MDF_UNCACHEABLE,
113         MDF_WRITECOMBINE,
114         MDF_UNKNOWN,
115         MDF_UNKNOWN,
116         MDF_WRITETHROUGH,
117         MDF_WRITEPROTECT,
118         MDF_WRITEBACK
119 };
120
121 #define MTRRTOMRTLEN NELEM(i686_mtrrtomrt)
122
123 static int
124 i686_mtrr2mrt(int val)
125 {
126
127         if (val < 0 || val >= MTRRTOMRTLEN)
128                 return (MDF_UNKNOWN);
129         return (i686_mtrrtomrt[val]);
130 }
131
132 /*
133  * i686 MTRR conflicts. Writeback and uncachable may overlap.
134  */
135 static int
136 i686_mtrrconflict(int flag1, int flag2)
137 {
138
139         flag1 &= MDF_ATTRMASK;
140         flag2 &= MDF_ATTRMASK;
141         if (flag1 == flag2 ||
142             (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
143             (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
144                 return (0);
145         return (1);
146 }
147
148 /*
149  * Look for an exactly-matching range.
150  */
151 static struct mem_range_desc *
152 mem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
153 {
154         struct mem_range_desc *cand;
155         int i;
156
157         for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
158                 if ((cand->mr_base == mrd->mr_base) &&
159                     (cand->mr_len == mrd->mr_len))
160                         return (cand);
161         return (NULL);
162 }
163
164 /*
165  * Fetch the current mtrr settings from the current CPU (assumed to
166  * all be in sync in the SMP case).  Note that if we are here, we
167  * assume that MTRRs are enabled, and we may or may not have fixed
168  * MTRRs.
169  */
170 static void
171 i686_mrfetch(struct mem_range_softc *sc)
172 {
173         struct mem_range_desc *mrd;
174         u_int64_t msrv;
175         int i, j, msr;
176
177         mrd = sc->mr_desc;
178
179         /* Get fixed-range MTRRs. */
180         if (sc->mr_cap & MR686_FIXMTRR) {
181                 msr = MSR_MTRR64kBase;
182                 for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
183                         msrv = rdmsr(msr);
184                         for (j = 0; j < 8; j++, mrd++) {
185                                 mrd->mr_flags =
186                                     (mrd->mr_flags & ~MDF_ATTRMASK) |
187                                     i686_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
188                                 if (mrd->mr_owner[0] == 0)
189                                         strcpy(mrd->mr_owner, mem_owner_bios);
190                                 msrv = msrv >> 8;
191                         }
192                 }
193                 msr = MSR_MTRR16kBase;
194                 for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
195                         msrv = rdmsr(msr);
196                         for (j = 0; j < 8; j++, mrd++) {
197                                 mrd->mr_flags =
198                                     (mrd->mr_flags & ~MDF_ATTRMASK) |
199                                     i686_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
200                                 if (mrd->mr_owner[0] == 0)
201                                         strcpy(mrd->mr_owner, mem_owner_bios);
202                                 msrv = msrv >> 8;
203                         }
204                 }
205                 msr = MSR_MTRR4kBase;
206                 for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
207                         msrv = rdmsr(msr);
208                         for (j = 0; j < 8; j++, mrd++) {
209                                 mrd->mr_flags =
210                                     (mrd->mr_flags & ~MDF_ATTRMASK) |
211                                     i686_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
212                                 if (mrd->mr_owner[0] == 0)
213                                         strcpy(mrd->mr_owner, mem_owner_bios);
214                                 msrv = msrv >> 8;
215                         }
216                 }
217         }
218
219         /* Get remainder which must be variable MTRRs. */
220         msr = MSR_MTRRVarBase;
221         for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
222                 msrv = rdmsr(msr);
223                 mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
224                     i686_mtrr2mrt(msrv & MTRR_PHYSBASE_TYPE);
225                 mrd->mr_base = msrv & mtrr_physmask;
226                 msrv = rdmsr(msr + 1);
227                 mrd->mr_flags = (msrv & MTRR_PHYSMASK_VALID) ?
228                     (mrd->mr_flags | MDF_ACTIVE) :
229                     (mrd->mr_flags & ~MDF_ACTIVE);
230
231                 /* Compute the range from the mask. Ick. */
232                 mrd->mr_len = (~(msrv & mtrr_physmask) &
233                     (mtrr_physmask | 0xfffLL)) + 1;
234                 if (!mrvalid(mrd->mr_base, mrd->mr_len))
235                         mrd->mr_flags |= MDF_BOGUS;
236
237                 /* If unclaimed and active, must be the BIOS. */
238                 if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
239                         strcpy(mrd->mr_owner, mem_owner_bios);
240         }
241 }
242
243 /*
244  * Return the MTRR memory type matching a region's flags
245  */
246 static int
247 i686_mtrrtype(int flags)
248 {
249         int i;
250
251         flags &= MDF_ATTRMASK;
252
253         for (i = 0; i < MTRRTOMRTLEN; i++) {
254                 if (i686_mtrrtomrt[i] == MDF_UNKNOWN)
255                         continue;
256                 if (flags == i686_mtrrtomrt[i])
257                         return (i);
258         }
259         return (-1);
260 }
261
262 static int
263 i686_mrt2mtrr(int flags, int oldval)
264 {
265         int val;
266
267         if ((val = i686_mtrrtype(flags)) == -1)
268                 return (oldval & 0xff);
269         return (val & 0xff);
270 }
271
272 /*
273  * Update running CPU(s) MTRRs to match the ranges in the descriptor
274  * list.
275  *
276  * XXX Must be called with interrupts enabled.
277  */
278 static void
279 i686_mrstore(struct mem_range_softc *sc)
280 {
281 #ifdef SMP
282         /*
283          * We should use ipi_all_but_self() to call other CPUs into a
284          * locking gate, then call a target function to do this work.
285          * The "proper" solution involves a generalised locking gate
286          * implementation, not ready yet.
287          */
288         lwkt_cpusync_simple(-1, i686_mrstoreone_cpusync, sc);
289 #else
290         mpintr_lock();
291         i686_mrstoreone(sc);
292         mpintr_unlock();
293 #endif
294 }
295
296 #ifdef SMP
297
298 static void
299 i686_mrstoreone_cpusync(void *arg)
300 {
301         i686_mrstoreone(arg);
302 }
303
304 #endif
305
306 /*
307  * Update the current CPU's MTRRs with those represented in the
308  * descriptor list.  Note that we do this wholesale rather than just
309  * stuffing one entry; this is simpler (but slower, of course).
310  */
311 static void
312 i686_mrstoreone(void *arg)
313 {
314         struct mem_range_softc *sc = arg;
315         struct mem_range_desc *mrd;
316         u_int64_t omsrv, msrv;
317         int i, j, msr;
318         u_int cr4save;
319
320         mrd = sc->mr_desc;
321
322         /* Disable PGE. */
323         cr4save = rcr4();
324         if (cr4save & CR4_PGE)
325                 load_cr4(cr4save & ~CR4_PGE);
326
327         /* Disable caches (CD = 1, NW = 0). */
328         load_cr0((rcr0() & ~CR0_NW) | CR0_CD);
329
330         /* Flushes caches and TLBs. */
331         wbinvd();
332
333         /* Disable MTRRs (E = 0). */
334         wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~MTRR_DEF_ENABLE);
335
336         /* Set fixed-range MTRRs. */
337         if (sc->mr_cap & MR686_FIXMTRR) {
338                 msr = MSR_MTRR64kBase;
339                 for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
340                         msrv = 0;
341                         omsrv = rdmsr(msr);
342                         for (j = 7; j >= 0; j--) {
343                                 msrv = msrv << 8;
344                                 msrv |= i686_mrt2mtrr((mrd + j)->mr_flags,
345                                     omsrv >> (j * 8));
346                         }
347                         wrmsr(msr, msrv);
348                         mrd += 8;
349                 }
350                 msr = MSR_MTRR16kBase;
351                 for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
352                         msrv = 0;
353                         omsrv = rdmsr(msr);
354                         for (j = 7; j >= 0; j--) {
355                                 msrv = msrv << 8;
356                                 msrv |= i686_mrt2mtrr((mrd + j)->mr_flags,
357                                     omsrv >> (j * 8));
358                         }
359                         wrmsr(msr, msrv);
360                         mrd += 8;
361                 }
362                 msr = MSR_MTRR4kBase;
363                 for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
364                         msrv = 0;
365                         omsrv = rdmsr(msr);
366                         for (j = 7; j >= 0; j--) {
367                                 msrv = msrv << 8;
368                                 msrv |= i686_mrt2mtrr((mrd + j)->mr_flags,
369                                     omsrv >> (j * 8));
370                         }
371                         wrmsr(msr, msrv);
372                         mrd += 8;
373                 }
374         }
375
376         /* Set remainder which must be variable MTRRs. */
377         msr = MSR_MTRRVarBase;
378         for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
379                 /* base/type register */
380                 omsrv = rdmsr(msr);
381                 if (mrd->mr_flags & MDF_ACTIVE) {
382                         msrv = mrd->mr_base & mtrr_physmask;
383                         msrv |= i686_mrt2mtrr(mrd->mr_flags, omsrv);
384                 } else {
385                         msrv = 0;
386                 }
387                 wrmsr(msr, msrv);
388
389                 /* mask/active register */
390                 if (mrd->mr_flags & MDF_ACTIVE) {
391                         msrv = MTRR_PHYSMASK_VALID |
392                             (~(mrd->mr_len - 1) & mtrr_physmask);
393                 } else {
394                         msrv = 0;
395                 }
396                 wrmsr(msr + 1, msrv);
397         }
398
399         /* Flush caches, TLBs. */
400         wbinvd();
401
402         /* Enable MTRRs. */
403         wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | MTRR_DEF_ENABLE);
404
405         /* Enable caches (CD = 0, NW = 0). */
406         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));
407
408         /* Restore PGE. */
409         load_cr4(cr4save);
410 }
411
412 /*
413  * Hunt for the fixed MTRR referencing (addr)
414  */
415 static struct mem_range_desc *
416 i686_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
417 {
418         struct mem_range_desc *mrd;
419         int i;
420
421         for (i = 0, mrd = sc->mr_desc; i < (MTRR_N64K + MTRR_N16K + MTRR_N4K);
422              i++, mrd++)
423                 if ((addr >= mrd->mr_base) &&
424                     (addr < (mrd->mr_base + mrd->mr_len)))
425                         return (mrd);
426         return (NULL);
427 }
428
429 /*
430  * Try to satisfy the given range request by manipulating the fixed
431  * MTRRs that cover low memory.
432  *
433  * Note that we try to be generous here; we'll bloat the range out to
434  * the next higher/lower boundary to avoid the consumer having to know
435  * too much about the mechanisms here.
436  *
437  * XXX note that this will have to be updated when we start supporting
438  * "busy" ranges.
439  */
440 static int
441 i686_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
442 {
443         struct mem_range_desc *first_md, *last_md, *curr_md;
444
445         /* Range check. */
446         if (((first_md = i686_mtrrfixsearch(sc, mrd->mr_base)) == NULL) ||
447             ((last_md = i686_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1)) == NULL))
448                 return (EINVAL);
449
450         /* Check that we aren't doing something risky. */
451         if (!(mrd->mr_flags & MDF_FORCE))
452                 for (curr_md = first_md; curr_md <= last_md; curr_md++) {
453                         if ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN)
454                                 return (EACCES);
455                 }
456
457         /* Set flags, clear set-by-firmware flag. */
458         for (curr_md = first_md; curr_md <= last_md; curr_md++) {
459                 curr_md->mr_flags = mrcopyflags(curr_md->mr_flags &
460                     ~MDF_FIRMWARE, mrd->mr_flags);
461                 bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
462         }
463
464         return (0);
465 }
466
467 /*
468  * Modify/add a variable MTRR to satisfy the request.
469  *
470  * XXX needs to be updated to properly support "busy" ranges.
471  */
472 static int
473 i686_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd,
474     int *arg)
475 {
476         struct mem_range_desc *curr_md, *free_md;
477         int i;
478
479         /*
480          * Scan the currently active variable descriptors, look for
481          * one we exactly match (straight takeover) and for possible
482          * accidental overlaps.
483          *
484          * Keep track of the first empty variable descriptor in case
485          * we can't perform a takeover.
486          */
487         i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
488         curr_md = sc->mr_desc + i;
489         free_md = NULL;
490         for (; i < sc->mr_ndesc; i++, curr_md++) {
491                 if (curr_md->mr_flags & MDF_ACTIVE) {
492                         /* Exact match? */
493                         if ((curr_md->mr_base == mrd->mr_base) &&
494                             (curr_md->mr_len == mrd->mr_len)) {
495
496                                 /* Whoops, owned by someone. */
497                                 if (curr_md->mr_flags & MDF_BUSY)
498                                         return (EBUSY);
499
500                                 /* Check that we aren't doing something risky */
501                                 if (!(mrd->mr_flags & MDF_FORCE) &&
502                                     ((curr_md->mr_flags & MDF_ATTRMASK) ==
503                                     MDF_UNKNOWN))
504                                         return (EACCES);
505
506                                 /* Ok, just hijack this entry. */
507                                 free_md = curr_md;
508                                 break;
509                         }
510
511                         /* Non-exact overlap? */
512                         if (mroverlap(curr_md, mrd)) {
513                                 /* Between conflicting region types? */
514                                 if (i686_mtrrconflict(curr_md->mr_flags,
515                                     mrd->mr_flags))
516                                         return (EINVAL);
517                         }
518                 } else if (free_md == NULL) {
519                         free_md = curr_md;
520                 }
521         }
522
523         /* Got somewhere to put it? */
524         if (free_md == NULL)
525                 return (ENOSPC);
526
527         /* Set up new descriptor. */
528         free_md->mr_base = mrd->mr_base;
529         free_md->mr_len = mrd->mr_len;
530         free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
531         bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
532         return (0);
533 }
534
535 /*
536  * Handle requests to set memory range attributes by manipulating MTRRs.
537  */
538 static int
539 i686_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
540 {
541         struct mem_range_desc *targ;
542         int error = 0;
543
544         switch(*arg) {
545         case MEMRANGE_SET_UPDATE:
546                 /*
547                  * Make sure that what's being asked for is even
548                  * possible at all.
549                  */
550                 if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
551                     i686_mtrrtype(mrd->mr_flags) == -1)
552                         return (EINVAL);
553
554 #define FIXTOP  ((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
555
556                 /* Are the "low memory" conditions applicable? */
557                 if ((sc->mr_cap & MR686_FIXMTRR) &&
558                     ((mrd->mr_base + mrd->mr_len) <= FIXTOP)) {
559                         if ((error = i686_mrsetlow(sc, mrd, arg)) != 0)
560                                 return (error);
561                 } else {
562                         /* It's time to play with variable MTRRs. */
563                         if ((error = i686_mrsetvariable(sc, mrd, arg)) != 0)
564                                 return (error);
565                 }
566                 break;
567
568         case MEMRANGE_SET_REMOVE:
569                 if ((targ = mem_range_match(sc, mrd)) == NULL)
570                         return (ENOENT);
571                 if (targ->mr_flags & MDF_FIXACTIVE)
572                         return (EPERM);
573                 if (targ->mr_flags & MDF_BUSY)
574                         return (EBUSY);
575                 targ->mr_flags &= ~MDF_ACTIVE;
576                 targ->mr_owner[0] = 0;
577                 break;
578
579         default:
580                 return (EOPNOTSUPP);
581         }
582
583         /* Update the hardware. */
584         i686_mrstore(sc);
585
586         /* Refetch to see where we're at. */
587         i686_mrfetch(sc);
588         return (0);
589 }
590
591 /*
592  * Work out how many ranges we support, initialise storage for them,
593  * and fetch the initial settings.
594  */
595 static void
596 i686_mrinit(struct mem_range_softc *sc)
597 {
598         struct mem_range_desc *mrd;
599         u_int regs[4];
600         int i, nmdesc = 0, pabits;
601
602         mtrrcap = rdmsr(MSR_MTRRcap);
603         mtrrdef = rdmsr(MSR_MTRRdefType);
604
605         /* For now, bail out if MTRRs are not enabled. */
606         if (!(mtrrdef & MTRR_DEF_ENABLE)) {
607                 if (bootverbose)
608                         kprintf("CPU supports MTRRs but not enabled\n");
609                 return;
610         }
611         nmdesc = mtrrcap & MTRR_CAP_VCNT;
612         if (bootverbose)
613                 kprintf("Pentium Pro MTRR support enabled\n");
614
615         /*
616          * Determine the size of the PhysMask and PhysBase fields in
617          * the variable range MTRRs.  If the extended CPUID 0x80000008
618          * is present, use that to figure out how many physical
619          * address bits the CPU supports.  Otherwise, default to 36
620          * address bits.
621          */
622         if (cpu_exthigh >= 0x80000008) {
623                 do_cpuid(0x80000008, regs);
624                 pabits = regs[0] & 0xff;
625         } else
626                 pabits = 36;
627         mtrr_physmask = ((1ULL << pabits) - 1) & ~0xfffULL;
628
629         /* If fixed MTRRs supported and enabled. */
630         if ((mtrrcap & MTRR_CAP_FIXED) && (mtrrdef & MTRR_DEF_FIXED_ENABLE)) {
631                 sc->mr_cap = MR686_FIXMTRR;
632                 nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
633         }
634
635         sc->mr_desc = kmalloc(nmdesc * sizeof(struct mem_range_desc), M_MEMDESC,
636             M_WAITOK | M_ZERO);
637         sc->mr_ndesc = nmdesc;
638
639         mrd = sc->mr_desc;
640
641         /* Populate the fixed MTRR entries' base/length. */
642         if (sc->mr_cap & MR686_FIXMTRR) {
643                 for (i = 0; i < MTRR_N64K; i++, mrd++) {
644                         mrd->mr_base = i * 0x10000;
645                         mrd->mr_len = 0x10000;
646                         mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
647                             MDF_FIXACTIVE;
648                 }
649                 for (i = 0; i < MTRR_N16K; i++, mrd++) {
650                         mrd->mr_base = i * 0x4000 + 0x80000;
651                         mrd->mr_len = 0x4000;
652                         mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
653                             MDF_FIXACTIVE;
654                 }
655                 for (i = 0; i < MTRR_N4K; i++, mrd++) {
656                         mrd->mr_base = i * 0x1000 + 0xc0000;
657                         mrd->mr_len = 0x1000;
658                         mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
659                             MDF_FIXACTIVE;
660                 }
661         }
662
663         /*
664          * Get current settings, anything set now is considered to
665          * have been set by the firmware. (XXX has something already
666          * played here?)
667          */
668         i686_mrfetch(sc);
669         mrd = sc->mr_desc;
670         for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
671                 if (mrd->mr_flags & MDF_ACTIVE)
672                         mrd->mr_flags |= MDF_FIRMWARE;
673         }
674 }
675
676 #ifdef SMP
677
678 static void
679 i686_mrAPinit_cpusync(void *arg)
680 {
681         i686_mrAPinit(arg);
682 }
683
684 #endif
685
686 /*
687  * Initialise MTRRs on an AP after the BSP has run the init code.
688  */
689 static void
690 i686_mrAPinit(struct mem_range_softc *sc)
691 {
692
693         i686_mrstoreone(sc);
694         wrmsr(MSR_MTRRdefType, mtrrdef);
695 }
696
697 /*
698  * Re-initialise running CPU(s) MTRRs to match the ranges in the descriptor
699  * list.
700  *
701  * XXX Must be called with interrupts enabled.
702  */
703 static void
704 i686_mrreinit(struct mem_range_softc *sc)
705 {
706 #ifdef SMP
707         /*
708          * We should use ipi_all_but_self() to call other CPUs into a
709          * locking gate, then call a target function to do this work.
710          * The "proper" solution involves a generalised locking gate
711          * implementation, not ready yet.
712          */
713         lwkt_cpusync_simple(-1, i686_mrAPinit_cpusync, sc);
714 #else
715         mpintr_lock();
716         i686_mrAPinit(sc);
717         mpintr_unlock();
718 #endif
719 }
720
721 static void
722 i686_mem_drvinit(void *unused)
723 {
724
725         if (mtrrs_disabled)
726                 return;
727         if (!(cpu_feature & CPUID_MTRR))
728                 return;
729         if ((cpu_id & 0xf00) != 0x600 && (cpu_id & 0xf00) != 0xf00)
730                 return;
731         switch (cpu_vendor_id) {
732         case CPU_VENDOR_INTEL:
733         case CPU_VENDOR_AMD:
734                 break;
735         case CPU_VENDOR_CENTAUR:
736                 if (cpu_exthigh >= 0x80000008)
737                         break;
738                 /* FALLTHROUGH */
739         default:
740                 return;
741         }
742         mem_range_softc.mr_op = &i686_mrops;
743 }
744 SYSINIT(i686memdev, SI_SUB_DRIVERS, SI_ORDER_FIRST, i686_mem_drvinit, NULL);