]> Git Repo - qemu.git/blame - hw/pci.c
qemu: add pci helper functions (Marcelo Tosatti)
[qemu.git] / hw / pci.c
CommitLineData
69b91039
FB
1/*
2 * QEMU PCI bus manager
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5fafdf24 5 *
69b91039
FB
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 */
87ecb68b
PB
24#include "hw.h"
25#include "pci.h"
26#include "console.h"
27#include "net.h"
fbe78f4f 28#include "virtio-net.h"
69b91039
FB
29
30//#define DEBUG_PCI
31
30468f78
FB
32struct PCIBus {
33 int bus_num;
34 int devfn_min;
502a5395 35 pci_set_irq_fn set_irq;
d2b59317 36 pci_map_irq_fn map_irq;
30468f78 37 uint32_t config_reg; /* XXX: suppress */
384d8876
FB
38 /* low level pic */
39 SetIRQFunc *low_set_irq;
d537cf6c 40 qemu_irq *irq_opaque;
30468f78 41 PCIDevice *devices[256];
80b3ada7
PB
42 PCIDevice *parent_dev;
43 PCIBus *next;
d2b59317
PB
44 /* The bus IRQ state is the logical OR of the connected devices.
45 Keep a count of the number of devices with raised IRQs. */
52fc1d83 46 int nirq;
80b3ada7 47 int irq_count[];
30468f78 48};
69b91039 49
1941d19c 50static void pci_update_mappings(PCIDevice *d);
d537cf6c 51static void pci_set_irq(void *opaque, int irq_num, int level);
1941d19c 52
69b91039 53target_phys_addr_t pci_mem_base;
d350d97d
AL
54static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
55static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
0ac32c83 56static int pci_irq_index;
30468f78
FB
57static PCIBus *first_bus;
58
52fc1d83
AZ
59static void pcibus_save(QEMUFile *f, void *opaque)
60{
61 PCIBus *bus = (PCIBus *)opaque;
62 int i;
63
64 qemu_put_be32(f, bus->nirq);
65 for (i = 0; i < bus->nirq; i++)
66 qemu_put_be32(f, bus->irq_count[i]);
67}
68
69static int pcibus_load(QEMUFile *f, void *opaque, int version_id)
70{
71 PCIBus *bus = (PCIBus *)opaque;
72 int i, nirq;
73
74 if (version_id != 1)
75 return -EINVAL;
76
77 nirq = qemu_get_be32(f);
78 if (bus->nirq != nirq) {
79 fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
80 nirq, bus->nirq);
81 return -EINVAL;
82 }
83
84 for (i = 0; i < nirq; i++)
85 bus->irq_count[i] = qemu_get_be32(f);
86
87 return 0;
88}
89
d2b59317 90PCIBus *pci_register_bus(pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
d537cf6c 91 qemu_irq *pic, int devfn_min, int nirq)
30468f78
FB
92{
93 PCIBus *bus;
52fc1d83
AZ
94 static int nbus = 0;
95
80b3ada7 96 bus = qemu_mallocz(sizeof(PCIBus) + (nirq * sizeof(int)));
502a5395 97 bus->set_irq = set_irq;
d2b59317 98 bus->map_irq = map_irq;
502a5395
PB
99 bus->irq_opaque = pic;
100 bus->devfn_min = devfn_min;
52fc1d83 101 bus->nirq = nirq;
30468f78 102 first_bus = bus;
52fc1d83 103 register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
30468f78
FB
104 return bus;
105}
69b91039 106
9596ebb7 107static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
80b3ada7
PB
108{
109 PCIBus *bus;
110 bus = qemu_mallocz(sizeof(PCIBus));
111 bus->map_irq = map_irq;
112 bus->parent_dev = dev;
113 bus->next = dev->bus->next;
114 dev->bus->next = bus;
115 return bus;
116}
117
502a5395
PB
118int pci_bus_num(PCIBus *s)
119{
120 return s->bus_num;
121}
122
1941d19c 123void pci_device_save(PCIDevice *s, QEMUFile *f)
30ca2aab 124{
52fc1d83
AZ
125 int i;
126
127 qemu_put_be32(f, 2); /* PCI device version */
30ca2aab 128 qemu_put_buffer(f, s->config, 256);
52fc1d83
AZ
129 for (i = 0; i < 4; i++)
130 qemu_put_be32(f, s->irq_state[i]);
30ca2aab
FB
131}
132
1941d19c 133int pci_device_load(PCIDevice *s, QEMUFile *f)
30ca2aab 134{
1941d19c 135 uint32_t version_id;
52fc1d83
AZ
136 int i;
137
1941d19c 138 version_id = qemu_get_be32(f);
52fc1d83 139 if (version_id > 2)
30ca2aab 140 return -EINVAL;
30ca2aab 141 qemu_get_buffer(f, s->config, 256);
1941d19c 142 pci_update_mappings(s);
52fc1d83
AZ
143
144 if (version_id >= 2)
145 for (i = 0; i < 4; i ++)
146 s->irq_state[i] = qemu_get_be32(f);
147
30ca2aab
FB
148 return 0;
149}
150
d350d97d
AL
151static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
152{
153 uint16_t *id;
154
155 id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
156 id[0] = cpu_to_le16(pci_default_sub_vendor_id);
157 id[1] = cpu_to_le16(pci_default_sub_device_id);
158 return 0;
159}
160
69b91039 161/* -1 for devfn means auto assign */
5fafdf24 162PCIDevice *pci_register_device(PCIBus *bus, const char *name,
30468f78 163 int instance_size, int devfn,
5fafdf24 164 PCIConfigReadFunc *config_read,
69b91039
FB
165 PCIConfigWriteFunc *config_write)
166{
30468f78 167 PCIDevice *pci_dev;
69b91039 168
0ac32c83
FB
169 if (pci_irq_index >= PCI_DEVICES_MAX)
170 return NULL;
3b46e624 171
69b91039 172 if (devfn < 0) {
30468f78
FB
173 for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
174 if (!bus->devices[devfn])
69b91039
FB
175 goto found;
176 }
177 return NULL;
178 found: ;
179 }
180 pci_dev = qemu_mallocz(instance_size);
30468f78 181 pci_dev->bus = bus;
69b91039
FB
182 pci_dev->devfn = devfn;
183 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
d2b59317 184 memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
d350d97d 185 pci_set_default_subsystem_id(pci_dev);
0ac32c83
FB
186
187 if (!config_read)
188 config_read = pci_default_read_config;
189 if (!config_write)
190 config_write = pci_default_write_config;
69b91039
FB
191 pci_dev->config_read = config_read;
192 pci_dev->config_write = config_write;
0ac32c83 193 pci_dev->irq_index = pci_irq_index++;
30468f78 194 bus->devices[devfn] = pci_dev;
d537cf6c 195 pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
69b91039
FB
196 return pci_dev;
197}
198
5fafdf24
TS
199void pci_register_io_region(PCIDevice *pci_dev, int region_num,
200 uint32_t size, int type,
69b91039
FB
201 PCIMapIORegionFunc *map_func)
202{
203 PCIIORegion *r;
d7ce493a 204 uint32_t addr;
69b91039 205
8a8696a3 206 if ((unsigned int)region_num >= PCI_NUM_REGIONS)
69b91039
FB
207 return;
208 r = &pci_dev->io_regions[region_num];
209 r->addr = -1;
210 r->size = size;
211 r->type = type;
212 r->map_func = map_func;
d7ce493a
PB
213 if (region_num == PCI_ROM_SLOT) {
214 addr = 0x30;
215 } else {
216 addr = 0x10 + region_num * 4;
217 }
218 *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
69b91039
FB
219}
220
9596ebb7 221static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
69b91039 222{
502a5395 223 return addr + pci_mem_base;
69b91039
FB
224}
225
0ac32c83
FB
226static void pci_update_mappings(PCIDevice *d)
227{
228 PCIIORegion *r;
229 int cmd, i;
8a8696a3 230 uint32_t last_addr, new_addr, config_ofs;
3b46e624 231
0ac32c83 232 cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
8a8696a3 233 for(i = 0; i < PCI_NUM_REGIONS; i++) {
0ac32c83 234 r = &d->io_regions[i];
8a8696a3
FB
235 if (i == PCI_ROM_SLOT) {
236 config_ofs = 0x30;
237 } else {
238 config_ofs = 0x10 + i * 4;
239 }
0ac32c83
FB
240 if (r->size != 0) {
241 if (r->type & PCI_ADDRESS_SPACE_IO) {
242 if (cmd & PCI_COMMAND_IO) {
5fafdf24 243 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
8a8696a3 244 config_ofs));
0ac32c83
FB
245 new_addr = new_addr & ~(r->size - 1);
246 last_addr = new_addr + r->size - 1;
247 /* NOTE: we have only 64K ioports on PC */
248 if (last_addr <= new_addr || new_addr == 0 ||
249 last_addr >= 0x10000) {
250 new_addr = -1;
251 }
252 } else {
253 new_addr = -1;
254 }
255 } else {
256 if (cmd & PCI_COMMAND_MEMORY) {
5fafdf24 257 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
8a8696a3
FB
258 config_ofs));
259 /* the ROM slot has a specific enable bit */
260 if (i == PCI_ROM_SLOT && !(new_addr & 1))
261 goto no_mem_map;
0ac32c83
FB
262 new_addr = new_addr & ~(r->size - 1);
263 last_addr = new_addr + r->size - 1;
264 /* NOTE: we do not support wrapping */
265 /* XXX: as we cannot support really dynamic
266 mappings, we handle specific values as invalid
267 mappings. */
268 if (last_addr <= new_addr || new_addr == 0 ||
269 last_addr == -1) {
270 new_addr = -1;
271 }
272 } else {
8a8696a3 273 no_mem_map:
0ac32c83
FB
274 new_addr = -1;
275 }
276 }
277 /* now do the real mapping */
278 if (new_addr != r->addr) {
279 if (r->addr != -1) {
280 if (r->type & PCI_ADDRESS_SPACE_IO) {
281 int class;
282 /* NOTE: specific hack for IDE in PC case:
283 only one byte must be mapped. */
284 class = d->config[0x0a] | (d->config[0x0b] << 8);
285 if (class == 0x0101 && r->size == 4) {
286 isa_unassign_ioport(r->addr + 2, 1);
287 } else {
288 isa_unassign_ioport(r->addr, r->size);
289 }
290 } else {
502a5395 291 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
5fafdf24 292 r->size,
0ac32c83 293 IO_MEM_UNASSIGNED);
f65ed4c1 294 qemu_unregister_coalesced_mmio(r->addr, r->size);
0ac32c83
FB
295 }
296 }
297 r->addr = new_addr;
298 if (r->addr != -1) {
299 r->map_func(d, i, r->addr, r->size, r->type);
300 }
301 }
302 }
303 }
304}
305
5fafdf24 306uint32_t pci_default_read_config(PCIDevice *d,
0ac32c83 307 uint32_t address, int len)
69b91039 308{
0ac32c83 309 uint32_t val;
a2d4e44b 310
0ac32c83 311 switch(len) {
0ac32c83
FB
312 default:
313 case 4:
a2d4e44b
TS
314 if (address <= 0xfc) {
315 val = le32_to_cpu(*(uint32_t *)(d->config + address));
316 break;
317 }
318 /* fall through */
319 case 2:
320 if (address <= 0xfe) {
321 val = le16_to_cpu(*(uint16_t *)(d->config + address));
322 break;
323 }
324 /* fall through */
325 case 1:
326 val = d->config[address];
0ac32c83
FB
327 break;
328 }
329 return val;
330}
331
5fafdf24 332void pci_default_write_config(PCIDevice *d,
0ac32c83
FB
333 uint32_t address, uint32_t val, int len)
334{
335 int can_write, i;
7bf5be70 336 uint32_t end, addr;
0ac32c83 337
5fafdf24 338 if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
8a8696a3 339 (address >= 0x30 && address < 0x34))) {
0ac32c83
FB
340 PCIIORegion *r;
341 int reg;
342
8a8696a3
FB
343 if ( address >= 0x30 ) {
344 reg = PCI_ROM_SLOT;
345 }else{
346 reg = (address - 0x10) >> 2;
347 }
0ac32c83
FB
348 r = &d->io_regions[reg];
349 if (r->size == 0)
350 goto default_config;
351 /* compute the stored value */
8a8696a3
FB
352 if (reg == PCI_ROM_SLOT) {
353 /* keep ROM enable bit */
354 val &= (~(r->size - 1)) | 1;
355 } else {
356 val &= ~(r->size - 1);
357 val |= r->type;
358 }
359 *(uint32_t *)(d->config + address) = cpu_to_le32(val);
0ac32c83 360 pci_update_mappings(d);
69b91039 361 return;
0ac32c83
FB
362 }
363 default_config:
364 /* not efficient, but simple */
7bf5be70 365 addr = address;
0ac32c83
FB
366 for(i = 0; i < len; i++) {
367 /* default read/write accesses */
1f62d938 368 switch(d->config[0x0e]) {
0ac32c83 369 case 0x00:
1f62d938
FB
370 case 0x80:
371 switch(addr) {
372 case 0x00:
373 case 0x01:
374 case 0x02:
375 case 0x03:
376 case 0x08:
377 case 0x09:
378 case 0x0a:
379 case 0x0b:
380 case 0x0e:
381 case 0x10 ... 0x27: /* base */
8098ed41 382 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
1f62d938
FB
383 case 0x30 ... 0x33: /* rom */
384 case 0x3d:
385 can_write = 0;
386 break;
387 default:
388 can_write = 1;
389 break;
390 }
0ac32c83
FB
391 break;
392 default:
1f62d938
FB
393 case 0x01:
394 switch(addr) {
395 case 0x00:
396 case 0x01:
397 case 0x02:
398 case 0x03:
399 case 0x08:
400 case 0x09:
401 case 0x0a:
402 case 0x0b:
403 case 0x0e:
8098ed41 404 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
1f62d938
FB
405 case 0x38 ... 0x3b: /* rom */
406 case 0x3d:
407 can_write = 0;
408 break;
409 default:
410 can_write = 1;
411 break;
412 }
0ac32c83
FB
413 break;
414 }
415 if (can_write) {
8098ed41
AJ
416 /* Mask out writes to reserved bits in registers */
417 switch (addr) {
475dc65f
AJ
418 case 0x05:
419 val &= ~PCI_COMMAND_RESERVED_MASK_HI;
420 break;
8098ed41
AJ
421 case 0x06:
422 val &= ~PCI_STATUS_RESERVED_MASK_LO;
423 break;
424 case 0x07:
425 val &= ~PCI_STATUS_RESERVED_MASK_HI;
426 break;
427 }
7bf5be70 428 d->config[addr] = val;
0ac32c83 429 }
a2d4e44b
TS
430 if (++addr > 0xff)
431 break;
0ac32c83
FB
432 val >>= 8;
433 }
434
435 end = address + len;
436 if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
437 /* if the command register is modified, we must modify the mappings */
438 pci_update_mappings(d);
69b91039
FB
439 }
440}
441
502a5395 442void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
69b91039 443{
30468f78
FB
444 PCIBus *s = opaque;
445 PCIDevice *pci_dev;
446 int config_addr, bus_num;
3b46e624 447
69b91039
FB
448#if defined(DEBUG_PCI) && 0
449 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
502a5395 450 addr, val, len);
69b91039 451#endif
502a5395 452 bus_num = (addr >> 16) & 0xff;
80b3ada7
PB
453 while (s && s->bus_num != bus_num)
454 s = s->next;
455 if (!s)
69b91039 456 return;
502a5395 457 pci_dev = s->devices[(addr >> 8) & 0xff];
69b91039
FB
458 if (!pci_dev)
459 return;
502a5395 460 config_addr = addr & 0xff;
69b91039
FB
461#if defined(DEBUG_PCI)
462 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
463 pci_dev->name, config_addr, val, len);
464#endif
0ac32c83 465 pci_dev->config_write(pci_dev, config_addr, val, len);
69b91039
FB
466}
467
502a5395 468uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
69b91039 469{
30468f78
FB
470 PCIBus *s = opaque;
471 PCIDevice *pci_dev;
472 int config_addr, bus_num;
69b91039
FB
473 uint32_t val;
474
502a5395 475 bus_num = (addr >> 16) & 0xff;
80b3ada7
PB
476 while (s && s->bus_num != bus_num)
477 s= s->next;
478 if (!s)
69b91039 479 goto fail;
502a5395 480 pci_dev = s->devices[(addr >> 8) & 0xff];
69b91039
FB
481 if (!pci_dev) {
482 fail:
63ce9e0a
FB
483 switch(len) {
484 case 1:
485 val = 0xff;
486 break;
487 case 2:
488 val = 0xffff;
489 break;
490 default:
491 case 4:
492 val = 0xffffffff;
493 break;
494 }
69b91039
FB
495 goto the_end;
496 }
502a5395 497 config_addr = addr & 0xff;
69b91039
FB
498 val = pci_dev->config_read(pci_dev, config_addr, len);
499#if defined(DEBUG_PCI)
500 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
501 pci_dev->name, config_addr, val, len);
502#endif
503 the_end:
504#if defined(DEBUG_PCI) && 0
505 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
502a5395 506 addr, val, len);
69b91039
FB
507#endif
508 return val;
509}
510
502a5395
PB
511/***********************************************************/
512/* generic PCI irq support */
30468f78 513
502a5395 514/* 0 <= irq_num <= 3. level must be 0 or 1 */
d537cf6c 515static void pci_set_irq(void *opaque, int irq_num, int level)
69b91039 516{
d537cf6c 517 PCIDevice *pci_dev = (PCIDevice *)opaque;
80b3ada7
PB
518 PCIBus *bus;
519 int change;
3b46e624 520
80b3ada7
PB
521 change = level - pci_dev->irq_state[irq_num];
522 if (!change)
523 return;
d2b59317 524
d2b59317 525 pci_dev->irq_state[irq_num] = level;
5e966ce6
PB
526 for (;;) {
527 bus = pci_dev->bus;
80b3ada7 528 irq_num = bus->map_irq(pci_dev, irq_num);
5e966ce6
PB
529 if (bus->set_irq)
530 break;
80b3ada7 531 pci_dev = bus->parent_dev;
80b3ada7
PB
532 }
533 bus->irq_count[irq_num] += change;
d2b59317 534 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
69b91039
FB
535}
536
502a5395
PB
537/***********************************************************/
538/* monitor info on PCI */
0ac32c83 539
6650ee6d
PB
540typedef struct {
541 uint16_t class;
542 const char *desc;
543} pci_class_desc;
544
09bc878a 545static const pci_class_desc pci_class_descriptions[] =
6650ee6d 546{
4ca9c76f 547 { 0x0100, "SCSI controller"},
6650ee6d 548 { 0x0101, "IDE controller"},
dcb5b19a
TS
549 { 0x0102, "Floppy controller"},
550 { 0x0103, "IPI controller"},
551 { 0x0104, "RAID controller"},
552 { 0x0106, "SATA controller"},
553 { 0x0107, "SAS controller"},
554 { 0x0180, "Storage controller"},
6650ee6d 555 { 0x0200, "Ethernet controller"},
dcb5b19a
TS
556 { 0x0201, "Token Ring controller"},
557 { 0x0202, "FDDI controller"},
558 { 0x0203, "ATM controller"},
559 { 0x0280, "Network controller"},
6650ee6d 560 { 0x0300, "VGA controller"},
dcb5b19a
TS
561 { 0x0301, "XGA controller"},
562 { 0x0302, "3D controller"},
563 { 0x0380, "Display controller"},
564 { 0x0400, "Video controller"},
565 { 0x0401, "Audio controller"},
566 { 0x0402, "Phone"},
567 { 0x0480, "Multimedia controller"},
568 { 0x0500, "RAM controller"},
569 { 0x0501, "Flash controller"},
570 { 0x0580, "Memory controller"},
6650ee6d
PB
571 { 0x0600, "Host bridge"},
572 { 0x0601, "ISA bridge"},
dcb5b19a
TS
573 { 0x0602, "EISA bridge"},
574 { 0x0603, "MC bridge"},
6650ee6d 575 { 0x0604, "PCI bridge"},
dcb5b19a
TS
576 { 0x0605, "PCMCIA bridge"},
577 { 0x0606, "NUBUS bridge"},
578 { 0x0607, "CARDBUS bridge"},
579 { 0x0608, "RACEWAY bridge"},
580 { 0x0680, "Bridge"},
6650ee6d
PB
581 { 0x0c03, "USB controller"},
582 { 0, NULL}
583};
584
502a5395 585static void pci_info_device(PCIDevice *d)
30468f78 586{
502a5395
PB
587 int i, class;
588 PCIIORegion *r;
09bc878a 589 const pci_class_desc *desc;
30468f78 590
502a5395
PB
591 term_printf(" Bus %2d, device %3d, function %d:\n",
592 d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
593 class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
594 term_printf(" ");
6650ee6d
PB
595 desc = pci_class_descriptions;
596 while (desc->desc && class != desc->class)
597 desc++;
598 if (desc->desc) {
599 term_printf("%s", desc->desc);
600 } else {
502a5395 601 term_printf("Class %04x", class);
72cc6cfe 602 }
502a5395
PB
603 term_printf(": PCI device %04x:%04x\n",
604 le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
605 le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
30468f78 606
502a5395
PB
607 if (d->config[PCI_INTERRUPT_PIN] != 0) {
608 term_printf(" IRQ %d.\n", d->config[PCI_INTERRUPT_LINE]);
30468f78 609 }
80b3ada7
PB
610 if (class == 0x0604) {
611 term_printf(" BUS %d.\n", d->config[0x19]);
612 }
502a5395
PB
613 for(i = 0;i < PCI_NUM_REGIONS; i++) {
614 r = &d->io_regions[i];
615 if (r->size != 0) {
616 term_printf(" BAR%d: ", i);
617 if (r->type & PCI_ADDRESS_SPACE_IO) {
5fafdf24 618 term_printf("I/O at 0x%04x [0x%04x].\n",
502a5395
PB
619 r->addr, r->addr + r->size - 1);
620 } else {
5fafdf24 621 term_printf("32 bit memory at 0x%08x [0x%08x].\n",
502a5395
PB
622 r->addr, r->addr + r->size - 1);
623 }
624 }
77d4bc34 625 }
80b3ada7
PB
626 if (class == 0x0604 && d->config[0x19] != 0) {
627 pci_for_each_device(d->config[0x19], pci_info_device);
628 }
384d8876
FB
629}
630
80b3ada7 631void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
384d8876 632{
502a5395 633 PCIBus *bus = first_bus;
384d8876 634 PCIDevice *d;
502a5395 635 int devfn;
3b46e624 636
80b3ada7
PB
637 while (bus && bus->bus_num != bus_num)
638 bus = bus->next;
502a5395
PB
639 if (bus) {
640 for(devfn = 0; devfn < 256; devfn++) {
641 d = bus->devices[devfn];
642 if (d)
643 fn(d);
644 }
f2aa58c6 645 }
f2aa58c6
FB
646}
647
502a5395 648void pci_info(void)
f2aa58c6 649{
80b3ada7 650 pci_for_each_device(0, pci_info_device);
77d4bc34 651}
a41b2ff2 652
cb457d76
AL
653static const char * const pci_nic_models[] = {
654 "ne2k_pci",
655 "i82551",
656 "i82557b",
657 "i82559er",
658 "rtl8139",
659 "e1000",
660 "pcnet",
661 "virtio",
662 NULL
663};
664
665typedef void (*PCINICInitFn)(PCIBus *, NICInfo *, int);
666
667static PCINICInitFn pci_nic_init_fns[] = {
668 pci_ne2000_init,
669 pci_i82551_init,
670 pci_i82557b_init,
671 pci_i82559er_init,
672 pci_rtl8139_init,
673 pci_e1000_init,
674 pci_pcnet_init,
675 virtio_net_init,
676 NULL
677};
678
a41b2ff2 679/* Initialize a PCI NIC. */
cb457d76
AL
680void pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
681 const char *default_model)
a41b2ff2 682{
cb457d76
AL
683 int i;
684
685 qemu_check_nic_model_list(nd, pci_nic_models, default_model);
686
687 for (i = 0; pci_nic_models[i]; i++)
688 if (strcmp(nd->model, pci_nic_models[i]) == 0)
689 pci_nic_init_fns[i](bus, nd, devfn);
a41b2ff2
PB
690}
691
80b3ada7
PB
692typedef struct {
693 PCIDevice dev;
694 PCIBus *bus;
695} PCIBridge;
696
9596ebb7 697static void pci_bridge_write_config(PCIDevice *d,
80b3ada7
PB
698 uint32_t address, uint32_t val, int len)
699{
700 PCIBridge *s = (PCIBridge *)d;
701
702 if (address == 0x19 || (address == 0x18 && len > 1)) {
703 if (address == 0x19)
704 s->bus->bus_num = val & 0xff;
705 else
706 s->bus->bus_num = (val >> 8) & 0xff;
707#if defined(DEBUG_PCI)
708 printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);
709#endif
710 }
711 pci_default_write_config(d, address, val, len);
712}
713
3ae80618
AL
714PCIBus *pci_find_bus(int bus_num)
715{
716 PCIBus *bus = first_bus;
717
718 while (bus && bus->bus_num != bus_num)
719 bus = bus->next;
720
721 return bus;
722}
723
724PCIDevice *pci_find_device(int bus_num, int slot, int function)
725{
726 PCIBus *bus = pci_find_bus(bus_num);
727
728 if (!bus)
729 return NULL;
730
731 return bus->devices[PCI_DEVFN(slot, function)];
732}
733
480b9f24 734PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
80b3ada7
PB
735 pci_map_irq_fn map_irq, const char *name)
736{
737 PCIBridge *s;
5fafdf24 738 s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
80b3ada7 739 devfn, NULL, pci_bridge_write_config);
480b9f24
BS
740
741 pci_config_set_vendor_id(s->dev.config, vid);
742 pci_config_set_device_id(s->dev.config, did);
743
80b3ada7
PB
744 s->dev.config[0x04] = 0x06; // command = bus master, pci mem
745 s->dev.config[0x05] = 0x00;
746 s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
747 s->dev.config[0x07] = 0x00; // status = fast devsel
748 s->dev.config[0x08] = 0x00; // revision
749 s->dev.config[0x09] = 0x00; // programming i/f
173a543b 750 pci_config_set_class(s->dev.config, PCI_CLASS_BRIDGE_PCI);
80b3ada7
PB
751 s->dev.config[0x0D] = 0x10; // latency_timer
752 s->dev.config[0x0E] = 0x81; // header_type
753 s->dev.config[0x1E] = 0xa0; // secondary status
754
755 s->bus = pci_register_secondary_bus(&s->dev, map_irq);
756 return s->bus;
757}
This page took 0.37027 seconds and 4 git commands to generate.