x86_mem.c revision 94683
145405Smsmith/*-
245405Smsmith * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
345405Smsmith * All rights reserved.
445405Smsmith *
545405Smsmith * Redistribution and use in source and binary forms, with or without
645405Smsmith * modification, are permitted provided that the following conditions
745405Smsmith * are met:
845405Smsmith * 1. Redistributions of source code must retain the above copyright
945405Smsmith *    notice, this list of conditions and the following disclaimer.
1045405Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1145405Smsmith *    notice, this list of conditions and the following disclaimer in the
1245405Smsmith *    documentation and/or other materials provided with the distribution.
1345405Smsmith *
1445405Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1545405Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1645405Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1745405Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1845405Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1945405Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2045405Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2145405Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2245405Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2345405Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2445405Smsmith * SUCH DAMAGE.
2545405Smsmith *
2650477Speter * $FreeBSD: head/sys/i386/i386/i686_mem.c 94683 2002-04-14 20:13:08Z dwmalone $
2745405Smsmith */
2845405Smsmith
2945405Smsmith#include <sys/param.h>
3045405Smsmith#include <sys/kernel.h>
3145405Smsmith#include <sys/systm.h>
3245405Smsmith#include <sys/malloc.h>
3345405Smsmith#include <sys/memrange.h>
3476078Sjhb#include <sys/smp.h>
3545405Smsmith
3645405Smsmith#include <machine/md_var.h>
3745405Smsmith#include <machine/specialreg.h>
3845405Smsmith
3945405Smsmith/*
4045405Smsmith * i686 memory range operations
4145405Smsmith *
4245405Smsmith * This code will probably be impenetrable without reference to the
4345405Smsmith * Intel Pentium Pro documentation.
4445405Smsmith */
4545405Smsmith
4645405Smsmithstatic char *mem_owner_bios = "BIOS";
4745405Smsmith
4845405Smsmith#define MR686_FIXMTRR	(1<<0)
4945405Smsmith
5045405Smsmith#define mrwithin(mr, a) \
5145405Smsmith    (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
5245405Smsmith#define mroverlap(mra, mrb) \
5345405Smsmith    (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
5445405Smsmith
5545405Smsmith#define mrvalid(base, len) 						\
5645405Smsmith    ((!(base & ((1 << 12) - 1))) && 	/* base is multiple of 4k */	\
5745405Smsmith     ((len) >= (1 << 12)) && 		/* length is >= 4k */		\
5845405Smsmith     powerof2((len)) && 		/* ... and power of two */	\
5945405Smsmith     !((base) & ((len) - 1)))		/* range is not discontiuous */
6045405Smsmith
6145405Smsmith#define mrcopyflags(curr, new) (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
6245405Smsmith
6346215Smsmithstatic void			i686_mrinit(struct mem_range_softc *sc);
6446215Smsmithstatic int			i686_mrset(struct mem_range_softc *sc,
6546215Smsmith					   struct mem_range_desc *mrd,
6646215Smsmith					   int *arg);
6746215Smsmithstatic void			i686_mrAPinit(struct mem_range_softc *sc);
6845405Smsmith
6945405Smsmithstatic struct mem_range_ops i686_mrops = {
7045405Smsmith    i686_mrinit,
7146215Smsmith    i686_mrset,
7246215Smsmith    i686_mrAPinit
7345405Smsmith};
7445405Smsmith
7546215Smsmith/* XXX for AP startup hook */
7646215Smsmithstatic u_int64_t		mtrrcap, mtrrdef;
7746215Smsmith
7845405Smsmithstatic struct mem_range_desc	*mem_range_match(struct mem_range_softc *sc,
7945405Smsmith						 struct mem_range_desc *mrd);
8045405Smsmithstatic void			i686_mrfetch(struct mem_range_softc *sc);
8145405Smsmithstatic int			i686_mtrrtype(int flags);
8294683Sdwmalonestatic int			i686_mrt2mtrr(int flags, int oldval);
8394683Sdwmalonestatic int			i686_mtrrconflict(int flag1, int flag2);
8448925Smsmithstatic void			i686_mrstore(struct mem_range_softc *sc);
8548925Smsmithstatic void			i686_mrstoreone(void *arg);
8645405Smsmithstatic struct mem_range_desc	*i686_mtrrfixsearch(struct mem_range_softc *sc,
8745405Smsmith						    u_int64_t addr);
8845405Smsmithstatic int			i686_mrsetlow(struct mem_range_softc *sc,
8945405Smsmith					      struct mem_range_desc *mrd,
9045405Smsmith					      int *arg);
9145405Smsmithstatic int			i686_mrsetvariable(struct mem_range_softc *sc,
9245405Smsmith						   struct mem_range_desc *mrd,
9345405Smsmith						   int *arg);
9445405Smsmith
9545405Smsmith/* i686 MTRR type to memory range type conversion */
9645405Smsmithstatic int i686_mtrrtomrt[] = {
9745405Smsmith    MDF_UNCACHEABLE,
9845405Smsmith    MDF_WRITECOMBINE,
9994683Sdwmalone    MDF_UNKNOWN,
10094683Sdwmalone    MDF_UNKNOWN,
10145405Smsmith    MDF_WRITETHROUGH,
10245405Smsmith    MDF_WRITEPROTECT,
10345405Smsmith    MDF_WRITEBACK
10445405Smsmith};
10545405Smsmith
10694683Sdwmalone#define MTRRTOMRTLEN (sizeof(i686_mtrrtomrt) / sizeof(i686_mtrrtomrt[0]))
10794683Sdwmalone
10894683Sdwmalonestatic int
10994683Sdwmalonei686_mtrr2mrt(int val) {
11094683Sdwmalone	if (val < 0 || val >= MTRRTOMRTLEN)
11194683Sdwmalone		return MDF_UNKNOWN;
11294683Sdwmalone	return i686_mtrrtomrt[val];
11394683Sdwmalone}
11494683Sdwmalone
11548925Smsmith/*
11694683Sdwmalone * i686 MTRR conflicts. Writeback and uncachable may overlap.
11748925Smsmith */
11894683Sdwmalonestatic int
11994683Sdwmalonei686_mtrrconflict(int flag1, int flag2) {
12094683Sdwmalone	flag1 &= MDF_ATTRMASK;
12194683Sdwmalone	flag2 &= MDF_ATTRMASK;
12294683Sdwmalone	if (flag1 == flag2 ||
12394683Sdwmalone	    (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
12494683Sdwmalone	    (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
12594683Sdwmalone		return 0;
12694683Sdwmalone	return 1;
12794683Sdwmalone}
12845405Smsmith
12945405Smsmith/*
13045405Smsmith * Look for an exactly-matching range.
13145405Smsmith */
13245405Smsmithstatic struct mem_range_desc *
13345405Smsmithmem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
13445405Smsmith{
13545405Smsmith    struct mem_range_desc	*cand;
13645405Smsmith    int				i;
13745405Smsmith
13845405Smsmith    for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
13945405Smsmith	if ((cand->mr_base == mrd->mr_base) &&
14045405Smsmith	    (cand->mr_len == mrd->mr_len))
14145405Smsmith	    return(cand);
14245405Smsmith    return(NULL);
14345405Smsmith}
14445405Smsmith
14545405Smsmith/*
14645405Smsmith * Fetch the current mtrr settings from the current CPU (assumed to all
14745405Smsmith * be in sync in the SMP case).  Note that if we are here, we assume
14845405Smsmith * that MTRRs are enabled, and we may or may not have fixed MTRRs.
14945405Smsmith */
15045405Smsmithstatic void
15145405Smsmithi686_mrfetch(struct mem_range_softc *sc)
15245405Smsmith{
15345405Smsmith    struct mem_range_desc	*mrd;
15445405Smsmith    u_int64_t			msrv;
15545405Smsmith    int				i, j, msr;
15645405Smsmith
15745405Smsmith    mrd = sc->mr_desc;
15845405Smsmith
15945405Smsmith    /* Get fixed-range MTRRs */
16045405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
16145405Smsmith	msr = MSR_MTRR64kBase;
16245405Smsmith	for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
16345405Smsmith	    msrv = rdmsr(msr);
16445405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
16545405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
16694683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
16745405Smsmith		    MDF_ACTIVE;
16845405Smsmith		if (mrd->mr_owner[0] == 0)
16945405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
17045405Smsmith		msrv = msrv >> 8;
17145405Smsmith	    }
17245405Smsmith	}
17345405Smsmith	msr = MSR_MTRR16kBase;
17445405Smsmith	for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
17545405Smsmith	    msrv = rdmsr(msr);
17645405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
17745405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
17894683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
17945405Smsmith		    MDF_ACTIVE;
18045405Smsmith		if (mrd->mr_owner[0] == 0)
18145405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
18245405Smsmith		msrv = msrv >> 8;
18345405Smsmith	    }
18445405Smsmith	}
18545405Smsmith	msr = MSR_MTRR4kBase;
18645405Smsmith	for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
18745405Smsmith	    msrv = rdmsr(msr);
18845405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
18945405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
19094683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
19145405Smsmith		    MDF_ACTIVE;
19245405Smsmith		if (mrd->mr_owner[0] == 0)
19345405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
19445405Smsmith		msrv = msrv >> 8;
19545405Smsmith	    }
19645405Smsmith	}
19745405Smsmith    }
19845405Smsmith
19945405Smsmith    /* Get remainder which must be variable MTRRs */
20045405Smsmith    msr = MSR_MTRRVarBase;
20145405Smsmith    for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
20245405Smsmith	msrv = rdmsr(msr);
20345405Smsmith	mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
20494683Sdwmalone	    i686_mtrr2mrt(msrv & 0xff);
20545405Smsmith	mrd->mr_base = msrv & 0x0000000ffffff000LL;
20645405Smsmith	msrv = rdmsr(msr + 1);
20745405Smsmith	mrd->mr_flags = (msrv & 0x800) ?
20845405Smsmith	    (mrd->mr_flags | MDF_ACTIVE) :
20945405Smsmith	    (mrd->mr_flags & ~MDF_ACTIVE);
21045405Smsmith	/* Compute the range from the mask. Ick. */
21145405Smsmith	mrd->mr_len = (~(msrv & 0x0000000ffffff000LL) & 0x0000000fffffffffLL) + 1;
21245405Smsmith	if (!mrvalid(mrd->mr_base, mrd->mr_len))
21345405Smsmith	    mrd->mr_flags |= MDF_BOGUS;
21445405Smsmith	/* If unclaimed and active, must be the BIOS */
21545405Smsmith	if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
21645405Smsmith	    strcpy(mrd->mr_owner, mem_owner_bios);
21745405Smsmith    }
21845405Smsmith}
21945405Smsmith
22045405Smsmith/*
22145405Smsmith * Return the MTRR memory type matching a region's flags
22245405Smsmith */
22345405Smsmithstatic int
22445405Smsmithi686_mtrrtype(int flags)
22545405Smsmith{
22645405Smsmith    int		i;
22745405Smsmith
22845405Smsmith    flags &= MDF_ATTRMASK;
22945405Smsmith
23094683Sdwmalone    for (i = 0; i < MTRRTOMRTLEN; i++) {
23194683Sdwmalone	if (i686_mtrrtomrt[i] == MDF_UNKNOWN)
23245405Smsmith	    continue;
23345405Smsmith	if (flags == i686_mtrrtomrt[i])
23445405Smsmith	    return(i);
23545405Smsmith    }
23645405Smsmith    return(-1);
23745405Smsmith}
23845405Smsmith
23994683Sdwmalonestatic int
24094683Sdwmalonei686_mrt2mtrr(int flags, int oldval)
24194683Sdwmalone{
24294683Sdwmalone	int val;
24394683Sdwmalone
24494683Sdwmalone	if ((val = i686_mtrrtype(flags)) == -1)
24594683Sdwmalone		return oldval & 0xff;
24694683Sdwmalone	return val & 0xff;
24794683Sdwmalone}
24894683Sdwmalone
24945405Smsmith/*
25046215Smsmith * Update running CPU(s) MTRRs to match the ranges in the descriptor
25146215Smsmith * list.
25246215Smsmith *
25346215Smsmith * XXX Must be called with interrupts enabled.
25445405Smsmith */
25548925Smsmithstatic void
25645405Smsmithi686_mrstore(struct mem_range_softc *sc)
25745405Smsmith{
25845405Smsmith#ifdef SMP
25945405Smsmith    /*
26075421Sjhb     * We should use ipi_all_but_self() to call other CPUs into a
26145405Smsmith     * locking gate, then call a target function to do this work.
26245405Smsmith     * The "proper" solution involves a generalised locking gate
26345405Smsmith     * implementation, not ready yet.
26445405Smsmith     */
26548925Smsmith    smp_rendezvous(NULL, i686_mrstoreone, NULL, (void *)sc);
26648925Smsmith#else
26745405Smsmith    disable_intr();				/* disable interrupts */
26849421Smsmith    i686_mrstoreone((void *)sc);
26946215Smsmith    enable_intr();
27048925Smsmith#endif
27146215Smsmith}
27246215Smsmith
27346215Smsmith/*
27446215Smsmith * Update the current CPU's MTRRs with those represented in the
27548925Smsmith * descriptor list.  Note that we do this wholesale rather than
27648925Smsmith * just stuffing one entry; this is simpler (but slower, of course).
27746215Smsmith */
27848925Smsmithstatic void
27948925Smsmithi686_mrstoreone(void *arg)
28046215Smsmith{
28148925Smsmith    struct mem_range_softc 	*sc = (struct mem_range_softc *)arg;
28246215Smsmith    struct mem_range_desc	*mrd;
28394683Sdwmalone    u_int64_t			omsrv, msrv;
28446215Smsmith    int				i, j, msr;
28546215Smsmith    u_int			cr4save;
28646215Smsmith
28748925Smsmith    mrd = sc->mr_desc;
28846215Smsmith
28945405Smsmith    cr4save = rcr4();				/* save cr4 */
29045405Smsmith    if (cr4save & CR4_PGE)
29145405Smsmith	load_cr4(cr4save & ~CR4_PGE);
29245405Smsmith    load_cr0((rcr0() & ~CR0_NW) | CR0_CD);	/* disable caches (CD = 1, NW = 0) */
29348925Smsmith    wbinvd();					/* flush caches, TLBs */
29445405Smsmith    wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~0x800);	/* disable MTRRs (E = 0) */
29545405Smsmith
29645405Smsmith    /* Set fixed-range MTRRs */
29745405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
29845405Smsmith	msr = MSR_MTRR64kBase;
29945405Smsmith	for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
30045405Smsmith	    msrv = 0;
30194683Sdwmalone	    omsrv = rdmsr(msr);
30245405Smsmith	    for (j = 7; j >= 0; j--) {
30345405Smsmith		msrv = msrv << 8;
30494683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
30545405Smsmith	    }
30645405Smsmith	    wrmsr(msr, msrv);
30745405Smsmith	    mrd += 8;
30845405Smsmith	}
30945405Smsmith	msr = MSR_MTRR16kBase;
31045405Smsmith	for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
31145405Smsmith	    msrv = 0;
31294683Sdwmalone	    omsrv = rdmsr(msr);
31345405Smsmith	    for (j = 7; j >= 0; j--) {
31445405Smsmith		msrv = msrv << 8;
31594683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
31645405Smsmith	    }
31745405Smsmith	    wrmsr(msr, msrv);
31845405Smsmith	    mrd += 8;
31945405Smsmith	}
32045405Smsmith	msr = MSR_MTRR4kBase;
32145405Smsmith	for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
32245405Smsmith	    msrv = 0;
32394683Sdwmalone	    omsrv = rdmsr(msr);
32445405Smsmith	    for (j = 7; j >= 0; j--) {
32545405Smsmith		msrv = msrv << 8;
32694683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
32745405Smsmith	    }
32845405Smsmith	    wrmsr(msr, msrv);
32945405Smsmith	    mrd += 8;
33045405Smsmith	}
33145405Smsmith    }
33245405Smsmith
33345405Smsmith    /* Set remainder which must be variable MTRRs */
33445405Smsmith    msr = MSR_MTRRVarBase;
33545405Smsmith    for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
33645405Smsmith	/* base/type register */
33794683Sdwmalone	omsrv = rdmsr(msr);
33845405Smsmith	if (mrd->mr_flags & MDF_ACTIVE) {
33945405Smsmith	    msrv = mrd->mr_base & 0x0000000ffffff000LL;
34094683Sdwmalone	    msrv |= i686_mrt2mtrr(mrd->mr_flags, omsrv);
34145405Smsmith	} else {
34245405Smsmith	    msrv = 0;
34345405Smsmith	}
34445405Smsmith	wrmsr(msr, msrv);
34545405Smsmith
34645405Smsmith	/* mask/active register */
34745405Smsmith	if (mrd->mr_flags & MDF_ACTIVE) {
34845405Smsmith	    msrv = 0x800 | (~(mrd->mr_len - 1) & 0x0000000ffffff000LL);
34945405Smsmith	} else {
35045405Smsmith	    msrv = 0;
35145405Smsmith	}
35245405Smsmith	wrmsr(msr + 1, msrv);
35345405Smsmith    }
35448925Smsmith    wbinvd();							/* flush caches, TLBs */
35545405Smsmith    wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | 0x800);	/* restore MTRR state */
35645405Smsmith    load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  			/* enable caches CD = 0 and NW = 0 */
35745405Smsmith    load_cr4(cr4save);						/* restore cr4 */
35845405Smsmith}
35945405Smsmith
36045405Smsmith/*
36145405Smsmith * Hunt for the fixed MTRR referencing (addr)
36245405Smsmith */
36345405Smsmithstatic struct mem_range_desc *
36445405Smsmithi686_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
36545405Smsmith{
36645405Smsmith    struct mem_range_desc *mrd;
36745405Smsmith    int			i;
36845405Smsmith
36945405Smsmith    for (i = 0, mrd = sc->mr_desc; i < (MTRR_N64K + MTRR_N16K + MTRR_N4K); i++, mrd++)
37045405Smsmith	if ((addr >= mrd->mr_base) && (addr < (mrd->mr_base + mrd->mr_len)))
37145405Smsmith	    return(mrd);
37245405Smsmith    return(NULL);
37345405Smsmith}
37445405Smsmith
37545405Smsmith/*
37645405Smsmith * Try to satisfy the given range request by manipulating the fixed MTRRs that
37745405Smsmith * cover low memory.
37845405Smsmith *
37945405Smsmith * Note that we try to be generous here; we'll bloat the range out to the
38045405Smsmith * next higher/lower boundary to avoid the consumer having to know too much
38145405Smsmith * about the mechanisms here.
38245405Smsmith *
38345405Smsmith * XXX note that this will have to be updated when we start supporting "busy" ranges.
38445405Smsmith */
38545405Smsmithstatic int
38645405Smsmithi686_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
38745405Smsmith{
38845405Smsmith    struct mem_range_desc	*first_md, *last_md, *curr_md;
38945405Smsmith
39045405Smsmith    /* range check */
39145405Smsmith    if (((first_md = i686_mtrrfixsearch(sc, mrd->mr_base)) == NULL) ||
39245405Smsmith	((last_md = i686_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1)) == NULL))
39345405Smsmith	return(EINVAL);
39445405Smsmith
39545405Smsmith    /* set flags, clear set-by-firmware flag */
39645405Smsmith    for (curr_md = first_md; curr_md <= last_md; curr_md++) {
39745405Smsmith	curr_md->mr_flags = mrcopyflags(curr_md->mr_flags & ~MDF_FIRMWARE, mrd->mr_flags);
39845405Smsmith	bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
39945405Smsmith    }
40045405Smsmith
40145405Smsmith    return(0);
40245405Smsmith}
40345405Smsmith
40445405Smsmith
40545405Smsmith/*
40645405Smsmith * Modify/add a variable MTRR to satisfy the request.
40745405Smsmith *
40845405Smsmith * XXX needs to be updated to properly support "busy" ranges.
40945405Smsmith */
41045405Smsmithstatic int
41145405Smsmithi686_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
41245405Smsmith{
41345405Smsmith    struct mem_range_desc	*curr_md, *free_md;
41445405Smsmith    int				i;
41545405Smsmith
41645405Smsmith    /*
41745405Smsmith     * Scan the currently active variable descriptors, look for
41845405Smsmith     * one we exactly match (straight takeover) and for possible
41945405Smsmith     * accidental overlaps.
42045405Smsmith     * Keep track of the first empty variable descriptor in case we
42145405Smsmith     * can't perform a takeover.
42245405Smsmith     */
42345405Smsmith    i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
42445405Smsmith    curr_md = sc->mr_desc + i;
42545405Smsmith    free_md = NULL;
42645405Smsmith    for (; i < sc->mr_ndesc; i++, curr_md++) {
42745405Smsmith	if (curr_md->mr_flags & MDF_ACTIVE) {
42845405Smsmith	    /* exact match? */
42945405Smsmith	    if ((curr_md->mr_base == mrd->mr_base) &&
43045405Smsmith		(curr_md->mr_len == mrd->mr_len)) {
43145405Smsmith		/* whoops, owned by someone */
43245405Smsmith		if (curr_md->mr_flags & MDF_BUSY)
43345405Smsmith		    return(EBUSY);
43445405Smsmith		/* Ok, just hijack this entry */
43545405Smsmith		free_md = curr_md;
43645405Smsmith		break;
43745405Smsmith	    }
43848925Smsmith	    /* non-exact overlap ? */
43948925Smsmith	    if (mroverlap(curr_md, mrd)) {
44048925Smsmith		/* between conflicting region types? */
44194683Sdwmalone		if (i686_mtrrconflict(curr_md->mr_flags, mrd->mr_flags))
44248925Smsmith		    return(EINVAL);
44348925Smsmith	    }
44445405Smsmith	} else if (free_md == NULL) {
44545405Smsmith	    free_md = curr_md;
44645405Smsmith	}
44745405Smsmith    }
44845405Smsmith    /* got somewhere to put it? */
44945405Smsmith    if (free_md == NULL)
45045405Smsmith	return(ENOSPC);
45145405Smsmith
45245405Smsmith    /* Set up new descriptor */
45345405Smsmith    free_md->mr_base = mrd->mr_base;
45445405Smsmith    free_md->mr_len = mrd->mr_len;
45545405Smsmith    free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
45645405Smsmith    bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
45745405Smsmith    return(0);
45845405Smsmith}
45945405Smsmith
46045405Smsmith/*
46145405Smsmith * Handle requests to set memory range attributes by manipulating MTRRs.
46245405Smsmith *
46345405Smsmith */
46445405Smsmithstatic int
46545405Smsmithi686_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
46645405Smsmith{
46745405Smsmith    struct mem_range_desc	*targ;
46845405Smsmith    int				error = 0;
46945405Smsmith
47045405Smsmith    switch(*arg) {
47145405Smsmith    case MEMRANGE_SET_UPDATE:
47245405Smsmith	/* make sure that what's being asked for is even possible at all */
47345405Smsmith	if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
47494683Sdwmalone	    i686_mtrrtype(mrd->mr_flags) == -1)
47545405Smsmith	    return(EINVAL);
47645405Smsmith
47745405Smsmith#define FIXTOP	((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
47845405Smsmith
47945405Smsmith	/* are the "low memory" conditions applicable? */
48045405Smsmith	if ((sc->mr_cap & MR686_FIXMTRR) &&
48145405Smsmith	    ((mrd->mr_base + mrd->mr_len) <= FIXTOP)) {
48245405Smsmith	    if ((error = i686_mrsetlow(sc, mrd, arg)) != 0)
48345405Smsmith		return(error);
48445405Smsmith	} else {
48545405Smsmith	    /* it's time to play with variable MTRRs */
48645405Smsmith	    if ((error = i686_mrsetvariable(sc, mrd, arg)) != 0)
48745405Smsmith		return(error);
48845405Smsmith	}
48945405Smsmith	break;
49045405Smsmith
49145405Smsmith    case MEMRANGE_SET_REMOVE:
49245405Smsmith	if ((targ = mem_range_match(sc, mrd)) == NULL)
49345405Smsmith	    return(ENOENT);
49445405Smsmith	if (targ->mr_flags & MDF_FIXACTIVE)
49545405Smsmith	    return(EPERM);
49645405Smsmith	if (targ->mr_flags & MDF_BUSY)
49745405Smsmith	    return(EBUSY);
49845405Smsmith	targ->mr_flags &= ~MDF_ACTIVE;
49945405Smsmith	targ->mr_owner[0] = 0;
50045405Smsmith	break;
50145405Smsmith
50245405Smsmith    default:
50345405Smsmith	return(EOPNOTSUPP);
50445405Smsmith    }
50545405Smsmith
50645405Smsmith    /* update the hardware */
50748925Smsmith    i686_mrstore(sc);
50845405Smsmith    i686_mrfetch(sc);	/* refetch to see where we're at */
50948925Smsmith    return(0);
51045405Smsmith}
51145405Smsmith
51245405Smsmith/*
51345405Smsmith * Work out how many ranges we support, initialise storage for them,
51445405Smsmith * fetch the initial settings.
51545405Smsmith */
51645405Smsmithstatic void
51745405Smsmithi686_mrinit(struct mem_range_softc *sc)
51845405Smsmith{
51945405Smsmith    struct mem_range_desc	*mrd;
52045405Smsmith    int				nmdesc = 0;
52145405Smsmith    int				i;
52245405Smsmith
52345405Smsmith    mtrrcap = rdmsr(MSR_MTRRcap);
52445405Smsmith    mtrrdef = rdmsr(MSR_MTRRdefType);
52545405Smsmith
52645405Smsmith    /* For now, bail out if MTRRs are not enabled */
52745405Smsmith    if (!(mtrrdef & 0x800)) {
52845405Smsmith	if (bootverbose)
52945405Smsmith	    printf("CPU supports MTRRs but not enabled\n");
53045405Smsmith	return;
53145405Smsmith    }
53245405Smsmith    nmdesc = mtrrcap & 0xff;
53348925Smsmith    printf("Pentium Pro MTRR support enabled\n");
53445405Smsmith
53545405Smsmith    /* If fixed MTRRs supported and enabled */
53645405Smsmith    if ((mtrrcap & 0x100) && (mtrrdef & 0x400)) {
53745405Smsmith	sc->mr_cap = MR686_FIXMTRR;
53845405Smsmith	nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
53945405Smsmith    }
54045405Smsmith
54145405Smsmith    sc->mr_desc =
54245405Smsmith	(struct mem_range_desc *)malloc(nmdesc * sizeof(struct mem_range_desc),
54369781Sdwmalone					M_MEMDESC, M_WAITOK | M_ZERO);
54445405Smsmith    sc->mr_ndesc = nmdesc;
54545405Smsmith
54645405Smsmith    mrd = sc->mr_desc;
54745405Smsmith
54845405Smsmith    /* Populate the fixed MTRR entries' base/length */
54945405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
55045405Smsmith	for (i = 0; i < MTRR_N64K; i++, mrd++) {
55145405Smsmith	    mrd->mr_base = i * 0x10000;
55245405Smsmith	    mrd->mr_len = 0x10000;
55345405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
55445405Smsmith	}
55545405Smsmith	for (i = 0; i < MTRR_N16K; i++, mrd++) {
55645405Smsmith	    mrd->mr_base = i * 0x4000 + 0x80000;
55745405Smsmith	    mrd->mr_len = 0x4000;
55845405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
55945405Smsmith	}
56045405Smsmith	for (i = 0; i < MTRR_N4K; i++, mrd++) {
56145405Smsmith	    mrd->mr_base = i * 0x1000 + 0xc0000;
56245405Smsmith	    mrd->mr_len = 0x1000;
56345405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
56445405Smsmith	}
56545405Smsmith    }
56645405Smsmith
56745405Smsmith    /*
56845405Smsmith     * Get current settings, anything set now is considered to have
56945405Smsmith     * been set by the firmware. (XXX has something already played here?)
57045405Smsmith     */
57145405Smsmith    i686_mrfetch(sc);
57245405Smsmith    mrd = sc->mr_desc;
57345405Smsmith    for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
57445405Smsmith	if (mrd->mr_flags & MDF_ACTIVE)
57545405Smsmith	    mrd->mr_flags |= MDF_FIRMWARE;
57645405Smsmith    }
57745405Smsmith}
57845405Smsmith
57946215Smsmith/*
58046215Smsmith * Initialise MTRRs on an AP after the BSP has run the init code.
58146215Smsmith */
58245405Smsmithstatic void
58346215Smsmithi686_mrAPinit(struct mem_range_softc *sc)
58446215Smsmith{
58548925Smsmith    i686_mrstoreone((void *)sc);	/* set MTRRs to match BSP */
58646215Smsmith    wrmsr(MSR_MTRRdefType, mtrrdef);	/* set MTRR behaviour to match BSP */
58746215Smsmith}
58846215Smsmith
58946215Smsmithstatic void
59045405Smsmithi686_mem_drvinit(void *unused)
59145405Smsmith{
59245405Smsmith    /* Try for i686 MTRRs */
59352177Sgreen    if ((cpu_feature & CPUID_MTRR) &&
59452177Sgreen	((cpu_id & 0xf00) == 0x600) &&
59552177Sgreen	((strcmp(cpu_vendor, "GenuineIntel") == 0) ||
59652177Sgreen	(strcmp(cpu_vendor, "AuthenticAMD") == 0))) {
59745405Smsmith	mem_range_softc.mr_op = &i686_mrops;
59845405Smsmith    }
59945405Smsmith}
60045405Smsmith
60145405SmsmithSYSINIT(i686memdev,SI_SUB_DRIVERS,SI_ORDER_FIRST,i686_mem_drvinit,NULL)
60245405Smsmith
60345405Smsmith
604