Home | History | Annotate | Line # | Download | only in linux
linux_pci.c revision 1.27
      1 /*	$NetBSD: linux_pci.c,v 1.27 2023/09/30 10:46:46 mrg 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 #ifdef _KERNEL_OPT
     33 #include "acpica.h"
     34 #include "opt_pci.h"
     35 #endif
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.27 2023/09/30 10:46:46 mrg Exp $");
     39 
     40 #if NACPICA > 0
     41 #include <dev/acpi/acpivar.h>
     42 #include <dev/acpi/acpi_pci.h>
     43 #endif
     44 
     45 #include <linux/pci.h>
     46 
     47 #include <drm/drm_agp_netbsd.h>
     48 
     49 device_t
     50 pci_dev_dev(struct pci_dev *pdev)
     51 {
     52 
     53 	return pdev->pd_dev;
     54 }
     55 
     56 void
     57 pci_set_drvdata(struct pci_dev *pdev, void *drvdata)
     58 {
     59 	pdev->pd_drvdata = drvdata;
     60 }
     61 
     62 void *
     63 pci_get_drvdata(struct pci_dev *pdev)
     64 {
     65 	return pdev->pd_drvdata;
     66 }
     67 
     68 const char *
     69 pci_name(struct pci_dev *pdev)
     70 {
     71 
     72 	/* XXX not sure this has the right format */
     73 	return device_xname(pci_dev_dev(pdev));
     74 }
     75 
     76 /*
     77  * Setup enough of a parent that we can access config space.
     78  * This is gross and grovels pci(4) and ppb(4) internals.
     79  */
     80 static struct pci_dev *
     81 alloc_fake_parent_device(device_t parent, const struct pci_attach_args *pa)
     82 {
     83 
     84 	if (parent == NULL || !device_is_a(parent, "pci"))
     85 		return NULL;
     86 
     87 	device_t pparent = device_parent(parent);
     88 	if (pparent == NULL || !device_is_a(pparent, "ppb"))
     89 		return NULL;
     90 
     91 	struct pci_softc *pcisc = device_private(parent);
     92 	struct ppb_softc *ppbsc = device_private(pparent);
     93 
     94 	struct pci_dev *parentdev = kmem_zalloc(sizeof(*parentdev), KM_SLEEP);
     95 
     96 	/* Copy this device's pci_attach_args{} as a base-line. */
     97 	struct pci_attach_args *npa = &parentdev->pd_pa;
     98 	*npa = *pa;
     99 
    100 	/* Now update with stuff found in parent. */
    101 	npa->pa_iot = pcisc->sc_iot;
    102 	npa->pa_memt = pcisc->sc_memt;
    103 	npa->pa_dmat = pcisc->sc_dmat;
    104 	npa->pa_dmat64 = pcisc->sc_dmat64;
    105 	npa->pa_pc = pcisc->sc_pc;
    106 	npa->pa_flags = 0;	/* XXX? */
    107 
    108 	/* Copy the parent tag, and read some info about it. */
    109 	npa->pa_tag = ppbsc->sc_tag;
    110 	pcireg_t id = pci_conf_read(npa->pa_pc, npa->pa_tag, PCI_ID_REG);
    111 	pcireg_t subid = pci_conf_read(npa->pa_pc, npa->pa_tag,
    112 	    PCI_SUBSYS_ID_REG);
    113 	pcireg_t class = pci_conf_read(npa->pa_pc, npa->pa_tag, PCI_CLASS_REG);
    114 
    115 	/*
    116 	 * Fill in as much of pci_attach_args and pci_dev as reasonably possible.
    117 	 * Most of this is not used currently.
    118 	 */
    119 	int bus, device, function;
    120 	pci_decompose_tag(npa->pa_pc, npa->pa_tag, &bus, &device, &function);
    121 	npa->pa_device = device;
    122 	npa->pa_function = function;
    123 	npa->pa_bus = bus;
    124 	npa->pa_id = id;
    125 	npa->pa_class = class;
    126 	npa->pa_intrswiz = pcisc->sc_intrswiz;
    127 	npa->pa_intrtag = pcisc->sc_intrtag;
    128 	npa->pa_intrpin = PCI_INTERRUPT_PIN_NONE;
    129 
    130 	parentdev->pd_dev = parent;
    131 
    132 	parentdev->bus = NULL;
    133 	parentdev->devfn = device << 3 | function;
    134 	parentdev->vendor = PCI_VENDOR(id);
    135 	parentdev->device = PCI_PRODUCT(id);
    136 	parentdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subid);
    137 	parentdev->subsystem_device = PCI_SUBSYS_ID(subid);
    138 	parentdev->revision = PCI_REVISION(class);
    139 	parentdev->class = __SHIFTOUT(class, 0xffffff00UL); /* ? */
    140 
    141 	return parentdev;
    142 }
    143 
    144 void
    145 linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent,
    146     const struct pci_attach_args *pa, int kludges)
    147 {
    148 	const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag,
    149 	    PCI_SUBSYS_ID_REG);
    150 	unsigned i;
    151 
    152 	memset(pdev, 0, sizeof(*pdev)); /* paranoia */
    153 
    154 	pdev->pd_pa = *pa;
    155 	pdev->pd_kludges = kludges;
    156 	pdev->pd_rom_vaddr = NULL;
    157 	pdev->pd_dev = dev;
    158 #if (NACPICA > 0)
    159 	const int seg = pci_get_segment(pa->pa_pc);
    160 	pdev->pd_ad = acpi_pcidev_find(seg, pa->pa_bus,
    161 	    pa->pa_device, pa->pa_function);
    162 #else
    163 	pdev->pd_ad = NULL;
    164 #endif
    165 	pdev->pd_saved_state = NULL;
    166 	pdev->pd_intr_handles = NULL;
    167 	pdev->pd_drvdata = NULL;
    168 	pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP);
    169 	pdev->bus->pb_pc = pa->pa_pc;
    170 	pdev->bus->pb_dev = parent;
    171 	pdev->bus->number = pa->pa_bus;
    172 	/*
    173 	 * NetBSD doesn't have an easy "am I PCIe" or "give me PCIe speed
    174 	 * from capability" function, but we already emulate the Linux
    175 	 * versions that do.
    176 	 */
    177 	if (pci_is_pcie(pdev)) {
    178 		pdev->bus->max_bus_speed = pcie_get_speed_cap(pdev);
    179 	} else {
    180 		/* XXX: Do AGP/PCI-X, etc.? */
    181 		pdev->bus->max_bus_speed = PCI_SPEED_UNKNOWN;
    182 	}
    183 	pdev->bus->self = alloc_fake_parent_device(parent, pa);
    184 	pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function);
    185 	pdev->vendor = PCI_VENDOR(pa->pa_id);
    186 	pdev->device = PCI_PRODUCT(pa->pa_id);
    187 	pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id);
    188 	pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id);
    189 	pdev->revision = PCI_REVISION(pa->pa_class);
    190 	pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */
    191 
    192 	CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
    193 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
    194 		const int reg = PCI_BAR(i);
    195 
    196 		pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc,
    197 		    pa->pa_tag, reg);
    198 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
    199 			pdev->pd_resources[i].type,
    200 			&pdev->pd_resources[i].addr,
    201 			&pdev->pd_resources[i].size,
    202 			&pdev->pd_resources[i].flags)) {
    203 			pdev->pd_resources[i].addr = 0;
    204 			pdev->pd_resources[i].size = 0;
    205 			pdev->pd_resources[i].flags = 0;
    206 		}
    207 		pdev->pd_resources[i].kva = NULL;
    208 		pdev->pd_resources[i].mapped = false;
    209 	}
    210 }
    211 
    212 int
    213 pci_find_capability(struct pci_dev *pdev, int cap)
    214 {
    215 
    216 	return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap,
    217 	    NULL, NULL);
    218 }
    219 
    220 int
    221 pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
    222 {
    223 
    224 	KASSERT(!ISSET(reg, 3));
    225 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg);
    226 	return 0;
    227 }
    228 
    229 int
    230 pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
    231 {
    232 
    233 	KASSERT(!ISSET(reg, 1));
    234 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    235 	    (reg &~ 2)) >> (8 * (reg & 2));
    236 	return 0;
    237 }
    238 
    239 int
    240 pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep)
    241 {
    242 
    243 	*valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    244 	    (reg &~ 3)) >> (8 * (reg & 3));
    245 	return 0;
    246 }
    247 
    248 int
    249 pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value)
    250 {
    251 
    252 	KASSERT(!ISSET(reg, 3));
    253 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value);
    254 	return 0;
    255 }
    256 
    257 int
    258 pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
    259     uint32_t *valuep)
    260 {
    261 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    262 	    PCI_FUNC(devfn));
    263 
    264 	KASSERT(!ISSET(reg, 1));
    265 	*valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3));
    266 	return 0;
    267 }
    268 
    269 int
    270 pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg,
    271     uint16_t *valuep)
    272 {
    273 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    274 	    PCI_FUNC(devfn));
    275 
    276 	KASSERT(!ISSET(reg, 1));
    277 	*valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2));
    278 	return 0;
    279 }
    280 
    281 int
    282 pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
    283     uint8_t *valuep)
    284 {
    285 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    286 	    PCI_FUNC(devfn));
    287 
    288 	*valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3));
    289 	return 0;
    290 }
    291 
    292 int
    293 pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg,
    294     uint32_t value)
    295 {
    296 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    297 	    PCI_FUNC(devfn));
    298 
    299 	KASSERT(!ISSET(reg, 3));
    300 	pci_conf_write(bus->pb_pc, tag, reg, value);
    301 	return 0;
    302 }
    303 
    304 static void
    305 pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes,
    306     uint32_t value)
    307 {
    308 	const uint32_t mask = ~((~0UL) << (8 * bytes));
    309 	const int reg32 = (reg &~ 3);
    310 	const unsigned int shift = (8 * (reg & 3));
    311 	uint32_t value32;
    312 
    313 	KASSERT(bytes <= 4);
    314 	KASSERT(!ISSET(value, ~mask));
    315 	value32 = pci_conf_read(pc, tag, reg32);
    316 	value32 &=~ (mask << shift);
    317 	value32 |= (value << shift);
    318 	pci_conf_write(pc, tag, reg32, value32);
    319 }
    320 
    321 int
    322 pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value)
    323 {
    324 
    325 	KASSERT(!ISSET(reg, 1));
    326 	pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value);
    327 	return 0;
    328 }
    329 
    330 int
    331 pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value)
    332 {
    333 
    334 	pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value);
    335 	return 0;
    336 }
    337 
    338 int
    339 pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg,
    340     uint16_t value)
    341 {
    342 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    343 	    PCI_FUNC(devfn));
    344 
    345 	KASSERT(!ISSET(reg, 1));
    346 	pci_rmw_config(bus->pb_pc, tag, reg, 2, value);
    347 	return 0;
    348 }
    349 
    350 int
    351 pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg,
    352     uint8_t value)
    353 {
    354 	pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn),
    355 	    PCI_FUNC(devfn));
    356 
    357 	pci_rmw_config(bus->pb_pc, tag, reg, 1, value);
    358 	return 0;
    359 }
    360 
    361 int
    362 pci_enable_msi(struct pci_dev *pdev)
    363 {
    364 	const struct pci_attach_args *const pa = &pdev->pd_pa;
    365 
    366 	if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1))
    367 		return -EINVAL;
    368 
    369 	pdev->msi_enabled = 1;
    370 	return 0;
    371 }
    372 
    373 void
    374 pci_disable_msi(struct pci_dev *pdev __unused)
    375 {
    376 	const struct pci_attach_args *const pa = &pdev->pd_pa;
    377 
    378 	if (pdev->pd_intr_handles != NULL) {
    379 		pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1);
    380 		pdev->pd_intr_handles = NULL;
    381 	}
    382 	pdev->msi_enabled = 0;
    383 }
    384 
    385 void
    386 pci_set_master(struct pci_dev *pdev)
    387 {
    388 	pcireg_t csr;
    389 
    390 	csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    391 	    PCI_COMMAND_STATUS_REG);
    392 	csr |= PCI_COMMAND_MASTER_ENABLE;
    393 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    394 	    PCI_COMMAND_STATUS_REG, csr);
    395 }
    396 
    397 void
    398 pci_clear_master(struct pci_dev *pdev)
    399 {
    400 	pcireg_t csr;
    401 
    402 	csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    403 	    PCI_COMMAND_STATUS_REG);
    404 	csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE;
    405 	pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    406 	    PCI_COMMAND_STATUS_REG, csr);
    407 }
    408 
    409 int
    410 pcie_capability_read_dword(struct pci_dev *pdev, int reg, uint32_t *valuep)
    411 {
    412 	pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    413 	pcitag_t tag = pdev->pd_pa.pa_tag;
    414 	int off;
    415 
    416 	*valuep = 0;
    417 
    418 	/* Must have capabilities. */
    419 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    420 		return 1;
    421 
    422 	*valuep = pci_conf_read(pc, tag, off + reg);
    423 
    424 	return 0;
    425 }
    426 
    427 int
    428 pcie_capability_read_word(struct pci_dev *pdev, int reg, uint16_t *valuep)
    429 {
    430 	pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    431 	pcitag_t tag = pdev->pd_pa.pa_tag;
    432 	int off;
    433 
    434 	*valuep = 0;
    435 
    436 	/* Must have capabilities. */
    437 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    438 		return 1;
    439 
    440 	*valuep = pci_conf_read(pc, tag, off + (reg &~ 2)) >> (8 * (reg & 2));
    441 
    442 	return 0;
    443 }
    444 
    445 int
    446 pcie_capability_write_dword(struct pci_dev *pdev, int reg, uint32_t value)
    447 {
    448 	pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    449 	pcitag_t tag = pdev->pd_pa.pa_tag;
    450 	int off;
    451 
    452 	/* Must have capabilities. */
    453 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    454 		return 1;
    455 
    456 	pci_conf_write(pc, tag, off + reg, value);
    457 
    458 	return 0;
    459 }
    460 
    461 int
    462 pcie_capability_write_word(struct pci_dev *pdev, int reg, uint16_t value)
    463 {
    464 	pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    465 	pcitag_t tag = pdev->pd_pa.pa_tag;
    466 	int off;
    467 
    468 	/* Must have capabilities. */
    469 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    470 		return 1;
    471 
    472 	pci_rmw_config(pc, tag, off + reg, 2, value);
    473 
    474 	return 0;
    475 }
    476 
    477 /* From PCIe 5.0 7.5.3.4 "Device Control Register" */
    478 static const unsigned readrqmax[] = {
    479 	128,
    480 	256,
    481 	512,
    482 	1024,
    483 	2048,
    484 	4096,
    485 };
    486 
    487 int
    488 pcie_get_readrq(struct pci_dev *pdev)
    489 {
    490 	pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    491 	pcitag_t tag = pdev->pd_pa.pa_tag;
    492 	unsigned val;
    493 	int off;
    494 
    495 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    496 		return -EINVAL; /* XXX NetBSD->Linux */
    497 
    498 	val = __SHIFTOUT(pci_conf_read(pc, tag, off + PCIE_DCSR),
    499 	    PCIE_DCSR_MAX_READ_REQ);
    500 
    501 	if (val >= __arraycount(readrqmax))
    502 		val = 0;
    503 	return readrqmax[val];
    504 }
    505 
    506 int
    507 pcie_set_readrq(struct pci_dev *pdev, int val)
    508 {
    509 	pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    510 	pcitag_t tag = pdev->pd_pa.pa_tag;
    511 	pcireg_t reg, newval = 0;
    512 	unsigned i;
    513 	int off;
    514 
    515 	for (i = 0; i < __arraycount(readrqmax); i++) {
    516 		if (readrqmax[i] == val) {
    517 			newval = i;
    518 			break;
    519 		}
    520 	}
    521 
    522 	if (i == __arraycount(readrqmax))
    523 		return -EINVAL;
    524 
    525 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
    526 		return -EINVAL; /* XXX NetBSD->Linux */
    527 
    528 	reg = pci_conf_read(pc, tag, off + PCIE_DCSR);
    529 	reg &= ~PCIE_DCSR_MAX_READ_REQ | (newval << 12);
    530 	pci_conf_write(pc, tag, off + PCIE_DCSR, reg);
    531 
    532 	return 0;
    533 }
    534 
    535 bus_addr_t
    536 pcibios_align_resource(void *p, const struct resource *resource,
    537     bus_addr_t addr, bus_size_t size)
    538 {
    539 	panic("pcibios_align_resource has accessed unaligned neurons!");
    540 }
    541 
    542 int
    543 pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource,
    544     bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused,
    545     bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t,
    546 	bus_size_t) __unused,
    547     struct pci_dev *pdev)
    548 {
    549 	const struct pci_attach_args *const pa = &pdev->pd_pa;
    550 	bus_space_tag_t bst;
    551 	int error;
    552 
    553 	switch (resource->flags) {
    554 	case IORESOURCE_MEM:
    555 		bst = pa->pa_memt;
    556 		break;
    557 
    558 	case IORESOURCE_IO:
    559 		bst = pa->pa_iot;
    560 		break;
    561 
    562 	default:
    563 		panic("I don't know what kind of resource you want!");
    564 	}
    565 
    566 	resource->r_bst = bst;
    567 	error = bus_space_alloc(bst, start, __type_max(bus_addr_t),
    568 	    size, align, 0, 0, &resource->start, &resource->r_bsh);
    569 	if (error)
    570 		return error;
    571 
    572 	resource->end = start + (size - 1);
    573 	return 0;
    574 }
    575 
    576 /*
    577  * XXX Mega-kludgerific!  pci_get_bus_and_slot and pci_get_class are
    578  * defined only for their single purposes in i915drm, in
    579  * i915_get_bridge_dev and intel_detect_pch.  We can't define them more
    580  * generally without adapting pci_find_device (and pci_enumerate_bus
    581  * internally) to pass a cookie through.
    582  */
    583 
    584 static int
    585 pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa)
    586 {
    587 
    588 	/* XXX domain */
    589 	if (pa->pa_bus != 0)
    590 		return 0;
    591 	if (pa->pa_device != 0)
    592 		return 0;
    593 	if (pa->pa_function != 0)
    594 		return 0;
    595 
    596 	return 1;
    597 }
    598 
    599 struct pci_dev *
    600 pci_get_domain_bus_and_slot(int domain, int bus, int slot)
    601 {
    602 	struct pci_attach_args pa;
    603 
    604 	KASSERT(domain == 0);
    605 	KASSERT(bus == 0);
    606 	KASSERT(slot == PCI_DEVFN(0, 0));
    607 
    608 	if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0))
    609 		return NULL;
    610 
    611 	struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
    612 	linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
    613 
    614 	return pdev;
    615 }
    616 
    617 static int
    618 pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa)
    619 {
    620 
    621 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
    622 		return 0;
    623 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
    624 		return 0;
    625 
    626 	return 1;
    627 }
    628 
    629 void
    630 pci_dev_put(struct pci_dev *pdev)
    631 {
    632 
    633 	if (pdev == NULL)
    634 		return;
    635 
    636 	KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE));
    637 	kmem_free(pdev->bus, sizeof(*pdev->bus));
    638 	kmem_free(pdev, sizeof(*pdev));
    639 }
    640 
    641 struct pci_dev *		/* XXX i915 kludge */
    642 pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from)
    643 {
    644 	struct pci_attach_args pa;
    645 
    646 	KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8));
    647 
    648 	if (from != NULL) {
    649 		pci_dev_put(from);
    650 		return NULL;
    651 	}
    652 
    653 	if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge))
    654 		return NULL;
    655 
    656 	struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP);
    657 	linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE);
    658 
    659 	return pdev;
    660 }
    661 
    662 int
    663 pci_dev_present(const struct pci_device_id *ids)
    664 {
    665 
    666 	/* XXX implement me -- pci_find_device doesn't pass a cookie */
    667 	return 0;
    668 }
    669 
    670 void
    671 pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused)
    672 {
    673 
    674 	/* XXX Disable the ROM address decoder.  */
    675 	KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
    676 	KASSERT(vaddr == pdev->pd_rom_vaddr);
    677 	bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size);
    678 	pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM;
    679 	pdev->pd_rom_vaddr = NULL;
    680 }
    681 
    682 /* XXX Whattakludge!  Should move this in sys/arch/.  */
    683 static int
    684 pci_map_rom_md(struct pci_dev *pdev)
    685 {
    686 #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
    687 	const bus_addr_t rom_base = 0xc0000;
    688 	const bus_size_t rom_size = 0x20000;
    689 	bus_space_handle_t rom_bsh;
    690 	int error;
    691 
    692 	if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY)
    693 		return ENXIO;
    694 	if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    695 		return ENXIO;
    696 	/* XXX Check whether this is the primary VGA card?  */
    697 	error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size,
    698 	    (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh);
    699 	if (error)
    700 		return ENXIO;
    701 
    702 	pdev->pd_rom_bst = pdev->pd_pa.pa_memt;
    703 	pdev->pd_rom_bsh = rom_bsh;
    704 	pdev->pd_rom_size = rom_size;
    705 	pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
    706 
    707 	return 0;
    708 #else
    709 	return ENXIO;
    710 #endif
    711 }
    712 
    713 void __pci_rom_iomem *
    714 pci_map_rom(struct pci_dev *pdev, size_t *sizep)
    715 {
    716 
    717 	KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM));
    718 
    719 	if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM,
    720 		(BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR),
    721 		&pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size)
    722 	    != 0)
    723 		goto fail_mi;
    724 	pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM;
    725 
    726 	/* XXX This type is obviously wrong in general...  */
    727 	if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
    728 		pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
    729 		&pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
    730 		pci_unmap_rom(pdev, NULL);
    731 		goto fail_mi;
    732 	}
    733 	goto success;
    734 
    735 fail_mi:
    736 	if (pci_map_rom_md(pdev) != 0)
    737 		goto fail_md;
    738 
    739 	/* XXX This type is obviously wrong in general...  */
    740 	if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh,
    741 		pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86,
    742 		&pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) {
    743 		pci_unmap_rom(pdev, NULL);
    744 		goto fail_md;
    745 	}
    746 
    747 success:
    748 	KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX);
    749 	*sizep = pdev->pd_rom_found_size;
    750 	pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst,
    751 	    pdev->pd_rom_found_bsh);
    752 	return pdev->pd_rom_vaddr;
    753 
    754 fail_md:
    755 	return NULL;
    756 }
    757 
    758 void __pci_rom_iomem *
    759 pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep)
    760 {
    761 
    762 	*sizep = 0;
    763 	return NULL;
    764 }
    765 
    766 int
    767 pci_enable_rom(struct pci_dev *pdev)
    768 {
    769 	const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    770 	const pcitag_t tag = pdev->pd_pa.pa_tag;
    771 	pcireg_t addr;
    772 	int s;
    773 
    774 	/* XXX Don't do anything if the ROM isn't there.  */
    775 
    776 	s = splhigh();
    777 	addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
    778 	addr |= PCI_MAPREG_ROM_ENABLE;
    779 	pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
    780 	splx(s);
    781 
    782 	return 0;
    783 }
    784 
    785 void
    786 pci_disable_rom(struct pci_dev *pdev)
    787 {
    788 	const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc;
    789 	const pcitag_t tag = pdev->pd_pa.pa_tag;
    790 	pcireg_t addr;
    791 	int s;
    792 
    793 	s = splhigh();
    794 	addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM);
    795 	addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE;
    796 	pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr);
    797 	splx(s);
    798 }
    799 
    800 bus_addr_t
    801 pci_resource_start(struct pci_dev *pdev, unsigned i)
    802 {
    803 
    804 	if (i >= PCI_NUM_RESOURCES)
    805 		panic("resource %d >= max %d", i, PCI_NUM_RESOURCES);
    806 	return pdev->pd_resources[i].addr;
    807 }
    808 
    809 bus_size_t
    810 pci_resource_len(struct pci_dev *pdev, unsigned i)
    811 {
    812 
    813 	if (i >= PCI_NUM_RESOURCES)
    814 		panic("resource %d >= max %d", i, PCI_NUM_RESOURCES);
    815 	return pdev->pd_resources[i].size;
    816 }
    817 
    818 bus_addr_t
    819 pci_resource_end(struct pci_dev *pdev, unsigned i)
    820 {
    821 
    822 	return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1);
    823 }
    824 
    825 int
    826 pci_resource_flags(struct pci_dev *pdev, unsigned i)
    827 {
    828 
    829 	if (i >= PCI_NUM_RESOURCES)
    830 		panic("resource %d >= max %d", i, PCI_NUM_RESOURCES);
    831 	return pdev->pd_resources[i].flags;
    832 }
    833 
    834 void __pci_iomem *
    835 pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size)
    836 {
    837 	int error;
    838 
    839 	KASSERT(i < PCI_NUM_RESOURCES);
    840 	KASSERT(pdev->pd_resources[i].kva == NULL);
    841 
    842 	if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM)
    843 		return NULL;
    844 	if (pdev->pd_resources[i].size < size)
    845 		return NULL;
    846 	error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr,
    847 	    size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags,
    848 	    &pdev->pd_resources[i].bsh);
    849 	if (error)
    850 		return NULL;
    851 	pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt;
    852 	pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst,
    853 	    pdev->pd_resources[i].bsh);
    854 	pdev->pd_resources[i].mapped = true;
    855 
    856 	return pdev->pd_resources[i].kva;
    857 }
    858 
    859 void
    860 pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva)
    861 {
    862 	unsigned i;
    863 
    864 	CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES);
    865 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
    866 		if (pdev->pd_resources[i].kva == kva)
    867 			break;
    868 	}
    869 	KASSERT(i < PCI_NUM_RESOURCES);
    870 
    871 	pdev->pd_resources[i].kva = NULL;
    872 	bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh,
    873 	    pdev->pd_resources[i].size);
    874 }
    875 
    876 void
    877 pci_save_state(struct pci_dev *pdev)
    878 {
    879 
    880 	KASSERT(pdev->pd_saved_state == NULL);
    881 	pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state),
    882 	    KM_SLEEP);
    883 	pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    884 	    pdev->pd_saved_state);
    885 }
    886 
    887 void
    888 pci_restore_state(struct pci_dev *pdev)
    889 {
    890 
    891 	KASSERT(pdev->pd_saved_state != NULL);
    892 	pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag,
    893 	    pdev->pd_saved_state);
    894 	kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state));
    895 	pdev->pd_saved_state = NULL;
    896 }
    897 
    898 bool
    899 pci_is_pcie(struct pci_dev *pdev)
    900 {
    901 
    902 	return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0);
    903 }
    904 
    905 bool
    906 pci_dma_supported(struct pci_dev *pdev, uintmax_t mask)
    907 {
    908 
    909 	/* XXX Cop-out.  */
    910 	if (mask > DMA_BIT_MASK(32))
    911 		return pci_dma64_available(&pdev->pd_pa);
    912 	else
    913 		return true;
    914 }
    915 
    916 bool
    917 pci_is_thunderbolt_attached(struct pci_dev *pdev)
    918 {
    919 
    920 	/* XXX Cop-out.  */
    921 	return false;
    922 }
    923 
    924 bool
    925 pci_is_root_bus(struct pci_bus *bus)
    926 {
    927 
    928 	return bus->number == 0;
    929 }
    930 
    931 int
    932 pci_domain_nr(struct pci_bus *bus)
    933 {
    934 
    935 	return pci_get_segment(bus->pb_pc);
    936 }
    937 
    938 /*
    939  * We explicitly rename pci_enable/disable_device so that you have to
    940  * review each use of them, since NetBSD's PCI API does _not_ respect
    941  * our local enablecnt here, but there are different parts of NetBSD
    942  * that automatically enable/disable like PMF, so you have to decide
    943  * for each one whether to call it or not.
    944  */
    945 
    946 int
    947 linux_pci_enable_device(struct pci_dev *pdev)
    948 {
    949 	const struct pci_attach_args *pa = &pdev->pd_pa;
    950 	pcireg_t csr;
    951 	int s;
    952 
    953 	if (pdev->pd_enablecnt++)
    954 		return 0;
    955 
    956 	s = splhigh();
    957 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    958 	/* If someone else (firmware) already enabled it, credit them.  */
    959 	if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE))
    960 		pdev->pd_enablecnt++;
    961 	csr |= PCI_COMMAND_IO_ENABLE;
    962 	csr |= PCI_COMMAND_MEM_ENABLE;
    963 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    964 	splx(s);
    965 
    966 	return 0;
    967 }
    968 
    969 void
    970 linux_pci_disable_device(struct pci_dev *pdev)
    971 {
    972 	const struct pci_attach_args *pa = &pdev->pd_pa;
    973 	pcireg_t csr;
    974 	int s;
    975 
    976 	if (--pdev->pd_enablecnt)
    977 		return;
    978 
    979 	s = splhigh();
    980 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    981 	csr &= ~PCI_COMMAND_IO_ENABLE;
    982 	csr &= ~PCI_COMMAND_MEM_ENABLE;
    983 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    984 	splx(s);
    985 }
    986 
    987 void
    988 linux_pci_dev_destroy(struct pci_dev *pdev)
    989 {
    990 	unsigned i;
    991 
    992 	if (pdev->bus->self != NULL) {
    993 		kmem_free(pdev->bus->self, sizeof(*pdev->bus->self));
    994 	}
    995 	if (pdev->bus != NULL) {
    996 		kmem_free(pdev->bus, sizeof(*pdev->bus));
    997 		pdev->bus = NULL;
    998 	}
    999 	if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) {
   1000 		pci_unmap_rom(pdev, pdev->pd_rom_vaddr);
   1001 		pdev->pd_rom_vaddr = 0;
   1002 	}
   1003 	for (i = 0; i < __arraycount(pdev->pd_resources); i++) {
   1004 		if (!pdev->pd_resources[i].mapped)
   1005 			continue;
   1006 		bus_space_unmap(pdev->pd_resources[i].bst,
   1007 		    pdev->pd_resources[i].bsh, pdev->pd_resources[i].size);
   1008 	}
   1009 
   1010 	/* There is no way these should be still in use.  */
   1011 	KASSERT(pdev->pd_saved_state == NULL);
   1012 	KASSERT(pdev->pd_intr_handles == NULL);
   1013 }
   1014 
   1015 enum pci_bus_speed
   1016 pcie_get_speed_cap(struct pci_dev *dev)
   1017 {
   1018 	pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
   1019 	pcitag_t tag = dev->pd_pa.pa_tag;
   1020 	pcireg_t lcap, lcap2, xcap;
   1021 	int off;
   1022 
   1023 	/* Must have capabilities. */
   1024 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
   1025 		return PCI_SPEED_UNKNOWN;
   1026 
   1027 	/* Only PCIe 3.x has LCAP2. */
   1028 	xcap = pci_conf_read(pc, tag, off + PCIE_XCAP);
   1029 	if (__SHIFTOUT(xcap, PCIE_XCAP_VER_MASK) >= 2) {
   1030 		lcap2 = pci_conf_read(pc, tag, off + PCIE_LCAP2);
   1031 		if (lcap2) {
   1032 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS64) != 0) {
   1033 				return PCIE_SPEED_64_0GT;
   1034 			}
   1035 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS32) != 0) {
   1036 				return PCIE_SPEED_32_0GT;
   1037 			}
   1038 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS16) != 0) {
   1039 				return PCIE_SPEED_16_0GT;
   1040 			}
   1041 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS8) != 0) {
   1042 				return PCIE_SPEED_8_0GT;
   1043 			}
   1044 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS5) != 0) {
   1045 				return PCIE_SPEED_5_0GT;
   1046 			}
   1047 			if ((lcap2 & PCIE_LCAP2_SUP_LNKS2) != 0) {
   1048 				return PCIE_SPEED_2_5GT;
   1049 			}
   1050 		}
   1051 	}
   1052 
   1053 	lcap = pci_conf_read(pc, tag, off + PCIE_LCAP);
   1054 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_64) {
   1055 		return PCIE_SPEED_64_0GT;
   1056 	}
   1057 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_32) {
   1058 		return PCIE_SPEED_32_0GT;
   1059 	}
   1060 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_16) {
   1061 		return PCIE_SPEED_16_0GT;
   1062 	}
   1063 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_8) {
   1064 		return PCIE_SPEED_8_0GT;
   1065 	}
   1066 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_5) {
   1067 		return PCIE_SPEED_5_0GT;
   1068 	}
   1069 	if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_2) {
   1070 		return PCIE_SPEED_2_5GT;
   1071 	}
   1072 
   1073 	return PCI_SPEED_UNKNOWN;
   1074 }
   1075 
   1076 /*
   1077  * This should walk the tree, it only checks this device currently.
   1078  * It also does not write to limiting_dev (the only caller in drm2
   1079  * currently does not use it.)
   1080  */
   1081 unsigned
   1082 pcie_bandwidth_available(struct pci_dev *dev,
   1083     struct pci_dev **limiting_dev,
   1084     enum pci_bus_speed *speed,
   1085     enum pcie_link_width *width)
   1086 {
   1087 	pci_chipset_tag_t pc = dev->pd_pa.pa_pc;
   1088 	pcitag_t tag = dev->pd_pa.pa_tag;
   1089 	pcireg_t lcsr;
   1090 	unsigned per_line_speed, num_lanes;
   1091 	int off;
   1092 
   1093 	/* Must have capabilities. */
   1094 	if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0)
   1095 		return 0;
   1096 
   1097 	if (speed)
   1098 		*speed = PCI_SPEED_UNKNOWN;
   1099 	if (width)
   1100 		*width = 0;
   1101 
   1102 	lcsr = pci_conf_read(pc, tag, off + PCIE_LCSR);
   1103 
   1104 	switch (lcsr & PCIE_LCSR_NLW) {
   1105 	case PCIE_LCSR_NLW_X1:
   1106 	case PCIE_LCSR_NLW_X2:
   1107 	case PCIE_LCSR_NLW_X4:
   1108 	case PCIE_LCSR_NLW_X8:
   1109 	case PCIE_LCSR_NLW_X12:
   1110 	case PCIE_LCSR_NLW_X16:
   1111 	case PCIE_LCSR_NLW_X32:
   1112 		num_lanes = __SHIFTOUT(lcsr, PCIE_LCSR_NLW);
   1113 		if (width)
   1114 			*width = num_lanes;
   1115 		break;
   1116 	default:
   1117 		num_lanes = 0;
   1118 		break;
   1119 	}
   1120 
   1121 	switch (__SHIFTOUT(lcsr, PCIE_LCSR_LINKSPEED)) {
   1122 	case PCIE_LCSR_LINKSPEED_2:
   1123 		*speed = PCIE_SPEED_2_5GT;
   1124 		per_line_speed = 2500 * 8 / 10;
   1125 		break;
   1126 	case PCIE_LCSR_LINKSPEED_5:
   1127 		*speed = PCIE_SPEED_5_0GT;
   1128 		per_line_speed = 5000 * 8 / 10;
   1129 		break;
   1130 	case PCIE_LCSR_LINKSPEED_8:
   1131 		*speed = PCIE_SPEED_8_0GT;
   1132 		per_line_speed = 8000 * 128 / 130;
   1133 		break;
   1134 	case PCIE_LCSR_LINKSPEED_16:
   1135 		*speed = PCIE_SPEED_16_0GT;
   1136 		per_line_speed = 16000 * 128 / 130;
   1137 		break;
   1138 	case PCIE_LCSR_LINKSPEED_32:
   1139 		*speed = PCIE_SPEED_32_0GT;
   1140 		per_line_speed = 32000 * 128 / 130;
   1141 		break;
   1142 	case PCIE_LCSR_LINKSPEED_64:
   1143 		*speed = PCIE_SPEED_64_0GT;
   1144 		per_line_speed = 64000 * 128 / 130;
   1145 		break;
   1146 	default:
   1147 		per_line_speed = 0;
   1148 	}
   1149 
   1150 	return num_lanes * per_line_speed;
   1151 }
   1152