s390-pci-bus.c 27.6 KB
Newer Older
F
Frank Blaschka 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * s390 PCI BUS
 *
 * Copyright 2014 IBM Corp.
 * Author(s): Frank Blaschka <frank.blaschka@de.ibm.com>
 *            Hong Bo Li <lihbbj@cn.ibm.com>
 *            Yi Min Zhao <zyimin@cn.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or (at
 * your option) any later version. See the COPYING file in the top-level
 * directory.
 */

P
Peter Maydell 已提交
14
#include "qemu/osdep.h"
15 16
#include "qapi/error.h"
#include "qapi/visitor.h"
17 18
#include "qemu-common.h"
#include "cpu.h"
F
Frank Blaschka 已提交
19
#include "s390-pci-bus.h"
20
#include "s390-pci-inst.h"
21
#include "hw/pci/pci_bus.h"
22
#include "hw/pci/pci_bridge.h"
23 24
#include "hw/pci/msi.h"
#include "qemu/error-report.h"
F
Frank Blaschka 已提交
25 26 27 28 29 30 31 32 33 34

/* #define DEBUG_S390PCI_BUS */
#ifdef DEBUG_S390PCI_BUS
#define DPRINTF(fmt, ...) \
    do { fprintf(stderr, "S390pci-bus: " fmt, ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) \
    do { } while (0)
#endif

35
S390pciState *s390_get_phb(void)
36 37 38 39 40 41 42 43 44 45 46 47
{
    static S390pciState *phb;

    if (!phb) {
        phb = S390_PCI_HOST_BRIDGE(
            object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
        assert(phb != NULL);
    }

    return phb;
}

F
Frank Blaschka 已提交
48 49 50 51 52 53 54
int chsc_sei_nt2_get_event(void *res)
{
    ChscSeiNt2Res *nt2_res = (ChscSeiNt2Res *)res;
    PciCcdfAvail *accdf;
    PciCcdfErr *eccdf;
    int rc = 1;
    SeiContainer *sei_cont;
55
    S390pciState *s = s390_get_phb();
F
Frank Blaschka 已提交
56 57 58 59 60 61

    sei_cont = QTAILQ_FIRST(&s->pending_sei);
    if (sei_cont) {
        QTAILQ_REMOVE(&s->pending_sei, sei_cont, link);
        nt2_res->nt = 2;
        nt2_res->cc = sei_cont->cc;
62
        nt2_res->length = cpu_to_be16(sizeof(ChscSeiNt2Res));
F
Frank Blaschka 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        switch (sei_cont->cc) {
        case 1: /* error event */
            eccdf = (PciCcdfErr *)nt2_res->ccdf;
            eccdf->fid = cpu_to_be32(sei_cont->fid);
            eccdf->fh = cpu_to_be32(sei_cont->fh);
            eccdf->e = cpu_to_be32(sei_cont->e);
            eccdf->faddr = cpu_to_be64(sei_cont->faddr);
            eccdf->pec = cpu_to_be16(sei_cont->pec);
            break;
        case 2: /* availability event */
            accdf = (PciCcdfAvail *)nt2_res->ccdf;
            accdf->fid = cpu_to_be32(sei_cont->fid);
            accdf->fh = cpu_to_be32(sei_cont->fh);
            accdf->pec = cpu_to_be16(sei_cont->pec);
            break;
        default:
            abort();
        }
        g_free(sei_cont);
        rc = 0;
    }

    return rc;
}

int chsc_sei_nt2_have_event(void)
{
90
    S390pciState *s = s390_get_phb();
F
Frank Blaschka 已提交
91 92 93 94

    return !QTAILQ_EMPTY(&s->pending_sei);
}

95 96
S390PCIBusDevice *s390_pci_find_next_avail_dev(S390pciState *s,
                                               S390PCIBusDevice *pbdev)
Y
Yi Min Zhao 已提交
97
{
98 99
    S390PCIBusDevice *ret = pbdev ? QTAILQ_NEXT(pbdev, link) :
        QTAILQ_FIRST(&s->zpci_devs);
Y
Yi Min Zhao 已提交
100

101 102
    while (ret && ret->state == ZPCI_FS_RESERVED) {
        ret = QTAILQ_NEXT(ret, link);
Y
Yi Min Zhao 已提交
103 104
    }

105
    return ret;
Y
Yi Min Zhao 已提交
106 107
}

108
S390PCIBusDevice *s390_pci_find_dev_by_fid(S390pciState *s, uint32_t fid)
F
Frank Blaschka 已提交
109 110 111
{
    S390PCIBusDevice *pbdev;

112 113
    QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
        if (pbdev->fid == fid) {
F
Frank Blaschka 已提交
114 115 116 117 118 119 120
            return pbdev;
        }
    }

    return NULL;
}

121
void s390_pci_sclp_configure(SCCB *sccb)
F
Frank Blaschka 已提交
122 123
{
    PciCfgSccb *psccb = (PciCfgSccb *)sccb;
124 125
    S390PCIBusDevice *pbdev = s390_pci_find_dev_by_fid(s390_get_phb(),
                                                       be32_to_cpu(psccb->aid));
F
Frank Blaschka 已提交
126 127
    uint16_t rc;

128 129 130 131 132
    if (be16_to_cpu(sccb->h.length) < 16) {
        rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH;
        goto out;
    }

133
    if (!pbdev) {
134 135
        DPRINTF("sclp config no dev found\n");
        rc = SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED;
136 137 138 139 140 141 142 143 144 145 146 147 148
        goto out;
    }

    switch (pbdev->state) {
    case ZPCI_FS_RESERVED:
        rc = SCLP_RC_ADAPTER_IN_RESERVED_STATE;
        break;
    case ZPCI_FS_STANDBY:
        pbdev->state = ZPCI_FS_DISABLED;
        rc = SCLP_RC_NORMAL_COMPLETION;
        break;
    default:
        rc = SCLP_RC_NO_ACTION_REQUIRED;
149
    }
150
out:
151 152 153 154 155 156
    psccb->header.response_code = cpu_to_be16(rc);
}

void s390_pci_sclp_deconfigure(SCCB *sccb)
{
    PciCfgSccb *psccb = (PciCfgSccb *)sccb;
157 158
    S390PCIBusDevice *pbdev = s390_pci_find_dev_by_fid(s390_get_phb(),
                                                       be32_to_cpu(psccb->aid));
159 160
    uint16_t rc;

161 162 163 164 165
    if (be16_to_cpu(sccb->h.length) < 16) {
        rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH;
        goto out;
    }

166
    if (!pbdev) {
167
        DPRINTF("sclp deconfig no dev found\n");
F
Frank Blaschka 已提交
168
        rc = SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED;
169 170 171 172 173 174 175 176 177 178 179 180 181 182
        goto out;
    }

    switch (pbdev->state) {
    case ZPCI_FS_RESERVED:
        rc = SCLP_RC_ADAPTER_IN_RESERVED_STATE;
        break;
    case ZPCI_FS_STANDBY:
        rc = SCLP_RC_NO_ACTION_REQUIRED;
        break;
    default:
        if (pbdev->summary_ind) {
            pci_dereg_irqs(pbdev);
        }
183 184
        if (pbdev->iommu->enabled) {
            pci_dereg_ioat(pbdev->iommu);
185 186 187
        }
        pbdev->state = ZPCI_FS_STANDBY;
        rc = SCLP_RC_NORMAL_COMPLETION;
188 189 190 191

        if (pbdev->release_timer) {
            qdev_unplug(DEVICE(pbdev->pdev), NULL);
        }
F
Frank Blaschka 已提交
192
    }
193
out:
F
Frank Blaschka 已提交
194 195 196
    psccb->header.response_code = cpu_to_be16(rc);
}

197
static S390PCIBusDevice *s390_pci_find_dev_by_uid(S390pciState *s, uint16_t uid)
198 199 200
{
    S390PCIBusDevice *pbdev;

201
    QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
202 203 204 205 206 207 208 209
        if (pbdev->uid == uid) {
            return pbdev;
        }
    }

    return NULL;
}

