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