Convert USB detection code to use struct pci_device.
[seabios.git] / src / pci.h
1 #ifndef __PCI_H
2 #define __PCI_H
3
4 #include "types.h" // u32
5
6 static inline u8 pci_bdf_to_bus(u16 bdf) {
7     return bdf >> 8;
8 }
9 static inline u8 pci_bdf_to_devfn(u16 bdf) {
10     return bdf & 0xff;
11 }
12 static inline u16 pci_bdf_to_busdev(u16 bdf) {
13     return bdf & ~0x07;
14 }
15 static inline u8 pci_bdf_to_dev(u16 bdf) {
16     return (bdf >> 3) & 0x1f;
17 }
18 static inline u8 pci_bdf_to_fn(u16 bdf) {
19     return bdf & 0x07;
20 }
21 static inline u16 pci_to_bdf(int bus, int dev, int fn) {
22     return (bus<<8) | (dev<<3) | fn;
23 }
24 static inline u16 pci_bus_devfn_to_bdf(int bus, u16 devfn) {
25     return (bus << 8) | devfn;
26 }
27
28 void pci_config_writel(u16 bdf, u32 addr, u32 val);
29 void pci_config_writew(u16 bdf, u32 addr, u16 val);
30 void pci_config_writeb(u16 bdf, u32 addr, u8 val);
31 u32 pci_config_readl(u16 bdf, u32 addr);
32 u16 pci_config_readw(u16 bdf, u32 addr);
33 u8 pci_config_readb(u16 bdf, u32 addr);
34 void pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on);
35
36 int pci_find_device(u16 vendid, u16 devid);
37 int pci_find_class(u16 classid);
38
39 struct pci_device {
40     u16 bdf;
41     u8 rootbus;
42     struct pci_device *next;
43     struct pci_device *parent;
44
45     // Configuration space device information
46     u16 vendor, device;
47     u16 class;
48     u8 prog_if, revision;
49     u8 header_type;
50     u8 secondary_bus;
51 };
52 extern struct pci_device *PCIDevices;
53 extern int MaxPCIBus;
54 void pci_probe(void);
55 static inline u32 pci_classprog(struct pci_device *pci) {
56     return (pci->class << 8) | pci->prog_if;
57 }
58
59 #define foreachpci(PCI)                         \
60     for (PCI=PCIDevices; PCI; PCI=PCI->next)
61
62 #define PP_ROOT      (1<<17)
63 #define PP_PCIBRIDGE (1<<18)
64 extern int *PCIpaths;
65 void pci_path_setup(void);
66
67 int pci_next(int bdf, int *pmax);
68 #define foreachbdf(BDF, MAX)                    \
69     for (MAX=0x0100, BDF=pci_next(0, &MAX)      \
70          ; BDF >= 0                             \
71          ; BDF=pci_next(BDF+1, &MAX))
72
73 #define foreachbdf_in_bus(BDF, MAX, BUS)                                \
74     for (MAX = pci_bus_devfn_to_bdf(BUS, 0) + 0x0100,                   \
75          BDF = pci_next(pci_bus_devfn_to_bdf(BUS, 0), &MAX)             \
76          ; BDF >= 0 && BDF < pci_bus_devfn_to_bdf(BUS, 0) + 0x0100      \
77          ; MAX = pci_bus_devfn_to_bdf(BUS, 0) + 0x0100,                 \
78            BDF = pci_next(BDF + 1, &MAX))
79
80 #define PCI_ANY_ID      (~0)
81 struct pci_device_id {
82     u32 vendid;
83     u32 devid;
84     u32 class;
85     u32 class_mask;
86     void (*func)(u16 bdf, void *arg);
87 };
88
89 #define PCI_DEVICE(vendor_id, device_id, init_func)     \
90     {                                                   \
91         .vendid = (vendor_id),                          \
92         .devid = (device_id),                           \
93         .class = PCI_ANY_ID,                            \
94         .class_mask = 0,                                \
95         .func = (init_func)                             \
96     }
97
98 #define PCI_DEVICE_CLASS(vendor_id, device_id, class_code, init_func)   \
99     {                                                                   \
100         .vendid = (vendor_id),                                          \
101         .devid = (device_id),                                           \
102         .class = (class_code),                                          \
103         .class_mask = ~0,                                               \
104         .func = (init_func)                                             \
105     }
106
107 #define PCI_DEVICE_END                          \
108     {                                           \
109         .vendid = 0,                            \
110     }
111
112 int pci_init_device(const struct pci_device_id *table, u16 bdf, void *arg);
113 int pci_find_init_device(const struct pci_device_id *ids, void *arg);
114 void pci_reboot(void);
115
116 // helper functions to access pci mmio bars from real mode
117 u32 pci_readl(u32 addr);
118 void pci_writel(u32 addr, u32 val);
119
120 // pirtable.c
121 void create_pirtable(void);
122
123
124 /****************************************************************
125  * PIR table
126  ****************************************************************/
127
128 extern u16 PirOffset;
129
130 struct link_info {
131     u8 link;
132     u16 bitmap;
133 } PACKED;
134
135 struct pir_slot {
136     u8 bus;
137     u8 dev;
138     struct link_info links[4];
139     u8 slot_nr;
140     u8 reserved;
141 } PACKED;
142
143 struct pir_header {
144     u32 signature;
145     u16 version;
146     u16 size;
147     u8 router_bus;
148     u8 router_devfunc;
149     u16 exclusive_irqs;
150     u32 compatible_devid;
151     u32 miniport_data;
152     u8 reserved[11];
153     u8 checksum;
154     struct pir_slot slots[0];
155 } PACKED;
156
157 #define PIR_SIGNATURE 0x52495024 // $PIR
158
159
160 #endif