root / hw / pci.c @ 1062977b
History | View | Annotate | Download (43 kB)
1 |
/*
|
---|---|
2 |
* QEMU PCI bus manager
|
3 |
*
|
4 |
* Copyright (c) 2004 Fabrice Bellard
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "hw.h" |
25 |
#include "pci.h" |
26 |
#include "monitor.h" |
27 |
#include "net.h" |
28 |
#include "sysemu.h" |
29 |
|
30 |
//#define DEBUG_PCI
|
31 |
#ifdef DEBUG_PCI
|
32 |
# define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
33 |
#else
|
34 |
# define PCI_DPRINTF(format, ...) do { } while (0) |
35 |
#endif
|
36 |
|
37 |
struct PCIBus {
|
38 |
BusState qbus; |
39 |
int devfn_min;
|
40 |
pci_set_irq_fn set_irq; |
41 |
pci_map_irq_fn map_irq; |
42 |
pci_hotplug_fn hotplug; |
43 |
uint32_t config_reg; /* XXX: suppress */
|
44 |
void *irq_opaque;
|
45 |
PCIDevice *devices[256];
|
46 |
PCIDevice *parent_dev; |
47 |
|
48 |
QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
|
49 |
QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
|
50 |
|
51 |
/* The bus IRQ state is the logical OR of the connected devices.
|
52 |
Keep a count of the number of devices with raised IRQs. */
|
53 |
int nirq;
|
54 |
int *irq_count;
|
55 |
}; |
56 |
|
57 |
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
58 |
|
59 |
static struct BusInfo pci_bus_info = { |
60 |
.name = "PCI",
|
61 |
.size = sizeof(PCIBus),
|
62 |
.print_dev = pcibus_dev_print, |
63 |
.props = (Property[]) { |
64 |
DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
65 |
DEFINE_PROP_END_OF_LIST() |
66 |
} |
67 |
}; |
68 |
|
69 |
static void pci_update_mappings(PCIDevice *d); |
70 |
static void pci_set_irq(void *opaque, int irq_num, int level); |
71 |
|
72 |
target_phys_addr_t pci_mem_base; |
73 |
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
74 |
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
75 |
|
76 |
struct PCIHostBus {
|
77 |
int domain;
|
78 |
struct PCIBus *bus;
|
79 |
QLIST_ENTRY(PCIHostBus) next; |
80 |
}; |
81 |
static QLIST_HEAD(, PCIHostBus) host_buses;
|
82 |
|
83 |
static const VMStateDescription vmstate_pcibus = { |
84 |
.name = "PCIBUS",
|
85 |
.version_id = 1,
|
86 |
.minimum_version_id = 1,
|
87 |
.minimum_version_id_old = 1,
|
88 |
.fields = (VMStateField []) { |
89 |
VMSTATE_INT32_EQUAL(nirq, PCIBus), |
90 |
VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
91 |
VMSTATE_END_OF_LIST() |
92 |
} |
93 |
}; |
94 |
|
95 |
static int pci_bar(PCIDevice *d, int reg) |
96 |
{ |
97 |
uint8_t type; |
98 |
|
99 |
if (reg != PCI_ROM_SLOT)
|
100 |
return PCI_BASE_ADDRESS_0 + reg * 4; |
101 |
|
102 |
type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
103 |
return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
104 |
} |
105 |
|
106 |
static void pci_device_reset(PCIDevice *dev) |
107 |
{ |
108 |
int r;
|
109 |
|
110 |
memset(dev->irq_state, 0, sizeof dev->irq_state); |
111 |
dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
112 |
PCI_COMMAND_MASTER); |
113 |
dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
114 |
dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
115 |
for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
116 |
if (!dev->io_regions[r].size) {
|
117 |
continue;
|
118 |
} |
119 |
pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type); |
120 |
} |
121 |
pci_update_mappings(dev); |
122 |
} |
123 |
|
124 |
static void pci_bus_reset(void *opaque) |
125 |
{ |
126 |
PCIBus *bus = opaque; |
127 |
int i;
|
128 |
|
129 |
for (i = 0; i < bus->nirq; i++) { |
130 |
bus->irq_count[i] = 0;
|
131 |
} |
132 |
for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
133 |
if (bus->devices[i]) {
|
134 |
pci_device_reset(bus->devices[i]); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
static void pci_host_bus_register(int domain, PCIBus *bus) |
140 |
{ |
141 |
struct PCIHostBus *host;
|
142 |
host = qemu_mallocz(sizeof(*host));
|
143 |
host->domain = domain; |
144 |
host->bus = bus; |
145 |
QLIST_INSERT_HEAD(&host_buses, host, next); |
146 |
} |
147 |
|
148 |
PCIBus *pci_find_root_bus(int domain)
|
149 |
{ |
150 |
struct PCIHostBus *host;
|
151 |
|
152 |
QLIST_FOREACH(host, &host_buses, next) { |
153 |
if (host->domain == domain) {
|
154 |
return host->bus;
|
155 |
} |
156 |
} |
157 |
|
158 |
return NULL; |
159 |
} |
160 |
|
161 |
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
162 |
const char *name, int devfn_min) |
163 |
{ |
164 |
static int nbus = 0; |
165 |
|
166 |
qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name); |
167 |
bus->devfn_min = devfn_min; |
168 |
|
169 |
/* host bridge */
|
170 |
QLIST_INIT(&bus->child); |
171 |
pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
172 |
|
173 |
vmstate_register(nbus++, &vmstate_pcibus, bus); |
174 |
qemu_register_reset(pci_bus_reset, bus); |
175 |
} |
176 |
|
177 |
PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min) |
178 |
{ |
179 |
PCIBus *bus; |
180 |
|
181 |
bus = qemu_mallocz(sizeof(*bus));
|
182 |
bus->qbus.qdev_allocated = 1;
|
183 |
pci_bus_new_inplace(bus, parent, name, devfn_min); |
184 |
return bus;
|
185 |
} |
186 |
|
187 |
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
188 |
void *irq_opaque, int nirq) |
189 |
{ |
190 |
bus->set_irq = set_irq; |
191 |
bus->map_irq = map_irq; |
192 |
bus->irq_opaque = irq_opaque; |
193 |
bus->nirq = nirq; |
194 |
bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0])); |
195 |
} |
196 |
|
197 |
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
|
198 |
{ |
199 |
bus->qbus.allow_hotplug = 1;
|
200 |
bus->hotplug = hotplug; |
201 |
} |
202 |
|
203 |
PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
204 |
pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
205 |
void *irq_opaque, int devfn_min, int nirq) |
206 |
{ |
207 |
PCIBus *bus; |
208 |
|
209 |
bus = pci_bus_new(parent, name, devfn_min); |
210 |
pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
211 |
return bus;
|
212 |
} |
213 |
|
214 |
static void pci_register_secondary_bus(PCIBus *parent, |
215 |
PCIBus *bus, |
216 |
PCIDevice *dev, |
217 |
pci_map_irq_fn map_irq, |
218 |
const char *name) |
219 |
{ |
220 |
qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name); |
221 |
bus->map_irq = map_irq; |
222 |
bus->parent_dev = dev; |
223 |
|
224 |
QLIST_INIT(&bus->child); |
225 |
QLIST_INSERT_HEAD(&parent->child, bus, sibling); |
226 |
} |
227 |
|
228 |
static void pci_unregister_secondary_bus(PCIBus *bus) |
229 |
{ |
230 |
assert(QLIST_EMPTY(&bus->child)); |
231 |
QLIST_REMOVE(bus, sibling); |
232 |
} |
233 |
|
234 |
int pci_bus_num(PCIBus *s)
|
235 |
{ |
236 |
if (!s->parent_dev)
|
237 |
return 0; /* pci host bridge */ |
238 |
return s->parent_dev->config[PCI_SECONDARY_BUS];
|
239 |
} |
240 |
|
241 |
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
242 |
{ |
243 |
PCIDevice *s = container_of(pv, PCIDevice, config); |
244 |
uint8_t *config; |
245 |
int i;
|
246 |
|
247 |
assert(size == pci_config_size(s)); |
248 |
config = qemu_malloc(size); |
249 |
|
250 |
qemu_get_buffer(f, config, size); |
251 |
for (i = 0; i < size; ++i) { |
252 |
if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
|
253 |
qemu_free(config); |
254 |
return -EINVAL;
|
255 |
} |
256 |
} |
257 |
memcpy(s->config, config, size); |
258 |
|
259 |
pci_update_mappings(s); |
260 |
|
261 |
qemu_free(config); |
262 |
return 0; |
263 |
} |
264 |
|
265 |
/* just put buffer */
|
266 |
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
267 |
{ |
268 |
const uint8_t **v = pv;
|
269 |
assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
270 |
qemu_put_buffer(f, *v, size); |
271 |
} |
272 |
|
273 |
static VMStateInfo vmstate_info_pci_config = {
|
274 |
.name = "pci config",
|
275 |
.get = get_pci_config_device, |
276 |
.put = put_pci_config_device, |
277 |
}; |
278 |
|
279 |
const VMStateDescription vmstate_pci_device = {
|
280 |
.name = "PCIDevice",
|
281 |
.version_id = 2,
|
282 |
.minimum_version_id = 1,
|
283 |
.minimum_version_id_old = 1,
|
284 |
.fields = (VMStateField []) { |
285 |
VMSTATE_INT32_LE(version_id, PCIDevice), |
286 |
VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
287 |
vmstate_info_pci_config, |
288 |
PCI_CONFIG_SPACE_SIZE), |
289 |
VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
|
290 |
VMSTATE_END_OF_LIST() |
291 |
} |
292 |
}; |
293 |
|
294 |
const VMStateDescription vmstate_pcie_device = {
|
295 |
.name = "PCIDevice",
|
296 |
.version_id = 2,
|
297 |
.minimum_version_id = 1,
|
298 |
.minimum_version_id_old = 1,
|
299 |
.fields = (VMStateField []) { |
300 |
VMSTATE_INT32_LE(version_id, PCIDevice), |
301 |
VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
302 |
vmstate_info_pci_config, |
303 |
PCIE_CONFIG_SPACE_SIZE), |
304 |
VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
|
305 |
VMSTATE_END_OF_LIST() |
306 |
} |
307 |
}; |
308 |
|
309 |
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
310 |
{ |
311 |
return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
312 |
} |
313 |
|
314 |
void pci_device_save(PCIDevice *s, QEMUFile *f)
|
315 |
{ |
316 |
vmstate_save_state(f, pci_get_vmstate(s), s); |
317 |
} |
318 |
|
319 |
int pci_device_load(PCIDevice *s, QEMUFile *f)
|
320 |
{ |
321 |
return vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
|
322 |
} |
323 |
|
324 |
static int pci_set_default_subsystem_id(PCIDevice *pci_dev) |
325 |
{ |
326 |
uint16_t *id; |
327 |
|
328 |
id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
|
329 |
id[0] = cpu_to_le16(pci_default_sub_vendor_id);
|
330 |
id[1] = cpu_to_le16(pci_default_sub_device_id);
|
331 |
return 0; |
332 |
} |
333 |
|
334 |
/*
|
335 |
* Parse [[<domain>:]<bus>:]<slot>, return -1 on error
|
336 |
*/
|
337 |
static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp) |
338 |
{ |
339 |
const char *p; |
340 |
char *e;
|
341 |
unsigned long val; |
342 |
unsigned long dom = 0, bus = 0; |
343 |
unsigned slot = 0; |
344 |
|
345 |
p = addr; |
346 |
val = strtoul(p, &e, 16);
|
347 |
if (e == p)
|
348 |
return -1; |
349 |
if (*e == ':') { |
350 |
bus = val; |
351 |
p = e + 1;
|
352 |
val = strtoul(p, &e, 16);
|
353 |
if (e == p)
|
354 |
return -1; |
355 |
if (*e == ':') { |
356 |
dom = bus; |
357 |
bus = val; |
358 |
p = e + 1;
|
359 |
val = strtoul(p, &e, 16);
|
360 |
if (e == p)
|
361 |
return -1; |
362 |
} |
363 |
} |
364 |
|
365 |
if (dom > 0xffff || bus > 0xff || val > 0x1f) |
366 |
return -1; |
367 |
|
368 |
slot = val; |
369 |
|
370 |
if (*e)
|
371 |
return -1; |
372 |
|
373 |
/* Note: QEMU doesn't implement domains other than 0 */
|
374 |
if (!pci_find_bus(pci_find_root_bus(dom), bus))
|
375 |
return -1; |
376 |
|
377 |
*domp = dom; |
378 |
*busp = bus; |
379 |
*slotp = slot; |
380 |
return 0; |
381 |
} |
382 |
|
383 |
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp, |
384 |
unsigned *slotp)
|
385 |
{ |
386 |
/* strip legacy tag */
|
387 |
if (!strncmp(addr, "pci_addr=", 9)) { |
388 |
addr += 9;
|
389 |
} |
390 |
if (pci_parse_devaddr(addr, domp, busp, slotp)) {
|
391 |
monitor_printf(mon, "Invalid pci address\n");
|
392 |
return -1; |
393 |
} |
394 |
return 0; |
395 |
} |
396 |
|
397 |
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr) |
398 |
{ |
399 |
int dom, bus;
|
400 |
unsigned slot;
|
401 |
|
402 |
if (!devaddr) {
|
403 |
*devfnp = -1;
|
404 |
return pci_find_bus(pci_find_root_bus(0), 0); |
405 |
} |
406 |
|
407 |
if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) { |
408 |
return NULL; |
409 |
} |
410 |
|
411 |
*devfnp = slot << 3;
|
412 |
return pci_find_bus(pci_find_root_bus(0), bus); |
413 |
} |
414 |
|
415 |
static void pci_init_cmask(PCIDevice *dev) |
416 |
{ |
417 |
pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
418 |
pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
419 |
dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
420 |
dev->cmask[PCI_REVISION_ID] = 0xff;
|
421 |
dev->cmask[PCI_CLASS_PROG] = 0xff;
|
422 |
pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
423 |
dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
424 |
dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
425 |
} |
426 |
|
427 |
static void pci_init_wmask(PCIDevice *dev) |
428 |
{ |
429 |
int config_size = pci_config_size(dev);
|
430 |
|
431 |
dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
432 |
dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
433 |
pci_set_word(dev->wmask + PCI_COMMAND, |
434 |
PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
435 |
|
436 |
memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
|
437 |
config_size - PCI_CONFIG_HEADER_SIZE); |
438 |
} |
439 |
|
440 |
static void pci_init_wmask_bridge(PCIDevice *d) |
441 |
{ |
442 |
/* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
|
443 |
PCI_SEC_LETENCY_TIMER */
|
444 |
memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); |
445 |
|
446 |
/* base and limit */
|
447 |
d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
|
448 |
d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
|
449 |
pci_set_word(d->wmask + PCI_MEMORY_BASE, |
450 |
PCI_MEMORY_RANGE_MASK & 0xffff);
|
451 |
pci_set_word(d->wmask + PCI_MEMORY_LIMIT, |
452 |
PCI_MEMORY_RANGE_MASK & 0xffff);
|
453 |
pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, |
454 |
PCI_PREF_RANGE_MASK & 0xffff);
|
455 |
pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, |
456 |
PCI_PREF_RANGE_MASK & 0xffff);
|
457 |
|
458 |
/* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
|
459 |
memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); |
460 |
|
461 |
pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
|
462 |
} |
463 |
|
464 |
static void pci_config_alloc(PCIDevice *pci_dev) |
465 |
{ |
466 |
int config_size = pci_config_size(pci_dev);
|
467 |
|
468 |
pci_dev->config = qemu_mallocz(config_size); |
469 |
pci_dev->cmask = qemu_mallocz(config_size); |
470 |
pci_dev->wmask = qemu_mallocz(config_size); |
471 |
pci_dev->used = qemu_mallocz(config_size); |
472 |
} |
473 |
|
474 |
static void pci_config_free(PCIDevice *pci_dev) |
475 |
{ |
476 |
qemu_free(pci_dev->config); |
477 |
qemu_free(pci_dev->cmask); |
478 |
qemu_free(pci_dev->wmask); |
479 |
qemu_free(pci_dev->used); |
480 |
} |
481 |
|
482 |
/* -1 for devfn means auto assign */
|
483 |
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
484 |
const char *name, int devfn, |
485 |
PCIConfigReadFunc *config_read, |
486 |
PCIConfigWriteFunc *config_write, |
487 |
uint8_t header_type) |
488 |
{ |
489 |
if (devfn < 0) { |
490 |
for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
|
491 |
devfn += 8) {
|
492 |
if (!bus->devices[devfn])
|
493 |
goto found;
|
494 |
} |
495 |
hw_error("PCI: no devfn available for %s, all in use\n", name);
|
496 |
found: ;
|
497 |
} else if (bus->devices[devfn]) { |
498 |
hw_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
|
499 |
name, bus->devices[devfn]->name); |
500 |
} |
501 |
pci_dev->bus = bus; |
502 |
pci_dev->devfn = devfn; |
503 |
pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
504 |
memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state)); |
505 |
pci_config_alloc(pci_dev); |
506 |
|
507 |
header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
508 |
if (header_type == PCI_HEADER_TYPE_NORMAL) {
|
509 |
pci_set_default_subsystem_id(pci_dev); |
510 |
} |
511 |
pci_init_cmask(pci_dev); |
512 |
pci_init_wmask(pci_dev); |
513 |
if (header_type == PCI_HEADER_TYPE_BRIDGE) {
|
514 |
pci_init_wmask_bridge(pci_dev); |
515 |
} |
516 |
|
517 |
if (!config_read)
|
518 |
config_read = pci_default_read_config; |
519 |
if (!config_write)
|
520 |
config_write = pci_default_write_config; |
521 |
pci_dev->config_read = config_read; |
522 |
pci_dev->config_write = config_write; |
523 |
bus->devices[devfn] = pci_dev; |
524 |
pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
525 |
pci_dev->version_id = 2; /* Current pci device vmstate version */ |
526 |
return pci_dev;
|
527 |
} |
528 |
|
529 |
PCIDevice *pci_register_device(PCIBus *bus, const char *name, |
530 |
int instance_size, int devfn, |
531 |
PCIConfigReadFunc *config_read, |
532 |
PCIConfigWriteFunc *config_write) |
533 |
{ |
534 |
PCIDevice *pci_dev; |
535 |
|
536 |
pci_dev = qemu_mallocz(instance_size); |
537 |
pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, |
538 |
config_read, config_write, |
539 |
PCI_HEADER_TYPE_NORMAL); |
540 |
return pci_dev;
|
541 |
} |
542 |
static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
|
543 |
{ |
544 |
return addr + pci_mem_base;
|
545 |
} |
546 |
|
547 |
static void pci_unregister_io_regions(PCIDevice *pci_dev) |
548 |
{ |
549 |
PCIIORegion *r; |
550 |
int i;
|
551 |
|
552 |
for(i = 0; i < PCI_NUM_REGIONS; i++) { |
553 |
r = &pci_dev->io_regions[i]; |
554 |
if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
555 |
continue;
|
556 |
if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
|
557 |
isa_unassign_ioport(r->addr, r->filtered_size); |
558 |
} else {
|
559 |
cpu_register_physical_memory(pci_to_cpu_addr(r->addr), |
560 |
r->filtered_size, |
561 |
IO_MEM_UNASSIGNED); |
562 |
} |
563 |
} |
564 |
} |
565 |
|
566 |
static int pci_unregister_device(DeviceState *dev) |
567 |
{ |
568 |
PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev); |
569 |
PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info); |
570 |
int ret = 0; |
571 |
|
572 |
if (info->exit)
|
573 |
ret = info->exit(pci_dev); |
574 |
if (ret)
|
575 |
return ret;
|
576 |
|
577 |
pci_unregister_io_regions(pci_dev); |
578 |
|
579 |
qemu_free_irqs(pci_dev->irq); |
580 |
pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
581 |
pci_config_free(pci_dev); |
582 |
return 0; |
583 |
} |
584 |
|
585 |
void pci_register_bar(PCIDevice *pci_dev, int region_num, |
586 |
pcibus_t size, int type,
|
587 |
PCIMapIORegionFunc *map_func) |
588 |
{ |
589 |
PCIIORegion *r; |
590 |
uint32_t addr; |
591 |
pcibus_t wmask; |
592 |
|
593 |
if ((unsigned int)region_num >= PCI_NUM_REGIONS) |
594 |
return;
|
595 |
|
596 |
if (size & (size-1)) { |
597 |
fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
598 |
"type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
599 |
exit(1);
|
600 |
} |
601 |
|
602 |
r = &pci_dev->io_regions[region_num]; |
603 |
r->addr = PCI_BAR_UNMAPPED; |
604 |
r->size = size; |
605 |
r->filtered_size = size; |
606 |
r->type = type; |
607 |
r->map_func = map_func; |
608 |
|
609 |
wmask = ~(size - 1);
|
610 |
addr = pci_bar(pci_dev, region_num); |
611 |
if (region_num == PCI_ROM_SLOT) {
|
612 |
/* ROM enable bit is writeable */
|
613 |
wmask |= PCI_ROM_ADDRESS_ENABLE; |
614 |
} |
615 |
pci_set_long(pci_dev->config + addr, type); |
616 |
if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
617 |
r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
618 |
pci_set_quad(pci_dev->wmask + addr, wmask); |
619 |
pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
620 |
} else {
|
621 |
pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
622 |
pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
623 |
} |
624 |
} |
625 |
|
626 |
static uint32_t pci_config_get_io_base(PCIDevice *d,
|
627 |
uint32_t base, uint32_t base_upper16) |
628 |
{ |
629 |
uint32_t val; |
630 |
|
631 |
val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
|
632 |
if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
|
633 |
val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
|
634 |
} |
635 |
return val;
|
636 |
} |
637 |
|
638 |
static pcibus_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
|
639 |
{ |
640 |
return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
|
641 |
<< 16;
|
642 |
} |
643 |
|
644 |
static pcibus_t pci_config_get_pref_base(PCIDevice *d,
|
645 |
uint32_t base, uint32_t upper) |
646 |
{ |
647 |
pcibus_t tmp; |
648 |
pcibus_t val; |
649 |
|
650 |
tmp = (pcibus_t)pci_get_word(d->config + base); |
651 |
val = (tmp & PCI_PREF_RANGE_MASK) << 16;
|
652 |
if (tmp & PCI_PREF_RANGE_TYPE_64) {
|
653 |
val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
|
654 |
} |
655 |
return val;
|
656 |
} |
657 |
|
658 |
static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
|
659 |
{ |
660 |
pcibus_t base; |
661 |
if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
662 |
base = pci_config_get_io_base(bridge, |
663 |
PCI_IO_BASE, PCI_IO_BASE_UPPER16); |
664 |
} else {
|
665 |
if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
|
666 |
base = pci_config_get_pref_base( |
667 |
bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32); |
668 |
} else {
|
669 |
base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE); |
670 |
} |
671 |
} |
672 |
|
673 |
return base;
|
674 |
} |
675 |
|
676 |
static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
|
677 |
{ |
678 |
pcibus_t limit; |
679 |
if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
680 |
limit = pci_config_get_io_base(bridge, |
681 |
PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16); |
682 |
limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */ |
683 |
} else {
|
684 |
if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
|
685 |
limit = pci_config_get_pref_base( |
686 |
bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32); |
687 |
} else {
|
688 |
limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT); |
689 |
} |
690 |
limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */ |
691 |
} |
692 |
return limit;
|
693 |
} |
694 |
|
695 |
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size, |
696 |
uint8_t type) |
697 |
{ |
698 |
pcibus_t base = *addr; |
699 |
pcibus_t limit = *addr + *size - 1;
|
700 |
PCIDevice *br; |
701 |
|
702 |
for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
|
703 |
uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
704 |
|
705 |
if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
706 |
if (!(cmd & PCI_COMMAND_IO)) {
|
707 |
goto no_map;
|
708 |
} |
709 |
} else {
|
710 |
if (!(cmd & PCI_COMMAND_MEMORY)) {
|
711 |
goto no_map;
|
712 |
} |
713 |
} |
714 |
|
715 |
base = MAX(base, pci_bridge_get_base(br, type)); |
716 |
limit = MIN(limit, pci_bridge_get_limit(br, type)); |
717 |
} |
718 |
|
719 |
if (base > limit) {
|
720 |
goto no_map;
|
721 |
} |
722 |
*addr = base; |
723 |
*size = limit - base + 1;
|
724 |
return;
|
725 |
no_map:
|
726 |
*addr = PCI_BAR_UNMAPPED; |
727 |
*size = 0;
|
728 |
} |
729 |
|
730 |
static pcibus_t pci_bar_address(PCIDevice *d,
|
731 |
int reg, uint8_t type, pcibus_t size)
|
732 |
{ |
733 |
pcibus_t new_addr, last_addr; |
734 |
int bar = pci_bar(d, reg);
|
735 |
uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
736 |
|
737 |
if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
738 |
if (!(cmd & PCI_COMMAND_IO)) {
|
739 |
return PCI_BAR_UNMAPPED;
|
740 |
} |
741 |
new_addr = pci_get_long(d->config + bar) & ~(size - 1);
|
742 |
last_addr = new_addr + size - 1;
|
743 |
/* NOTE: we have only 64K ioports on PC */
|
744 |
if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) { |
745 |
return PCI_BAR_UNMAPPED;
|
746 |
} |
747 |
return new_addr;
|
748 |
} |
749 |
|
750 |
if (!(cmd & PCI_COMMAND_MEMORY)) {
|
751 |
return PCI_BAR_UNMAPPED;
|
752 |
} |
753 |
if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
754 |
new_addr = pci_get_quad(d->config + bar); |
755 |
} else {
|
756 |
new_addr = pci_get_long(d->config + bar); |
757 |
} |
758 |
/* the ROM slot has a specific enable bit */
|
759 |
if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
|
760 |
return PCI_BAR_UNMAPPED;
|
761 |
} |
762 |
new_addr &= ~(size - 1);
|
763 |
last_addr = new_addr + size - 1;
|
764 |
/* NOTE: we do not support wrapping */
|
765 |
/* XXX: as we cannot support really dynamic
|
766 |
mappings, we handle specific values as invalid
|
767 |
mappings. */
|
768 |
if (last_addr <= new_addr || new_addr == 0 || |
769 |
last_addr == PCI_BAR_UNMAPPED) { |
770 |
return PCI_BAR_UNMAPPED;
|
771 |
} |
772 |
|
773 |
/* Now pcibus_t is 64bit.
|
774 |
* Check if 32 bit BAR wraps around explicitly.
|
775 |
* Without this, PC ide doesn't work well.
|
776 |
* TODO: remove this work around.
|
777 |
*/
|
778 |
if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
|
779 |
return PCI_BAR_UNMAPPED;
|
780 |
} |
781 |
|
782 |
/*
|
783 |
* OS is allowed to set BAR beyond its addressable
|
784 |
* bits. For example, 32 bit OS can set 64bit bar
|
785 |
* to >4G. Check it. TODO: we might need to support
|
786 |
* it in the future for e.g. PAE.
|
787 |
*/
|
788 |
if (last_addr >= TARGET_PHYS_ADDR_MAX) {
|
789 |
return PCI_BAR_UNMAPPED;
|
790 |
} |
791 |
|
792 |
return new_addr;
|
793 |
} |
794 |
|
795 |
static void pci_update_mappings(PCIDevice *d) |
796 |
{ |
797 |
PCIIORegion *r; |
798 |
int i;
|
799 |
pcibus_t new_addr, filtered_size; |
800 |
|
801 |
for(i = 0; i < PCI_NUM_REGIONS; i++) { |
802 |
r = &d->io_regions[i]; |
803 |
|
804 |
/* this region isn't registered */
|
805 |
if (!r->size)
|
806 |
continue;
|
807 |
|
808 |
new_addr = pci_bar_address(d, i, r->type, r->size); |
809 |
|
810 |
/* bridge filtering */
|
811 |
filtered_size = r->size; |
812 |
if (new_addr != PCI_BAR_UNMAPPED) {
|
813 |
pci_bridge_filter(d, &new_addr, &filtered_size, r->type); |
814 |
} |
815 |
|
816 |
/* This bar isn't changed */
|
817 |
if (new_addr == r->addr && filtered_size == r->filtered_size)
|
818 |
continue;
|
819 |
|
820 |
/* now do the real mapping */
|
821 |
if (r->addr != PCI_BAR_UNMAPPED) {
|
822 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
823 |
int class;
|
824 |
/* NOTE: specific hack for IDE in PC case:
|
825 |
only one byte must be mapped. */
|
826 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
827 |
if (class == 0x0101 && r->size == 4) { |
828 |
isa_unassign_ioport(r->addr + 2, 1); |
829 |
} else {
|
830 |
isa_unassign_ioport(r->addr, r->filtered_size); |
831 |
} |
832 |
} else {
|
833 |
cpu_register_physical_memory(pci_to_cpu_addr(r->addr), |
834 |
r->filtered_size, |
835 |
IO_MEM_UNASSIGNED); |
836 |
qemu_unregister_coalesced_mmio(r->addr, r->filtered_size); |
837 |
} |
838 |
} |
839 |
r->addr = new_addr; |
840 |
r->filtered_size = filtered_size; |
841 |
if (r->addr != PCI_BAR_UNMAPPED) {
|
842 |
/*
|
843 |
* TODO: currently almost all the map funcions assumes
|
844 |
* filtered_size == size and addr & ~(size - 1) == addr.
|
845 |
* However with bridge filtering, they aren't always true.
|
846 |
* Teach them such cases, such that filtered_size < size and
|
847 |
* addr & (size - 1) != 0.
|
848 |
*/
|
849 |
r->map_func(d, i, r->addr, r->filtered_size, r->type); |
850 |
} |
851 |
} |
852 |
} |
853 |
|
854 |
uint32_t pci_default_read_config(PCIDevice *d, |
855 |
uint32_t address, int len)
|
856 |
{ |
857 |
uint32_t val = 0;
|
858 |
assert(len == 1 || len == 2 || len == 4); |
859 |
len = MIN(len, pci_config_size(d) - address); |
860 |
memcpy(&val, d->config + address, len); |
861 |
return le32_to_cpu(val);
|
862 |
} |
863 |
|
864 |
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
865 |
{ |
866 |
int i;
|
867 |
uint32_t config_size = pci_config_size(d); |
868 |
|
869 |
for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) { |
870 |
uint8_t wmask = d->wmask[addr + i]; |
871 |
d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
872 |
} |
873 |
if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
874 |
ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
|
875 |
ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
|
876 |
range_covers_byte(addr, l, PCI_COMMAND)) |
877 |
pci_update_mappings(d); |
878 |
} |
879 |
|
880 |
/***********************************************************/
|
881 |
/* generic PCI irq support */
|
882 |
|
883 |
/* 0 <= irq_num <= 3. level must be 0 or 1 */
|
884 |
static void pci_set_irq(void *opaque, int irq_num, int level) |
885 |
{ |
886 |
PCIDevice *pci_dev = opaque; |
887 |
PCIBus *bus; |
888 |
int change;
|
889 |
|
890 |
change = level - pci_dev->irq_state[irq_num]; |
891 |
if (!change)
|
892 |
return;
|
893 |
|
894 |
pci_dev->irq_state[irq_num] = level; |
895 |
for (;;) {
|
896 |
bus = pci_dev->bus; |
897 |
irq_num = bus->map_irq(pci_dev, irq_num); |
898 |
if (bus->set_irq)
|
899 |
break;
|
900 |
pci_dev = bus->parent_dev; |
901 |
} |
902 |
bus->irq_count[irq_num] += change; |
903 |
bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
904 |
} |
905 |
|
906 |
/***********************************************************/
|
907 |
/* monitor info on PCI */
|
908 |
|
909 |
typedef struct { |
910 |
uint16_t class; |
911 |
const char *desc; |
912 |
} pci_class_desc; |
913 |
|
914 |
static const pci_class_desc pci_class_descriptions[] = |
915 |
{ |
916 |
{ 0x0100, "SCSI controller"}, |
917 |
{ 0x0101, "IDE controller"}, |
918 |
{ 0x0102, "Floppy controller"}, |
919 |
{ 0x0103, "IPI controller"}, |
920 |
{ 0x0104, "RAID controller"}, |
921 |
{ 0x0106, "SATA controller"}, |
922 |
{ 0x0107, "SAS controller"}, |
923 |
{ 0x0180, "Storage controller"}, |
924 |
{ 0x0200, "Ethernet controller"}, |
925 |
{ 0x0201, "Token Ring controller"}, |
926 |
{ 0x0202, "FDDI controller"}, |
927 |
{ 0x0203, "ATM controller"}, |
928 |
{ 0x0280, "Network controller"}, |
929 |
{ 0x0300, "VGA controller"}, |
930 |
{ 0x0301, "XGA controller"}, |
931 |
{ 0x0302, "3D controller"}, |
932 |
{ 0x0380, "Display controller"}, |
933 |
{ 0x0400, "Video controller"}, |
934 |
{ 0x0401, "Audio controller"}, |
935 |
{ 0x0402, "Phone"}, |
936 |
{ 0x0480, "Multimedia controller"}, |
937 |
{ 0x0500, "RAM controller"}, |
938 |
{ 0x0501, "Flash controller"}, |
939 |
{ 0x0580, "Memory controller"}, |
940 |
{ 0x0600, "Host bridge"}, |
941 |
{ 0x0601, "ISA bridge"}, |
942 |
{ 0x0602, "EISA bridge"}, |
943 |
{ 0x0603, "MC bridge"}, |
944 |
{ 0x0604, "PCI bridge"}, |
945 |
{ 0x0605, "PCMCIA bridge"}, |
946 |
{ 0x0606, "NUBUS bridge"}, |
947 |
{ 0x0607, "CARDBUS bridge"}, |
948 |
{ 0x0608, "RACEWAY bridge"}, |
949 |
{ 0x0680, "Bridge"}, |
950 |
{ 0x0c03, "USB controller"}, |
951 |
{ 0, NULL} |
952 |
}; |
953 |
|
954 |
static void pci_info_device(PCIBus *bus, PCIDevice *d) |
955 |
{ |
956 |
Monitor *mon = cur_mon; |
957 |
int i, class;
|
958 |
PCIIORegion *r; |
959 |
const pci_class_desc *desc;
|
960 |
|
961 |
monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
|
962 |
pci_bus_num(d->bus), |
963 |
PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); |
964 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
965 |
monitor_printf(mon, " ");
|
966 |
desc = pci_class_descriptions; |
967 |
while (desc->desc && class != desc->class)
|
968 |
desc++; |
969 |
if (desc->desc) {
|
970 |
monitor_printf(mon, "%s", desc->desc);
|
971 |
} else {
|
972 |
monitor_printf(mon, "Class %04x", class);
|
973 |
} |
974 |
monitor_printf(mon, ": PCI device %04x:%04x\n",
|
975 |
pci_get_word(d->config + PCI_VENDOR_ID), |
976 |
pci_get_word(d->config + PCI_DEVICE_ID)); |
977 |
|
978 |
if (d->config[PCI_INTERRUPT_PIN] != 0) { |
979 |
monitor_printf(mon, " IRQ %d.\n",
|
980 |
d->config[PCI_INTERRUPT_LINE]); |
981 |
} |
982 |
if (class == 0x0604) { |
983 |
uint64_t base; |
984 |
uint64_t limit; |
985 |
|
986 |
monitor_printf(mon, " BUS %d.\n", d->config[0x19]); |
987 |
monitor_printf(mon, " secondary bus %d.\n",
|
988 |
d->config[PCI_SECONDARY_BUS]); |
989 |
monitor_printf(mon, " subordinate bus %d.\n",
|
990 |
d->config[PCI_SUBORDINATE_BUS]); |
991 |
|
992 |
base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_IO); |
993 |
limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_IO); |
994 |
monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n", |
995 |
base, limit); |
996 |
|
997 |
base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY); |
998 |
limit= pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY); |
999 |
monitor_printf(mon, |
1000 |
" memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1001 |
base, limit); |
1002 |
|
1003 |
base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1004 |
PCI_BASE_ADDRESS_MEM_PREFETCH); |
1005 |
limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1006 |
PCI_BASE_ADDRESS_MEM_PREFETCH); |
1007 |
monitor_printf(mon, " prefetchable memory range "
|
1008 |
"[0x%08"PRIx64", 0x%08"PRIx64"]\n", base, limit); |
1009 |
} |
1010 |
for(i = 0;i < PCI_NUM_REGIONS; i++) { |
1011 |
r = &d->io_regions[i]; |
1012 |
if (r->size != 0) { |
1013 |
monitor_printf(mon, " BAR%d: ", i);
|
1014 |
if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1015 |
monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
|
1016 |
" [0x%04"FMT_PCIBUS"].\n", |
1017 |
r->addr, r->addr + r->size - 1);
|
1018 |
} else {
|
1019 |
const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ? |
1020 |
"64 bit" : "32 bit"; |
1021 |
const char *prefetch = |
1022 |
r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ? |
1023 |
" prefetchable" : ""; |
1024 |
|
1025 |
monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
|
1026 |
" [0x%08"FMT_PCIBUS"].\n", |
1027 |
type, prefetch, |
1028 |
r->addr, r->addr + r->size - 1);
|
1029 |
} |
1030 |
} |
1031 |
} |
1032 |
monitor_printf(mon, " id \"%s\"\n", d->qdev.id ? d->qdev.id : ""); |
1033 |
if (class == 0x0604 && d->config[0x19] != 0) { |
1034 |
pci_for_each_device(bus, d->config[0x19], pci_info_device);
|
1035 |
} |
1036 |
} |
1037 |
|
1038 |
static void pci_for_each_device_under_bus(PCIBus *bus, |
1039 |
void (*fn)(PCIBus *b, PCIDevice *d))
|
1040 |
{ |
1041 |
PCIDevice *d; |
1042 |
int devfn;
|
1043 |
|
1044 |
for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1045 |
d = bus->devices[devfn]; |
1046 |
if (d)
|
1047 |
fn(bus, d); |
1048 |
} |
1049 |
} |
1050 |
|
1051 |
void pci_for_each_device(PCIBus *bus, int bus_num, |
1052 |
void (*fn)(PCIBus *b, PCIDevice *d))
|
1053 |
{ |
1054 |
bus = pci_find_bus(bus, bus_num); |
1055 |
|
1056 |
if (bus) {
|
1057 |
pci_for_each_device_under_bus(bus, fn); |
1058 |
} |
1059 |
} |
1060 |
|
1061 |
void pci_info(Monitor *mon)
|
1062 |
{ |
1063 |
struct PCIHostBus *host;
|
1064 |
QLIST_FOREACH(host, &host_buses, next) { |
1065 |
pci_for_each_device(host->bus, 0, pci_info_device);
|
1066 |
} |
1067 |
} |
1068 |
|
1069 |
static const char * const pci_nic_models[] = { |
1070 |
"ne2k_pci",
|
1071 |
"i82551",
|
1072 |
"i82557b",
|
1073 |
"i82559er",
|
1074 |
"rtl8139",
|
1075 |
"e1000",
|
1076 |
"pcnet",
|
1077 |
"virtio",
|
1078 |
NULL
|
1079 |
}; |
1080 |
|
1081 |
static const char * const pci_nic_names[] = { |
1082 |
"ne2k_pci",
|
1083 |
"i82551",
|
1084 |
"i82557b",
|
1085 |
"i82559er",
|
1086 |
"rtl8139",
|
1087 |
"e1000",
|
1088 |
"pcnet",
|
1089 |
"virtio-net-pci",
|
1090 |
NULL
|
1091 |
}; |
1092 |
|
1093 |
/* Initialize a PCI NIC. */
|
1094 |
/* FIXME callers should check for failure, but don't */
|
1095 |
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model, |
1096 |
const char *default_devaddr) |
1097 |
{ |
1098 |
const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
1099 |
PCIBus *bus; |
1100 |
int devfn;
|
1101 |
PCIDevice *pci_dev; |
1102 |
DeviceState *dev; |
1103 |
int i;
|
1104 |
|
1105 |
i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1106 |
if (i < 0) |
1107 |
return NULL; |
1108 |
|
1109 |
bus = pci_get_bus_devfn(&devfn, devaddr); |
1110 |
if (!bus) {
|
1111 |
qemu_error("Invalid PCI device address %s for device %s\n",
|
1112 |
devaddr, pci_nic_names[i]); |
1113 |
return NULL; |
1114 |
} |
1115 |
|
1116 |
pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
1117 |
dev = &pci_dev->qdev; |
1118 |
if (nd->name)
|
1119 |
dev->id = qemu_strdup(nd->name); |
1120 |
qdev_set_nic_properties(dev, nd); |
1121 |
if (qdev_init(dev) < 0) |
1122 |
return NULL; |
1123 |
return pci_dev;
|
1124 |
} |
1125 |
|
1126 |
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model, |
1127 |
const char *default_devaddr) |
1128 |
{ |
1129 |
PCIDevice *res; |
1130 |
|
1131 |
if (qemu_show_nic_models(nd->model, pci_nic_models))
|
1132 |
exit(0);
|
1133 |
|
1134 |
res = pci_nic_init(nd, default_model, default_devaddr); |
1135 |
if (!res)
|
1136 |
exit(1);
|
1137 |
return res;
|
1138 |
} |
1139 |
|
1140 |
typedef struct { |
1141 |
PCIDevice dev; |
1142 |
PCIBus bus; |
1143 |
uint32_t vid; |
1144 |
uint32_t did; |
1145 |
} PCIBridge; |
1146 |
|
1147 |
|
1148 |
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d) |
1149 |
{ |
1150 |
pci_update_mappings(d); |
1151 |
} |
1152 |
|
1153 |
static void pci_bridge_update_mappings(PCIBus *b) |
1154 |
{ |
1155 |
PCIBus *child; |
1156 |
|
1157 |
pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn); |
1158 |
|
1159 |
QLIST_FOREACH(child, &b->child, sibling) { |
1160 |
pci_bridge_update_mappings(child); |
1161 |
} |
1162 |
} |
1163 |
|
1164 |
static void pci_bridge_write_config(PCIDevice *d, |
1165 |
uint32_t address, uint32_t val, int len)
|
1166 |
{ |
1167 |
pci_default_write_config(d, address, val, len); |
1168 |
|
1169 |
if (/* io base/limit */ |
1170 |
ranges_overlap(address, len, PCI_IO_BASE, 2) ||
|
1171 |
|
1172 |
/* memory base/limit, prefetchable base/limit and
|
1173 |
io base/limit upper 16 */
|
1174 |
ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
|
1175 |
pci_bridge_update_mappings(d->bus); |
1176 |
} |
1177 |
} |
1178 |
|
1179 |
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
|
1180 |
{ |
1181 |
PCIBus *sec; |
1182 |
|
1183 |
if (!bus)
|
1184 |
return NULL; |
1185 |
|
1186 |
if (pci_bus_num(bus) == bus_num) {
|
1187 |
return bus;
|
1188 |
} |
1189 |
|
1190 |
/* try child bus */
|
1191 |
QLIST_FOREACH(sec, &bus->child, sibling) { |
1192 |
|
1193 |
if (!bus->parent_dev /* pci host bridge */ |
1194 |
|| (pci_bus_num(sec) <= bus_num && |
1195 |
bus->parent_dev->config[PCI_SUBORDINATE_BUS])) { |
1196 |
return pci_find_bus(sec, bus_num);
|
1197 |
} |
1198 |
} |
1199 |
|
1200 |
return NULL; |
1201 |
} |
1202 |
|
1203 |
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function) |
1204 |
{ |
1205 |
bus = pci_find_bus(bus, bus_num); |
1206 |
|
1207 |
if (!bus)
|
1208 |
return NULL; |
1209 |
|
1210 |
return bus->devices[PCI_DEVFN(slot, function)];
|
1211 |
} |
1212 |
|
1213 |
static int pci_bridge_initfn(PCIDevice *dev) |
1214 |
{ |
1215 |
PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev); |
1216 |
|
1217 |
pci_config_set_vendor_id(s->dev.config, s->vid); |
1218 |
pci_config_set_device_id(s->dev.config, s->did); |
1219 |
|
1220 |
/* TODO: intial value
|
1221 |
* command register:
|
1222 |
* According to PCI bridge spec, after reset
|
1223 |
* bus master bit is off
|
1224 |
* memory space enable bit is off
|
1225 |
* According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
|
1226 |
* the reset value should be zero unless the boot pin is tied high
|
1227 |
* (which is tru) and thus it should be PCI_COMMAND_MEMORY.
|
1228 |
*
|
1229 |
* For now, don't touch the value.
|
1230 |
* Later command register will be set to zero and apb_pci.c will
|
1231 |
* override the value.
|
1232 |
* Same for latency timer, and multi function bit of header type.
|
1233 |
*/
|
1234 |
pci_set_word(dev->config + PCI_COMMAND, |
1235 |
PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
1236 |
|
1237 |
pci_set_word(dev->config + PCI_STATUS, |
1238 |
PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1239 |
pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI); |
1240 |
dev->config[PCI_LATENCY_TIMER] = 0x10;
|
1241 |
dev->config[PCI_HEADER_TYPE] = |
1242 |
PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE; |
1243 |
pci_set_word(dev->config + PCI_SEC_STATUS, |
1244 |
PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1245 |
return 0; |
1246 |
} |
1247 |
|
1248 |
static int pci_bridge_exitfn(PCIDevice *pci_dev) |
1249 |
{ |
1250 |
PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev); |
1251 |
PCIBus *bus = &s->bus; |
1252 |
pci_unregister_secondary_bus(bus); |
1253 |
return 0; |
1254 |
} |
1255 |
|
1256 |
PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
|
1257 |
pci_map_irq_fn map_irq, const char *name) |
1258 |
{ |
1259 |
PCIDevice *dev; |
1260 |
PCIBridge *s; |
1261 |
|
1262 |
dev = pci_create(bus, devfn, "pci-bridge");
|
1263 |
qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
|
1264 |
qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
|
1265 |
qdev_init_nofail(&dev->qdev); |
1266 |
|
1267 |
s = DO_UPCAST(PCIBridge, dev, dev); |
1268 |
pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name); |
1269 |
return &s->bus;
|
1270 |
} |
1271 |
|
1272 |
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) |
1273 |
{ |
1274 |
PCIDevice *pci_dev = (PCIDevice *)qdev; |
1275 |
PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev); |
1276 |
PCIBus *bus; |
1277 |
int devfn, rc;
|
1278 |
|
1279 |
/* initialize cap_present for pci_is_express() and pci_config_size() */
|
1280 |
if (info->is_express) {
|
1281 |
pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1282 |
} |
1283 |
|
1284 |
bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev)); |
1285 |
devfn = pci_dev->devfn; |
1286 |
pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn, |
1287 |
info->config_read, info->config_write, |
1288 |
info->header_type); |
1289 |
rc = info->init(pci_dev); |
1290 |
if (rc != 0) |
1291 |
return rc;
|
1292 |
if (qdev->hotplugged)
|
1293 |
bus->hotplug(pci_dev, 1);
|
1294 |
return 0; |
1295 |
} |
1296 |
|
1297 |
static int pci_unplug_device(DeviceState *qdev) |
1298 |
{ |
1299 |
PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev); |
1300 |
|
1301 |
dev->bus->hotplug(dev, 0);
|
1302 |
return 0; |
1303 |
} |
1304 |
|
1305 |
void pci_qdev_register(PCIDeviceInfo *info)
|
1306 |
{ |
1307 |
info->qdev.init = pci_qdev_init; |
1308 |
info->qdev.unplug = pci_unplug_device; |
1309 |
info->qdev.exit = pci_unregister_device; |
1310 |
info->qdev.bus_info = &pci_bus_info; |
1311 |
qdev_register(&info->qdev); |
1312 |
} |
1313 |
|
1314 |
void pci_qdev_register_many(PCIDeviceInfo *info)
|
1315 |
{ |
1316 |
while (info->qdev.name) {
|
1317 |
pci_qdev_register(info); |
1318 |
info++; |
1319 |
} |
1320 |
} |
1321 |
|
1322 |
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1323 |
{ |
1324 |
DeviceState *dev; |
1325 |
|
1326 |
dev = qdev_create(&bus->qbus, name); |
1327 |
qdev_prop_set_uint32(dev, "addr", devfn);
|
1328 |
return DO_UPCAST(PCIDevice, qdev, dev);
|
1329 |
} |
1330 |
|
1331 |
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1332 |
{ |
1333 |
PCIDevice *dev = pci_create(bus, devfn, name); |
1334 |
qdev_init_nofail(&dev->qdev); |
1335 |
return dev;
|
1336 |
} |
1337 |
|
1338 |
static int pci_find_space(PCIDevice *pdev, uint8_t size) |
1339 |
{ |
1340 |
int config_size = pci_config_size(pdev);
|
1341 |
int offset = PCI_CONFIG_HEADER_SIZE;
|
1342 |
int i;
|
1343 |
for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
1344 |
if (pdev->used[i])
|
1345 |
offset = i + 1;
|
1346 |
else if (i - offset + 1 == size) |
1347 |
return offset;
|
1348 |
return 0; |
1349 |
} |
1350 |
|
1351 |
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1352 |
uint8_t *prev_p) |
1353 |
{ |
1354 |
uint8_t next, prev; |
1355 |
|
1356 |
if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1357 |
return 0; |
1358 |
|
1359 |
for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1360 |
prev = next + PCI_CAP_LIST_NEXT) |
1361 |
if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1362 |
break;
|
1363 |
|
1364 |
if (prev_p)
|
1365 |
*prev_p = prev; |
1366 |
return next;
|
1367 |
} |
1368 |
|
1369 |
/* Reserve space and add capability to the linked list in pci config space */
|
1370 |
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1371 |
{ |
1372 |
uint8_t offset = pci_find_space(pdev, size); |
1373 |
uint8_t *config = pdev->config + offset; |
1374 |
if (!offset)
|
1375 |
return -ENOSPC;
|
1376 |
config[PCI_CAP_LIST_ID] = cap_id; |
1377 |
config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
1378 |
pdev->config[PCI_CAPABILITY_LIST] = offset; |
1379 |
pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
1380 |
memset(pdev->used + offset, 0xFF, size);
|
1381 |
/* Make capability read-only by default */
|
1382 |
memset(pdev->wmask + offset, 0, size);
|
1383 |
/* Check capability by default */
|
1384 |
memset(pdev->cmask + offset, 0xFF, size);
|
1385 |
return offset;
|
1386 |
} |
1387 |
|
1388 |
/* Unlink capability from the pci config space. */
|
1389 |
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1390 |
{ |
1391 |
uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
1392 |
if (!offset)
|
1393 |
return;
|
1394 |
pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
1395 |
/* Make capability writeable again */
|
1396 |
memset(pdev->wmask + offset, 0xff, size);
|
1397 |
/* Clear cmask as device-specific registers can't be checked */
|
1398 |
memset(pdev->cmask + offset, 0, size);
|
1399 |
memset(pdev->used + offset, 0, size);
|
1400 |
|
1401 |
if (!pdev->config[PCI_CAPABILITY_LIST])
|
1402 |
pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
1403 |
} |
1404 |
|
1405 |
/* Reserve space for capability at a known offset (to call after load). */
|
1406 |
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
|
1407 |
{ |
1408 |
memset(pdev->used + offset, 0xff, size);
|
1409 |
} |
1410 |
|
1411 |
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
1412 |
{ |
1413 |
return pci_find_capability_list(pdev, cap_id, NULL); |
1414 |
} |
1415 |
|
1416 |
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
1417 |
{ |
1418 |
PCIDevice *d = (PCIDevice *)dev; |
1419 |
const pci_class_desc *desc;
|
1420 |
char ctxt[64]; |
1421 |
PCIIORegion *r; |
1422 |
int i, class;
|
1423 |
|
1424 |
class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1425 |
desc = pci_class_descriptions; |
1426 |
while (desc->desc && class != desc->class)
|
1427 |
desc++; |
1428 |
if (desc->desc) {
|
1429 |
snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
1430 |
} else {
|
1431 |
snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
1432 |
} |
1433 |
|
1434 |
monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
1435 |
"pci id %04x:%04x (sub %04x:%04x)\n",
|
1436 |
indent, "", ctxt,
|
1437 |
d->config[PCI_SECONDARY_BUS], |
1438 |
PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
1439 |
pci_get_word(d->config + PCI_VENDOR_ID), |
1440 |
pci_get_word(d->config + PCI_DEVICE_ID), |
1441 |
pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
1442 |
pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
1443 |
for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1444 |
r = &d->io_regions[i]; |
1445 |
if (!r->size)
|
1446 |
continue;
|
1447 |
monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
1448 |
" [0x%"FMT_PCIBUS"]\n", |
1449 |
indent, "",
|
1450 |
i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
1451 |
r->addr, r->addr + r->size - 1);
|
1452 |
} |
1453 |
} |
1454 |
|
1455 |
static PCIDeviceInfo bridge_info = {
|
1456 |
.qdev.name = "pci-bridge",
|
1457 |
.qdev.size = sizeof(PCIBridge),
|
1458 |
.init = pci_bridge_initfn, |
1459 |
.exit = pci_bridge_exitfn, |
1460 |
.config_write = pci_bridge_write_config, |
1461 |
.qdev.props = (Property[]) { |
1462 |
DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0), |
1463 |
DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0), |
1464 |
DEFINE_PROP_END_OF_LIST(), |
1465 |
} |
1466 |
}; |
1467 |
|
1468 |
static void pci_register_devices(void) |
1469 |
{ |
1470 |
pci_qdev_register(&bridge_info); |
1471 |
} |
1472 |
|
1473 |
device_init(pci_register_devices) |