210 211
static S390PCIBusDevice *s390_pci_find_dev_by_target(S390pciState *s,
                                                     const char *target)
212 213 214 215 216 217 218
{
    S390PCIBusDevice *pbdev;

    if (!target) {
        return NULL;
    }

219
    QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
220 221 222 223 224 225 226 227
        if (!strcmp(pbdev->target, target)) {
            return pbdev;
        }
    }

    return NULL;
}

228
S390PCIBusDevice *s390_pci_find_dev_by_idx(S390pciState *s, uint32_t idx)
F
Frank Blaschka 已提交
229
{
230
    S390PCIBusDevice *pbdev;
F
Frank Blaschka 已提交
231

232 233 234 235 236 237 238
    QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
        if (pbdev->idx == idx) {
            return pbdev;
        }
    }

    return NULL;
F
Frank Blaschka 已提交
239 240
}

241
S390PCIBusDevice *s390_pci_find_dev_by_fh(S390pciState *s, uint32_t fh)
F
Frank Blaschka 已提交
242
{
243
    S390PCIBusDevice *pbdev;
F
Frank Blaschka 已提交
244

245 246 247 248
    QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
        if (pbdev->fh == fh) {
            return pbdev;
        }
F
Frank Blaschka 已提交
249 250 251 252 253 254 255 256
    }

    return NULL;
}

