1
2
3
4
5
6
7
8
9
10#include "hw/sysbus.h"
11#include "hw/pci/pci.h"
12#include "hw/pci/pci_bus.h"
13#include "hw/pci/pci_host.h"
14#include "exec/address-spaces.h"
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62enum {
63 PCI_VPB_IRQMAP_ASSUME_OK,
64 PCI_VPB_IRQMAP_BROKEN,
65 PCI_VPB_IRQMAP_FORCE_OK,
66};
67
68typedef struct {
69 PCIHostState parent_obj;
70
71 qemu_irq irq[4];
72 MemoryRegion controlregs;
73 MemoryRegion mem_config;
74 MemoryRegion mem_config2;
75
76 MemoryRegion pci_io_space;
77 MemoryRegion pci_mem_space;
78
79
80
81 MemoryRegion pci_io_window;
82 MemoryRegion pci_mem_window[3];
83 PCIBus pci_bus;
84 PCIDevice pci_dev;
85
86
87 int realview;
88 uint32_t mem_win_size[3];
89 uint8_t irq_mapping_prop;
90
91
92 uint32_t imap[3];
93 uint32_t smap[3];
94 uint32_t selfid;
95 uint32_t flags;
96 uint8_t irq_mapping;
97} PCIVPBState;
98
99static void pci_vpb_update_window(PCIVPBState *s, int i)
100{
101
102
103
104
105
106
107 hwaddr offset;
108 if (s->realview) {
109
110
111
112 offset = s->imap[i] & ~(s->mem_win_size[i] - 1);
113 } else {
114
115 offset = s->imap[i] << 28;
116 }
117 memory_region_set_alias_offset(&s->pci_mem_window[i], offset);
118}
119
120static void pci_vpb_update_all_windows(PCIVPBState *s)
121{
122
123 int i;
124
125 for (i = 0; i < 3; i++) {
126 pci_vpb_update_window(s, i);
127 }
128}
129
130static int pci_vpb_post_load(void *opaque, int version_id)
131{
132 PCIVPBState *s = opaque;
133 pci_vpb_update_all_windows(s);
134 return 0;
135}
136
137static const VMStateDescription pci_vpb_vmstate = {
138 .name = "versatile-pci",
139 .version_id = 1,
140 .minimum_version_id = 1,
141 .post_load = pci_vpb_post_load,
142 .fields = (VMStateField[]) {
143 VMSTATE_UINT32_ARRAY(imap, PCIVPBState, 3),
144 VMSTATE_UINT32_ARRAY(smap, PCIVPBState, 3),
145 VMSTATE_UINT32(selfid, PCIVPBState),
146 VMSTATE_UINT32(flags, PCIVPBState),
147 VMSTATE_UINT8(irq_mapping, PCIVPBState),
148 VMSTATE_END_OF_LIST()
149 }
150};
151
152#define TYPE_VERSATILE_PCI "versatile_pci"
153#define PCI_VPB(obj) \
154 OBJECT_CHECK(PCIVPBState, (obj), TYPE_VERSATILE_PCI)
155
156#define TYPE_VERSATILE_PCI_HOST "versatile_pci_host"
157#define PCI_VPB_HOST(obj) \
158 OBJECT_CHECK(PCIDevice, (obj), TYPE_VERSATILE_PCIHOST)
159
160typedef enum {
161 PCI_IMAP0 = 0x0,
162 PCI_IMAP1 = 0x4,
163 PCI_IMAP2 = 0x8,
164 PCI_SELFID = 0xc,
165 PCI_FLAGS = 0x10,
166 PCI_SMAP0 = 0x14,
167 PCI_SMAP1 = 0x18,
168 PCI_SMAP2 = 0x1c,
169} PCIVPBControlRegs;
170
171static void pci_vpb_reg_write(void *opaque, hwaddr addr,
172 uint64_t val, unsigned size)
173{
174 PCIVPBState *s = opaque;
175
176 switch (addr) {
177 case PCI_IMAP0:
178 case PCI_IMAP1:
179 case PCI_IMAP2:
180 {
181 int win = (addr - PCI_IMAP0) >> 2;
182 s->imap[win] = val;
183 pci_vpb_update_window(s, win);
184 break;
185 }
186 case PCI_SELFID:
187 s->selfid = val;
188 break;
189 case PCI_FLAGS:
190 s->flags = val;
191 break;
192 case PCI_SMAP0:
193 case PCI_SMAP1:
194 case PCI_SMAP2:
195 {
196 int win = (addr - PCI_SMAP0) >> 2;
197 s->smap[win] = val;
198 break;
199 }
200 default:
201 qemu_log_mask(LOG_GUEST_ERROR,
202 "pci_vpb_reg_write: Bad offset %x\n", (int)addr);
203 break;
204 }
205}
206
207static uint64_t pci_vpb_reg_read(void *opaque, hwaddr addr,
208 unsigned size)
209{
210 PCIVPBState *s = opaque;
211
212 switch (addr) {
213 case PCI_IMAP0:
214 case PCI_IMAP1:
215 case PCI_IMAP2:
216 {
217 int win = (addr - PCI_IMAP0) >> 2;
218 return s->imap[win];
219 }
220 case PCI_SELFID:
221 return s->selfid;
222 case PCI_FLAGS:
223 return s->flags;
224 case PCI_SMAP0:
225 case PCI_SMAP1:
226 case PCI_SMAP2:
227 {
228 int win = (addr - PCI_SMAP0) >> 2;
229 return s->smap[win];
230 }
231 default:
232 qemu_log_mask(LOG_GUEST_ERROR,
233 "pci_vpb_reg_read: Bad offset %x\n", (int)addr);
234 return 0;
235 }
236}
237
238static const MemoryRegionOps pci_vpb_reg_ops = {
239 .read = pci_vpb_reg_read,
240 .write = pci_vpb_reg_write,
241 .endianness = DEVICE_NATIVE_ENDIAN,
242 .valid = {
243 .min_access_size = 4,
244 .max_access_size = 4,
245 },
246};
247
248static int pci_vpb_broken_irq(int slot, int irq)
249{
250
251
252
253
254
255
256
257 slot %= PCI_NUM_PINS;
258
259 if (irq == 27) {
260 if (slot == 2) {
261
262
263
264 return PCI_VPB_IRQMAP_ASSUME_OK;
265 }
266
267 return PCI_VPB_IRQMAP_BROKEN;
268 }
269 if (irq == slot + 27) {
270
271 return PCI_VPB_IRQMAP_BROKEN;
272 }
273 if (irq == slot + 27 + 64) {
274
275 return PCI_VPB_IRQMAP_BROKEN;
276 }
277
278
279
280 return PCI_VPB_IRQMAP_FORCE_OK;
281}
282
283static void pci_vpb_config_write(void *opaque, hwaddr addr,
284 uint64_t val, unsigned size)
285{
286 PCIVPBState *s = opaque;
287 if (!s->realview && (addr & 0xff) == PCI_INTERRUPT_LINE
288 && s->irq_mapping == PCI_VPB_IRQMAP_ASSUME_OK) {
289 uint8_t devfn = addr >> 8;
290 s->irq_mapping = pci_vpb_broken_irq(PCI_SLOT(devfn), val);
291 }
292 pci_data_write(&s->pci_bus, addr, val, size);
293}
294
295static uint64_t pci_vpb_config_read(void *opaque, hwaddr addr,
296 unsigned size)
297{
298 PCIVPBState *s = opaque;
299 uint32_t val;
300 val = pci_data_read(&s->pci_bus, addr, size);
301 return val;
302}
303
304static const MemoryRegionOps pci_vpb_config_ops = {
305 .read = pci_vpb_config_read,
306 .write = pci_vpb_config_write,
307 .endianness = DEVICE_NATIVE_ENDIAN,
308};
309
310static int pci_vpb_map_irq(PCIDevice *d, int irq_num)
311{
312 PCIVPBState *s = container_of(d->bus, PCIVPBState, pci_bus);
313
314 if (s->irq_mapping == PCI_VPB_IRQMAP_BROKEN) {
315
316
317
318 return irq_num;
319 }
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 return pci_swizzle_map_irq_fn(d, irq_num + 2);
336}
337
338static int pci_vpb_rv_map_irq(PCIDevice *d, int irq_num)
339{
340
341
342
343
344
345
346
347
348
349
350
351
352 return pci_swizzle_map_irq_fn(d, irq_num + 3);
353}
354
355static void pci_vpb_set_irq(void *opaque, int irq_num, int level)
356{
357 qemu_irq *pic = opaque;
358
359 qemu_set_irq(pic[irq_num], level);
360}
361
362static void pci_vpb_reset(DeviceState *d)
363{
364 PCIVPBState *s = PCI_VPB(d);
365
366 s->imap[0] = 0;
367 s->imap[1] = 0;
368 s->imap[2] = 0;
369 s->smap[0] = 0;
370 s->smap[1] = 0;
371 s->smap[2] = 0;
372 s->selfid = 0;
373 s->flags = 0;
374 s->irq_mapping = s->irq_mapping_prop;
375
376 pci_vpb_update_all_windows(s);
377}
378
379static void pci_vpb_init(Object *obj)
380{
381 PCIHostState *h = PCI_HOST_BRIDGE(obj);
382 PCIVPBState *s = PCI_VPB(obj);
383
384 memory_region_init(&s->pci_io_space, OBJECT(s), "pci_io", 1ULL << 32);
385 memory_region_init(&s->pci_mem_space, OBJECT(s), "pci_mem", 1ULL << 32);
386
387 pci_bus_new_inplace(&s->pci_bus, sizeof(s->pci_bus), DEVICE(obj), "pci",
388 &s->pci_mem_space, &s->pci_io_space,
389 PCI_DEVFN(11, 0), TYPE_PCI_BUS);
390 h->bus = &s->pci_bus;
391
392 object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_VERSATILE_PCI_HOST);
393 qdev_set_parent_bus(DEVICE(&s->pci_dev), BUS(&s->pci_bus));
394
395
396 s->mem_win_size[0] = 0x0c000000;
397 s->mem_win_size[1] = 0x10000000;
398 s->mem_win_size[2] = 0x10000000;
399}
400
401static void pci_vpb_realize(DeviceState *dev, Error **errp)
402{
403 PCIVPBState *s = PCI_VPB(dev);
404 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
405 pci_map_irq_fn mapfn;
406 int i;
407
408 for (i = 0; i < 4; i++) {
409 sysbus_init_irq(sbd, &s->irq[i]);
410 }
411
412 if (s->realview) {
413 mapfn = pci_vpb_rv_map_irq;
414 } else {
415 mapfn = pci_vpb_map_irq;
416 }
417
418 pci_bus_irqs(&s->pci_bus, pci_vpb_set_irq, mapfn, s->irq, 4);
419
420
421
422
423
424
425
426
427 memory_region_init_io(&s->controlregs, OBJECT(s), &pci_vpb_reg_ops, s,
428 "pci-vpb-regs", 0x1000);
429 sysbus_init_mmio(sbd, &s->controlregs);
430 memory_region_init_io(&s->mem_config, OBJECT(s), &pci_vpb_config_ops, s,
431 "pci-vpb-selfconfig", 0x1000000);
432 sysbus_init_mmio(sbd, &s->mem_config);
433 memory_region_init_io(&s->mem_config2, OBJECT(s), &pci_vpb_config_ops, s,
434 "pci-vpb-config", 0x1000000);
435 sysbus_init_mmio(sbd, &s->mem_config2);
436
437
438
439
440 memory_region_init_alias(&s->pci_io_window, OBJECT(s), "pci-vbp-io-window",
441 &s->pci_io_space, 0, 0x100000);
442
443 sysbus_init_mmio(sbd, &s->pci_io_space);
444
445
446
447
448
449 for (i = 0; i < 3; i++) {
450 memory_region_init_alias(&s->pci_mem_window[i], OBJECT(s), "pci-vbp-window",
451 &s->pci_mem_space, 0, s->mem_win_size[i]);
452 sysbus_init_mmio(sbd, &s->pci_mem_window[i]);
453 }
454
455
456 object_property_set_bool(OBJECT(&s->pci_dev), true, "realized", errp);
457}
458
459static void versatile_pci_host_realize(PCIDevice *d, Error **errp)
460{
461 pci_set_word(d->config + PCI_STATUS,
462 PCI_STATUS_66MHZ | PCI_STATUS_DEVSEL_MEDIUM);
463 pci_set_byte(d->config + PCI_LATENCY_TIMER, 0x10);
464}
465
466static void versatile_pci_host_class_init(ObjectClass *klass, void *data)
467{
468 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
469 DeviceClass *dc = DEVICE_CLASS(klass);
470
471 k->realize = versatile_pci_host_realize;
472 k->vendor_id = PCI_VENDOR_ID_XILINX;
473 k->device_id = PCI_DEVICE_ID_XILINX_XC2VP30;
474 k->class_id = PCI_CLASS_PROCESSOR_CO;
475
476
477
478
479 dc->cannot_instantiate_with_device_add_yet = true;
480}
481
482static const TypeInfo versatile_pci_host_info = {
483 .name = TYPE_VERSATILE_PCI_HOST,
484 .parent = TYPE_PCI_DEVICE,
485 .instance_size = sizeof(PCIDevice),
486 .class_init = versatile_pci_host_class_init,
487};
488
489static Property pci_vpb_properties[] = {
490 DEFINE_PROP_UINT8("broken-irq-mapping", PCIVPBState, irq_mapping_prop,
491 PCI_VPB_IRQMAP_ASSUME_OK),
492 DEFINE_PROP_END_OF_LIST()
493};
494
495static void pci_vpb_class_init(ObjectClass *klass, void *data)
496{
497 DeviceClass *dc = DEVICE_CLASS(klass);
498
499 dc->realize = pci_vpb_realize;
500 dc->reset = pci_vpb_reset;
501 dc->vmsd = &pci_vpb_vmstate;
502 dc->props = pci_vpb_properties;
503}
504
505static const TypeInfo pci_vpb_info = {
506 .name = TYPE_VERSATILE_PCI,
507 .parent = TYPE_PCI_HOST_BRIDGE,
508 .instance_size = sizeof(PCIVPBState),
509 .instance_init = pci_vpb_init,
510 .class_init = pci_vpb_class_init,
511};
512
513static void pci_realview_init(Object *obj)
514{
515 PCIVPBState *s = PCI_VPB(obj);
516
517 s->realview = 1;
518
519 s->mem_win_size[0] = 0x01000000;
520 s->mem_win_size[1] = 0x04000000;
521 s->mem_win_size[2] = 0x08000000;
522}
523
524static const TypeInfo pci_realview_info = {
525 .name = "realview_pci",
526 .parent = TYPE_VERSATILE_PCI,
527 .instance_init = pci_realview_init,
528};
529
530static void versatile_pci_register_types(void)
531{
532 type_register_static(&pci_vpb_info);
533 type_register_static(&pci_realview_info);
534 type_register_static(&versatile_pci_host_info);
535}
536
537type_init(versatile_pci_register_types)
538