^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Simple, generic PCI host controller driver targeting firmware-initialised
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2014 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Will Deacon <will.deacon@arm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pci-ecam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static const struct pci_ecam_ops gen_pci_cfg_cam_bus_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) .bus_shift = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) .pci_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) .map_bus = pci_ecam_map_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .read = pci_generic_config_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .write = pci_generic_config_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static bool pci_dw_valid_device(struct pci_bus *bus, unsigned int devfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct pci_config_window *cfg = bus->sysdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * The Synopsys DesignWare PCIe controller in ECAM mode will not filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * type 0 config TLPs sent to devices 1 and up on its downstream port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * resulting in devices appearing multiple times on bus 0 unless we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * filter out those accesses here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (bus->number == cfg->busr.start && PCI_SLOT(devfn) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void __iomem *pci_dw_ecam_map_bus(struct pci_bus *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int devfn, int where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!pci_dw_valid_device(bus, devfn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return pci_ecam_map_bus(bus, devfn, where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const struct pci_ecam_ops pci_dw_ecam_bus_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .bus_shift = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .pci_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .map_bus = pci_dw_ecam_map_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .read = pci_generic_config_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .write = pci_generic_config_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static const struct of_device_id gen_pci_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) { .compatible = "pci-host-cam-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .data = &gen_pci_cfg_cam_bus_ops },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) { .compatible = "pci-host-ecam-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .data = &pci_generic_ecam_ops },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) { .compatible = "marvell,armada8k-pcie-ecam",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .data = &pci_dw_ecam_bus_ops },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) { .compatible = "socionext,synquacer-pcie-ecam",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .data = &pci_dw_ecam_bus_ops },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) { .compatible = "snps,dw-pcie-ecam",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .data = &pci_dw_ecam_bus_ops },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) MODULE_DEVICE_TABLE(of, gen_pci_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static struct platform_driver gen_pci_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .name = "pci-host-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .of_match_table = gen_pci_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .probe = pci_host_common_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .remove = pci_host_common_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) module_platform_driver(gen_pci_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) MODULE_LICENSE("GPL v2");