static void s390_pci_generate_event(uint8_t cc, uint16_t pec, uint32_t fh,
                                    uint32_t fid, uint64_t faddr, uint32_t e)
{
257
    SeiContainer *sei_cont;
258
    S390pciState *s = s390_get_phb();
F
Frank Blaschka 已提交
259

260
    sei_cont = g_malloc0(sizeof(SeiContainer));
F
Frank Blaschka 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    sei_cont->fh = fh;
    sei_cont->fid = fid;
    sei_cont->cc = cc;
    sei_cont->pec = pec;
    sei_cont->faddr = faddr;
    sei_cont->e = e;

    QTAILQ_INSERT_TAIL(&s->pending_sei, sei_cont, link);
    css_generate_css_crws(0);
}

static void s390_pci_generate_plug_event(uint16_t pec, uint32_t fh,
                                         uint32_t fid)
{
    s390_pci_generate_event(2, pec, fh, fid, 0, 0);
}

278 279
void s390_pci_generate_error_event(uint16_t pec, uint32_t fh, uint32_t fid,
                                   uint64_t faddr, uint32_t e)
F
Frank Blaschka 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
{
    s390_pci_generate_event(1, pec, fh, fid, faddr, e);
}

static void s390_pci_set_irq(void *opaque, int irq, int level)
{
    /* nothing to do */
}

static int s390_pci_map_irq(PCIDevice *pci_dev, int irq_num)
{
    /* nothing to do */
    return 0;
}

static uint64_t s390_pci_get_table_origin(uint64_t iota)
{
    return iota & ~ZPCI_IOTA_RTTO_FLAG;
}

