x86_mem.c revision 106842
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 106842 2002-11-13 09:37:43Z mdodd $
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>
35106842Smdodd#include <sys/sysctl.h>
3645405Smsmith
3745405Smsmith#include <machine/md_var.h>
3845405Smsmith#include <machine/specialreg.h>
3945405Smsmith
4045405Smsmith/*
4145405Smsmith * i686 memory range operations
4245405Smsmith *
4345405Smsmith * This code will probably be impenetrable without reference to the
4445405Smsmith * Intel Pentium Pro documentation.
4545405Smsmith */
4645405Smsmith
4745405Smsmithstatic char *mem_owner_bios = "BIOS";
4845405Smsmith
4945405Smsmith#define MR686_FIXMTRR	(1<<0)
5045405Smsmith
5145405Smsmith#define mrwithin(mr, a) \
5245405Smsmith    (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
5345405Smsmith#define mroverlap(mra, mrb) \
5445405Smsmith    (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
5545405Smsmith
5645405Smsmith#define mrvalid(base, len) 						\
5745405Smsmith    ((!(base & ((1 << 12) - 1))) && 	/* base is multiple of 4k */	\
5845405Smsmith     ((len) >= (1 << 12)) && 		/* length is >= 4k */		\
5945405Smsmith     powerof2((len)) && 		/* ... and power of two */	\
6045405Smsmith     !((base) & ((len) - 1)))		/* range is not discontiuous */
6145405Smsmith
6245405Smsmith#define mrcopyflags(curr, new) (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
6345405Smsmith
64106842Smdoddstatic int			mtrrs_disabled;
65106842SmdoddTUNABLE_INT("machdep.disable_mtrrs", &mtrrs_disabled);
66106842SmdoddSYSCTL_INT(_machdep, OID_AUTO, disable_mtrrs, CTLFLAG_RD,
67106842Smdodd	&mtrrs_disabled, 0, "Disable i686 MTRRs.");
68106842Smdodd
6946215Smsmithstatic void			i686_mrinit(struct mem_range_softc *sc);
7046215Smsmithstatic int			i686_mrset(struct mem_range_softc *sc,
7146215Smsmith					   struct mem_range_desc *mrd,
7246215Smsmith					   int *arg);
7346215Smsmithstatic void			i686_mrAPinit(struct mem_range_softc *sc);
7445405Smsmith
7545405Smsmithstatic struct mem_range_ops i686_mrops = {
7645405Smsmith    i686_mrinit,
7746215Smsmith    i686_mrset,
7846215Smsmith    i686_mrAPinit
7945405Smsmith};
8045405Smsmith
8146215Smsmith/* XXX for AP startup hook */
8246215Smsmithstatic u_int64_t		mtrrcap, mtrrdef;
8346215Smsmith
8445405Smsmithstatic struct mem_range_desc	*mem_range_match(struct mem_range_softc *sc,
8545405Smsmith						 struct mem_range_desc *mrd);
8645405Smsmithstatic void			i686_mrfetch(struct mem_range_softc *sc);
8745405Smsmithstatic int			i686_mtrrtype(int flags);
8894683Sdwmalonestatic int			i686_mrt2mtrr(int flags, int oldval);
8994683Sdwmalonestatic int			i686_mtrrconflict(int flag1, int flag2);
9048925Smsmithstatic void			i686_mrstore(struct mem_range_softc *sc);
9148925Smsmithstatic void			i686_mrstoreone(void *arg);
9245405Smsmithstatic struct mem_range_desc	*i686_mtrrfixsearch(struct mem_range_softc *sc,
9345405Smsmith						    u_int64_t addr);
9445405Smsmithstatic int			i686_mrsetlow(struct mem_range_softc *sc,
9545405Smsmith					      struct mem_range_desc *mrd,
9645405Smsmith					      int *arg);
9745405Smsmithstatic int			i686_mrsetvariable(struct mem_range_softc *sc,
9845405Smsmith						   struct mem_range_desc *mrd,
9945405Smsmith						   int *arg);
10045405Smsmith
10145405Smsmith/* i686 MTRR type to memory range type conversion */
10245405Smsmithstatic int i686_mtrrtomrt[] = {
10345405Smsmith    MDF_UNCACHEABLE,
10445405Smsmith    MDF_WRITECOMBINE,
10594683Sdwmalone    MDF_UNKNOWN,
10694683Sdwmalone    MDF_UNKNOWN,
10745405Smsmith    MDF_WRITETHROUGH,
10845405Smsmith    MDF_WRITEPROTECT,
10945405Smsmith    MDF_WRITEBACK
11045405Smsmith};
11145405Smsmith
11294683Sdwmalone#define MTRRTOMRTLEN (sizeof(i686_mtrrtomrt) / sizeof(i686_mtrrtomrt[0]))
11394683Sdwmalone
11494683Sdwmalonestatic int
11594683Sdwmalonei686_mtrr2mrt(int val) {
11694683Sdwmalone	if (val < 0 || val >= MTRRTOMRTLEN)
11794683Sdwmalone		return MDF_UNKNOWN;
11894683Sdwmalone	return i686_mtrrtomrt[val];
11994683Sdwmalone}
12094683Sdwmalone
12148925Smsmith/*
12294683Sdwmalone * i686 MTRR conflicts. Writeback and uncachable may overlap.
12348925Smsmith */
12494683Sdwmalonestatic int
12594683Sdwmalonei686_mtrrconflict(int flag1, int flag2) {
12694683Sdwmalone	flag1 &= MDF_ATTRMASK;
12794683Sdwmalone	flag2 &= MDF_ATTRMASK;
12894683Sdwmalone	if (flag1 == flag2 ||
12994683Sdwmalone	    (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
13094683Sdwmalone	    (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
13194683Sdwmalone		return 0;
13294683Sdwmalone	return 1;
13394683Sdwmalone}
13445405Smsmith
13545405Smsmith/*
13645405Smsmith * Look for an exactly-matching range.
13745405Smsmith */
13845405Smsmithstatic struct mem_range_desc *
13945405Smsmithmem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
14045405Smsmith{
14145405Smsmith    struct mem_range_desc	*cand;
14245405Smsmith    int				i;
14345405Smsmith
14445405Smsmith    for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
14545405Smsmith	if ((cand->mr_base == mrd->mr_base) &&
14645405Smsmith	    (cand->mr_len == mrd->mr_len))
14745405Smsmith	    return(cand);
14845405Smsmith    return(NULL);
14945405Smsmith}
15045405Smsmith
15145405Smsmith/*
15245405Smsmith * Fetch the current mtrr settings from the current CPU (assumed to all
15345405Smsmith * be in sync in the SMP case).  Note that if we are here, we assume
15445405Smsmith * that MTRRs are enabled, and we may or may not have fixed MTRRs.
15545405Smsmith */
15645405Smsmithstatic void
15745405Smsmithi686_mrfetch(struct mem_range_softc *sc)
15845405Smsmith{
15945405Smsmith    struct mem_range_desc	*mrd;
16045405Smsmith    u_int64_t			msrv;
16145405Smsmith    int				i, j, msr;
16245405Smsmith
16345405Smsmith    mrd = sc->mr_desc;
16445405Smsmith
16545405Smsmith    /* Get fixed-range MTRRs */
16645405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
16745405Smsmith	msr = MSR_MTRR64kBase;
16845405Smsmith	for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
16945405Smsmith	    msrv = rdmsr(msr);
17045405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
17145405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
17294683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
17345405Smsmith		    MDF_ACTIVE;
17445405Smsmith		if (mrd->mr_owner[0] == 0)
17545405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
17645405Smsmith		msrv = msrv >> 8;
17745405Smsmith	    }
17845405Smsmith	}
17945405Smsmith	msr = MSR_MTRR16kBase;
18045405Smsmith	for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
18145405Smsmith	    msrv = rdmsr(msr);
18245405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
18345405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
18494683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
18545405Smsmith		    MDF_ACTIVE;
18645405Smsmith		if (mrd->mr_owner[0] == 0)
18745405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
18845405Smsmith		msrv = msrv >> 8;
18945405Smsmith	    }
19045405Smsmith	}
19145405Smsmith	msr = MSR_MTRR4kBase;
19245405Smsmith	for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
19345405Smsmith	    msrv = rdmsr(msr);
19445405Smsmith	    for (j = 0; j < 8; j++, mrd++) {
19545405Smsmith		mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
19694683Sdwmalone		    i686_mtrr2mrt(msrv & 0xff) |
19745405Smsmith		    MDF_ACTIVE;
19845405Smsmith		if (mrd->mr_owner[0] == 0)
19945405Smsmith		    strcpy(mrd->mr_owner, mem_owner_bios);
20045405Smsmith		msrv = msrv >> 8;
20145405Smsmith	    }
20245405Smsmith	}
20345405Smsmith    }
20445405Smsmith
20545405Smsmith    /* Get remainder which must be variable MTRRs */
20645405Smsmith    msr = MSR_MTRRVarBase;
20745405Smsmith    for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
20845405Smsmith	msrv = rdmsr(msr);
20945405Smsmith	mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
21094683Sdwmalone	    i686_mtrr2mrt(msrv & 0xff);
21145405Smsmith	mrd->mr_base = msrv & 0x0000000ffffff000LL;
21245405Smsmith	msrv = rdmsr(msr + 1);
21345405Smsmith	mrd->mr_flags = (msrv & 0x800) ?
21445405Smsmith	    (mrd->mr_flags | MDF_ACTIVE) :
21545405Smsmith	    (mrd->mr_flags & ~MDF_ACTIVE);
21645405Smsmith	/* Compute the range from the mask. Ick. */
21745405Smsmith	mrd->mr_len = (~(msrv & 0x0000000ffffff000LL) & 0x0000000fffffffffLL) + 1;
21845405Smsmith	if (!mrvalid(mrd->mr_base, mrd->mr_len))
21945405Smsmith	    mrd->mr_flags |= MDF_BOGUS;
22045405Smsmith	/* If unclaimed and active, must be the BIOS */
22145405Smsmith	if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
22245405Smsmith	    strcpy(mrd->mr_owner, mem_owner_bios);
22345405Smsmith    }
22445405Smsmith}
22545405Smsmith
22645405Smsmith/*
22745405Smsmith * Return the MTRR memory type matching a region's flags
22845405Smsmith */
22945405Smsmithstatic int
23045405Smsmithi686_mtrrtype(int flags)
23145405Smsmith{
23245405Smsmith    int		i;
23345405Smsmith
23445405Smsmith    flags &= MDF_ATTRMASK;
23545405Smsmith
23694683Sdwmalone    for (i = 0; i < MTRRTOMRTLEN; i++) {
23794683Sdwmalone	if (i686_mtrrtomrt[i] == MDF_UNKNOWN)
23845405Smsmith	    continue;
23945405Smsmith	if (flags == i686_mtrrtomrt[i])
24045405Smsmith	    return(i);
24145405Smsmith    }
24245405Smsmith    return(-1);
24345405Smsmith}
24445405Smsmith
24594683Sdwmalonestatic int
24694683Sdwmalonei686_mrt2mtrr(int flags, int oldval)
24794683Sdwmalone{
24894683Sdwmalone	int val;
24994683Sdwmalone
25094683Sdwmalone	if ((val = i686_mtrrtype(flags)) == -1)
25194683Sdwmalone		return oldval & 0xff;
25294683Sdwmalone	return val & 0xff;
25394683Sdwmalone}
25494683Sdwmalone
25545405Smsmith/*
25646215Smsmith * Update running CPU(s) MTRRs to match the ranges in the descriptor
25746215Smsmith * list.
25846215Smsmith *
25946215Smsmith * XXX Must be called with interrupts enabled.
26045405Smsmith */
26148925Smsmithstatic void
26245405Smsmithi686_mrstore(struct mem_range_softc *sc)
26345405Smsmith{
26445405Smsmith#ifdef SMP
26545405Smsmith    /*
26675421Sjhb     * We should use ipi_all_but_self() to call other CPUs into a
26745405Smsmith     * locking gate, then call a target function to do this work.
26845405Smsmith     * The "proper" solution involves a generalised locking gate
26945405Smsmith     * implementation, not ready yet.
27045405Smsmith     */
27148925Smsmith    smp_rendezvous(NULL, i686_mrstoreone, NULL, (void *)sc);
27248925Smsmith#else
27345405Smsmith    disable_intr();				/* disable interrupts */
27449421Smsmith    i686_mrstoreone((void *)sc);
27546215Smsmith    enable_intr();
27648925Smsmith#endif
27746215Smsmith}
27846215Smsmith
27946215Smsmith/*
28046215Smsmith * Update the current CPU's MTRRs with those represented in the
28148925Smsmith * descriptor list.  Note that we do this wholesale rather than
28248925Smsmith * just stuffing one entry; this is simpler (but slower, of course).
28346215Smsmith */
28448925Smsmithstatic void
28548925Smsmithi686_mrstoreone(void *arg)
28646215Smsmith{
28748925Smsmith    struct mem_range_softc 	*sc = (struct mem_range_softc *)arg;
28846215Smsmith    struct mem_range_desc	*mrd;
28994683Sdwmalone    u_int64_t			omsrv, msrv;
29046215Smsmith    int				i, j, msr;
29146215Smsmith    u_int			cr4save;
29246215Smsmith
29348925Smsmith    mrd = sc->mr_desc;
29446215Smsmith
29545405Smsmith    cr4save = rcr4();				/* save cr4 */
29645405Smsmith    if (cr4save & CR4_PGE)
29745405Smsmith	load_cr4(cr4save & ~CR4_PGE);
29845405Smsmith    load_cr0((rcr0() & ~CR0_NW) | CR0_CD);	/* disable caches (CD = 1, NW = 0) */
29948925Smsmith    wbinvd();					/* flush caches, TLBs */
30045405Smsmith    wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~0x800);	/* disable MTRRs (E = 0) */
30145405Smsmith
30245405Smsmith    /* Set fixed-range MTRRs */
30345405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
30445405Smsmith	msr = MSR_MTRR64kBase;
30545405Smsmith	for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
30645405Smsmith	    msrv = 0;
30794683Sdwmalone	    omsrv = rdmsr(msr);
30845405Smsmith	    for (j = 7; j >= 0; j--) {
30945405Smsmith		msrv = msrv << 8;
31094683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
31145405Smsmith	    }
31245405Smsmith	    wrmsr(msr, msrv);
31345405Smsmith	    mrd += 8;
31445405Smsmith	}
31545405Smsmith	msr = MSR_MTRR16kBase;
31645405Smsmith	for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
31745405Smsmith	    msrv = 0;
31894683Sdwmalone	    omsrv = rdmsr(msr);
31945405Smsmith	    for (j = 7; j >= 0; j--) {
32045405Smsmith		msrv = msrv << 8;
32194683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
32245405Smsmith	    }
32345405Smsmith	    wrmsr(msr, msrv);
32445405Smsmith	    mrd += 8;
32545405Smsmith	}
32645405Smsmith	msr = MSR_MTRR4kBase;
32745405Smsmith	for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
32845405Smsmith	    msrv = 0;
32994683Sdwmalone	    omsrv = rdmsr(msr);
33045405Smsmith	    for (j = 7; j >= 0; j--) {
33145405Smsmith		msrv = msrv << 8;
33294683Sdwmalone		msrv |= i686_mrt2mtrr((mrd + j)->mr_flags, omsrv >> (j*8));
33345405Smsmith	    }
33445405Smsmith	    wrmsr(msr, msrv);
33545405Smsmith	    mrd += 8;
33645405Smsmith	}
33745405Smsmith    }
33845405Smsmith
33945405Smsmith    /* Set remainder which must be variable MTRRs */
34045405Smsmith    msr = MSR_MTRRVarBase;
34145405Smsmith    for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
34245405Smsmith	/* base/type register */
34394683Sdwmalone	omsrv = rdmsr(msr);
34445405Smsmith	if (mrd->mr_flags & MDF_ACTIVE) {
34545405Smsmith	    msrv = mrd->mr_base & 0x0000000ffffff000LL;
34694683Sdwmalone	    msrv |= i686_mrt2mtrr(mrd->mr_flags, omsrv);
34745405Smsmith	} else {
34845405Smsmith	    msrv = 0;
34945405Smsmith	}
35045405Smsmith	wrmsr(msr, msrv);
35145405Smsmith
35245405Smsmith	/* mask/active register */
35345405Smsmith	if (mrd->mr_flags & MDF_ACTIVE) {
35445405Smsmith	    msrv = 0x800 | (~(mrd->mr_len - 1) & 0x0000000ffffff000LL);
35545405Smsmith	} else {
35645405Smsmith	    msrv = 0;
35745405Smsmith	}
35845405Smsmith	wrmsr(msr + 1, msrv);
35945405Smsmith    }
36048925Smsmith    wbinvd();							/* flush caches, TLBs */
36145405Smsmith    wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | 0x800);	/* restore MTRR state */
36245405Smsmith    load_cr0(rcr0() & ~(CR0_CD | CR0_NW));  			/* enable caches CD = 0 and NW = 0 */
36345405Smsmith    load_cr4(cr4save);						/* restore cr4 */
36445405Smsmith}
36545405Smsmith
36645405Smsmith/*
36745405Smsmith * Hunt for the fixed MTRR referencing (addr)
36845405Smsmith */
36945405Smsmithstatic struct mem_range_desc *
37045405Smsmithi686_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
37145405Smsmith{
37245405Smsmith    struct mem_range_desc *mrd;
37345405Smsmith    int			i;
37445405Smsmith
37545405Smsmith    for (i = 0, mrd = sc->mr_desc; i < (MTRR_N64K + MTRR_N16K + MTRR_N4K); i++, mrd++)
37645405Smsmith	if ((addr >= mrd->mr_base) && (addr < (mrd->mr_base + mrd->mr_len)))
37745405Smsmith	    return(mrd);
37845405Smsmith    return(NULL);
37945405Smsmith}
38045405Smsmith
38145405Smsmith/*
38245405Smsmith * Try to satisfy the given range request by manipulating the fixed MTRRs that
38345405Smsmith * cover low memory.
38445405Smsmith *
38545405Smsmith * Note that we try to be generous here; we'll bloat the range out to the
38645405Smsmith * next higher/lower boundary to avoid the consumer having to know too much
38745405Smsmith * about the mechanisms here.
38845405Smsmith *
38945405Smsmith * XXX note that this will have to be updated when we start supporting "busy" ranges.
39045405Smsmith */
39145405Smsmithstatic int
39245405Smsmithi686_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
39345405Smsmith{
39445405Smsmith    struct mem_range_desc	*first_md, *last_md, *curr_md;
39545405Smsmith
39645405Smsmith    /* range check */
39745405Smsmith    if (((first_md = i686_mtrrfixsearch(sc, mrd->mr_base)) == NULL) ||
39845405Smsmith	((last_md = i686_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1)) == NULL))
39945405Smsmith	return(EINVAL);
40045405Smsmith
401103346Sdwmalone    /* check we aren't doing something risky */
402103346Sdwmalone    if (!(mrd->mr_flags & MDF_FORCE))
403103346Sdwmalone	for (curr_md = first_md; curr_md <= last_md; curr_md++) {
404103346Sdwmalone	    if ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN)
405103346Sdwmalone		return (EACCES);
406103346Sdwmalone	}
407103346Sdwmalone
40845405Smsmith    /* set flags, clear set-by-firmware flag */
40945405Smsmith    for (curr_md = first_md; curr_md <= last_md; curr_md++) {
41045405Smsmith	curr_md->mr_flags = mrcopyflags(curr_md->mr_flags & ~MDF_FIRMWARE, mrd->mr_flags);
41145405Smsmith	bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
41245405Smsmith    }
41345405Smsmith
41445405Smsmith    return(0);
41545405Smsmith}
41645405Smsmith
41745405Smsmith
41845405Smsmith/*
41945405Smsmith * Modify/add a variable MTRR to satisfy the request.
42045405Smsmith *
42145405Smsmith * XXX needs to be updated to properly support "busy" ranges.
42245405Smsmith */
42345405Smsmithstatic int
42445405Smsmithi686_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
42545405Smsmith{
42645405Smsmith    struct mem_range_desc	*curr_md, *free_md;
42745405Smsmith    int				i;
42845405Smsmith
42945405Smsmith    /*
43045405Smsmith     * Scan the currently active variable descriptors, look for
43145405Smsmith     * one we exactly match (straight takeover) and for possible
43245405Smsmith     * accidental overlaps.
43345405Smsmith     * Keep track of the first empty variable descriptor in case we
43445405Smsmith     * can't perform a takeover.
43545405Smsmith     */
43645405Smsmith    i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
43745405Smsmith    curr_md = sc->mr_desc + i;
43845405Smsmith    free_md = NULL;
43945405Smsmith    for (; i < sc->mr_ndesc; i++, curr_md++) {
44045405Smsmith	if (curr_md->mr_flags & MDF_ACTIVE) {
44145405Smsmith	    /* exact match? */
44245405Smsmith	    if ((curr_md->mr_base == mrd->mr_base) &&
44345405Smsmith		(curr_md->mr_len == mrd->mr_len)) {
44445405Smsmith		/* whoops, owned by someone */
44545405Smsmith		if (curr_md->mr_flags & MDF_BUSY)
44645405Smsmith		    return(EBUSY);
447103346Sdwmalone		/* check we aren't doing something risky */
448103346Sdwmalone		if (!(mrd->mr_flags & MDF_FORCE) &&
449103346Sdwmalone		  ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN))
450103346Sdwmalone		    return (EACCES);
45145405Smsmith		/* Ok, just hijack this entry */
45245405Smsmith		free_md = curr_md;
45345405Smsmith		break;
45445405Smsmith	    }
45548925Smsmith	    /* non-exact overlap ? */
45648925Smsmith	    if (mroverlap(curr_md, mrd)) {
45748925Smsmith		/* between conflicting region types? */
45894683Sdwmalone		if (i686_mtrrconflict(curr_md->mr_flags, mrd->mr_flags))
45948925Smsmith		    return(EINVAL);
46048925Smsmith	    }
46145405Smsmith	} else if (free_md == NULL) {
46245405Smsmith	    free_md = curr_md;
46345405Smsmith	}
46445405Smsmith    }
46545405Smsmith    /* got somewhere to put it? */
46645405Smsmith    if (free_md == NULL)
46745405Smsmith	return(ENOSPC);
46845405Smsmith
46945405Smsmith    /* Set up new descriptor */
47045405Smsmith    free_md->mr_base = mrd->mr_base;
47145405Smsmith    free_md->mr_len = mrd->mr_len;
47245405Smsmith    free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
47345405Smsmith    bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
47445405Smsmith    return(0);
47545405Smsmith}
47645405Smsmith
47745405Smsmith/*
47845405Smsmith * Handle requests to set memory range attributes by manipulating MTRRs.
47945405Smsmith *
48045405Smsmith */
48145405Smsmithstatic int
48245405Smsmithi686_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
48345405Smsmith{
48445405Smsmith    struct mem_range_desc	*targ;
48545405Smsmith    int				error = 0;
48645405Smsmith
48745405Smsmith    switch(*arg) {
48845405Smsmith    case MEMRANGE_SET_UPDATE:
48945405Smsmith	/* make sure that what's being asked for is even possible at all */
49045405Smsmith	if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
49194683Sdwmalone	    i686_mtrrtype(mrd->mr_flags) == -1)
49245405Smsmith	    return(EINVAL);
49345405Smsmith
49445405Smsmith#define FIXTOP	((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
49545405Smsmith
49645405Smsmith	/* are the "low memory" conditions applicable? */
49745405Smsmith	if ((sc->mr_cap & MR686_FIXMTRR) &&
49845405Smsmith	    ((mrd->mr_base + mrd->mr_len) <= FIXTOP)) {
49945405Smsmith	    if ((error = i686_mrsetlow(sc, mrd, arg)) != 0)
50045405Smsmith		return(error);
50145405Smsmith	} else {
50245405Smsmith	    /* it's time to play with variable MTRRs */
50345405Smsmith	    if ((error = i686_mrsetvariable(sc, mrd, arg)) != 0)
50445405Smsmith		return(error);
50545405Smsmith	}
50645405Smsmith	break;
50745405Smsmith
50845405Smsmith    case MEMRANGE_SET_REMOVE:
50945405Smsmith	if ((targ = mem_range_match(sc, mrd)) == NULL)
51045405Smsmith	    return(ENOENT);
51145405Smsmith	if (targ->mr_flags & MDF_FIXACTIVE)
51245405Smsmith	    return(EPERM);
51345405Smsmith	if (targ->mr_flags & MDF_BUSY)
51445405Smsmith	    return(EBUSY);
51545405Smsmith	targ->mr_flags &= ~MDF_ACTIVE;
51645405Smsmith	targ->mr_owner[0] = 0;
51745405Smsmith	break;
51845405Smsmith
51945405Smsmith    default:
52045405Smsmith	return(EOPNOTSUPP);
52145405Smsmith    }
52245405Smsmith
52345405Smsmith    /* update the hardware */
52448925Smsmith    i686_mrstore(sc);
52545405Smsmith    i686_mrfetch(sc);	/* refetch to see where we're at */
52648925Smsmith    return(0);
52745405Smsmith}
52845405Smsmith
52945405Smsmith/*
53045405Smsmith * Work out how many ranges we support, initialise storage for them,
53145405Smsmith * fetch the initial settings.
53245405Smsmith */
53345405Smsmithstatic void
53445405Smsmithi686_mrinit(struct mem_range_softc *sc)
53545405Smsmith{
53645405Smsmith    struct mem_range_desc	*mrd;
53745405Smsmith    int				nmdesc = 0;
53845405Smsmith    int				i;
53945405Smsmith
54045405Smsmith    mtrrcap = rdmsr(MSR_MTRRcap);
54145405Smsmith    mtrrdef = rdmsr(MSR_MTRRdefType);
54245405Smsmith
54345405Smsmith    /* For now, bail out if MTRRs are not enabled */
54445405Smsmith    if (!(mtrrdef & 0x800)) {
54545405Smsmith	if (bootverbose)
54645405Smsmith	    printf("CPU supports MTRRs but not enabled\n");
54745405Smsmith	return;
54845405Smsmith    }
54945405Smsmith    nmdesc = mtrrcap & 0xff;
55048925Smsmith    printf("Pentium Pro MTRR support enabled\n");
55145405Smsmith
55245405Smsmith    /* If fixed MTRRs supported and enabled */
55345405Smsmith    if ((mtrrcap & 0x100) && (mtrrdef & 0x400)) {
55445405Smsmith	sc->mr_cap = MR686_FIXMTRR;
55545405Smsmith	nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
55645405Smsmith    }
55745405Smsmith
55845405Smsmith    sc->mr_desc =
55945405Smsmith	(struct mem_range_desc *)malloc(nmdesc * sizeof(struct mem_range_desc),
56069781Sdwmalone					M_MEMDESC, M_WAITOK | M_ZERO);
56145405Smsmith    sc->mr_ndesc = nmdesc;
56245405Smsmith
56345405Smsmith    mrd = sc->mr_desc;
56445405Smsmith
56545405Smsmith    /* Populate the fixed MTRR entries' base/length */
56645405Smsmith    if (sc->mr_cap & MR686_FIXMTRR) {
56745405Smsmith	for (i = 0; i < MTRR_N64K; i++, mrd++) {
56845405Smsmith	    mrd->mr_base = i * 0x10000;
56945405Smsmith	    mrd->mr_len = 0x10000;
57045405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
57145405Smsmith	}
57245405Smsmith	for (i = 0; i < MTRR_N16K; i++, mrd++) {
57345405Smsmith	    mrd->mr_base = i * 0x4000 + 0x80000;
57445405Smsmith	    mrd->mr_len = 0x4000;
57545405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
57645405Smsmith	}
57745405Smsmith	for (i = 0; i < MTRR_N4K; i++, mrd++) {
57845405Smsmith	    mrd->mr_base = i * 0x1000 + 0xc0000;
57945405Smsmith	    mrd->mr_len = 0x1000;
58045405Smsmith	    mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN | MDF_FIXACTIVE;
58145405Smsmith	}
58245405Smsmith    }
58345405Smsmith
58445405Smsmith    /*
58545405Smsmith     * Get current settings, anything set now is considered to have
58645405Smsmith     * been set by the firmware. (XXX has something already played here?)
58745405Smsmith     */
58845405Smsmith    i686_mrfetch(sc);
58945405Smsmith    mrd = sc->mr_desc;
59045405Smsmith    for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
59145405Smsmith	if (mrd->mr_flags & MDF_ACTIVE)
59245405Smsmith	    mrd->mr_flags |= MDF_FIRMWARE;
59345405Smsmith    }
59445405Smsmith}
59545405Smsmith
59646215Smsmith/*
59746215Smsmith * Initialise MTRRs on an AP after the BSP has run the init code.
59846215Smsmith */
59945405Smsmithstatic void
60046215Smsmithi686_mrAPinit(struct mem_range_softc *sc)
60146215Smsmith{
60248925Smsmith    i686_mrstoreone((void *)sc);	/* set MTRRs to match BSP */
60346215Smsmith    wrmsr(MSR_MTRRdefType, mtrrdef);	/* set MTRR behaviour to match BSP */
60446215Smsmith}
60546215Smsmith
60646215Smsmithstatic void
60745405Smsmithi686_mem_drvinit(void *unused)
60845405Smsmith{
60945405Smsmith    /* Try for i686 MTRRs */
610106842Smdodd    if (!mtrrs_disabled && (cpu_feature & CPUID_MTRR) &&
611103649Smdodd	((cpu_id & 0xf00) == 0x600 || (cpu_id & 0xf00) == 0xf00) &&
61252177Sgreen	((strcmp(cpu_vendor, "GenuineIntel") == 0) ||
61352177Sgreen	(strcmp(cpu_vendor, "AuthenticAMD") == 0))) {
61445405Smsmith	mem_range_softc.mr_op = &i686_mrops;
61545405Smsmith    }
61645405Smsmith}
61745405Smsmith
61845405SmsmithSYSINIT(i686memdev,SI_SUB_DRIVERS,SI_ORDER_FIRST,i686_mem_drvinit,NULL)
61945405Smsmith
62045405Smsmith
621