x86_mem.c revision 115683
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 *
2645405Smsmith */
2745405Smsmith
28115683Sobrien#include <sys/cdefs.h>
29115683Sobrien__FBSDID("$FreeBSD: head/sys/i386/i386/i686_mem.c 115683 2003-06-02 06:43:15Z obrien $");
30115683Sobrien
3145405Smsmith#include <sys/param.h>
3245405Smsmith#include <sys/kernel.h>
3345405Smsmith#include <sys/systm.h>
3445405Smsmith#include <sys/malloc.h>
3545405Smsmith#include <sys/memrange.h>
3676078Sjhb#include <sys/smp.h>
37106842Smdodd#include <sys/sysctl.h>
3845405Smsmith
3945405Smsmith#include <machine/md_var.h>
4045405Smsmith#include <machine/specialreg.h>
4145405Smsmith
4245405Smsmith/*
4345405Smsmith * i686 memory range operations
4445405Smsmith *
4545405Smsmith * This code will probably be impenetrable without reference to the
4645405Smsmith * Intel Pentium Pro documentation.
4745405Smsmith */
4845405Smsmith
4945405Smsmithstatic char *mem_owner_bios = "BIOS";
5045405Smsmith
5145405Smsmith#define MR686_FIXMTRR	(1<<0)
5245405Smsmith
5345405Smsmith#define mrwithin(mr, a) \
5445405Smsmith    (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
5545405Smsmith#define mroverlap(mra, mrb) \
5645405Smsmith    (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
5745405Smsmith
5845405Smsmith#define mrvalid(base, len) 						\
5945405Smsmith    ((!(base & ((1 << 12) - 1))) && 	/* base is multiple of 4k */	\
6045405Smsmith     ((len) >= (1 << 12)) && 		/* length is >= 4k */		\
6145405Smsmith     powerof2((len)) && 		/* ... and power of two */	\
6245405Smsmith     !((base) & ((len) - 1)))		/* range is not discontiuous */
6345405Smsmith
6445405Smsmith#define mrcopyflags(curr, new) (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
6545405Smsmith
66106842Smdoddstatic int			mtrrs_disabled;
67106842SmdoddTUNABLE_INT("machdep.disable_mtrrs", &mtrrs_disabled);
68106842SmdoddSYSCTL_INT(_machdep, OID_AUTO, disable_mtrrs, CTLFLAG_RD,
69106842Smdodd	&mtrrs_disabled, 0, "Disable i686 MTRRs.");
70106842Smdodd
7146215Smsmithstatic void			i686_mrinit(struct mem_range_softc *sc);
7246215Smsmithstatic int			i686_mrset(struct mem_range_softc *sc,
7346215Smsmith					   struct mem_range_desc *mrd,
7446215Smsmith					   int *arg);
7546215Smsmithstatic void			i686_mrAPinit(struct mem_range_softc *sc);
7645405Smsmith
7745405Smsmithstatic struct mem_range_ops i686_mrops = {
7845405Smsmith    i686_mrinit,
7946215Smsmith    i686_mrset,
8046215Smsmith    i686_mrAPinit
8145405Smsmith};
8245405Smsmith
8346215Smsmith/* XXX for AP startup hook */
8446215Smsmithstatic u_int64_t		mtrrcap, mtrrdef;
8546215Smsmith
8645405Smsmithstatic struct mem_range_desc	*mem_range_match(struct mem_range_softc *sc,
8745405Smsmith						 struct mem_range_desc *mrd);
8845405Smsmithstatic void			i686_mrfetch(struct mem_range_softc *sc);
8945405Smsmithstatic int			i686_mtrrtype(int flags);
9094683Sdwmalonestatic int			i686_mrt2mtrr(int flags, int oldval);
9194683Sdwmalonestatic int			i686_mtrrconflict(int flag1, int flag2);
9248925Smsmithstatic void			i686_mrstore(struct mem_range_softc *sc);
9348925Smsmithstatic void			i686_mrstoreone(void *arg);
9445405Smsmithstatic struct mem_range_desc	*i686_mtrrfixsearch(struct mem_range_softc *sc,
9545405Smsmith						    u_int64_t addr);
9645405Smsmithstatic int			i686_mrsetlow(struct mem_range_softc *sc,
9745405Smsmith					      struct mem_range_desc *mrd,
9845405Smsmith					      int *arg);
9945405Smsmithstatic int			i686_mrsetvariable(struct mem_range_softc *sc,
10045405Smsmith						   struct mem_range_desc *mrd,
10145405Smsmith						   int *arg);
10245405Smsmith
10345405Smsmith/* i686 MTRR type to memory range type conversion */
10445405Smsmithstatic int i686_mtrrtomrt[] = {
10545405Smsmith    MDF_UNCACHEABLE,
10645405Smsmith    MDF_WRITECOMBINE,
10794683Sdwmalone    MDF_UNKNOWN,
10894683Sdwmalone    MDF_UNKNOWN,
10945405Smsmith    MDF_WRITETHROUGH,
11045405Smsmith    MDF_WRITEPROTECT,
11145405Smsmith    MDF_WRITEBACK
11245405Smsmith};
11345405Smsmith
11494683Sdwmalone#define MTRRTOMRTLEN (sizeof(i686_mtrrtomrt) / sizeof(i686_mtrrtomrt[0]))
11594683Sdwmalone
11694683Sdwmalonestatic int
11794683Sdwmalonei686_mtrr2mrt(int val) {
11894683Sdwmalone	if (val < 0 || val >= MTRRTOMRTLEN)
11994683Sdwmalone		return MDF_UNKNOWN;
12094683Sdwmalone	return i686_mtrrtomrt[val];
12194683Sdwmalone}
12294683Sdwmalone
12348925Smsmith/*
12494683Sdwmalone * i686 MTRR conflicts. Writeback and uncachable may overlap.
12548925Smsmith */
12694683Sdwmalonestatic int
12794683Sdwmalonei686_mtrrconflict(int flag1, int flag2) {
12894683Sdwmalone	flag1 &= MDF_ATTRMASK;
12994683Sdwmalone	flag2 &= MDF_ATTRMASK;
13094683Sdwmalone	if (flag1 == flag2 ||
13194683Sdwmalone	    (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
13294683Sdwmalone	    (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
13394683Sdwmalone		return 0;
13494683Sdwmalone	return 1;
13594683Sdwmalone}
13645405Smsmith
13745405Smsmith/*
13845405Smsmith * Look for an exactly-matching range.
13945405Smsmith */
14045405Smsmithstatic struct mem_range_desc *
14145405Smsmithmem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
14245405Smsmith{
14345405Smsmith    struct mem_range_desc	*cand;
14445405Smsmith    int				i;
14545405Smsmith
14645405Smsmith    for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
14745405Smsmith	if ((cand->mr_base == mrd->mr_base) &&
14845405Smsmith	    (cand->mr_len == mrd->mr_len))
14945405Smsmith	    return(cand);
15045405Smsmith    return(NULL);
15145405Smsmith}
15245405Smsmith
15345405Smsmith/*
15445405Smsmith * Fetch the current mtrr settings from the current CPU (assumed to all
15545405Smsmith * be in sync in the SMP case).  Note that if we are here, we assume
15645405Smsmith * that MTRRs are enabled, and we may or may not have fixed MTRRs.
15745405Smsmith */
15845405Smsmithstatic void
15945405Smsmithi686_mrfetch(struct mem_range_softc *sc)
16045405Smsmith{
16145405Smsmith    struct mem_range_desc	*mrd;
16245405Smsmith    u_int64_t			msrv;
16345405Smsmith    int				i, j, msr;
16445405Smsmith
16545405Smsmith    mrd = sc->mr_desc;
16645405Smsmith
16745405Smsmith    /* Get fixed-range MTRRs */
16845405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
16945405Smsmith	msr = MSR_MTRR64kBase;
17045405Smsmith	for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
17145405Smsmith	    msrv = rdmsr(msr);
17245405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
17345405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
17494683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
17545405Smsmith		    MDF_ACTIVE;
17645405Smsmith		if (mrd->mr_owner[0] == 0)
17745405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
17845405Smsmith		msrv = msrv >> 8;
17945405Smsmith	    }
18045405Smsmith	}
18145405Smsmith	msr = MSR_MTRR16kBase;
18245405Smsmith	for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
18345405Smsmith	    msrv = rdmsr(msr);
18445405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
18545405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
18694683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
18745405Smsmith		    MDF_ACTIVE;
18845405Smsmith		if (mrd->mr_owner[0] == 0)
18945405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
19045405Smsmith		msrv = msrv >> 8;
19145405Smsmith	    }
19245405Smsmith	}
19345405Smsmith	msr = MSR_MTRR4kBase;
19445405Smsmith	for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
19545405Smsmith	    msrv = rdmsr(msr);
19645405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
19745405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
19894683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
19945405Smsmith		    MDF_ACTIVE;
20045405Smsmith		if (mrd->mr_owner[0] == 0)
20145405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
20245405Smsmith		msrv = msrv >> 8;
20345405Smsmith	    }
20445405Smsmith	}
20545405Smsmith    }
20645405Smsmith
20745405Smsmith    /* Get remainder which must be variable MTRRs */
20845405Smsmith    msr = MSR_MTRRVarBase;
20945405Smsmith    for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
21045405Smsmith	msrv = rdmsr(msr);
21145405Smsmith	mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
21294683Sdwmalone	    i686_mtrr2mrt(msrv & 0xff);
21345405Smsmith	mrd->mr_base = msrv & 0x0000000ffffff000LL;
21445405Smsmith	msrv = rdmsr(msr + 1);
21545405Smsmith	mrd->mr_flags = (msrv & 0x800) ?
21645405Smsmith	    (mrd->mr_flags | MDF_ACTIVE) :
21745405Smsmith	    (mrd->mr_flags & ~MDF_ACTIVE);
21845405Smsmith	/* Compute the range from the mask. Ick. */
21945405Smsmith	mrd->mr_len = (~(msrv & 0x0000000ffffff000LL) & 0x0000000fffffffffLL) + 1;
22045405Smsmith	if (!mrvalid(mrd->mr_base, mrd->mr_len))
22145405Smsmith	    mrd->mr_flags |= MDF_BOGUS;
22245405Smsmith	/* If unclaimed and active, must be the BIOS */
22345405Smsmith	if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
22445405Smsmith	    strcpy(mrd->mr_owner, mem_owner_bios);
22545405Smsmith    }
22645405Smsmith}
22745405Smsmith
22845405Smsmith/*
22945405Smsmith * Return the MTRR memory type matching a region's flags
23045405Smsmith */
23145405Smsmithstatic int
23245405Smsmithi686_mtrrtype(int flags)
23345405Smsmith{
23445405Smsmith    int		i;
23545405Smsmith
23645405Smsmith    flags &= MDF_ATTRMASK;
23745405Smsmith
23894683Sdwmalone    for (i = 0; i < MTRRTOMRTLEN; i++) {
23994683Sdwmalone	if (i686_mtrrtomrt[i] == MDF_UNKNOWN)
24045405Smsmith	    continue;
24145405Smsmith	if (flags == i686_mtrrtomrt[i])
24245405Smsmith	    return(i);
24345405Smsmith    }
24445405Smsmith    return(-1);
24545405Smsmith}
24645405Smsmith
24794683Sdwmalonestatic int
24894683Sdwmalonei686_mrt2mtrr(int flags, int oldval)
24994683Sdwmalone{
25094683Sdwmalone	int val;
25194683Sdwmalone
25294683Sdwmalone	if ((val = i686_mtrrtype(flags)) == -1)
25394683Sdwmalone		return oldval & 0xff;
25494683Sdwmalone	return val & 0xff;
25594683Sdwmalone}
25694683Sdwmalone
25745405Smsmith/*
25846215Smsmith * Update running CPU(s) MTRRs to match the ranges in the descriptor
25946215Smsmith * list.
26046215Smsmith *
26146215Smsmith * XXX Must be called with interrupts enabled.
26245405Smsmith */
26348925Smsmithstatic void
26445405Smsmithi686_mrstore(struct mem_range_softc *sc)
26545405Smsmith{
26645405Smsmith#ifdef SMP
26745405Smsmith    /*
26875421Sjhb     * We should use ipi_all_but_self() to call other CPUs into a
26945405Smsmith     * locking gate, then call a target function to do this work.
27045405Smsmith     * The "proper" solution involves a generalised locking gate
27145405Smsmith     * implementation, not ready yet.
27245405Smsmith     */
27348925Smsmith    smp_rendezvous(NULL, i686_mrstoreone, NULL, (void *)sc);
27448925Smsmith#else
27545405Smsmith    disable_intr();				/* disable interrupts */
27649421Smsmith    i686_mrstoreone((void *)sc);
27746215Smsmith    enable_intr();
27848925Smsmith#endif
27946215Smsmith}
28046215Smsmith
28146215Smsmith/*
28246215Smsmith * Update the current CPU's MTRRs with those represented in the
28348925Smsmith * descriptor list.  Note that we do this wholesale rather than
28448925Smsmith * just stuffing one entry; this is simpler (but slower, of course).
28546215Smsmith */
28648925Smsmithstatic void
28748925Smsmithi686_mrstoreone(void *arg)
28846215Smsmith{
28948925Smsmith    struct mem_range_softc 	*sc = (struct mem_range_softc *)arg;
29046215Smsmith    struct mem_range_desc	*mrd;
29194683Sdwmalone    u_int64_t			omsrv, msrv;
29246215Smsmith    int				i, j, msr;
29346215Smsmith    u_int			cr4save;
29446215Smsmith
29548925Smsmith    mrd = sc->mr_desc;
29646215Smsmith
29745405Smsmith    cr4save = rcr4();				/* save cr4 */
29845405Smsmith    if (cr4save & CR4_PGE)
29945405Smsmith	load_cr4(cr4save & ~CR4_PGE);
30045405Smsmith    load_cr0((rcr0() & ~CR0_NW) | CR0_CD);	/* disable caches (CD = 1, NW = 0) */
30148925Smsmith    wbinvd();					/* flush caches, TLBs */
30245405Smsmith    wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~0x800);	/* disable MTRRs (E = 0) */
30345405Smsmith
30445405Smsmith    /* Set fixed-range MTRRs */
30545405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
30645405Smsmith	msr = MSR_MTRR64kBase;
30745405Smsmith	for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
30845405Smsmith	    msrv = 0;
30994683Sdwmalone	    omsrv = rdmsr(msr);
31045405Smsmith	    for (j = 7; j >= 0; j--) {
31145405Smsmith		msrv = msrv << 8;
31294683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
31345405Smsmith	    }
31445405Smsmith	    wrmsr(msr, msrv);
31545405Smsmith	    mrd += 8;
31645405Smsmith	}
31745405Smsmith	msr = MSR_MTRR16kBase;
31845405Smsmith	for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
31945405Smsmith	    msrv = 0;
32094683Sdwmalone	    omsrv = rdmsr(msr);
32145405Smsmith	    for (j = 7; j >= 0; j--) {
32245405Smsmith		msrv = msrv << 8;
32394683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
32445405Smsmith	    }
32545405Smsmith	    wrmsr(msr, msrv);
32645405Smsmith	    mrd += 8;
32745405Smsmith	}
32845405Smsmith	msr = MSR_MTRR4kBase;
32945405Smsmith	for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
33045405Smsmith	    msrv = 0;
33194683Sdwmalone	    omsrv = rdmsr(msr);
33245405Smsmith	    for (j = 7; j >= 0; j--) {
33345405Smsmith		msrv = msrv << 8;
33494683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
33545405Smsmith	    }
33645405Smsmith	    wrmsr(msr, msrv);
33745405Smsmith	    mrd += 8;
33845405Smsmith	}
33945405Smsmith    }
34045405Smsmith
34145405Smsmith    /* Set remainder which must be variable MTRRs */
34245405Smsmith    msr = MSR_MTRRVarBase;
34345405Smsmith    for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
34445405Smsmith	/* base/type register */
34594683Sdwmalone	omsrv = rdmsr(msr);
34645405Smsmith	if (mrd->mr_flags & MDF_ACTIVE) {
34745405Smsmith	    msrv = mrd->mr_base & 0x0000000ffffff000LL;
34894683Sdwmalone	    msrv |= i686_mrt2mtrr(mrd->mr_flags, omsrv);
34945405Smsmith	} else {
35045405Smsmith	    msrv = 0;
35145405Smsmith	}
35245405Smsmith	wrmsr(msr, msrv);
35345405Smsmith
35445405Smsmith	/* mask/active register */
35545405Smsmith	if (mrd->mr_flags & MDF_ACTIVE) {
35645405Smsmith	    msrv = 0x800 | (~(mrd->mr_len - 1) & 0x0000000ffffff000LL);
35745405Smsmith	} else {
35845405Smsmith	    msrv = 0;
35945405Smsmith	}
36045405Smsmith	wrmsr(msr + 1, msrv);
36145405Smsmith    }
36248925Smsmith    wbinvd();							/* flush caches, TLBs */
36345405Smsmith    wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | 0x800);	/* restore MTRR state */
36445405Smsmith    load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  			/* enable caches CD = 0 and NW = 0 */
36545405Smsmith    load_cr4(cr4save);						/* restore cr4 */
36645405Smsmith}
36745405Smsmith
36845405Smsmith/*
36945405Smsmith * Hunt for the fixed MTRR referencing (addr)
37045405Smsmith */
37145405Smsmithstatic struct mem_range_desc *
37245405Smsmithi686_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
37345405Smsmith{
37445405Smsmith    struct mem_range_desc *mrd;
37545405Smsmith    int			i;
37645405Smsmith
37745405Smsmith    for (i = 0, mrd = sc->mr_desc; i < (MTRR_N64K + MTRR_N16K + MTRR_N4K); i++, mrd++)
37845405Smsmith	if ((addr >= mrd->mr_base) && (addr < (mrd->mr_base + mrd->mr_len)))
37945405Smsmith	    return(mrd);
38045405Smsmith    return(NULL);
38145405Smsmith}
38245405Smsmith
38345405Smsmith/*
38445405Smsmith * Try to satisfy the given range request by manipulating the fixed MTRRs that
38545405Smsmith * cover low memory.
38645405Smsmith *
38745405Smsmith * Note that we try to be generous here; we'll bloat the range out to the
38845405Smsmith * next higher/lower boundary to avoid the consumer having to know too much
38945405Smsmith * about the mechanisms here.
39045405Smsmith *
39145405Smsmith * XXX note that this will have to be updated when we start supporting "busy" ranges.
39245405Smsmith */
39345405Smsmithstatic int
39445405Smsmithi686_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
39545405Smsmith{
39645405Smsmith    struct mem_range_desc	*first_md, *last_md, *curr_md;
39745405Smsmith
39845405Smsmith    /* range check */
39945405Smsmith    if (((first_md = i686_mtrrfixsearch(sc, mrd->mr_base)) == NULL) ||
40045405Smsmith	((last_md = i686_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1)) == NULL))
40145405Smsmith	return(EINVAL);
40245405Smsmith
403103346Sdwmalone    /* check we aren't doing something risky */
404103346Sdwmalone    if (!(mrd->mr_flags & MDF_FORCE))
405103346Sdwmalone	for (curr_md = first_md; curr_md <= last_md; curr_md++) {
406103346Sdwmalone	    if ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN)
407103346Sdwmalone		return (EACCES);
408103346Sdwmalone	}
409103346Sdwmalone
41045405Smsmith    /* set flags, clear set-by-firmware flag */
41145405Smsmith    for (curr_md = first_md; curr_md <= last_md; curr_md++) {
41245405Smsmith	curr_md->mr_flags = mrcopyflags(curr_md->mr_flags & ~MDF_FIRMWARE, mrd->mr_flags);
41345405Smsmith	bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
41445405Smsmith    }
41545405Smsmith
41645405Smsmith    return(0);
41745405Smsmith}
41845405Smsmith
41945405Smsmith
42045405Smsmith/*
42145405Smsmith * Modify/add a variable MTRR to satisfy the request.
42245405Smsmith *
42345405Smsmith * XXX needs to be updated to properly support "busy" ranges.
42445405Smsmith */
42545405Smsmithstatic int
42645405Smsmithi686_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
42745405Smsmith{
42845405Smsmith    struct mem_range_desc	*curr_md, *free_md;
42945405Smsmith    int				i;
43045405Smsmith
43145405Smsmith    /*
43245405Smsmith     * Scan the currently active variable descriptors, look for
43345405Smsmith     * one we exactly match (straight takeover) and for possible
43445405Smsmith     * accidental overlaps.
43545405Smsmith     * Keep track of the first empty variable descriptor in case we
43645405Smsmith     * can't perform a takeover.
43745405Smsmith     */
43845405Smsmith    i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
43945405Smsmith    curr_md = sc->mr_desc + i;
44045405Smsmith    free_md = NULL;
44145405Smsmith    for (; i < sc->mr_ndesc; i++, curr_md++) {
44245405Smsmith	if (curr_md->mr_flags & MDF_ACTIVE) {
44345405Smsmith	    /* exact match? */
44445405Smsmith	    if ((curr_md->mr_base == mrd->mr_base) &&
44545405Smsmith		(curr_md->mr_len == mrd->mr_len)) {
44645405Smsmith		/* whoops, owned by someone */
44745405Smsmith		if (curr_md->mr_flags & MDF_BUSY)
44845405Smsmith		    return(EBUSY);
449103346Sdwmalone		/* check we aren't doing something risky */
450103346Sdwmalone		if (!(mrd->mr_flags & MDF_FORCE) &&
451103346Sdwmalone		  ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN))
452103346Sdwmalone		    return (EACCES);
45345405Smsmith		/* Ok, just hijack this entry */
45445405Smsmith		free_md = curr_md;
45545405Smsmith		break;
45645405Smsmith	    }
45748925Smsmith	    /* non-exact overlap ? */
45848925Smsmith	    if (mroverlap(curr_md, mrd)) {
45948925Smsmith		/* between conflicting region types? */
46094683Sdwmalone		if (i686_mtrrconflict(curr_md->mr_flags, mrd->mr_flags))
46148925Smsmith		    return(EINVAL);
46248925Smsmith	    }
46345405Smsmith	} else if (free_md == NULL) {
46445405Smsmith	    free_md = curr_md;
46545405Smsmith	}
46645405Smsmith    }
46745405Smsmith    /* got somewhere to put it? */
46845405Smsmith    if (free_md == NULL)
46945405Smsmith	return(ENOSPC);
47045405Smsmith
47145405Smsmith    /* Set up new descriptor */
47245405Smsmith    free_md->mr_base = mrd->mr_base;
47345405Smsmith    free_md->mr_len = mrd->mr_len;
47445405Smsmith    free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
47545405Smsmith    bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
47645405Smsmith    return(0);
47745405Smsmith}
47845405Smsmith
47945405Smsmith/*
48045405Smsmith * Handle requests to set memory range attributes by manipulating MTRRs.
48145405Smsmith *
48245405Smsmith */
48345405Smsmithstatic int
48445405Smsmithi686_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
48545405Smsmith{
48645405Smsmith    struct mem_range_desc	*targ;
48745405Smsmith    int				error = 0;
48845405Smsmith
48945405Smsmith    switch(*arg) {
49045405Smsmith    case MEMRANGE_SET_UPDATE:
49145405Smsmith	/* make sure that what's being asked for is even possible at all */
49245405Smsmith	if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
49394683Sdwmalone	    i686_mtrrtype(mrd->mr_flags) == -1)
49445405Smsmith	    return(EINVAL);
49545405Smsmith
49645405Smsmith#define FIXTOP	((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
49745405Smsmith
49845405Smsmith	/* are the "low memory" conditions applicable? */
49945405Smsmith	if ((sc->mr_cap & MR686_FIXMTRR) &&
50045405Smsmith	    ((mrd->mr_base + mrd->mr_len) <= FIXTOP)) {
50145405Smsmith	    if ((error = i686_mrsetlow(sc, mrd, arg)) != 0)
50245405Smsmith		return(error);
50345405Smsmith	} else {
50445405Smsmith	    /* it's time to play with variable MTRRs */
50545405Smsmith	    if ((error = i686_mrsetvariable(sc, mrd, arg)) != 0)
50645405Smsmith		return(error);
50745405Smsmith	}
50845405Smsmith	break;
50945405Smsmith
51045405Smsmith    case MEMRANGE_SET_REMOVE:
51145405Smsmith	if ((targ = mem_range_match(sc, mrd)) == NULL)
51245405Smsmith	    return(ENOENT);
51345405Smsmith	if (targ->mr_flags & MDF_FIXACTIVE)
51445405Smsmith	    return(EPERM);
51545405Smsmith	if (targ->mr_flags & MDF_BUSY)
51645405Smsmith	    return(EBUSY);
51745405Smsmith	targ->mr_flags &= ~MDF_ACTIVE;
51845405Smsmith	targ->mr_owner[0] = 0;
51945405Smsmith	break;
52045405Smsmith
52145405Smsmith    default:
52245405Smsmith	return(EOPNOTSUPP);
52345405Smsmith    }
52445405Smsmith
52545405Smsmith    /* update the hardware */
52648925Smsmith    i686_mrstore(sc);
52745405Smsmith    i686_mrfetch(sc);	/* refetch to see where we're at */
52848925Smsmith    return(0);
52945405Smsmith}
53045405Smsmith
53145405Smsmith/*
53245405Smsmith * Work out how many ranges we support, initialise storage for them,
53345405Smsmith * fetch the initial settings.
53445405Smsmith */
53545405Smsmithstatic void
53645405Smsmithi686_mrinit(struct mem_range_softc *sc)
53745405Smsmith{
53845405Smsmith    struct mem_range_desc	*mrd;
53945405Smsmith    int				nmdesc = 0;
54045405Smsmith    int				i;
54145405Smsmith
54245405Smsmith    mtrrcap = rdmsr(MSR_MTRRcap);
54345405Smsmith    mtrrdef = rdmsr(MSR_MTRRdefType);
54445405Smsmith
54545405Smsmith    /* For now, bail out if MTRRs are not enabled */
54645405Smsmith    if (!(mtrrdef & 0x800)) {
54745405Smsmith	if (bootverbose)
54845405Smsmith	    printf("CPU supports MTRRs but not enabled\n");
54945405Smsmith	return;
55045405Smsmith    }
55145405Smsmith    nmdesc = mtrrcap & 0xff;
55248925Smsmith    printf("Pentium Pro MTRR support enabled\n");
55345405Smsmith
55445405Smsmith    /* If fixed MTRRs supported and enabled */
55545405Smsmith    if ((mtrrcap & 0x100) && (mtrrdef & 0x400)) {
55645405Smsmith	sc->mr_cap = MR686_FIXMTRR;
55745405Smsmith	nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
55845405Smsmith    }
55945405Smsmith
56045405Smsmith    sc->mr_desc =
56145405Smsmith	(struct mem_range_desc *)malloc(nmdesc * sizeof(struct mem_range_desc),
562111119Simp					M_MEMDESC, M_WAITOK | M_ZERO);
56345405Smsmith    sc->mr_ndesc = nmdesc;
56445405Smsmith
56545405Smsmith    mrd = sc->mr_desc;
56645405Smsmith
56745405Smsmith    /* Populate the fixed MTRR entries' base/length */
56845405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
56945405Smsmith	for (i = 0; i < MTRR_N64K; i++, mrd++) {
57045405Smsmith	    mrd->mr_base = i * 0x10000;
57145405Smsmith	    mrd->mr_len = 0x10000;
57245405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
57345405Smsmith	}
57445405Smsmith	for (i = 0; i < MTRR_N16K; i++, mrd++) {
57545405Smsmith	    mrd->mr_base = i * 0x4000 + 0x80000;
57645405Smsmith	    mrd->mr_len = 0x4000;
57745405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
57845405Smsmith	}
57945405Smsmith	for (i = 0; i < MTRR_N4K; i++, mrd++) {
58045405Smsmith	    mrd->mr_base = i * 0x1000 + 0xc0000;
58145405Smsmith	    mrd->mr_len = 0x1000;
58245405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
58345405Smsmith	}
58445405Smsmith    }
58545405Smsmith
58645405Smsmith    /*
58745405Smsmith     * Get current settings, anything set now is considered to have
58845405Smsmith     * been set by the firmware. (XXX has something already played here?)
58945405Smsmith     */
59045405Smsmith    i686_mrfetch(sc);
59145405Smsmith    mrd = sc->mr_desc;
59245405Smsmith    for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
59345405Smsmith	if (mrd->mr_flags & MDF_ACTIVE)
59445405Smsmith	    mrd->mr_flags |= MDF_FIRMWARE;
59545405Smsmith    }
59645405Smsmith}
59745405Smsmith
59846215Smsmith/*
59946215Smsmith * Initialise MTRRs on an AP after the BSP has run the init code.
60046215Smsmith */
60145405Smsmithstatic void
60246215Smsmithi686_mrAPinit(struct mem_range_softc *sc)
60346215Smsmith{
60448925Smsmith    i686_mrstoreone((void *)sc);	/* set MTRRs to match BSP */
60546215Smsmith    wrmsr(MSR_MTRRdefType, mtrrdef);	/* set MTRR behaviour to match BSP */
60646215Smsmith}
60746215Smsmith
60846215Smsmithstatic void
60945405Smsmithi686_mem_drvinit(void *unused)
61045405Smsmith{
61145405Smsmith    /* Try for i686 MTRRs */
612106842Smdodd    if (!mtrrs_disabled && (cpu_feature & CPUID_MTRR) &&
613103649Smdodd	((cpu_id & 0xf00) == 0x600 || (cpu_id & 0xf00) == 0xf00) &&
61452177Sgreen	((strcmp(cpu_vendor, "GenuineIntel") == 0) ||
61552177Sgreen	(strcmp(cpu_vendor, "AuthenticAMD") == 0))) {
61645405Smsmith	mem_range_softc.mr_op = &i686_mrops;
61745405Smsmith    }
61845405Smsmith}
61945405Smsmith
62045405SmsmithSYSINIT(i686memdev,SI_SUB_DRIVERS,SI_ORDER_FIRST,i686_mem_drvinit,NULL)
62145405Smsmith
62245405Smsmith
623