The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/i686_mem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.2/sys/i386/i386/i686_mem.c 206165 2010-04-04 15:42:52Z rnoland $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/kernel.h>
   32 #include <sys/systm.h>
   33 #include <sys/malloc.h>
   34 #include <sys/memrange.h>
   35 #include <sys/smp.h>
   36 #include <sys/sysctl.h>
   37 
   38 #include <machine/cputypes.h>
   39 #include <machine/md_var.h>
   40 #include <machine/specialreg.h>
   41 
   42 /*
   43  * i686 memory range operations
   44  *
   45  * This code will probably be impenetrable without reference to the
   46  * Intel Pentium Pro documentation.
   47  */
   48 
   49 static char *mem_owner_bios = "BIOS";
   50 
   51 #define MR686_FIXMTRR   (1<<0)
   52 
   53 #define mrwithin(mr, a)                                                 \
   54         (((a) >= (mr)->mr_base) && ((a) < ((mr)->mr_base + (mr)->mr_len)))
   55 #define mroverlap(mra, mrb)                                             \
   56         (mrwithin(mra, mrb->mr_base) || mrwithin(mrb, mra->mr_base))
   57 
   58 #define mrvalid(base, len)                                              \
   59         ((!(base & ((1 << 12) - 1))) && /* base is multiple of 4k */    \
   60             ((len) >= (1 << 12)) &&     /* length is >= 4k */           \
   61             powerof2((len)) &&          /* ... and power of two */      \
   62             !((base) & ((len) - 1)))    /* range is not discontiuous */
   63 
   64 #define mrcopyflags(curr, new)                                          \
   65         (((curr) & ~MDF_ATTRMASK) | ((new) & MDF_ATTRMASK))
   66 
   67 static int mtrrs_disabled;
   68 TUNABLE_INT("machdep.disable_mtrrs", &mtrrs_disabled);
   69 SYSCTL_INT(_machdep, OID_AUTO, disable_mtrrs, CTLFLAG_RDTUN,
   70     &mtrrs_disabled, 0, "Disable i686 MTRRs.");
   71 
   72 static void     i686_mrinit(struct mem_range_softc *sc);
   73 static int      i686_mrset(struct mem_range_softc *sc,
   74                     struct mem_range_desc *mrd, int *arg);
   75 static void     i686_mrAPinit(struct mem_range_softc *sc);
   76 static void     i686_mrreinit(struct mem_range_softc *sc);
   77 
   78 static struct mem_range_ops i686_mrops = {
   79         i686_mrinit,
   80         i686_mrset,
   81         i686_mrAPinit,
   82         i686_mrreinit
   83 };
   84 
   85 /* XXX for AP startup hook */
   86 static u_int64_t mtrrcap, mtrrdef;
   87 
   88 /* The bitmask for the PhysBase and PhysMask fields of the variable MTRRs. */
   89 static u_int64_t mtrr_physmask;
   90 
   91 static struct mem_range_desc *mem_range_match(struct mem_range_softc *sc,
   92                     struct mem_range_desc *mrd);
   93 static void     i686_mrfetch(struct mem_range_softc *sc);
   94 static int      i686_mtrrtype(int flags);
   95 static int      i686_mrt2mtrr(int flags, int oldval);
   96 static int      i686_mtrrconflict(int flag1, int flag2);
   97 static void     i686_mrstore(struct mem_range_softc *sc);
   98 static void     i686_mrstoreone(void *arg);
   99 static struct mem_range_desc *i686_mtrrfixsearch(struct mem_range_softc *sc,
  100                     u_int64_t addr);
  101 static int      i686_mrsetlow(struct mem_range_softc *sc,
  102                     struct mem_range_desc *mrd, int *arg);
  103 static int      i686_mrsetvariable(struct mem_range_softc *sc,
  104                     struct mem_range_desc *mrd, int *arg);
  105 
  106 /* i686 MTRR type to memory range type conversion */
  107 static int i686_mtrrtomrt[] = {
  108         MDF_UNCACHEABLE,
  109         MDF_WRITECOMBINE,
  110         MDF_UNKNOWN,
  111         MDF_UNKNOWN,
  112         MDF_WRITETHROUGH,
  113         MDF_WRITEPROTECT,
  114         MDF_WRITEBACK
  115 };
  116 
  117 #define MTRRTOMRTLEN (sizeof(i686_mtrrtomrt) / sizeof(i686_mtrrtomrt[0]))
  118 
  119 static int
  120 i686_mtrr2mrt(int val)
  121 {
  122 
  123         if (val < 0 || val >= MTRRTOMRTLEN)
  124                 return (MDF_UNKNOWN);
  125         return (i686_mtrrtomrt[val]);
  126 }
  127 
  128 /*
  129  * i686 MTRR conflicts. Writeback and uncachable may overlap.
  130  */
  131 static int
  132 i686_mtrrconflict(int flag1, int flag2)
  133 {
  134 
  135         flag1 &= MDF_ATTRMASK;
  136         flag2 &= MDF_ATTRMASK;
  137         if (flag1 == flag2 ||
  138             (flag1 == MDF_WRITEBACK && flag2 == MDF_UNCACHEABLE) ||
  139             (flag2 == MDF_WRITEBACK && flag1 == MDF_UNCACHEABLE))
  140                 return (0);
  141         return (1);
  142 }
  143 
  144 /*
  145  * Look for an exactly-matching range.
  146  */
  147 static struct mem_range_desc *
  148 mem_range_match(struct mem_range_softc *sc, struct mem_range_desc *mrd)
  149 {
  150         struct mem_range_desc *cand;
  151         int i;
  152 
  153         for (i = 0, cand = sc->mr_desc; i < sc->mr_ndesc; i++, cand++)
  154                 if ((cand->mr_base == mrd->mr_base) &&
  155                     (cand->mr_len == mrd->mr_len))
  156                         return (cand);
  157         return (NULL);
  158 }
  159 
  160 /*
  161  * Fetch the current mtrr settings from the current CPU (assumed to
  162  * all be in sync in the SMP case).  Note that if we are here, we
  163  * assume that MTRRs are enabled, and we may or may not have fixed
  164  * MTRRs.
  165  */
  166 static void
  167 i686_mrfetch(struct mem_range_softc *sc)
  168 {
  169         struct mem_range_desc *mrd;
  170         u_int64_t msrv;
  171         int i, j, msr;
  172 
  173         mrd = sc->mr_desc;
  174 
  175         /* Get fixed-range MTRRs. */
  176         if (sc->mr_cap & MR686_FIXMTRR) {
  177                 msr = MSR_MTRR64kBase;
  178                 for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
  179                         msrv = rdmsr(msr);
  180                         for (j = 0; j < 8; j++, mrd++) {
  181                                 mrd->mr_flags =
  182                                     (mrd->mr_flags & ~MDF_ATTRMASK) |
  183                                     i686_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
  184                                 if (mrd->mr_owner[0] == 0)
  185                                         strcpy(mrd->mr_owner, mem_owner_bios);
  186                                 msrv = msrv >> 8;
  187                         }
  188                 }
  189                 msr = MSR_MTRR16kBase;
  190                 for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
  191                         msrv = rdmsr(msr);
  192                         for (j = 0; j < 8; j++, mrd++) {
  193                                 mrd->mr_flags =
  194                                     (mrd->mr_flags & ~MDF_ATTRMASK) |
  195                                     i686_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
  196                                 if (mrd->mr_owner[0] == 0)
  197                                         strcpy(mrd->mr_owner, mem_owner_bios);
  198                                 msrv = msrv >> 8;
  199                         }
  200                 }
  201                 msr = MSR_MTRR4kBase;
  202                 for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
  203                         msrv = rdmsr(msr);
  204                         for (j = 0; j < 8; j++, mrd++) {
  205                                 mrd->mr_flags =
  206                                     (mrd->mr_flags & ~MDF_ATTRMASK) |
  207                                     i686_mtrr2mrt(msrv & 0xff) | MDF_ACTIVE;
  208                                 if (mrd->mr_owner[0] == 0)
  209                                         strcpy(mrd->mr_owner, mem_owner_bios);
  210                                 msrv = msrv >> 8;
  211                         }
  212                 }
  213         }
  214 
  215         /* Get remainder which must be variable MTRRs. */
  216         msr = MSR_MTRRVarBase;
  217         for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
  218                 msrv = rdmsr(msr);
  219                 mrd->mr_flags = (mrd->mr_flags & ~MDF_ATTRMASK) |
  220                     i686_mtrr2mrt(msrv & MTRR_PHYSBASE_TYPE);
  221                 mrd->mr_base = msrv & mtrr_physmask;
  222                 msrv = rdmsr(msr + 1);
  223                 mrd->mr_flags = (msrv & MTRR_PHYSMASK_VALID) ?
  224                     (mrd->mr_flags | MDF_ACTIVE) :
  225                     (mrd->mr_flags & ~MDF_ACTIVE);
  226 
  227                 /* Compute the range from the mask. Ick. */
  228                 mrd->mr_len = (~(msrv & mtrr_physmask) &
  229                     (mtrr_physmask | 0xfffLL)) + 1;
  230                 if (!mrvalid(mrd->mr_base, mrd->mr_len))
  231                         mrd->mr_flags |= MDF_BOGUS;
  232 
  233                 /* If unclaimed and active, must be the BIOS. */
  234                 if ((mrd->mr_flags & MDF_ACTIVE) && (mrd->mr_owner[0] == 0))
  235                         strcpy(mrd->mr_owner, mem_owner_bios);
  236         }
  237 }
  238 
  239 /*
  240  * Return the MTRR memory type matching a region's flags
  241  */
  242 static int
  243 i686_mtrrtype(int flags)
  244 {
  245         int i;
  246 
  247         flags &= MDF_ATTRMASK;
  248 
  249         for (i = 0; i < MTRRTOMRTLEN; i++) {
  250                 if (i686_mtrrtomrt[i] == MDF_UNKNOWN)
  251                         continue;
  252                 if (flags == i686_mtrrtomrt[i])
  253                         return (i);
  254         }
  255         return (-1);
  256 }
  257 
  258 static int
  259 i686_mrt2mtrr(int flags, int oldval)
  260 {
  261         int val;
  262 
  263         if ((val = i686_mtrrtype(flags)) == -1)
  264                 return (oldval & 0xff);
  265         return (val & 0xff);
  266 }
  267 
  268 /*
  269  * Update running CPU(s) MTRRs to match the ranges in the descriptor
  270  * list.
  271  *
  272  * XXX Must be called with interrupts enabled.
  273  */
  274 static void
  275 i686_mrstore(struct mem_range_softc *sc)
  276 {
  277 #ifdef SMP
  278         /*
  279          * We should use ipi_all_but_self() to call other CPUs into a
  280          * locking gate, then call a target function to do this work.
  281          * The "proper" solution involves a generalised locking gate
  282          * implementation, not ready yet.
  283          */
  284         smp_rendezvous(NULL, i686_mrstoreone, NULL, sc);
  285 #else
  286         disable_intr();                         /* disable interrupts */
  287         i686_mrstoreone(sc);
  288         enable_intr();
  289 #endif
  290 }
  291 
  292 /*
  293  * Update the current CPU's MTRRs with those represented in the
  294  * descriptor list.  Note that we do this wholesale rather than just
  295  * stuffing one entry; this is simpler (but slower, of course).
  296  */
  297 static void
  298 i686_mrstoreone(void *arg)
  299 {
  300         struct mem_range_softc *sc = arg;
  301         struct mem_range_desc *mrd;
  302         u_int64_t omsrv, msrv;
  303         int i, j, msr;
  304         u_int cr4save;
  305 
  306         mrd = sc->mr_desc;
  307 
  308         /* Disable PGE. */
  309         cr4save = rcr4();
  310         if (cr4save & CR4_PGE)
  311                 load_cr4(cr4save & ~CR4_PGE);
  312 
  313         /* Disable caches (CD = 1, NW = 0). */
  314         load_cr0((rcr0() & ~CR0_NW) | CR0_CD);
  315 
  316         /* Flushes caches and TLBs. */
  317         wbinvd();
  318 
  319         /* Disable MTRRs (E = 0). */
  320         wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) & ~MTRR_DEF_ENABLE);
  321 
  322         /* Set fixed-range MTRRs. */
  323         if (sc->mr_cap & MR686_FIXMTRR) {
  324                 msr = MSR_MTRR64kBase;
  325                 for (i = 0; i < (MTRR_N64K / 8); i++, msr++) {
  326                         msrv = 0;
  327                         omsrv = rdmsr(msr);
  328                         for (j = 7; j >= 0; j--) {
  329                                 msrv = msrv << 8;
  330                                 msrv |= i686_mrt2mtrr((mrd + j)->mr_flags,
  331                                     omsrv >> (j * 8));
  332                         }
  333                         wrmsr(msr, msrv);
  334                         mrd += 8;
  335                 }
  336                 msr = MSR_MTRR16kBase;
  337                 for (i = 0; i < (MTRR_N16K / 8); i++, msr++) {
  338                         msrv = 0;
  339                         omsrv = rdmsr(msr);
  340                         for (j = 7; j >= 0; j--) {
  341                                 msrv = msrv << 8;
  342                                 msrv |= i686_mrt2mtrr((mrd + j)->mr_flags,
  343                                     omsrv >> (j * 8));
  344                         }
  345                         wrmsr(msr, msrv);
  346                         mrd += 8;
  347                 }
  348                 msr = MSR_MTRR4kBase;
  349                 for (i = 0; i < (MTRR_N4K / 8); i++, msr++) {
  350                         msrv = 0;
  351                         omsrv = rdmsr(msr);
  352                         for (j = 7; j >= 0; j--) {
  353                                 msrv = msrv << 8;
  354                                 msrv |= i686_mrt2mtrr((mrd + j)->mr_flags,
  355                                     omsrv >> (j * 8));
  356                         }
  357                         wrmsr(msr, msrv);
  358                         mrd += 8;
  359                 }
  360         }
  361 
  362         /* Set remainder which must be variable MTRRs. */
  363         msr = MSR_MTRRVarBase;
  364         for (; (mrd - sc->mr_desc) < sc->mr_ndesc; msr += 2, mrd++) {
  365                 /* base/type register */
  366                 omsrv = rdmsr(msr);
  367                 if (mrd->mr_flags & MDF_ACTIVE) {
  368                         msrv = mrd->mr_base & mtrr_physmask;
  369                         msrv |= i686_mrt2mtrr(mrd->mr_flags, omsrv);
  370                 } else {
  371                         msrv = 0;
  372                 }
  373                 wrmsr(msr, msrv);
  374 
  375                 /* mask/active register */
  376                 if (mrd->mr_flags & MDF_ACTIVE) {
  377                         msrv = MTRR_PHYSMASK_VALID |
  378                             (~(mrd->mr_len - 1) & mtrr_physmask);
  379                 } else {
  380                         msrv = 0;
  381                 }
  382                 wrmsr(msr + 1, msrv);
  383         }
  384 
  385         /* Flush caches, TLBs. */
  386         wbinvd();
  387 
  388         /* Enable MTRRs. */
  389         wrmsr(MSR_MTRRdefType, rdmsr(MSR_MTRRdefType) | MTRR_DEF_ENABLE);
  390 
  391         /* Enable caches (CD = 0, NW = 0). */
  392         load_cr0(rcr0() & ~(CR0_CD | CR0_NW));
  393 
  394         /* Restore PGE. */
  395         load_cr4(cr4save);
  396 }
  397 
  398 /*
  399  * Hunt for the fixed MTRR referencing (addr)
  400  */
  401 static struct mem_range_desc *
  402 i686_mtrrfixsearch(struct mem_range_softc *sc, u_int64_t addr)
  403 {
  404         struct mem_range_desc *mrd;
  405         int i;
  406 
  407         for (i = 0, mrd = sc->mr_desc; i < (MTRR_N64K + MTRR_N16K + MTRR_N4K);
  408              i++, mrd++)
  409                 if ((addr >= mrd->mr_base) &&
  410                     (addr < (mrd->mr_base + mrd->mr_len)))
  411                         return (mrd);
  412         return (NULL);
  413 }
  414 
  415 /*
  416  * Try to satisfy the given range request by manipulating the fixed
  417  * MTRRs that cover low memory.
  418  *
  419  * Note that we try to be generous here; we'll bloat the range out to
  420  * the next higher/lower boundary to avoid the consumer having to know
  421  * too much about the mechanisms here.
  422  *
  423  * XXX note that this will have to be updated when we start supporting
  424  * "busy" ranges.
  425  */
  426 static int
  427 i686_mrsetlow(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
  428 {
  429         struct mem_range_desc *first_md, *last_md, *curr_md;
  430 
  431         /* Range check. */
  432         if (((first_md = i686_mtrrfixsearch(sc, mrd->mr_base)) == NULL) ||
  433             ((last_md = i686_mtrrfixsearch(sc, mrd->mr_base + mrd->mr_len - 1)) == NULL))
  434                 return (EINVAL);
  435 
  436         /* Check that we aren't doing something risky. */
  437         if (!(mrd->mr_flags & MDF_FORCE))
  438                 for (curr_md = first_md; curr_md <= last_md; curr_md++) {
  439                         if ((curr_md->mr_flags & MDF_ATTRMASK) == MDF_UNKNOWN)
  440                                 return (EACCES);
  441                 }
  442 
  443         /* Set flags, clear set-by-firmware flag. */
  444         for (curr_md = first_md; curr_md <= last_md; curr_md++) {
  445                 curr_md->mr_flags = mrcopyflags(curr_md->mr_flags &
  446                     ~MDF_FIRMWARE, mrd->mr_flags);
  447                 bcopy(mrd->mr_owner, curr_md->mr_owner, sizeof(mrd->mr_owner));
  448         }
  449 
  450         return (0);
  451 }
  452 
  453 /*
  454  * Modify/add a variable MTRR to satisfy the request.
  455  *
  456  * XXX needs to be updated to properly support "busy" ranges.
  457  */
  458 static int
  459 i686_mrsetvariable(struct mem_range_softc *sc, struct mem_range_desc *mrd,
  460     int *arg)
  461 {
  462         struct mem_range_desc *curr_md, *free_md;
  463         int i;
  464 
  465         /*
  466          * Scan the currently active variable descriptors, look for
  467          * one we exactly match (straight takeover) and for possible
  468          * accidental overlaps.
  469          *
  470          * Keep track of the first empty variable descriptor in case
  471          * we can't perform a takeover.
  472          */
  473         i = (sc->mr_cap & MR686_FIXMTRR) ? MTRR_N64K + MTRR_N16K + MTRR_N4K : 0;
  474         curr_md = sc->mr_desc + i;
  475         free_md = NULL;
  476         for (; i < sc->mr_ndesc; i++, curr_md++) {
  477                 if (curr_md->mr_flags & MDF_ACTIVE) {
  478                         /* Exact match? */
  479                         if ((curr_md->mr_base == mrd->mr_base) &&
  480                             (curr_md->mr_len == mrd->mr_len)) {
  481 
  482                                 /* Whoops, owned by someone. */
  483                                 if (curr_md->mr_flags & MDF_BUSY)
  484                                         return (EBUSY);
  485 
  486                                 /* Check that we aren't doing something risky */
  487                                 if (!(mrd->mr_flags & MDF_FORCE) &&
  488                                     ((curr_md->mr_flags & MDF_ATTRMASK) ==
  489                                     MDF_UNKNOWN))
  490                                         return (EACCES);
  491 
  492                                 /* Ok, just hijack this entry. */
  493                                 free_md = curr_md;
  494                                 break;
  495                         }
  496 
  497                         /* Non-exact overlap? */
  498                         if (mroverlap(curr_md, mrd)) {
  499                                 /* Between conflicting region types? */
  500                                 if (i686_mtrrconflict(curr_md->mr_flags,
  501                                     mrd->mr_flags))
  502                                         return (EINVAL);
  503                         }
  504                 } else if (free_md == NULL) {
  505                         free_md = curr_md;
  506                 }
  507         }
  508 
  509         /* Got somewhere to put it? */
  510         if (free_md == NULL)
  511                 return (ENOSPC);
  512 
  513         /* Set up new descriptor. */
  514         free_md->mr_base = mrd->mr_base;
  515         free_md->mr_len = mrd->mr_len;
  516         free_md->mr_flags = mrcopyflags(MDF_ACTIVE, mrd->mr_flags);
  517         bcopy(mrd->mr_owner, free_md->mr_owner, sizeof(mrd->mr_owner));
  518         return (0);
  519 }
  520 
  521 /*
  522  * Handle requests to set memory range attributes by manipulating MTRRs.
  523  */
  524 static int
  525 i686_mrset(struct mem_range_softc *sc, struct mem_range_desc *mrd, int *arg)
  526 {
  527         struct mem_range_desc *targ;
  528         int error = 0;
  529 
  530         switch(*arg) {
  531         case MEMRANGE_SET_UPDATE:
  532                 /*
  533                  * Make sure that what's being asked for is even
  534                  * possible at all.
  535                  */
  536                 if (!mrvalid(mrd->mr_base, mrd->mr_len) ||
  537                     i686_mtrrtype(mrd->mr_flags) == -1)
  538                         return (EINVAL);
  539 
  540 #define FIXTOP  ((MTRR_N64K * 0x10000) + (MTRR_N16K * 0x4000) + (MTRR_N4K * 0x1000))
  541 
  542                 /* Are the "low memory" conditions applicable? */
  543                 if ((sc->mr_cap & MR686_FIXMTRR) &&
  544                     ((mrd->mr_base + mrd->mr_len) <= FIXTOP)) {
  545                         if ((error = i686_mrsetlow(sc, mrd, arg)) != 0)
  546                                 return (error);
  547                 } else {
  548                         /* It's time to play with variable MTRRs. */
  549                         if ((error = i686_mrsetvariable(sc, mrd, arg)) != 0)
  550                                 return (error);
  551                 }
  552                 break;
  553 
  554         case MEMRANGE_SET_REMOVE:
  555                 if ((targ = mem_range_match(sc, mrd)) == NULL)
  556                         return (ENOENT);
  557                 if (targ->mr_flags & MDF_FIXACTIVE)
  558                         return (EPERM);
  559                 if (targ->mr_flags & MDF_BUSY)
  560                         return (EBUSY);
  561                 targ->mr_flags &= ~MDF_ACTIVE;
  562                 targ->mr_owner[0] = 0;
  563                 break;
  564 
  565         default:
  566                 return (EOPNOTSUPP);
  567         }
  568 
  569         /* Update the hardware. */
  570         i686_mrstore(sc);
  571 
  572         /* Refetch to see where we're at. */
  573         i686_mrfetch(sc);
  574         return (0);
  575 }
  576 
  577 /*
  578  * Work out how many ranges we support, initialise storage for them,
  579  * and fetch the initial settings.
  580  */
  581 static void
  582 i686_mrinit(struct mem_range_softc *sc)
  583 {
  584         struct mem_range_desc *mrd;
  585         u_int regs[4];
  586         int i, nmdesc = 0, pabits;
  587 
  588         mtrrcap = rdmsr(MSR_MTRRcap);
  589         mtrrdef = rdmsr(MSR_MTRRdefType);
  590 
  591         /* For now, bail out if MTRRs are not enabled. */
  592         if (!(mtrrdef & MTRR_DEF_ENABLE)) {
  593                 if (bootverbose)
  594                         printf("CPU supports MTRRs but not enabled\n");
  595                 return;
  596         }
  597         nmdesc = mtrrcap & MTRR_CAP_VCNT;
  598         if (bootverbose)
  599                 printf("Pentium Pro MTRR support enabled\n");
  600 
  601         /*
  602          * Determine the size of the PhysMask and PhysBase fields in
  603          * the variable range MTRRs.  If the extended CPUID 0x80000008
  604          * is present, use that to figure out how many physical
  605          * address bits the CPU supports.  Otherwise, default to 36
  606          * address bits.
  607          */
  608         if (cpu_exthigh >= 0x80000008) {
  609                 do_cpuid(0x80000008, regs);
  610                 pabits = regs[0] & 0xff;
  611         } else
  612                 pabits = 36;
  613         mtrr_physmask = ((1ULL << pabits) - 1) & ~0xfffULL;
  614 
  615         /* If fixed MTRRs supported and enabled. */
  616         if ((mtrrcap & MTRR_CAP_FIXED) && (mtrrdef & MTRR_DEF_FIXED_ENABLE)) {
  617                 sc->mr_cap = MR686_FIXMTRR;
  618                 nmdesc += MTRR_N64K + MTRR_N16K + MTRR_N4K;
  619         }
  620 
  621         sc->mr_desc = malloc(nmdesc * sizeof(struct mem_range_desc), M_MEMDESC,
  622             M_WAITOK | M_ZERO);
  623         sc->mr_ndesc = nmdesc;
  624 
  625         mrd = sc->mr_desc;
  626 
  627         /* Populate the fixed MTRR entries' base/length. */
  628         if (sc->mr_cap & MR686_FIXMTRR) {
  629                 for (i = 0; i < MTRR_N64K; i++, mrd++) {
  630                         mrd->mr_base = i * 0x10000;
  631                         mrd->mr_len = 0x10000;
  632                         mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
  633                             MDF_FIXACTIVE;
  634                 }
  635                 for (i = 0; i < MTRR_N16K; i++, mrd++) {
  636                         mrd->mr_base = i * 0x4000 + 0x80000;
  637                         mrd->mr_len = 0x4000;
  638                         mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
  639                             MDF_FIXACTIVE;
  640                 }
  641                 for (i = 0; i < MTRR_N4K; i++, mrd++) {
  642                         mrd->mr_base = i * 0x1000 + 0xc0000;
  643                         mrd->mr_len = 0x1000;
  644                         mrd->mr_flags = MDF_FIXBASE | MDF_FIXLEN |
  645                             MDF_FIXACTIVE;
  646                 }
  647         }
  648 
  649         /*
  650          * Get current settings, anything set now is considered to
  651          * have been set by the firmware. (XXX has something already
  652          * played here?)
  653          */
  654         i686_mrfetch(sc);
  655         mrd = sc->mr_desc;
  656         for (i = 0; i < sc->mr_ndesc; i++, mrd++) {
  657                 if (mrd->mr_flags & MDF_ACTIVE)
  658                         mrd->mr_flags |= MDF_FIRMWARE;
  659         }
  660 }
  661 
  662 /*
  663  * Initialise MTRRs on an AP after the BSP has run the init code.
  664  */
  665 static void
  666 i686_mrAPinit(struct mem_range_softc *sc)
  667 {
  668 
  669         i686_mrstoreone(sc);
  670         wrmsr(MSR_MTRRdefType, mtrrdef);
  671 }
  672 
  673 /*
  674  * Re-initialise running CPU(s) MTRRs to match the ranges in the descriptor
  675  * list.
  676  *
  677  * XXX Must be called with interrupts enabled.
  678  */
  679 static void
  680 i686_mrreinit(struct mem_range_softc *sc)
  681 {
  682 #ifdef SMP
  683         /*
  684          * We should use ipi_all_but_self() to call other CPUs into a
  685          * locking gate, then call a target function to do this work.
  686          * The "proper" solution involves a generalised locking gate
  687          * implementation, not ready yet.
  688          */
  689         smp_rendezvous(NULL, (void *)i686_mrAPinit, NULL, sc);
  690 #else
  691         disable_intr();                         /* disable interrupts */
  692         i686_mrAPinit(sc);
  693         enable_intr();
  694 #endif
  695 }
  696 
  697 static void
  698 i686_mem_drvinit(void *unused)
  699 {
  700 
  701         if (mtrrs_disabled)
  702                 return;
  703         if (!(cpu_feature & CPUID_MTRR))
  704                 return;
  705         if ((cpu_id & 0xf00) != 0x600 && (cpu_id & 0xf00) != 0xf00)
  706                 return;
  707         switch (cpu_vendor_id) {
  708         case CPU_VENDOR_INTEL:
  709         case CPU_VENDOR_AMD:
  710         case CPU_VENDOR_CENTAUR:
  711                 break;
  712         default:
  713                 return;
  714         }
  715         mem_range_softc.mr_op = &i686_mrops;
  716 }
  717 SYSINIT(i686memdev, SI_SUB_DRIVERS, SI_ORDER_FIRST, i686_mem_drvinit, NULL);

Cache object: 9e89255c0c25fdcb69f02d9d3c387ae8


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.