root / hw / pci.c @ 9bb33586
History | View | Annotate | Download (59 kB)
1 | 69b91039 | bellard | /*
|
---|---|---|---|
2 | 69b91039 | bellard | * QEMU PCI bus manager
|
3 | 69b91039 | bellard | *
|
4 | 69b91039 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 69b91039 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 69b91039 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 69b91039 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 69b91039 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 69b91039 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 69b91039 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 69b91039 | bellard | *
|
13 | 69b91039 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 69b91039 | bellard | * all copies or substantial portions of the Software.
|
15 | 69b91039 | bellard | *
|
16 | 69b91039 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 69b91039 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 69b91039 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 69b91039 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 69b91039 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 69b91039 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 69b91039 | bellard | * THE SOFTWARE.
|
23 | 69b91039 | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "pci.h" |
26 | 783753fd | Isaku Yamahata | #include "pci_bridge.h" |
27 | cfb0a50a | Isaku Yamahata | #include "pci_internals.h" |
28 | a5d1fd20 | Isaku Yamahata | #include "msix.h" |
29 | a5d1fd20 | Isaku Yamahata | #include "msi.h" |
30 | 376253ec | aliguori | #include "monitor.h" |
31 | 87ecb68b | pbrook | #include "net.h" |
32 | 880345c4 | aliguori | #include "sysemu.h" |
33 | c2039bd0 | Anthony Liguori | #include "loader.h" |
34 | 163c8a59 | Luiz Capitulino | #include "qemu-objects.h" |
35 | bf1b0071 | Blue Swirl | #include "range.h" |
36 | 69b91039 | bellard | |
37 | 69b91039 | bellard | //#define DEBUG_PCI
|
38 | d8d2e079 | Isaku Yamahata | #ifdef DEBUG_PCI
|
39 | 2e49d64a | Isaku Yamahata | # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
40 | d8d2e079 | Isaku Yamahata | #else
|
41 | d8d2e079 | Isaku Yamahata | # define PCI_DPRINTF(format, ...) do { } while (0) |
42 | d8d2e079 | Isaku Yamahata | #endif
|
43 | 69b91039 | bellard | |
44 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
45 | 4f43c1ff | Alex Williamson | static char *pcibus_get_dev_path(DeviceState *dev); |
46 | 9bb33586 | Isaku Yamahata | static int pcibus_reset(BusState *qbus); |
47 | 10c4c98a | Gerd Hoffmann | |
48 | cfb0a50a | Isaku Yamahata | struct BusInfo pci_bus_info = {
|
49 | 10c4c98a | Gerd Hoffmann | .name = "PCI",
|
50 | 10c4c98a | Gerd Hoffmann | .size = sizeof(PCIBus),
|
51 | 10c4c98a | Gerd Hoffmann | .print_dev = pcibus_dev_print, |
52 | 4f43c1ff | Alex Williamson | .get_dev_path = pcibus_get_dev_path, |
53 | 9bb33586 | Isaku Yamahata | .reset = pcibus_reset, |
54 | ee6847d1 | Gerd Hoffmann | .props = (Property[]) { |
55 | 54586bd1 | Gerd Hoffmann | DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
56 | 8c52c8f3 | Gerd Hoffmann | DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
|
57 | 88169ddf | Gerd Hoffmann | DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), |
58 | 49823868 | Isaku Yamahata | DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
|
59 | 49823868 | Isaku Yamahata | QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
|
60 | 54586bd1 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST() |
61 | ee6847d1 | Gerd Hoffmann | } |
62 | 30468f78 | bellard | }; |
63 | 69b91039 | bellard | |
64 | 1941d19c | bellard | static void pci_update_mappings(PCIDevice *d); |
65 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level); |
66 | ab85ceb1 | Stefan Weil | static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom); |
67 | 230741dc | Alex Williamson | static void pci_del_option_rom(PCIDevice *pdev); |
68 | 1941d19c | bellard | |
69 | d350d97d | aliguori | static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
70 | d350d97d | aliguori | static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
71 | e822a52a | Isaku Yamahata | |
72 | e822a52a | Isaku Yamahata | struct PCIHostBus {
|
73 | e822a52a | Isaku Yamahata | int domain;
|
74 | e822a52a | Isaku Yamahata | struct PCIBus *bus;
|
75 | e822a52a | Isaku Yamahata | QLIST_ENTRY(PCIHostBus) next; |
76 | e822a52a | Isaku Yamahata | }; |
77 | e822a52a | Isaku Yamahata | static QLIST_HEAD(, PCIHostBus) host_buses;
|
78 | 30468f78 | bellard | |
79 | 2d1e9f96 | Juan Quintela | static const VMStateDescription vmstate_pcibus = { |
80 | 2d1e9f96 | Juan Quintela | .name = "PCIBUS",
|
81 | 2d1e9f96 | Juan Quintela | .version_id = 1,
|
82 | 2d1e9f96 | Juan Quintela | .minimum_version_id = 1,
|
83 | 2d1e9f96 | Juan Quintela | .minimum_version_id_old = 1,
|
84 | 2d1e9f96 | Juan Quintela | .fields = (VMStateField []) { |
85 | 2d1e9f96 | Juan Quintela | VMSTATE_INT32_EQUAL(nirq, PCIBus), |
86 | c7bde572 | Juan Quintela | VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
87 | 2d1e9f96 | Juan Quintela | VMSTATE_END_OF_LIST() |
88 | 52fc1d83 | balrog | } |
89 | 2d1e9f96 | Juan Quintela | }; |
90 | 52fc1d83 | balrog | |
91 | b3b11697 | Isaku Yamahata | static int pci_bar(PCIDevice *d, int reg) |
92 | 5330de09 | Michael S. Tsirkin | { |
93 | b3b11697 | Isaku Yamahata | uint8_t type; |
94 | b3b11697 | Isaku Yamahata | |
95 | b3b11697 | Isaku Yamahata | if (reg != PCI_ROM_SLOT)
|
96 | b3b11697 | Isaku Yamahata | return PCI_BASE_ADDRESS_0 + reg * 4; |
97 | b3b11697 | Isaku Yamahata | |
98 | b3b11697 | Isaku Yamahata | type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
99 | b3b11697 | Isaku Yamahata | return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
100 | 5330de09 | Michael S. Tsirkin | } |
101 | 5330de09 | Michael S. Tsirkin | |
102 | d036bb21 | Michael S. Tsirkin | static inline int pci_irq_state(PCIDevice *d, int irq_num) |
103 | d036bb21 | Michael S. Tsirkin | { |
104 | d036bb21 | Michael S. Tsirkin | return (d->irq_state >> irq_num) & 0x1; |
105 | d036bb21 | Michael S. Tsirkin | } |
106 | d036bb21 | Michael S. Tsirkin | |
107 | d036bb21 | Michael S. Tsirkin | static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level) |
108 | d036bb21 | Michael S. Tsirkin | { |
109 | d036bb21 | Michael S. Tsirkin | d->irq_state &= ~(0x1 << irq_num);
|
110 | d036bb21 | Michael S. Tsirkin | d->irq_state |= level << irq_num; |
111 | d036bb21 | Michael S. Tsirkin | } |
112 | d036bb21 | Michael S. Tsirkin | |
113 | d036bb21 | Michael S. Tsirkin | static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change) |
114 | d036bb21 | Michael S. Tsirkin | { |
115 | d036bb21 | Michael S. Tsirkin | PCIBus *bus; |
116 | d036bb21 | Michael S. Tsirkin | for (;;) {
|
117 | d036bb21 | Michael S. Tsirkin | bus = pci_dev->bus; |
118 | d036bb21 | Michael S. Tsirkin | irq_num = bus->map_irq(pci_dev, irq_num); |
119 | d036bb21 | Michael S. Tsirkin | if (bus->set_irq)
|
120 | d036bb21 | Michael S. Tsirkin | break;
|
121 | d036bb21 | Michael S. Tsirkin | pci_dev = bus->parent_dev; |
122 | d036bb21 | Michael S. Tsirkin | } |
123 | d036bb21 | Michael S. Tsirkin | bus->irq_count[irq_num] += change; |
124 | d036bb21 | Michael S. Tsirkin | bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
125 | d036bb21 | Michael S. Tsirkin | } |
126 | d036bb21 | Michael S. Tsirkin | |
127 | f9bf77dd | Michael S. Tsirkin | /* Update interrupt status bit in config space on interrupt
|
128 | f9bf77dd | Michael S. Tsirkin | * state change. */
|
129 | f9bf77dd | Michael S. Tsirkin | static void pci_update_irq_status(PCIDevice *dev) |
130 | f9bf77dd | Michael S. Tsirkin | { |
131 | f9bf77dd | Michael S. Tsirkin | if (dev->irq_state) {
|
132 | f9bf77dd | Michael S. Tsirkin | dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT; |
133 | f9bf77dd | Michael S. Tsirkin | } else {
|
134 | f9bf77dd | Michael S. Tsirkin | dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
135 | f9bf77dd | Michael S. Tsirkin | } |
136 | f9bf77dd | Michael S. Tsirkin | } |
137 | f9bf77dd | Michael S. Tsirkin | |
138 | 5330de09 | Michael S. Tsirkin | static void pci_device_reset(PCIDevice *dev) |
139 | 5330de09 | Michael S. Tsirkin | { |
140 | c0b1905b | Michael S. Tsirkin | int r;
|
141 | 9bb33586 | Isaku Yamahata | /* TODO: call the below unconditionally once all pci devices
|
142 | 9bb33586 | Isaku Yamahata | * are qdevified */
|
143 | 9bb33586 | Isaku Yamahata | if (dev->qdev.info) {
|
144 | 9bb33586 | Isaku Yamahata | qdev_reset_all(&dev->qdev); |
145 | 9bb33586 | Isaku Yamahata | } |
146 | c0b1905b | Michael S. Tsirkin | |
147 | d036bb21 | Michael S. Tsirkin | dev->irq_state = 0;
|
148 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(dev); |
149 | 71ebd6dc | Isaku Yamahata | /* Clear all writeable bits */
|
150 | 99443c21 | Isaku Yamahata | pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, |
151 | f9aebe2e | Michael S. Tsirkin | pci_get_word(dev->wmask + PCI_COMMAND) | |
152 | f9aebe2e | Michael S. Tsirkin | pci_get_word(dev->w1cmask + PCI_COMMAND)); |
153 | 89d437df | Isaku Yamahata | pci_word_test_and_clear_mask(dev->config + PCI_STATUS, |
154 | 89d437df | Isaku Yamahata | pci_get_word(dev->wmask + PCI_STATUS) | |
155 | 89d437df | Isaku Yamahata | pci_get_word(dev->w1cmask + PCI_STATUS)); |
156 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
157 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
158 | c0b1905b | Michael S. Tsirkin | for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
159 | 71ebd6dc | Isaku Yamahata | PCIIORegion *region = &dev->io_regions[r]; |
160 | 71ebd6dc | Isaku Yamahata | if (!region->size) {
|
161 | c0b1905b | Michael S. Tsirkin | continue;
|
162 | c0b1905b | Michael S. Tsirkin | } |
163 | 71ebd6dc | Isaku Yamahata | |
164 | 71ebd6dc | Isaku Yamahata | if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
165 | 71ebd6dc | Isaku Yamahata | region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
166 | 71ebd6dc | Isaku Yamahata | pci_set_quad(dev->config + pci_bar(dev, r), region->type); |
167 | 71ebd6dc | Isaku Yamahata | } else {
|
168 | 71ebd6dc | Isaku Yamahata | pci_set_long(dev->config + pci_bar(dev, r), region->type); |
169 | 71ebd6dc | Isaku Yamahata | } |
170 | c0b1905b | Michael S. Tsirkin | } |
171 | c0b1905b | Michael S. Tsirkin | pci_update_mappings(dev); |
172 | 5330de09 | Michael S. Tsirkin | } |
173 | 5330de09 | Michael S. Tsirkin | |
174 | 9bb33586 | Isaku Yamahata | /*
|
175 | 9bb33586 | Isaku Yamahata | * Trigger pci bus reset under a given bus.
|
176 | 9bb33586 | Isaku Yamahata | * To be called on RST# assert.
|
177 | 9bb33586 | Isaku Yamahata | */
|
178 | 9bb33586 | Isaku Yamahata | void pci_bus_reset(PCIBus *bus)
|
179 | 6eaa6847 | Gleb Natapov | { |
180 | 6eaa6847 | Gleb Natapov | int i;
|
181 | 6eaa6847 | Gleb Natapov | |
182 | 6eaa6847 | Gleb Natapov | for (i = 0; i < bus->nirq; i++) { |
183 | 6eaa6847 | Gleb Natapov | bus->irq_count[i] = 0;
|
184 | 6eaa6847 | Gleb Natapov | } |
185 | 5330de09 | Michael S. Tsirkin | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
186 | 5330de09 | Michael S. Tsirkin | if (bus->devices[i]) {
|
187 | 5330de09 | Michael S. Tsirkin | pci_device_reset(bus->devices[i]); |
188 | 5330de09 | Michael S. Tsirkin | } |
189 | 6eaa6847 | Gleb Natapov | } |
190 | 6eaa6847 | Gleb Natapov | } |
191 | 6eaa6847 | Gleb Natapov | |
192 | 9bb33586 | Isaku Yamahata | static int pcibus_reset(BusState *qbus) |
193 | 9bb33586 | Isaku Yamahata | { |
194 | 9bb33586 | Isaku Yamahata | pci_bus_reset(DO_UPCAST(PCIBus, qbus, qbus)); |
195 | 9bb33586 | Isaku Yamahata | |
196 | 9bb33586 | Isaku Yamahata | /* topology traverse is done by pci_bus_reset().
|
197 | 9bb33586 | Isaku Yamahata | Tell qbus/qdev walker not to traverse the tree */
|
198 | 9bb33586 | Isaku Yamahata | return 1; |
199 | 9bb33586 | Isaku Yamahata | } |
200 | 9bb33586 | Isaku Yamahata | |
201 | e822a52a | Isaku Yamahata | static void pci_host_bus_register(int domain, PCIBus *bus) |
202 | e822a52a | Isaku Yamahata | { |
203 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
204 | e822a52a | Isaku Yamahata | host = qemu_mallocz(sizeof(*host));
|
205 | e822a52a | Isaku Yamahata | host->domain = domain; |
206 | e822a52a | Isaku Yamahata | host->bus = bus; |
207 | e822a52a | Isaku Yamahata | QLIST_INSERT_HEAD(&host_buses, host, next); |
208 | e822a52a | Isaku Yamahata | } |
209 | e822a52a | Isaku Yamahata | |
210 | c469e1dd | Isaku Yamahata | PCIBus *pci_find_root_bus(int domain)
|
211 | e822a52a | Isaku Yamahata | { |
212 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
213 | e822a52a | Isaku Yamahata | |
214 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
215 | e822a52a | Isaku Yamahata | if (host->domain == domain) {
|
216 | e822a52a | Isaku Yamahata | return host->bus;
|
217 | e822a52a | Isaku Yamahata | } |
218 | e822a52a | Isaku Yamahata | } |
219 | e822a52a | Isaku Yamahata | |
220 | e822a52a | Isaku Yamahata | return NULL; |
221 | e822a52a | Isaku Yamahata | } |
222 | e822a52a | Isaku Yamahata | |
223 | e075e788 | Isaku Yamahata | int pci_find_domain(const PCIBus *bus) |
224 | e075e788 | Isaku Yamahata | { |
225 | e075e788 | Isaku Yamahata | PCIDevice *d; |
226 | e075e788 | Isaku Yamahata | struct PCIHostBus *host;
|
227 | e075e788 | Isaku Yamahata | |
228 | e075e788 | Isaku Yamahata | /* obtain root bus */
|
229 | e075e788 | Isaku Yamahata | while ((d = bus->parent_dev) != NULL) { |
230 | e075e788 | Isaku Yamahata | bus = d->bus; |
231 | e075e788 | Isaku Yamahata | } |
232 | e075e788 | Isaku Yamahata | |
233 | e075e788 | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
234 | e075e788 | Isaku Yamahata | if (host->bus == bus) {
|
235 | e075e788 | Isaku Yamahata | return host->domain;
|
236 | e075e788 | Isaku Yamahata | } |
237 | e075e788 | Isaku Yamahata | } |
238 | e075e788 | Isaku Yamahata | |
239 | e075e788 | Isaku Yamahata | abort(); /* should not be reached */
|
240 | e075e788 | Isaku Yamahata | return -1; |
241 | e075e788 | Isaku Yamahata | } |
242 | e075e788 | Isaku Yamahata | |
243 | 21eea4b3 | Gerd Hoffmann | void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
244 | 21eea4b3 | Gerd Hoffmann | const char *name, int devfn_min) |
245 | 30468f78 | bellard | { |
246 | 21eea4b3 | Gerd Hoffmann | qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name); |
247 | 6fa84913 | Isaku Yamahata | assert(PCI_FUNC(devfn_min) == 0);
|
248 | 502a5395 | pbrook | bus->devfn_min = devfn_min; |
249 | e822a52a | Isaku Yamahata | |
250 | e822a52a | Isaku Yamahata | /* host bridge */
|
251 | e822a52a | Isaku Yamahata | QLIST_INIT(&bus->child); |
252 | e822a52a | Isaku Yamahata | pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
253 | e822a52a | Isaku Yamahata | |
254 | 0be71e32 | Alex Williamson | vmstate_register(NULL, -1, &vmstate_pcibus, bus); |
255 | 21eea4b3 | Gerd Hoffmann | } |
256 | 21eea4b3 | Gerd Hoffmann | |
257 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min) |
258 | 21eea4b3 | Gerd Hoffmann | { |
259 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
260 | 21eea4b3 | Gerd Hoffmann | |
261 | 21eea4b3 | Gerd Hoffmann | bus = qemu_mallocz(sizeof(*bus));
|
262 | 21eea4b3 | Gerd Hoffmann | bus->qbus.qdev_allocated = 1;
|
263 | 21eea4b3 | Gerd Hoffmann | pci_bus_new_inplace(bus, parent, name, devfn_min); |
264 | 21eea4b3 | Gerd Hoffmann | return bus;
|
265 | 21eea4b3 | Gerd Hoffmann | } |
266 | 21eea4b3 | Gerd Hoffmann | |
267 | 21eea4b3 | Gerd Hoffmann | void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
268 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int nirq) |
269 | 21eea4b3 | Gerd Hoffmann | { |
270 | 21eea4b3 | Gerd Hoffmann | bus->set_irq = set_irq; |
271 | 21eea4b3 | Gerd Hoffmann | bus->map_irq = map_irq; |
272 | 21eea4b3 | Gerd Hoffmann | bus->irq_opaque = irq_opaque; |
273 | 21eea4b3 | Gerd Hoffmann | bus->nirq = nirq; |
274 | 21eea4b3 | Gerd Hoffmann | bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0])); |
275 | 21eea4b3 | Gerd Hoffmann | } |
276 | 21eea4b3 | Gerd Hoffmann | |
277 | 87c30546 | Isaku Yamahata | void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
|
278 | ee995ffb | Gerd Hoffmann | { |
279 | ee995ffb | Gerd Hoffmann | bus->qbus.allow_hotplug = 1;
|
280 | ee995ffb | Gerd Hoffmann | bus->hotplug = hotplug; |
281 | 87c30546 | Isaku Yamahata | bus->hotplug_qdev = qdev; |
282 | ee995ffb | Gerd Hoffmann | } |
283 | ee995ffb | Gerd Hoffmann | |
284 | 2e01c8cf | Blue Swirl | void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
|
285 | 2e01c8cf | Blue Swirl | { |
286 | 2e01c8cf | Blue Swirl | bus->mem_base = base; |
287 | 2e01c8cf | Blue Swirl | } |
288 | 2e01c8cf | Blue Swirl | |
289 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
290 | 21eea4b3 | Gerd Hoffmann | pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
291 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int devfn_min, int nirq) |
292 | 21eea4b3 | Gerd Hoffmann | { |
293 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
294 | 21eea4b3 | Gerd Hoffmann | |
295 | 21eea4b3 | Gerd Hoffmann | bus = pci_bus_new(parent, name, devfn_min); |
296 | 21eea4b3 | Gerd Hoffmann | pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
297 | 30468f78 | bellard | return bus;
|
298 | 30468f78 | bellard | } |
299 | 69b91039 | bellard | |
300 | 502a5395 | pbrook | int pci_bus_num(PCIBus *s)
|
301 | 502a5395 | pbrook | { |
302 | e94ff650 | Isaku Yamahata | if (!s->parent_dev)
|
303 | e94ff650 | Isaku Yamahata | return 0; /* pci host bridge */ |
304 | e94ff650 | Isaku Yamahata | return s->parent_dev->config[PCI_SECONDARY_BUS];
|
305 | 502a5395 | pbrook | } |
306 | 502a5395 | pbrook | |
307 | 73534f2f | Juan Quintela | static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
308 | 30ca2aab | bellard | { |
309 | 73534f2f | Juan Quintela | PCIDevice *s = container_of(pv, PCIDevice, config); |
310 | a9f49946 | Isaku Yamahata | uint8_t *config; |
311 | 52fc1d83 | balrog | int i;
|
312 | 52fc1d83 | balrog | |
313 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(s)); |
314 | a9f49946 | Isaku Yamahata | config = qemu_malloc(size); |
315 | a9f49946 | Isaku Yamahata | |
316 | a9f49946 | Isaku Yamahata | qemu_get_buffer(f, config, size); |
317 | a9f49946 | Isaku Yamahata | for (i = 0; i < size; ++i) { |
318 | f9aebe2e | Michael S. Tsirkin | if ((config[i] ^ s->config[i]) &
|
319 | f9aebe2e | Michael S. Tsirkin | s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) { |
320 | a9f49946 | Isaku Yamahata | qemu_free(config); |
321 | bd4b65ee | Michael S. Tsirkin | return -EINVAL;
|
322 | a9f49946 | Isaku Yamahata | } |
323 | a9f49946 | Isaku Yamahata | } |
324 | a9f49946 | Isaku Yamahata | memcpy(s->config, config, size); |
325 | bd4b65ee | Michael S. Tsirkin | |
326 | 1941d19c | bellard | pci_update_mappings(s); |
327 | 52fc1d83 | balrog | |
328 | a9f49946 | Isaku Yamahata | qemu_free(config); |
329 | 30ca2aab | bellard | return 0; |
330 | 30ca2aab | bellard | } |
331 | 30ca2aab | bellard | |
332 | 73534f2f | Juan Quintela | /* just put buffer */
|
333 | 84e2e3eb | Juan Quintela | static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
334 | 73534f2f | Juan Quintela | { |
335 | dbe73d7f | Juan Quintela | const uint8_t **v = pv;
|
336 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
337 | dbe73d7f | Juan Quintela | qemu_put_buffer(f, *v, size); |
338 | 73534f2f | Juan Quintela | } |
339 | 73534f2f | Juan Quintela | |
340 | 73534f2f | Juan Quintela | static VMStateInfo vmstate_info_pci_config = {
|
341 | 73534f2f | Juan Quintela | .name = "pci config",
|
342 | 73534f2f | Juan Quintela | .get = get_pci_config_device, |
343 | 73534f2f | Juan Quintela | .put = put_pci_config_device, |
344 | 73534f2f | Juan Quintela | }; |
345 | 73534f2f | Juan Quintela | |
346 | d036bb21 | Michael S. Tsirkin | static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
347 | d036bb21 | Michael S. Tsirkin | { |
348 | c3f8f611 | Michael S. Tsirkin | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
349 | d036bb21 | Michael S. Tsirkin | uint32_t irq_state[PCI_NUM_PINS]; |
350 | d036bb21 | Michael S. Tsirkin | int i;
|
351 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
352 | d036bb21 | Michael S. Tsirkin | irq_state[i] = qemu_get_be32(f); |
353 | d036bb21 | Michael S. Tsirkin | if (irq_state[i] != 0x1 && irq_state[i] != 0) { |
354 | d036bb21 | Michael S. Tsirkin | fprintf(stderr, "irq state %d: must be 0 or 1.\n",
|
355 | d036bb21 | Michael S. Tsirkin | irq_state[i]); |
356 | d036bb21 | Michael S. Tsirkin | return -EINVAL;
|
357 | d036bb21 | Michael S. Tsirkin | } |
358 | d036bb21 | Michael S. Tsirkin | } |
359 | d036bb21 | Michael S. Tsirkin | |
360 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
361 | d036bb21 | Michael S. Tsirkin | pci_set_irq_state(s, i, irq_state[i]); |
362 | d036bb21 | Michael S. Tsirkin | } |
363 | d036bb21 | Michael S. Tsirkin | |
364 | d036bb21 | Michael S. Tsirkin | return 0; |
365 | d036bb21 | Michael S. Tsirkin | } |
366 | d036bb21 | Michael S. Tsirkin | |
367 | d036bb21 | Michael S. Tsirkin | static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
368 | d036bb21 | Michael S. Tsirkin | { |
369 | d036bb21 | Michael S. Tsirkin | int i;
|
370 | c3f8f611 | Michael S. Tsirkin | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
371 | d036bb21 | Michael S. Tsirkin | |
372 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
373 | d036bb21 | Michael S. Tsirkin | qemu_put_be32(f, pci_irq_state(s, i)); |
374 | d036bb21 | Michael S. Tsirkin | } |
375 | d036bb21 | Michael S. Tsirkin | } |
376 | d036bb21 | Michael S. Tsirkin | |
377 | d036bb21 | Michael S. Tsirkin | static VMStateInfo vmstate_info_pci_irq_state = {
|
378 | d036bb21 | Michael S. Tsirkin | .name = "pci irq state",
|
379 | d036bb21 | Michael S. Tsirkin | .get = get_pci_irq_state, |
380 | d036bb21 | Michael S. Tsirkin | .put = put_pci_irq_state, |
381 | d036bb21 | Michael S. Tsirkin | }; |
382 | d036bb21 | Michael S. Tsirkin | |
383 | 73534f2f | Juan Quintela | const VMStateDescription vmstate_pci_device = {
|
384 | 73534f2f | Juan Quintela | .name = "PCIDevice",
|
385 | 73534f2f | Juan Quintela | .version_id = 2,
|
386 | 73534f2f | Juan Quintela | .minimum_version_id = 1,
|
387 | 73534f2f | Juan Quintela | .minimum_version_id_old = 1,
|
388 | 73534f2f | Juan Quintela | .fields = (VMStateField []) { |
389 | 73534f2f | Juan Quintela | VMSTATE_INT32_LE(version_id, PCIDevice), |
390 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
391 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
392 | a9f49946 | Isaku Yamahata | PCI_CONFIG_SPACE_SIZE), |
393 | d036bb21 | Michael S. Tsirkin | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
394 | d036bb21 | Michael S. Tsirkin | vmstate_info_pci_irq_state, |
395 | d036bb21 | Michael S. Tsirkin | PCI_NUM_PINS * sizeof(int32_t)),
|
396 | a9f49946 | Isaku Yamahata | VMSTATE_END_OF_LIST() |
397 | a9f49946 | Isaku Yamahata | } |
398 | a9f49946 | Isaku Yamahata | }; |
399 | a9f49946 | Isaku Yamahata | |
400 | a9f49946 | Isaku Yamahata | const VMStateDescription vmstate_pcie_device = {
|
401 | a9f49946 | Isaku Yamahata | .name = "PCIDevice",
|
402 | a9f49946 | Isaku Yamahata | .version_id = 2,
|
403 | a9f49946 | Isaku Yamahata | .minimum_version_id = 1,
|
404 | a9f49946 | Isaku Yamahata | .minimum_version_id_old = 1,
|
405 | a9f49946 | Isaku Yamahata | .fields = (VMStateField []) { |
406 | a9f49946 | Isaku Yamahata | VMSTATE_INT32_LE(version_id, PCIDevice), |
407 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
408 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
409 | a9f49946 | Isaku Yamahata | PCIE_CONFIG_SPACE_SIZE), |
410 | d036bb21 | Michael S. Tsirkin | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
411 | d036bb21 | Michael S. Tsirkin | vmstate_info_pci_irq_state, |
412 | d036bb21 | Michael S. Tsirkin | PCI_NUM_PINS * sizeof(int32_t)),
|
413 | 73534f2f | Juan Quintela | VMSTATE_END_OF_LIST() |
414 | 73534f2f | Juan Quintela | } |
415 | 73534f2f | Juan Quintela | }; |
416 | 73534f2f | Juan Quintela | |
417 | a9f49946 | Isaku Yamahata | static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
418 | a9f49946 | Isaku Yamahata | { |
419 | a9f49946 | Isaku Yamahata | return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
420 | a9f49946 | Isaku Yamahata | } |
421 | a9f49946 | Isaku Yamahata | |
422 | 73534f2f | Juan Quintela | void pci_device_save(PCIDevice *s, QEMUFile *f)
|
423 | 73534f2f | Juan Quintela | { |
424 | f9bf77dd | Michael S. Tsirkin | /* Clear interrupt status bit: it is implicit
|
425 | f9bf77dd | Michael S. Tsirkin | * in irq_state which we are saving.
|
426 | f9bf77dd | Michael S. Tsirkin | * This makes us compatible with old devices
|
427 | f9bf77dd | Michael S. Tsirkin | * which never set or clear this bit. */
|
428 | f9bf77dd | Michael S. Tsirkin | s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
429 | a9f49946 | Isaku Yamahata | vmstate_save_state(f, pci_get_vmstate(s), s); |
430 | f9bf77dd | Michael S. Tsirkin | /* Restore the interrupt status bit. */
|
431 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(s); |
432 | 73534f2f | Juan Quintela | } |
433 | 73534f2f | Juan Quintela | |
434 | 73534f2f | Juan Quintela | int pci_device_load(PCIDevice *s, QEMUFile *f)
|
435 | 73534f2f | Juan Quintela | { |
436 | f9bf77dd | Michael S. Tsirkin | int ret;
|
437 | f9bf77dd | Michael S. Tsirkin | ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id); |
438 | f9bf77dd | Michael S. Tsirkin | /* Restore the interrupt status bit. */
|
439 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(s); |
440 | f9bf77dd | Michael S. Tsirkin | return ret;
|
441 | 73534f2f | Juan Quintela | } |
442 | 73534f2f | Juan Quintela | |
443 | 5e434f4e | Isaku Yamahata | static void pci_set_default_subsystem_id(PCIDevice *pci_dev) |
444 | d350d97d | aliguori | { |
445 | 5e434f4e | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
446 | 5e434f4e | Isaku Yamahata | pci_default_sub_vendor_id); |
447 | 5e434f4e | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, |
448 | 5e434f4e | Isaku Yamahata | pci_default_sub_device_id); |
449 | d350d97d | aliguori | } |
450 | d350d97d | aliguori | |
451 | 880345c4 | aliguori | /*
|
452 | 43c945f1 | Isaku Yamahata | * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
|
453 | 43c945f1 | Isaku Yamahata | * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
|
454 | 880345c4 | aliguori | */
|
455 | 43c945f1 | Isaku Yamahata | int pci_parse_devaddr(const char *addr, int *domp, int *busp, |
456 | 43c945f1 | Isaku Yamahata | unsigned int *slotp, unsigned int *funcp) |
457 | 880345c4 | aliguori | { |
458 | 880345c4 | aliguori | const char *p; |
459 | 880345c4 | aliguori | char *e;
|
460 | 880345c4 | aliguori | unsigned long val; |
461 | 880345c4 | aliguori | unsigned long dom = 0, bus = 0; |
462 | 43c945f1 | Isaku Yamahata | unsigned int slot = 0; |
463 | 43c945f1 | Isaku Yamahata | unsigned int func = 0; |
464 | 880345c4 | aliguori | |
465 | 880345c4 | aliguori | p = addr; |
466 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
467 | 880345c4 | aliguori | if (e == p)
|
468 | 880345c4 | aliguori | return -1; |
469 | 880345c4 | aliguori | if (*e == ':') { |
470 | 880345c4 | aliguori | bus = val; |
471 | 880345c4 | aliguori | p = e + 1;
|
472 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
473 | 880345c4 | aliguori | if (e == p)
|
474 | 880345c4 | aliguori | return -1; |
475 | 880345c4 | aliguori | if (*e == ':') { |
476 | 880345c4 | aliguori | dom = bus; |
477 | 880345c4 | aliguori | bus = val; |
478 | 880345c4 | aliguori | p = e + 1;
|
479 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
480 | 880345c4 | aliguori | if (e == p)
|
481 | 880345c4 | aliguori | return -1; |
482 | 880345c4 | aliguori | } |
483 | 880345c4 | aliguori | } |
484 | 880345c4 | aliguori | |
485 | 880345c4 | aliguori | slot = val; |
486 | 880345c4 | aliguori | |
487 | 43c945f1 | Isaku Yamahata | if (funcp != NULL) { |
488 | 43c945f1 | Isaku Yamahata | if (*e != '.') |
489 | 43c945f1 | Isaku Yamahata | return -1; |
490 | 43c945f1 | Isaku Yamahata | |
491 | 43c945f1 | Isaku Yamahata | p = e + 1;
|
492 | 43c945f1 | Isaku Yamahata | val = strtoul(p, &e, 16);
|
493 | 43c945f1 | Isaku Yamahata | if (e == p)
|
494 | 43c945f1 | Isaku Yamahata | return -1; |
495 | 43c945f1 | Isaku Yamahata | |
496 | 43c945f1 | Isaku Yamahata | func = val; |
497 | 43c945f1 | Isaku Yamahata | } |
498 | 43c945f1 | Isaku Yamahata | |
499 | 43c945f1 | Isaku Yamahata | /* if funcp == NULL func is 0 */
|
500 | 43c945f1 | Isaku Yamahata | if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) |
501 | 43c945f1 | Isaku Yamahata | return -1; |
502 | 43c945f1 | Isaku Yamahata | |
503 | 880345c4 | aliguori | if (*e)
|
504 | 880345c4 | aliguori | return -1; |
505 | 880345c4 | aliguori | |
506 | 880345c4 | aliguori | /* Note: QEMU doesn't implement domains other than 0 */
|
507 | c469e1dd | Isaku Yamahata | if (!pci_find_bus(pci_find_root_bus(dom), bus))
|
508 | 880345c4 | aliguori | return -1; |
509 | 880345c4 | aliguori | |
510 | 880345c4 | aliguori | *domp = dom; |
511 | 880345c4 | aliguori | *busp = bus; |
512 | 880345c4 | aliguori | *slotp = slot; |
513 | 43c945f1 | Isaku Yamahata | if (funcp != NULL) |
514 | 43c945f1 | Isaku Yamahata | *funcp = func; |
515 | 880345c4 | aliguori | return 0; |
516 | 880345c4 | aliguori | } |
517 | 880345c4 | aliguori | |
518 | e9283f8b | Jan Kiszka | int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp, |
519 | e9283f8b | Jan Kiszka | unsigned *slotp)
|
520 | 880345c4 | aliguori | { |
521 | e9283f8b | Jan Kiszka | /* strip legacy tag */
|
522 | e9283f8b | Jan Kiszka | if (!strncmp(addr, "pci_addr=", 9)) { |
523 | e9283f8b | Jan Kiszka | addr += 9;
|
524 | e9283f8b | Jan Kiszka | } |
525 | 43c945f1 | Isaku Yamahata | if (pci_parse_devaddr(addr, domp, busp, slotp, NULL)) { |
526 | e9283f8b | Jan Kiszka | monitor_printf(mon, "Invalid pci address\n");
|
527 | 880345c4 | aliguori | return -1; |
528 | e9283f8b | Jan Kiszka | } |
529 | e9283f8b | Jan Kiszka | return 0; |
530 | 880345c4 | aliguori | } |
531 | 880345c4 | aliguori | |
532 | 49bd1458 | Markus Armbruster | PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr) |
533 | 5607c388 | Markus Armbruster | { |
534 | 5607c388 | Markus Armbruster | int dom, bus;
|
535 | 5607c388 | Markus Armbruster | unsigned slot;
|
536 | 5607c388 | Markus Armbruster | |
537 | 5607c388 | Markus Armbruster | if (!devaddr) {
|
538 | 5607c388 | Markus Armbruster | *devfnp = -1;
|
539 | c469e1dd | Isaku Yamahata | return pci_find_bus(pci_find_root_bus(0), 0); |
540 | 5607c388 | Markus Armbruster | } |
541 | 5607c388 | Markus Armbruster | |
542 | 43c945f1 | Isaku Yamahata | if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) { |
543 | 5607c388 | Markus Armbruster | return NULL; |
544 | 5607c388 | Markus Armbruster | } |
545 | 5607c388 | Markus Armbruster | |
546 | 5607c388 | Markus Armbruster | *devfnp = slot << 3;
|
547 | e075e788 | Isaku Yamahata | return pci_find_bus(pci_find_root_bus(dom), bus);
|
548 | 5607c388 | Markus Armbruster | } |
549 | 5607c388 | Markus Armbruster | |
550 | bd4b65ee | Michael S. Tsirkin | static void pci_init_cmask(PCIDevice *dev) |
551 | bd4b65ee | Michael S. Tsirkin | { |
552 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
553 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
554 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
555 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_REVISION_ID] = 0xff;
|
556 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CLASS_PROG] = 0xff;
|
557 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
558 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
559 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
560 | bd4b65ee | Michael S. Tsirkin | } |
561 | bd4b65ee | Michael S. Tsirkin | |
562 | b7ee1603 | Michael S. Tsirkin | static void pci_init_wmask(PCIDevice *dev) |
563 | b7ee1603 | Michael S. Tsirkin | { |
564 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(dev);
|
565 | a9f49946 | Isaku Yamahata | |
566 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
567 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
568 | 67a51b48 | Isaku Yamahata | pci_set_word(dev->wmask + PCI_COMMAND, |
569 | a7b15a5c | Michael S. Tsirkin | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | |
570 | a7b15a5c | Michael S. Tsirkin | PCI_COMMAND_INTX_DISABLE); |
571 | 3e21ffc9 | Isaku Yamahata | |
572 | 3e21ffc9 | Isaku Yamahata | memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
|
573 | 3e21ffc9 | Isaku Yamahata | config_size - PCI_CONFIG_HEADER_SIZE); |
574 | b7ee1603 | Michael S. Tsirkin | } |
575 | b7ee1603 | Michael S. Tsirkin | |
576 | 89d437df | Isaku Yamahata | static void pci_init_w1cmask(PCIDevice *dev) |
577 | 89d437df | Isaku Yamahata | { |
578 | 89d437df | Isaku Yamahata | /*
|
579 | f6bdfcc9 | Michael S. Tsirkin | * Note: It's okay to set w1cmask even for readonly bits as
|
580 | 89d437df | Isaku Yamahata | * long as their value is hardwired to 0.
|
581 | 89d437df | Isaku Yamahata | */
|
582 | 89d437df | Isaku Yamahata | pci_set_word(dev->w1cmask + PCI_STATUS, |
583 | 89d437df | Isaku Yamahata | PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT | |
584 | 89d437df | Isaku Yamahata | PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT | |
585 | 89d437df | Isaku Yamahata | PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY); |
586 | 89d437df | Isaku Yamahata | } |
587 | 89d437df | Isaku Yamahata | |
588 | fb231628 | Isaku Yamahata | static void pci_init_wmask_bridge(PCIDevice *d) |
589 | fb231628 | Isaku Yamahata | { |
590 | fb231628 | Isaku Yamahata | /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
|
591 | fb231628 | Isaku Yamahata | PCI_SEC_LETENCY_TIMER */
|
592 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); |
593 | fb231628 | Isaku Yamahata | |
594 | fb231628 | Isaku Yamahata | /* base and limit */
|
595 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
|
596 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
|
597 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_BASE, |
598 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
599 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_LIMIT, |
600 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
601 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, |
602 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
603 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, |
604 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
605 | fb231628 | Isaku Yamahata | |
606 | fb231628 | Isaku Yamahata | /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
|
607 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); |
608 | fb231628 | Isaku Yamahata | |
609 | f6bdfcc9 | Michael S. Tsirkin | /* TODO: add this define to pci_regs.h in linux and then in qemu. */
|
610 | f6bdfcc9 | Michael S. Tsirkin | #define PCI_BRIDGE_CTL_VGA_16BIT 0x10 /* VGA 16-bit decode */ |
611 | f6bdfcc9 | Michael S. Tsirkin | #define PCI_BRIDGE_CTL_DISCARD 0x100 /* Primary discard timer */ |
612 | f6bdfcc9 | Michael S. Tsirkin | #define PCI_BRIDGE_CTL_SEC_DISCARD 0x200 /* Secondary discard timer */ |
613 | f6bdfcc9 | Michael S. Tsirkin | #define PCI_BRIDGE_CTL_DISCARD_STATUS 0x400 /* Discard timer status */ |
614 | f6bdfcc9 | Michael S. Tsirkin | #define PCI_BRIDGE_CTL_DISCARD_SERR 0x800 /* Discard timer SERR# enable */ |
615 | f6bdfcc9 | Michael S. Tsirkin | pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, |
616 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_PARITY | |
617 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_SERR | |
618 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_ISA | |
619 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_VGA | |
620 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_VGA_16BIT | |
621 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_MASTER_ABORT | |
622 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_BUS_RESET | |
623 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_FAST_BACK | |
624 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD | |
625 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_SEC_DISCARD | |
626 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD_STATUS | |
627 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD_SERR); |
628 | f6bdfcc9 | Michael S. Tsirkin | /* Below does not do anything as we never set this bit, put here for
|
629 | f6bdfcc9 | Michael S. Tsirkin | * completeness. */
|
630 | f6bdfcc9 | Michael S. Tsirkin | pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL, |
631 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD_STATUS); |
632 | fb231628 | Isaku Yamahata | } |
633 | fb231628 | Isaku Yamahata | |
634 | 6eab3de1 | Isaku Yamahata | static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) |
635 | 6eab3de1 | Isaku Yamahata | { |
636 | 6eab3de1 | Isaku Yamahata | uint8_t slot = PCI_SLOT(dev->devfn); |
637 | 6eab3de1 | Isaku Yamahata | uint8_t func; |
638 | 6eab3de1 | Isaku Yamahata | |
639 | 6eab3de1 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
640 | 6eab3de1 | Isaku Yamahata | dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; |
641 | 6eab3de1 | Isaku Yamahata | } |
642 | 6eab3de1 | Isaku Yamahata | |
643 | 6eab3de1 | Isaku Yamahata | /*
|
644 | b0cd712c | Stefan Weil | * multifunction bit is interpreted in two ways as follows.
|
645 | 6eab3de1 | Isaku Yamahata | * - all functions must set the bit to 1.
|
646 | 6eab3de1 | Isaku Yamahata | * Example: Intel X53
|
647 | 6eab3de1 | Isaku Yamahata | * - function 0 must set the bit, but the rest function (> 0)
|
648 | 6eab3de1 | Isaku Yamahata | * is allowed to leave the bit to 0.
|
649 | 6eab3de1 | Isaku Yamahata | * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
|
650 | 6eab3de1 | Isaku Yamahata | *
|
651 | 6eab3de1 | Isaku Yamahata | * So OS (at least Linux) checks the bit of only function 0,
|
652 | 6eab3de1 | Isaku Yamahata | * and doesn't see the bit of function > 0.
|
653 | 6eab3de1 | Isaku Yamahata | *
|
654 | 6eab3de1 | Isaku Yamahata | * The below check allows both interpretation.
|
655 | 6eab3de1 | Isaku Yamahata | */
|
656 | 6eab3de1 | Isaku Yamahata | if (PCI_FUNC(dev->devfn)) {
|
657 | 6eab3de1 | Isaku Yamahata | PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
|
658 | 6eab3de1 | Isaku Yamahata | if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
|
659 | 6eab3de1 | Isaku Yamahata | /* function 0 should set multifunction bit */
|
660 | 6eab3de1 | Isaku Yamahata | error_report("PCI: single function device can't be populated "
|
661 | 6eab3de1 | Isaku Yamahata | "in function %x.%x", slot, PCI_FUNC(dev->devfn));
|
662 | 6eab3de1 | Isaku Yamahata | return -1; |
663 | 6eab3de1 | Isaku Yamahata | } |
664 | 6eab3de1 | Isaku Yamahata | return 0; |
665 | 6eab3de1 | Isaku Yamahata | } |
666 | 6eab3de1 | Isaku Yamahata | |
667 | 6eab3de1 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
668 | 6eab3de1 | Isaku Yamahata | return 0; |
669 | 6eab3de1 | Isaku Yamahata | } |
670 | 6eab3de1 | Isaku Yamahata | /* function 0 indicates single function, so function > 0 must be NULL */
|
671 | 6eab3de1 | Isaku Yamahata | for (func = 1; func < PCI_FUNC_MAX; ++func) { |
672 | 6eab3de1 | Isaku Yamahata | if (bus->devices[PCI_DEVFN(slot, func)]) {
|
673 | 6eab3de1 | Isaku Yamahata | error_report("PCI: %x.0 indicates single function, "
|
674 | 6eab3de1 | Isaku Yamahata | "but %x.%x is already populated.",
|
675 | 6eab3de1 | Isaku Yamahata | slot, slot, func); |
676 | 6eab3de1 | Isaku Yamahata | return -1; |
677 | 6eab3de1 | Isaku Yamahata | } |
678 | 6eab3de1 | Isaku Yamahata | } |
679 | 6eab3de1 | Isaku Yamahata | return 0; |
680 | 6eab3de1 | Isaku Yamahata | } |
681 | 6eab3de1 | Isaku Yamahata | |
682 | a9f49946 | Isaku Yamahata | static void pci_config_alloc(PCIDevice *pci_dev) |
683 | a9f49946 | Isaku Yamahata | { |
684 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pci_dev);
|
685 | a9f49946 | Isaku Yamahata | |
686 | a9f49946 | Isaku Yamahata | pci_dev->config = qemu_mallocz(config_size); |
687 | a9f49946 | Isaku Yamahata | pci_dev->cmask = qemu_mallocz(config_size); |
688 | a9f49946 | Isaku Yamahata | pci_dev->wmask = qemu_mallocz(config_size); |
689 | 92ba5f51 | Isaku Yamahata | pci_dev->w1cmask = qemu_mallocz(config_size); |
690 | a9f49946 | Isaku Yamahata | pci_dev->used = qemu_mallocz(config_size); |
691 | a9f49946 | Isaku Yamahata | } |
692 | a9f49946 | Isaku Yamahata | |
693 | a9f49946 | Isaku Yamahata | static void pci_config_free(PCIDevice *pci_dev) |
694 | a9f49946 | Isaku Yamahata | { |
695 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->config); |
696 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->cmask); |
697 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->wmask); |
698 | 92ba5f51 | Isaku Yamahata | qemu_free(pci_dev->w1cmask); |
699 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->used); |
700 | a9f49946 | Isaku Yamahata | } |
701 | a9f49946 | Isaku Yamahata | |
702 | 69b91039 | bellard | /* -1 for devfn means auto assign */
|
703 | 6b1b92d3 | Paul Brook | static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
704 | 6b1b92d3 | Paul Brook | const char *name, int devfn, |
705 | 6b1b92d3 | Paul Brook | PCIConfigReadFunc *config_read, |
706 | fb231628 | Isaku Yamahata | PCIConfigWriteFunc *config_write, |
707 | e327e323 | Isaku Yamahata | bool is_bridge)
|
708 | 69b91039 | bellard | { |
709 | 69b91039 | bellard | if (devfn < 0) { |
710 | b47b0706 | Isaku Yamahata | for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
|
711 | 6fa84913 | Isaku Yamahata | devfn += PCI_FUNC_MAX) { |
712 | 30468f78 | bellard | if (!bus->devices[devfn])
|
713 | 69b91039 | bellard | goto found;
|
714 | 69b91039 | bellard | } |
715 | 3709c1b7 | Daniel P. Berrange | error_report("PCI: no slot/function available for %s, all in use", name);
|
716 | 09e3acc6 | Gerd Hoffmann | return NULL; |
717 | 69b91039 | bellard | found: ;
|
718 | 07b7d053 | Markus Armbruster | } else if (bus->devices[devfn]) { |
719 | 3709c1b7 | Daniel P. Berrange | error_report("PCI: slot %d function %d not available for %s, in use by %s",
|
720 | 3709c1b7 | Daniel P. Berrange | PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name); |
721 | 09e3acc6 | Gerd Hoffmann | return NULL; |
722 | 69b91039 | bellard | } |
723 | 30468f78 | bellard | pci_dev->bus = bus; |
724 | 69b91039 | bellard | pci_dev->devfn = devfn; |
725 | 69b91039 | bellard | pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
726 | d036bb21 | Michael S. Tsirkin | pci_dev->irq_state = 0;
|
727 | a9f49946 | Isaku Yamahata | pci_config_alloc(pci_dev); |
728 | fb231628 | Isaku Yamahata | |
729 | e327e323 | Isaku Yamahata | if (!is_bridge) {
|
730 | fb231628 | Isaku Yamahata | pci_set_default_subsystem_id(pci_dev); |
731 | fb231628 | Isaku Yamahata | } |
732 | bd4b65ee | Michael S. Tsirkin | pci_init_cmask(pci_dev); |
733 | b7ee1603 | Michael S. Tsirkin | pci_init_wmask(pci_dev); |
734 | 89d437df | Isaku Yamahata | pci_init_w1cmask(pci_dev); |
735 | e327e323 | Isaku Yamahata | if (is_bridge) {
|
736 | fb231628 | Isaku Yamahata | pci_init_wmask_bridge(pci_dev); |
737 | fb231628 | Isaku Yamahata | } |
738 | 6eab3de1 | Isaku Yamahata | if (pci_init_multifunction(bus, pci_dev)) {
|
739 | 6eab3de1 | Isaku Yamahata | pci_config_free(pci_dev); |
740 | 6eab3de1 | Isaku Yamahata | return NULL; |
741 | 6eab3de1 | Isaku Yamahata | } |
742 | 0ac32c83 | bellard | |
743 | 0ac32c83 | bellard | if (!config_read)
|
744 | 0ac32c83 | bellard | config_read = pci_default_read_config; |
745 | 0ac32c83 | bellard | if (!config_write)
|
746 | 0ac32c83 | bellard | config_write = pci_default_write_config; |
747 | 69b91039 | bellard | pci_dev->config_read = config_read; |
748 | 69b91039 | bellard | pci_dev->config_write = config_write; |
749 | 30468f78 | bellard | bus->devices[devfn] = pci_dev; |
750 | e369cad7 | Isaku Yamahata | pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
751 | f16c4abf | Juan Quintela | pci_dev->version_id = 2; /* Current pci device vmstate version */ |
752 | 69b91039 | bellard | return pci_dev;
|
753 | 69b91039 | bellard | } |
754 | 69b91039 | bellard | |
755 | 925fe64a | Alex Williamson | static void do_pci_unregister_device(PCIDevice *pci_dev) |
756 | 925fe64a | Alex Williamson | { |
757 | 925fe64a | Alex Williamson | qemu_free_irqs(pci_dev->irq); |
758 | 925fe64a | Alex Williamson | pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
759 | 925fe64a | Alex Williamson | pci_config_free(pci_dev); |
760 | 925fe64a | Alex Williamson | } |
761 | 925fe64a | Alex Williamson | |
762 | 6b1b92d3 | Paul Brook | PCIDevice *pci_register_device(PCIBus *bus, const char *name, |
763 | 6b1b92d3 | Paul Brook | int instance_size, int devfn, |
764 | 6b1b92d3 | Paul Brook | PCIConfigReadFunc *config_read, |
765 | 6b1b92d3 | Paul Brook | PCIConfigWriteFunc *config_write) |
766 | 6b1b92d3 | Paul Brook | { |
767 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev; |
768 | 6b1b92d3 | Paul Brook | |
769 | 6b1b92d3 | Paul Brook | pci_dev = qemu_mallocz(instance_size); |
770 | 6b1b92d3 | Paul Brook | pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, |
771 | fb231628 | Isaku Yamahata | config_read, config_write, |
772 | fb231628 | Isaku Yamahata | PCI_HEADER_TYPE_NORMAL); |
773 | 09e3acc6 | Gerd Hoffmann | if (pci_dev == NULL) { |
774 | 09e3acc6 | Gerd Hoffmann | hw_error("PCI: can't register device\n");
|
775 | 09e3acc6 | Gerd Hoffmann | } |
776 | 6b1b92d3 | Paul Brook | return pci_dev;
|
777 | 6b1b92d3 | Paul Brook | } |
778 | 2e01c8cf | Blue Swirl | |
779 | 2e01c8cf | Blue Swirl | static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
|
780 | 2e01c8cf | Blue Swirl | target_phys_addr_t addr) |
781 | 5851e08c | aliguori | { |
782 | 2e01c8cf | Blue Swirl | return addr + bus->mem_base;
|
783 | 5851e08c | aliguori | } |
784 | 5851e08c | aliguori | |
785 | 5851e08c | aliguori | static void pci_unregister_io_regions(PCIDevice *pci_dev) |
786 | 5851e08c | aliguori | { |
787 | 5851e08c | aliguori | PCIIORegion *r; |
788 | 5851e08c | aliguori | int i;
|
789 | 5851e08c | aliguori | |
790 | 5851e08c | aliguori | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
791 | 5851e08c | aliguori | r = &pci_dev->io_regions[i]; |
792 | 182f9c8a | Isaku Yamahata | if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
793 | 5851e08c | aliguori | continue;
|
794 | 0392a017 | Isaku Yamahata | if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
|
795 | a0c7a97e | Isaku Yamahata | isa_unassign_ioport(r->addr, r->filtered_size); |
796 | 5851e08c | aliguori | } else {
|
797 | 2e01c8cf | Blue Swirl | cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus, |
798 | 2e01c8cf | Blue Swirl | r->addr), |
799 | 2e01c8cf | Blue Swirl | r->filtered_size, |
800 | 2e01c8cf | Blue Swirl | IO_MEM_UNASSIGNED); |
801 | 5851e08c | aliguori | } |
802 | 5851e08c | aliguori | } |
803 | 5851e08c | aliguori | } |
804 | 5851e08c | aliguori | |
805 | a36a344d | Gerd Hoffmann | static int pci_unregister_device(DeviceState *dev) |
806 | 5851e08c | aliguori | { |
807 | a36a344d | Gerd Hoffmann | PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev); |
808 | e3936fa5 | Gerd Hoffmann | PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info); |
809 | 5851e08c | aliguori | int ret = 0; |
810 | 5851e08c | aliguori | |
811 | e3936fa5 | Gerd Hoffmann | if (info->exit)
|
812 | e3936fa5 | Gerd Hoffmann | ret = info->exit(pci_dev); |
813 | 5851e08c | aliguori | if (ret)
|
814 | 5851e08c | aliguori | return ret;
|
815 | 5851e08c | aliguori | |
816 | 5851e08c | aliguori | pci_unregister_io_regions(pci_dev); |
817 | 230741dc | Alex Williamson | pci_del_option_rom(pci_dev); |
818 | 925fe64a | Alex Williamson | do_pci_unregister_device(pci_dev); |
819 | 5851e08c | aliguori | return 0; |
820 | 5851e08c | aliguori | } |
821 | 5851e08c | aliguori | |
822 | 28c2c264 | Avi Kivity | void pci_register_bar(PCIDevice *pci_dev, int region_num, |
823 | 0bb750ef | Isaku Yamahata | pcibus_t size, uint8_t type, |
824 | 69b91039 | bellard | PCIMapIORegionFunc *map_func) |
825 | 69b91039 | bellard | { |
826 | 69b91039 | bellard | PCIIORegion *r; |
827 | d7ce493a | pbrook | uint32_t addr; |
828 | 5a9ff381 | Isaku Yamahata | uint64_t wmask; |
829 | a4c20c6a | aliguori | |
830 | 2bbb9c2f | Isaku Yamahata | assert(region_num >= 0);
|
831 | 2bbb9c2f | Isaku Yamahata | assert(region_num < PCI_NUM_REGIONS); |
832 | a4c20c6a | aliguori | if (size & (size-1)) { |
833 | a4c20c6a | aliguori | fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
834 | 89e8b13c | Isaku Yamahata | "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
835 | a4c20c6a | aliguori | exit(1);
|
836 | a4c20c6a | aliguori | } |
837 | a4c20c6a | aliguori | |
838 | 69b91039 | bellard | r = &pci_dev->io_regions[region_num]; |
839 | 182f9c8a | Isaku Yamahata | r->addr = PCI_BAR_UNMAPPED; |
840 | 69b91039 | bellard | r->size = size; |
841 | a0c7a97e | Isaku Yamahata | r->filtered_size = size; |
842 | 69b91039 | bellard | r->type = type; |
843 | 69b91039 | bellard | r->map_func = map_func; |
844 | b7ee1603 | Michael S. Tsirkin | |
845 | b7ee1603 | Michael S. Tsirkin | wmask = ~(size - 1);
|
846 | b3b11697 | Isaku Yamahata | addr = pci_bar(pci_dev, region_num); |
847 | d7ce493a | pbrook | if (region_num == PCI_ROM_SLOT) {
|
848 | b7ee1603 | Michael S. Tsirkin | /* ROM enable bit is writeable */
|
849 | 5330de09 | Michael S. Tsirkin | wmask |= PCI_ROM_ADDRESS_ENABLE; |
850 | d7ce493a | pbrook | } |
851 | b0ff8eb2 | Isaku Yamahata | pci_set_long(pci_dev->config + addr, type); |
852 | 14421258 | Isaku Yamahata | if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
853 | 14421258 | Isaku Yamahata | r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
854 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->wmask + addr, wmask); |
855 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
856 | 14421258 | Isaku Yamahata | } else {
|
857 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
858 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
859 | 14421258 | Isaku Yamahata | } |
860 | 69b91039 | bellard | } |
861 | 69b91039 | bellard | |
862 | a0c7a97e | Isaku Yamahata | static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size, |
863 | a0c7a97e | Isaku Yamahata | uint8_t type) |
864 | a0c7a97e | Isaku Yamahata | { |
865 | a0c7a97e | Isaku Yamahata | pcibus_t base = *addr; |
866 | a0c7a97e | Isaku Yamahata | pcibus_t limit = *addr + *size - 1;
|
867 | a0c7a97e | Isaku Yamahata | PCIDevice *br; |
868 | a0c7a97e | Isaku Yamahata | |
869 | a0c7a97e | Isaku Yamahata | for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
|
870 | a0c7a97e | Isaku Yamahata | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
871 | a0c7a97e | Isaku Yamahata | |
872 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
873 | a0c7a97e | Isaku Yamahata | if (!(cmd & PCI_COMMAND_IO)) {
|
874 | a0c7a97e | Isaku Yamahata | goto no_map;
|
875 | a0c7a97e | Isaku Yamahata | } |
876 | a0c7a97e | Isaku Yamahata | } else {
|
877 | a0c7a97e | Isaku Yamahata | if (!(cmd & PCI_COMMAND_MEMORY)) {
|
878 | a0c7a97e | Isaku Yamahata | goto no_map;
|
879 | a0c7a97e | Isaku Yamahata | } |
880 | a0c7a97e | Isaku Yamahata | } |
881 | a0c7a97e | Isaku Yamahata | |
882 | a0c7a97e | Isaku Yamahata | base = MAX(base, pci_bridge_get_base(br, type)); |
883 | a0c7a97e | Isaku Yamahata | limit = MIN(limit, pci_bridge_get_limit(br, type)); |
884 | a0c7a97e | Isaku Yamahata | } |
885 | a0c7a97e | Isaku Yamahata | |
886 | a0c7a97e | Isaku Yamahata | if (base > limit) {
|
887 | 88a95564 | Michael S. Tsirkin | goto no_map;
|
888 | a0c7a97e | Isaku Yamahata | } |
889 | 88a95564 | Michael S. Tsirkin | *addr = base; |
890 | 88a95564 | Michael S. Tsirkin | *size = limit - base + 1;
|
891 | 88a95564 | Michael S. Tsirkin | return;
|
892 | 88a95564 | Michael S. Tsirkin | no_map:
|
893 | 88a95564 | Michael S. Tsirkin | *addr = PCI_BAR_UNMAPPED; |
894 | 88a95564 | Michael S. Tsirkin | *size = 0;
|
895 | a0c7a97e | Isaku Yamahata | } |
896 | a0c7a97e | Isaku Yamahata | |
897 | 876a350d | Michael S. Tsirkin | static pcibus_t pci_bar_address(PCIDevice *d,
|
898 | 876a350d | Michael S. Tsirkin | int reg, uint8_t type, pcibus_t size)
|
899 | 876a350d | Michael S. Tsirkin | { |
900 | 876a350d | Michael S. Tsirkin | pcibus_t new_addr, last_addr; |
901 | 876a350d | Michael S. Tsirkin | int bar = pci_bar(d, reg);
|
902 | 876a350d | Michael S. Tsirkin | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
903 | 876a350d | Michael S. Tsirkin | |
904 | 876a350d | Michael S. Tsirkin | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
905 | 876a350d | Michael S. Tsirkin | if (!(cmd & PCI_COMMAND_IO)) {
|
906 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
907 | 876a350d | Michael S. Tsirkin | } |
908 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_long(d->config + bar) & ~(size - 1);
|
909 | 876a350d | Michael S. Tsirkin | last_addr = new_addr + size - 1;
|
910 | 876a350d | Michael S. Tsirkin | /* NOTE: we have only 64K ioports on PC */
|
911 | 876a350d | Michael S. Tsirkin | if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) { |
912 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
913 | 876a350d | Michael S. Tsirkin | } |
914 | 876a350d | Michael S. Tsirkin | return new_addr;
|
915 | 876a350d | Michael S. Tsirkin | } |
916 | 876a350d | Michael S. Tsirkin | |
917 | 876a350d | Michael S. Tsirkin | if (!(cmd & PCI_COMMAND_MEMORY)) {
|
918 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
919 | 876a350d | Michael S. Tsirkin | } |
920 | 876a350d | Michael S. Tsirkin | if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
921 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_quad(d->config + bar); |
922 | 876a350d | Michael S. Tsirkin | } else {
|
923 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_long(d->config + bar); |
924 | 876a350d | Michael S. Tsirkin | } |
925 | 876a350d | Michael S. Tsirkin | /* the ROM slot has a specific enable bit */
|
926 | 876a350d | Michael S. Tsirkin | if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
|
927 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
928 | 876a350d | Michael S. Tsirkin | } |
929 | 876a350d | Michael S. Tsirkin | new_addr &= ~(size - 1);
|
930 | 876a350d | Michael S. Tsirkin | last_addr = new_addr + size - 1;
|
931 | 876a350d | Michael S. Tsirkin | /* NOTE: we do not support wrapping */
|
932 | 876a350d | Michael S. Tsirkin | /* XXX: as we cannot support really dynamic
|
933 | 876a350d | Michael S. Tsirkin | mappings, we handle specific values as invalid
|
934 | 876a350d | Michael S. Tsirkin | mappings. */
|
935 | 876a350d | Michael S. Tsirkin | if (last_addr <= new_addr || new_addr == 0 || |
936 | 876a350d | Michael S. Tsirkin | last_addr == PCI_BAR_UNMAPPED) { |
937 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
938 | 876a350d | Michael S. Tsirkin | } |
939 | 876a350d | Michael S. Tsirkin | |
940 | 876a350d | Michael S. Tsirkin | /* Now pcibus_t is 64bit.
|
941 | 876a350d | Michael S. Tsirkin | * Check if 32 bit BAR wraps around explicitly.
|
942 | 876a350d | Michael S. Tsirkin | * Without this, PC ide doesn't work well.
|
943 | 876a350d | Michael S. Tsirkin | * TODO: remove this work around.
|
944 | 876a350d | Michael S. Tsirkin | */
|
945 | 876a350d | Michael S. Tsirkin | if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
|
946 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
947 | 876a350d | Michael S. Tsirkin | } |
948 | 876a350d | Michael S. Tsirkin | |
949 | 876a350d | Michael S. Tsirkin | /*
|
950 | 876a350d | Michael S. Tsirkin | * OS is allowed to set BAR beyond its addressable
|
951 | 876a350d | Michael S. Tsirkin | * bits. For example, 32 bit OS can set 64bit bar
|
952 | 876a350d | Michael S. Tsirkin | * to >4G. Check it. TODO: we might need to support
|
953 | 876a350d | Michael S. Tsirkin | * it in the future for e.g. PAE.
|
954 | 876a350d | Michael S. Tsirkin | */
|
955 | 876a350d | Michael S. Tsirkin | if (last_addr >= TARGET_PHYS_ADDR_MAX) {
|
956 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
957 | 876a350d | Michael S. Tsirkin | } |
958 | 876a350d | Michael S. Tsirkin | |
959 | 876a350d | Michael S. Tsirkin | return new_addr;
|
960 | 876a350d | Michael S. Tsirkin | } |
961 | 876a350d | Michael S. Tsirkin | |
962 | 0ac32c83 | bellard | static void pci_update_mappings(PCIDevice *d) |
963 | 0ac32c83 | bellard | { |
964 | 0ac32c83 | bellard | PCIIORegion *r; |
965 | 876a350d | Michael S. Tsirkin | int i;
|
966 | c71b5b4a | Blue Swirl | pcibus_t new_addr, filtered_size; |
967 | 3b46e624 | ths | |
968 | 8a8696a3 | bellard | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
969 | 0ac32c83 | bellard | r = &d->io_regions[i]; |
970 | a9688570 | Isaku Yamahata | |
971 | a9688570 | Isaku Yamahata | /* this region isn't registered */
|
972 | ec503442 | Isaku Yamahata | if (!r->size)
|
973 | a9688570 | Isaku Yamahata | continue;
|
974 | a9688570 | Isaku Yamahata | |
975 | 876a350d | Michael S. Tsirkin | new_addr = pci_bar_address(d, i, r->type, r->size); |
976 | a9688570 | Isaku Yamahata | |
977 | a0c7a97e | Isaku Yamahata | /* bridge filtering */
|
978 | a0c7a97e | Isaku Yamahata | filtered_size = r->size; |
979 | a0c7a97e | Isaku Yamahata | if (new_addr != PCI_BAR_UNMAPPED) {
|
980 | a0c7a97e | Isaku Yamahata | pci_bridge_filter(d, &new_addr, &filtered_size, r->type); |
981 | a0c7a97e | Isaku Yamahata | } |
982 | a0c7a97e | Isaku Yamahata | |
983 | a9688570 | Isaku Yamahata | /* This bar isn't changed */
|
984 | a0c7a97e | Isaku Yamahata | if (new_addr == r->addr && filtered_size == r->filtered_size)
|
985 | a9688570 | Isaku Yamahata | continue;
|
986 | a9688570 | Isaku Yamahata | |
987 | a9688570 | Isaku Yamahata | /* now do the real mapping */
|
988 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
989 | a9688570 | Isaku Yamahata | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
990 | a9688570 | Isaku Yamahata | int class;
|
991 | a9688570 | Isaku Yamahata | /* NOTE: specific hack for IDE in PC case:
|
992 | a9688570 | Isaku Yamahata | only one byte must be mapped. */
|
993 | a9688570 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
994 | a9688570 | Isaku Yamahata | if (class == 0x0101 && r->size == 4) { |
995 | a9688570 | Isaku Yamahata | isa_unassign_ioport(r->addr + 2, 1); |
996 | a9688570 | Isaku Yamahata | } else {
|
997 | a0c7a97e | Isaku Yamahata | isa_unassign_ioport(r->addr, r->filtered_size); |
998 | 0ac32c83 | bellard | } |
999 | a9688570 | Isaku Yamahata | } else {
|
1000 | c71b5b4a | Blue Swirl | cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr), |
1001 | a0c7a97e | Isaku Yamahata | r->filtered_size, |
1002 | a9688570 | Isaku Yamahata | IO_MEM_UNASSIGNED); |
1003 | a0c7a97e | Isaku Yamahata | qemu_unregister_coalesced_mmio(r->addr, r->filtered_size); |
1004 | 0ac32c83 | bellard | } |
1005 | 0ac32c83 | bellard | } |
1006 | a9688570 | Isaku Yamahata | r->addr = new_addr; |
1007 | a0c7a97e | Isaku Yamahata | r->filtered_size = filtered_size; |
1008 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
1009 | a0c7a97e | Isaku Yamahata | /*
|
1010 | a0c7a97e | Isaku Yamahata | * TODO: currently almost all the map funcions assumes
|
1011 | a0c7a97e | Isaku Yamahata | * filtered_size == size and addr & ~(size - 1) == addr.
|
1012 | a0c7a97e | Isaku Yamahata | * However with bridge filtering, they aren't always true.
|
1013 | a0c7a97e | Isaku Yamahata | * Teach them such cases, such that filtered_size < size and
|
1014 | a0c7a97e | Isaku Yamahata | * addr & (size - 1) != 0.
|
1015 | a0c7a97e | Isaku Yamahata | */
|
1016 | cf616802 | Blue Swirl | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1017 | cf616802 | Blue Swirl | r->map_func(d, i, r->addr, r->filtered_size, r->type); |
1018 | cf616802 | Blue Swirl | } else {
|
1019 | cf616802 | Blue Swirl | r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr), |
1020 | cf616802 | Blue Swirl | r->filtered_size, r->type); |
1021 | cf616802 | Blue Swirl | } |
1022 | a9688570 | Isaku Yamahata | } |
1023 | 0ac32c83 | bellard | } |
1024 | 0ac32c83 | bellard | } |
1025 | 0ac32c83 | bellard | |
1026 | a7b15a5c | Michael S. Tsirkin | static inline int pci_irq_disabled(PCIDevice *d) |
1027 | a7b15a5c | Michael S. Tsirkin | { |
1028 | a7b15a5c | Michael S. Tsirkin | return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
|
1029 | a7b15a5c | Michael S. Tsirkin | } |
1030 | a7b15a5c | Michael S. Tsirkin | |
1031 | a7b15a5c | Michael S. Tsirkin | /* Called after interrupt disabled field update in config space,
|
1032 | a7b15a5c | Michael S. Tsirkin | * assert/deassert interrupts if necessary.
|
1033 | a7b15a5c | Michael S. Tsirkin | * Gets original interrupt disable bit value (before update). */
|
1034 | a7b15a5c | Michael S. Tsirkin | static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled) |
1035 | a7b15a5c | Michael S. Tsirkin | { |
1036 | a7b15a5c | Michael S. Tsirkin | int i, disabled = pci_irq_disabled(d);
|
1037 | a7b15a5c | Michael S. Tsirkin | if (disabled == was_irq_disabled)
|
1038 | a7b15a5c | Michael S. Tsirkin | return;
|
1039 | a7b15a5c | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
1040 | a7b15a5c | Michael S. Tsirkin | int state = pci_irq_state(d, i);
|
1041 | a7b15a5c | Michael S. Tsirkin | pci_change_irq_level(d, i, disabled ? -state : state); |
1042 | a7b15a5c | Michael S. Tsirkin | } |
1043 | a7b15a5c | Michael S. Tsirkin | } |
1044 | a7b15a5c | Michael S. Tsirkin | |
1045 | 5fafdf24 | ths | uint32_t pci_default_read_config(PCIDevice *d, |
1046 | 0ac32c83 | bellard | uint32_t address, int len)
|
1047 | 69b91039 | bellard | { |
1048 | 5029fe12 | Isaku Yamahata | uint32_t val = 0;
|
1049 | 5029fe12 | Isaku Yamahata | assert(len == 1 || len == 2 || len == 4); |
1050 | a9f49946 | Isaku Yamahata | len = MIN(len, pci_config_size(d) - address); |
1051 | 5029fe12 | Isaku Yamahata | memcpy(&val, d->config + address, len); |
1052 | 5029fe12 | Isaku Yamahata | return le32_to_cpu(val);
|
1053 | 0ac32c83 | bellard | } |
1054 | 0ac32c83 | bellard | |
1055 | b7ee1603 | Michael S. Tsirkin | void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
1056 | 0ac32c83 | bellard | { |
1057 | a7b15a5c | Michael S. Tsirkin | int i, was_irq_disabled = pci_irq_disabled(d);
|
1058 | a9f49946 | Isaku Yamahata | uint32_t config_size = pci_config_size(d); |
1059 | 0ac32c83 | bellard | |
1060 | 91011d4f | Stefan Weil | for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) { |
1061 | 91011d4f | Stefan Weil | uint8_t wmask = d->wmask[addr + i]; |
1062 | 92ba5f51 | Isaku Yamahata | uint8_t w1cmask = d->w1cmask[addr + i]; |
1063 | 92ba5f51 | Isaku Yamahata | assert(!(wmask & w1cmask)); |
1064 | 91011d4f | Stefan Weil | d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
1065 | 92ba5f51 | Isaku Yamahata | d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
|
1066 | 0ac32c83 | bellard | } |
1067 | 260c0cd3 | Isaku Yamahata | if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
1068 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
|
1069 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
|
1070 | 260c0cd3 | Isaku Yamahata | range_covers_byte(addr, l, PCI_COMMAND)) |
1071 | 0ac32c83 | bellard | pci_update_mappings(d); |
1072 | a7b15a5c | Michael S. Tsirkin | |
1073 | a7b15a5c | Michael S. Tsirkin | if (range_covers_byte(addr, l, PCI_COMMAND))
|
1074 | a7b15a5c | Michael S. Tsirkin | pci_update_irq_disabled(d, was_irq_disabled); |
1075 | 69b91039 | bellard | } |
1076 | 69b91039 | bellard | |
1077 | 502a5395 | pbrook | /***********************************************************/
|
1078 | 502a5395 | pbrook | /* generic PCI irq support */
|
1079 | 30468f78 | bellard | |
1080 | 502a5395 | pbrook | /* 0 <= irq_num <= 3. level must be 0 or 1 */
|
1081 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level) |
1082 | 69b91039 | bellard | { |
1083 | a60380a5 | Juan Quintela | PCIDevice *pci_dev = opaque; |
1084 | 80b3ada7 | pbrook | int change;
|
1085 | 3b46e624 | ths | |
1086 | d036bb21 | Michael S. Tsirkin | change = level - pci_irq_state(pci_dev, irq_num); |
1087 | 80b3ada7 | pbrook | if (!change)
|
1088 | 80b3ada7 | pbrook | return;
|
1089 | d2b59317 | pbrook | |
1090 | d036bb21 | Michael S. Tsirkin | pci_set_irq_state(pci_dev, irq_num, level); |
1091 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(pci_dev); |
1092 | a7b15a5c | Michael S. Tsirkin | if (pci_irq_disabled(pci_dev))
|
1093 | a7b15a5c | Michael S. Tsirkin | return;
|
1094 | d036bb21 | Michael S. Tsirkin | pci_change_irq_level(pci_dev, irq_num, change); |
1095 | 69b91039 | bellard | } |
1096 | 69b91039 | bellard | |
1097 | a5d1fd20 | Isaku Yamahata | bool pci_msi_enabled(PCIDevice *dev)
|
1098 | a5d1fd20 | Isaku Yamahata | { |
1099 | a5d1fd20 | Isaku Yamahata | return msix_enabled(dev) || msi_enabled(dev);
|
1100 | a5d1fd20 | Isaku Yamahata | } |
1101 | a5d1fd20 | Isaku Yamahata | |
1102 | a5d1fd20 | Isaku Yamahata | void pci_msi_notify(PCIDevice *dev, unsigned int vector) |
1103 | a5d1fd20 | Isaku Yamahata | { |
1104 | a5d1fd20 | Isaku Yamahata | if (msix_enabled(dev)) {
|
1105 | a5d1fd20 | Isaku Yamahata | msix_notify(dev, vector); |
1106 | a5d1fd20 | Isaku Yamahata | } else if (msi_enabled(dev)) { |
1107 | a5d1fd20 | Isaku Yamahata | msi_notify(dev, vector); |
1108 | a5d1fd20 | Isaku Yamahata | } else {
|
1109 | a5d1fd20 | Isaku Yamahata | /* MSI/MSI-X must be enabled */
|
1110 | a5d1fd20 | Isaku Yamahata | abort(); |
1111 | a5d1fd20 | Isaku Yamahata | } |
1112 | a5d1fd20 | Isaku Yamahata | } |
1113 | a5d1fd20 | Isaku Yamahata | |
1114 | 502a5395 | pbrook | /***********************************************************/
|
1115 | 502a5395 | pbrook | /* monitor info on PCI */
|
1116 | 0ac32c83 | bellard | |
1117 | 6650ee6d | pbrook | typedef struct { |
1118 | 6650ee6d | pbrook | uint16_t class; |
1119 | 6650ee6d | pbrook | const char *desc; |
1120 | 6650ee6d | pbrook | } pci_class_desc; |
1121 | 6650ee6d | pbrook | |
1122 | 09bc878a | blueswir1 | static const pci_class_desc pci_class_descriptions[] = |
1123 | 6650ee6d | pbrook | { |
1124 | 4ca9c76f | pbrook | { 0x0100, "SCSI controller"}, |
1125 | 6650ee6d | pbrook | { 0x0101, "IDE controller"}, |
1126 | dcb5b19a | ths | { 0x0102, "Floppy controller"}, |
1127 | dcb5b19a | ths | { 0x0103, "IPI controller"}, |
1128 | dcb5b19a | ths | { 0x0104, "RAID controller"}, |
1129 | dcb5b19a | ths | { 0x0106, "SATA controller"}, |
1130 | dcb5b19a | ths | { 0x0107, "SAS controller"}, |
1131 | dcb5b19a | ths | { 0x0180, "Storage controller"}, |
1132 | 6650ee6d | pbrook | { 0x0200, "Ethernet controller"}, |
1133 | dcb5b19a | ths | { 0x0201, "Token Ring controller"}, |
1134 | dcb5b19a | ths | { 0x0202, "FDDI controller"}, |
1135 | dcb5b19a | ths | { 0x0203, "ATM controller"}, |
1136 | dcb5b19a | ths | { 0x0280, "Network controller"}, |
1137 | 6650ee6d | pbrook | { 0x0300, "VGA controller"}, |
1138 | dcb5b19a | ths | { 0x0301, "XGA controller"}, |
1139 | dcb5b19a | ths | { 0x0302, "3D controller"}, |
1140 | dcb5b19a | ths | { 0x0380, "Display controller"}, |
1141 | dcb5b19a | ths | { 0x0400, "Video controller"}, |
1142 | dcb5b19a | ths | { 0x0401, "Audio controller"}, |
1143 | dcb5b19a | ths | { 0x0402, "Phone"}, |
1144 | dcb5b19a | ths | { 0x0480, "Multimedia controller"}, |
1145 | dcb5b19a | ths | { 0x0500, "RAM controller"}, |
1146 | dcb5b19a | ths | { 0x0501, "Flash controller"}, |
1147 | dcb5b19a | ths | { 0x0580, "Memory controller"}, |
1148 | 6650ee6d | pbrook | { 0x0600, "Host bridge"}, |
1149 | 6650ee6d | pbrook | { 0x0601, "ISA bridge"}, |
1150 | dcb5b19a | ths | { 0x0602, "EISA bridge"}, |
1151 | dcb5b19a | ths | { 0x0603, "MC bridge"}, |
1152 | 6650ee6d | pbrook | { 0x0604, "PCI bridge"}, |
1153 | dcb5b19a | ths | { 0x0605, "PCMCIA bridge"}, |
1154 | dcb5b19a | ths | { 0x0606, "NUBUS bridge"}, |
1155 | dcb5b19a | ths | { 0x0607, "CARDBUS bridge"}, |
1156 | dcb5b19a | ths | { 0x0608, "RACEWAY bridge"}, |
1157 | dcb5b19a | ths | { 0x0680, "Bridge"}, |
1158 | 6650ee6d | pbrook | { 0x0c03, "USB controller"}, |
1159 | 6650ee6d | pbrook | { 0, NULL} |
1160 | 6650ee6d | pbrook | }; |
1161 | 6650ee6d | pbrook | |
1162 | 163c8a59 | Luiz Capitulino | static void pci_for_each_device_under_bus(PCIBus *bus, |
1163 | 163c8a59 | Luiz Capitulino | void (*fn)(PCIBus *b, PCIDevice *d))
|
1164 | 30468f78 | bellard | { |
1165 | 163c8a59 | Luiz Capitulino | PCIDevice *d; |
1166 | 163c8a59 | Luiz Capitulino | int devfn;
|
1167 | 30468f78 | bellard | |
1168 | 163c8a59 | Luiz Capitulino | for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1169 | 163c8a59 | Luiz Capitulino | d = bus->devices[devfn]; |
1170 | 163c8a59 | Luiz Capitulino | if (d) {
|
1171 | 163c8a59 | Luiz Capitulino | fn(bus, d); |
1172 | 163c8a59 | Luiz Capitulino | } |
1173 | 163c8a59 | Luiz Capitulino | } |
1174 | 163c8a59 | Luiz Capitulino | } |
1175 | 163c8a59 | Luiz Capitulino | |
1176 | 163c8a59 | Luiz Capitulino | void pci_for_each_device(PCIBus *bus, int bus_num, |
1177 | 163c8a59 | Luiz Capitulino | void (*fn)(PCIBus *b, PCIDevice *d))
|
1178 | 163c8a59 | Luiz Capitulino | { |
1179 | 163c8a59 | Luiz Capitulino | bus = pci_find_bus(bus, bus_num); |
1180 | 163c8a59 | Luiz Capitulino | |
1181 | 163c8a59 | Luiz Capitulino | if (bus) {
|
1182 | 163c8a59 | Luiz Capitulino | pci_for_each_device_under_bus(bus, fn); |
1183 | 163c8a59 | Luiz Capitulino | } |
1184 | 163c8a59 | Luiz Capitulino | } |
1185 | 163c8a59 | Luiz Capitulino | |
1186 | 163c8a59 | Luiz Capitulino | static void pci_device_print(Monitor *mon, QDict *device) |
1187 | 163c8a59 | Luiz Capitulino | { |
1188 | 163c8a59 | Luiz Capitulino | QDict *qdict; |
1189 | 163c8a59 | Luiz Capitulino | QListEntry *entry; |
1190 | 163c8a59 | Luiz Capitulino | uint64_t addr, size; |
1191 | 163c8a59 | Luiz Capitulino | |
1192 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " Bus %2" PRId64 ", ", qdict_get_int(device, "bus")); |
1193 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n", |
1194 | 163c8a59 | Luiz Capitulino | qdict_get_int(device, "slot"),
|
1195 | 163c8a59 | Luiz Capitulino | qdict_get_int(device, "function"));
|
1196 | 376253ec | aliguori | monitor_printf(mon, " ");
|
1197 | 163c8a59 | Luiz Capitulino | |
1198 | 163c8a59 | Luiz Capitulino | qdict = qdict_get_qdict(device, "class_info");
|
1199 | 163c8a59 | Luiz Capitulino | if (qdict_haskey(qdict, "desc")) { |
1200 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "%s", qdict_get_str(qdict, "desc")); |
1201 | 6650ee6d | pbrook | } else {
|
1202 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class")); |
1203 | 72cc6cfe | bellard | } |
1204 | 30468f78 | bellard | |
1205 | 163c8a59 | Luiz Capitulino | qdict = qdict_get_qdict(device, "id");
|
1206 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n", |
1207 | 163c8a59 | Luiz Capitulino | qdict_get_int(qdict, "device"),
|
1208 | 163c8a59 | Luiz Capitulino | qdict_get_int(qdict, "vendor"));
|
1209 | 163c8a59 | Luiz Capitulino | |
1210 | 163c8a59 | Luiz Capitulino | if (qdict_haskey(device, "irq")) { |
1211 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " IRQ %" PRId64 ".\n", |
1212 | 163c8a59 | Luiz Capitulino | qdict_get_int(device, "irq"));
|
1213 | 30468f78 | bellard | } |
1214 | b4dccd8d | Isaku Yamahata | |
1215 | 163c8a59 | Luiz Capitulino | if (qdict_haskey(device, "pci_bridge")) { |
1216 | 163c8a59 | Luiz Capitulino | QDict *info; |
1217 | 163c8a59 | Luiz Capitulino | |
1218 | 163c8a59 | Luiz Capitulino | qdict = qdict_get_qdict(device, "pci_bridge");
|
1219 | 163c8a59 | Luiz Capitulino | |
1220 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "bus");
|
1221 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " BUS %" PRId64 ".\n", |
1222 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "number"));
|
1223 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " secondary bus %" PRId64 ".\n", |
1224 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "secondary"));
|
1225 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " subordinate bus %" PRId64 ".\n", |
1226 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "subordinate"));
|
1227 | b4dccd8d | Isaku Yamahata | |
1228 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "io_range");
|
1229 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n", |
1230 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "base"),
|
1231 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "limit"));
|
1232 | b4dccd8d | Isaku Yamahata | |
1233 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "memory_range");
|
1234 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, |
1235 | b4dccd8d | Isaku Yamahata | " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1236 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "base"),
|
1237 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "limit"));
|
1238 | b4dccd8d | Isaku Yamahata | |
1239 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "prefetchable_range");
|
1240 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " prefetchable memory range "
|
1241 | 163c8a59 | Luiz Capitulino | "[0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1242 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "base"),
|
1243 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "limit"));
|
1244 | 80b3ada7 | pbrook | } |
1245 | 14421258 | Isaku Yamahata | |
1246 | 163c8a59 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
|
1247 | 163c8a59 | Luiz Capitulino | qdict = qobject_to_qdict(qlist_entry_obj(entry)); |
1248 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " BAR%d: ", (int) qdict_get_int(qdict, "bar")); |
1249 | 163c8a59 | Luiz Capitulino | |
1250 | 163c8a59 | Luiz Capitulino | addr = qdict_get_int(qdict, "address");
|
1251 | 163c8a59 | Luiz Capitulino | size = qdict_get_int(qdict, "size");
|
1252 | 163c8a59 | Luiz Capitulino | |
1253 | 163c8a59 | Luiz Capitulino | if (!strcmp(qdict_get_str(qdict, "type"), "io")) { |
1254 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
|
1255 | 163c8a59 | Luiz Capitulino | " [0x%04"FMT_PCIBUS"].\n", |
1256 | 163c8a59 | Luiz Capitulino | addr, addr + size - 1);
|
1257 | 163c8a59 | Luiz Capitulino | } else {
|
1258 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
|
1259 | 89e8b13c | Isaku Yamahata | " [0x%08"FMT_PCIBUS"].\n", |
1260 | 163c8a59 | Luiz Capitulino | qdict_get_bool(qdict, "mem_type_64") ? 64 : 32, |
1261 | 163c8a59 | Luiz Capitulino | qdict_get_bool(qdict, "prefetch") ?
|
1262 | 163c8a59 | Luiz Capitulino | " prefetchable" : "", addr, addr + size - 1); |
1263 | 502a5395 | pbrook | } |
1264 | 77d4bc34 | bellard | } |
1265 | 163c8a59 | Luiz Capitulino | |
1266 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " id \"%s\"\n", qdict_get_str(device, "qdev_id")); |
1267 | 163c8a59 | Luiz Capitulino | |
1268 | d5e4acf7 | Luiz Capitulino | if (qdict_haskey(device, "pci_bridge")) { |
1269 | d5e4acf7 | Luiz Capitulino | qdict = qdict_get_qdict(device, "pci_bridge");
|
1270 | d5e4acf7 | Luiz Capitulino | if (qdict_haskey(qdict, "devices")) { |
1271 | d5e4acf7 | Luiz Capitulino | QListEntry *dev; |
1272 | d5e4acf7 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
|
1273 | d5e4acf7 | Luiz Capitulino | pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev))); |
1274 | d5e4acf7 | Luiz Capitulino | } |
1275 | d5e4acf7 | Luiz Capitulino | } |
1276 | d5e4acf7 | Luiz Capitulino | } |
1277 | 163c8a59 | Luiz Capitulino | } |
1278 | 163c8a59 | Luiz Capitulino | |
1279 | 163c8a59 | Luiz Capitulino | void do_pci_info_print(Monitor *mon, const QObject *data) |
1280 | 163c8a59 | Luiz Capitulino | { |
1281 | 163c8a59 | Luiz Capitulino | QListEntry *bus, *dev; |
1282 | 163c8a59 | Luiz Capitulino | |
1283 | 163c8a59 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) { |
1284 | 163c8a59 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus)); |
1285 | 163c8a59 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
|
1286 | 163c8a59 | Luiz Capitulino | pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev))); |
1287 | 163c8a59 | Luiz Capitulino | } |
1288 | 80b3ada7 | pbrook | } |
1289 | 384d8876 | bellard | } |
1290 | 384d8876 | bellard | |
1291 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_dev_class(const PCIDevice *dev) |
1292 | 163c8a59 | Luiz Capitulino | { |
1293 | 163c8a59 | Luiz Capitulino | int class;
|
1294 | 163c8a59 | Luiz Capitulino | const pci_class_desc *desc;
|
1295 | 163c8a59 | Luiz Capitulino | |
1296 | 163c8a59 | Luiz Capitulino | class = pci_get_word(dev->config + PCI_CLASS_DEVICE); |
1297 | 163c8a59 | Luiz Capitulino | desc = pci_class_descriptions; |
1298 | 163c8a59 | Luiz Capitulino | while (desc->desc && class != desc->class)
|
1299 | 163c8a59 | Luiz Capitulino | desc++; |
1300 | 163c8a59 | Luiz Capitulino | |
1301 | 163c8a59 | Luiz Capitulino | if (desc->desc) {
|
1302 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'desc': %s, 'class': %d }", |
1303 | 163c8a59 | Luiz Capitulino | desc->desc, class); |
1304 | 163c8a59 | Luiz Capitulino | } else {
|
1305 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'class': %d }", class); |
1306 | 163c8a59 | Luiz Capitulino | } |
1307 | 163c8a59 | Luiz Capitulino | } |
1308 | 163c8a59 | Luiz Capitulino | |
1309 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_dev_id(const PCIDevice *dev) |
1310 | 163c8a59 | Luiz Capitulino | { |
1311 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }", |
1312 | 163c8a59 | Luiz Capitulino | pci_get_word(dev->config + PCI_VENDOR_ID), |
1313 | 163c8a59 | Luiz Capitulino | pci_get_word(dev->config + PCI_DEVICE_ID)); |
1314 | 163c8a59 | Luiz Capitulino | } |
1315 | 163c8a59 | Luiz Capitulino | |
1316 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_regions_list(const PCIDevice *dev) |
1317 | 163c8a59 | Luiz Capitulino | { |
1318 | 163c8a59 | Luiz Capitulino | int i;
|
1319 | 163c8a59 | Luiz Capitulino | QList *regions_list; |
1320 | 163c8a59 | Luiz Capitulino | |
1321 | 163c8a59 | Luiz Capitulino | regions_list = qlist_new(); |
1322 | 163c8a59 | Luiz Capitulino | |
1323 | 163c8a59 | Luiz Capitulino | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1324 | 163c8a59 | Luiz Capitulino | QObject *obj; |
1325 | 163c8a59 | Luiz Capitulino | const PCIIORegion *r = &dev->io_regions[i];
|
1326 | 163c8a59 | Luiz Capitulino | |
1327 | 163c8a59 | Luiz Capitulino | if (!r->size) {
|
1328 | 163c8a59 | Luiz Capitulino | continue;
|
1329 | 163c8a59 | Luiz Capitulino | } |
1330 | 163c8a59 | Luiz Capitulino | |
1331 | 163c8a59 | Luiz Capitulino | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1332 | 163c8a59 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
|
1333 | 163c8a59 | Luiz Capitulino | "'address': %" PRId64 ", " |
1334 | 163c8a59 | Luiz Capitulino | "'size': %" PRId64 " }", |
1335 | 163c8a59 | Luiz Capitulino | i, r->addr, r->size); |
1336 | 163c8a59 | Luiz Capitulino | } else {
|
1337 | 163c8a59 | Luiz Capitulino | int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
|
1338 | 163c8a59 | Luiz Capitulino | |
1339 | 163c8a59 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
|
1340 | 163c8a59 | Luiz Capitulino | "'mem_type_64': %i, 'prefetch': %i, "
|
1341 | 163c8a59 | Luiz Capitulino | "'address': %" PRId64 ", " |
1342 | 163c8a59 | Luiz Capitulino | "'size': %" PRId64 " }", |
1343 | 163c8a59 | Luiz Capitulino | i, mem_type_64, |
1344 | 163c8a59 | Luiz Capitulino | r->type & PCI_BASE_ADDRESS_MEM_PREFETCH, |
1345 | 163c8a59 | Luiz Capitulino | r->addr, r->size); |
1346 | 163c8a59 | Luiz Capitulino | } |
1347 | 163c8a59 | Luiz Capitulino | |
1348 | 163c8a59 | Luiz Capitulino | qlist_append_obj(regions_list, obj); |
1349 | 163c8a59 | Luiz Capitulino | } |
1350 | 163c8a59 | Luiz Capitulino | |
1351 | 163c8a59 | Luiz Capitulino | return QOBJECT(regions_list);
|
1352 | 163c8a59 | Luiz Capitulino | } |
1353 | 163c8a59 | Luiz Capitulino | |
1354 | d5e4acf7 | Luiz Capitulino | static QObject *pci_get_devices_list(PCIBus *bus, int bus_num); |
1355 | d5e4acf7 | Luiz Capitulino | |
1356 | d5e4acf7 | Luiz Capitulino | static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num) |
1357 | 163c8a59 | Luiz Capitulino | { |
1358 | b5937f29 | Isaku Yamahata | uint8_t type; |
1359 | 163c8a59 | Luiz Capitulino | QObject *obj; |
1360 | 163c8a59 | Luiz Capitulino | |
1361 | 163c8a59 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d," "'class_info': %p, 'id': %p, 'regions': %p," |
1362 | 163c8a59 | Luiz Capitulino | " 'qdev_id': %s }",
|
1363 | 163c8a59 | Luiz Capitulino | bus_num, |
1364 | 163c8a59 | Luiz Capitulino | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
1365 | 163c8a59 | Luiz Capitulino | pci_get_dev_class(dev), pci_get_dev_id(dev), |
1366 | 163c8a59 | Luiz Capitulino | pci_get_regions_list(dev), |
1367 | 163c8a59 | Luiz Capitulino | dev->qdev.id ? dev->qdev.id : "");
|
1368 | 163c8a59 | Luiz Capitulino | |
1369 | 163c8a59 | Luiz Capitulino | if (dev->config[PCI_INTERRUPT_PIN] != 0) { |
1370 | 163c8a59 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(obj); |
1371 | 163c8a59 | Luiz Capitulino | qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
|
1372 | 163c8a59 | Luiz Capitulino | } |
1373 | 163c8a59 | Luiz Capitulino | |
1374 | b5937f29 | Isaku Yamahata | type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
1375 | b5937f29 | Isaku Yamahata | if (type == PCI_HEADER_TYPE_BRIDGE) {
|
1376 | 163c8a59 | Luiz Capitulino | QDict *qdict; |
1377 | 163c8a59 | Luiz Capitulino | QObject *pci_bridge; |
1378 | 163c8a59 | Luiz Capitulino | |
1379 | 163c8a59 | Luiz Capitulino | pci_bridge = qobject_from_jsonf("{ 'bus': "
|
1380 | 163c8a59 | Luiz Capitulino | "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
|
1381 | 163c8a59 | Luiz Capitulino | "'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, " |
1382 | 163c8a59 | Luiz Capitulino | "'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, " |
1383 | 163c8a59 | Luiz Capitulino | "'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }", |
1384 | c021f8e6 | Blue Swirl | dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS], |
1385 | 163c8a59 | Luiz Capitulino | dev->config[PCI_SUBORDINATE_BUS], |
1386 | 163c8a59 | Luiz Capitulino | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO), |
1387 | 163c8a59 | Luiz Capitulino | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO), |
1388 | 163c8a59 | Luiz Capitulino | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY), |
1389 | 163c8a59 | Luiz Capitulino | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY), |
1390 | 163c8a59 | Luiz Capitulino | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1391 | 163c8a59 | Luiz Capitulino | PCI_BASE_ADDRESS_MEM_PREFETCH), |
1392 | 163c8a59 | Luiz Capitulino | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1393 | 163c8a59 | Luiz Capitulino | PCI_BASE_ADDRESS_MEM_PREFETCH)); |
1394 | 163c8a59 | Luiz Capitulino | |
1395 | c021f8e6 | Blue Swirl | if (dev->config[PCI_SECONDARY_BUS] != 0) { |
1396 | c021f8e6 | Blue Swirl | PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]); |
1397 | d5e4acf7 | Luiz Capitulino | |
1398 | c021f8e6 | Blue Swirl | if (child_bus) {
|
1399 | c021f8e6 | Blue Swirl | qdict = qobject_to_qdict(pci_bridge); |
1400 | c021f8e6 | Blue Swirl | qdict_put_obj(qdict, "devices",
|
1401 | c021f8e6 | Blue Swirl | pci_get_devices_list(child_bus, |
1402 | c021f8e6 | Blue Swirl | dev->config[PCI_SECONDARY_BUS])); |
1403 | c021f8e6 | Blue Swirl | } |
1404 | c021f8e6 | Blue Swirl | } |
1405 | 163c8a59 | Luiz Capitulino | qdict = qobject_to_qdict(obj); |
1406 | 163c8a59 | Luiz Capitulino | qdict_put_obj(qdict, "pci_bridge", pci_bridge);
|
1407 | 163c8a59 | Luiz Capitulino | } |
1408 | 163c8a59 | Luiz Capitulino | |
1409 | 163c8a59 | Luiz Capitulino | return obj;
|
1410 | 163c8a59 | Luiz Capitulino | } |
1411 | 163c8a59 | Luiz Capitulino | |
1412 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_devices_list(PCIBus *bus, int bus_num) |
1413 | 384d8876 | bellard | { |
1414 | 502a5395 | pbrook | int devfn;
|
1415 | 163c8a59 | Luiz Capitulino | PCIDevice *dev; |
1416 | 163c8a59 | Luiz Capitulino | QList *dev_list; |
1417 | 3b46e624 | ths | |
1418 | 163c8a59 | Luiz Capitulino | dev_list = qlist_new(); |
1419 | 163c8a59 | Luiz Capitulino | |
1420 | 163c8a59 | Luiz Capitulino | for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1421 | 163c8a59 | Luiz Capitulino | dev = bus->devices[devfn]; |
1422 | 163c8a59 | Luiz Capitulino | if (dev) {
|
1423 | d5e4acf7 | Luiz Capitulino | qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num)); |
1424 | 163c8a59 | Luiz Capitulino | } |
1425 | 1074df4f | Isaku Yamahata | } |
1426 | 163c8a59 | Luiz Capitulino | |
1427 | 163c8a59 | Luiz Capitulino | return QOBJECT(dev_list);
|
1428 | 1074df4f | Isaku Yamahata | } |
1429 | 1074df4f | Isaku Yamahata | |
1430 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num) |
1431 | 1074df4f | Isaku Yamahata | { |
1432 | e822a52a | Isaku Yamahata | bus = pci_find_bus(bus, bus_num); |
1433 | 502a5395 | pbrook | if (bus) {
|
1434 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }", |
1435 | 163c8a59 | Luiz Capitulino | bus_num, pci_get_devices_list(bus, bus_num)); |
1436 | f2aa58c6 | bellard | } |
1437 | 163c8a59 | Luiz Capitulino | |
1438 | 163c8a59 | Luiz Capitulino | return NULL; |
1439 | f2aa58c6 | bellard | } |
1440 | f2aa58c6 | bellard | |
1441 | 163c8a59 | Luiz Capitulino | void do_pci_info(Monitor *mon, QObject **ret_data)
|
1442 | f2aa58c6 | bellard | { |
1443 | 163c8a59 | Luiz Capitulino | QList *bus_list; |
1444 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
1445 | 163c8a59 | Luiz Capitulino | |
1446 | 163c8a59 | Luiz Capitulino | bus_list = qlist_new(); |
1447 | 163c8a59 | Luiz Capitulino | |
1448 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
1449 | 163c8a59 | Luiz Capitulino | QObject *obj = pci_get_bus_dict(host->bus, 0);
|
1450 | 163c8a59 | Luiz Capitulino | if (obj) {
|
1451 | 163c8a59 | Luiz Capitulino | qlist_append_obj(bus_list, obj); |
1452 | 163c8a59 | Luiz Capitulino | } |
1453 | e822a52a | Isaku Yamahata | } |
1454 | 163c8a59 | Luiz Capitulino | |
1455 | 163c8a59 | Luiz Capitulino | *ret_data = QOBJECT(bus_list); |
1456 | 77d4bc34 | bellard | } |
1457 | a41b2ff2 | pbrook | |
1458 | cb457d76 | aliguori | static const char * const pci_nic_models[] = { |
1459 | cb457d76 | aliguori | "ne2k_pci",
|
1460 | cb457d76 | aliguori | "i82551",
|
1461 | cb457d76 | aliguori | "i82557b",
|
1462 | cb457d76 | aliguori | "i82559er",
|
1463 | cb457d76 | aliguori | "rtl8139",
|
1464 | cb457d76 | aliguori | "e1000",
|
1465 | cb457d76 | aliguori | "pcnet",
|
1466 | cb457d76 | aliguori | "virtio",
|
1467 | cb457d76 | aliguori | NULL
|
1468 | cb457d76 | aliguori | }; |
1469 | cb457d76 | aliguori | |
1470 | 9d07d757 | Paul Brook | static const char * const pci_nic_names[] = { |
1471 | 9d07d757 | Paul Brook | "ne2k_pci",
|
1472 | 9d07d757 | Paul Brook | "i82551",
|
1473 | 9d07d757 | Paul Brook | "i82557b",
|
1474 | 9d07d757 | Paul Brook | "i82559er",
|
1475 | 9d07d757 | Paul Brook | "rtl8139",
|
1476 | 9d07d757 | Paul Brook | "e1000",
|
1477 | 9d07d757 | Paul Brook | "pcnet",
|
1478 | 53c25cea | Paul Brook | "virtio-net-pci",
|
1479 | cb457d76 | aliguori | NULL
|
1480 | cb457d76 | aliguori | }; |
1481 | cb457d76 | aliguori | |
1482 | a41b2ff2 | pbrook | /* Initialize a PCI NIC. */
|
1483 | 33e66b86 | Markus Armbruster | /* FIXME callers should check for failure, but don't */
|
1484 | 5607c388 | Markus Armbruster | PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model, |
1485 | 5607c388 | Markus Armbruster | const char *default_devaddr) |
1486 | a41b2ff2 | pbrook | { |
1487 | 5607c388 | Markus Armbruster | const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
1488 | 07caea31 | Markus Armbruster | PCIBus *bus; |
1489 | 07caea31 | Markus Armbruster | int devfn;
|
1490 | 5607c388 | Markus Armbruster | PCIDevice *pci_dev; |
1491 | 9d07d757 | Paul Brook | DeviceState *dev; |
1492 | cb457d76 | aliguori | int i;
|
1493 | cb457d76 | aliguori | |
1494 | 07caea31 | Markus Armbruster | i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1495 | 07caea31 | Markus Armbruster | if (i < 0) |
1496 | 07caea31 | Markus Armbruster | return NULL; |
1497 | 07caea31 | Markus Armbruster | |
1498 | 07caea31 | Markus Armbruster | bus = pci_get_bus_devfn(&devfn, devaddr); |
1499 | 07caea31 | Markus Armbruster | if (!bus) {
|
1500 | 1ecda02b | Markus Armbruster | error_report("Invalid PCI device address %s for device %s",
|
1501 | 1ecda02b | Markus Armbruster | devaddr, pci_nic_names[i]); |
1502 | 07caea31 | Markus Armbruster | return NULL; |
1503 | 07caea31 | Markus Armbruster | } |
1504 | 07caea31 | Markus Armbruster | |
1505 | 499cf102 | Markus Armbruster | pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
1506 | 9ee05825 | Markus Armbruster | dev = &pci_dev->qdev; |
1507 | 1cc33683 | Gerd Hoffmann | qdev_set_nic_properties(dev, nd); |
1508 | 07caea31 | Markus Armbruster | if (qdev_init(dev) < 0) |
1509 | 07caea31 | Markus Armbruster | return NULL; |
1510 | 9ee05825 | Markus Armbruster | return pci_dev;
|
1511 | a41b2ff2 | pbrook | } |
1512 | a41b2ff2 | pbrook | |
1513 | 07caea31 | Markus Armbruster | PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model, |
1514 | 07caea31 | Markus Armbruster | const char *default_devaddr) |
1515 | 07caea31 | Markus Armbruster | { |
1516 | 07caea31 | Markus Armbruster | PCIDevice *res; |
1517 | 07caea31 | Markus Armbruster | |
1518 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, pci_nic_models))
|
1519 | 07caea31 | Markus Armbruster | exit(0);
|
1520 | 07caea31 | Markus Armbruster | |
1521 | 07caea31 | Markus Armbruster | res = pci_nic_init(nd, default_model, default_devaddr); |
1522 | 07caea31 | Markus Armbruster | if (!res)
|
1523 | 07caea31 | Markus Armbruster | exit(1);
|
1524 | 07caea31 | Markus Armbruster | return res;
|
1525 | 07caea31 | Markus Armbruster | } |
1526 | 07caea31 | Markus Armbruster | |
1527 | a0c7a97e | Isaku Yamahata | static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d) |
1528 | a0c7a97e | Isaku Yamahata | { |
1529 | a0c7a97e | Isaku Yamahata | pci_update_mappings(d); |
1530 | a0c7a97e | Isaku Yamahata | } |
1531 | a0c7a97e | Isaku Yamahata | |
1532 | 783753fd | Isaku Yamahata | void pci_bridge_update_mappings(PCIBus *b)
|
1533 | a0c7a97e | Isaku Yamahata | { |
1534 | a0c7a97e | Isaku Yamahata | PCIBus *child; |
1535 | a0c7a97e | Isaku Yamahata | |
1536 | a0c7a97e | Isaku Yamahata | pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn); |
1537 | a0c7a97e | Isaku Yamahata | |
1538 | a0c7a97e | Isaku Yamahata | QLIST_FOREACH(child, &b->child, sibling) { |
1539 | a0c7a97e | Isaku Yamahata | pci_bridge_update_mappings(child); |
1540 | a0c7a97e | Isaku Yamahata | } |
1541 | a0c7a97e | Isaku Yamahata | } |
1542 | a0c7a97e | Isaku Yamahata | |
1543 | e822a52a | Isaku Yamahata | PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
|
1544 | 3ae80618 | aliguori | { |
1545 | 470e6363 | Isaku Yamahata | PCIBus *sec; |
1546 | 3ae80618 | aliguori | |
1547 | 470e6363 | Isaku Yamahata | if (!bus) {
|
1548 | e822a52a | Isaku Yamahata | return NULL; |
1549 | 470e6363 | Isaku Yamahata | } |
1550 | 3ae80618 | aliguori | |
1551 | e822a52a | Isaku Yamahata | if (pci_bus_num(bus) == bus_num) {
|
1552 | e822a52a | Isaku Yamahata | return bus;
|
1553 | e822a52a | Isaku Yamahata | } |
1554 | e822a52a | Isaku Yamahata | |
1555 | e822a52a | Isaku Yamahata | /* try child bus */
|
1556 | 470e6363 | Isaku Yamahata | if (!bus->parent_dev /* host pci bridge */ || |
1557 | 470e6363 | Isaku Yamahata | (bus->parent_dev->config[PCI_SECONDARY_BUS] < bus_num && |
1558 | 470e6363 | Isaku Yamahata | bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS])) { |
1559 | 470e6363 | Isaku Yamahata | for (; bus; bus = sec) {
|
1560 | 470e6363 | Isaku Yamahata | QLIST_FOREACH(sec, &bus->child, sibling) { |
1561 | 470e6363 | Isaku Yamahata | assert(sec->parent_dev); |
1562 | 470e6363 | Isaku Yamahata | if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
|
1563 | 470e6363 | Isaku Yamahata | return sec;
|
1564 | 470e6363 | Isaku Yamahata | } |
1565 | 470e6363 | Isaku Yamahata | if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
|
1566 | 470e6363 | Isaku Yamahata | bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) { |
1567 | 470e6363 | Isaku Yamahata | break;
|
1568 | 470e6363 | Isaku Yamahata | } |
1569 | c021f8e6 | Blue Swirl | } |
1570 | e822a52a | Isaku Yamahata | } |
1571 | e822a52a | Isaku Yamahata | } |
1572 | e822a52a | Isaku Yamahata | |
1573 | e822a52a | Isaku Yamahata | return NULL; |
1574 | 3ae80618 | aliguori | } |
1575 | 3ae80618 | aliguori | |
1576 | e822a52a | Isaku Yamahata | PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function) |
1577 | 3ae80618 | aliguori | { |
1578 | e822a52a | Isaku Yamahata | bus = pci_find_bus(bus, bus_num); |
1579 | 3ae80618 | aliguori | |
1580 | 3ae80618 | aliguori | if (!bus)
|
1581 | 3ae80618 | aliguori | return NULL; |
1582 | 3ae80618 | aliguori | |
1583 | 3ae80618 | aliguori | return bus->devices[PCI_DEVFN(slot, function)];
|
1584 | 3ae80618 | aliguori | } |
1585 | 3ae80618 | aliguori | |
1586 | 81a322d4 | Gerd Hoffmann | static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) |
1587 | 6b1b92d3 | Paul Brook | { |
1588 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev = (PCIDevice *)qdev; |
1589 | 02e2da45 | Paul Brook | PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev); |
1590 | 6b1b92d3 | Paul Brook | PCIBus *bus; |
1591 | ee995ffb | Gerd Hoffmann | int devfn, rc;
|
1592 | ab85ceb1 | Stefan Weil | bool is_default_rom;
|
1593 | 6b1b92d3 | Paul Brook | |
1594 | a9f49946 | Isaku Yamahata | /* initialize cap_present for pci_is_express() and pci_config_size() */
|
1595 | a9f49946 | Isaku Yamahata | if (info->is_express) {
|
1596 | a9f49946 | Isaku Yamahata | pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1597 | a9f49946 | Isaku Yamahata | } |
1598 | a9f49946 | Isaku Yamahata | |
1599 | 02e2da45 | Paul Brook | bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev)); |
1600 | ee6847d1 | Gerd Hoffmann | devfn = pci_dev->devfn; |
1601 | 16eaedf2 | Gerd Hoffmann | pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn, |
1602 | fb231628 | Isaku Yamahata | info->config_read, info->config_write, |
1603 | e327e323 | Isaku Yamahata | info->is_bridge); |
1604 | 09e3acc6 | Gerd Hoffmann | if (pci_dev == NULL) |
1605 | 09e3acc6 | Gerd Hoffmann | return -1; |
1606 | ee995ffb | Gerd Hoffmann | rc = info->init(pci_dev); |
1607 | 925fe64a | Alex Williamson | if (rc != 0) { |
1608 | 925fe64a | Alex Williamson | do_pci_unregister_device(pci_dev); |
1609 | ee995ffb | Gerd Hoffmann | return rc;
|
1610 | 925fe64a | Alex Williamson | } |
1611 | 8c52c8f3 | Gerd Hoffmann | |
1612 | 8c52c8f3 | Gerd Hoffmann | /* rom loading */
|
1613 | ab85ceb1 | Stefan Weil | is_default_rom = false;
|
1614 | ab85ceb1 | Stefan Weil | if (pci_dev->romfile == NULL && info->romfile != NULL) { |
1615 | 8c52c8f3 | Gerd Hoffmann | pci_dev->romfile = qemu_strdup(info->romfile); |
1616 | ab85ceb1 | Stefan Weil | is_default_rom = true;
|
1617 | ab85ceb1 | Stefan Weil | } |
1618 | ab85ceb1 | Stefan Weil | pci_add_option_rom(pci_dev, is_default_rom); |
1619 | 8c52c8f3 | Gerd Hoffmann | |
1620 | 5beb8ad5 | Isaku Yamahata | if (bus->hotplug) {
|
1621 | e927d487 | Michael S. Tsirkin | /* Let buses differentiate between hotplug and when device is
|
1622 | e927d487 | Michael S. Tsirkin | * enabled during qemu machine creation. */
|
1623 | e927d487 | Michael S. Tsirkin | rc = bus->hotplug(bus->hotplug_qdev, pci_dev, |
1624 | e927d487 | Michael S. Tsirkin | qdev->hotplugged ? PCI_HOTPLUG_ENABLED: |
1625 | e927d487 | Michael S. Tsirkin | PCI_COLDPLUG_ENABLED); |
1626 | a213ff63 | Isaku Yamahata | if (rc != 0) { |
1627 | a213ff63 | Isaku Yamahata | int r = pci_unregister_device(&pci_dev->qdev);
|
1628 | a213ff63 | Isaku Yamahata | assert(!r); |
1629 | a213ff63 | Isaku Yamahata | return rc;
|
1630 | a213ff63 | Isaku Yamahata | } |
1631 | a213ff63 | Isaku Yamahata | } |
1632 | ee995ffb | Gerd Hoffmann | return 0; |
1633 | ee995ffb | Gerd Hoffmann | } |
1634 | ee995ffb | Gerd Hoffmann | |
1635 | ee995ffb | Gerd Hoffmann | static int pci_unplug_device(DeviceState *qdev) |
1636 | ee995ffb | Gerd Hoffmann | { |
1637 | ee995ffb | Gerd Hoffmann | PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev); |
1638 | ee995ffb | Gerd Hoffmann | |
1639 | e927d487 | Michael S. Tsirkin | return dev->bus->hotplug(dev->bus->hotplug_qdev, dev,
|
1640 | e927d487 | Michael S. Tsirkin | PCI_HOTPLUG_DISABLED); |
1641 | 6b1b92d3 | Paul Brook | } |
1642 | 6b1b92d3 | Paul Brook | |
1643 | 0aab0d3a | Gerd Hoffmann | void pci_qdev_register(PCIDeviceInfo *info)
|
1644 | 6b1b92d3 | Paul Brook | { |
1645 | 02e2da45 | Paul Brook | info->qdev.init = pci_qdev_init; |
1646 | ee995ffb | Gerd Hoffmann | info->qdev.unplug = pci_unplug_device; |
1647 | a36a344d | Gerd Hoffmann | info->qdev.exit = pci_unregister_device; |
1648 | 10c4c98a | Gerd Hoffmann | info->qdev.bus_info = &pci_bus_info; |
1649 | 074f2fff | Gerd Hoffmann | qdev_register(&info->qdev); |
1650 | 6b1b92d3 | Paul Brook | } |
1651 | 6b1b92d3 | Paul Brook | |
1652 | 0aab0d3a | Gerd Hoffmann | void pci_qdev_register_many(PCIDeviceInfo *info)
|
1653 | 0aab0d3a | Gerd Hoffmann | { |
1654 | 0aab0d3a | Gerd Hoffmann | while (info->qdev.name) {
|
1655 | 0aab0d3a | Gerd Hoffmann | pci_qdev_register(info); |
1656 | 0aab0d3a | Gerd Hoffmann | info++; |
1657 | 0aab0d3a | Gerd Hoffmann | } |
1658 | 0aab0d3a | Gerd Hoffmann | } |
1659 | 0aab0d3a | Gerd Hoffmann | |
1660 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction, |
1661 | 49823868 | Isaku Yamahata | const char *name) |
1662 | 6b1b92d3 | Paul Brook | { |
1663 | 6b1b92d3 | Paul Brook | DeviceState *dev; |
1664 | 6b1b92d3 | Paul Brook | |
1665 | 02e2da45 | Paul Brook | dev = qdev_create(&bus->qbus, name); |
1666 | a6307b08 | Gerd Hoffmann | qdev_prop_set_uint32(dev, "addr", devfn);
|
1667 | 49823868 | Isaku Yamahata | qdev_prop_set_bit(dev, "multifunction", multifunction);
|
1668 | 71077c1c | Gerd Hoffmann | return DO_UPCAST(PCIDevice, qdev, dev);
|
1669 | 71077c1c | Gerd Hoffmann | } |
1670 | 6b1b92d3 | Paul Brook | |
1671 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
|
1672 | 49823868 | Isaku Yamahata | bool multifunction,
|
1673 | 49823868 | Isaku Yamahata | const char *name) |
1674 | 71077c1c | Gerd Hoffmann | { |
1675 | 49823868 | Isaku Yamahata | PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name); |
1676 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
1677 | 71077c1c | Gerd Hoffmann | return dev;
|
1678 | 6b1b92d3 | Paul Brook | } |
1679 | 6f4cbd39 | Michael S. Tsirkin | |
1680 | 49823868 | Isaku Yamahata | PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1681 | 49823868 | Isaku Yamahata | { |
1682 | 49823868 | Isaku Yamahata | return pci_create_multifunction(bus, devfn, false, name); |
1683 | 49823868 | Isaku Yamahata | } |
1684 | 49823868 | Isaku Yamahata | |
1685 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1686 | 49823868 | Isaku Yamahata | { |
1687 | 49823868 | Isaku Yamahata | return pci_create_simple_multifunction(bus, devfn, false, name); |
1688 | 49823868 | Isaku Yamahata | } |
1689 | 49823868 | Isaku Yamahata | |
1690 | 6f4cbd39 | Michael S. Tsirkin | static int pci_find_space(PCIDevice *pdev, uint8_t size) |
1691 | 6f4cbd39 | Michael S. Tsirkin | { |
1692 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pdev);
|
1693 | 6f4cbd39 | Michael S. Tsirkin | int offset = PCI_CONFIG_HEADER_SIZE;
|
1694 | 6f4cbd39 | Michael S. Tsirkin | int i;
|
1695 | a9f49946 | Isaku Yamahata | for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
1696 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->used[i])
|
1697 | 6f4cbd39 | Michael S. Tsirkin | offset = i + 1;
|
1698 | 6f4cbd39 | Michael S. Tsirkin | else if (i - offset + 1 == size) |
1699 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1700 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1701 | 6f4cbd39 | Michael S. Tsirkin | } |
1702 | 6f4cbd39 | Michael S. Tsirkin | |
1703 | 6f4cbd39 | Michael S. Tsirkin | static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1704 | 6f4cbd39 | Michael S. Tsirkin | uint8_t *prev_p) |
1705 | 6f4cbd39 | Michael S. Tsirkin | { |
1706 | 6f4cbd39 | Michael S. Tsirkin | uint8_t next, prev; |
1707 | 6f4cbd39 | Michael S. Tsirkin | |
1708 | 6f4cbd39 | Michael S. Tsirkin | if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1709 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1710 | 6f4cbd39 | Michael S. Tsirkin | |
1711 | 6f4cbd39 | Michael S. Tsirkin | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1712 | 6f4cbd39 | Michael S. Tsirkin | prev = next + PCI_CAP_LIST_NEXT) |
1713 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1714 | 6f4cbd39 | Michael S. Tsirkin | break;
|
1715 | 6f4cbd39 | Michael S. Tsirkin | |
1716 | 6f4cbd39 | Michael S. Tsirkin | if (prev_p)
|
1717 | 6f4cbd39 | Michael S. Tsirkin | *prev_p = prev; |
1718 | 6f4cbd39 | Michael S. Tsirkin | return next;
|
1719 | 6f4cbd39 | Michael S. Tsirkin | } |
1720 | 6f4cbd39 | Michael S. Tsirkin | |
1721 | c2039bd0 | Anthony Liguori | static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type) |
1722 | c2039bd0 | Anthony Liguori | { |
1723 | c2039bd0 | Anthony Liguori | cpu_register_physical_memory(addr, size, pdev->rom_offset); |
1724 | c2039bd0 | Anthony Liguori | } |
1725 | c2039bd0 | Anthony Liguori | |
1726 | ab85ceb1 | Stefan Weil | /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
|
1727 | ab85ceb1 | Stefan Weil | This is needed for an option rom which is used for more than one device. */
|
1728 | ab85ceb1 | Stefan Weil | static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size) |
1729 | ab85ceb1 | Stefan Weil | { |
1730 | ab85ceb1 | Stefan Weil | uint16_t vendor_id; |
1731 | ab85ceb1 | Stefan Weil | uint16_t device_id; |
1732 | ab85ceb1 | Stefan Weil | uint16_t rom_vendor_id; |
1733 | ab85ceb1 | Stefan Weil | uint16_t rom_device_id; |
1734 | ab85ceb1 | Stefan Weil | uint16_t rom_magic; |
1735 | ab85ceb1 | Stefan Weil | uint16_t pcir_offset; |
1736 | ab85ceb1 | Stefan Weil | uint8_t checksum; |
1737 | ab85ceb1 | Stefan Weil | |
1738 | ab85ceb1 | Stefan Weil | /* Words in rom data are little endian (like in PCI configuration),
|
1739 | ab85ceb1 | Stefan Weil | so they can be read / written with pci_get_word / pci_set_word. */
|
1740 | ab85ceb1 | Stefan Weil | |
1741 | ab85ceb1 | Stefan Weil | /* Only a valid rom will be patched. */
|
1742 | ab85ceb1 | Stefan Weil | rom_magic = pci_get_word(ptr); |
1743 | ab85ceb1 | Stefan Weil | if (rom_magic != 0xaa55) { |
1744 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
|
1745 | ab85ceb1 | Stefan Weil | return;
|
1746 | ab85ceb1 | Stefan Weil | } |
1747 | ab85ceb1 | Stefan Weil | pcir_offset = pci_get_word(ptr + 0x18);
|
1748 | ab85ceb1 | Stefan Weil | if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) { |
1749 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
|
1750 | ab85ceb1 | Stefan Weil | return;
|
1751 | ab85ceb1 | Stefan Weil | } |
1752 | ab85ceb1 | Stefan Weil | |
1753 | ab85ceb1 | Stefan Weil | vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID); |
1754 | ab85ceb1 | Stefan Weil | device_id = pci_get_word(pdev->config + PCI_DEVICE_ID); |
1755 | ab85ceb1 | Stefan Weil | rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
|
1756 | ab85ceb1 | Stefan Weil | rom_device_id = pci_get_word(ptr + pcir_offset + 6);
|
1757 | ab85ceb1 | Stefan Weil | |
1758 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
|
1759 | ab85ceb1 | Stefan Weil | vendor_id, device_id, rom_vendor_id, rom_device_id); |
1760 | ab85ceb1 | Stefan Weil | |
1761 | ab85ceb1 | Stefan Weil | checksum = ptr[6];
|
1762 | ab85ceb1 | Stefan Weil | |
1763 | ab85ceb1 | Stefan Weil | if (vendor_id != rom_vendor_id) {
|
1764 | ab85ceb1 | Stefan Weil | /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
|
1765 | ab85ceb1 | Stefan Weil | checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
|
1766 | ab85ceb1 | Stefan Weil | checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
|
1767 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); |
1768 | ab85ceb1 | Stefan Weil | ptr[6] = checksum;
|
1769 | ab85ceb1 | Stefan Weil | pci_set_word(ptr + pcir_offset + 4, vendor_id);
|
1770 | ab85ceb1 | Stefan Weil | } |
1771 | ab85ceb1 | Stefan Weil | |
1772 | ab85ceb1 | Stefan Weil | if (device_id != rom_device_id) {
|
1773 | ab85ceb1 | Stefan Weil | /* Patch device id and checksum (at offset 6 for etherboot roms). */
|
1774 | ab85ceb1 | Stefan Weil | checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
|
1775 | ab85ceb1 | Stefan Weil | checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
|
1776 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); |
1777 | ab85ceb1 | Stefan Weil | ptr[6] = checksum;
|
1778 | ab85ceb1 | Stefan Weil | pci_set_word(ptr + pcir_offset + 6, device_id);
|
1779 | ab85ceb1 | Stefan Weil | } |
1780 | ab85ceb1 | Stefan Weil | } |
1781 | ab85ceb1 | Stefan Weil | |
1782 | c2039bd0 | Anthony Liguori | /* Add an option rom for the device */
|
1783 | ab85ceb1 | Stefan Weil | static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom) |
1784 | c2039bd0 | Anthony Liguori | { |
1785 | c2039bd0 | Anthony Liguori | int size;
|
1786 | c2039bd0 | Anthony Liguori | char *path;
|
1787 | c2039bd0 | Anthony Liguori | void *ptr;
|
1788 | 1724f049 | Alex Williamson | char name[32]; |
1789 | c2039bd0 | Anthony Liguori | |
1790 | 8c52c8f3 | Gerd Hoffmann | if (!pdev->romfile)
|
1791 | 8c52c8f3 | Gerd Hoffmann | return 0; |
1792 | 8c52c8f3 | Gerd Hoffmann | if (strlen(pdev->romfile) == 0) |
1793 | 8c52c8f3 | Gerd Hoffmann | return 0; |
1794 | 8c52c8f3 | Gerd Hoffmann | |
1795 | 88169ddf | Gerd Hoffmann | if (!pdev->rom_bar) {
|
1796 | 88169ddf | Gerd Hoffmann | /*
|
1797 | 88169ddf | Gerd Hoffmann | * Load rom via fw_cfg instead of creating a rom bar,
|
1798 | 88169ddf | Gerd Hoffmann | * for 0.11 compatibility.
|
1799 | 88169ddf | Gerd Hoffmann | */
|
1800 | 88169ddf | Gerd Hoffmann | int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
|
1801 | 88169ddf | Gerd Hoffmann | if (class == 0x0300) { |
1802 | 88169ddf | Gerd Hoffmann | rom_add_vga(pdev->romfile); |
1803 | 88169ddf | Gerd Hoffmann | } else {
|
1804 | 88169ddf | Gerd Hoffmann | rom_add_option(pdev->romfile); |
1805 | 88169ddf | Gerd Hoffmann | } |
1806 | 88169ddf | Gerd Hoffmann | return 0; |
1807 | 88169ddf | Gerd Hoffmann | } |
1808 | 88169ddf | Gerd Hoffmann | |
1809 | 8c52c8f3 | Gerd Hoffmann | path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); |
1810 | c2039bd0 | Anthony Liguori | if (path == NULL) { |
1811 | 8c52c8f3 | Gerd Hoffmann | path = qemu_strdup(pdev->romfile); |
1812 | c2039bd0 | Anthony Liguori | } |
1813 | c2039bd0 | Anthony Liguori | |
1814 | c2039bd0 | Anthony Liguori | size = get_image_size(path); |
1815 | 8c52c8f3 | Gerd Hoffmann | if (size < 0) { |
1816 | 1ecda02b | Markus Armbruster | error_report("%s: failed to find romfile \"%s\"",
|
1817 | 1ecda02b | Markus Armbruster | __FUNCTION__, pdev->romfile); |
1818 | 8c52c8f3 | Gerd Hoffmann | return -1; |
1819 | 8c52c8f3 | Gerd Hoffmann | } |
1820 | c2039bd0 | Anthony Liguori | if (size & (size - 1)) { |
1821 | c2039bd0 | Anthony Liguori | size = 1 << qemu_fls(size);
|
1822 | c2039bd0 | Anthony Liguori | } |
1823 | c2039bd0 | Anthony Liguori | |
1824 | 1724f049 | Alex Williamson | if (pdev->qdev.info->vmsd)
|
1825 | 1724f049 | Alex Williamson | snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name); |
1826 | 1724f049 | Alex Williamson | else
|
1827 | 1724f049 | Alex Williamson | snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name); |
1828 | 1724f049 | Alex Williamson | pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size); |
1829 | c2039bd0 | Anthony Liguori | |
1830 | c2039bd0 | Anthony Liguori | ptr = qemu_get_ram_ptr(pdev->rom_offset); |
1831 | c2039bd0 | Anthony Liguori | load_image(path, ptr); |
1832 | c2039bd0 | Anthony Liguori | qemu_free(path); |
1833 | c2039bd0 | Anthony Liguori | |
1834 | ab85ceb1 | Stefan Weil | if (is_default_rom) {
|
1835 | ab85ceb1 | Stefan Weil | /* Only the default rom images will be patched (if needed). */
|
1836 | ab85ceb1 | Stefan Weil | pci_patch_ids(pdev, ptr, size); |
1837 | ab85ceb1 | Stefan Weil | } |
1838 | ab85ceb1 | Stefan Weil | |
1839 | c2039bd0 | Anthony Liguori | pci_register_bar(pdev, PCI_ROM_SLOT, size, |
1840 | c2039bd0 | Anthony Liguori | 0, pci_map_option_rom);
|
1841 | c2039bd0 | Anthony Liguori | |
1842 | c2039bd0 | Anthony Liguori | return 0; |
1843 | c2039bd0 | Anthony Liguori | } |
1844 | c2039bd0 | Anthony Liguori | |
1845 | 230741dc | Alex Williamson | static void pci_del_option_rom(PCIDevice *pdev) |
1846 | 230741dc | Alex Williamson | { |
1847 | 230741dc | Alex Williamson | if (!pdev->rom_offset)
|
1848 | 230741dc | Alex Williamson | return;
|
1849 | 230741dc | Alex Williamson | |
1850 | 230741dc | Alex Williamson | qemu_ram_free(pdev->rom_offset); |
1851 | 230741dc | Alex Williamson | pdev->rom_offset = 0;
|
1852 | 230741dc | Alex Williamson | } |
1853 | 230741dc | Alex Williamson | |
1854 | ca77089d | Isaku Yamahata | /*
|
1855 | ca77089d | Isaku Yamahata | * if !offset
|
1856 | ca77089d | Isaku Yamahata | * Reserve space and add capability to the linked list in pci config space
|
1857 | ca77089d | Isaku Yamahata | *
|
1858 | ca77089d | Isaku Yamahata | * if offset = 0,
|
1859 | ca77089d | Isaku Yamahata | * Find and reserve space and add capability to the linked list
|
1860 | ca77089d | Isaku Yamahata | * in pci config space */
|
1861 | ca77089d | Isaku Yamahata | int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
|
1862 | ca77089d | Isaku Yamahata | uint8_t offset, uint8_t size) |
1863 | 6f4cbd39 | Michael S. Tsirkin | { |
1864 | ca77089d | Isaku Yamahata | uint8_t *config; |
1865 | ca77089d | Isaku Yamahata | if (!offset) {
|
1866 | ca77089d | Isaku Yamahata | offset = pci_find_space(pdev, size); |
1867 | ca77089d | Isaku Yamahata | if (!offset) {
|
1868 | ca77089d | Isaku Yamahata | return -ENOSPC;
|
1869 | ca77089d | Isaku Yamahata | } |
1870 | ca77089d | Isaku Yamahata | } |
1871 | ca77089d | Isaku Yamahata | |
1872 | ca77089d | Isaku Yamahata | config = pdev->config + offset; |
1873 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_ID] = cap_id; |
1874 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
1875 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_CAPABILITY_LIST] = offset; |
1876 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
1877 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0xFF, size);
|
1878 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability read-only by default */
|
1879 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0, size);
|
1880 | bd4b65ee | Michael S. Tsirkin | /* Check capability by default */
|
1881 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0xFF, size);
|
1882 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1883 | 6f4cbd39 | Michael S. Tsirkin | } |
1884 | 6f4cbd39 | Michael S. Tsirkin | |
1885 | 6f4cbd39 | Michael S. Tsirkin | /* Unlink capability from the pci config space. */
|
1886 | 6f4cbd39 | Michael S. Tsirkin | void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1887 | 6f4cbd39 | Michael S. Tsirkin | { |
1888 | 6f4cbd39 | Michael S. Tsirkin | uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
1889 | 6f4cbd39 | Michael S. Tsirkin | if (!offset)
|
1890 | 6f4cbd39 | Michael S. Tsirkin | return;
|
1891 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
1892 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability writeable again */
|
1893 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0xff, size);
|
1894 | 1a4f5971 | Isaku Yamahata | memset(pdev->w1cmask + offset, 0, size);
|
1895 | bd4b65ee | Michael S. Tsirkin | /* Clear cmask as device-specific registers can't be checked */
|
1896 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0, size);
|
1897 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0, size);
|
1898 | 6f4cbd39 | Michael S. Tsirkin | |
1899 | 6f4cbd39 | Michael S. Tsirkin | if (!pdev->config[PCI_CAPABILITY_LIST])
|
1900 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
1901 | 6f4cbd39 | Michael S. Tsirkin | } |
1902 | 6f4cbd39 | Michael S. Tsirkin | |
1903 | 6f4cbd39 | Michael S. Tsirkin | /* Reserve space for capability at a known offset (to call after load). */
|
1904 | 6f4cbd39 | Michael S. Tsirkin | void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
|
1905 | 6f4cbd39 | Michael S. Tsirkin | { |
1906 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0xff, size);
|
1907 | 6f4cbd39 | Michael S. Tsirkin | } |
1908 | 6f4cbd39 | Michael S. Tsirkin | |
1909 | 6f4cbd39 | Michael S. Tsirkin | uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
1910 | 6f4cbd39 | Michael S. Tsirkin | { |
1911 | 6f4cbd39 | Michael S. Tsirkin | return pci_find_capability_list(pdev, cap_id, NULL); |
1912 | 6f4cbd39 | Michael S. Tsirkin | } |
1913 | 10c4c98a | Gerd Hoffmann | |
1914 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
1915 | 10c4c98a | Gerd Hoffmann | { |
1916 | 10c4c98a | Gerd Hoffmann | PCIDevice *d = (PCIDevice *)dev; |
1917 | 10c4c98a | Gerd Hoffmann | const pci_class_desc *desc;
|
1918 | 10c4c98a | Gerd Hoffmann | char ctxt[64]; |
1919 | 10c4c98a | Gerd Hoffmann | PCIIORegion *r; |
1920 | 10c4c98a | Gerd Hoffmann | int i, class;
|
1921 | 10c4c98a | Gerd Hoffmann | |
1922 | b0ff8eb2 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1923 | 10c4c98a | Gerd Hoffmann | desc = pci_class_descriptions; |
1924 | 10c4c98a | Gerd Hoffmann | while (desc->desc && class != desc->class)
|
1925 | 10c4c98a | Gerd Hoffmann | desc++; |
1926 | 10c4c98a | Gerd Hoffmann | if (desc->desc) {
|
1927 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
1928 | 10c4c98a | Gerd Hoffmann | } else {
|
1929 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
1930 | 10c4c98a | Gerd Hoffmann | } |
1931 | 10c4c98a | Gerd Hoffmann | |
1932 | 10c4c98a | Gerd Hoffmann | monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
1933 | 10c4c98a | Gerd Hoffmann | "pci id %04x:%04x (sub %04x:%04x)\n",
|
1934 | 7f5feab4 | Alex Williamson | indent, "", ctxt, pci_bus_num(d->bus),
|
1935 | e822a52a | Isaku Yamahata | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
1936 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_VENDOR_ID), |
1937 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_DEVICE_ID), |
1938 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
1939 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
1940 | 10c4c98a | Gerd Hoffmann | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1941 | 10c4c98a | Gerd Hoffmann | r = &d->io_regions[i]; |
1942 | 10c4c98a | Gerd Hoffmann | if (!r->size)
|
1943 | 10c4c98a | Gerd Hoffmann | continue;
|
1944 | 89e8b13c | Isaku Yamahata | monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
1945 | 89e8b13c | Isaku Yamahata | " [0x%"FMT_PCIBUS"]\n", |
1946 | 89e8b13c | Isaku Yamahata | indent, "",
|
1947 | 0392a017 | Isaku Yamahata | i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
1948 | 10c4c98a | Gerd Hoffmann | r->addr, r->addr + r->size - 1);
|
1949 | 10c4c98a | Gerd Hoffmann | } |
1950 | 10c4c98a | Gerd Hoffmann | } |
1951 | 03587182 | Gerd Hoffmann | |
1952 | 4f43c1ff | Alex Williamson | static char *pcibus_get_dev_path(DeviceState *dev) |
1953 | 4f43c1ff | Alex Williamson | { |
1954 | 4f43c1ff | Alex Williamson | PCIDevice *d = (PCIDevice *)dev; |
1955 | 4f43c1ff | Alex Williamson | char path[16]; |
1956 | 4f43c1ff | Alex Williamson | |
1957 | 4f43c1ff | Alex Williamson | snprintf(path, sizeof(path), "%04x:%02x:%02x.%x", |
1958 | 4f43c1ff | Alex Williamson | pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS], |
1959 | 4f43c1ff | Alex Williamson | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); |
1960 | 4f43c1ff | Alex Williamson | |
1961 | 4f43c1ff | Alex Williamson | return strdup(path);
|
1962 | 4f43c1ff | Alex Williamson | } |