static unsigned int calc_rtx(dma_addr_t ptr)
{
    return ((unsigned long) ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
}

static unsigned int calc_sx(dma_addr_t ptr)
{
    return ((unsigned long) ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
}

static unsigned int calc_px(dma_addr_t ptr)
{
    return ((unsigned long) ptr >> PAGE_SHIFT) & ZPCI_PT_MASK;
}

static uint64_t get_rt_sto(uint64_t entry)
{
    return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
                ? (entry & ZPCI_RTE_ADDR_MASK)
                : 0;
}

static uint64_t get_st_pto(uint64_t entry)
{
    return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
            ? (entry & ZPCI_STE_ADDR_MASK)
            : 0;
}

static uint64_t s390_guest_io_table_walk(uint64_t guest_iota,
                                  uint64_t guest_dma_address)
{
    uint64_t sto_a, pto_a, px_a;
    uint64_t sto, pto, pte;
    uint32_t rtx, sx, px;

    rtx = calc_rtx(guest_dma_address);
    sx = calc_sx(guest_dma_address);
    px = calc_px(guest_dma_address);

    sto_a = guest_iota + rtx * sizeof(uint64_t);
341 342
    sto = address_space_ldq(&address_space_memory, sto_a,
                            MEMTXATTRS_UNSPECIFIED, NULL);
F
Frank Blaschka 已提交
343 344 345 346 347 348 349
    sto = get_rt_sto(sto);
    if (!sto) {
        pte = 0;
        goto out;
    }

    pto_a = sto + sx * sizeof(uint64_t);
350 351
    pto = address_space_ldq(&address_space_memory, pto_a,
                            MEMTXATTRS_UNSPECIFIED, NULL);
F
Frank Blaschka 已提交
352 353 354 355 356 357 358
    pto = get_st_pto(pto);
    if (!pto) {
        pte = 0;
        goto out;
    }

    px_a = pto + px * sizeof(uint64_t);
359 360
    pte = address_space_ldq(&address_space_memory, px_a,
                            MEMTXATTRS_UNSPECIFIED, NULL);
F
Frank Blaschka 已提交
361 362 363 364 365

out:
    return pte;
}

366
static IOMMUTLBEntry s390_translate_iommu(MemoryRegion *mr, hwaddr addr,
F
Frank Blaschka 已提交
367 368 369 370
                                          bool is_write)
{
    uint64_t pte;
    uint32_t flags;
371
    S390PCIIOMMU *iommu = container_of(mr, S390PCIIOMMU, iommu_mr);
F
Frank Blaschka 已提交
372 373 374 375 376 377 378 379
    IOMMUTLBEntry ret = {
        .target_as = &address_space_memory,
        .iova = 0,
        .translated_addr = 0,
        .addr_mask = ~(hwaddr)0,
        .perm = IOMMU_NONE,
    };

380
    switch (iommu->pbdev->state) {
381 382
    case ZPCI_FS_ENABLED:
    case ZPCI_FS_BLOCKED:
383
        if (!iommu->enabled) {
384 385 386 387
            return ret;
        }
        break;
    default:
388 389 390
        return ret;
    }

F
Frank Blaschka 已提交
391 392
    DPRINTF("iommu trans addr 0x%" PRIx64 "\n", addr);

393
    if (addr < iommu->pba || addr > iommu->pal) {
F
Frank Blaschka 已提交
394 395 396
        return ret;
    }

397
    pte = s390_guest_io_table_walk(s390_pci_get_table_origin(iommu->g_iota),
F
Frank Blaschka 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
                                   addr);
    if (!pte) {
        return ret;
    }

    flags = pte & ZPCI_PTE_FLAG_MASK;
    ret.iova = addr;
    ret.translated_addr = pte & ZPCI_PTE_ADDR_MASK;
    ret.addr_mask = 0xfff;

    if (flags & ZPCI_PTE_INVALID) {
        ret.perm = IOMMU_NONE;
    } else {
        ret.perm = IOMMU_RW;
    }

    return ret;
}

static const MemoryRegionIOMMUOps s390_iommu_ops = {
    .translate = s390_translate_iommu,
};

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static S390PCIIOMMU *s390_pci_get_iommu(S390pciState *s, PCIBus *bus,
                                        int devfn)
{
    uint64_t key = (uintptr_t)bus;
    S390PCIIOMMUTable *table = g_hash_table_lookup(s->iommu_table, &key);
    S390PCIIOMMU *iommu;

    if (!table) {
        table = g_malloc0(sizeof(S390PCIIOMMUTable));
        table->key = key;
        g_hash_table_insert(s->iommu_table, &table->key, table);
    }

    iommu = table->iommu[PCI_SLOT(devfn)];
    if (!iommu) {
        iommu = S390_PCI_IOMMU(object_new(TYPE_S390_PCI_IOMMU));

        char *mr_name = g_strdup_printf("iommu-root-%02x:%02x.%01x",
                                        pci_bus_num(bus),
                                        PCI_SLOT(devfn),
                                        PCI_FUNC(devfn));
        char *as_name = g_strdup_printf("iommu-pci-%02x:%02x.%01x",
                                        pci_bus_num(bus),
                                        PCI_SLOT(devfn),
                                        PCI_FUNC(devfn));
        memory_region_init(&iommu->mr, OBJECT(iommu), mr_name, UINT64_MAX);
        address_space_init(&iommu->as, &iommu->mr, as_name);
        table->iommu[PCI_SLOT(devfn)] = iommu;

        g_free(mr_name);
        g_free(as_name);
    }

    return iommu;
}

F
Frank Blaschka 已提交
457 458 459
static AddressSpace *s390_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
{
    S390pciState *s = opaque;
460
    S390PCIIOMMU *iommu = s390_pci_get_iommu(s, bus, devfn);
F
Frank Blaschka 已提交
461

462
    return &iommu->as;
F
Frank Blaschka 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
}

static uint8_t set_ind_atomic(uint64_t ind_loc, uint8_t to_be_set)
{
    uint8_t ind_old, ind_new;
    hwaddr len = 1;
    uint8_t *ind_addr;

    ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
    if (!ind_addr) {
        s390_pci_generate_error_event(ERR_EVENT_AIRERR, 0, 0, 0, 0);
        return -1;
    }
    do {
        ind_old = *ind_addr;
        ind_new = ind_old | to_be_set;
    } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
    cpu_physical_memory_unmap(ind_addr, len, 1, len);

    return ind_old;
}

static void s390_msi_ctrl_write(void *opaque, hwaddr addr, uint64_t data,
                                unsigned int size)
{
488
    S390PCIBusDevice *pbdev = opaque;
489
    uint32_t idx = data >> ZPCI_MSI_VEC_BITS;
F
Frank Blaschka 已提交
490 491 492 493 494
    uint32_t vec = data & ZPCI_MSI_VEC_MASK;
    uint64_t ind_bit;
    uint32_t sum_bit;
    uint32_t e = 0;

495
    DPRINTF("write_msix data 0x%" PRIx64 " idx %d vec 0x%x\n", data, idx, vec);
F
Frank Blaschka 已提交
496 497 498

    if (!pbdev) {
        e |= (vec << ERR_EVENT_MVN_OFFSET);
499
        s390_pci_generate_error_event(ERR_EVENT_NOMSI, idx, 0, addr, e);
F
Frank Blaschka 已提交
500 501 502
        return;
    }

503
    if (pbdev->state != ZPCI_FS_ENABLED) {
504 505 506
        return;
    }

F
Frank Blaschka 已提交
507 508 509 510 511 512 513
    ind_bit = pbdev->routes.adapter.ind_offset;
    sum_bit = pbdev->routes.adapter.summary_offset;

    set_ind_atomic(pbdev->routes.adapter.ind_addr + (ind_bit + vec) / 8,
                   0x80 >> ((ind_bit + vec) % 8));
    if (!set_ind_atomic(pbdev->routes.adapter.summary_addr + sum_bit / 8,
                                       0x80 >> (sum_bit % 8))) {
514
        css_adapter_interrupt(pbdev->isc);
F
Frank Blaschka 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528
    }
}

static uint64_t s390_msi_ctrl_read(void *opaque, hwaddr addr, unsigned size)
{
    return 0xffffffff;
}

static const MemoryRegionOps s390_msi_ctrl_ops = {
    .write = s390_msi_ctrl_write,
    .read = s390_msi_ctrl_read,
    .endianness = DEVICE_LITTLE_ENDIAN,
};

529
void s390_pci_iommu_enable(S390PCIIOMMU *iommu)
Y
Yi Min Zhao 已提交
530
{
531 532 533 534 535 536
    char *name = g_strdup_printf("iommu-s390-%04x", iommu->pbdev->uid);
    memory_region_init_iommu(&iommu->iommu_mr, OBJECT(&iommu->mr),
                             &s390_iommu_ops, name, iommu->pal + 1);
    iommu->enabled = true;
    memory_region_add_subregion(&iommu->mr, 0, &iommu->iommu_mr);
    g_free(name);
537
}
Y
Yi Min Zhao 已提交
538

539
void s390_pci_iommu_disable(S390PCIIOMMU *iommu)
540
{
541 542 543
    iommu->enabled = false;
    memory_region_del_subregion(&iommu->mr, &iommu->iommu_mr);
    object_unparent(OBJECT(&iommu->iommu_mr));
Y
Yi Min Zhao 已提交
544 545
}

546
static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)
F
Frank Blaschka 已提交
547
{
548 549 550
    uint64_t key = (uintptr_t)bus;
    S390PCIIOMMUTable *table = g_hash_table_lookup(s->iommu_table, &key);
    S390PCIIOMMU *iommu = table ? table->iommu[PCI_SLOT(devfn)] : NULL;
F
Frank Blaschka 已提交
551

552 553
    if (!table || !iommu) {
        return;
F
Frank Blaschka 已提交
554
    }
555 556 557 558 559 560

    table->iommu[PCI_SLOT(devfn)] = NULL;
    address_space_destroy(&iommu->as);
    object_unparent(OBJECT(&iommu->mr));
    object_unparent(OBJECT(iommu));
    object_unref(OBJECT(iommu));
F
Frank Blaschka 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
}

static int s390_pcihost_init(SysBusDevice *dev)
{
    PCIBus *b;
    BusState *bus;
    PCIHostState *phb = PCI_HOST_BRIDGE(dev);
    S390pciState *s = S390_PCI_HOST_BRIDGE(dev);

    DPRINTF("host_init\n");

    b = pci_register_bus(DEVICE(dev), NULL,
                         s390_pci_set_irq, s390_pci_map_irq, NULL,
                         get_system_memory(), get_system_io(), 0, 64,
                         TYPE_PCI_BUS);
    pci_setup_iommu(b, s390_pci_dma_iommu, s);

    bus = BUS(b);
    qbus_set_hotplug_handler(bus, DEVICE(dev), NULL);
    phb->bus = b;
Y
Yi Min Zhao 已提交
581 582

    s->bus = S390_PCI_BUS(qbus_create(TYPE_S390_PCI_BUS, DEVICE(s), NULL));
583
    qbus_set_hotplug_handler(BUS(s->bus), DEVICE(s), NULL);
Y
Yi Min Zhao 已提交
584

585 586
    s->iommu_table = g_hash_table_new_full(g_int64_hash, g_int64_equal,
                                           NULL, g_free);
F
Frank Blaschka 已提交
587
    QTAILQ_INIT(&s->pending_sei);
588
    QTAILQ_INIT(&s->zpci_devs);
F
Frank Blaschka 已提交
589 590 591
    return 0;
}

592
static int s390_pci_setup_msix(S390PCIBusDevice *pbdev)
F
Frank Blaschka 已提交
593 594 595 596 597 598 599 600 601 602 603
{
    uint8_t pos;
    uint16_t ctrl;
    uint32_t table, pba;

    pos = pci_find_capability(pbdev->pdev, PCI_CAP_ID_MSIX);
    if (!pos) {
        pbdev->msix.available = false;
        return 0;
    }

604
    ctrl = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_FLAGS,
F
Frank Blaschka 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
             pci_config_size(pbdev->pdev), sizeof(ctrl));
    table = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_TABLE,
             pci_config_size(pbdev->pdev), sizeof(table));
    pba = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_PBA,
             pci_config_size(pbdev->pdev), sizeof(pba));

    pbdev->msix.table_bar = table & PCI_MSIX_FLAGS_BIRMASK;
    pbdev->msix.table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
    pbdev->msix.pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK;
    pbdev->msix.pba_offset = pba & ~PCI_MSIX_FLAGS_BIRMASK;
    pbdev->msix.entries = (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
    pbdev->msix.available = true;
    return 0;
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
static void s390_pci_msix_init(S390PCIBusDevice *pbdev)
{
    char *name;

    name = g_strdup_printf("msix-s390-%04x", pbdev->uid);

    memory_region_init_io(&pbdev->msix_notify_mr, OBJECT(pbdev),
                          &s390_msi_ctrl_ops, pbdev, name, PAGE_SIZE);
    memory_region_add_subregion(&pbdev->iommu->mr, ZPCI_MSI_ADDR,
                                &pbdev->msix_notify_mr);

    g_free(name);
}

static void s390_pci_msix_free(S390PCIBusDevice *pbdev)
{
    memory_region_del_subregion(&pbdev->iommu->mr, &pbdev->msix_notify_mr);
    object_unparent(OBJECT(&pbdev->msix_notify_mr));
}

640 641
static S390PCIBusDevice *s390_pci_device_new(S390pciState *s,
                                             const char *target)
642 643 644 645 646 647 648 649 650 651 652 653 654 655
{
    DeviceState *dev = NULL;

    dev = qdev_try_create(BUS(s->bus), TYPE_S390_PCI_DEVICE);
    if (!dev) {
        return NULL;
    }

    qdev_prop_set_string(dev, "target", target);
    qdev_init_nofail(dev);

    return S390_PCI_DEVICE(dev);
}

656
static bool s390_pci_alloc_idx(S390pciState *s, S390PCIBusDevice *pbdev)
657 658 659 660
{
    uint32_t idx;

    idx = s->next_idx;
661
    while (s390_pci_find_dev_by_idx(s, idx)) {
662 663 664 665 666 667 668 669 670 671 672 673
        idx = (idx + 1) & FH_MASK_INDEX;
        if (idx == s->next_idx) {
            return false;
        }
    }

    pbdev->idx = idx;
    s->next_idx = (idx + 1) & FH_MASK_INDEX;

    return true;
}

F
Frank Blaschka 已提交
674 675 676
static void s390_pcihost_hot_plug(HotplugHandler *hotplug_dev,
                                  DeviceState *dev, Error **errp)
{
677 678 679
    PCIDevice *pdev = NULL;
    S390PCIBusDevice *pbdev = NULL;
    S390pciState *s = s390_get_phb();
680

681 682 683 684 685 686 687 688 689 690
    if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
        BusState *bus;
        PCIBridge *pb = PCI_BRIDGE(dev);

        pci_bridge_map_irq(pb, dev->id, s390_pci_map_irq);
        pci_setup_iommu(&pb->sec_bus, s390_pci_dma_iommu, s);

        bus = BUS(&pb->sec_bus);
        qbus_set_hotplug_handler(bus, DEVICE(s), errp);
    } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
691 692 693 694 695 696 697 698 699 700 701
        pdev = PCI_DEVICE(dev);

        if (!dev->id) {
            /* In the case the PCI device does not define an id */
            /* we generate one based on the PCI address         */
            dev->id = g_strdup_printf("auto_%02x:%02x.%01x",
                                      pci_bus_num(pdev->bus),
                                      PCI_SLOT(pdev->devfn),
                                      PCI_FUNC(pdev->devfn));
        }

702
        pbdev = s390_pci_find_dev_by_target(s, dev->id);
703
        if (!pbdev) {
704
            pbdev = s390_pci_device_new(s, dev->id);
705 706
            if (!pbdev) {
                error_setg(errp, "create zpci device failed");
707
                return;
708
            }
709
        }
F
Frank Blaschka 已提交
710

711 712 713 714 715
        if (object_dynamic_cast(OBJECT(dev), "vfio-pci")) {
            pbdev->fh |= FH_SHM_VFIO;
        } else {
            pbdev->fh |= FH_SHM_EMUL;
        }
F
Frank Blaschka 已提交
716

717
        pbdev->pdev = pdev;
718
        pbdev->iommu = s390_pci_get_iommu(s, pdev->bus, pdev->devfn);
719
        pbdev->iommu->pbdev = pbdev;
720
        pbdev->state = ZPCI_FS_STANDBY;
721 722 723

        s390_pci_msix_init(pbdev);
        s390_pci_setup_msix(pbdev);
F
Frank Blaschka 已提交
724

725 726 727 728 729 730 731
        if (dev->hotplugged) {
            s390_pci_generate_plug_event(HP_EVENT_RESERVED_TO_STANDBY,
                                         pbdev->fh, pbdev->fid);
        }
    } else if (object_dynamic_cast(OBJECT(dev), TYPE_S390_PCI_DEVICE)) {
        pbdev = S390_PCI_DEVICE(dev);

732
        if (!s390_pci_alloc_idx(s, pbdev)) {
733 734 735 736 737
            error_setg(errp, "no slot for plugging zpci device");
            return;
        }
        pbdev->fh = pbdev->idx;
        QTAILQ_INSERT_TAIL(&s->zpci_devs, pbdev, link);
F
Frank Blaschka 已提交
738 739 740
    }
}

