19#include <drivers/core/dm.h>
20#include <drivers/core/driver.h>
22#include "../../pci/pci_ids.h"
23#include "../../pci/pci_regs.h"
25#define RT_PCI_INTX_PIN_MAX 4
26#define RT_PCI_BAR_NR_MAX 6
27#define RT_PCI_DEVICE_MAX 32
28#define RT_PCI_FUNCTION_MAX 8
30#define RT_PCI_FIND_CAP_TTL 48
40#define RT_PCI_DEVID(bus, devfn) ((((rt_uint16_t)(bus)) << 8) | (devfn))
41#define RT_PCI_DEVFN(slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
42#define RT_PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
43#define RT_PCI_FUNC(devfn) ((devfn) & 0x07)
45#define PCIE_LINK_STATE_L0S RT_BIT(0)
46#define PCIE_LINK_STATE_L1 RT_BIT(1)
47#define PCIE_LINK_STATE_CLKPM RT_BIT(2)
48#define PCIE_LINK_STATE_L1_1 RT_BIT(3)
49#define PCIE_LINK_STATE_L1_2 RT_BIT(4)
50#define PCIE_LINK_STATE_L1_1_PCIPM RT_BIT(5)
51#define PCIE_LINK_STATE_L1_2_PCIPM RT_BIT(6)
52#define PCIE_LINK_STATE_ALL \
54 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 | \
55 PCIE_LINK_STATE_CLKPM | \
56 PCIE_LINK_STATE_L1_1 | PCIE_LINK_STATE_L1_2 | \
57 PCIE_LINK_STATE_L1_1_PCIPM | PCIE_LINK_STATE_L1_2_PCIPM \
68#define PCI_BUS_REGION_F_NONE 0xffffffff
69#define PCI_BUS_REGION_F_MEM 0x00000000
70#define PCI_BUS_REGION_F_IO 0x00000001
71#define PCI_BUS_REGION_F_PREFETCH 0x00000008
107#define PCI_ANY_ID (~0)
108#define RT_PCI_DEVICE_ID(vend, dev) \
111 .subsystem_vendor = PCI_ANY_ID, \
112 .subsystem_device = PCI_ANY_ID
114#define RT_PCI_DEVICE_CLASS(dev_class, dev_class_mask) \
115 .vendor = PCI_ANY_ID, .device = PCI_ANY_ID, \
116 .subsystem_vendor = PCI_ANY_ID, \
117 .subsystem_device = PCI_ANY_ID, \
118 .class = (dev_class), .class_mask = (dev_class_mask),
207#define rt_device_to_pci_host_bridge(dev) rt_container_of(dev, struct rt_pci_host_bridge, parent)
339 return pdev->
hdr_type == PCIM_HDRTYPE_BRIDGE ||
340 pdev->
hdr_type == PCIM_HDRTYPE_CARDBUS;
348#define rt_pci_foreach_bridge(pdev, bus) \
349 rt_list_for_each_entry(pdev, &bus->devices_nodes, list) \
350 if (rt_pci_is_bridge(pdev))
466 return rt_ioremap((
void *)res->
base, res->
size);
481#define RT_PCI_IRQ_F_LEGACY RT_BIT(0)
482#define RT_PCI_IRQ_F_MSI RT_BIT(1)
483#define RT_PCI_IRQ_F_MSIX RT_BIT(2)
484#define RT_PCI_IRQ_F_AFFINITY RT_BIT(3)
485#define RT_PCI_IRQ_F_ALL_TYPES (RT_PCI_IRQ_F_LEGACY | RT_PCI_IRQ_F_MSI | RT_PCI_IRQ_F_MSIX)
551 for (
int i = 0; i < nvectors; ++i)
553 entries[i].
index = i;
566 return res == 1 ? res : RT_EOK;
600#define RT_PCI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, pci, BUILIN)
#define RT_ARRAY_SIZE(arr)
rt_err_t rt_pci_host_bridge_probe(struct rt_pci_host_bridge *host_bridge)
rt_err_t rt_pci_device_register(struct rt_pci_device *pdev)
rt_err_t rt_pci_device_alloc_resource(struct rt_pci_host_bridge *host_bridge, struct rt_pci_device *pdev)
rt_err_t rt_pci_host_bridge_remove(struct rt_pci_host_bridge *host_bridge)
rt_bool_t rt_pci_check_and_mask_intx(struct rt_pci_device *pdev)
struct rt_pci_bus_resource * rt_pci_find_bar(struct rt_pci_device *pdev, rt_ubase_t flags, int index)
struct rt_pci_host_bridge * rt_pci_host_bridge_alloc(rt_size_t priv_size)
rt_err_t rt_pci_bus_read_config_u8(struct rt_pci_bus *bus, rt_uint32_t devfn, int pos, rt_uint8_t *value)
rt_err_t rt_pci_bus_read_config_u32(struct rt_pci_bus *bus, rt_uint32_t devfn, int pos, rt_uint32_t *value)
rt_inline rt_ssize_t rt_pci_msi_enable_range(struct rt_pci_device *pdev, int min, int max)
rt_inline rt_ssize_t rt_pci_alloc_vector(struct rt_pci_device *pdev, int min, int max, rt_uint32_t flags, RT_IRQ_AFFINITY_DECLARE((*affinities)))
rt_inline rt_ssize_t rt_pci_msix_enable_range(struct rt_pci_device *pdev, struct rt_pci_msix_entry *entries, int min, int max)
void rt_pci_intx(struct rt_pci_device *pdev, rt_bool_t enable)
rt_inline rt_ssize_t rt_pci_msix_vector_count(struct rt_pci_device *pdev)
rt_err_t rt_pci_setup_device(struct rt_pci_device *pdev)
rt_inline rt_err_t rt_pci_ofw_host_bridge_init(struct rt_ofw_node *dev_np, struct rt_pci_host_bridge *host_bridge)
rt_err_t rt_pci_bus_write_config_u8(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, rt_uint8_t value)
rt_inline rt_err_t rt_pci_ofw_bus_init(struct rt_pci_bus *bus)
rt_err_t rt_pci_host_bridge_init(struct rt_pci_host_bridge *host_bridge)
rt_inline rt_bool_t rt_pci_pme_capable(struct rt_pci_device *pdev, enum rt_pci_power state)
rt_inline rt_ssize_t rt_pci_msi_vector_count(struct rt_pci_device *pdev)
rt_inline rt_bool_t rt_pci_is_pcie(struct rt_pci_device *pdev)
rt_err_t rt_pci_bus_read_config_generic_u32(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, int width, rt_uint32_t *value)
rt_err_t rt_pci_scan_root_bus_bridge(struct rt_pci_host_bridge *host_bridge)
rt_uint8_t rt_pci_bus_find_capability(struct rt_pci_bus *bus, rt_uint32_t devfn, int cap)
rt_err_t rt_pci_bus_read_config_u16(struct rt_pci_bus *bus, rt_uint32_t devfn, int pos, rt_uint16_t *value)
rt_inline rt_ssize_t rt_pci_msix_enable(struct rt_pci_device *pdev, struct rt_pci_msix_entry *entries, int count)
rt_inline rt_ssize_t rt_pci_msix_enable_range_affinity(struct rt_pci_device *pdev, struct rt_pci_msix_entry *entries, int min, int max, RT_IRQ_AFFINITY_DECLARE((*affinities)))
rt_err_t rt_pci_driver_register(struct rt_pci_driver *pdrv)
rt_err_t rt_pci_bus_read_config_uxx(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, int width, rt_uint32_t *value)
struct rt_pci_device * rt_pci_alloc_device(struct rt_pci_bus *bus)
rt_err_t rt_pci_device_remove(struct rt_pci_device *pdev)
rt_inline void rt_pci_free_vector(struct rt_pci_device *pdev)
rt_size_t rt_pci_scan_slot(struct rt_pci_bus *bus, rt_uint32_t devfn)
void rt_pci_enum_device(struct rt_pci_bus *bus, rt_bool_t(callback(struct rt_pci_device *, void *)), void *data)
rt_inline rt_err_t rt_pci_read_config_u16(const struct rt_pci_device *pdev, int reg, rt_uint16_t *value)
struct rt_pci_host_bridge * rt_pci_find_host_bridge(struct rt_pci_bus *bus)
rt_err_t rt_pci_bus_write_config_u32(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, rt_uint32_t value)
void rt_pci_irq_mask(struct rt_pci_device *pdev)
#define RT_PCI_DEVID(bus, devfn)
rt_err_t rt_pci_enable_wake(struct rt_pci_device *pci_dev, enum rt_pci_power state, rt_bool_t enable)
rt_uint16_t rt_pci_find_ext_next_capability(struct rt_pci_device *pdev, rt_uint16_t pos, int cap)
rt_inline rt_err_t rt_pci_write_config_u32(const struct rt_pci_device *pdev, int reg, rt_uint32_t value)
rt_bool_t rt_pci_check_and_unmask_intx(struct rt_pci_device *pdev)
rt_inline rt_err_t rt_pci_msi_enable(struct rt_pci_device *pdev)
void rt_pci_pme_active(struct rt_pci_device *pdev, rt_bool_t enable)
rt_inline rt_bool_t rt_pci_is_bridge(struct rt_pci_device *pdev)
void rt_pci_msix_init(struct rt_pci_device *pdev)
rt_inline void * rt_pci_iomap(struct rt_pci_device *pdev, int bar_idx)
rt_inline rt_err_t rt_pci_write_config_u16(const struct rt_pci_device *pdev, int reg, rt_uint16_t value)
rt_inline void rt_pci_msix_entry_index_linear(struct rt_pci_msix_entry *entries, rt_size_t nvectors)
rt_err_t rt_pci_bus_write_config_generic_u32(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, int width, rt_uint32_t value)
rt_inline rt_err_t rt_pci_read_config_u8(const struct rt_pci_device *pdev, int reg, rt_uint8_t *value)
rt_uint8_t rt_pci_irq_slot(struct rt_pci_device *pdev, rt_uint8_t *pinp)
rt_err_t rt_pci_host_bridge_register(struct rt_pci_host_bridge *host_bridge)
rt_uint16_t rt_pci_find_ext_capability(struct rt_pci_device *pdev, int cap)
rt_uint32_t rt_pci_domain(struct rt_pci_device *pdev)
struct rt_spinlock rt_pci_lock
rt_uint32_t rt_pci_scan_child_buses(struct rt_pci_bus *bus, rt_size_t buses)
rt_inline rt_err_t rt_pci_ofw_bus_free(struct rt_pci_bus *bus)
rt_err_t rt_pci_bus_write_config_uxx(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, int width, rt_uint32_t value)
rt_inline rt_err_t rt_pci_write_config_u8(const struct rt_pci_device *pdev, int reg, rt_uint8_t value)
rt_err_t rt_pci_region_setup(struct rt_pci_host_bridge *host_bridge)
rt_uint32_t rt_pci_scan_child_bus(struct rt_pci_bus *bus)
void rt_pci_clear_master(struct rt_pci_device *pdev)
void rt_pci_pme_init(struct rt_pci_device *pdev)
rt_uint8_t rt_pci_find_next_capability(struct rt_pci_device *pdev, rt_uint8_t pos, int cap)
void rt_pci_assign_irq(struct rt_pci_device *pdev)
rt_inline rt_err_t rt_pci_ofw_device_init(struct rt_pci_device *pdev)
rt_inline int rt_pci_ofw_irq_parse_and_map(struct rt_pci_device *pdev, rt_uint8_t slot, rt_uint8_t pin)
rt_err_t rt_pci_bus_remove(struct rt_pci_bus *bus)
rt_err_t rt_pci_host_bridge_free(struct rt_pci_host_bridge *)
rt_inline rt_err_t rt_pci_read_config_u32(const struct rt_pci_device *pdev, int reg, rt_uint32_t *value)
rt_err_t rt_pci_bus_write_config_u16(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, rt_uint16_t value)
rt_uint8_t rt_pci_find_capability(struct rt_pci_device *pdev, int cap)
struct rt_pci_bus_region * rt_pci_region_alloc(struct rt_pci_host_bridge *host_bridge, void **out_addr, rt_size_t size, rt_ubase_t flags, rt_bool_t mem64)
void rt_pci_msi_init(struct rt_pci_device *pdev)
rt_inline rt_err_t rt_pci_msi_disable(struct rt_pci_device *pdev)
void rt_pci_set_master(struct rt_pci_device *pdev)
rt_inline rt_err_t rt_pci_ofw_parse_ranges(struct rt_ofw_node *dev_np, struct rt_pci_host_bridge *host_bridge)
struct rt_pci_bus * rt_pci_find_root_bus(struct rt_pci_bus *bus)
rt_inline rt_uint16_t rt_pci_dev_id(struct rt_pci_device *pdev)
rt_uint8_t rt_pci_irq_intx(struct rt_pci_device *pdev, rt_uint8_t pin)
rt_inline rt_err_t rt_pci_msix_disable(struct rt_pci_device *pdev)
void rt_pci_irq_unmask(struct rt_pci_device *pdev)
rt_inline rt_err_t rt_pci_ofw_device_free(struct rt_pci_device *pdev)
rt_inline rt_bool_t rt_pci_is_root_bus(struct rt_pci_bus *bus)
struct rt_pci_device * rt_pci_scan_single_device(struct rt_pci_bus *bus, rt_uint32_t devfn)
rt_inline rt_ssize_t rt_pci_msi_enable_range_affinity(struct rt_pci_device *pdev, int min, int max, RT_IRQ_AFFINITY_DECLARE((*affinities)))
#define RT_PCI_BAR_NR_MAX
const struct rt_pci_device_id * rt_pci_match_ids(struct rt_pci_device *pdev, const struct rt_pci_device_id *ids)
const struct rt_pci_device_id * rt_pci_match_id(struct rt_pci_device *pdev, const struct rt_pci_device_id *id)
#define RT_IRQ_AFFINITY_DECLARE(name)
unsigned short rt_uint16_t
struct rt_list_node rt_list_t
unsigned long long rt_uint64_t
struct rt_pci_bus * parent
struct rt_pci_device * self
struct rt_pci_host_bridge * host_bridge
const struct rt_pci_ops * ops
rt_uint32_t subsystem_device
rt_uint32_t subsystem_vendor
rt_uint8_t broken_intx_masking
rt_uint16_t subsystem_device
rt_uint16_t subsystem_vendor
struct rt_pci_bus * subbus
const struct rt_pci_device_id * id
struct rt_pci_bus_resource resource[RT_PCI_BAR_NR_MAX]
rt_uint8_t multi_function
const struct rt_pci_device_id * ids
rt_err_t(* shutdown)(struct rt_pci_device *pdev)
rt_err_t(* remove)(struct rt_pci_device *pdev)
rt_err_t(* probe)(struct rt_pci_device *pdev)
int(* irq_map)(struct rt_pci_device *pdev, rt_uint8_t slot, rt_uint8_t pin)
struct rt_pci_bus_region * bus_regions
struct rt_pci_bus * root_bus
const struct rt_pci_ops * ops
rt_uint8_t(* irq_slot)(struct rt_pci_device *pdev, rt_uint8_t *pinp)
struct rt_pci_bus_region * dma_regions
const struct rt_pci_ops * child_ops
rt_err_t(* remove)(struct rt_pci_bus *bus)
rt_err_t(* write)(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, int width, rt_uint32_t value)
rt_err_t(* add)(struct rt_pci_bus *bus)
rt_err_t(* read)(struct rt_pci_bus *bus, rt_uint32_t devfn, int reg, int width, rt_uint32_t *value)