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/k6_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 Brian Fundakowski Feldman
    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$");
   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 
   36 #include <machine/md_var.h>
   37 #include <machine/specialreg.h>
   38 
   39 /*
   40  * A K6-2 MTRR is defined as the highest 15 bits having the address, the next
   41  * 15 having the mask, the 1st bit being "write-combining" and the 0th bit
   42  * being "uncacheable".
   43  *
   44  *      Address             Mask        WC  UC
   45  * | XXXXXXXXXXXXXXX | XXXXXXXXXXXXXXX | X | X |
   46  *
   47  * There are two of these in the 64-bit UWCCR.
   48  */
   49 
   50 #define UWCCR 0xc0000085
   51 
   52 #define K6_REG_GET(reg, addr, mask, wc, uc) do {                        \
   53                 addr = (reg) & 0xfffe0000;                              \
   54                 mask = ((reg) & 0x1fffc) >> 2;                          \
   55                 wc = ((reg) & 0x2) >> 1;                                \
   56                 uc = (reg) & 0x1;                                       \
   57         } while (0)
   58 
   59 #define K6_REG_MAKE(addr, mask, wc, uc)                                 \
   60         ((addr) | ((mask) << 2) | ((wc) << 1) | uc)
   61 
   62 static void     k6_mrinit(struct mem_range_softc *sc);
   63 static int      k6_mrset(struct mem_range_softc *, struct mem_range_desc *,
   64                     int *);
   65 static __inline int k6_mrmake(struct mem_range_desc *, u_int32_t *);
   66 static void     k6_mem_drvinit(void *);
   67 
   68 static struct mem_range_ops k6_mrops =
   69 {
   70         k6_mrinit,
   71         k6_mrset,
   72         NULL
   73 };
   74 
   75 static __inline int
   76 k6_mrmake(struct mem_range_desc *desc, u_int32_t *mtrr)
   77 {
   78         u_int32_t len = 0, wc, uc;
   79         register int bit;
   80 
   81         if (desc->mr_base &~ 0xfffe0000)
   82                 return (EINVAL);
   83         if (desc->mr_len < 131072 || !powerof2(desc->mr_len))
   84                 return (EINVAL);
   85         if (desc->mr_flags &~ (MDF_WRITECOMBINE|MDF_UNCACHEABLE|MDF_FORCE))
   86                 return (EOPNOTSUPP);
   87 
   88         for (bit = ffs(desc->mr_len >> 17) - 1; bit < 15; bit++)
   89                 len |= 1 << bit; 
   90         wc = (desc->mr_flags & MDF_WRITECOMBINE) ? 1 : 0;
   91         uc = (desc->mr_flags & MDF_UNCACHEABLE) ? 1 : 0;
   92 
   93         *mtrr = K6_REG_MAKE(desc->mr_base, len, wc, uc);
   94         return (0);
   95 }
   96 
   97 static void
   98 k6_mrinit(struct mem_range_softc *sc)
   99 {
  100         u_int64_t reg;
  101         u_int32_t addr, mask, wc, uc;
  102         int d;
  103 
  104         sc->mr_cap = 0;
  105         sc->mr_ndesc = 2; /* XXX (BFF) For now, we only have one msr for this */
  106         sc->mr_desc = malloc(sc->mr_ndesc * sizeof(struct mem_range_desc),
  107             M_MEMDESC, M_NOWAIT | M_ZERO);
  108         if (sc->mr_desc == NULL)
  109                 panic("k6_mrinit: malloc returns NULL");
  110 
  111         reg = rdmsr(UWCCR);
  112         for (d = 0; d < sc->mr_ndesc; d++) {
  113                 u_int32_t one = (reg & (0xffffffff << (32 * d))) >> (32 * d);
  114 
  115                 K6_REG_GET(one, addr, mask, wc, uc);
  116                 sc->mr_desc[d].mr_base = addr;
  117                 sc->mr_desc[d].mr_len = ffs(mask) << 17;
  118                 if (wc)
  119                         sc->mr_desc[d].mr_flags |= MDF_WRITECOMBINE;
  120                 if (uc)
  121                         sc->mr_desc[d].mr_flags |= MDF_UNCACHEABLE;
  122         }
  123 
  124         printf("K6-family MTRR support enabled (%d registers)\n", sc->mr_ndesc);
  125 }
  126 
  127 static int
  128 k6_mrset(struct mem_range_softc *sc, struct mem_range_desc *desc, int *arg)
  129 {
  130         u_int64_t reg;
  131         u_int32_t mtrr;
  132         int error, d;
  133 
  134         switch (*arg) {
  135         case MEMRANGE_SET_UPDATE:
  136                 error = k6_mrmake(desc, &mtrr);
  137                 if (error)
  138                         return (error);
  139                 for (d = 0; d < sc->mr_ndesc; d++) {
  140                         if (!sc->mr_desc[d].mr_len) {
  141                                 sc->mr_desc[d] = *desc;
  142                                 goto out;
  143                         }
  144                         if (sc->mr_desc[d].mr_base == desc->mr_base &&
  145                             sc->mr_desc[d].mr_len == desc->mr_len)
  146                                 return (EEXIST);
  147                 }
  148                 return (ENOSPC);
  149         case MEMRANGE_SET_REMOVE:
  150                 mtrr = 0;
  151                 for (d = 0; d < sc->mr_ndesc; d++)
  152                         if (sc->mr_desc[d].mr_base == desc->mr_base &&
  153                             sc->mr_desc[d].mr_len == desc->mr_len) {
  154                                 bzero(&sc->mr_desc[d], sizeof(sc->mr_desc[d]));
  155                                 goto out;
  156                         }
  157                 return (ENOENT);
  158         default:
  159                 return (EOPNOTSUPP);
  160         }
  161 out:    
  162         disable_intr();
  163         wbinvd();
  164         reg = rdmsr(UWCCR);
  165         reg &= ~(0xffffffff << (32 * d));
  166         reg |= mtrr << (32 * d);
  167         wrmsr(UWCCR, reg);
  168         wbinvd();
  169         enable_intr();
  170 
  171         return (0);
  172 }
  173 
  174 static void
  175 k6_mem_drvinit(void *unused)
  176 {
  177 
  178         if (strcmp(cpu_vendor, "AuthenticAMD") != 0)
  179                 return;
  180         if ((cpu_id & 0xf00) != 0x500)
  181                 return;
  182         if ((cpu_id & 0xf0) < 0x80 ||
  183             ((cpu_id & 0xf0) == 0x80 && (cpu_id & 0xf) <= 0x7))
  184                 return;
  185         mem_range_softc.mr_op = &k6_mrops;
  186 }
  187 SYSINIT(k6memdev, SI_SUB_DRIVERS, SI_ORDER_FIRST, k6_mem_drvinit, NULL);

Cache object: e0a7f979eb000cf7f4fa2bc626ec4247


[ 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.