Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / pci.h
1 #ifndef __PCI_H
2 #define __PCI_H
3
4 #include "types.h" // u32
5 #include "list.h" // hlist_node
6
7 #define PORT_PCI_CMD           0x0cf8
8 #define PORT_PCI_REBOOT        0x0cf9
9 #define PORT_PCI_DATA          0x0cfc
10
11 #define PCI_ROM_SLOT 6
12 #define PCI_NUM_REGIONS 7
13 #define PCI_BRIDGE_NUM_REGIONS 2
14
15 enum pci_region_type {
16     PCI_REGION_TYPE_IO,
17     PCI_REGION_TYPE_MEM,
18     PCI_REGION_TYPE_PREFMEM,
19     PCI_REGION_TYPE_COUNT,
20 };
21
22 static inline u8 pci_bdf_to_bus(u16 bdf) {
23     return bdf >> 8;
24 }
25 static inline u8 pci_bdf_to_devfn(u16 bdf) {
26     return bdf & 0xff;
27 }
28 static inline u16 pci_bdf_to_busdev(u16 bdf) {
29     return bdf & ~0x07;
30 }
31 static inline u8 pci_bdf_to_dev(u16 bdf) {
32     return (bdf >> 3) & 0x1f;
33 }
34 static inline u8 pci_bdf_to_fn(u16 bdf) {
35     return bdf & 0x07;
36 }
37 static inline u16 pci_to_bdf(int bus, int dev, int fn) {
38     return (bus<<8) | (dev<<3) | fn;
39 }
40 static inline u16 pci_bus_devfn_to_bdf(int bus, u16 devfn) {
41     return (bus << 8) | devfn;
42 }
43
44 void pci_config_writel(u16 bdf, u32 addr, u32 val);
45 void pci_config_writew(u16 bdf, u32 addr, u16 val);
46 void pci_config_writeb(u16 bdf, u32 addr, u8 val);
47 u32 pci_config_readl(u16 bdf, u32 addr);
48 u16 pci_config_readw(u16 bdf, u32 addr);
49 u8 pci_config_readb(u16 bdf, u32 addr);
50 void pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on);
51
52 struct pci_device *pci_find_device(u16 vendid, u16 devid);
53 struct pci_device *pci_find_class(u16 classid);
54
55 struct pci_device {
56     u16 bdf;
57     u8 rootbus;
58     struct hlist_node node;
59     struct pci_device *parent;
60
61     // Configuration space device information
62     u16 vendor, device;
63     u16 class;
64     u8 prog_if, revision;
65     u8 header_type;
66     u8 secondary_bus;
67
68     // Local information on device.
69     int have_driver;
70 };
71 extern u64 pcimem_start, pcimem_end;
72 extern u64 pcimem64_start, pcimem64_end;
73 extern struct hlist_head PCIDevices;
74 extern int MaxPCIBus;
75 int pci_probe_host(void);
76 void pci_probe_devices(void);
77 static inline u32 pci_classprog(struct pci_device *pci) {
78     return (pci->class << 8) | pci->prog_if;
79 }
80
81 #define foreachpci(PCI)                                 \
82     hlist_for_each_entry(PCI, &PCIDevices, node)
83
84 int pci_next(int bdf, int bus);
85 #define foreachbdf(BDF, BUS)                                    \
86     for (BDF=pci_next(pci_bus_devfn_to_bdf((BUS), 0)-1, (BUS))  \
87          ; BDF >= 0                                             \
88          ; BDF=pci_next(BDF, (BUS)))
89
90 #define PCI_ANY_ID      (~0)
91 struct pci_device_id {
92     u32 vendid;
93     u32 devid;
94     u32 class;
95     u32 class_mask;
96     void (*func)(struct pci_device *pci, void *arg);
97 };
98
99 #define PCI_DEVICE(vendor_id, device_id, init_func)     \
100     {                                                   \
101         .vendid = (vendor_id),                          \
102         .devid = (device_id),                           \
103         .class = PCI_ANY_ID,                            \
104         .class_mask = 0,                                \
105         .func = (init_func)                             \
106     }
107
108 #define PCI_DEVICE_CLASS(vendor_id, device_id, class_code, init_func)   \
109     {                                                                   \
110         .vendid = (vendor_id),                                          \
111         .devid = (device_id),                                           \
112         .class = (class_code),                                          \
113         .class_mask = ~0,                                               \
114         .func = (init_func)                                             \
115     }
116
117 #define PCI_DEVICE_END                          \
118     {                                           \
119         .vendid = 0,                            \
120     }
121
122 int pci_init_device(const struct pci_device_id *ids
123                     , struct pci_device *pci, void *arg);
124 struct pci_device *pci_find_init_device(const struct pci_device_id *ids
125                                         , void *arg);
126 u8 pci_find_capability(struct pci_device *pci, u8 cap_id);
127 int pci_bridge_has_region(struct pci_device *pci,
128                           enum pci_region_type region_type);
129 void pci_reboot(void);
130
131 #endif