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