741 742 743 744 745 746 747
static void s390_pcihost_timer_cb(void *opaque)
{
    S390PCIBusDevice *pbdev = opaque;

    if (pbdev->summary_ind) {
        pci_dereg_irqs(pbdev);
    }
748 749
    if (pbdev->iommu->enabled) {
        pci_dereg_ioat(pbdev->iommu);
750 751 752 753 754 755 756 757
    }

    pbdev->state = ZPCI_FS_STANDBY;
    s390_pci_generate_plug_event(HP_EVENT_CONFIGURED_TO_STBRES,
                                 pbdev->fh, pbdev->fid);
    qdev_unplug(DEVICE(pbdev), NULL);
}

F
Frank Blaschka 已提交
758 759 760
static void s390_pcihost_hot_unplug(HotplugHandler *hotplug_dev,
                                    DeviceState *dev, Error **errp)
{
761
    PCIDevice *pci_dev = NULL;
762
    PCIBus *bus;
763
    int32_t devfn;
764 765 766
    S390PCIBusDevice *pbdev = NULL;
    S390pciState *s = s390_get_phb();

767 768 769 770
    if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
        error_setg(errp, "PCI bridge hot unplug currently not supported");
        return;
    } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
771 772
        pci_dev = PCI_DEVICE(dev);

