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 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/memrange.h>
38 #include <sys/sysctl.h>
41 #include <vm/vm_param.h>
44 #include <machine/cputypes.h>
45 #include <machine/md_var.h>
46 #include <machine/specialreg.h>
49 * Pentium Pro+ memory range operations
51 * This code will probably be impenetrable without reference to the
52 * Intel Pentium Pro documentation or x86-64 programmers manual vol 2.
55 static char *mem_owner_bios = "BIOS";
57 #define MR686_FIXMTRR (1<<0)
59 #define mrwithin(mr, a) \
60 (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
61 #define mroverlap(mra, mrb) \
62 (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
64 #define mrvalid(base, len) \
65 ((!(base & ((1 << 12) - 1))) && /* base is multiple of 4k */ \
66 ((len) >= (1 << 12)) && /* length is >= 4k */ \
67 powerof2((len)) && /* ... and power of two */ \
68 !((base) & ((len) - 1))) /* range is not discontiuous */
70 #define mrcopyflags(curr, new) \
71 (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
73 static int mtrrs_disabled;
74 SYSCTL_INT(_machdep, OID_AUTO, disable_mtrrs, CTLFLAG_RDTUN,
78 static void x86_mrinit(struct mem_range_softc *sc);
79 static int x86_mrset(struct mem_range_softc *sc,
80 struct mem_range_desc *mrd, int *arg);
81 static void x86_mrAPinit(struct mem_range_softc *sc);
82 static void x86_mrreinit(struct mem_range_softc *sc);
84 static struct mem_range_ops x86_mrops = {
91 /* XXX for AP startup hook */
92 static u_int64_t mtrrcap, mtrrdef;
94 /* The bitmask for the PhysBase and PhysMask fields of the variable MTRRs. */
95 static u_int64_t mtrr_physmask;
97 static struct mem_range_desc *mem_range_match(struct mem_range_softc *sc,
98 struct mem_range_desc *mrd);
99 static void x86_mrfetch(struct mem_range_softc *sc);
100 static int x86_mtrrtype(int flags);
101 static int x86_mrt2mtrr(int flags, int oldval);
102 static int x86_mtrrconflict(int flag1, int flag2);
103 static void x86_mrstore(struct mem_range_softc *sc);
104 static void x86_mrstoreone(void *arg);
105 static struct mem_range_desc *x86_mtrrfixsearch(struct mem_range_softc *sc,
107 static int x86_mrsetlow(struct mem_range_softc *sc,
108 struct mem_range_desc *mrd, int *arg);
109 static int x86_mrsetvariable(struct mem_range_softc *sc,
110 struct mem_range_desc *mrd, int *arg);
112 /* ia32 MTRR type to memory range type conversion */
113 static int x86_mtrrtomrt[] = {
123 #define MTRRTOMRTLEN nitems(x86_mtrrtomrt)
126 x86_mtrr2mrt(int val)
129 if (val < 0 || val >= MTRRTOMRTLEN)
130 return (MDF_UNKNOWN);
131 return (x86_mtrrtomrt[val]);
135 * x86 MTRR conflicts. Writeback and uncachable may overlap.
138 x86_mtrrconflict(int flag1, int flag2)
141 flag1 &= MDF_ATTRMASK;
142 flag2 &= MDF_ATTRMASK;
143 if ((flag1 & MDF_UNKNOWN) || (flag2 & MDF_UNKNOWN))
145 if (flag1 == flag2 ||
146 (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
147 (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
153 * Look for an exactly-matching range.
155 static struct mem_range_desc *
156 mem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
158 struct mem_range_desc *cand;
161 for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
162 if ((cand->mr_base == mrd->mr_base) &&
163 (cand->mr_len == mrd->mr_len))
169 * Ensure that the direct map region does not contain any mappings
170 * that span MTRRs of different types. However, the fixed MTRRs can
171 * be ignored, because a large page mapping the first 1 MB of physical
172 * memory is a special case that the processor handles. Invalidate
173 * any old TLB entries that might hold inconsistent memory type
177 x86_mr_split_dmap(struct mem_range_softc *sc __unused)
180 struct mem_range_desc *mrd;
183 i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
184 mrd = sc->mr_desc + i;
185 for (; i < sc->mr_ndesc; i++, mrd++) {
186 if ((mrd->mr_flags & (MDF_ACTIVE | MDF_BOGUS)) == MDF_ACTIVE)
187 pmap_demote_DMAP(mrd->mr_base, mrd->mr_len, TRUE);
193 * Fetch the current mtrr settings from the current CPU (assumed to
194 * all be in sync in the SMP case). Note that if we are here, we
195 * assume that MTRRs are enabled, and we may or may not have fixed
199 x86_mrfetch(struct mem_range_softc *sc)
201 struct mem_range_desc *mrd;
207 /* Get fixed-range MTRRs. */
208 if (sc->mr_cap & MR686_FIXMTRR) {
209 msr = MSR_MTRR64kBase;
210 for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
212 for (j = 0; j < 8; j++, mrd++) {
214 (mrd->mr_flags & ~MDF_ATTRMASK) |
215 x86_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
216 if (mrd->mr_owner[0] == 0)
217 strcpy(mrd->mr_owner, mem_owner_bios);
221 msr = MSR_MTRR16kBase;
222 for (i = 0; i < MTRR_N16K / 8; i++, msr++) {
224 for (j = 0; j < 8; j++, mrd++) {
226 (mrd->mr_flags & ~MDF_ATTRMASK) |
227 x86_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
228 if (mrd->mr_owner[0] == 0)
229 strcpy(mrd->mr_owner, mem_owner_bios);
233 msr = MSR_MTRR4kBase;
234 for (i = 0; i < MTRR_N4K / 8; i++, msr++) {
236 for (j = 0; j < 8; j++, mrd++) {
238 (mrd->mr_flags & ~MDF_ATTRMASK) |
239 x86_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
240 if (mrd->mr_owner[0] == 0)
241 strcpy(mrd->mr_owner, mem_owner_bios);
247 /* Get remainder which must be variable MTRRs. */
248 msr = MSR_MTRRVarBase;
249 for (; mrd - sc->mr_desc < sc->mr_ndesc; msr += 2, mrd++) {
251 mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
252 x86_mtrr2mrt(msrv & MTRR_PHYSBASE_TYPE);
253 mrd->mr_base = msrv & mtrr_physmask;
254 msrv = rdmsr(msr + 1);
255 mrd->mr_flags = (msrv & MTRR_PHYSMASK_VALID) ?
256 (mrd->mr_flags | MDF_ACTIVE) :
257 (mrd->mr_flags & ~MDF_ACTIVE);
259 /* Compute the range from the mask. Ick. */
260 mrd->mr_len = (~(msrv & mtrr_physmask) &
261 (mtrr_physmask | 0xfff)) + 1;
262 if (!mrvalid(mrd->mr_base, mrd->mr_len))
263 mrd->mr_flags |= MDF_BOGUS;
265 /* If unclaimed and active, must be the BIOS. */
266 if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
267 strcpy(mrd->mr_owner, mem_owner_bios);
272 * Return the MTRR memory type matching a region's flags
275 x86_mtrrtype(int flags)
279 flags &= MDF_ATTRMASK;
281 for (i = 0; i < MTRRTOMRTLEN; i++) {
282 if (x86_mtrrtomrt[i] == MDF_UNKNOWN)
284 if (flags == x86_mtrrtomrt[i])
291 x86_mrt2mtrr(int flags, int oldval)
295 if ((val = x86_mtrrtype(flags)) == -1)
296 return (oldval & 0xff);
301 * Update running CPU(s) MTRRs to match the ranges in the descriptor
304 * Must be called with interrupts enabled.
307 x86_mrstore(struct mem_range_softc *sc)
310 smp_rendezvous(NULL, x86_mrstoreone, NULL, sc);
314 * Update the current CPU's MTRRs with those represented in the
315 * descriptor list. Note that we do this wholesale rather than just
316 * stuffing one entry; this is simpler (but slower, of course).
319 x86_mrstoreone(void *arg)
321 struct mem_range_softc *sc = arg;
322 struct mem_range_desc *mrd;
323 u_int64_t omsrv, msrv;
333 load_cr4(cr4 & ~CR4_PGE);
335 /* Disable caches (CD = 1, NW = 0). */
337 load_cr0((cr0 & ~CR0_NW) | CR0_CD);
339 /* Flushes caches and TLBs. */
343 /* Disable MTRRs (E = 0). */
344 wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~MTRR_DEF_ENABLE);
346 /* Set fixed-range MTRRs. */
347 if (sc->mr_cap & MR686_FIXMTRR) {
348 msr = MSR_MTRR64kBase;
349 for (i = 0; i < MTRR_N64K / 8; i++, msr++) {
352 for (j = 7; j >= 0; j--) {
354 msrv |= x86_mrt2mtrr((mrd + j)->mr_flags,
360 msr = MSR_MTRR16kBase;
361 for (i = 0; i < MTRR_N16K / 8; i++, msr++) {
364 for (j = 7; j >= 0; j--) {
366 msrv |= x86_mrt2mtrr((mrd + j)->mr_flags,
372 msr = MSR_MTRR4kBase;
373 for (i = 0; i < MTRR_N4K / 8; i++, msr++) {
376 for (j = 7; j >= 0; j--) {
378 msrv |= x86_mrt2mtrr((mrd + j)->mr_flags,
386 /* Set remainder which must be variable MTRRs. */
387 msr = MSR_MTRRVarBase;
388 for (; mrd - sc->mr_desc < sc->mr_ndesc; msr += 2, mrd++) {
389 /* base/type register */
391 if (mrd->mr_flags & MDF_ACTIVE) {
392 msrv = mrd->mr_base & mtrr_physmask;
393 msrv |= x86_mrt2mtrr(mrd->mr_flags, omsrv);
399 /* mask/active register */
400 if (mrd->mr_flags & MDF_ACTIVE) {
401 msrv = MTRR_PHYSMASK_VALID |
402 rounddown2(mtrr_physmask, mrd->mr_len);
406 wrmsr(msr + 1, msrv);
409 /* Flush caches and TLBs. */
414 wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | MTRR_DEF_ENABLE);
416 /* Restore caches and PGE. */
424 * Hunt for the fixed MTRR referencing (addr)
426 static struct mem_range_desc *
427 x86_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
429 struct mem_range_desc *mrd;
432 for (i = 0, mrd = sc->mr_desc; i < MTRR_N64K + MTRR_N16K + MTRR_N4K;
434 if (addr >= mrd->mr_base &&
435 addr < mrd->mr_base + mrd->mr_len)
441 * Try to satisfy the given range request by manipulating the fixed
442 * MTRRs that cover low memory.
444 * Note that we try to be generous here; we'll bloat the range out to
445 * the next higher/lower boundary to avoid the consumer having to know
446 * too much about the mechanisms here.
448 * XXX note that this will have to be updated when we start supporting
452 x86_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
454 struct mem_range_desc *first_md, *last_md, *curr_md;
457 if ((first_md = x86_mtrrfixsearch(sc, mrd->mr_base)) == NULL ||
458 (last_md = x86_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1))
462 /* Check that we aren't doing something risky. */
463 if ((mrd->mr_flags & MDF_FORCE) == 0) {
464 for (curr_md = first_md; curr_md <= last_md; curr_md++) {
465 if ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN)
470 /* Set flags, clear set-by-firmware flag. */
471 for (curr_md = first_md; curr_md <= last_md; curr_md++) {
472 curr_md->mr_flags = mrcopyflags(curr_md->mr_flags &
473 ~MDF_FIRMWARE, mrd->mr_flags);
474 bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
481 * Modify/add a variable MTRR to satisfy the request.
483 * XXX needs to be updated to properly support "busy" ranges.
486 x86_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd,
489 struct mem_range_desc *curr_md, *free_md;
493 * Scan the currently active variable descriptors, look for
494 * one we exactly match (straight takeover) and for possible
495 * accidental overlaps.
497 * Keep track of the first empty variable descriptor in case
498 * we can't perform a takeover.
500 i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
501 curr_md = sc->mr_desc + i;
503 for (; i < sc->mr_ndesc; i++, curr_md++) {
504 if (curr_md->mr_flags & MDF_ACTIVE) {
506 if (curr_md->mr_base == mrd->mr_base &&
507 curr_md->mr_len == mrd->mr_len) {
508 /* Whoops, owned by someone. */
509 if (curr_md->mr_flags & MDF_BUSY)
512 /* Check that we aren't doing something risky */
513 if (!(mrd->mr_flags & MDF_FORCE) &&
514 (curr_md->mr_flags & MDF_ATTRMASK) ==
518 /* Ok, just hijack this entry. */
523 /* Non-exact overlap? */
524 if (mroverlap(curr_md, mrd)) {
525 /* Between conflicting region types? */
526 if (x86_mtrrconflict(curr_md->mr_flags,
530 } else if (free_md == NULL) {
535 /* Got somewhere to put it? */
539 /* Set up new descriptor. */
540 free_md->mr_base = mrd->mr_base;
541 free_md->mr_len = mrd->mr_len;
542 free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
543 bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
548 * Handle requests to set memory range attributes by manipulating MTRRs.
551 x86_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
553 struct mem_range_desc *targ;
557 case MEMRANGE_SET_UPDATE:
559 * Make sure that what's being asked for is even
562 if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
563 x86_mtrrtype(mrd->mr_flags) == -1)
567 ((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
569 /* Are the "low memory" conditions applicable? */
570 if ((sc->mr_cap & MR686_FIXMTRR) != 0 &&
571 mrd->mr_base + mrd->mr_len <= FIXTOP) {
572 if ((error = x86_mrsetlow(sc, mrd, arg)) != 0)
575 /* It's time to play with variable MTRRs. */
576 if ((error = x86_mrsetvariable(sc, mrd, arg)) != 0)
581 case MEMRANGE_SET_REMOVE:
582 if ((targ = mem_range_match(sc, mrd)) == NULL)
584 if (targ->mr_flags & MDF_FIXACTIVE)
586 if (targ->mr_flags & MDF_BUSY)
588 targ->mr_flags &= ~MDF_ACTIVE;
589 targ->mr_owner[0] = 0;
596 x86_mr_split_dmap(sc);
598 /* Update the hardware. */
601 /* Refetch to see where we're at. */
607 * Work out how many ranges we support, initialise storage for them,
608 * and fetch the initial settings.
611 x86_mrinit(struct mem_range_softc *sc)
613 struct mem_range_desc *mrd;
616 if (sc->mr_desc != NULL)
617 /* Already initialized. */
621 mtrrcap = rdmsr(MSR_MTRRcap);
622 mtrrdef = rdmsr(MSR_MTRRdefType);
624 /* For now, bail out if MTRRs are not enabled. */
625 if (!(mtrrdef & MTRR_DEF_ENABLE)) {
627 printf("CPU supports MTRRs but not enabled\n");
630 nmdesc = mtrrcap & MTRR_CAP_VCNT;
632 printf("Pentium Pro MTRR support enabled\n");
635 * Determine the size of the PhysMask and PhysBase fields in
636 * the variable range MTRRs.
638 mtrr_physmask = (((uint64_t)1 << cpu_maxphyaddr) - 1) &
641 /* If fixed MTRRs supported and enabled. */
642 if ((mtrrcap & MTRR_CAP_FIXED) && (mtrrdef & MTRR_DEF_FIXED_ENABLE)) {
643 sc->mr_cap = MR686_FIXMTRR;
644 nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
647 sc->mr_desc = malloc(nmdesc * sizeof(struct mem_range_desc), M_MEMDESC,
649 sc->mr_ndesc = nmdesc;
653 /* Populate the fixed MTRR entries' base/length. */
654 if (sc->mr_cap & MR686_FIXMTRR) {
655 for (i = 0; i < MTRR_N64K; i++, mrd++) {
656 mrd->mr_base = i * 0x10000;
657 mrd->mr_len = 0x10000;
658 mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
661 for (i = 0; i < MTRR_N16K; i++, mrd++) {
662 mrd->mr_base = i * 0x4000 + 0x80000;
663 mrd->mr_len = 0x4000;
664 mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
667 for (i = 0; i < MTRR_N4K; i++, mrd++) {
668 mrd->mr_base = i * 0x1000 + 0xc0000;
669 mrd->mr_len = 0x1000;
670 mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
676 * Get current settings, anything set now is considered to
677 * have been set by the firmware. (XXX has something already
682 for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
683 if (mrd->mr_flags & MDF_ACTIVE)
684 mrd->mr_flags |= MDF_FIRMWARE;
687 x86_mr_split_dmap(sc);
691 * Initialise MTRRs on an AP after the BSP has run the init code.
694 x86_mrAPinit(struct mem_range_softc *sc)
698 wrmsr(MSR_MTRRdefType, mtrrdef);
702 * Re-initialise running CPU(s) MTRRs to match the ranges in the descriptor
705 * Must be called with interrupts enabled.
708 x86_mrreinit(struct mem_range_softc *sc)
711 smp_rendezvous(NULL, (void (*)(void *))x86_mrAPinit, NULL, sc);
715 x86_mem_drvinit(void *unused)
720 if (!(cpu_feature & CPUID_MTRR))
722 mem_range_softc.mr_op = &x86_mrops;
723 x86_mrinit(&mem_range_softc);
725 SYSINIT(x86memdev, SI_SUB_CPU, SI_ORDER_ANY, x86_mem_drvinit, NULL);