1*0a6a1f1dSLionel Sambuc /* $NetBSD: pci_machdep_common.h,v 1.21 2015/08/17 06:16:02 knakahara Exp $ */ 21cd76c75SBen Gras 31cd76c75SBen Gras /* 41cd76c75SBen Gras * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 51cd76c75SBen Gras * Copyright (c) 1994 Charles M. Hannum. All rights reserved. 61cd76c75SBen Gras * 71cd76c75SBen Gras * Redistribution and use in source and binary forms, with or without 81cd76c75SBen Gras * modification, are permitted provided that the following conditions 91cd76c75SBen Gras * are met: 101cd76c75SBen Gras * 1. Redistributions of source code must retain the above copyright 111cd76c75SBen Gras * notice, this list of conditions and the following disclaimer. 121cd76c75SBen Gras * 2. Redistributions in binary form must reproduce the above copyright 131cd76c75SBen Gras * notice, this list of conditions and the following disclaimer in the 141cd76c75SBen Gras * documentation and/or other materials provided with the distribution. 151cd76c75SBen Gras * 3. All advertising materials mentioning features or use of this software 161cd76c75SBen Gras * must display the following acknowledgement: 171cd76c75SBen Gras * This product includes software developed by Charles M. Hannum. 181cd76c75SBen Gras * 4. The name of the author may not be used to endorse or promote products 191cd76c75SBen Gras * derived from this software without specific prior written permission. 201cd76c75SBen Gras * 211cd76c75SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 221cd76c75SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 231cd76c75SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 241cd76c75SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 251cd76c75SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 261cd76c75SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 271cd76c75SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 281cd76c75SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 291cd76c75SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 301cd76c75SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 311cd76c75SBen Gras */ 321cd76c75SBen Gras 331cd76c75SBen Gras #ifndef _X86_PCI_MACHDEP_COMMON_H_ 341cd76c75SBen Gras #define _X86_PCI_MACHDEP_COMMON_H_ 351cd76c75SBen Gras 361cd76c75SBen Gras /* 371cd76c75SBen Gras * Machine-specific definitions for PCI autoconfiguration. 381cd76c75SBen Gras */ 391cd76c75SBen Gras #define __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH 401cd76c75SBen Gras #ifndef XEN 411cd76c75SBen Gras #define __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_DISESTABLISH 421cd76c75SBen Gras #endif 431cd76c75SBen Gras 44*0a6a1f1dSLionel Sambuc #include <sys/kcpuset.h> 45*0a6a1f1dSLionel Sambuc 461cd76c75SBen Gras /* 4784d9c625SLionel Sambuc * x86-specific PCI structure and type definitions. 481cd76c75SBen Gras * NOT TO BE USED DIRECTLY BY MACHINE INDEPENDENT CODE. 491cd76c75SBen Gras * 501cd76c75SBen Gras * Configuration tag; created from a {bus,device,function} triplet by 511cd76c75SBen Gras * pci_make_tag(), and passed to pci_conf_read() and pci_conf_write(). 521cd76c75SBen Gras * We could instead always pass the {bus,device,function} triplet to 531cd76c75SBen Gras * the read and write routines, but this would cause extra overhead. 541cd76c75SBen Gras * 551cd76c75SBen Gras * Mode 2 is historical and deprecated by the Revision 2.0 specification. 561cd76c75SBen Gras * 571cd76c75SBen Gras * 581cd76c75SBen Gras * Mode 1 tag: 591cd76c75SBen Gras * 31 24 16 15 11 10 8 601cd76c75SBen Gras * +---------------------------------------------------------------+ 611cd76c75SBen Gras * |1| 0 | BUS | DEV |FUNC | 0 | 621cd76c75SBen Gras * +---------------------------------------------------------------+ 631cd76c75SBen Gras */ 641cd76c75SBen Gras union x86_pci_tag_u { 651cd76c75SBen Gras uint32_t mode1; 661cd76c75SBen Gras struct { 671cd76c75SBen Gras uint16_t port; 681cd76c75SBen Gras uint8_t enable; 691cd76c75SBen Gras uint8_t forward; 701cd76c75SBen Gras } mode2; 711cd76c75SBen Gras }; 721cd76c75SBen Gras 731cd76c75SBen Gras extern struct x86_bus_dma_tag pci_bus_dma_tag; 741cd76c75SBen Gras #ifdef _LP64 751cd76c75SBen Gras extern struct x86_bus_dma_tag pci_bus_dma64_tag; 761cd76c75SBen Gras #endif 771cd76c75SBen Gras 781cd76c75SBen Gras struct pci_attach_args; 791cd76c75SBen Gras struct pci_chipset_tag; 801cd76c75SBen Gras 811cd76c75SBen Gras /* 821cd76c75SBen Gras * Types provided to machine-independent PCI code 831cd76c75SBen Gras */ 841cd76c75SBen Gras typedef struct pci_chipset_tag *pci_chipset_tag_t; 851cd76c75SBen Gras typedef union x86_pci_tag_u pcitag_t; 861cd76c75SBen Gras 871cd76c75SBen Gras struct pci_chipset_tag { 881cd76c75SBen Gras pci_chipset_tag_t pc_super; 891cd76c75SBen Gras uint64_t pc_present; 901cd76c75SBen Gras const struct pci_overrides *pc_ov; 911cd76c75SBen Gras void *pc_ctx; 921cd76c75SBen Gras }; 931cd76c75SBen Gras 941cd76c75SBen Gras /* 9584d9c625SLionel Sambuc * x86-specific PCI variables and functions. 961cd76c75SBen Gras * NOT TO BE USED DIRECTLY BY MACHINE INDEPENDENT CODE. 971cd76c75SBen Gras */ 981cd76c75SBen Gras int pci_mode_detect(void); 991cd76c75SBen Gras void pci_mode_set(int); 1001cd76c75SBen Gras 1011cd76c75SBen Gras /* 1021cd76c75SBen Gras * Functions provided to machine-independent PCI code. 1031cd76c75SBen Gras */ 1041cd76c75SBen Gras void pci_attach_hook(device_t, device_t, 1051cd76c75SBen Gras struct pcibus_attach_args *); 1061cd76c75SBen Gras int pci_bus_maxdevs(pci_chipset_tag_t, int); 1071cd76c75SBen Gras pcitag_t pci_make_tag(pci_chipset_tag_t, int, int, int); 1081cd76c75SBen Gras void pci_decompose_tag(pci_chipset_tag_t, pcitag_t, 1091cd76c75SBen Gras int *, int *, int *); 1101cd76c75SBen Gras pcireg_t pci_conf_read(pci_chipset_tag_t, pcitag_t, int); 1111cd76c75SBen Gras void pci_conf_write(pci_chipset_tag_t, pcitag_t, int, 1121cd76c75SBen Gras pcireg_t); 1131cd76c75SBen Gras int pci_intr_map(const struct pci_attach_args *, 1141cd76c75SBen Gras pci_intr_handle_t *); 115*0a6a1f1dSLionel Sambuc const char *pci_intr_string(pci_chipset_tag_t, pci_intr_handle_t, 116*0a6a1f1dSLionel Sambuc char *, size_t); 1171cd76c75SBen Gras const struct evcnt *pci_intr_evcnt(pci_chipset_tag_t, pci_intr_handle_t); 1181cd76c75SBen Gras void *pci_intr_establish(pci_chipset_tag_t, pci_intr_handle_t, 1191cd76c75SBen Gras int, int (*)(void *), void *); 1201cd76c75SBen Gras void pci_intr_disestablish(pci_chipset_tag_t, void *); 1211cd76c75SBen Gras 122*0a6a1f1dSLionel Sambuc typedef enum { 123*0a6a1f1dSLionel Sambuc PCI_INTR_TYPE_INTX = 0, 124*0a6a1f1dSLionel Sambuc PCI_INTR_TYPE_MSI, 125*0a6a1f1dSLionel Sambuc PCI_INTR_TYPE_MSIX, 126*0a6a1f1dSLionel Sambuc PCI_INTR_TYPE_SIZE, 127*0a6a1f1dSLionel Sambuc } pci_intr_type_t; 128*0a6a1f1dSLionel Sambuc 129*0a6a1f1dSLionel Sambuc pci_intr_type_t pci_intr_type(pci_intr_handle_t); 130*0a6a1f1dSLionel Sambuc 131*0a6a1f1dSLionel Sambuc /* 132*0a6a1f1dSLionel Sambuc * If device drivers use MSI/MSI-X, they should use these API for INTx 133*0a6a1f1dSLionel Sambuc * instead of pci_intr_map(), because of conforming the pci_intr_handle 134*0a6a1f1dSLionel Sambuc * ownership to MSI/MSI-X. 135*0a6a1f1dSLionel Sambuc */ 136*0a6a1f1dSLionel Sambuc int pci_intx_alloc(const struct pci_attach_args *, 137*0a6a1f1dSLionel Sambuc pci_intr_handle_t **); 138*0a6a1f1dSLionel Sambuc 139*0a6a1f1dSLionel Sambuc /* 140*0a6a1f1dSLionel Sambuc * Wrapper function for generally unitied allocation to fallback MSI-X/MSI/INTx 141*0a6a1f1dSLionel Sambuc * automatically. 142*0a6a1f1dSLionel Sambuc */ 143*0a6a1f1dSLionel Sambuc int pci_intr_alloc(const struct pci_attach_args *, 144*0a6a1f1dSLionel Sambuc pci_intr_handle_t **, int *, pci_intr_type_t); 145*0a6a1f1dSLionel Sambuc 1461cd76c75SBen Gras /* experimental MSI support */ 147*0a6a1f1dSLionel Sambuc int pci_msi_alloc(const struct pci_attach_args *, 148*0a6a1f1dSLionel Sambuc pci_intr_handle_t **, int *); 149*0a6a1f1dSLionel Sambuc int pci_msi_alloc_exact(const struct pci_attach_args *, 150*0a6a1f1dSLionel Sambuc pci_intr_handle_t **, int); 151*0a6a1f1dSLionel Sambuc 152*0a6a1f1dSLionel Sambuc /* experimental MSI-X support */ 153*0a6a1f1dSLionel Sambuc int pci_msix_alloc(const struct pci_attach_args *, 154*0a6a1f1dSLionel Sambuc pci_intr_handle_t **, int *); 155*0a6a1f1dSLionel Sambuc int pci_msix_alloc_exact(const struct pci_attach_args *, 156*0a6a1f1dSLionel Sambuc pci_intr_handle_t **, int); 157*0a6a1f1dSLionel Sambuc int pci_msix_alloc_map(const struct pci_attach_args *, 158*0a6a1f1dSLionel Sambuc pci_intr_handle_t **, u_int *, int); 159*0a6a1f1dSLionel Sambuc 160*0a6a1f1dSLionel Sambuc void pci_intr_release(pci_chipset_tag_t, pci_intr_handle_t *, 161*0a6a1f1dSLionel Sambuc int); 1621cd76c75SBen Gras 1631cd76c75SBen Gras /* 1641cd76c75SBen Gras * ALL OF THE FOLLOWING ARE MACHINE-DEPENDENT, AND SHOULD NOT BE USED 1651cd76c75SBen Gras * BY PORTABLE CODE. 1661cd76c75SBen Gras */ 1671cd76c75SBen Gras 1681cd76c75SBen Gras /* Extract Bus Number for a host bridge or -1 if unknown. */ 1691cd76c75SBen Gras int pchb_get_bus_number(pci_chipset_tag_t, pcitag_t); 1701cd76c75SBen Gras 1711cd76c75SBen Gras /* 1721cd76c75SBen Gras * Section 6.2.4, `Miscellaneous Functions' of the PCI Specification, 1731cd76c75SBen Gras * says that 255 means `unknown' or `no connection' to the interrupt 1741cd76c75SBen Gras * controller on a PC. 1751cd76c75SBen Gras */ 1761cd76c75SBen Gras #define X86_PCI_INTERRUPT_LINE_NO_CONNECTION 0xff 1771cd76c75SBen Gras 1781cd76c75SBen Gras void pci_device_foreach(pci_chipset_tag_t, int, 1791cd76c75SBen Gras void (*)(pci_chipset_tag_t, pcitag_t, void*), 1801cd76c75SBen Gras void *); 1811cd76c75SBen Gras 1821cd76c75SBen Gras void pci_device_foreach_min(pci_chipset_tag_t, int, int, 1831cd76c75SBen Gras void (*)(pci_chipset_tag_t, pcitag_t, void*), 1841cd76c75SBen Gras void *); 1851cd76c75SBen Gras 1861cd76c75SBen Gras void pci_bridge_foreach(pci_chipset_tag_t, int, int, 1871cd76c75SBen Gras void (*) (pci_chipset_tag_t, pcitag_t, void *), void *); 1881cd76c75SBen Gras 1891cd76c75SBen Gras void pci_ranges_infer(pci_chipset_tag_t, int, int, bus_addr_t *, 1901cd76c75SBen Gras bus_size_t *, bus_addr_t *, bus_size_t *); 1911cd76c75SBen Gras 1921cd76c75SBen Gras extern prop_dictionary_t pci_rsrc_dict; 1931cd76c75SBen Gras prop_dictionary_t pci_rsrc_filter(prop_dictionary_t, 1941cd76c75SBen Gras bool (*)(void *, prop_dictionary_t), void *arg); 1951cd76c75SBen Gras 1961cd76c75SBen Gras #endif /* _X86_PCI_MACHDEP_COMMON_H_ */ 197