773 774
        QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
            if (pbdev->pdev == pci_dev) {
775 776 777
                break;
            }
        }
778
        assert(pbdev != NULL);
779 780 781 782
    } else if (object_dynamic_cast(OBJECT(dev), TYPE_S390_PCI_DEVICE)) {
        pbdev = S390_PCI_DEVICE(dev);
        pci_dev = pbdev->pdev;
    }
F
Frank Blaschka 已提交
783

784 785 786 787 788 789
    switch (pbdev->state) {
    case ZPCI_FS_RESERVED:
        goto out;
    case ZPCI_FS_STANDBY:
        break;
    default:
790
        s390_pci_generate_plug_event(HP_EVENT_DECONFIGURE_REQUEST,
F
Frank Blaschka 已提交
791
                                     pbdev->fh, pbdev->fid);
792 793 794 795 796 797 798 799 800 801 802 803
        pbdev->release_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                            s390_pcihost_timer_cb,
                                            pbdev);
        timer_mod(pbdev->release_timer,
                  qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + HOT_UNPLUG_TIMEOUT);
        return;
    }

    if (pbdev->release_timer && timer_pending(pbdev->release_timer)) {
        timer_del(pbdev->release_timer);
        timer_free(pbdev->release_timer);
        pbdev->release_timer = NULL;
F
Frank Blaschka 已提交
804 805 806 807
    }

    s390_pci_generate_plug_event(HP_EVENT_STANDBY_TO_RESERVED,
                                 pbdev->fh, pbdev->fid);
