]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/pci/pci_cfgreg.c
Merge ath again (addition of wisoc files).
[FreeBSD/FreeBSD.git] / sys / amd64 / pci / pci_cfgreg.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <dev/pci/pcivar.h>
38 #include <dev/pci/pcireg.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <machine/pci_cfgreg.h>
42
43 enum {
44         CFGMECH_NONE = 0,
45         CFGMECH_1,
46         CFGMECH_PCIE,
47 };
48
49 static int      pciereg_cfgread(int bus, unsigned slot, unsigned func,
50                     unsigned reg, unsigned bytes);
51 static void     pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
52                     unsigned reg, int data, unsigned bytes);
53 static int      pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
54 static void     pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
55
56 static int cfgmech;
57 static vm_offset_t pcie_base;
58 static int pcie_minbus, pcie_maxbus;
59 static struct mtx pcicfg_mtx;
60
61 /* 
62  * Initialise access to PCI configuration space 
63  */
64 int
65 pci_cfgregopen(void)
66 {
67         uint64_t pciebar;
68         uint16_t did, vid;
69
70         if (cfgmech != CFGMECH_NONE)
71                 return (1);
72         mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
73         cfgmech = CFGMECH_1;
74
75         /*
76          * Grope around in the PCI config space to see if this is a
77          * chipset that is capable of doing memory-mapped config cycles.
78          * This also implies that it can do PCIe extended config cycles.
79          */
80
81         /* Check for supported chipsets */
82         vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
83         did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
84         switch (vid) {
85         case 0x8086:
86                 switch (did) {
87                 case 0x3590:
88                 case 0x3592:
89                         /* Intel 7520 or 7320 */
90                         pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
91                         pcie_cfgregopen(pciebar, 0, 255);
92                         break;
93                 case 0x2580:
94                 case 0x2584:
95                 case 0x2590:
96                         /* Intel 915, 925, or 915GM */
97                         pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
98                         pcie_cfgregopen(pciebar, 0, 255);
99                         break;
100                 }
101         }
102
103         return (1);
104 }
105
106 /* 
107  * Read configuration space register
108  */
109 u_int32_t
110 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
111 {
112         uint32_t line;
113
114         /*
115          * Some BIOS writers seem to want to ignore the spec and put
116          * 0 in the intline rather than 255 to indicate none.  Some use
117          * numbers in the range 128-254 to indicate something strange and
118          * apparently undocumented anywhere.  Assume these are completely bogus
119          * and map them to 255, which the rest of the PCI code recognizes as
120          * as an invalid IRQ.
121          */
122         if (reg == PCIR_INTLINE && bytes == 1) {
123                 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
124                 if (line == 0 || line >= 128)
125                         line = PCI_INVALID_IRQ;
126                 return (line);
127         }
128         return (pcireg_cfgread(bus, slot, func, reg, bytes));
129 }
130
131 /* 
132  * Write configuration space register 
133  */
134 void
135 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
136 {
137
138         pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
139 }
140
141 /* 
142  * Configuration space access using direct register operations
143  */
144
145 /* enable configuration space accesses and return data port address */
146 static int
147 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
148 {
149         int dataport = 0;
150
151         if (bus <= PCI_BUSMAX && slot < 32 && func <= PCI_FUNCMAX &&
152             reg <= PCI_REGMAX && bytes != 3 && (unsigned) bytes <= 4 &&
153             (reg & (bytes - 1)) == 0) {
154                 outl(CONF1_ADDR_PORT, (1 << 31) | (bus << 16) | (slot << 11) 
155                     | (func << 8) | (reg & ~0x03));
156                 dataport = CONF1_DATA_PORT + (reg & 0x03);
157         }
158         return (dataport);
159 }
160
161 /* disable configuration space accesses */
162 static void
163 pci_cfgdisable(void)
164 {
165
166         /*
167          * Do nothing.  Writing a 0 to the address port can apparently
168          * confuse some bridges and cause spurious access failures.
169          */
170 }
171
172 static int
173 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
174 {
175         int data = -1;
176         int port;
177
178         if (cfgmech == CFGMECH_PCIE) {
179                 data = pciereg_cfgread(bus, slot, func, reg, bytes);
180                 return (data);
181         }
182
183         mtx_lock_spin(&pcicfg_mtx);
184         port = pci_cfgenable(bus, slot, func, reg, bytes);
185         if (port != 0) {
186                 switch (bytes) {
187                 case 1:
188                         data = inb(port);
189                         break;
190                 case 2:
191                         data = inw(port);
192                         break;
193                 case 4:
194                         data = inl(port);
195                         break;
196                 }
197                 pci_cfgdisable();
198         }
199         mtx_unlock_spin(&pcicfg_mtx);
200         return (data);
201 }
202
203 static void
204 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
205 {
206         int port;
207
208         if (cfgmech == CFGMECH_PCIE) {
209                 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
210                 return;
211         }
212
213         mtx_lock_spin(&pcicfg_mtx);
214         port = pci_cfgenable(bus, slot, func, reg, bytes);
215         if (port != 0) {
216                 switch (bytes) {
217                 case 1:
218                         outb(port, data);
219                         break;
220                 case 2:
221                         outw(port, data);
222                         break;
223                 case 4:
224                         outl(port, data);
225                         break;
226                 }
227                 pci_cfgdisable();
228         }
229         mtx_unlock_spin(&pcicfg_mtx);
230 }
231
232 int
233 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
234 {
235
236         if (minbus != 0)
237                 return (0);
238
239         if (bootverbose)
240                 printf("PCIe: Memory Mapped configuration base @ 0x%lx\n",
241                     base);
242
243         /* XXX: We should make sure this really fits into the direct map. */
244         pcie_base = (vm_offset_t)pmap_mapdev(base, (maxbus + 1) << 20);
245         pcie_minbus = minbus;
246         pcie_maxbus = maxbus;
247         cfgmech = CFGMECH_PCIE;
248         return (1);
249 }
250
251 #define PCIE_VADDR(base, reg, bus, slot, func)  \
252         ((base)                         +       \
253         ((((bus) & 0xff) << 20)         |       \
254         (((slot) & 0x1f) << 15)         |       \
255         (((func) & 0x7) << 12)          |       \
256         ((reg) & 0xfff)))
257
258 static int
259 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
260     unsigned bytes)
261 {
262         volatile vm_offset_t va;
263         int data = -1;
264
265         if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
266             func > PCI_FUNCMAX || reg >= 0x1000)
267                 return (-1);
268
269         va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
270
271         switch (bytes) {
272         case 4:
273                 data = *(volatile uint32_t *)(va);
274                 break;
275         case 2:
276                 data = *(volatile uint16_t *)(va);
277                 break;
278         case 1:
279                 data = *(volatile uint8_t *)(va);
280                 break;
281         }
282
283         return (data);
284 }
285
286 static void
287 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
288     unsigned bytes)
289 {
290         volatile vm_offset_t va;
291
292         if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
293             func > PCI_FUNCMAX || reg >= 0x1000)
294                 return;
295
296         va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
297
298         switch (bytes) {
299         case 4:
300                 *(volatile uint32_t *)(va) = data;
301                 break;
302         case 2:
303                 *(volatile uint16_t *)(va) = data;
304                 break;
305         case 1:
306                 *(volatile uint8_t *)(va) = data;
307                 break;
308         }
309 }