xref: /openbmc/qemu/hw/s390x/s390-pci-bus.c (revision 61b9251a)
1 /*
2  * s390 PCI BUS
3  *
4  * Copyright 2014 IBM Corp.
5  * Author(s): Frank Blaschka <frank.blaschka@de.ibm.com>
6  *            Hong Bo Li <lihbbj@cn.ibm.com>
7  *            Yi Min Zhao <zyimin@cn.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or (at
10  * your option) any later version. See the COPYING file in the top-level
11  * directory.
12  */
13 
14 #include "s390-pci-bus.h"
15 #include <hw/pci/pci_bus.h>
16 #include <hw/pci/msi.h>
17 #include <qemu/error-report.h>
18 
19 /* #define DEBUG_S390PCI_BUS */
20 #ifdef DEBUG_S390PCI_BUS
21 #define DPRINTF(fmt, ...) \
22     do { fprintf(stderr, "S390pci-bus: " fmt, ## __VA_ARGS__); } while (0)
23 #else
24 #define DPRINTF(fmt, ...) \
25     do { } while (0)
26 #endif
27 
28 int chsc_sei_nt2_get_event(void *res)
29 {
30     ChscSeiNt2Res *nt2_res = (ChscSeiNt2Res *)res;
31     PciCcdfAvail *accdf;
32     PciCcdfErr *eccdf;
33     int rc = 1;
34     SeiContainer *sei_cont;
35     S390pciState *s = S390_PCI_HOST_BRIDGE(
36         object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
37 
38     if (!s) {
39         return rc;
40     }
41 
42     sei_cont = QTAILQ_FIRST(&s->pending_sei);
43     if (sei_cont) {
44         QTAILQ_REMOVE(&s->pending_sei, sei_cont, link);
45         nt2_res->nt = 2;
46         nt2_res->cc = sei_cont->cc;
47         nt2_res->length = cpu_to_be16(sizeof(ChscSeiNt2Res));
48         switch (sei_cont->cc) {
49         case 1: /* error event */
50             eccdf = (PciCcdfErr *)nt2_res->ccdf;
51             eccdf->fid = cpu_to_be32(sei_cont->fid);
52             eccdf->fh = cpu_to_be32(sei_cont->fh);
53             eccdf->e = cpu_to_be32(sei_cont->e);
54             eccdf->faddr = cpu_to_be64(sei_cont->faddr);
55             eccdf->pec = cpu_to_be16(sei_cont->pec);
56             break;
57         case 2: /* availability event */
58             accdf = (PciCcdfAvail *)nt2_res->ccdf;
59             accdf->fid = cpu_to_be32(sei_cont->fid);
60             accdf->fh = cpu_to_be32(sei_cont->fh);
61             accdf->pec = cpu_to_be16(sei_cont->pec);
62             break;
63         default:
64             abort();
65         }
66         g_free(sei_cont);
67         rc = 0;
68     }
69 
70     return rc;
71 }
72 
73 int chsc_sei_nt2_have_event(void)
74 {
75     S390pciState *s = S390_PCI_HOST_BRIDGE(
76         object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
77 
78     if (!s) {
79         return 0;
80     }
81 
82     return !QTAILQ_EMPTY(&s->pending_sei);
83 }
84 
85 S390PCIBusDevice *s390_pci_find_dev_by_fid(uint32_t fid)
86 {
87     S390PCIBusDevice *pbdev;
88     int i;
89     S390pciState *s = S390_PCI_HOST_BRIDGE(
90         object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
91 
92     if (!s) {
93         return NULL;
94     }
95 
96     for (i = 0; i < PCI_SLOT_MAX; i++) {
97         pbdev = &s->pbdev[i];
98         if ((pbdev->fh != 0) && (pbdev->fid == fid)) {
99             return pbdev;
100         }
101     }
102 
103     return NULL;
104 }
105 
106 void s390_pci_sclp_configure(int configure, SCCB *sccb)
107 {
108     PciCfgSccb *psccb = (PciCfgSccb *)sccb;
109     S390PCIBusDevice *pbdev = s390_pci_find_dev_by_fid(be32_to_cpu(psccb->aid));
110     uint16_t rc;
111 
112     if (pbdev) {
113         if ((configure == 1 && pbdev->configured == true) ||
114             (configure == 0 && pbdev->configured == false)) {
115             rc = SCLP_RC_NO_ACTION_REQUIRED;
116         } else {
117             pbdev->configured = !pbdev->configured;
118             rc = SCLP_RC_NORMAL_COMPLETION;
119         }
120     } else {
121         DPRINTF("sclp config %d no dev found\n", configure);
122         rc = SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED;
123     }
124 
125     psccb->header.response_code = cpu_to_be16(rc);
126     return;
127 }
128 
129 static uint32_t s390_pci_get_pfid(PCIDevice *pdev)
130 {
131     return PCI_SLOT(pdev->devfn);
132 }
133 
134 static uint32_t s390_pci_get_pfh(PCIDevice *pdev)
135 {
136     return PCI_SLOT(pdev->devfn) | FH_VIRT;
137 }
138 
139 S390PCIBusDevice *s390_pci_find_dev_by_idx(uint32_t idx)
140 {
141     S390PCIBusDevice *pbdev;
142     int i;
143     int j = 0;
144     S390pciState *s = S390_PCI_HOST_BRIDGE(
145         object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
146 
147     if (!s) {
148         return NULL;
149     }
150 
151     for (i = 0; i < PCI_SLOT_MAX; i++) {
152         pbdev = &s->pbdev[i];
153 
154         if (pbdev->fh == 0) {
155             continue;
156         }
157 
158         if (j == idx) {
159             return pbdev;
160         }
161         j++;
162     }
163 
164     return NULL;
165 }
166 
167 S390PCIBusDevice *s390_pci_find_dev_by_fh(uint32_t fh)
168 {
169     S390PCIBusDevice *pbdev;
170     int i;
171     S390pciState *s = S390_PCI_HOST_BRIDGE(
172         object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
173 
174     if (!s || !fh) {
175         return NULL;
176     }
177 
178     for (i = 0; i < PCI_SLOT_MAX; i++) {
179         pbdev = &s->pbdev[i];
180         if (pbdev->fh == fh) {
181             return pbdev;
182         }
183     }
184 
185     return NULL;
186 }
187 
188 static void s390_pci_generate_event(uint8_t cc, uint16_t pec, uint32_t fh,
189                                     uint32_t fid, uint64_t faddr, uint32_t e)
190 {
191     SeiContainer *sei_cont;
192     S390pciState *s = S390_PCI_HOST_BRIDGE(
193         object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
194 
195     if (!s) {
196         return;
197     }
198 
199     sei_cont = g_malloc0(sizeof(SeiContainer));
200     sei_cont->fh = fh;
201     sei_cont->fid = fid;
202     sei_cont->cc = cc;
203     sei_cont->pec = pec;
204     sei_cont->faddr = faddr;
205     sei_cont->e = e;
206 
207     QTAILQ_INSERT_TAIL(&s->pending_sei, sei_cont, link);
208     css_generate_css_crws(0);
209 }
210 
211 static void s390_pci_generate_plug_event(uint16_t pec, uint32_t fh,
212                                          uint32_t fid)
213 {
214     s390_pci_generate_event(2, pec, fh, fid, 0, 0);
215 }
216 
217 static void s390_pci_generate_error_event(uint16_t pec, uint32_t fh,
218                                           uint32_t fid, uint64_t faddr,
219                                           uint32_t e)
220 {
221     s390_pci_generate_event(1, pec, fh, fid, faddr, e);
222 }
223 
224 static void s390_pci_set_irq(void *opaque, int irq, int level)
225 {
226     /* nothing to do */
227 }
228 
229 static int s390_pci_map_irq(PCIDevice *pci_dev, int irq_num)
230 {
231     /* nothing to do */
232     return 0;
233 }
234 
235 static uint64_t s390_pci_get_table_origin(uint64_t iota)
236 {
237     return iota & ~ZPCI_IOTA_RTTO_FLAG;
238 }
239 
240 static unsigned int calc_rtx(dma_addr_t ptr)
241 {
242     return ((unsigned long) ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
243 }
244 
245 static unsigned int calc_sx(dma_addr_t ptr)
246 {
247     return ((unsigned long) ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
248 }
249 
250 static unsigned int calc_px(dma_addr_t ptr)
251 {
252     return ((unsigned long) ptr >> PAGE_SHIFT) & ZPCI_PT_MASK;
253 }
254 
255 static uint64_t get_rt_sto(uint64_t entry)
256 {
257     return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
258                 ? (entry & ZPCI_RTE_ADDR_MASK)
259                 : 0;
260 }
261 
262 static uint64_t get_st_pto(uint64_t entry)
263 {
264     return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
265             ? (entry & ZPCI_STE_ADDR_MASK)
266             : 0;
267 }
268 
269 static uint64_t s390_guest_io_table_walk(uint64_t guest_iota,
270                                   uint64_t guest_dma_address)
271 {
272     uint64_t sto_a, pto_a, px_a;
273     uint64_t sto, pto, pte;
274     uint32_t rtx, sx, px;
275 
276     rtx = calc_rtx(guest_dma_address);
277     sx = calc_sx(guest_dma_address);
278     px = calc_px(guest_dma_address);
279 
280     sto_a = guest_iota + rtx * sizeof(uint64_t);
281     sto = address_space_ldq(&address_space_memory, sto_a,
282                             MEMTXATTRS_UNSPECIFIED, NULL);
283     sto = get_rt_sto(sto);
284     if (!sto) {
285         pte = 0;
286         goto out;
287     }
288 
289     pto_a = sto + sx * sizeof(uint64_t);
290     pto = address_space_ldq(&address_space_memory, pto_a,
291                             MEMTXATTRS_UNSPECIFIED, NULL);
292     pto = get_st_pto(pto);
293     if (!pto) {
294         pte = 0;
295         goto out;
296     }
297 
298     px_a = pto + px * sizeof(uint64_t);
299     pte = address_space_ldq(&address_space_memory, px_a,
300                             MEMTXATTRS_UNSPECIFIED, NULL);
301 
302 out:
303     return pte;
304 }
305 
306 static IOMMUTLBEntry s390_translate_iommu(MemoryRegion *iommu, hwaddr addr,
307                                           bool is_write)
308 {
309     uint64_t pte;
310     uint32_t flags;
311     S390PCIBusDevice *pbdev = container_of(iommu, S390PCIBusDevice, mr);
312     S390pciState *s;
313     IOMMUTLBEntry ret = {
314         .target_as = &address_space_memory,
315         .iova = 0,
316         .translated_addr = 0,
317         .addr_mask = ~(hwaddr)0,
318         .perm = IOMMU_NONE,
319     };
320 
321     if (!pbdev->configured || !pbdev->pdev) {
322         return ret;
323     }
324 
325     DPRINTF("iommu trans addr 0x%" PRIx64 "\n", addr);
326 
327     s = S390_PCI_HOST_BRIDGE(pci_device_root_bus(pbdev->pdev)->qbus.parent);
328     /* s390 does not have an APIC mapped to main storage so we use
329      * a separate AddressSpace only for msix notifications
330      */
331     if (addr == ZPCI_MSI_ADDR) {
332         ret.target_as = &s->msix_notify_as;
333         ret.iova = addr;
334         ret.translated_addr = addr;
335         ret.addr_mask = 0xfff;
336         ret.perm = IOMMU_RW;
337         return ret;
338     }
339 
340     if (!pbdev->g_iota) {
341         pbdev->error_state = true;
342         pbdev->lgstg_blocked = true;
343         s390_pci_generate_error_event(ERR_EVENT_INVALAS, pbdev->fh, pbdev->fid,
344                                       addr, 0);
345         return ret;
346     }
347 
348     if (addr < pbdev->pba || addr > pbdev->pal) {
349         pbdev->error_state = true;
350         pbdev->lgstg_blocked = true;
351         s390_pci_generate_error_event(ERR_EVENT_OORANGE, pbdev->fh, pbdev->fid,
352                                       addr, 0);
353         return ret;
354     }
355 
356     pte = s390_guest_io_table_walk(s390_pci_get_table_origin(pbdev->g_iota),
357                                    addr);
358 
359     if (!pte) {
360         pbdev->error_state = true;
361         pbdev->lgstg_blocked = true;
362         s390_pci_generate_error_event(ERR_EVENT_SERR, pbdev->fh, pbdev->fid,
363                                       addr, ERR_EVENT_Q_BIT);
364         return ret;
365     }
366 
367     flags = pte & ZPCI_PTE_FLAG_MASK;
368     ret.iova = addr;
369     ret.translated_addr = pte & ZPCI_PTE_ADDR_MASK;
370     ret.addr_mask = 0xfff;
371 
372     if (flags & ZPCI_PTE_INVALID) {
373         ret.perm = IOMMU_NONE;
374     } else {
375         ret.perm = IOMMU_RW;
376     }
377 
378     return ret;
379 }
380 
381 static const MemoryRegionIOMMUOps s390_iommu_ops = {
382     .translate = s390_translate_iommu,
383 };
384 
385 static AddressSpace *s390_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
386 {
387     S390pciState *s = opaque;
388 
389     return &s->pbdev[PCI_SLOT(devfn)].as;
390 }
391 
392 static uint8_t set_ind_atomic(uint64_t ind_loc, uint8_t to_be_set)
393 {
394     uint8_t ind_old, ind_new;
395     hwaddr len = 1;
396     uint8_t *ind_addr;
397 
398     ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
399     if (!ind_addr) {
400         s390_pci_generate_error_event(ERR_EVENT_AIRERR, 0, 0, 0, 0);
401         return -1;
402     }
403     do {
404         ind_old = *ind_addr;
405         ind_new = ind_old | to_be_set;
406     } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
407     cpu_physical_memory_unmap(ind_addr, len, 1, len);
408 
409     return ind_old;
410 }
411 
412 static void s390_msi_ctrl_write(void *opaque, hwaddr addr, uint64_t data,
413                                 unsigned int size)
414 {
415     S390PCIBusDevice *pbdev;
416     uint32_t io_int_word;
417     uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
418     uint32_t vec = data & ZPCI_MSI_VEC_MASK;
419     uint64_t ind_bit;
420     uint32_t sum_bit;
421     uint32_t e = 0;
422 
423     DPRINTF("write_msix data 0x%" PRIx64 " fid %d vec 0x%x\n", data, fid, vec);
424 
425     pbdev = s390_pci_find_dev_by_fid(fid);
426     if (!pbdev) {
427         e |= (vec << ERR_EVENT_MVN_OFFSET);
428         s390_pci_generate_error_event(ERR_EVENT_NOMSI, 0, fid, addr, e);
429         return;
430     }
431 
432     ind_bit = pbdev->routes.adapter.ind_offset;
433     sum_bit = pbdev->routes.adapter.summary_offset;
434 
435     set_ind_atomic(pbdev->routes.adapter.ind_addr + (ind_bit + vec) / 8,
436                    0x80 >> ((ind_bit + vec) % 8));
437     if (!set_ind_atomic(pbdev->routes.adapter.summary_addr + sum_bit / 8,
438                                        0x80 >> (sum_bit % 8))) {
439         io_int_word = (pbdev->isc << 27) | IO_INT_WORD_AI;
440         s390_io_interrupt(0, 0, 0, io_int_word);
441     }
442 
443     return;
444 }
445 
446 static uint64_t s390_msi_ctrl_read(void *opaque, hwaddr addr, unsigned size)
447 {
448     return 0xffffffff;
449 }
450 
451 static const MemoryRegionOps s390_msi_ctrl_ops = {
452     .write = s390_msi_ctrl_write,
453     .read = s390_msi_ctrl_read,
454     .endianness = DEVICE_LITTLE_ENDIAN,
455 };
456 
457 static void s390_pcihost_init_as(S390pciState *s)
458 {
459     int i;
460 
461     for (i = 0; i < PCI_SLOT_MAX; i++) {
462         memory_region_init_iommu(&s->pbdev[i].mr, OBJECT(s),
463                                  &s390_iommu_ops, "iommu-s390", UINT64_MAX);
464         address_space_init(&s->pbdev[i].as, &s->pbdev[i].mr, "iommu-pci");
465     }
466 
467     memory_region_init_io(&s->msix_notify_mr, OBJECT(s),
468                           &s390_msi_ctrl_ops, s, "msix-s390", UINT64_MAX);
469     address_space_init(&s->msix_notify_as, &s->msix_notify_mr, "msix-pci");
470 }
471 
472 static int s390_pcihost_init(SysBusDevice *dev)
473 {
474     PCIBus *b;
475     BusState *bus;
476     PCIHostState *phb = PCI_HOST_BRIDGE(dev);
477     S390pciState *s = S390_PCI_HOST_BRIDGE(dev);
478 
479     DPRINTF("host_init\n");
480 
481     b = pci_register_bus(DEVICE(dev), NULL,
482                          s390_pci_set_irq, s390_pci_map_irq, NULL,
483                          get_system_memory(), get_system_io(), 0, 64,
484                          TYPE_PCI_BUS);
485     s390_pcihost_init_as(s);
486     pci_setup_iommu(b, s390_pci_dma_iommu, s);
487 
488     bus = BUS(b);
489     qbus_set_hotplug_handler(bus, DEVICE(dev), NULL);
490     phb->bus = b;
491     QTAILQ_INIT(&s->pending_sei);
492     return 0;
493 }
494 
495 static int s390_pcihost_setup_msix(S390PCIBusDevice *pbdev)
496 {
497     uint8_t pos;
498     uint16_t ctrl;
499     uint32_t table, pba;
500 
501     pos = pci_find_capability(pbdev->pdev, PCI_CAP_ID_MSIX);
502     if (!pos) {
503         pbdev->msix.available = false;
504         return 0;
505     }
506 
507     ctrl = pci_host_config_read_common(pbdev->pdev, pos + PCI_CAP_FLAGS,
508              pci_config_size(pbdev->pdev), sizeof(ctrl));
509     table = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_TABLE,
510              pci_config_size(pbdev->pdev), sizeof(table));
511     pba = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_PBA,
512              pci_config_size(pbdev->pdev), sizeof(pba));
513 
514     pbdev->msix.table_bar = table & PCI_MSIX_FLAGS_BIRMASK;
515     pbdev->msix.table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
516     pbdev->msix.pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK;
517     pbdev->msix.pba_offset = pba & ~PCI_MSIX_FLAGS_BIRMASK;
518     pbdev->msix.entries = (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
519     pbdev->msix.available = true;
520     return 0;
521 }
522 
523 static void s390_pcihost_hot_plug(HotplugHandler *hotplug_dev,
524                                   DeviceState *dev, Error **errp)
525 {
526     PCIDevice *pci_dev = PCI_DEVICE(dev);
527     S390PCIBusDevice *pbdev;
528     S390pciState *s = S390_PCI_HOST_BRIDGE(pci_device_root_bus(pci_dev)
529                                            ->qbus.parent);
530 
531     pbdev = &s->pbdev[PCI_SLOT(pci_dev->devfn)];
532 
533     pbdev->fid = s390_pci_get_pfid(pci_dev);
534     pbdev->pdev = pci_dev;
535     pbdev->configured = true;
536     pbdev->fh = s390_pci_get_pfh(pci_dev);
537 
538     s390_pcihost_setup_msix(pbdev);
539 
540     if (dev->hotplugged) {
541         s390_pci_generate_plug_event(HP_EVENT_RESERVED_TO_STANDBY,
542                                      pbdev->fh, pbdev->fid);
543         s390_pci_generate_plug_event(HP_EVENT_TO_CONFIGURED,
544                                      pbdev->fh, pbdev->fid);
545     }
546     return;
547 }
548 
549 static void s390_pcihost_hot_unplug(HotplugHandler *hotplug_dev,
550                                     DeviceState *dev, Error **errp)
551 {
552     PCIDevice *pci_dev = PCI_DEVICE(dev);
553     S390pciState *s = S390_PCI_HOST_BRIDGE(pci_device_root_bus(pci_dev)
554                                            ->qbus.parent);
555     S390PCIBusDevice *pbdev = &s->pbdev[PCI_SLOT(pci_dev->devfn)];
556 
557     if (pbdev->configured) {
558         pbdev->configured = false;
559         s390_pci_generate_plug_event(HP_EVENT_CONFIGURED_TO_STBRES,
560                                      pbdev->fh, pbdev->fid);
561     }
562 
563     s390_pci_generate_plug_event(HP_EVENT_STANDBY_TO_RESERVED,
564                                  pbdev->fh, pbdev->fid);
565     pbdev->fh = 0;
566     pbdev->fid = 0;
567     pbdev->pdev = NULL;
568     object_unparent(OBJECT(pci_dev));
569 }
570 
571 static void s390_pcihost_class_init(ObjectClass *klass, void *data)
572 {
573     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
574     DeviceClass *dc = DEVICE_CLASS(klass);
575     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
576 
577     dc->cannot_instantiate_with_device_add_yet = true;
578     k->init = s390_pcihost_init;
579     hc->plug = s390_pcihost_hot_plug;
580     hc->unplug = s390_pcihost_hot_unplug;
581     msi_supported = true;
582 }
583 
584 static const TypeInfo s390_pcihost_info = {
585     .name          = TYPE_S390_PCI_HOST_BRIDGE,
586     .parent        = TYPE_PCI_HOST_BRIDGE,
587     .instance_size = sizeof(S390pciState),
588     .class_init    = s390_pcihost_class_init,
589     .interfaces = (InterfaceInfo[]) {
590         { TYPE_HOTPLUG_HANDLER },
591         { }
592     }
593 };
594 
595 static void s390_pci_register_types(void)
596 {
597     type_register_static(&s390_pcihost_info);
598 }
599 
600 type_init(s390_pci_register_types)
601