Home | History | Annotate | Line # | Download | only in linux
linux_pci.c revision 1.24
      1  1.24       mrg /*	$NetBSD: linux_pci.c,v 1.24 2022/09/20 23:01:42 mrg Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad /*-
      4   1.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5   1.1  riastrad  * All rights reserved.
      6   1.1  riastrad  *
      7   1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  riastrad  * by Taylor R. Campbell.
      9   1.1  riastrad  *
     10   1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11   1.1  riastrad  * modification, are permitted provided that the following conditions
     12   1.1  riastrad  * are met:
     13   1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14   1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15   1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18   1.1  riastrad  *
     19   1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  riastrad  */
     31   1.1  riastrad 
     32   1.7  jmcneill #ifdef _KERNEL_OPT
     33  1.17  riastrad #include "acpica.h"
     34   1.7  jmcneill #include "opt_pci.h"
     35   1.7  jmcneill #endif
     36   1.7  jmcneill 
     37   1.1  riastrad #include <sys/cdefs.h>
     38  1.24       mrg __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.24 2022/09/20 23:01:42 mrg Exp $");
     39  1.16  riastrad 
     40  1.16  riastrad #if NACPICA > 0
     41  1.16  riastrad #include <dev/acpi/acpivar.h>
     42  1.16  riastrad #include <dev/acpi/acpi_pci.h>
     43  1.16  riastrad #endif
     44   1.1  riastrad 
     45   1.1  riastrad #include <linux/pci.h>
     46   1.1  riastrad 
     47   1.5  riastrad #include <drm/drm_agp_netbsd.h>
     48   1.5  riastrad 
     49   1.1  riastrad device_t
     50   1.1  riastrad pci_dev_dev(struct pci_dev *pdev)
     51   1.1  riastrad {
     52   1.1  riastrad 
     53   1.1  riastrad 	return pdev->pd_dev;
     54   1.1  riastrad }
     55   1.1  riastrad 
     56  1.12  riastrad void
     57  1.12  riastrad pci_set_drvdata(struct pci_dev *pdev, void *drvdata)
     58  1.12  riastrad {
     59  1.12  riastrad 	pdev->pd_drvdata = drvdata;
     60  1.12  riastrad }
     61  1.12  riastrad 
     62  1.12  riastrad void *
     63   1.1  riastrad pci_get_drvdata(struct pci_dev *pdev)
     64   1.1  riastrad {
     65  1.12  riastrad 	return pdev->pd_drvdata;
     66   1.1  riastrad }
     67   1.1  riastrad 
     68  1.20  riastrad const char *
     69  1.20  riastrad pci_name(struct pci_dev *pdev)
     70  1.20  riastrad {
     71  1.20  riastrad 
     72  1.20  riastrad 	/* XXX not sure this has the right format */
     73  1.20  riastrad 	return device_xname(pci_dev_dev(pdev));
     74  1.20  riastrad }
     75  1.20  riastrad 
     76   1.1  riastrad void
     77   1.1  riastrad linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent,
     78   1.1  riastrad     const struct pci_attach_args *pa, int kludges)
     79   1.1  riastrad {
     80   1.1  riastrad 	const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag,
     81   1.1  riastrad 	    PCI_SUBSYS_ID_REG);
     82   1.1  riastrad 	unsigned i;
     83   1.1  riastrad 
     84   1.3  riastrad 	memset(pdev, 0, sizeof(*pdev)); /* paranoia */
     85   1.3  riastrad 
     86   1.1  riastrad 	pdev->pd_pa = *pa;
     87   1.1  riastrad 	pdev->pd_kludges = kludges;
     88   1.1  riastrad 	pdev->pd_rom_vaddr = NULL;
     89   1.1  riastrad 	pdev->pd_dev = dev;
     90   1.1  riastrad #if (NACPICA > 0)
     91   1.7  jmcneill 	const int seg = pci_get_segment(pa->pa_pc);
     92   1.7  jmcneill 	pdev->pd_ad = acpi_pcidev_find(seg, pa->pa_bus,
     93   1.1  riastrad 	    pa->pa_device, pa->pa_function);
     94   1.1  riastrad #else
     95   1.1  riastrad 	pdev->pd_ad = NULL;
     96   1.1  riastrad #endif
     97   1.1  riastrad 	pdev->pd_saved_state = NULL;
     98   1.1  riastrad 	pdev->pd_intr_handles = NULL;
     99  1.12  riastrad 	pdev->pd_drvdata = NULL;
    100   1.1  riastrad 	pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP);
    101   1.1  riastrad 	pdev->bus->pb_pc = pa->pa_pc;
    102   1.1  riastrad 	pdev->bus->pb_dev = parent;
    103   1.1  riastrad 	pdev->bus->number = pa->pa_bus;
    104   1.1  riastrad 	pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
    105   1.1  riastrad 	pdev->vendor = PCI_VENDOR(pa->pa_id);
    106   1.1  riastrad 	pdev->device = PCI_PRODUCT(pa->pa_id);
    107   1.1  riastrad 	pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id);
    108   1.1  riastrad 	pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id);
    109   1.1  riastrad 	pdev->revision = PCI_REVISION(pa->pa_class);
    110   1.1  riastrad 	pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */
    111   1.1  riastrad 
    112   1.1  riastrad 	CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
    113   1.1  riastrad 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
    114   1.1  riastrad 		const int reg = PCI_BAR(i);
    115   1.1  riastrad 
    116   1.1  riastrad 		pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc,
    117   1.1  riastrad 		    pa->pa_tag, reg);
    118   1.1  riastrad 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
    119   1.1  riastrad 			pdev->pd_resources[i].type,
    120   1.1  riastrad 			&pdev->pd_resources[i].addr,
    121   1.1  riastrad 			&pdev->pd_resources[i].size,
    122   1.1  riastrad 			&pdev->pd_resources[i].flags)) {
    123   1.1  riastrad 			pdev->pd_resources[i].addr = 0;
    124   1.1  riastrad 			pdev->pd_resources[i].size = 0;
    125   1.1  riastrad 			pdev->pd_resources[i].flags = 0;
    126   1.1  riastrad 		}
    127   1.1  riastrad 		pdev->pd_resources[i].kva = NULL;
    128   1.2  riastrad 		pdev->pd_resources[i].mapped = false;
    129   1.1  riastrad 	}
    130   1.1  riastrad }
    131   1.1  riastrad 
    132   1.1  riastrad int
    133   1.1  riastrad pci_find_capability(struct pci_dev *pdev, int cap)
    134   1.1  riastrad {
    135   1.1  riastrad 
    136   1.1  riastrad 	return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
    137   1.1  riastrad 	    NULL, NULL);
    138   1.1  riastrad }
    139   1.1  riastrad 
    140   1.1  riastrad int
    141   1.1  riastrad pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
    142   1.1  riastrad {
    143   1.1  riastrad 
    144   1.1  riastrad 	KASSERT(!ISSET(reg, 3));
    145   1.1  riastrad 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
    146   1.1  riastrad 	return 0;
    147   1.1  riastrad }
    148   1.1  riastrad 
    149   1.1  riastrad int
    150   1.1  riastrad pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
    151   1.1  riastrad {
    152   1.1  riastrad 
    153   1.1  riastrad 	KASSERT(!ISSET(reg, 1));
    154   1.1  riastrad 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    155   1.1  riastrad 	    (reg &~ 2)) >> (8 * (reg & 2));
    156   1.1  riastrad 	return 0;
    157   1.1  riastrad }
    158   1.1  riastrad 
    159   1.1  riastrad int
    160   1.1  riastrad pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep)
    161   1.1  riastrad {
    162   1.1  riastrad 
    163   1.1  riastrad 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    164   1.1  riastrad 	    (reg &~ 3)) >> (8 * (reg & 3));
    165   1.1  riastrad 	return 0;
    166   1.1  riastrad }
    167   1.1  riastrad 
    168   1.1  riastrad int
    169   1.1  riastrad pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
    170   1.1  riastrad {
    171   1.1  riastrad 
    172   1.1  riastrad 	KASSERT(!ISSET(reg, 3));
    173   1.1  riastrad 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
    174   1.1  riastrad 	return 0;
    175   1.1  riastrad }
    176   1.1  riastrad 
    177   1.1  riastrad int
    178   1.1  riastrad pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
    179   1.1  riastrad     uint32_t *valuep)
    180   1.1  riastrad {
    181   1.1  riastrad 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    182   1.1  riastrad 	    PCI_FUNC(devfn));
    183   1.1  riastrad 
    184   1.1  riastrad 	KASSERT(!ISSET(reg, 1));
    185   1.1  riastrad 	*valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3));
    186   1.1  riastrad 	return 0;
    187   1.1  riastrad }
    188   1.1  riastrad 
    189   1.1  riastrad int
    190   1.1  riastrad pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg,
    191   1.1  riastrad     uint16_t *valuep)
    192   1.1  riastrad {
    193   1.1  riastrad 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    194   1.1  riastrad 	    PCI_FUNC(devfn));
    195   1.1  riastrad 
    196   1.1  riastrad 	KASSERT(!ISSET(reg, 1));
    197   1.1  riastrad 	*valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2));
    198   1.1  riastrad 	return 0;
    199   1.1  riastrad }
    200   1.1  riastrad 
    201   1.1  riastrad int
    202   1.1  riastrad pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
    203   1.1  riastrad     uint8_t *valuep)
    204   1.1  riastrad {
    205   1.1  riastrad 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    206   1.1  riastrad 	    PCI_FUNC(devfn));
    207   1.1  riastrad 
    208   1.1  riastrad 	*valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3));
    209   1.1  riastrad 	return 0;
    210   1.1  riastrad }
    211   1.1  riastrad 
    212   1.1  riastrad int
    213   1.1  riastrad pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
    214   1.1  riastrad     uint32_t value)
    215   1.1  riastrad {
    216   1.1  riastrad 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    217   1.1  riastrad 	    PCI_FUNC(devfn));
    218   1.1  riastrad 
    219   1.1  riastrad 	KASSERT(!ISSET(reg, 3));
    220   1.1  riastrad 	pci_conf_write(bus->pb_pc, tag, reg, value);
    221   1.1  riastrad 	return 0;
    222   1.1  riastrad }
    223   1.1  riastrad 
    224   1.1  riastrad static void
    225   1.1  riastrad pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes,
    226   1.1  riastrad     uint32_t value)
    227   1.1  riastrad {
    228   1.1  riastrad 	const uint32_t mask = ~((~0UL) << (8 * bytes));
    229   1.1  riastrad 	const int reg32 = (reg &~ 3);
    230   1.1  riastrad 	const unsigned int shift = (8 * (reg & 3));
    231   1.1  riastrad 	uint32_t value32;
    232   1.1  riastrad 
    233   1.1  riastrad 	KASSERT(bytes <= 4);
    234   1.1  riastrad 	KASSERT(!ISSET(value, ~mask));
    235   1.1  riastrad 	value32 = pci_conf_read(pc, tag, reg32);
    236   1.1  riastrad 	value32 &=~ (mask << shift);
    237   1.1  riastrad 	value32 |= (value << shift);
    238   1.1  riastrad 	pci_conf_write(pc, tag, reg32, value32);
    239   1.1  riastrad }
    240   1.1  riastrad 
    241   1.1  riastrad int
    242   1.1  riastrad pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value)
    243   1.1  riastrad {
    244   1.1  riastrad 
    245   1.1  riastrad 	KASSERT(!ISSET(reg, 1));
    246   1.1  riastrad 	pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value);
    247   1.1  riastrad 	return 0;
    248   1.1  riastrad }
    249   1.1  riastrad 
    250   1.1  riastrad int
    251   1.1  riastrad pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value)
    252   1.1  riastrad {
    253   1.1  riastrad 
    254   1.1  riastrad 	pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value);
    255   1.1  riastrad 	return 0;
    256   1.1  riastrad }
    257   1.1  riastrad 
    258   1.1  riastrad int
    259   1.1  riastrad pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg,
    260   1.1  riastrad     uint16_t value)
    261   1.1  riastrad {
    262   1.1  riastrad 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    263   1.1  riastrad 	    PCI_FUNC(devfn));
    264   1.1  riastrad 
    265   1.1  riastrad 	KASSERT(!ISSET(reg, 1));
    266   1.1  riastrad 	pci_rmw_config(bus->pb_pc, tag, reg, 2, value);
    267   1.1  riastrad 	return 0;
    268   1.1  riastrad }
    269   1.1  riastrad 
    270   1.1  riastrad int
    271   1.1  riastrad pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
    272   1.1  riastrad     uint8_t value)
    273   1.1  riastrad {
    274   1.1  riastrad 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    275   1.1  riastrad 	    PCI_FUNC(devfn));
    276   1.1  riastrad 
    277   1.1  riastrad 	pci_rmw_config(bus->pb_pc, tag, reg, 1, value);
    278   1.1  riastrad 	return 0;
    279   1.1  riastrad }
    280   1.1  riastrad 
    281   1.1  riastrad int
    282   1.1  riastrad pci_enable_msi(struct pci_dev *pdev)
    283   1.1  riastrad {
    284   1.1  riastrad 	const struct pci_attach_args *const pa = &pdev->pd_pa;
    285   1.1  riastrad 
    286   1.1  riastrad 	if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1))
    287   1.1  riastrad 		return -EINVAL;
    288   1.1  riastrad 
    289   1.1  riastrad 	pdev->msi_enabled = 1;
    290   1.1  riastrad 	return 0;
    291   1.1  riastrad }
    292   1.1  riastrad 
    293   1.1  riastrad void
    294   1.1  riastrad pci_disable_msi(struct pci_dev *pdev __unused)
    295   1.1  riastrad {
    296   1.1  riastrad 	const struct pci_attach_args *const pa = &pdev->pd_pa;
    297   1.1  riastrad 
    298   1.1  riastrad 	if (pdev->pd_intr_handles != NULL) {
    299   1.1  riastrad 		pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1);
    300   1.1  riastrad 		pdev->pd_intr_handles = NULL;
    301   1.1  riastrad 	}
    302   1.1  riastrad 	pdev->msi_enabled = 0;
    303   1.1  riastrad }
    304   1.1  riastrad 
    305   1.1  riastrad void
    306   1.1  riastrad pci_set_master(struct pci_dev *pdev)
    307   1.1  riastrad {
    308   1.1  riastrad 	pcireg_t csr;
    309   1.1  riastrad 
    310   1.1  riastrad 	csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    311   1.1  riastrad 	    PCI_COMMAND_STATUS_REG);
    312   1.1  riastrad 	csr |= PCI_COMMAND_MASTER_ENABLE;
    313   1.1  riastrad 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    314   1.1  riastrad 	    PCI_COMMAND_STATUS_REG, csr);
    315   1.1  riastrad }
    316   1.1  riastrad 
    317   1.1  riastrad void
    318   1.1  riastrad pci_clear_master(struct pci_dev *pdev)
    319   1.1  riastrad {
    320   1.1  riastrad 	pcireg_t csr;
    321   1.1  riastrad 
    322   1.1  riastrad 	csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    323   1.1  riastrad 	    PCI_COMMAND_STATUS_REG);
    324   1.1  riastrad 	csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE;
    325   1.1  riastrad 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    326   1.1  riastrad 	    PCI_COMMAND_STATUS_REG, csr);
    327   1.1  riastrad }
    328   1.1  riastrad 
    329   1.1  riastrad bus_addr_t
    330   1.1  riastrad pcibios_align_resource(void *p, const struct resource *resource,
    331   1.1  riastrad     bus_addr_t addr, bus_size_t size)
    332   1.1  riastrad {
    333   1.1  riastrad 	panic("pcibios_align_resource has accessed unaligned neurons!");
    334   1.1  riastrad }
    335   1.1  riastrad 
    336   1.1  riastrad int
    337   1.1  riastrad pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
    338   1.1  riastrad     bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
    339   1.1  riastrad     bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
    340   1.1  riastrad 	bus_size_t) __unused,
    341   1.1  riastrad     struct pci_dev *pdev)
    342   1.1  riastrad {
    343   1.1  riastrad 	const struct pci_attach_args *const pa = &pdev->pd_pa;
    344   1.1  riastrad 	bus_space_tag_t bst;
    345   1.1  riastrad 	int error;
    346   1.1  riastrad 
    347   1.1  riastrad 	switch (resource->flags) {
    348   1.1  riastrad 	case IORESOURCE_MEM:
    349   1.1  riastrad 		bst = pa->pa_memt;
    350   1.1  riastrad 		break;
    351   1.1  riastrad 
    352   1.1  riastrad 	case IORESOURCE_IO:
    353   1.1  riastrad 		bst = pa->pa_iot;
    354   1.1  riastrad 		break;
    355   1.1  riastrad 
    356   1.1  riastrad 	default:
    357   1.1  riastrad 		panic("I don't know what kind of resource you want!");
    358   1.1  riastrad 	}
    359   1.1  riastrad 
    360   1.1  riastrad 	resource->r_bst = bst;
    361   1.1  riastrad 	error = bus_space_alloc(bst, start, __type_max(bus_addr_t),
    362   1.1  riastrad 	    size, align, 0, 0, &resource->start, &resource->r_bsh);
    363   1.1  riastrad 	if (error)
    364   1.1  riastrad 		return error;
    365   1.1  riastrad 
    366  1.13  riastrad 	resource->end = start + (size - 1);
    367   1.1  riastrad 	return 0;
    368   1.1  riastrad }
    369   1.1  riastrad 
    370   1.1  riastrad /*
    371   1.1  riastrad  * XXX Mega-kludgerific!  pci_get_bus_and_slot and pci_get_class are
    372   1.1  riastrad  * defined only for their single purposes in i915drm, in
    373   1.1  riastrad  * i915_get_bridge_dev and intel_detect_pch.  We can't define them more
    374   1.1  riastrad  * generally without adapting pci_find_device (and pci_enumerate_bus
    375   1.1  riastrad  * internally) to pass a cookie through.
    376   1.1  riastrad  */
    377   1.1  riastrad 
    378   1.1  riastrad static int
    379   1.1  riastrad pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa)
    380   1.1  riastrad {
    381   1.1  riastrad 
    382  1.11  riastrad 	/* XXX domain */
    383   1.1  riastrad 	if (pa->pa_bus != 0)
    384   1.1  riastrad 		return 0;
    385   1.1  riastrad 	if (pa->pa_device != 0)
    386   1.1  riastrad 		return 0;
    387   1.1  riastrad 	if (pa->pa_function != 0)
    388   1.1  riastrad 		return 0;
    389   1.1  riastrad 
    390   1.1  riastrad 	return 1;
    391   1.1  riastrad }
    392   1.1  riastrad 
    393   1.1  riastrad struct pci_dev *
    394  1.11  riastrad pci_get_domain_bus_and_slot(int domain, int bus, int slot)
    395   1.1  riastrad {
    396   1.1  riastrad 	struct pci_attach_args pa;
    397   1.1  riastrad 
    398  1.11  riastrad 	KASSERT(domain == 0);
    399   1.1  riastrad 	KASSERT(bus == 0);
    400   1.1  riastrad 	KASSERT(slot == PCI_DEVFN(0, 0));
    401   1.1  riastrad 
    402   1.1  riastrad 	if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0))
    403   1.1  riastrad 		return NULL;
    404   1.1  riastrad 
    405   1.1  riastrad 	struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
    406   1.1  riastrad 	linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
    407   1.1  riastrad 
    408   1.1  riastrad 	return pdev;
    409   1.1  riastrad }
    410   1.1  riastrad 
    411   1.1  riastrad static int
    412   1.1  riastrad pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa)
    413   1.1  riastrad {
    414   1.1  riastrad 
    415   1.1  riastrad 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
    416   1.1  riastrad 		return 0;
    417   1.1  riastrad 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
    418   1.1  riastrad 		return 0;
    419   1.1  riastrad 
    420   1.1  riastrad 	return 1;
    421   1.1  riastrad }
    422   1.1  riastrad 
    423   1.1  riastrad void
    424   1.1  riastrad pci_dev_put(struct pci_dev *pdev)
    425   1.1  riastrad {
    426   1.1  riastrad 
    427   1.1  riastrad 	if (pdev == NULL)
    428   1.1  riastrad 		return;
    429   1.1  riastrad 
    430   1.1  riastrad 	KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE));
    431   1.1  riastrad 	kmem_free(pdev->bus, sizeof(*pdev->bus));
    432   1.1  riastrad 	kmem_free(pdev, sizeof(*pdev));
    433   1.1  riastrad }
    434   1.1  riastrad 
    435   1.1  riastrad struct pci_dev *		/* XXX i915 kludge */
    436   1.1  riastrad pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from)
    437   1.1  riastrad {
    438   1.1  riastrad 	struct pci_attach_args pa;
    439   1.1  riastrad 
    440   1.1  riastrad 	KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8));
    441   1.1  riastrad 
    442   1.1  riastrad 	if (from != NULL) {
    443   1.1  riastrad 		pci_dev_put(from);
    444   1.1  riastrad 		return NULL;
    445   1.1  riastrad 	}
    446   1.1  riastrad 
    447   1.1  riastrad 	if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge))
    448   1.1  riastrad 		return NULL;
    449   1.1  riastrad 
    450   1.1  riastrad 	struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
    451   1.1  riastrad 	linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
    452   1.1  riastrad 
    453   1.1  riastrad 	return pdev;
    454   1.1  riastrad }
    455   1.1  riastrad 
    456  1.19  riastrad int
    457  1.19  riastrad pci_dev_present(const struct pci_device_id *ids)
    458  1.19  riastrad {
    459  1.19  riastrad 
    460  1.19  riastrad 	/* XXX implement me -- pci_find_device doesn't pass a cookie */
    461  1.19  riastrad 	return 0;
    462  1.19  riastrad }
    463  1.19  riastrad 
    464   1.1  riastrad void
    465   1.1  riastrad pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused)
    466   1.1  riastrad {
    467   1.1  riastrad 
    468   1.1  riastrad 	/* XXX Disable the ROM address decoder.  */
    469   1.1  riastrad 	KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
    470   1.1  riastrad 	KASSERT(vaddr == pdev->pd_rom_vaddr);
    471   1.1  riastrad 	bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size);
    472   1.1  riastrad 	pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM;
    473   1.1  riastrad 	pdev->pd_rom_vaddr = NULL;
    474   1.1  riastrad }
    475   1.1  riastrad 
    476   1.1  riastrad /* XXX Whattakludge!  Should move this in sys/arch/.  */
    477   1.1  riastrad static int
    478   1.1  riastrad pci_map_rom_md(struct pci_dev *pdev)
    479   1.1  riastrad {
    480   1.1  riastrad #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
    481   1.1  riastrad 	const bus_addr_t rom_base = 0xc0000;
    482   1.1  riastrad 	const bus_size_t rom_size = 0x20000;
    483   1.1  riastrad 	bus_space_handle_t rom_bsh;
    484   1.1  riastrad 	int error;
    485   1.1  riastrad 
    486   1.1  riastrad 	if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY)
    487   1.1  riastrad 		return ENXIO;
    488   1.1  riastrad 	if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    489   1.1  riastrad 		return ENXIO;
    490   1.1  riastrad 	/* XXX Check whether this is the primary VGA card?  */
    491   1.1  riastrad 	error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size,
    492   1.1  riastrad 	    (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh);
    493   1.1  riastrad 	if (error)
    494   1.1  riastrad 		return ENXIO;
    495   1.1  riastrad 
    496   1.1  riastrad 	pdev->pd_rom_bst = pdev->pd_pa.pa_memt;
    497   1.1  riastrad 	pdev->pd_rom_bsh = rom_bsh;
    498   1.1  riastrad 	pdev->pd_rom_size = rom_size;
    499   1.1  riastrad 	pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
    500   1.1  riastrad 
    501   1.1  riastrad 	return 0;
    502   1.1  riastrad #else
    503   1.1  riastrad 	return ENXIO;
    504   1.1  riastrad #endif
    505   1.1  riastrad }
    506   1.1  riastrad 
    507   1.1  riastrad void __pci_rom_iomem *
    508   1.1  riastrad pci_map_rom(struct pci_dev *pdev, size_t *sizep)
    509   1.1  riastrad {
    510   1.1  riastrad 
    511   1.1  riastrad 	KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
    512   1.1  riastrad 
    513   1.1  riastrad 	if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
    514   1.1  riastrad 		(BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR),
    515   1.1  riastrad 		&pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size)
    516   1.1  riastrad 	    != 0)
    517   1.1  riastrad 		goto fail_mi;
    518   1.1  riastrad 	pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
    519   1.1  riastrad 
    520   1.1  riastrad 	/* XXX This type is obviously wrong in general...  */
    521   1.1  riastrad 	if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
    522   1.1  riastrad 		pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
    523   1.1  riastrad 		&pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
    524   1.1  riastrad 		pci_unmap_rom(pdev, NULL);
    525   1.1  riastrad 		goto fail_mi;
    526   1.1  riastrad 	}
    527   1.1  riastrad 	goto success;
    528   1.1  riastrad 
    529   1.1  riastrad fail_mi:
    530   1.1  riastrad 	if (pci_map_rom_md(pdev) != 0)
    531   1.1  riastrad 		goto fail_md;
    532   1.1  riastrad 
    533   1.1  riastrad 	/* XXX This type is obviously wrong in general...  */
    534   1.1  riastrad 	if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
    535   1.1  riastrad 		pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
    536   1.1  riastrad 		&pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
    537   1.1  riastrad 		pci_unmap_rom(pdev, NULL);
    538   1.1  riastrad 		goto fail_md;
    539   1.1  riastrad 	}
    540   1.1  riastrad 
    541   1.1  riastrad success:
    542   1.1  riastrad 	KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX);
    543   1.1  riastrad 	*sizep = pdev->pd_rom_found_size;
    544   1.1  riastrad 	pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst,
    545   1.1  riastrad 	    pdev->pd_rom_found_bsh);
    546   1.1  riastrad 	return pdev->pd_rom_vaddr;
    547   1.1  riastrad 
    548   1.1  riastrad fail_md:
    549   1.1  riastrad 	return NULL;
    550   1.1  riastrad }
    551   1.1  riastrad 
    552   1.1  riastrad void __pci_rom_iomem *
    553   1.1  riastrad pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep)
    554   1.1  riastrad {
    555   1.1  riastrad 
    556   1.1  riastrad 	*sizep = 0;
    557   1.1  riastrad 	return NULL;
    558   1.1  riastrad }
    559   1.1  riastrad 
    560   1.1  riastrad int
    561   1.1  riastrad pci_enable_rom(struct pci_dev *pdev)
    562   1.1  riastrad {
    563   1.1  riastrad 	const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    564   1.1  riastrad 	const pcitag_t tag = pdev->pd_pa.pa_tag;
    565   1.1  riastrad 	pcireg_t addr;
    566   1.1  riastrad 	int s;
    567   1.1  riastrad 
    568   1.1  riastrad 	/* XXX Don't do anything if the ROM isn't there.  */
    569   1.1  riastrad 
    570   1.1  riastrad 	s = splhigh();
    571   1.1  riastrad 	addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
    572   1.1  riastrad 	addr |= PCI_MAPREG_ROM_ENABLE;
    573   1.1  riastrad 	pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
    574   1.1  riastrad 	splx(s);
    575   1.1  riastrad 
    576   1.1  riastrad 	return 0;
    577   1.1  riastrad }
    578   1.1  riastrad 
    579   1.1  riastrad void
    580   1.1  riastrad pci_disable_rom(struct pci_dev *pdev)
    581   1.1  riastrad {
    582   1.1  riastrad 	const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    583   1.1  riastrad 	const pcitag_t tag = pdev->pd_pa.pa_tag;
    584   1.1  riastrad 	pcireg_t addr;
    585   1.1  riastrad 	int s;
    586   1.1  riastrad 
    587   1.1  riastrad 	s = splhigh();
    588   1.1  riastrad 	addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
    589   1.1  riastrad 	addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE;
    590   1.1  riastrad 	pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
    591   1.1  riastrad 	splx(s);
    592   1.1  riastrad }
    593   1.1  riastrad 
    594   1.1  riastrad bus_addr_t
    595   1.1  riastrad pci_resource_start(struct pci_dev *pdev, unsigned i)
    596   1.1  riastrad {
    597   1.1  riastrad 
    598   1.1  riastrad 	KASSERT(i < PCI_NUM_RESOURCES);
    599   1.1  riastrad 	return pdev->pd_resources[i].addr;
    600   1.1  riastrad }
    601   1.1  riastrad 
    602   1.1  riastrad bus_size_t
    603   1.1  riastrad pci_resource_len(struct pci_dev *pdev, unsigned i)
    604   1.1  riastrad {
    605   1.1  riastrad 
    606   1.1  riastrad 	KASSERT(i < PCI_NUM_RESOURCES);
    607   1.1  riastrad 	return pdev->pd_resources[i].size;
    608   1.1  riastrad }
    609   1.1  riastrad 
    610   1.1  riastrad bus_addr_t
    611   1.1  riastrad pci_resource_end(struct pci_dev *pdev, unsigned i)
    612   1.1  riastrad {
    613   1.1  riastrad 
    614   1.1  riastrad 	return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1);
    615   1.1  riastrad }
    616   1.1  riastrad 
    617   1.1  riastrad int
    618   1.1  riastrad pci_resource_flags(struct pci_dev *pdev, unsigned i)
    619   1.1  riastrad {
    620   1.1  riastrad 
    621   1.1  riastrad 	KASSERT(i < PCI_NUM_RESOURCES);
    622   1.1  riastrad 	return pdev->pd_resources[i].flags;
    623   1.1  riastrad }
    624   1.1  riastrad 
    625   1.1  riastrad void __pci_iomem *
    626   1.1  riastrad pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size)
    627   1.1  riastrad {
    628   1.1  riastrad 	int error;
    629   1.1  riastrad 
    630   1.1  riastrad 	KASSERT(i < PCI_NUM_RESOURCES);
    631   1.1  riastrad 	KASSERT(pdev->pd_resources[i].kva == NULL);
    632   1.1  riastrad 
    633   1.1  riastrad 	if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM)
    634   1.1  riastrad 		return NULL;
    635   1.1  riastrad 	if (pdev->pd_resources[i].size < size)
    636   1.1  riastrad 		return NULL;
    637   1.1  riastrad 	error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr,
    638   1.1  riastrad 	    size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags,
    639   1.1  riastrad 	    &pdev->pd_resources[i].bsh);
    640   1.6  riastrad 	if (error)
    641   1.5  riastrad 		return NULL;
    642   1.1  riastrad 	pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt;
    643   1.1  riastrad 	pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst,
    644   1.1  riastrad 	    pdev->pd_resources[i].bsh);
    645   1.1  riastrad 	pdev->pd_resources[i].mapped = true;
    646   1.1  riastrad 
    647   1.1  riastrad 	return pdev->pd_resources[i].kva;
    648   1.1  riastrad }
    649   1.1  riastrad 
    650   1.1  riastrad void
    651   1.1  riastrad pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva)
    652   1.1  riastrad {
    653   1.1  riastrad 	unsigned i;
    654   1.1  riastrad 
    655   1.1  riastrad 	CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
    656   1.1  riastrad 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
    657   1.1  riastrad 		if (pdev->pd_resources[i].kva == kva)
    658   1.1  riastrad 			break;
    659   1.1  riastrad 	}
    660   1.1  riastrad 	KASSERT(i < PCI_NUM_RESOURCES);
    661   1.1  riastrad 
    662   1.1  riastrad 	pdev->pd_resources[i].kva = NULL;
    663   1.1  riastrad 	bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh,
    664   1.1  riastrad 	    pdev->pd_resources[i].size);
    665   1.1  riastrad }
    666   1.1  riastrad 
    667   1.1  riastrad void
    668   1.1  riastrad pci_save_state(struct pci_dev *pdev)
    669   1.1  riastrad {
    670   1.1  riastrad 
    671   1.1  riastrad 	KASSERT(pdev->pd_saved_state == NULL);
    672   1.1  riastrad 	pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state),
    673   1.1  riastrad 	    KM_SLEEP);
    674   1.1  riastrad 	pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    675   1.1  riastrad 	    pdev->pd_saved_state);
    676   1.1  riastrad }
    677   1.1  riastrad 
    678   1.1  riastrad void
    679   1.1  riastrad pci_restore_state(struct pci_dev *pdev)
    680   1.1  riastrad {
    681   1.1  riastrad 
    682   1.1  riastrad 	KASSERT(pdev->pd_saved_state != NULL);
    683   1.1  riastrad 	pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    684   1.1  riastrad 	    pdev->pd_saved_state);
    685   1.1  riastrad 	kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state));
    686   1.1  riastrad 	pdev->pd_saved_state = NULL;
    687   1.1  riastrad }
    688   1.1  riastrad 
    689   1.1  riastrad bool
    690   1.1  riastrad pci_is_pcie(struct pci_dev *pdev)
    691   1.1  riastrad {
    692   1.1  riastrad 
    693   1.1  riastrad 	return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0);
    694   1.1  riastrad }
    695   1.1  riastrad 
    696   1.1  riastrad bool
    697   1.1  riastrad pci_dma_supported(struct pci_dev *pdev, uintmax_t mask)
    698   1.1  riastrad {
    699   1.1  riastrad 
    700   1.1  riastrad 	/* XXX Cop-out.  */
    701   1.1  riastrad 	if (mask > DMA_BIT_MASK(32))
    702   1.1  riastrad 		return pci_dma64_available(&pdev->pd_pa);
    703   1.1  riastrad 	else
    704   1.1  riastrad 		return true;
    705   1.1  riastrad }
    706   1.1  riastrad 
    707   1.1  riastrad bool
    708  1.14  riastrad pci_is_thunderbolt_attached(struct pci_dev *pdev)
    709  1.14  riastrad {
    710  1.14  riastrad 
    711  1.14  riastrad 	/* XXX Cop-out.  */
    712  1.14  riastrad 	return false;
    713  1.14  riastrad }
    714  1.14  riastrad 
    715  1.14  riastrad bool
    716   1.1  riastrad pci_is_root_bus(struct pci_bus *bus)
    717   1.1  riastrad {
    718   1.1  riastrad 
    719  1.24       mrg 	return bus->number == 0;
    720   1.1  riastrad }
    721   1.1  riastrad 
    722   1.1  riastrad int
    723   1.1  riastrad pci_domain_nr(struct pci_bus *bus)
    724   1.1  riastrad {
    725   1.1  riastrad 
    726  1.23  riastrad 	return pci_get_segment(bus->pb_pc);
    727   1.1  riastrad }
    728   1.1  riastrad 
    729   1.1  riastrad /*
    730   1.1  riastrad  * We explicitly rename pci_enable/disable_device so that you have to
    731   1.1  riastrad  * review each use of them, since NetBSD's PCI API does _not_ respect
    732   1.1  riastrad  * our local enablecnt here, but there are different parts of NetBSD
    733   1.1  riastrad  * that automatically enable/disable like PMF, so you have to decide
    734   1.1  riastrad  * for each one whether to call it or not.
    735   1.1  riastrad  */
    736   1.1  riastrad 
    737   1.1  riastrad int
    738   1.1  riastrad linux_pci_enable_device(struct pci_dev *pdev)
    739   1.1  riastrad {
    740   1.1  riastrad 	const struct pci_attach_args *pa = &pdev->pd_pa;
    741   1.1  riastrad 	pcireg_t csr;
    742   1.1  riastrad 	int s;
    743   1.1  riastrad 
    744   1.1  riastrad 	if (pdev->pd_enablecnt++)
    745   1.1  riastrad 		return 0;
    746   1.1  riastrad 
    747   1.1  riastrad 	s = splhigh();
    748   1.1  riastrad 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    749   1.4  riastrad 	/* If someone else (firmware) already enabled it, credit them.  */
    750   1.4  riastrad 	if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE))
    751   1.4  riastrad 		pdev->pd_enablecnt++;
    752   1.1  riastrad 	csr |= PCI_COMMAND_IO_ENABLE;
    753   1.1  riastrad 	csr |= PCI_COMMAND_MEM_ENABLE;
    754   1.1  riastrad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    755   1.1  riastrad 	splx(s);
    756   1.1  riastrad 
    757   1.1  riastrad 	return 0;
    758   1.1  riastrad }
    759   1.1  riastrad 
    760   1.1  riastrad void
    761   1.1  riastrad linux_pci_disable_device(struct pci_dev *pdev)
    762   1.1  riastrad {
    763   1.1  riastrad 	const struct pci_attach_args *pa = &pdev->pd_pa;
    764   1.1  riastrad 	pcireg_t csr;
    765   1.1  riastrad 	int s;
    766   1.1  riastrad 
    767   1.1  riastrad 	if (--pdev->pd_enablecnt)
    768   1.1  riastrad 		return;
    769   1.1  riastrad 
    770   1.1  riastrad 	s = splhigh();
    771   1.1  riastrad 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    772   1.1  riastrad 	csr &= ~PCI_COMMAND_IO_ENABLE;
    773   1.1  riastrad 	csr &= ~PCI_COMMAND_MEM_ENABLE;
    774   1.1  riastrad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    775   1.1  riastrad 	splx(s);
    776   1.1  riastrad }
    777   1.1  riastrad 
    778   1.1  riastrad void
    779   1.1  riastrad linux_pci_dev_destroy(struct pci_dev *pdev)
    780   1.1  riastrad {
    781   1.1  riastrad 	unsigned i;
    782   1.1  riastrad 
    783   1.1  riastrad 	if (pdev->bus != NULL) {
    784   1.1  riastrad 		kmem_free(pdev->bus, sizeof(*pdev->bus));
    785   1.1  riastrad 		pdev->bus = NULL;
    786   1.1  riastrad 	}
    787   1.1  riastrad 	if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) {
    788   1.1  riastrad 		pci_unmap_rom(pdev, pdev->pd_rom_vaddr);
    789   1.1  riastrad 		pdev->pd_rom_vaddr = 0;
    790   1.1  riastrad 	}
    791   1.1  riastrad 	for (i = 0; i < __arraycount(pdev->pd_resources); i++) {
    792   1.1  riastrad 		if (!pdev->pd_resources[i].mapped)
    793   1.1  riastrad 			continue;
    794   1.1  riastrad 		bus_space_unmap(pdev->pd_resources[i].bst,
    795   1.1  riastrad 		    pdev->pd_resources[i].bsh, pdev->pd_resources[i].size);
    796   1.1  riastrad 	}
    797   1.1  riastrad 
    798   1.1  riastrad 	/* There is no way these should be still in use.  */
    799   1.1  riastrad 	KASSERT(pdev->pd_saved_state == NULL);
    800   1.1  riastrad 	KASSERT(pdev->pd_intr_handles == NULL);
    801   1.1  riastrad }
    802  1.24       mrg 
    803  1.24       mrg enum pci_bus_speed
    804  1.24       mrg pcie_get_speed_cap(struct pci_dev *dev)
    805  1.24       mrg {
    806  1.24       mrg 	pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
    807  1.24       mrg 	pcitag_t tag = dev->pd_pa.pa_tag;
    808  1.24       mrg 	pcireg_t lcap, lcap2, xcap;
    809  1.24       mrg 	int off;
    810  1.24       mrg 
    811  1.24       mrg 	/* Must have capabilities. */
    812  1.24       mrg 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    813  1.24       mrg 		return PCI_SPEED_UNKNOWN;
    814  1.24       mrg 
    815  1.24       mrg 	/* Only PCIe 3.x has LCAP2. */
    816  1.24       mrg 	xcap = pci_conf_read(pc, tag, off + PCIE_XCAP);
    817  1.24       mrg 	if (__SHIFTOUT(xcap, PCIE_XCAP_VER_MASK) >= 2) {
    818  1.24       mrg 		lcap2 = pci_conf_read(pc, tag, off + PCIE_LCAP2);
    819  1.24       mrg 		if (lcap2) {
    820  1.24       mrg 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS64) != 0) {
    821  1.24       mrg 				return PCIE_SPEED_64_0GT;
    822  1.24       mrg 			}
    823  1.24       mrg 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS32) != 0) {
    824  1.24       mrg 				return PCIE_SPEED_32_0GT;
    825  1.24       mrg 			}
    826  1.24       mrg 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS16) != 0) {
    827  1.24       mrg 				return PCIE_SPEED_16_0GT;
    828  1.24       mrg 			}
    829  1.24       mrg 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS8) != 0) {
    830  1.24       mrg 				return PCIE_SPEED_8_0GT;
    831  1.24       mrg 			}
    832  1.24       mrg 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS5) != 0) {
    833  1.24       mrg 				return PCIE_SPEED_5_0GT;
    834  1.24       mrg 			}
    835  1.24       mrg 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS2) != 0) {
    836  1.24       mrg 				return PCIE_SPEED_2_5GT;
    837  1.24       mrg 			}
    838  1.24       mrg 		}
    839  1.24       mrg 	}
    840  1.24       mrg 
    841  1.24       mrg 	lcap = pci_conf_read(pc, tag, off + PCIE_LCAP);
    842  1.24       mrg 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_64) {
    843  1.24       mrg 		return PCIE_SPEED_64_0GT;
    844  1.24       mrg 	}
    845  1.24       mrg 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_32) {
    846  1.24       mrg 		return PCIE_SPEED_32_0GT;
    847  1.24       mrg 	}
    848  1.24       mrg 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_16) {
    849  1.24       mrg 		return PCIE_SPEED_16_0GT;
    850  1.24       mrg 	}
    851  1.24       mrg 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_8) {
    852  1.24       mrg 		return PCIE_SPEED_8_0GT;
    853  1.24       mrg 	}
    854  1.24       mrg 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_5) {
    855  1.24       mrg 		return PCIE_SPEED_5_0GT;
    856  1.24       mrg 	}
    857  1.24       mrg 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_2) {
    858  1.24       mrg 		return PCIE_SPEED_2_5GT;
    859  1.24       mrg 	}
    860  1.24       mrg 
    861  1.24       mrg 	return PCI_SPEED_UNKNOWN;
    862  1.24       mrg }
    863  1.24       mrg 
    864  1.24       mrg /*
    865  1.24       mrg  * This should walk the tree, it only checks this device currently.
    866  1.24       mrg  * It also does not write to limiting_dev (the only caller in drm2
    867  1.24       mrg  * currently does not use it.)
    868  1.24       mrg  */
    869  1.24       mrg unsigned
    870  1.24       mrg pcie_bandwidth_available(struct pci_dev *dev,
    871  1.24       mrg     struct pci_dev **limiting_dev,
    872  1.24       mrg     enum pci_bus_speed *speed,
    873  1.24       mrg     enum pcie_link_width *width)
    874  1.24       mrg {
    875  1.24       mrg 	pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
    876  1.24       mrg 	pcitag_t tag = dev->pd_pa.pa_tag;
    877  1.24       mrg 	pcireg_t lcsr;
    878  1.24       mrg 	unsigned per_line_speed, num_lanes;
    879  1.24       mrg 	int off;
    880  1.24       mrg 
    881  1.24       mrg 	/* Must have capabilities. */
    882  1.24       mrg 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    883  1.24       mrg 		return 0;
    884  1.24       mrg 
    885  1.24       mrg 	if (speed)
    886  1.24       mrg 		*speed = PCI_SPEED_UNKNOWN;
    887  1.24       mrg 	if (width)
    888  1.24       mrg 		*width = 0;
    889  1.24       mrg 
    890  1.24       mrg 	lcsr = pci_conf_read(pc, tag, off + PCIE_LCSR);
    891  1.24       mrg 
    892  1.24       mrg 	switch (lcsr & PCIE_LCSR_NLW) {
    893  1.24       mrg 	case PCIE_LCSR_NLW_X1:
    894  1.24       mrg 	case PCIE_LCSR_NLW_X2:
    895  1.24       mrg 	case PCIE_LCSR_NLW_X4:
    896  1.24       mrg 	case PCIE_LCSR_NLW_X8:
    897  1.24       mrg 	case PCIE_LCSR_NLW_X12:
    898  1.24       mrg 	case PCIE_LCSR_NLW_X16:
    899  1.24       mrg 	case PCIE_LCSR_NLW_X32:
    900  1.24       mrg 		num_lanes = __SHIFTOUT(lcsr, PCIE_LCSR_NLW);
    901  1.24       mrg 		if (width)
    902  1.24       mrg 			*width = num_lanes;
    903  1.24       mrg 		break;
    904  1.24       mrg 	default:
    905  1.24       mrg 		num_lanes = 0;
    906  1.24       mrg 		break;
    907  1.24       mrg 	}
    908  1.24       mrg 
    909  1.24       mrg 	switch (__SHIFTOUT(lcsr, PCIE_LCSR_LINKSPEED)) {
    910  1.24       mrg 	case PCIE_LCSR_LINKSPEED_2:
    911  1.24       mrg 		*speed = PCIE_SPEED_2_5GT;
    912  1.24       mrg 		per_line_speed = 2500 * 8 / 10;
    913  1.24       mrg 		break;
    914  1.24       mrg 	case PCIE_LCSR_LINKSPEED_5:
    915  1.24       mrg 		*speed = PCIE_SPEED_5_0GT;
    916  1.24       mrg 		per_line_speed = 5000 * 8 / 10;
    917  1.24       mrg 		break;
    918  1.24       mrg 	case PCIE_LCSR_LINKSPEED_8:
    919  1.24       mrg 		*speed = PCIE_SPEED_8_0GT;
    920  1.24       mrg 		per_line_speed = 8000 * 128 / 130;
    921  1.24       mrg 		break;
    922  1.24       mrg 	case PCIE_LCSR_LINKSPEED_16:
    923  1.24       mrg 		*speed = PCIE_SPEED_16_0GT;
    924  1.24       mrg 		per_line_speed = 16000 * 128 / 130;
    925  1.24       mrg 		break;
    926  1.24       mrg 	case PCIE_LCSR_LINKSPEED_32:
    927  1.24       mrg 		*speed = PCIE_SPEED_32_0GT;
    928  1.24       mrg 		per_line_speed = 32000 * 128 / 130;
    929  1.24       mrg 		break;
    930  1.24       mrg 	case PCIE_LCSR_LINKSPEED_64:
    931  1.24       mrg 		*speed = PCIE_SPEED_64_0GT;
    932  1.24       mrg 		per_line_speed = 64000 * 128 / 130;
    933  1.24       mrg 		break;
    934  1.24       mrg 	default:
    935  1.24       mrg 		per_line_speed = 0;
    936  1.24       mrg 	}
    937  1.24       mrg 
    938  1.24       mrg 	return num_lanes * per_line_speed;
    939  1.24       mrg }
    940