4 #include <lunaix/device.h>
5 #include <lunaix/ds/ldga.h>
6 #include <lunaix/ds/llist.h>
7 #include <lunaix/ds/hashtable.h>
8 #include <lunaix/types.h>
9 #include <lunaix/changeling.h>
11 #include <asm-generic/isrm.h>
13 #define PCI_VENDOR_INVLD 0xffff
15 #define PCI_REG_VENDOR_DEV 0
16 #define PCI_REG_STATUS_CMD 0x4
17 #define PCI_REG_BAR(num) (0x10 + (num - 1) * 4)
19 #define PCI_DEV_VENDOR(x) ((x) & 0xffff)
20 #define PCI_DEV_DEVID(x) (((x) & 0xffff0000) >> 16)
21 #define PCI_INTR_IRQ(x) ((x) & 0xff)
22 #define PCI_INTR_PIN(x) (((x) & 0xff00) >> 8)
23 #define PCI_DEV_CLASS(x) ((x) >> 8)
24 #define PCI_DEV_REV(x) (((x) & 0xff))
25 #define PCI_BUS_NUM(x) (((x) >> 16) & 0xff)
26 #define PCI_SLOT_NUM(x) (((x) >> 11) & 0x1f)
27 #define PCI_FUNCT_NUM(x) (((x) >> 8) & 0x7)
29 #define PCI_BAR_MMIO(x) (!((x) & 0x1))
30 #define PCI_BAR_CACHEABLE(x) ((x) & 0x8)
31 #define PCI_BAR_TYPE(x) ((x) & 0x6)
32 #define PCI_BAR_ADDR_MM(x) ((x) & ~0xf)
33 #define PCI_BAR_ADDR_IO(x) ((x) & ~0x3)
34 #define PCI_BAR_COUNT 6
36 #define PCI_MSI_ADDR_LO(msi_base) ((msi_base) + 4)
37 #define PCI_MSI_ADDR_HI(msi_base) ((msi_base) + 8)
38 #define PCI_MSI_DATA(msi_base, offset) ((msi_base) + 8 + offset)
39 #define PCI_MSI_MASK(msi_base, offset) ((msi_base) + 0xc + offset)
41 #define MSI_CAP_64BIT 0x80
42 #define MSI_CAP_MASK 0x100
43 #define MSI_CAP_ENABLE 0x1
45 #define PCI_RCMD_DISABLE_INTR (1 << 10)
46 #define PCI_RCMD_FAST_B2B (1 << 9)
47 #define PCI_RCMD_BUS_MASTER (1 << 2)
48 #define PCI_RCMD_MM_ACCESS (1 << 1)
49 #define PCI_RCMD_IO_ACCESS 1
51 #define PCI_CFGADDR(pciloc) ((u32_t)(pciloc) << 8) | 0x80000000UL
53 #define PCILOC(bus, dev, funct) \
54 (((bus) & 0xff) << 8) | (((dev) & 0x1f) << 3) | ((funct) & 0x7)
55 #define PCILOC_BUS(loc) (((loc) >> 8) & 0xff)
56 #define PCILOC_DEV(loc) (((loc) >> 3) & 0x1f)
57 #define PCILOC_FN(loc) ((loc) & 0x7)
59 typedef unsigned int pci_reg_t;
60 typedef u16_t pciaddr_t;
62 // PCI device header format
63 // Ref: "PCI Local Bus Specification, Rev.3, Section 6.1"
65 #define BAR_TYPE_MMIO 0x1
66 #define BAR_TYPE_CACHABLE 0x2
67 #define PCI_DRV_NAME_LEN 32
86 struct pci_base_addr bar[6];
90 #define pci_probe_morpher morphable_attrs(pci_probe, mobj)
92 typedef bool (*pci_id_checker_t)(struct pci_probe*);
96 struct hlist_node entries;
97 struct device_def* definition;
99 pci_id_checker_t check_compact;
103 pci_register_driver(struct device_def* def, pci_id_checker_t checker);
106 * @brief 初始化PCI设备的基地址寄存器。返回由该基地址代表的,
107 * 设备所使用的MMIO或I/O地址空间的,大小。
108 * 参阅:PCI LB Spec. (Rev 3) Section 6.2.5.1, Implementation Note.
110 * @param dev The PCI device
111 * @param bar_out Value in BAR
112 * @param bar_num The index of BAR (starting from 1)
116 pci_bar_sizing(struct pci_probe* probe, u32_t* bar_out, u32_t bar_num);
119 * @brief Bind an abstract device instance to the pci device
121 * @param pcidev pci device
122 * @param dev abstract device instance
125 pci_bind_instance(struct pci_probe* probe, struct device* dev)
132 pci_msi_start(struct pci_probe* probe);
135 pci_msi_setup_at(msienv_t msienv, struct pci_probe* probe,
136 int i, isr_cb handler);
139 pci_msi_done(msienv_t env)
144 static inline msi_vector_t
145 pci_msi_setup_simple(struct pci_probe* probe, isr_cb handler)
150 env = pci_msi_start(probe);
151 msiv = pci_msi_setup_at(env, probe, 0, handler);
158 pci_bind_driver(struct pci_registry* reg);
161 static inline unsigned int
162 pci_device_vendor(struct pci_probe* probe)
164 return PCI_DEV_VENDOR(probe->device_info);
167 static inline unsigned int
168 pci_device_devid(struct pci_probe* probe)
170 return PCI_DEV_DEVID(probe->device_info);
173 static inline unsigned int
174 pci_device_class(struct pci_probe* probe)
176 return PCI_DEV_CLASS(probe->class_info);
179 static inline struct pci_base_addr*
180 pci_device_bar(struct pci_probe* probe, int index)
182 return &probe->bar[index];
186 pci_cmd_set_mmio(pci_reg_t* cmd)
188 *cmd |= PCI_RCMD_MM_ACCESS;
192 pci_requester_id(struct pci_probe* probe)
198 pci_cmd_set_pmio(pci_reg_t* cmd)
200 *cmd |= PCI_RCMD_IO_ACCESS;
204 pci_cmd_set_msi(pci_reg_t* cmd)
206 *cmd |= PCI_RCMD_DISABLE_INTR;
210 pci_cmd_set_bus_master(pci_reg_t* cmd)
212 *cmd |= PCI_RCMD_BUS_MASTER;
216 pci_cmd_set_fast_b2b(pci_reg_t* cmd)
218 *cmd |= PCI_RCMD_FAST_B2B;
222 pci_bar_mmio_space(struct pci_base_addr* bar)
224 return (bar->type & BAR_TYPE_MMIO);
228 pci_capability_msi(struct pci_probe* probe)
230 return !!probe->msi_loc;
234 pci_intr_irq(struct pci_probe* probe)
236 return PCI_INTR_IRQ(probe->intr_info);
240 pci_apply_command(struct pci_probe* probe, pci_reg_t cmd);
243 pci_read_cspace(ptr_t base, int offset);
246 pci_write_cspace(ptr_t base, int offset, pci_reg_t data);
248 #endif /* __LUNAIX_PCI_H */