Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / bridge / ecc / ecc.c
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 static int setup_none(device_t dev);
51 static int setup_amd64(device_t dev);
52 static void poll_amd64(void *dev_arg);
53
54 struct pci_memory_controller { 
55         uint16_t        vid;
56         uint16_t        did;
57         const char      *desc;
58         int (*setup)(device_t dev);
59 };
60
61 struct pci_ecc_softc {
62         struct pci_memory_controller *config;
63         struct callout poll_callout;
64         int poll_enable;
65 };
66
67 static struct pci_memory_controller mem_controllers[] = {
68         /* AMD */
69         { 0x1022, 0x7006, "AMD 751", setup_none },
70         { 0x1022, 0x700c, "AMD 762", setup_none },
71         { 0x1022, 0x700e, "AMD 761", setup_none },
72         { 0x1022, 0x1100, "AMD 8000", setup_amd64 },
73         { 0x1022, 0x7454, "AMD 8000", setup_amd64 }
74 };
75
76 static int
77 pci_ecc_probe(device_t dev)
78 {
79         struct pci_ecc_softc *sc;
80         uint16_t vid;
81         uint16_t did;
82         int i;
83
84         vid = pci_get_vendor(dev);
85         did = pci_get_device(dev);
86
87         for (i = 0; i < NELEM(mem_controllers); ++i) {
88                 if (mem_controllers[i].vid == vid &&
89                     mem_controllers[i].did == did
90                 ) {
91                         sc = device_get_softc(dev);
92                         sc->config = &mem_controllers[i];
93                         return(0);
94                 }
95         }
96         return (ENXIO);
97 }
98
99 static int
100 pci_ecc_attach(device_t dev)
101 {
102         struct pci_ecc_softc *sc = device_get_softc(dev);
103
104         return (sc->config->setup(dev));
105 }
106
107 static device_method_t pci_ecc_methods[] = {
108         /* Device interface */
109         DEVMETHOD(device_probe,         pci_ecc_probe),
110         DEVMETHOD(device_attach,        pci_ecc_attach),
111         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
112         DEVMETHOD(device_suspend,       bus_generic_suspend),
113         DEVMETHOD(device_resume,        bus_generic_resume),
114         { 0, 0 }
115 };
116
117 static driver_t pci_ecc_driver = {
118         "ecc",
119         pci_ecc_methods,
120         sizeof(struct pci_ecc_softc)
121 };
122 static devclass_t ecc_devclass;
123 DRIVER_MODULE(ecc, pci, pci_ecc_driver, ecc_devclass, NULL, NULL);
124 MODULE_DEPEND(ecc, pci, 1, 1, 1);
125
126 /*
127  * Architecture-specific procedures
128  */
129 static
130 int
131 setup_none(device_t dev)
132 {
133         return(0);
134 }
135
136 static
137 int
138 setup_amd64(device_t dev)
139 {
140         struct pci_ecc_softc *sc = device_get_softc(dev);
141         uint32_t draminfo;
142         uint32_t eccinfo;
143         int bus = pci_get_bus(dev);
144         int slot = pci_get_slot(dev);
145
146         /*
147          * The memory bridge is recognized as four PCI devices
148          * using function codes 0, 1, 2, and 3.  We probe for the
149          * device at function code 0 and assume that all four exist.
150          */
151         draminfo = pcib_read_config(dev, bus, slot, 2, 0x90, 4);
152         eccinfo = pcib_read_config(dev, bus, slot, 3, 0x44, 4);
153
154         device_printf(dev, "attached %s memory controller\n", sc->config->desc);
155         if ((draminfo >> 17) & 1)
156                 device_printf(dev, "memory type: ECC\n");
157         else
158                 device_printf(dev, "memory type: NON-ECC\n");
159         switch((eccinfo >> 22) & 3) {
160         case 0:
161                 device_printf(dev, "ecc mode: DISABLED\n");
162                 break;
163         case 1:
164                 device_printf(dev, "ecc mode: ENABLED/CORRECT-MODE\n");
165                 sc->poll_enable = 1;
166                 break;
167         case 2:
168                 device_printf(dev, "ecc mode: ENABLED/RESERVED (disabled)\n");
169                 break;
170         case 3:
171                 device_printf(dev, "ecc mode: ENABLED/CHIPKILL-MODE\n");
172                 sc->poll_enable = 1;
173                 break;
174         }
175
176         /*
177          * Enable ECC logging and clear any previous error.
178          */
179         if (sc->poll_enable) {
180                 uint64_t v64;
181                 uint32_t v32;
182
183                 v64 = rdmsr(0x017B);
184                 wrmsr(0x17B, (v64 & ~0xFFFFFFFFLL) | 0x00000010LL);
185                 v32 = pcib_read_config(dev, bus, slot, 3, 0x4C, 4);
186                 v32 &= 0x7F801EFC;
187                 pcib_write_config(dev, bus, slot, 3, 0x4C, v32, 4);
188
189                 callout_init(&sc->poll_callout);
190                 callout_reset(&sc->poll_callout, hz, poll_amd64, dev);
191         }
192         return(0);
193 }
194
195 static
196 void
197 poll_amd64(void *dev_arg)
198 {
199         device_t dev = dev_arg;
200         struct pci_ecc_softc *sc = device_get_softc(dev);
201         int bus = pci_get_bus(dev);
202         int slot = pci_get_slot(dev);
203         uint32_t v32;
204         uint32_t addr;
205
206         /*
207          * The address calculation is not entirely correct.  We need to
208          * look at the AMD chipset documentation.
209          */
210         v32 = pcib_read_config(dev, bus, slot, 3, 0x4C, 4);
211         if ((v32 & 0x80004000) == 0x80004000) {
212                 addr = pcib_read_config(dev, bus, slot, 3, 0x50, 4);
213                 device_printf(dev, "Correctable ECC error at %08x\n", addr);
214                 pcib_write_config(dev, bus, slot, 3, 0x4C, v32 & 0x7F801EFC, 4);
215         } else if ((v32 & 0x80002000) == 0x80002000) {
216                 addr = pcib_read_config(dev, bus ,slot, 3, 0x50, 4);
217                 device_printf(dev, "Uncorrectable ECC error at %08x\n", addr);
218                 pcib_write_config(dev, bus, slot, 3, 0x4C, v32 & 0x7F801EFC, 4);
219         }
220         callout_reset(&sc->poll_callout, hz, poll_amd64, dev);
221 }
222