808 809
    bus = pci_dev->bus;
    devfn = pci_dev->devfn;
810
    object_unparent(OBJECT(pci_dev));
811
    s390_pci_msix_free(pbdev);
812
    s390_pci_iommu_free(s, bus, devfn);
F
Frank Blaschka 已提交
813
    pbdev->pdev = NULL;
814 815
    pbdev->state = ZPCI_FS_RESERVED;
out:
816
    pbdev->fid = 0;
817
    QTAILQ_REMOVE(&s->zpci_devs, pbdev, link);
818
    object_unparent(OBJECT(pbdev));
F
Frank Blaschka 已提交
819 820 821 822 823 824 825 826 827 828 829 830
}

static void s390_pcihost_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
    DeviceClass *dc = DEVICE_CLASS(klass);
    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);

    dc->cannot_instantiate_with_device_add_yet = true;
    k->init = s390_pcihost_init;
    hc->plug = s390_pcihost_hot_plug;
    hc->unplug = s390_pcihost_hot_unplug;
831
    msi_nonbroken = true;
F
Frank Blaschka 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844
}

static const TypeInfo s390_pcihost_info = {
    .name          = TYPE_S390_PCI_HOST_BRIDGE,
    .parent        = TYPE_PCI_HOST_BRIDGE,
    .instance_size = sizeof(S390pciState),
    .class_init    = s390_pcihost_class_init,
    .interfaces = (InterfaceInfo[]) {
        { TYPE_HOTPLUG_HANDLER },
        { }
    }
};

Y
Yi Min Zhao 已提交
845 846 847 848 849 850
static const TypeInfo s390_pcibus_info = {
    .name = TYPE_S390_PCI_BUS,
    .parent = TYPE_BUS,
    .instance_size = sizeof(S390PCIBus),
};

851
static uint16_t s390_pci_generate_uid(S390pciState *s)
852 853 854 855 856
{
    uint16_t uid = 0;

    do {
        uid++;
857
        if (!s390_pci_find_dev_by_uid(s, uid)) {
858 859 860 861 862 863 864
            return uid;
        }
    } while (uid < ZPCI_MAX_UID);

    return UID_UNDEFINED;
}

865
static uint32_t s390_pci_generate_fid(S390pciState *s, Error **errp)
866 867 868
{
    uint32_t fid = 0;

869
    do {
870
        if (!s390_pci_find_dev_by_fid(s, fid)) {
871 872
            return fid;
        }
873
    } while (fid++ != ZPCI_MAX_FID);
874 875 876 877 878 879 880 881

    error_setg(errp, "no free fid could be found");
    return 0;
}

