pci_machdep.c revision 1.37 1 1.37 dyoung /* $NetBSD: pci_machdep.c,v 1.37 2011/07/01 16:56:52 dyoung Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*
4 1.1 nonaka * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.1 nonaka * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * Redistribution and use in source and binary forms, with or without
8 1.1 nonaka * modification, are permitted provided that the following conditions
9 1.1 nonaka * are met:
10 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
11 1.1 nonaka * notice, this list of conditions and the following disclaimer.
12 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
14 1.1 nonaka * documentation and/or other materials provided with the distribution.
15 1.1 nonaka * 3. All advertising materials mentioning features or use of this software
16 1.1 nonaka * must display the following acknowledgement:
17 1.1 nonaka * This product includes software developed by Charles M. Hannum.
18 1.1 nonaka * 4. The name of the author may not be used to endorse or promote products
19 1.1 nonaka * derived from this software without specific prior written permission.
20 1.1 nonaka *
21 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 nonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 nonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 nonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 nonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 nonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 nonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 nonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 nonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 nonaka */
32 1.1 nonaka
33 1.1 nonaka /*
34 1.1 nonaka * Machine-specific functions for PCI autoconfiguration.
35 1.1 nonaka *
36 1.1 nonaka * On PCs, there are two methods of generating PCI configuration cycles.
37 1.1 nonaka * We try to detect the appropriate mechanism for this machine and set
38 1.1 nonaka * up a few function pointers to access the correct method directly.
39 1.1 nonaka */
40 1.1 nonaka
41 1.18 lukem #include <sys/cdefs.h>
42 1.37 dyoung __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.37 2011/07/01 16:56:52 dyoung Exp $");
43 1.18 lukem
44 1.1 nonaka #include <sys/types.h>
45 1.1 nonaka #include <sys/param.h>
46 1.1 nonaka #include <sys/time.h>
47 1.1 nonaka #include <sys/systm.h>
48 1.1 nonaka #include <sys/errno.h>
49 1.15 kleink #include <sys/extent.h>
50 1.1 nonaka #include <sys/device.h>
51 1.27 garbled #include <sys/malloc.h>
52 1.1 nonaka
53 1.6 mrg #include <uvm/uvm_extern.h>
54 1.1 nonaka
55 1.10 nonaka #define _POWERPC_BUS_DMA_PRIVATE
56 1.37 dyoung #include <sys/bus.h>
57 1.1 nonaka #include <machine/intr.h>
58 1.13 nonaka #include <machine/platform.h>
59 1.24 garbled #include <machine/pnp.h>
60 1.1 nonaka
61 1.1 nonaka #include <dev/isa/isavar.h>
62 1.15 kleink
63 1.1 nonaka #include <dev/pci/pcivar.h>
64 1.1 nonaka #include <dev/pci/pcireg.h>
65 1.1 nonaka #include <dev/pci/pcidevs.h>
66 1.15 kleink #include <dev/pci/pciconf.h>
67 1.13 nonaka
68 1.27 garbled /* 0 == direct 1 == indirect */
69 1.27 garbled int prep_pci_config_mode = 1;
70 1.32 garbled extern struct genppc_pci_chipset *genppc_pct;
71 1.32 garbled extern u_int32_t prep_pci_baseaddr;
72 1.32 garbled extern u_int32_t prep_pci_basedata;
73 1.32 garbled
74 1.32 garbled static void
75 1.32 garbled prep_pci_get_chipset_tag_indirect(pci_chipset_tag_t pc)
76 1.32 garbled {
77 1.32 garbled
78 1.32 garbled pc->pc_conf_v = (void *)pc;
79 1.32 garbled
80 1.32 garbled pc->pc_attach_hook = genppc_pci_indirect_attach_hook;
81 1.32 garbled pc->pc_bus_maxdevs = prep_pci_bus_maxdevs;
82 1.32 garbled pc->pc_make_tag = genppc_pci_indirect_make_tag;
83 1.32 garbled pc->pc_conf_read = genppc_pci_indirect_conf_read;
84 1.32 garbled pc->pc_conf_write = genppc_pci_indirect_conf_write;
85 1.32 garbled
86 1.32 garbled pc->pc_intr_v = (void *)pc;
87 1.32 garbled
88 1.32 garbled pc->pc_intr_map = prep_pci_intr_map;
89 1.32 garbled pc->pc_intr_string = genppc_pci_intr_string;
90 1.32 garbled pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
91 1.32 garbled pc->pc_intr_establish = genppc_pci_intr_establish;
92 1.32 garbled pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
93 1.35 matt pc->pc_intr_setattr = genppc_pci_intr_setattr;
94 1.32 garbled
95 1.32 garbled pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
96 1.32 garbled pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
97 1.32 garbled pc->pc_conf_hook = prep_pci_conf_hook;
98 1.32 garbled
99 1.36 matt pc->pc_addr = mapiodev(prep_pci_baseaddr, 4, false);
100 1.36 matt pc->pc_data = mapiodev(prep_pci_basedata, 4, false);
101 1.32 garbled pc->pc_bus = 0;
102 1.32 garbled pc->pc_node = 0;
103 1.32 garbled pc->pc_memt = 0;
104 1.32 garbled pc->pc_iot = 0;
105 1.32 garbled }
106 1.27 garbled
107 1.24 garbled void
108 1.24 garbled prep_pci_get_chipset_tag(pci_chipset_tag_t pc)
109 1.24 garbled {
110 1.24 garbled int i;
111 1.24 garbled
112 1.24 garbled i = pci_chipset_tag_type();
113 1.24 garbled
114 1.27 garbled if (i == PCIBridgeIndirect || i == PCIBridgeRS6K) {
115 1.27 garbled prep_pci_config_mode = 1;
116 1.24 garbled prep_pci_get_chipset_tag_indirect(pc);
117 1.27 garbled } else if (i == PCIBridgeDirect) {
118 1.24 garbled prep_pci_get_chipset_tag_direct(pc);
119 1.27 garbled prep_pci_config_mode = 0;
120 1.27 garbled } else
121 1.24 garbled panic("Unknown PCI chipset tag configuration method");
122 1.24 garbled }
123 1.24 garbled
124 1.1 nonaka int
125 1.33 matt prep_pci_bus_maxdevs(void *v, int busno)
126 1.1 nonaka {
127 1.32 garbled struct genppc_pci_chipset_businfo *pbi;
128 1.27 garbled prop_object_t busmax;
129 1.27 garbled
130 1.32 garbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
131 1.27 garbled while (busno--)
132 1.27 garbled pbi = SIMPLEQ_NEXT(pbi, next);
133 1.27 garbled if (pbi == NULL)
134 1.27 garbled return 32;
135 1.27 garbled
136 1.27 garbled busmax = prop_dictionary_get(pbi->pbi_properties,
137 1.27 garbled "prep-pcibus-maxdevices");
138 1.27 garbled if (busmax == NULL)
139 1.27 garbled return 32;
140 1.27 garbled else
141 1.27 garbled return prop_number_integer_value(busmax);
142 1.1 nonaka
143 1.27 garbled return 32;
144 1.1 nonaka }
145 1.1 nonaka
146 1.1 nonaka int
147 1.34 dyoung prep_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
148 1.1 nonaka {
149 1.32 garbled struct genppc_pci_chipset_businfo *pbi;
150 1.27 garbled prop_dictionary_t dict, devsub;
151 1.27 garbled prop_object_t pinsub;
152 1.27 garbled prop_number_t pbus;
153 1.31 garbled int busno, bus, pin, line, swiz, dev, origdev, i;
154 1.27 garbled char key[20];
155 1.27 garbled
156 1.27 garbled pin = pa->pa_intrpin;
157 1.27 garbled line = pa->pa_intrline;
158 1.27 garbled bus = busno = pa->pa_bus;
159 1.27 garbled swiz = pa->pa_intrswiz;
160 1.31 garbled origdev = dev = pa->pa_device;
161 1.30 garbled i = 0;
162 1.27 garbled
163 1.32 garbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
164 1.27 garbled while (busno--)
165 1.27 garbled pbi = SIMPLEQ_NEXT(pbi, next);
166 1.27 garbled KASSERT(pbi != NULL);
167 1.27 garbled
168 1.27 garbled dict = prop_dictionary_get(pbi->pbi_properties, "prep-pci-intrmap");
169 1.31 garbled
170 1.30 garbled if (dict != NULL)
171 1.30 garbled i = prop_dictionary_count(dict);
172 1.30 garbled
173 1.30 garbled if (dict == NULL || i == 0) {
174 1.27 garbled /* We have a non-PReP bus. now it gets hard */
175 1.27 garbled pbus = prop_dictionary_get(pbi->pbi_properties,
176 1.27 garbled "prep-pcibus-parent");
177 1.27 garbled if (pbus == NULL)
178 1.27 garbled goto bad;
179 1.27 garbled busno = prop_number_integer_value(pbus);
180 1.27 garbled pbus = prop_dictionary_get(pbi->pbi_properties,
181 1.27 garbled "prep-pcibus-rawdevnum");
182 1.27 garbled dev = prop_number_integer_value(pbus);
183 1.27 garbled
184 1.27 garbled /* now that we know the parent bus, we need to find it's pbi */
185 1.32 garbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
186 1.27 garbled while (busno--)
187 1.27 garbled pbi = SIMPLEQ_NEXT(pbi, next);
188 1.27 garbled KASSERT(pbi != NULL);
189 1.27 garbled
190 1.31 garbled /* swizzle the pin */
191 1.31 garbled pin = ((pin + origdev - 1) & 3) + 1;
192 1.31 garbled
193 1.27 garbled /* now we have the pbi, ask for dict again */
194 1.27 garbled dict = prop_dictionary_get(pbi->pbi_properties,
195 1.27 garbled "prep-pci-intrmap");
196 1.27 garbled if (dict == NULL)
197 1.27 garbled goto bad;
198 1.27 garbled }
199 1.1 nonaka
200 1.27 garbled /* No IRQ used. */
201 1.27 garbled if (pin == 0)
202 1.1 nonaka goto bad;
203 1.1 nonaka if (pin > 4) {
204 1.32 garbled aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
205 1.1 nonaka goto bad;
206 1.1 nonaka }
207 1.1 nonaka
208 1.27 garbled sprintf(key, "devfunc-%d", dev);
209 1.27 garbled devsub = prop_dictionary_get(dict, key);
210 1.27 garbled if (devsub == NULL)
211 1.27 garbled goto bad;
212 1.27 garbled sprintf(key, "pin-%c", 'A' + (pin-1));
213 1.27 garbled pinsub = prop_dictionary_get(devsub, key);
214 1.27 garbled if (pinsub == NULL)
215 1.27 garbled goto bad;
216 1.27 garbled line = prop_number_integer_value(pinsub);
217 1.27 garbled
218 1.1 nonaka /*
219 1.1 nonaka * Section 6.2.4, `Miscellaneous Functions', says that 255 means
220 1.1 nonaka * `unknown' or `no connection' on a PC. We assume that a device with
221 1.1 nonaka * `no connection' either doesn't have an interrupt (in which case the
222 1.1 nonaka * pin number should be 0, and would have been noticed above), or
223 1.1 nonaka * wasn't configured by the BIOS (in which case we punt, since there's
224 1.1 nonaka * no real way we can know how the interrupt lines are mapped in the
225 1.1 nonaka * hardware).
226 1.1 nonaka *
227 1.1 nonaka * XXX
228 1.1 nonaka * Since IRQ 0 is only used by the clock, and we can't actually be sure
229 1.1 nonaka * that the BIOS did its job, we also recognize that as meaning that
230 1.1 nonaka * the BIOS has not configured the device.
231 1.1 nonaka */
232 1.1 nonaka if (line == 0 || line == 255) {
233 1.32 garbled aprint_error("pci_intr_map: no mapping for pin %c\n",
234 1.32 garbled '@' + pin);
235 1.1 nonaka goto bad;
236 1.1 nonaka } else {
237 1.1 nonaka if (line >= ICU_LEN) {
238 1.32 garbled aprint_error("pci_intr_map: bad interrupt line %d\n",
239 1.32 garbled line);
240 1.1 nonaka goto bad;
241 1.1 nonaka }
242 1.1 nonaka if (line == IRQ_SLAVE) {
243 1.32 garbled aprint_verbose("pci_intr_map: changed line 2 to line 9\n");
244 1.1 nonaka line = 9;
245 1.1 nonaka }
246 1.1 nonaka }
247 1.1 nonaka
248 1.1 nonaka *ihp = line;
249 1.1 nonaka return 0;
250 1.1 nonaka
251 1.1 nonaka bad:
252 1.1 nonaka *ihp = -1;
253 1.1 nonaka return 1;
254 1.1 nonaka }
255 1.1 nonaka
256 1.27 garbled extern pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
257 1.32 garbled extern pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
258 1.27 garbled extern pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
259 1.32 garbled extern pcireg_t genppc_pci_indirect_conf_read(void *, pcitag_t, int);
260 1.15 kleink
261 1.15 kleink int
262 1.35 matt prep_pci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
263 1.15 kleink {
264 1.35 matt pci_chipset_tag_t pc = v;
265 1.32 garbled struct genppc_pci_chipset_businfo *pbi;
266 1.27 garbled prop_number_t bmax, pbus;
267 1.27 garbled pcitag_t tag;
268 1.27 garbled pcireg_t class;
269 1.15 kleink
270 1.15 kleink /*
271 1.15 kleink * The P9100 board found in some IBM machines cannot be
272 1.15 kleink * over-configured.
273 1.15 kleink */
274 1.15 kleink if (PCI_VENDOR(id) == PCI_VENDOR_WEITEK &&
275 1.15 kleink PCI_PRODUCT(id) == PCI_PRODUCT_WEITEK_P9100)
276 1.15 kleink return 0;
277 1.15 kleink
278 1.25 snj /* We have already mapped the MPIC2 if we have one, so leave it
279 1.23 garbled alone */
280 1.23 garbled if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
281 1.23 garbled PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
282 1.23 garbled return 0;
283 1.23 garbled
284 1.28 garbled if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
285 1.28 garbled PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
286 1.28 garbled return 0;
287 1.28 garbled
288 1.27 garbled if (PCI_VENDOR(id) == PCI_VENDOR_INTEL &&
289 1.27 garbled PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_PCEB)
290 1.27 garbled return 0;
291 1.27 garbled
292 1.29 garbled if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
293 1.29 garbled PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
294 1.29 garbled return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
295 1.29 garbled
296 1.27 garbled /* NOTE, all device specific stuff must be above this line */
297 1.27 garbled /* don't do this on the primary host bridge */
298 1.27 garbled if (bus == 0 && dev == 0 && func == 0)
299 1.27 garbled return PCI_CONF_DEFAULT;
300 1.27 garbled
301 1.27 garbled if (prep_pci_config_mode) {
302 1.35 matt tag = genppc_pci_indirect_make_tag(pc, bus, dev, func);
303 1.35 matt class = genppc_pci_indirect_conf_read(pc, tag,
304 1.27 garbled PCI_CLASS_REG);
305 1.27 garbled } else {
306 1.35 matt tag = prep_pci_direct_make_tag(pc, bus, dev, func);
307 1.35 matt class = prep_pci_direct_conf_read(pc, tag,
308 1.27 garbled PCI_CLASS_REG);
309 1.27 garbled }
310 1.27 garbled
311 1.27 garbled /*
312 1.27 garbled * PCI bridges have special needs. We need to discover where they
313 1.27 garbled * came from, and wire them appropriately.
314 1.27 garbled */
315 1.27 garbled if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
316 1.27 garbled PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
317 1.32 garbled pbi = malloc(sizeof(struct genppc_pci_chipset_businfo),
318 1.32 garbled M_DEVBUF, M_NOWAIT);
319 1.27 garbled KASSERT(pbi != NULL);
320 1.27 garbled pbi->pbi_properties = prop_dictionary_create();
321 1.27 garbled KASSERT(pbi->pbi_properties != NULL);
322 1.27 garbled setup_pciintr_map(pbi, bus, dev, func);
323 1.27 garbled
324 1.27 garbled /* record the parent bus, and the parent device number */
325 1.27 garbled pbus = prop_number_create_integer(bus);
326 1.27 garbled prop_dictionary_set(pbi->pbi_properties, "prep-pcibus-parent",
327 1.27 garbled pbus);
328 1.27 garbled prop_object_release(pbus);
329 1.27 garbled pbus = prop_number_create_integer(dev);
330 1.27 garbled prop_dictionary_set(pbi->pbi_properties,
331 1.27 garbled "prep-pcibus-rawdevnum", pbus);
332 1.27 garbled prop_object_release(pbus);
333 1.27 garbled
334 1.27 garbled /* now look for bus quirks */
335 1.27 garbled
336 1.27 garbled if (PCI_VENDOR(id) == PCI_VENDOR_DEC &&
337 1.27 garbled PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21154) {
338 1.27 garbled bmax = prop_number_create_integer(8);
339 1.27 garbled KASSERT(bmax != NULL);
340 1.27 garbled prop_dictionary_set(pbi->pbi_properties,
341 1.27 garbled "prep-pcibus-maxdevices", bmax);
342 1.27 garbled prop_object_release(bmax);
343 1.27 garbled }
344 1.27 garbled
345 1.32 garbled SIMPLEQ_INSERT_TAIL(&genppc_pct->pc_pbi, pbi, next);
346 1.27 garbled }
347 1.27 garbled
348 1.22 gdamore return (PCI_CONF_DEFAULT);
349 1.1 nonaka }
350