8c7057a318c0a3e008ad30c18a73fba3e7311390
[lunaix-os.git] / lunaix-os / includes / hal / pci.h
1 #ifndef __LUNAIX_PCI_H
2 #define __LUNAIX_PCI_H
3
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>
10
11 #include <asm-generic/isrm.h>
12
13 #include "irq.h"
14
15 #define PCI_VENDOR_INVLD 0xffff
16
17 #define PCI_REG_VENDOR_DEV 0
18 #define PCI_REG_STATUS_CMD 0x4
19 #define PCI_REG_BAR(num) (0x10 + (num - 1) * 4)
20
21 #define PCI_DEV_VENDOR(x) ((x) & 0xffff)
22 #define PCI_DEV_DEVID(x) (((x) & 0xffff0000) >> 16)
23 #define PCI_INTR_IRQ(x) ((x) & 0xff)
24 #define PCI_INTR_PIN(x) (((x) & 0xff00) >> 8)
25 #define PCI_DEV_CLASS(x) ((x) >> 8)
26 #define PCI_DEV_REV(x) (((x) & 0xff))
27 #define PCI_BUS_NUM(x) (((x) >> 16) & 0xff)
28 #define PCI_SLOT_NUM(x) (((x) >> 11) & 0x1f)
29 #define PCI_FUNCT_NUM(x) (((x) >> 8) & 0x7)
30
31 #define PCI_BAR_MMIO(x) (!((x) & 0x1))
32 #define PCI_BAR_CACHEABLE(x) ((x) & 0x8)
33 #define PCI_BAR_TYPE(x) ((x) & 0x6)
34 #define PCI_BAR_ADDR_MM(x) ((x) & ~0xf)
35 #define PCI_BAR_ADDR_IO(x) ((x) & ~0x3)
36 #define PCI_BAR_COUNT 6
37
38 #define PCI_MSI_ADDR_LO(msi_base) ((msi_base) + 4)
39 #define PCI_MSI_ADDR_HI(msi_base) ((msi_base) + 8)
40 #define PCI_MSI_DATA(msi_base, offset) ((msi_base) + 8 + offset)
41 #define PCI_MSI_MASK(msi_base, offset) ((msi_base) + 0xc + offset)
42
43 #define MSI_CAP_64BIT 0x80
44 #define MSI_CAP_MASK 0x100
45 #define MSI_CAP_ENABLE 0x1
46
47 #define PCI_RCMD_DISABLE_INTR (1 << 10)
48 #define PCI_RCMD_FAST_B2B (1 << 9)
49 #define PCI_RCMD_BUS_MASTER (1 << 2)
50 #define PCI_RCMD_MM_ACCESS (1 << 1)
51 #define PCI_RCMD_IO_ACCESS 1
52
53 #define PCI_CFGADDR(pciloc) ((u32_t)(pciloc) << 8) | 0x80000000UL
54
55 #define PCILOC(bus, dev, funct)                                                \
56     (((bus) & 0xff) << 8) | (((dev) & 0x1f) << 3) | ((funct) & 0x7)
57 #define PCILOC_BUS(loc) (((loc) >> 8) & 0xff)
58 #define PCILOC_DEV(loc) (((loc) >> 3) & 0x1f)
59 #define PCILOC_FN(loc) ((loc) & 0x7)
60
61 typedef unsigned int pci_reg_t;
62 typedef u16_t pciaddr_t;
63
64 // PCI device header format
65 // Ref: "PCI Local Bus Specification, Rev.3, Section 6.1"
66
67 #define BAR_TYPE_MMIO 0x1
68 #define BAR_TYPE_CACHABLE 0x2
69 #define PCI_DRV_NAME_LEN 32
70
71 struct pci_base_addr
72 {
73     u32_t start;
74     u32_t size;
75     u32_t type;
76 };
77
78 struct pci_probe
79 {
80     morph_t mobj;
81
82     pciaddr_t loc;
83     u16_t intr_info;
84     u32_t device_info;
85     u32_t class_info;
86     u32_t cspace_base;
87     u32_t msi_loc;
88     struct pci_base_addr bar[6];
89
90     struct device* bind;
91     struct irq_domain* irq_domain;
92 };
93 #define pci_probe_morpher   morphable_attrs(pci_probe, mobj)
94
95 typedef bool (*pci_id_checker_t)(struct pci_probe*);
96
97 struct pci_registry
98 {
99     struct hlist_node entries;
100     struct device_def* definition;
101
102     pci_id_checker_t check_compact;
103 };
104
105 bool
106 pci_register_driver(struct device_def* def, pci_id_checker_t checker);
107
108 /**
109  * @brief 初始化PCI设备的基地址寄存器。返回由该基地址代表的,
110  * 设备所使用的MMIO或I/O地址空间的,大小。
111  * 参阅:PCI LB Spec. (Rev 3) Section 6.2.5.1, Implementation Note.
112  *
113  * @param dev The PCI device
114  * @param bar_out Value in BAR
115  * @param bar_num The index of BAR (starting from 1)
116  * @return size_t
117  */
118 size_t
119 pci_bar_sizing(struct pci_probe* probe, u32_t* bar_out, u32_t bar_num);
120
121 irq_t
122 pci_declare_msi_irq(irq_servant callback, 
123                     struct pci_probe* probe, void *irq_extra);
124
125 int
126 pci_assign_msi(struct pci_probe* probe, irq_t irq);
127
128 /**
129  * @brief Bind an abstract device instance to the pci device
130  *
131  * @param pcidev pci device
132  * @param dev abstract device instance
133  */
134 static inline void
135 pci_bind_instance(struct pci_probe* probe, struct device* dev)
136 {
137     probe->bind = dev;
138
139 }
140
141 int
142 pci_bind_driver(struct pci_registry* reg);
143
144
145 static inline unsigned int
146 pci_device_vendor(struct pci_probe* probe)
147 {
148     return PCI_DEV_VENDOR(probe->device_info);
149 }
150
151 static inline unsigned int
152 pci_device_devid(struct pci_probe* probe)
153 {
154     return PCI_DEV_DEVID(probe->device_info);
155 }
156
157 static inline unsigned int
158 pci_device_class(struct pci_probe* probe)
159 {
160     return PCI_DEV_CLASS(probe->class_info);
161 }
162
163 static inline struct pci_base_addr*
164 pci_device_bar(struct pci_probe* probe, int index)
165 {
166     return &probe->bar[index];
167 }
168
169 static inline void
170 pci_cmd_set_mmio(pci_reg_t* cmd)
171 {
172     *cmd |= PCI_RCMD_MM_ACCESS;
173 }
174
175 static inline ptr_t
176 pci_requester_id(struct pci_probe* probe)
177 {
178     return probe->loc;
179 }
180
181 static inline void
182 pci_cmd_set_pmio(pci_reg_t* cmd)
183 {
184     *cmd |= PCI_RCMD_IO_ACCESS;
185 }
186
187 static inline void
188 pci_cmd_set_msi(pci_reg_t* cmd)
189 {
190     *cmd |= PCI_RCMD_DISABLE_INTR;
191 }
192
193 static inline void
194 pci_cmd_set_bus_master(pci_reg_t* cmd)
195 {
196     *cmd |= PCI_RCMD_BUS_MASTER;
197 }
198
199 static inline void
200 pci_cmd_set_fast_b2b(pci_reg_t* cmd)
201 {
202     *cmd |= PCI_RCMD_FAST_B2B;
203 }
204
205 static inline bool
206 pci_bar_mmio_space(struct pci_base_addr* bar)
207 {
208     return (bar->type & BAR_TYPE_MMIO);
209 }
210
211 static inline bool
212 pci_capability_msi(struct pci_probe* probe)
213 {
214     return !!probe->msi_loc;
215 }
216
217 static inline int
218 pci_intr_irq(struct pci_probe* probe)
219 {
220     return PCI_INTR_IRQ(probe->intr_info);
221 }
222
223 void
224 pci_apply_command(struct pci_probe* probe, pci_reg_t cmd);
225
226 pci_reg_t
227 pci_read_cspace(ptr_t base, int offset);
228
229 void
230 pci_write_cspace(ptr_t base, int offset, pci_reg_t data);
231
232 #endif /* __LUNAIX_PCI_H */