Home | History | Annotate | Line # | Download | only in linux
pci.h revision 1.1.2.6
      1 /*	$NetBSD: pci.h,v 1.1.2.6 2013/07/24 03:01:54 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      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 _LINUX_PCI_H_
     33 #define _LINUX_PCI_H_
     34 
     35 #include <sys/types.h>
     36 #include <sys/bus.h>
     37 #include <sys/kmem.h>
     38 #include <sys/systm.h>
     39 
     40 #include <dev/pci/pcivar.h>
     41 
     42 #include <linux/ioport.h>
     43 
     44 struct pci_bus;
     45 struct pci_device_id;
     46 
     47 struct pci_dev {
     48 	struct pci_bus *bus;
     49 	unsigned int device;
     50 	struct pci_attach_args pd_pa;
     51 	bool pd_kludged;	/* XXX pci_kludgey_find_dev hack */
     52 };
     53 
     54 #define	PCI_CAP_ID_AGP	PCI_CAP_AGP
     55 
     56 static inline int
     57 pci_find_capability(struct pci_dev *pdev, int cap)
     58 {
     59 	return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
     60 	    NULL, NULL);
     61 }
     62 
     63 static inline void
     64 pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
     65 {
     66 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
     67 }
     68 
     69 static inline void
     70 pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
     71 {
     72 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
     73 }
     74 
     75 #define	PCIBIOS_MIN_MEM	0	/* XXX bogus x86 kludge bollocks */
     76 
     77 static inline bus_addr_t
     78 pcibios_align_resource(void *p, const struct resource *resource,
     79     bus_addr_t addr, bus_size_t size)
     80 {
     81 	panic("pcibios_align_resource has accessed unaligned neurons!");
     82 }
     83 
     84 static inline int
     85 pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
     86     bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
     87     bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
     88 	bus_size_t) __unused,
     89     struct pci_dev *pdev)
     90 {
     91 	const struct pci_attach_args *const pa = &pdev->pd_pa;
     92 	bus_space_tag_t bst;
     93 	int error;
     94 
     95 	switch (resource->flags) {
     96 	case IORESOURCE_MEM:
     97 		bst = pa->pa_memt;
     98 		break;
     99 
    100 	case IORESOURCE_IO:
    101 		bst = pa->pa_iot;
    102 		break;
    103 
    104 	default:
    105 		panic("I don't know what kind of resource you want!");
    106 	}
    107 
    108 	resource->r_bst = bst;
    109 	error = bus_space_alloc(bst, start, 0xffffffffffffffffULL /* XXX */,
    110 	    size, align, 0, 0, &resource->start, &resource->r_bsh);
    111 	if (error)
    112 		return error;
    113 
    114 	resource->size = size;
    115 	return 0;
    116 }
    117 
    118 /*
    119  * XXX Mega-kludgerific!
    120  *
    121  * XXX Doesn't check whether any such device actually exists.
    122  */
    123 
    124 static inline struct pci_dev *
    125 pci_kludgey_find_dev(struct pci_dev *pdev, int bus, int dev, int func)
    126 {
    127 	struct pci_dev *const otherdev = kmem_zalloc(sizeof(*otherdev),
    128 	    KM_SLEEP);
    129 
    130 #ifdef DIAGNOSTIC
    131 	{
    132 		int obus, odev, ofunc;
    133 
    134 		pci_decompose_tag(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, &obus,
    135 		    &odev, &ofunc);
    136 		KASSERT(obus == bus);
    137 	}
    138 #endif
    139 
    140 	otherdev->bus = NULL;	/* XXX struct pci_dev::bus */
    141 	otherdev->device = dev;
    142 	otherdev->pd_pa = pdev->pd_pa;
    143 	otherdev->pd_pa.pa_tag = pci_make_tag(otherdev->pd_pa.pa_pc,
    144 	    bus, dev, func);
    145 	otherdev->pd_kludged = true;
    146 
    147 	return otherdev;
    148 }
    149 
    150 static inline void
    151 pci_dev_put(struct pci_dev *pdev)
    152 {
    153 
    154 	KASSERT(pdev->pd_kludged);
    155 	kmem_free(pdev, sizeof(*pdev));
    156 }
    157 
    158 #endif  /* _LINUX_PCI_H_ */
    159