pci_machdep.h revision 1.13 1 /* $NetBSD: pci_machdep.h,v 1.13 2014/03/29 19:28:29 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2002,2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Klaus Klein and 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 #ifndef _PCI_MACHDEP_H_
33 #define _PCI_MACHDEP_H_
34
35 #include <prop/proplib.h>
36
37 /*
38 * Machine-specific definitions for PCI autoconfiguration.
39 */
40
41 #define __HAVE_PCI_CONF_HOOK
42
43 /*
44 * Types provided to machine-independent PCI code
45 */
46 typedef struct genppc_pci_chipset *pci_chipset_tag_t;
47 typedef void *pci_msi_handle_t;
48 typedef int pcitag_t;
49 typedef int pci_intr_handle_t;
50
51 /*
52 * Forward declarations.
53 */
54 struct pci_attach_args;
55 struct pcibus_attach_args;
56
57 static inline pci_chipset_tag_t pcibus_attach_args_pc(
58 struct pcibus_attach_args *);
59 static inline pci_chipset_tag_t pci_attach_args_pc(
60 const struct pci_attach_args *);
61
62 #include <dev/pci/pcireg.h>
63
64 #ifdef _KERNEL
65 extern struct powerpc_bus_dma_tag pci_bus_dma_tag;
66 #endif
67
68
69 #if defined(_KERNEL) && (defined(_MODULE) || defined(__PCI_NOINLINE))
70 void pci_attach_hook(device_t, device_t,
71 struct pcibus_attach_args *);
72
73 int pci_bus_maxdevs(pci_chipset_tag_t, int);
74 pcitag_t pci_make_tag(pci_chipset_tag_t, int, int, int);
75 pcireg_t pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
76 void pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
77 void pci_decompose_tag(pci_chipset_tag_t, pcitag_t,
78 int *, int *, int *);
79
80 const char * pci_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
81 char *, size_t);
82 const struct evcnt *
83 pci_intr_evcnt(pci_chipset_tag_t, pci_intr_handle_t);
84 void * pci_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
85 int, int (*)(void *), void *);
86 void pci_intr_disestablish(pci_chipset_tag_t, void *);
87 int pci_intr_map(const struct pci_attach_args *,
88 pci_intr_handle_t *ihp);
89 int pci_intr_setattr(pci_chipset_tag_t, pci_intr_handle_t *,
90 int, uint64_t);
91
92 int pci_msi_request(const struct pci_attach_args *,
93 pci_msi_handle_t *, size_t, int, int);
94 int pci_msi_type(pci_chipset_tag_t, pci_msi_handle_t);
95 size_t pci_msi_available(pci_chipset_tag_t, pci_msi_handle_t);
96 const char * pci_msi_string(pci_chipset_tag_t, pci_msi_handle_t, size_t);
97 const struct evcnt *
98 pci_msi_evcnt(pci_chipset_tag_t, pci_msi_handle_t, size_t);
99 void * pci_msi_establish(pci_chipset_tag_t, pci_msi_handle_t, size_t,
100 int, int (*)(void *), void *);
101 void * pci_msix_establish(pci_chipset_tag_t, pci_msi_handle_t, size_t,
102 size_t, int, int (*)(void *), void *);
103 void pci_msi_disestablish(pci_chipset_tag_t, void *);
104 void pci_msi_free(pci_chipset_tag_t, pci_msi_handle_t, size_t);
105 void pci_msi_release(pci_chipset_tag_t, pci_msi_handle_t);
106
107 void pci_conf_interrupt(pci_chipset_tag_t, int, int, int,
108 int, int *);
109 int pci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
110 #endif /* _KERNEL && (_MODULE || __PCI_NOINLINE) */
111
112 /* Per bus information structure */
113 struct genppc_pci_chipset_businfo {
114 SIMPLEQ_ENTRY(genppc_pci_chipset_businfo) next;
115 prop_dictionary_t pbi_properties; /* chipset properties */
116 };
117
118 #if !defined(_MODULE)
119 /*
120 * Generic PPC PCI structure and type definitions.
121 * NOT TO BE USED DIRECTLY BY MACHINE INDEPENDENT CODE.
122 */
123 struct genppc_pci_chipset {
124 void *pc_conf_v;
125 void (*pc_attach_hook)(device_t, device_t,
126 struct pcibus_attach_args *);
127 int (*pc_bus_maxdevs)(void *, int);
128 pcitag_t (*pc_make_tag)(void *, int, int, int);
129 pcireg_t (*pc_conf_read)(void *, pcitag_t, int);
130 void (*pc_conf_write)(void *, pcitag_t, int, pcireg_t);
131
132 void *pc_intr_v;
133 int (*pc_intr_map)(const struct pci_attach_args *,
134 pci_intr_handle_t *);
135 const char *(*pc_intr_string)(void *, pci_intr_handle_t, char *,
136 size_t);
137 const struct evcnt *(*pc_intr_evcnt)(void *, pci_intr_handle_t);
138 void *(*pc_intr_establish)(void *, pci_intr_handle_t,
139 int, int (*)(void *), void *);
140 void (*pc_intr_disestablish)(void *, void *);
141 int (*pc_intr_setattr)(void *, pci_intr_handle_t *,
142 int, uint64_t);
143
144 void *pc_msi_v;
145 int (*pc_msi_request)(const struct pci_attach_args *,
146 pci_msi_handle_t *, size_t, int, int);
147 int (*pc_msi_type)(void *, pci_msi_handle_t);
148 size_t (*pc_msi_available)(void *, pci_msi_handle_t);
149 const char * (*pc_msi_string)(void *, pci_msi_handle_t, size_t);
150 const struct evcnt *
151 (*pc_msi_evcnt)(void *, pci_msi_handle_t, size_t);
152 void * (*pc_msi_establish)(void *, pci_msi_handle_t, size_t,
153 int, int (*)(void *), void *);
154 void * (*pc_msix_establish)(void *, pci_msi_handle_t, size_t,
155 size_t, int, int (*)(void *), void *);
156 void (*pc_msi_disestablish)(void *, void *);
157 void (*pc_msi_free)(void *, pci_msi_handle_t, size_t);
158 void (*pc_msi_release)(void *, pci_msi_handle_t);
159
160 void (*pc_conf_interrupt)(void *, int, int, int, int, int *);
161 void (*pc_decompose_tag)(void *, pcitag_t, int *,
162 int *, int *);
163 int (*pc_conf_hook)(void *, int, int, int, pcireg_t);
164
165 uint32_t *pc_addr;
166 uint32_t *pc_data;
167 int pc_node;
168 int pc_ihandle;
169 int pc_bus;
170 bus_space_tag_t pc_memt;
171 bus_space_tag_t pc_iot;
172
173 SIMPLEQ_HEAD(, genppc_pci_chipset_businfo) pc_pbi;
174 };
175
176 #ifdef _KERNEL
177
178 #ifdef __PCI_NOINLINE
179 #define __pci_inline
180 #else
181 #define __pci_inline static inline
182 #endif
183
184 /*
185 * Functions provided to machine-independent PCI code.
186 */
187 __pci_inline void
188 pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
189 {
190 (*pcibus_attach_args_pc(pba)->pc_attach_hook)(parent, self, pba);
191 }
192
193 __pci_inline int
194 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
195 {
196 return (*pc->pc_bus_maxdevs)(pc->pc_conf_v, busno);
197 }
198
199 __pci_inline pcitag_t
200 pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
201 {
202 return (*pc->pc_make_tag)(pc->pc_conf_v, bus, device, function);
203 }
204
205 __pci_inline pcireg_t
206 pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
207 {
208 return (*pc->pc_conf_read)(pc->pc_conf_v, tag, reg);
209 }
210
211 __pci_inline void
212 pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
213 {
214 (*pc->pc_conf_write)(pc->pc_conf_v, tag, reg, val);
215 }
216
217 __pci_inline void
218 pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
219 {
220 (*pc->pc_decompose_tag)(pc->pc_conf_v, tag, bp, dp, fp);
221 }
222
223 __pci_inline int
224 pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
225 {
226 return (*pci_attach_args_pc(pa)->pc_intr_map)(pa, ihp);
227 }
228
229 __pci_inline const char *
230 pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char * buf,
231 size_t len)
232 {
233 return (*pc->pc_intr_string)(pc->pc_intr_v, ih, buf, len);
234 }
235
236 __pci_inline const struct evcnt *
237 pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
238 {
239 return (*pc->pc_intr_evcnt)(pc->pc_intr_v, ih);
240 }
241
242 __pci_inline void *
243 pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int ipl,
244 int (*handler)(void *), void *arg)
245 {
246 return (*pc->pc_intr_establish)(pc->pc_intr_v, ih, ipl, handler, arg);
247 }
248
249 __pci_inline void
250 pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
251 {
252 (*pc->pc_intr_disestablish)(pc->pc_intr_v, cookie);
253 }
254
255 __pci_inline int
256 pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ihp, int attr,
257 uint64_t data)
258 {
259 return (*pc->pc_intr_setattr)(pc->pc_intr_v, ihp, attr, data);
260 }
261
262
263 __pci_inline void
264 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int device, int pin,
265 int swiz, int *iline)
266 {
267 (*pc->pc_conf_interrupt)(pc->pc_conf_v, bus, device, pin, swiz, iline);
268 }
269
270 __pci_inline int
271 pci_conf_hook(pci_chipset_tag_t pc, int bus, int device, int function,
272 pcireg_t id)
273 {
274 return (*pc->pc_conf_hook)(pc->pc_conf_v, bus, device, function, id);
275 }
276
277 __pci_inline int
278 pci_msi_request(const struct pci_attach_args *pa, pci_msi_handle_t *msihp,
279 size_t nmsi, int ipl, int capid)
280 {
281 return (*pci_attach_args_pc(pa)->pc_msi_request)(pa, msihp, nmsi,
282 ipl, capid);
283 }
284
285 __pci_inline int
286 pci_msi_type(pci_chipset_tag_t pc, pci_msi_handle_t msih)
287 {
288 return (*pc->pc_msi_type)(pc->pc_msi_v, msih);
289 }
290
291 __pci_inline size_t
292 pci_msi_available(pci_chipset_tag_t pc, pci_msi_handle_t msih)
293 {
294 return (*pc->pc_msi_available)(pc->pc_msi_v, msih);
295 }
296
297 __pci_inline const char *
298 pci_msi_string(pci_chipset_tag_t pc, pci_msi_handle_t msih, size_t msirq)
299 {
300 return (*pc->pc_msi_string)(pc->pc_msi_v, msih, msirq);
301 }
302
303 __pci_inline const struct evcnt *
304 pci_msi_evcnt(pci_chipset_tag_t pc, pci_msi_handle_t msih, size_t msirq)
305 {
306 return (*pc->pc_msi_evcnt)(pc->pc_msi_v, msih, msirq);
307 }
308
309 __pci_inline void *
310 pci_msi_establish(pci_chipset_tag_t pc, pci_msi_handle_t msih, size_t msirq,
311 int ipl, int (*func)(void *), void *arg)
312 {
313 return (*pc->pc_msi_establish)(pc->pc_msi_v, msih, msirq, ipl,
314 func, arg);
315 }
316
317 __pci_inline void *
318 pci_msix_establish(pci_chipset_tag_t pc, pci_msi_handle_t msih, size_t vec,
319 size_t msirq, int ipl, int (*func)(void *), void *arg)
320 {
321 return (*pc->pc_msix_establish)(pc->pc_msi_v, msih, vec, msirq, ipl,
322 func, arg);
323 }
324
325 __pci_inline void
326 pci_msi_disestablish(pci_chipset_tag_t pc, void *ih)
327 {
328 (*pc->pc_msi_disestablish)(pc->pc_msi_v, ih);
329 }
330
331 __pci_inline void
332 pci_msi_free(pci_chipset_tag_t pc, pci_msi_handle_t msih, size_t msirq)
333 {
334 (*pc->pc_msi_free)(pc->pc_msi_v, msih, msirq);
335 }
336
337 __pci_inline void
338 pci_msi_release(pci_chipset_tag_t pc, pci_msi_handle_t msih)
339 {
340 (*pc->pc_msi_release)(pc->pc_msi_v, msih);
341 }
342
343 #undef __pci_inline
344
345 /*
346 * Generic PowerPC PCI functions. Override if necc.
347 */
348
349 int genppc_pci_bus_maxdevs(void *, int);
350
351 int genppc_pci_intr_map(const struct pci_attach_args *,
352 pci_intr_handle_t *);
353 const char *genppc_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
354 const struct evcnt *genppc_pci_intr_evcnt(void *, pci_intr_handle_t);
355 void *genppc_pci_intr_establish(void *, pci_intr_handle_t, int, int (*)(void *),
356 void *);
357 void genppc_pci_intr_disestablish(void *, void *);
358 int genppc_pci_intr_setattr(void *, pci_intr_handle_t *, int, uint64_t);
359
360 int genppc_pci_msi_request(const struct pci_attach_args *, pci_msi_handle_t *,
361 size_t, int, int);
362 int genppc_pci_msi_type(void *, pci_msi_handle_t);
363 size_t genppc_pci_msi_available(void *, pci_msi_handle_t);
364 const struct evcnt *genppc_pci_msi_evcnt(void *, pci_msi_handle_t, size_t);
365 const char *genppc_pci_msi_string(void *, pci_msi_handle_t, size_t);
366 void *genppc_pci_msi_establish(void *, pci_msi_handle_t, size_t,
367 int, int (*)(void *), void *);
368 void *genppc_pci_msix_establish(void *, pci_msi_handle_t, size_t,
369 size_t, int, int (*)(void *), void *);
370 void genppc_pci_msi_disestablish(void *, void *);
371 void genppc_pci_msi_free(void *, pci_msi_handle_t, size_t);
372 void genppc_pci_msi_release(void *, pci_msi_handle_t);
373 void genppc_pci_chipset_msi_init(pci_chipset_tag_t);
374
375 #define GENPPC_PCI_MSI_INITIALIZER \
376 .pc_msi_request = genppc_pci_msi_request, \
377 .pc_msi_type = genppc_pci_msi_type, \
378 .pc_msi_available = genppc_pci_msi_available, \
379 .pc_msi_evcnt = genppc_pci_msi_evcnt, \
380 .pc_msi_string = genppc_pci_msi_string, \
381 .pc_msi_establish = genppc_pci_msi_establish, \
382 .pc_msix_establish = genppc_pci_msix_establish, \
383 .pc_msi_disestablish = genppc_pci_msi_disestablish, \
384 .pc_msi_free = genppc_pci_msi_free, \
385 .pc_msi_release = genppc_pci_msi_release
386
387 void genppc_pci_conf_interrupt(void *, int, int, int, int, int *);
388 int genppc_pci_conf_hook(void *, int, int, int, pcireg_t);
389
390 /* generic indirect PCI functions */
391 void genppc_pci_indirect_attach_hook(device_t, device_t,
392 struct pcibus_attach_args *);
393 pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
394 pcireg_t genppc_pci_indirect_conf_read(void *, pcitag_t, int);
395 void genppc_pci_indirect_conf_write(void *, pcitag_t, int, pcireg_t);
396 void genppc_pci_indirect_decompose_tag(void *, pcitag_t, int *, int *, int *);
397
398 /* generic OFW method PCI functions */
399 void genppc_pci_ofmethod_attach_hook(device_t, device_t,
400 struct pcibus_attach_args *);
401 pcitag_t genppc_pci_ofmethod_make_tag(void *, int, int, int);
402 pcireg_t genppc_pci_ofmethod_conf_read(void *, pcitag_t, int);
403 void genppc_pci_ofmethod_conf_write(void *, pcitag_t, int, pcireg_t);
404 void genppc_pci_ofmethod_decompose_tag(void *, pcitag_t, int *, int *, int *);
405
406 /* Generic OFW PCI functions */
407
408 int genofw_find_picnode(int);
409 void genofw_find_ofpics(int);
410 void genofw_fixup_picnode_offsets(void);
411 void genofw_setup_pciintr_map(void *, struct genppc_pci_chipset_businfo *, int);
412 int genofw_find_node_by_devfunc(int, int, int, int);
413 int genofw_pci_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
414 int genofw_pci_conf_hook(void *, int, int, int, pcireg_t);
415
416 /* OFW PCI structures and defines */
417 #define PICNODE_TYPE_OPENPIC 1
418 #define PICNODE_TYPE_8259 2
419 #define PICNODE_TYPE_HEATHROW 3
420 #define PICNODE_TYPE_OHARE 4
421 #define PICNODE_TYPE_IVR 5
422
423 typedef struct _ofw_pic_node_t {
424 int node;
425 int parent;
426 int16_t cells;
427 int16_t intrs;
428 int16_t offset;
429 int16_t type;
430 } ofw_pic_node_t;
431
432 #endif /* _KERNEL */
433
434 #endif /* !_MODULE */
435
436 #endif /* _PCI_MACHDEP_H_ */
437