2 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Konstantin Belousov
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/memrange.h>
40 #include <sys/sysctl.h>
43 #include <vm/vm_param.h>
46 #include <machine/cputypes.h>
47 #include <machine/md_var.h>
48 #include <machine/specialreg.h>
51 * Pentium Pro+ memory range operations
53 * This code will probably be impenetrable without reference to the
54 * Intel Pentium Pro documentation or x86-64 programmers manual vol 2.
57 static char *mem_owner_bios = "BIOS";
59 #define MR686_FIXMTRR (1<<0)
61 #define mrwithin(mr, a) \
62 (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
63 #define mroverlap(mra, mrb) \
64 (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
66 #define mrvalid(base, len) \
67 ((!(base & ((1 << 12) - 1))) && /* base is multiple of 4k */ \
68 ((len) >= (1 << 12)) && /* length is >= 4k */ \
69 powerof2((len)) && /* ... and power of two */ \
70 !((base) & ((len) - 1))) /* range is not discontiuous */
72 #define mrcopyflags(curr, new) \
73 (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
75 static int mtrrs_disabled;
76 SYSCTL_INT(_machdep, OID_AUTO, disable_mtrrs, CTLFLAG_RDTUN,
80 static void x86_mrinit(struct mem_range_softc *sc);
81 static int x86_mrset(struct mem_range_softc *sc,
82 struct mem_range_desc *mrd, int *arg);
83 static void x86_mrAPinit(struct mem_range_softc *sc);
84 static void x86_mrreinit(struct mem_range_softc *sc);
86 static struct mem_range_ops x86_mrops = {
93 /* XXX for AP startup hook */
94 static u_int64_t mtrrcap, mtrrdef;
96 /* The bitmask for the PhysBase and PhysMask fields of the variable MTRRs. */
97 static u_int64_t mtrr_physmask;
99 static struct mem_range_desc *mem_range_match(struct mem_range_softc *sc,
100 struct mem_range_desc *mrd);
101 static void x86_mrfetch(struct mem_range_softc *sc);
102 static int x86_mtrrtype(int flags);
103 static int x86_mrt2mtrr(int flags, int oldval);
104 static int x86_mtrrconflict(int flag1, int flag2);
105 static void x86_mrstore(struct mem_range_softc *sc);
106 static void x86_mrstoreone(void *arg);
107 static struct mem_range_desc *x86_mtrrfixsearch(struct mem_range_softc *sc,
109 static int x86_mrsetlow(struct mem_range_softc *sc,
110 struct mem_range_desc *mrd, int *arg);
111 static int x86_mrsetvariable(struct mem_range_softc *sc,
112 struct mem_range_desc *mrd, int *arg);
114 /* ia32 MTRR type to memory range type conversion */
115 static int x86_mtrrtomrt[] = {
125 #define MTRRTOMRTLEN nitems(x86_mtrrtomrt)
128 x86_mtrr2mrt(int val)
131 if (val < 0 || val >= MTRRTOMRTLEN)
132 return (MDF_UNKNOWN);
133 return (x86_mtrrtomrt[val]);
137 * x86 MTRR conflicts. Writeback and uncachable may overlap.
140 x86_mtrrconflict(int flag1, int flag2)
143 flag1 &= MDF_ATTRMASK;
144 flag2 &= MDF_ATTRMASK;
145 if ((flag1 & MDF_UNKNOWN) || (flag2 & MDF_UNKNOWN))
147 if (flag1 == flag2 ||
148 (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
149 (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
155 * Look for an exactly-matching range.
157 static struct mem_range_desc *
158 mem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
160 struct mem_range_desc *cand;
163 for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
164 if ((cand->mr_base == mrd->mr_base) &&
165 (cand->mr_len == mrd->mr_len))
171 * Ensure that the direct map region does not contain any mappings
172 * that span MTRRs of different types. However, the fixed MTRRs can
173 * be ignored, because a large page mapping the first 1 MB of physical
174 * memory is a special case that the processor handles. Invalidate
175 * any old TLB entries that might hold inconsistent memory type
179 x86_mr_split_dmap(struct mem_range_softc *sc __unused)
182 struct mem_range_desc *mrd;
185 i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
186 mrd = sc->mr_desc + i;
187 for (; i < sc->mr_ndesc; i++, mrd++) {
188 if ((mrd->mr_flags & (MDF_ACTIVE | MDF_BOGUS)) == MDF_ACTIVE)
189 pmap_demote_DMAP(mrd->mr_base, mrd->mr_len, TRUE);
195 * Fetch the current mtrr settings from the current CPU (assumed to
196 * all be in sync in the SMP case). Note that if we are here, we
197 * assume that MTRRs are enabled, and we may or may not have fixed
201 x86_mrfetch(struct mem_range_softc *sc)
203 struct mem_range_desc *mrd;
209 /* Get fixed-range MTRRs. */
210 if (sc->mr_cap & MR686_FIXMTRR) {
211 msr = MSR_MTRR64kBase;
212 for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
214 for (j = 0; j < 8; j++, mrd++) {
216 (mrd->mr_flags & ~MDF_ATTRMASK) |
217 x86_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
218 if (mrd->mr_owner[0] == 0)
219 strcpy(mrd->mr_owner, mem_owner_bios);
223 msr = MSR_MTRR16kBase;
224 for (i = 0; i < MTRR_N16K / 8; i++, msr++) {
226 for (j = 0; j < 8; j++, mrd++) {
228 (mrd->mr_flags & ~MDF_ATTRMASK) |
229 x86_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
230 if (mrd->mr_owner[0] == 0)
231 strcpy(mrd->mr_owner, mem_owner_bios);
235 msr = MSR_MTRR4kBase;
236 for (i = 0; i < MTRR_N4K / 8; i++, msr++) {
238 for (j = 0; j < 8; j++, mrd++) {
240 (mrd->mr_flags & ~MDF_ATTRMASK) |
241 x86_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
242 if (mrd->mr_owner[0] == 0)
243 strcpy(mrd->mr_owner, mem_owner_bios);
249 /* Get remainder which must be variable MTRRs. */
250 msr = MSR_MTRRVarBase;
251 for (; mrd - sc->mr_desc < sc->mr_ndesc; msr += 2, mrd++) {
253 mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
254 x86_mtrr2mrt(msrv & MTRR_PHYSBASE_TYPE);
255 mrd->mr_base = msrv & mtrr_physmask;
256 msrv = rdmsr(msr + 1);
257 mrd->mr_flags = (msrv & MTRR_PHYSMASK_VALID) ?
258 (mrd->mr_flags | MDF_ACTIVE) :
259 (mrd->mr_flags & ~MDF_ACTIVE);
261 /* Compute the range from the mask. Ick. */
262 mrd->mr_len = (~(msrv & mtrr_physmask) &
263 (mtrr_physmask | 0xfff)) + 1;
264 if (!mrvalid(mrd->mr_base, mrd->mr_len))
265 mrd->mr_flags |= MDF_BOGUS;
267 /* If unclaimed and active, must be the BIOS. */
268 if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
269 strcpy(mrd->mr_owner, mem_owner_bios);
274 * Return the MTRR memory type matching a region's flags
277 x86_mtrrtype(int flags)
281 flags &= MDF_ATTRMASK;
283 for (i = 0; i < MTRRTOMRTLEN; i++) {
284 if (x86_mtrrtomrt[i] == MDF_UNKNOWN)
286 if (flags == x86_mtrrtomrt[i])
293 x86_mrt2mtrr(int flags, int oldval)
297 if ((val = x86_mtrrtype(flags)) == -1)
298 return (oldval & 0xff);
303 * Update running CPU(s) MTRRs to match the ranges in the descriptor
306 * Must be called with interrupts enabled.
309 x86_mrstore(struct mem_range_softc *sc)
312 smp_rendezvous(NULL, x86_mrstoreone, NULL, sc);
316 * Update the current CPU's MTRRs with those represented in the
317 * descriptor list. Note that we do this wholesale rather than just
318 * stuffing one entry; this is simpler (but slower, of course).
321 x86_mrstoreone(void *arg)
323 struct mem_range_softc *sc = arg;
324 struct mem_range_desc *mrd;
325 u_int64_t omsrv, msrv;
335 load_cr4(cr4 & ~CR4_PGE);
337 /* Disable caches (CD = 1, NW = 0). */
339 load_cr0((cr0 & ~CR0_NW) | CR0_CD);
341 /* Flushes caches and TLBs. */
345 /* Disable MTRRs (E = 0). */
346 wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~MTRR_DEF_ENABLE);
348 /* Set fixed-range MTRRs. */
349 if (sc->mr_cap & MR686_FIXMTRR) {
350 msr = MSR_MTRR64kBase;
351 for (i = 0; i < MTRR_N64K / 8; i++, msr++) {
354 for (j = 7; j >= 0; j--) {
356 msrv |= x86_mrt2mtrr((mrd + j)->mr_flags,
362 msr = MSR_MTRR16kBase;
363 for (i = 0; i < MTRR_N16K / 8; i++, msr++) {
366 for (j = 7; j >= 0; j--) {
368 msrv |= x86_mrt2mtrr((mrd + j)->mr_flags,
374 msr = MSR_MTRR4kBase;
375 for (i = 0; i < MTRR_N4K / 8; i++, msr++) {
378 for (j = 7; j >= 0; j--) {
380 msrv |= x86_mrt2mtrr((mrd + j)->mr_flags,
388 /* Set remainder which must be variable MTRRs. */
389 msr = MSR_MTRRVarBase;
390 for (; mrd - sc->mr_desc < sc->mr_ndesc; msr += 2, mrd++) {
391 /* base/type register */
393 if (mrd->mr_flags & MDF_ACTIVE) {
394 msrv = mrd->mr_base & mtrr_physmask;
395 msrv |= x86_mrt2mtrr(mrd->mr_flags, omsrv);
401 /* mask/active register */
402 if (mrd->mr_flags & MDF_ACTIVE) {
403 msrv = MTRR_PHYSMASK_VALID |
404 rounddown2(mtrr_physmask, mrd->mr_len);
408 wrmsr(msr + 1, msrv);
411 /* Flush caches and TLBs. */
416 wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | MTRR_DEF_ENABLE);
418 /* Restore caches and PGE. */
426 * Hunt for the fixed MTRR referencing (addr)
428 static struct mem_range_desc *
429 x86_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
431 struct mem_range_desc *mrd;
434 for (i = 0, mrd = sc->mr_desc; i < MTRR_N64K + MTRR_N16K + MTRR_N4K;
436 if (addr >= mrd->mr_base &&
437 addr < mrd->mr_base + mrd->mr_len)
443 * Try to satisfy the given range request by manipulating the fixed
444 * MTRRs that cover low memory.
446 * Note that we try to be generous here; we'll bloat the range out to
447 * the next higher/lower boundary to avoid the consumer having to know
448 * too much about the mechanisms here.
450 * XXX note that this will have to be updated when we start supporting
454 x86_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
456 struct mem_range_desc *first_md, *last_md, *curr_md;
459 if ((first_md = x86_mtrrfixsearch(sc, mrd->mr_base)) == NULL ||
460 (last_md = x86_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1))
464 /* Check that we aren't doing something risky. */
465 if ((mrd->mr_flags & MDF_FORCE) == 0) {
466 for (curr_md = first_md; curr_md <= last_md; curr_md++) {
467 if ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN)
472 /* Set flags, clear set-by-firmware flag. */
473 for (curr_md = first_md; curr_md <= last_md; curr_md++) {
474 curr_md->mr_flags = mrcopyflags(curr_md->mr_flags &
475 ~MDF_FIRMWARE, mrd->mr_flags);
476 bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
483 * Modify/add a variable MTRR to satisfy the request.
485 * XXX needs to be updated to properly support "busy" ranges.
488 x86_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd,
491 struct mem_range_desc *curr_md, *free_md;
495 * Scan the currently active variable descriptors, look for
496 * one we exactly match (straight takeover) and for possible
497 * accidental overlaps.
499 * Keep track of the first empty variable descriptor in case
500 * we can't perform a takeover.
502 i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
503 curr_md = sc->mr_desc + i;
505 for (; i < sc->mr_ndesc; i++, curr_md++) {
506 if (curr_md->mr_flags & MDF_ACTIVE) {
508 if (curr_md->mr_base == mrd->mr_base &&
509 curr_md->mr_len == mrd->mr_len) {
510 /* Whoops, owned by someone. */
511 if (curr_md->mr_flags & MDF_BUSY)
514 /* Check that we aren't doing something risky */
515 if (!(mrd->mr_flags & MDF_FORCE) &&
516 (curr_md->mr_flags & MDF_ATTRMASK) ==
520 /* Ok, just hijack this entry. */
525 /* Non-exact overlap? */
526 if (mroverlap(curr_md, mrd)) {
527 /* Between conflicting region types? */
528 if (x86_mtrrconflict(curr_md->mr_flags,
532 } else if (free_md == NULL) {
537 /* Got somewhere to put it? */
541 /* Set up new descriptor. */
542 free_md->mr_base = mrd->mr_base;
543 free_md->mr_len = mrd->mr_len;
544 free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
545 bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
550 * Handle requests to set memory range attributes by manipulating MTRRs.
553 x86_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
555 struct mem_range_desc *targ;
559 case MEMRANGE_SET_UPDATE:
561 * Make sure that what's being asked for is even
564 if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
565 x86_mtrrtype(mrd->mr_flags) == -1)
569 ((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
571 /* Are the "low memory" conditions applicable? */
572 if ((sc->mr_cap & MR686_FIXMTRR) != 0 &&
573 mrd->mr_base + mrd->mr_len <= FIXTOP) {
574 if ((error = x86_mrsetlow(sc, mrd, arg)) != 0)
577 /* It's time to play with variable MTRRs. */
578 if ((error = x86_mrsetvariable(sc, mrd, arg)) != 0)
583 case MEMRANGE_SET_REMOVE:
584 if ((targ = mem_range_match(sc, mrd)) == NULL)
586 if (targ->mr_flags & MDF_FIXACTIVE)
588 if (targ->mr_flags & MDF_BUSY)
590 targ->mr_flags &= ~MDF_ACTIVE;
591 targ->mr_owner[0] = 0;
598 x86_mr_split_dmap(sc);
600 /* Update the hardware. */
603 /* Refetch to see where we're at. */
609 * Work out how many ranges we support, initialise storage for them,
610 * and fetch the initial settings.
613 x86_mrinit(struct mem_range_softc *sc)
615 struct mem_range_desc *mrd;
618 if (sc->mr_desc != NULL)
619 /* Already initialized. */
623 mtrrcap = rdmsr(MSR_MTRRcap);
624 mtrrdef = rdmsr(MSR_MTRRdefType);
626 /* For now, bail out if MTRRs are not enabled. */
627 if (!(mtrrdef & MTRR_DEF_ENABLE)) {
629 printf("CPU supports MTRRs but not enabled\n");
632 nmdesc = mtrrcap & MTRR_CAP_VCNT;
634 printf("Pentium Pro MTRR support enabled\n");
637 * Determine the size of the PhysMask and PhysBase fields in
638 * the variable range MTRRs.
640 mtrr_physmask = (((uint64_t)1 << cpu_maxphyaddr) - 1) &
643 /* If fixed MTRRs supported and enabled. */
644 if ((mtrrcap & MTRR_CAP_FIXED) && (mtrrdef & MTRR_DEF_FIXED_ENABLE)) {
645 sc->mr_cap = MR686_FIXMTRR;
646 nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
649 sc->mr_desc = malloc(nmdesc * sizeof(struct mem_range_desc), M_MEMDESC,
651 sc->mr_ndesc = nmdesc;
655 /* Populate the fixed MTRR entries' base/length. */
656 if (sc->mr_cap & MR686_FIXMTRR) {
657 for (i = 0; i < MTRR_N64K; i++, mrd++) {
658 mrd->mr_base = i * 0x10000;
659 mrd->mr_len = 0x10000;
660 mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
663 for (i = 0; i < MTRR_N16K; i++, mrd++) {
664 mrd->mr_base = i * 0x4000 + 0x80000;
665 mrd->mr_len = 0x4000;
666 mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
669 for (i = 0; i < MTRR_N4K; i++, mrd++) {
670 mrd->mr_base = i * 0x1000 + 0xc0000;
671 mrd->mr_len = 0x1000;
672 mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
678 * Get current settings, anything set now is considered to
679 * have been set by the firmware. (XXX has something already
684 for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
685 if (mrd->mr_flags & MDF_ACTIVE)
686 mrd->mr_flags |= MDF_FIRMWARE;
689 x86_mr_split_dmap(sc);
693 * Initialise MTRRs on an AP after the BSP has run the init code.
696 x86_mrAPinit(struct mem_range_softc *sc)
700 wrmsr(MSR_MTRRdefType, mtrrdef);
704 * Re-initialise running CPU(s) MTRRs to match the ranges in the descriptor
707 * Must be called with interrupts enabled.
710 x86_mrreinit(struct mem_range_softc *sc)
713 smp_rendezvous(NULL, (void (*)(void *))x86_mrAPinit, NULL, sc);
717 x86_mem_drvinit(void *unused)
722 if (!(cpu_feature & CPUID_MTRR))
724 mem_range_softc.mr_op = &x86_mrops;
725 x86_mrinit(&mem_range_softc);
727 SYSINIT(x86memdev, SI_SUB_CPU, SI_ORDER_ANY, x86_mem_drvinit, NULL);