xref: /netbsd-src/sys/arch/powerpc/pci/pci_machdep_common.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /* $NetBSD: pci_machdep_common.c,v 1.15 2012/02/01 09:54:03 matt Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tim Rightnour
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Generic PowerPC functions for dealing with a PCI bridge.  For most cases,
34  * these functions will work just fine, however, some machines may need
35  * specialized code, so those ports are free to write thier own functions
36  * and call those instead where appropriate.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: pci_machdep_common.c,v 1.15 2012/02/01 09:54:03 matt Exp $");
41 
42 #define _POWERPC_BUS_DMA_PRIVATE
43 
44 #include <sys/param.h>
45 #include <sys/bus.h>
46 #include <sys/device.h>
47 #include <sys/errno.h>
48 #include <sys/extent.h>
49 #include <sys/intr.h>
50 #include <sys/systm.h>
51 #include <sys/time.h>
52 
53 #include <uvm/uvm_extern.h>
54 
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pcireg.h>
57 #include <dev/pci/pcidevs.h>
58 #include <dev/pci/pciconf.h>
59 #include <dev/pci/pciidereg.h>
60 
61 /*
62  * PCI doesn't have any special needs; just use the generic versions
63  * of these functions.
64  */
65 struct powerpc_bus_dma_tag pci_bus_dma_tag = {
66 	0,			/* _bounce_thresh */
67 	_bus_dmamap_create,
68 	_bus_dmamap_destroy,
69 	_bus_dmamap_load,
70 	_bus_dmamap_load_mbuf,
71 	_bus_dmamap_load_uio,
72 	_bus_dmamap_load_raw,
73 	_bus_dmamap_unload,
74 	NULL,			/* _dmamap_sync */
75 	_bus_dmamem_alloc,
76 	_bus_dmamem_free,
77 	_bus_dmamem_map,
78 	_bus_dmamem_unmap,
79 	_bus_dmamem_mmap,
80 };
81 
82 int
83 genppc_pci_bus_maxdevs(void *v, int busno)
84 {
85 	return 32;
86 }
87 
88 const char *
89 genppc_pci_intr_string(void *v, pci_intr_handle_t ih)
90 {
91 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
92 
93 #ifdef ICU_LEN
94 	if (ih == 0 || ih >= ICU_LEN
95 /* XXX on macppc it's completely legal to have PCI interrupts on a slave PIC */
96 #ifdef IRQ_SLAVE
97 	    || ih == IRQ_SLAVE
98 #endif
99 	    )
100 		panic("pci_intr_string: bogus handle 0x%x", ih);
101 #endif
102 
103 	sprintf(irqstr, "irq %d", ih);
104 	return (irqstr);
105 
106 }
107 
108 const struct evcnt *
109 genppc_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
110 {
111 
112 	/* XXX for now, no evcnt parent reported */
113 	return NULL;
114 }
115 
116 void *
117 genppc_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
118     int (*func)(void *), void *arg)
119 {
120 
121 #ifdef ICU_LEN
122 	if (ih == 0 || ih >= ICU_LEN
123 #ifdef IRQ_SLAVE
124 	    || ih == IRQ_SLAVE
125 #endif
126 	    )
127 		panic("pci_intr_establish: bogus handle 0x%x", ih);
128 #endif
129 
130 	return intr_establish(ih, IST_LEVEL, level, func, arg);
131 }
132 
133 void
134 genppc_pci_intr_disestablish(void *v, void *cookie)
135 {
136 
137 	intr_disestablish(cookie);
138 }
139 
140 int
141 genppc_pci_intr_setattr(void *v, pci_intr_handle_t *ihp, int attr,
142     uint64_t data)
143 {
144 
145 	return ENODEV;
146 }
147 
148 void
149 genppc_pci_conf_interrupt(void *v, int bus, int dev, int pin,
150     int swiz, int *iline)
151 {
152 	/* do nothing */
153 }
154 
155 int
156 genppc_pci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
157 {
158 	return (PCI_CONF_DEFAULT);
159 }
160 
161 int
162 genppc_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
163 {
164 	int pin = pa->pa_intrpin;
165 	int line = pa->pa_intrline;
166 
167 #ifdef DEBUG
168 	printf("%s: pin: %d, line: %d\n", __func__, pin, line);
169 #endif
170 
171 	if (pin == 0) {
172 		/* No IRQ used. */
173 		aprint_error("pci_intr_map: interrupt pin %d\n", pin);
174 		goto bad;
175 	}
176 
177 	if (pin > 4) {
178 		aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
179 		goto bad;
180 	}
181 
182 	/*
183 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
184 	 * `unknown' or `no connection' on a PC.  We assume that a device with
185 	 * `no connection' either doesn't have an interrupt (in which case the
186 	 * pin number should be 0, and would have been noticed above), or
187 	 * wasn't configured by the BIOS (in which case we punt, since there's
188 	 * no real way we can know how the interrupt lines are mapped in the
189 	 * hardware).
190 	 *
191 	 * XXX
192 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
193 	 * that the BIOS did its job, we also recognize that as meaning that
194 	 * the BIOS has not configured the device.
195 	 */
196 	if (line == 0 || line == 255) {
197 		aprint_error("pci_intr_map: no mapping for pin %c\n", '@' + pin);
198 		goto bad;
199 #ifdef ICU_LEN
200 	} else {
201 		if (line >= ICU_LEN) {
202 			aprint_error("pci_intr_map: bad interrupt line %d\n", line);
203 			goto bad;
204 		}
205 #endif
206 	}
207 
208 	*ihp = line;
209 	return 0;
210 
211 bad:
212 	*ihp = -1;
213 	return 1;
214 }
215 
216 int
217 genppc_pci_msi_request(const struct pci_attach_args *pa,
218     pci_msi_handle_t *msihp, size_t nmsirq, int ipl, int capid)
219 {
220 	return EOPNOTSUPP;
221 }
222 
223 int
224 genppc_pci_msi_type(void *v, pci_msi_handle_t msih)
225 {
226 	panic("%s", __func__);
227 }
228 
229 size_t
230 genppc_pci_msi_available(void *v, pci_msi_handle_t msih)
231 {
232 	panic("%s", __func__);
233 }
234 
235 const char *
236 genppc_pci_msi_string(void *v, pci_msi_handle_t msih, size_t msirq)
237 {
238 	panic("%s", __func__);
239 }
240 
241 const struct evcnt *
242 genppc_pci_msi_evcnt(void *v, pci_msi_handle_t msih, size_t msirq)
243 {
244 	panic("%s", __func__);
245 }
246 
247 void *
248 genppc_pci_msi_establish(void *v, pci_msi_handle_t msih, size_t msirq,
249 		    int ipl, int (*func)(void *), void *arg)
250 {
251 	panic("%s", __func__);
252 }
253 
254 void *
255 genppc_pci_msix_establish(void *v, pci_msi_handle_t msih, size_t vec,
256     size_t msirq, int ipl, int (*func)(void *), void *arg)
257 {
258 	panic("%s", __func__);
259 }
260 
261 void
262 genppc_pci_msi_disestablish(void *v, void *ih)
263 {
264 	panic("%s", __func__);
265 }
266 
267 void
268 genppc_pci_msi_free(void *v, pci_msi_handle_t msih, size_t msirq)
269 {
270 	panic("%s", __func__);
271 }
272 
273 void
274 genppc_pci_msi_release(void *v, pci_msi_handle_t msih)
275 {
276 	panic("%s", __func__);
277 }
278 
279 void
280 genppc_pci_chipset_msi_init(pci_chipset_tag_t pc)
281 {
282 	pc->pc_msi_request = genppc_pci_msi_request;
283 	pc->pc_msi_type = genppc_pci_msi_type;
284 	pc->pc_msi_available = genppc_pci_msi_available;
285 	pc->pc_msi_evcnt = genppc_pci_msi_evcnt;
286 	pc->pc_msi_string = genppc_pci_msi_string;
287 	pc->pc_msi_establish = genppc_pci_msi_establish;
288 	pc->pc_msix_establish = genppc_pci_msix_establish;
289 	pc->pc_msi_disestablish = genppc_pci_msi_disestablish;
290 	pc->pc_msi_free = genppc_pci_msi_free;
291 	pc->pc_msi_release = genppc_pci_msi_release;
292 }
293 
294 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
295 #include <machine/isa_machdep.h>
296 #include "isa.h"
297 
298 void *genppc_pciide_machdep_compat_intr_establish(device_t,
299     struct pci_attach_args *, int, int (*)(void *), void *);
300 
301 void *
302 genppc_pciide_machdep_compat_intr_establish(device_t dev,
303     struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg)
304 {
305 #if NISA > 0
306 	int irq;
307 	void *cookie;
308 
309 	irq = PCIIDE_COMPAT_IRQ(chan);
310 	cookie = isa_intr_establish(NULL, irq, IST_LEVEL, IPL_BIO, func, arg);
311 	if (cookie == NULL)
312 		return (NULL);
313 	aprint_normal_dev(dev, "%s channel interrupting at irq %d\n",
314 	    PCIIDE_CHANNEL_NAME(chan), irq);
315 	return (cookie);
316 #else
317 	panic("pciide_machdep_compat_intr_establish() called");
318 #endif
319 }
320 #endif /* __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH */
321