1 /* 2 * Copyright (c) 2006 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com>. AMD register addresses and 6 * values were pulled from MemTest-86 and Linux. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bus.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 42 #include <bus/pci/pcivar.h> 43 #include <bus/pci/pcireg.h> 44 #include <bus/pci/pcibus.h> 45 #include <bus/pci/pci_cfgreg.h> 46 #include <bus/pci/pcib_private.h> 47 48 #include "pcib_if.h" 49 50 struct ecc_amd8000_memctrl { 51 uint16_t vid; 52 uint16_t did; 53 const char *desc; 54 }; 55 56 struct ecc_amd8000_softc { 57 device_t ecc_device; 58 device_t ecc_mydev; 59 struct callout ecc_callout; 60 }; 61 62 #define ecc_printf(sc, fmt, arg...) \ 63 device_printf((sc)->ecc_mydev, fmt , ##arg) 64 65 static void ecc_amd8000_callout(void *); 66 static void ecc_amd8000_stop(device_t); 67 68 static int ecc_amd8000_probe(device_t); 69 static int ecc_amd8000_attach(device_t); 70 static int ecc_amd8000_detach(device_t); 71 static void ecc_amd8000_shutdown(device_t); 72 73 static const struct ecc_amd8000_memctrl ecc_memctrls[] = { 74 { 0x1022, 0x1100, "AMD 8000 memory controller" }, 75 { 0x1022, 0x7454, "AMD 8151 memory controller" }, 76 { 0, 0, NULL } /* required last entry */ 77 }; 78 79 static device_method_t ecc_amd8000_methods[] = { 80 /* Device interface */ 81 DEVMETHOD(device_probe, ecc_amd8000_probe), 82 DEVMETHOD(device_attach, ecc_amd8000_attach), 83 DEVMETHOD(device_detach, ecc_amd8000_detach), 84 DEVMETHOD(device_shutdown, ecc_amd8000_shutdown), 85 DEVMETHOD(device_suspend, bus_generic_suspend), 86 DEVMETHOD(device_resume, bus_generic_resume), 87 DEVMETHOD_END 88 }; 89 90 static driver_t ecc_amd8000_driver = { 91 "ecc", 92 ecc_amd8000_methods, 93 sizeof(struct ecc_amd8000_softc) 94 }; 95 static devclass_t ecc_devclass; 96 DRIVER_MODULE(ecc_amd8000, pci, ecc_amd8000_driver, ecc_devclass, NULL, NULL); 97 MODULE_DEPEND(ecc_amd8000, pci, 1, 1, 1); 98 99 static int 100 ecc_amd8000_probe(device_t dev) 101 { 102 const struct ecc_amd8000_memctrl *mc; 103 uint16_t vid, did; 104 105 vid = pci_get_vendor(dev); 106 did = pci_get_device(dev); 107 108 for (mc = ecc_memctrls; mc->desc != NULL; ++mc) { 109 if (mc->vid == vid && mc->did == did) { 110 struct ecc_amd8000_softc *sc = device_get_softc(dev); 111 112 device_set_desc(dev, mc->desc); 113 sc->ecc_mydev = dev; 114 sc->ecc_device = device_get_parent(dev); 115 return (0); 116 } 117 } 118 return (ENXIO); 119 } 120 121 static int 122 ecc_amd8000_attach(device_t dev) 123 { 124 struct ecc_amd8000_softc *sc = device_get_softc(dev); 125 uint32_t draminfo, eccinfo; 126 int bus, slot, poll = 0; 127 128 callout_init_mp(&sc->ecc_callout); 129 130 dev = sc->ecc_device; /* XXX */ 131 132 bus = pci_get_bus(dev); 133 slot = pci_get_slot(dev); 134 135 /* 136 * The memory bridge is recognized as four PCI devices 137 * using function codes 0, 1, 2, and 3. We probe for the 138 * device at function code 0 and assume that all four exist. 139 */ 140 draminfo = pcib_read_config(dev, bus, slot, 2, 0x90, 4); 141 eccinfo = pcib_read_config(dev, bus, slot, 3, 0x44, 4); 142 143 if ((draminfo >> 17) & 1) 144 ecc_printf(sc, "memory type: ECC\n"); 145 else 146 ecc_printf(sc, "memory type: NON-ECC\n"); 147 switch((eccinfo >> 22) & 3) { 148 case 0: 149 ecc_printf(sc, "ecc mode: DISABLED\n"); 150 break; 151 case 1: 152 ecc_printf(sc, "ecc mode: ENABLED/CORRECT-MODE\n"); 153 poll = 1; 154 break; 155 case 2: 156 ecc_printf(sc, "ecc mode: ENABLED/RESERVED (disabled)\n"); 157 break; 158 case 3: 159 ecc_printf(sc, "ecc mode: ENABLED/CHIPKILL-MODE\n"); 160 poll = 1; 161 break; 162 } 163 164 /* 165 * Enable ECC logging and clear any previous error. 166 */ 167 if (poll) { 168 uint64_t v64; 169 uint32_t v32; 170 171 v64 = rdmsr(0x017B); 172 wrmsr(0x17B, (v64 & ~0xFFFFFFFFLL) | 0x00000010LL); 173 v32 = pcib_read_config(dev, bus, slot, 3, 0x4C, 4); 174 v32 &= 0x7F801EFC; 175 pcib_write_config(dev, bus, slot, 3, 0x4C, v32, 4); 176 177 callout_reset(&sc->ecc_callout, hz, ecc_amd8000_callout, sc); 178 } 179 return (0); 180 } 181 182 static void 183 ecc_amd8000_callout(void *xsc) 184 { 185 struct ecc_amd8000_softc *sc = xsc; 186 device_t dev = sc->ecc_device; 187 uint32_t v32, addr; 188 int bus, slot; 189 190 bus = pci_get_bus(dev); 191 slot = pci_get_slot(dev); 192 193 /* 194 * The address calculation is not entirely correct. We need to 195 * look at the AMD chipset documentation. 196 */ 197 v32 = pcib_read_config(dev, bus, slot, 3, 0x4C, 4); 198 if ((v32 & 0x80004000) == 0x80004000) { 199 addr = pcib_read_config(dev, bus, slot, 3, 0x50, 4); 200 ecc_printf(sc, "Correctable ECC error at %08x\n", addr); 201 pcib_write_config(dev, bus, slot, 3, 0x4C, v32 & 0x7F801EFC, 4); 202 } else if ((v32 & 0x80002000) == 0x80002000) { 203 addr = pcib_read_config(dev, bus ,slot, 3, 0x50, 4); 204 ecc_printf(sc, "Uncorrectable ECC error at %08x\n", addr); 205 pcib_write_config(dev, bus, slot, 3, 0x4C, v32 & 0x7F801EFC, 4); 206 } 207 callout_reset(&sc->ecc_callout, hz, ecc_amd8000_callout, sc); 208 } 209 210 static void 211 ecc_amd8000_stop(device_t dev) 212 { 213 struct ecc_amd8000_softc *sc = device_get_softc(dev); 214 215 callout_stop_sync(&sc->ecc_callout); 216 } 217 218 static int 219 ecc_amd8000_detach(device_t dev) 220 { 221 ecc_amd8000_stop(dev); 222 return 0; 223 } 224 225 static void 226 ecc_amd8000_shutdown(device_t dev) 227 { 228 ecc_amd8000_stop(dev); 229 } 230