static void s390_pci_device_realize(DeviceState *dev, Error **errp)
{
    S390PCIBusDevice *zpci = S390_PCI_DEVICE(dev);
882
    S390pciState *s = s390_get_phb();
883 884 885 886 887 888

    if (!zpci->target) {
        error_setg(errp, "target must be defined");
        return;
    }

889
    if (s390_pci_find_dev_by_target(s, zpci->target)) {
890 891 892 893 894 895
        error_setg(errp, "target %s already has an associated zpci device",
                   zpci->target);
        return;
    }

    if (zpci->uid == UID_UNDEFINED) {
896
        zpci->uid = s390_pci_generate_uid(s);
897 898 899 900
        if (!zpci->uid) {
            error_setg(errp, "no free uid could be found");
            return;
        }
901
    } else if (s390_pci_find_dev_by_uid(s, zpci->uid)) {
902 903 904 905 906 907 908
        error_setg(errp, "uid %u already in use", zpci->uid);
        return;
    }

    if (!zpci->fid_defined) {
        Error *local_error = NULL;

909
        zpci->fid = s390_pci_generate_fid(s, &local_error);
910 911 912 913
        if (local_error) {
            error_propagate(errp, local_error);
            return;
        }
914
    } else if (s390_pci_find_dev_by_fid(s, zpci->fid)) {
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
        error_setg(errp, "fid %u already in use", zpci->fid);
        return;
    }

    zpci->state = ZPCI_FS_RESERVED;
}

static void s390_pci_device_reset(DeviceState *dev)
{
    S390PCIBusDevice *pbdev = S390_PCI_DEVICE(dev);

    switch (pbdev->state) {
    case ZPCI_FS_RESERVED:
        return;
    case ZPCI_FS_STANDBY:
        break;
    default:
        pbdev->fh &= ~FH_MASK_ENABLE;
        pbdev->state = ZPCI_FS_DISABLED;
        break;
    }

    if (pbdev->summary_ind) {
        pci_dereg_irqs(pbdev);
    }
940 941
    if (pbdev->iommu->enabled) {
        pci_dereg_ioat(pbdev->iommu);
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    }

    pbdev->fmb_addr = 0;
}

static void s390_pci_get_fid(Object *obj, Visitor *v, const char *name,
                         void *opaque, Error **errp)
{
    Property *prop = opaque;
    uint32_t *ptr = qdev_get_prop_ptr(DEVICE(obj), prop);

    visit_type_uint32(v, name, ptr, errp);
}

static void s390_pci_set_fid(Object *obj, Visitor *v, const char *name,
                         void *opaque, Error **errp)
{
    DeviceState *dev = DEVICE(obj);
    S390PCIBusDevice *zpci = S390_PCI_DEVICE(obj);
    Property *prop = opaque;
    uint32_t *ptr = qdev_get_prop_ptr(dev, prop);

    if (dev->realized) {
        qdev_prop_set_after_realize(dev, name, errp);
        return;
    }

    visit_type_uint32(v, name, ptr, errp);
    zpci->fid_defined = true;
}

static PropertyInfo s390_pci_fid_propinfo = {
    .name = "zpci_fid",
    .get = s390_pci_get_fid,
    .set = s390_pci_set_fid,
};

#define DEFINE_PROP_S390_PCI_FID(_n, _s, _f) \
    DEFINE_PROP(_n, _s, _f, s390_pci_fid_propinfo, uint32_t)

static Property s390_pci_device_properties[] = {
    DEFINE_PROP_UINT16("uid", S390PCIBusDevice, uid, UID_UNDEFINED),
    DEFINE_PROP_S390_PCI_FID("fid", S390PCIBusDevice, fid),
    DEFINE_PROP_STRING("target", S390PCIBusDevice, target),
    DEFINE_PROP_END_OF_LIST(),
};

static void s390_pci_device_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->desc = "zpci device";
    dc->reset = s390_pci_device_reset;
    dc->bus_type = TYPE_S390_PCI_BUS;
    dc->realize = s390_pci_device_realize;
    dc->props = s390_pci_device_properties;
}

static const TypeInfo s390_pci_device_info = {
    .name = TYPE_S390_PCI_DEVICE,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(S390PCIBusDevice),
    .class_init = s390_pci_device_class_init,
};

1007 1008 1009 1010 1011 1012
static TypeInfo s390_pci_iommu_info = {
    .name = TYPE_S390_PCI_IOMMU,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(S390PCIIOMMU),
};

F
Frank Blaschka 已提交
1013 1014 1015
static void s390_pci_register_types(void)
{
    type_register_static(&s390_pcihost_info);
Y
Yi Min Zhao 已提交
1016
    type_register_static(&s390_pcibus_info);
1017
    type_register_static(&s390_pci_device_info);
1018
    type_register_static(&s390_pci_iommu_info);
F
Frank Blaschka 已提交
1019 1020 1021
}

type_init(s390_pci_register_types)