2 * s390 PCI instructions
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>
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
14 #include "qemu/osdep.h"
15 #include "exec/memop.h"
16 #include "exec/memory-internal.h"
17 #include "qemu/error-report.h"
18 #include "sysemu/hw_accel.h"
19 #include "hw/s390x/s390-pci-inst.h"
20 #include "hw/s390x/s390-pci-bus.h"
21 #include "hw/s390x/tod.h"
23 #ifndef DEBUG_S390PCI_INST
24 #define DEBUG_S390PCI_INST 0
27 #define DPRINTF(fmt, ...) \
29 if (DEBUG_S390PCI_INST) { \
30 fprintf(stderr, "s390pci-inst: " fmt, ## __VA_ARGS__); \
34 static inline void inc_dma_avail(S390PCIIOMMU
*iommu
)
36 if (iommu
->dma_limit
) {
37 iommu
->dma_limit
->avail
++;
41 static inline void dec_dma_avail(S390PCIIOMMU
*iommu
)
43 if (iommu
->dma_limit
) {
44 iommu
->dma_limit
->avail
--;
48 static void s390_set_status_code(CPUS390XState
*env
,
49 uint8_t r
, uint64_t status_code
)
51 env
->regs
[r
] &= ~0xff000000ULL
;
52 env
->regs
[r
] |= (status_code
& 0xff) << 24;
55 static int list_pci(ClpReqRspListPci
*rrb
, uint8_t *cc
)
57 S390PCIBusDevice
*pbdev
= NULL
;
58 S390pciState
*s
= s390_get_phb();
59 uint32_t res_code
, initial_l2
, g_l2
;
61 uint64_t resume_token
;
64 if (lduw_p(&rrb
->request
.hdr
.len
) != 32) {
65 res_code
= CLP_RC_LEN
;
70 if ((ldl_p(&rrb
->request
.fmt
) & CLP_MASK_FMT
) != 0) {
71 res_code
= CLP_RC_FMT
;
76 if ((ldl_p(&rrb
->request
.fmt
) & ~CLP_MASK_FMT
) != 0 ||
77 ldq_p(&rrb
->request
.reserved1
) != 0) {
78 res_code
= CLP_RC_RESNOT0
;
83 resume_token
= ldq_p(&rrb
->request
.resume_token
);
86 pbdev
= s390_pci_find_dev_by_idx(s
, resume_token
);
88 res_code
= CLP_RC_LISTPCI_BADRT
;
93 pbdev
= s390_pci_find_next_avail_dev(s
, NULL
);
96 if (lduw_p(&rrb
->response
.hdr
.len
) < 48) {
102 initial_l2
= lduw_p(&rrb
->response
.hdr
.len
);
103 if ((initial_l2
- LIST_PCI_HDR_LEN
) % sizeof(ClpFhListEntry
)
105 res_code
= CLP_RC_LEN
;
111 stl_p(&rrb
->response
.fmt
, 0);
112 stq_p(&rrb
->response
.reserved1
, 0);
113 stl_p(&rrb
->response
.mdd
, FH_MASK_SHM
);
114 stw_p(&rrb
->response
.max_fn
, PCI_MAX_FUNCTIONS
);
115 rrb
->response
.flags
= UID_CHECKING_ENABLED
;
116 rrb
->response
.entry_size
= sizeof(ClpFhListEntry
);
119 g_l2
= LIST_PCI_HDR_LEN
;
120 while (g_l2
< initial_l2
&& pbdev
) {
121 stw_p(&rrb
->response
.fh_list
[i
].device_id
,
122 pci_get_word(pbdev
->pdev
->config
+ PCI_DEVICE_ID
));
123 stw_p(&rrb
->response
.fh_list
[i
].vendor_id
,
124 pci_get_word(pbdev
->pdev
->config
+ PCI_VENDOR_ID
));
125 /* Ignore RESERVED devices. */
126 stl_p(&rrb
->response
.fh_list
[i
].config
,
127 pbdev
->state
== ZPCI_FS_STANDBY
? 0 : 1 << 31);
128 stl_p(&rrb
->response
.fh_list
[i
].fid
, pbdev
->fid
);
129 stl_p(&rrb
->response
.fh_list
[i
].fh
, pbdev
->fh
);
131 g_l2
+= sizeof(ClpFhListEntry
);
132 /* Add endian check for DPRINTF? */
133 DPRINTF("g_l2 %d vendor id 0x%x device id 0x%x fid 0x%x fh 0x%x\n",
135 lduw_p(&rrb
->response
.fh_list
[i
].vendor_id
),
136 lduw_p(&rrb
->response
.fh_list
[i
].device_id
),
137 ldl_p(&rrb
->response
.fh_list
[i
].fid
),
138 ldl_p(&rrb
->response
.fh_list
[i
].fh
));
139 pbdev
= s390_pci_find_next_avail_dev(s
, pbdev
);
146 resume_token
= pbdev
->fh
& FH_MASK_INDEX
;
148 stq_p(&rrb
->response
.resume_token
, resume_token
);
149 stw_p(&rrb
->response
.hdr
.len
, g_l2
);
150 stw_p(&rrb
->response
.hdr
.rsp
, CLP_RC_OK
);
153 DPRINTF("list pci failed rc 0x%x\n", rc
);
154 stw_p(&rrb
->response
.hdr
.rsp
, res_code
);
159 int clp_service_call(S390CPU
*cpu
, uint8_t r2
, uintptr_t ra
)
163 S390PCIBusDevice
*pbdev
;
166 uint8_t buffer
[4096 * 2];
168 CPUS390XState
*env
= &cpu
->env
;
169 S390pciState
*s
= s390_get_phb();
172 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
173 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
177 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
, sizeof(*reqh
))) {
178 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
181 reqh
= (ClpReqHdr
*)buffer
;
182 req_len
= lduw_p(&reqh
->len
);
183 if (req_len
< 16 || req_len
> 8184 || (req_len
% 8 != 0)) {
184 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
188 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
,
189 req_len
+ sizeof(*resh
))) {
190 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
193 resh
= (ClpRspHdr
*)(buffer
+ req_len
);
194 res_len
= lduw_p(&resh
->len
);
195 if (res_len
< 8 || res_len
> 8176 || (res_len
% 8 != 0)) {
196 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
199 if ((req_len
+ res_len
) > 8192) {
200 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
204 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
,
205 req_len
+ res_len
)) {
206 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
211 stw_p(&resh
->rsp
, CLP_RC_LEN
);
215 switch (lduw_p(&reqh
->cmd
)) {
217 ClpReqRspListPci
*rrb
= (ClpReqRspListPci
*)buffer
;
221 case CLP_SET_PCI_FN
: {
222 ClpReqSetPci
*reqsetpci
= (ClpReqSetPci
*)reqh
;
223 ClpRspSetPci
*ressetpci
= (ClpRspSetPci
*)resh
;
225 pbdev
= s390_pci_find_dev_by_fh(s
, ldl_p(&reqsetpci
->fh
));
227 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FH
);
231 switch (reqsetpci
->oc
) {
232 case CLP_SET_ENABLE_PCI_FN
:
233 switch (reqsetpci
->ndas
) {
235 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_DMAAS
);
240 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_RES
);
244 if (pbdev
->fh
& FH_MASK_ENABLE
) {
245 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
249 pbdev
->fh
|= FH_MASK_ENABLE
;
250 pbdev
->state
= ZPCI_FS_ENABLED
;
251 stl_p(&ressetpci
->fh
, pbdev
->fh
);
252 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_OK
);
254 case CLP_SET_DISABLE_PCI_FN
:
255 if (!(pbdev
->fh
& FH_MASK_ENABLE
)) {
256 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
259 device_legacy_reset(DEVICE(pbdev
));
260 pbdev
->fh
&= ~FH_MASK_ENABLE
;
261 pbdev
->state
= ZPCI_FS_DISABLED
;
262 stl_p(&ressetpci
->fh
, pbdev
->fh
);
263 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_OK
);
266 DPRINTF("unknown set pci command\n");
267 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
272 case CLP_QUERY_PCI_FN
: {
273 ClpReqQueryPci
*reqquery
= (ClpReqQueryPci
*)reqh
;
274 ClpRspQueryPci
*resquery
= (ClpRspQueryPci
*)resh
;
276 pbdev
= s390_pci_find_dev_by_fh(s
, ldl_p(&reqquery
->fh
));
278 DPRINTF("query pci no pci dev\n");
279 stw_p(&resquery
->hdr
.rsp
, CLP_RC_SETPCIFN_FH
);
283 stq_p(&resquery
->sdma
, pbdev
->zpci_fn
.sdma
);
284 stq_p(&resquery
->edma
, pbdev
->zpci_fn
.edma
);
285 stw_p(&resquery
->pchid
, pbdev
->zpci_fn
.pchid
);
286 stw_p(&resquery
->vfn
, pbdev
->zpci_fn
.vfn
);
287 resquery
->flags
= pbdev
->zpci_fn
.flags
;
288 resquery
->pfgid
= pbdev
->zpci_fn
.pfgid
;
289 resquery
->pft
= pbdev
->zpci_fn
.pft
;
290 resquery
->fmbl
= pbdev
->zpci_fn
.fmbl
;
291 stl_p(&resquery
->fid
, pbdev
->zpci_fn
.fid
);
292 stl_p(&resquery
->uid
, pbdev
->zpci_fn
.uid
);
293 memcpy(resquery
->pfip
, pbdev
->zpci_fn
.pfip
, CLP_PFIP_NR_SEGMENTS
);
294 memcpy(resquery
->util_str
, pbdev
->zpci_fn
.util_str
, CLP_UTIL_STR_LEN
);
296 for (i
= 0; i
< PCI_BAR_COUNT
; i
++) {
297 uint32_t data
= pci_get_long(pbdev
->pdev
->config
+
298 PCI_BASE_ADDRESS_0
+ (i
* 4));
300 stl_p(&resquery
->bar
[i
], data
);
301 resquery
->bar_size
[i
] = pbdev
->pdev
->io_regions
[i
].size
?
302 ctz64(pbdev
->pdev
->io_regions
[i
].size
) : 0;
303 DPRINTF("bar %d addr 0x%x size 0x%" PRIx64
"barsize 0x%x\n", i
,
304 ldl_p(&resquery
->bar
[i
]),
305 pbdev
->pdev
->io_regions
[i
].size
,
306 resquery
->bar_size
[i
]);
309 stw_p(&resquery
->hdr
.rsp
, CLP_RC_OK
);
312 case CLP_QUERY_PCI_FNGRP
: {
313 ClpRspQueryPciGrp
*resgrp
= (ClpRspQueryPciGrp
*)resh
;
315 ClpReqQueryPciGrp
*reqgrp
= (ClpReqQueryPciGrp
*)reqh
;
318 group
= s390_group_find(reqgrp
->g
);
320 /* We do not allow access to unknown groups */
321 /* The group must have been obtained with a vfio device */
322 stw_p(&resgrp
->hdr
.rsp
, CLP_RC_QUERYPCIFG_PFGID
);
325 resgrp
->fr
= group
->zpci_group
.fr
;
326 stq_p(&resgrp
->dasm
, group
->zpci_group
.dasm
);
327 stq_p(&resgrp
->msia
, group
->zpci_group
.msia
);
328 stw_p(&resgrp
->mui
, group
->zpci_group
.mui
);
329 stw_p(&resgrp
->i
, group
->zpci_group
.i
);
330 stw_p(&resgrp
->maxstbl
, group
->zpci_group
.maxstbl
);
331 resgrp
->version
= group
->zpci_group
.version
;
332 resgrp
->dtsm
= group
->zpci_group
.dtsm
;
333 stw_p(&resgrp
->hdr
.rsp
, CLP_RC_OK
);
337 DPRINTF("unknown clp command\n");
338 stw_p(&resh
->rsp
, CLP_RC_CMD
);
343 if (s390_cpu_virt_mem_write(cpu
, env
->regs
[r2
], r2
, buffer
,
344 req_len
+ res_len
)) {
345 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
353 * Swap data contained in s390x big endian registers to little endian
356 * @ptr: a pointer to a uint64_t data field
357 * @len: the length of the valid data, must be 1,2,4 or 8
359 static int zpci_endian_swap(uint64_t *ptr
, uint8_t len
)
361 uint64_t data
= *ptr
;
367 data
= bswap16(data
);
370 data
= bswap32(data
);
373 data
= bswap64(data
);
382 static MemoryRegion
*s390_get_subregion(MemoryRegion
*mr
, uint64_t offset
,
385 MemoryRegion
*subregion
;
386 uint64_t subregion_size
;
388 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
389 subregion_size
= int128_get64(subregion
->size
);
390 if ((offset
>= subregion
->addr
) &&
391 (offset
+ len
) <= (subregion
->addr
+ subregion_size
)) {
399 static MemTxResult
zpci_read_bar(S390PCIBusDevice
*pbdev
, uint8_t pcias
,
400 uint64_t offset
, uint64_t *data
, uint8_t len
)
404 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
405 mr
= s390_get_subregion(mr
, offset
, len
);
407 return memory_region_dispatch_read(mr
, offset
, data
,
408 size_memop(len
) | MO_BE
,
409 MEMTXATTRS_UNSPECIFIED
);
412 int pcilg_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
, uintptr_t ra
)
414 CPUS390XState
*env
= &cpu
->env
;
415 S390PCIBusDevice
*pbdev
;
423 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
424 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
429 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
433 fh
= env
->regs
[r2
] >> 32;
434 pcias
= (env
->regs
[r2
] >> 16) & 0xf;
435 len
= env
->regs
[r2
] & 0xf;
436 offset
= env
->regs
[r2
+ 1];
438 if (!(fh
& FH_MASK_ENABLE
)) {
439 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
443 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
445 DPRINTF("pcilg no pci dev\n");
446 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
450 switch (pbdev
->state
) {
451 case ZPCI_FS_PERMANENT_ERROR
:
453 setcc(cpu
, ZPCI_PCI_LS_ERR
);
454 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_BLOCKED
);
461 case ZPCI_IO_BAR_MIN
...ZPCI_IO_BAR_MAX
:
462 if (!len
|| (len
> (8 - (offset
& 0x7)))) {
463 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
466 result
= zpci_read_bar(pbdev
, pcias
, offset
, &data
, len
);
467 if (result
!= MEMTX_OK
) {
468 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
472 case ZPCI_CONFIG_BAR
:
473 if (!len
|| (len
> (4 - (offset
& 0x3))) || len
== 3) {
474 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
477 data
= pci_host_config_read_common(
478 pbdev
->pdev
, offset
, pci_config_size(pbdev
->pdev
), len
);
480 if (zpci_endian_swap(&data
, len
)) {
481 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
486 DPRINTF("pcilg invalid space\n");
487 setcc(cpu
, ZPCI_PCI_LS_ERR
);
488 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_INVAL_AS
);
492 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_LD
]++;
494 env
->regs
[r1
] = data
;
495 setcc(cpu
, ZPCI_PCI_LS_OK
);
499 static MemTxResult
zpci_write_bar(S390PCIBusDevice
*pbdev
, uint8_t pcias
,
500 uint64_t offset
, uint64_t data
, uint8_t len
)
504 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
505 mr
= s390_get_subregion(mr
, offset
, len
);
507 return memory_region_dispatch_write(mr
, offset
, data
,
508 size_memop(len
) | MO_BE
,
509 MEMTXATTRS_UNSPECIFIED
);
512 int pcistg_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
, uintptr_t ra
)
514 CPUS390XState
*env
= &cpu
->env
;
515 uint64_t offset
, data
;
516 S390PCIBusDevice
*pbdev
;
522 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
523 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
528 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
532 fh
= env
->regs
[r2
] >> 32;
533 pcias
= (env
->regs
[r2
] >> 16) & 0xf;
534 len
= env
->regs
[r2
] & 0xf;
535 offset
= env
->regs
[r2
+ 1];
536 data
= env
->regs
[r1
];
538 if (!(fh
& FH_MASK_ENABLE
)) {
539 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
543 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
545 DPRINTF("pcistg no pci dev\n");
546 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
550 switch (pbdev
->state
) {
551 /* ZPCI_FS_RESERVED, ZPCI_FS_STANDBY and ZPCI_FS_DISABLED
552 * are already covered by the FH_MASK_ENABLE check above
554 case ZPCI_FS_PERMANENT_ERROR
:
556 setcc(cpu
, ZPCI_PCI_LS_ERR
);
557 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_BLOCKED
);
564 /* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */
565 case ZPCI_IO_BAR_MIN
...ZPCI_IO_BAR_MAX
:
567 * A length of 0 is invalid and length should not cross a double word
569 if (!len
|| (len
> (8 - (offset
& 0x7)))) {
570 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
574 result
= zpci_write_bar(pbdev
, pcias
, offset
, data
, len
);
575 if (result
!= MEMTX_OK
) {
576 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
580 case ZPCI_CONFIG_BAR
:
581 /* ZPCI uses the pseudo BAR number 15 as configuration space */
582 /* possible access lengths are 1,2,4 and must not cross a word */
583 if (!len
|| (len
> (4 - (offset
& 0x3))) || len
== 3) {
584 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
587 /* len = 1,2,4 so we do not need to test */
588 zpci_endian_swap(&data
, len
);
589 pci_host_config_write_common(pbdev
->pdev
, offset
,
590 pci_config_size(pbdev
->pdev
),
594 DPRINTF("pcistg invalid space\n");
595 setcc(cpu
, ZPCI_PCI_LS_ERR
);
596 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_INVAL_AS
);
600 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_ST
]++;
602 setcc(cpu
, ZPCI_PCI_LS_OK
);
606 static uint32_t s390_pci_update_iotlb(S390PCIIOMMU
*iommu
,
607 S390IOTLBEntry
*entry
)
609 S390IOTLBEntry
*cache
= g_hash_table_lookup(iommu
->iotlb
, &entry
->iova
);
610 IOMMUTLBEvent event
= {
611 .type
= entry
->perm
? IOMMU_NOTIFIER_MAP
: IOMMU_NOTIFIER_UNMAP
,
613 .target_as
= &address_space_memory
,
615 .translated_addr
= entry
->translated_addr
,
617 .addr_mask
= ~TARGET_PAGE_MASK
,
621 if (event
.type
== IOMMU_NOTIFIER_UNMAP
) {
625 g_hash_table_remove(iommu
->iotlb
, &entry
->iova
);
626 inc_dma_avail(iommu
);
629 if (cache
->perm
== entry
->perm
&&
630 cache
->translated_addr
== entry
->translated_addr
) {
634 event
.type
= IOMMU_NOTIFIER_UNMAP
;
635 event
.entry
.perm
= IOMMU_NONE
;
636 memory_region_notify_iommu(&iommu
->iommu_mr
, 0, event
);
637 event
.type
= IOMMU_NOTIFIER_MAP
;
638 event
.entry
.perm
= entry
->perm
;
641 cache
= g_new(S390IOTLBEntry
, 1);
642 cache
->iova
= entry
->iova
;
643 cache
->translated_addr
= entry
->translated_addr
;
644 cache
->len
= TARGET_PAGE_SIZE
;
645 cache
->perm
= entry
->perm
;
646 g_hash_table_replace(iommu
->iotlb
, &cache
->iova
, cache
);
647 dec_dma_avail(iommu
);
650 memory_region_notify_iommu(&iommu
->iommu_mr
, 0, event
);
653 return iommu
->dma_limit
? iommu
->dma_limit
->avail
: 1;
656 int rpcit_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
, uintptr_t ra
)
658 CPUS390XState
*env
= &cpu
->env
;
661 S390PCIBusDevice
*pbdev
;
663 S390IOTLBEntry entry
;
667 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
668 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
673 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
677 fh
= env
->regs
[r1
] >> 32;
678 start
= env
->regs
[r2
];
679 end
= start
+ env
->regs
[r2
+ 1];
681 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
683 DPRINTF("rpcit no pci dev\n");
684 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
688 switch (pbdev
->state
) {
689 case ZPCI_FS_RESERVED
:
690 case ZPCI_FS_STANDBY
:
691 case ZPCI_FS_DISABLED
:
692 case ZPCI_FS_PERMANENT_ERROR
:
693 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
696 setcc(cpu
, ZPCI_PCI_LS_ERR
);
697 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_ERROR_RECOVER
);
703 iommu
= pbdev
->iommu
;
704 if (iommu
->dma_limit
) {
705 dma_avail
= iommu
->dma_limit
->avail
;
709 if (!iommu
->g_iota
) {
710 error
= ERR_EVENT_INVALAS
;
714 if (end
< iommu
->pba
|| start
> iommu
->pal
) {
715 error
= ERR_EVENT_OORANGE
;
719 while (start
< end
) {
720 error
= s390_guest_io_table_walk(iommu
->g_iota
, start
, &entry
);
726 while (entry
.iova
< start
&& entry
.iova
< end
&&
727 (dma_avail
> 0 || entry
.perm
== IOMMU_NONE
)) {
728 dma_avail
= s390_pci_update_iotlb(iommu
, &entry
);
729 entry
.iova
+= TARGET_PAGE_SIZE
;
730 entry
.translated_addr
+= TARGET_PAGE_SIZE
;
735 pbdev
->state
= ZPCI_FS_ERROR
;
736 setcc(cpu
, ZPCI_PCI_LS_ERR
);
737 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_FUNC_IN_ERR
);
738 s390_pci_generate_error_event(error
, pbdev
->fh
, pbdev
->fid
, start
, 0);
740 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_RPCIT
]++;
742 setcc(cpu
, ZPCI_PCI_LS_OK
);
744 /* vfio DMA mappings are exhausted, trigger a RPCIT */
745 setcc(cpu
, ZPCI_PCI_LS_ERR
);
746 s390_set_status_code(env
, r1
, ZPCI_RPCIT_ST_INSUFF_RES
);
752 int pcistb_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r3
, uint64_t gaddr
,
753 uint8_t ar
, uintptr_t ra
)
755 CPUS390XState
*env
= &cpu
->env
;
756 S390PCIBusDevice
*pbdev
;
766 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
767 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
771 fh
= env
->regs
[r1
] >> 32;
772 pcias
= (env
->regs
[r1
] >> 16) & 0xf;
773 len
= env
->regs
[r1
] & 0x1fff;
774 offset
= env
->regs
[r3
];
776 if (!(fh
& FH_MASK_ENABLE
)) {
777 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
781 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
783 DPRINTF("pcistb no pci dev fh 0x%x\n", fh
);
784 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
788 switch (pbdev
->state
) {
789 case ZPCI_FS_PERMANENT_ERROR
:
791 setcc(cpu
, ZPCI_PCI_LS_ERR
);
792 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_BLOCKED
);
798 if (pcias
> ZPCI_IO_BAR_MAX
) {
799 DPRINTF("pcistb invalid space\n");
800 setcc(cpu
, ZPCI_PCI_LS_ERR
);
801 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_INVAL_AS
);
805 /* Verify the address, offset and length */
806 /* offset must be a multiple of 8 */
808 goto specification_error
;
810 /* Length must be greater than 8, a multiple of 8 */
811 /* and not greater than maxstbl */
812 if ((len
<= 8) || (len
% 8) ||
813 (len
> pbdev
->pci_group
->zpci_group
.maxstbl
)) {
814 goto specification_error
;
816 /* Do not cross a 4K-byte boundary */
817 if (((offset
& 0xfff) + len
) > 0x1000) {
818 goto specification_error
;
820 /* Guest address must be double word aligned */
821 if (gaddr
& 0x07UL
) {
822 goto specification_error
;
825 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
826 mr
= s390_get_subregion(mr
, offset
, len
);
829 for (i
= 0; i
< len
; i
+= 8) {
830 if (!memory_region_access_valid(mr
, offset
+ i
, 8, true,
831 MEMTXATTRS_UNSPECIFIED
)) {
832 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
837 if (s390_cpu_virt_mem_read(cpu
, gaddr
, ar
, buffer
, len
)) {
838 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
842 for (i
= 0; i
< len
/ 8; i
++) {
843 result
= memory_region_dispatch_write(mr
, offset
+ i
* 8,
844 ldq_p(buffer
+ i
* 8),
845 MO_64
, MEMTXATTRS_UNSPECIFIED
);
846 if (result
!= MEMTX_OK
) {
847 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
852 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_STB
]++;
854 setcc(cpu
, ZPCI_PCI_LS_OK
);
858 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
862 static int reg_irqs(CPUS390XState
*env
, S390PCIBusDevice
*pbdev
, ZpciFib fib
)
865 uint8_t isc
= FIB_DATA_ISC(ldl_p(&fib
.data
));
867 pbdev
->routes
.adapter
.adapter_id
= css_get_adapter_id(
868 CSS_IO_ADAPTER_PCI
, isc
);
869 pbdev
->summary_ind
= get_indicator(ldq_p(&fib
.aisb
), sizeof(uint64_t));
870 len
= BITS_TO_LONGS(FIB_DATA_NOI(ldl_p(&fib
.data
))) * sizeof(unsigned long);
871 pbdev
->indicator
= get_indicator(ldq_p(&fib
.aibv
), len
);
873 ret
= map_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
878 ret
= map_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
883 pbdev
->routes
.adapter
.summary_addr
= ldq_p(&fib
.aisb
);
884 pbdev
->routes
.adapter
.summary_offset
= FIB_DATA_AISBO(ldl_p(&fib
.data
));
885 pbdev
->routes
.adapter
.ind_addr
= ldq_p(&fib
.aibv
);
886 pbdev
->routes
.adapter
.ind_offset
= FIB_DATA_AIBVO(ldl_p(&fib
.data
));
888 pbdev
->noi
= FIB_DATA_NOI(ldl_p(&fib
.data
));
889 pbdev
->sum
= FIB_DATA_SUM(ldl_p(&fib
.data
));
891 DPRINTF("reg_irqs adapter id %d\n", pbdev
->routes
.adapter
.adapter_id
);
894 release_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
895 release_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
896 pbdev
->summary_ind
= NULL
;
897 pbdev
->indicator
= NULL
;
901 int pci_dereg_irqs(S390PCIBusDevice
*pbdev
)
903 release_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
904 release_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
906 pbdev
->summary_ind
= NULL
;
907 pbdev
->indicator
= NULL
;
908 pbdev
->routes
.adapter
.summary_addr
= 0;
909 pbdev
->routes
.adapter
.summary_offset
= 0;
910 pbdev
->routes
.adapter
.ind_addr
= 0;
911 pbdev
->routes
.adapter
.ind_offset
= 0;
916 DPRINTF("dereg_irqs adapter id %d\n", pbdev
->routes
.adapter
.adapter_id
);
920 static int reg_ioat(CPUS390XState
*env
, S390PCIBusDevice
*pbdev
, ZpciFib fib
,
923 S390PCIIOMMU
*iommu
= pbdev
->iommu
;
924 uint64_t pba
= ldq_p(&fib
.pba
);
925 uint64_t pal
= ldq_p(&fib
.pal
);
926 uint64_t g_iota
= ldq_p(&fib
.iota
);
927 uint8_t dt
= (g_iota
>> 2) & 0x7;
928 uint8_t t
= (g_iota
>> 11) & 0x1;
932 if (pba
> pal
|| pba
< pbdev
->zpci_fn
.sdma
|| pal
> pbdev
->zpci_fn
.edma
) {
933 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
937 /* currently we only support designation type 1 with translation */
938 if (!(dt
== ZPCI_IOTA_RTTO
&& t
)) {
939 error_report("unsupported ioat dt %d t %d", dt
, t
);
940 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
946 iommu
->g_iota
= g_iota
;
948 s390_pci_iommu_enable(iommu
);
953 void pci_dereg_ioat(S390PCIIOMMU
*iommu
)
955 s390_pci_iommu_disable(iommu
);
961 void fmb_timer_free(S390PCIBusDevice
*pbdev
)
963 if (pbdev
->fmb_timer
) {
964 timer_free(pbdev
->fmb_timer
);
965 pbdev
->fmb_timer
= NULL
;
968 memset(&pbdev
->fmb
, 0, sizeof(ZpciFmb
));
971 static int fmb_do_update(S390PCIBusDevice
*pbdev
, int offset
, uint64_t val
,
975 uint64_t dst
= pbdev
->fmb_addr
+ offset
;
979 address_space_stq_be(&address_space_memory
, dst
, val
,
980 MEMTXATTRS_UNSPECIFIED
,
984 address_space_stl_be(&address_space_memory
, dst
, val
,
985 MEMTXATTRS_UNSPECIFIED
,
989 address_space_stw_be(&address_space_memory
, dst
, val
,
990 MEMTXATTRS_UNSPECIFIED
,
994 address_space_stb(&address_space_memory
, dst
, val
,
995 MEMTXATTRS_UNSPECIFIED
,
1002 if (ret
!= MEMTX_OK
) {
1003 s390_pci_generate_error_event(ERR_EVENT_FMBA
, pbdev
->fh
, pbdev
->fid
,
1004 pbdev
->fmb_addr
, 0);
1005 fmb_timer_free(pbdev
);
1011 static void fmb_update(void *opaque
)
1013 S390PCIBusDevice
*pbdev
= opaque
;
1014 int64_t t
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1018 pbdev
->fmb
.last_update
*= 2;
1019 pbdev
->fmb
.last_update
|= UPDATE_U_BIT
;
1020 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, last_update
),
1021 pbdev
->fmb
.last_update
,
1022 sizeof(pbdev
->fmb
.last_update
))) {
1026 /* Update FMB sample count */
1027 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, sample
),
1028 pbdev
->fmb
.sample
++,
1029 sizeof(pbdev
->fmb
.sample
))) {
1033 /* Update FMB counters */
1034 for (i
= 0; i
< ZPCI_FMB_CNT_MAX
; i
++) {
1035 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, counter
[i
]),
1036 pbdev
->fmb
.counter
[i
],
1037 sizeof(pbdev
->fmb
.counter
[0]))) {
1042 /* Clear U bit and update the time */
1043 pbdev
->fmb
.last_update
= time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
1044 pbdev
->fmb
.last_update
*= 2;
1045 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, last_update
),
1046 pbdev
->fmb
.last_update
,
1047 sizeof(pbdev
->fmb
.last_update
))) {
1050 timer_mod(pbdev
->fmb_timer
, t
+ pbdev
->pci_group
->zpci_group
.mui
);
1053 int mpcifc_service_call(S390CPU
*cpu
, uint8_t r1
, uint64_t fiba
, uint8_t ar
,
1056 CPUS390XState
*env
= &cpu
->env
;
1060 S390PCIBusDevice
*pbdev
;
1061 uint64_t cc
= ZPCI_PCI_LS_OK
;
1063 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
1064 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
1068 oc
= env
->regs
[r1
] & 0xff;
1069 dmaas
= (env
->regs
[r1
] >> 16) & 0xff;
1070 fh
= env
->regs
[r1
] >> 32;
1073 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
1077 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
1079 DPRINTF("mpcifc no pci dev fh 0x%x\n", fh
);
1080 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1084 switch (pbdev
->state
) {
1085 case ZPCI_FS_RESERVED
:
1086 case ZPCI_FS_STANDBY
:
1087 case ZPCI_FS_DISABLED
:
1088 case ZPCI_FS_PERMANENT_ERROR
:
1089 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1095 if (s390_cpu_virt_mem_read(cpu
, fiba
, ar
, (uint8_t *)&fib
, sizeof(fib
))) {
1096 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
1101 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
1106 case ZPCI_MOD_FC_REG_INT
:
1107 if (pbdev
->summary_ind
) {
1108 cc
= ZPCI_PCI_LS_ERR
;
1109 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1110 } else if (reg_irqs(env
, pbdev
, fib
)) {
1111 cc
= ZPCI_PCI_LS_ERR
;
1112 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_RES_NOT_AVAIL
);
1115 case ZPCI_MOD_FC_DEREG_INT
:
1116 if (!pbdev
->summary_ind
) {
1117 cc
= ZPCI_PCI_LS_ERR
;
1118 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1120 pci_dereg_irqs(pbdev
);
1123 case ZPCI_MOD_FC_REG_IOAT
:
1125 cc
= ZPCI_PCI_LS_ERR
;
1126 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
1127 } else if (pbdev
->iommu
->enabled
) {
1128 cc
= ZPCI_PCI_LS_ERR
;
1129 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1130 } else if (reg_ioat(env
, pbdev
, fib
, ra
)) {
1131 cc
= ZPCI_PCI_LS_ERR
;
1132 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_INSUF_RES
);
1135 case ZPCI_MOD_FC_DEREG_IOAT
:
1137 cc
= ZPCI_PCI_LS_ERR
;
1138 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
1139 } else if (!pbdev
->iommu
->enabled
) {
1140 cc
= ZPCI_PCI_LS_ERR
;
1141 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1143 pci_dereg_ioat(pbdev
->iommu
);
1146 case ZPCI_MOD_FC_REREG_IOAT
:
1148 cc
= ZPCI_PCI_LS_ERR
;
1149 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
1150 } else if (!pbdev
->iommu
->enabled
) {
1151 cc
= ZPCI_PCI_LS_ERR
;
1152 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1154 pci_dereg_ioat(pbdev
->iommu
);
1155 if (reg_ioat(env
, pbdev
, fib
, ra
)) {
1156 cc
= ZPCI_PCI_LS_ERR
;
1157 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_INSUF_RES
);
1161 case ZPCI_MOD_FC_RESET_ERROR
:
1162 switch (pbdev
->state
) {
1163 case ZPCI_FS_BLOCKED
:
1165 pbdev
->state
= ZPCI_FS_ENABLED
;
1168 cc
= ZPCI_PCI_LS_ERR
;
1169 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1172 case ZPCI_MOD_FC_RESET_BLOCK
:
1173 switch (pbdev
->state
) {
1175 pbdev
->state
= ZPCI_FS_BLOCKED
;
1178 cc
= ZPCI_PCI_LS_ERR
;
1179 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1182 case ZPCI_MOD_FC_SET_MEASURE
: {
1183 uint64_t fmb_addr
= ldq_p(&fib
.fmb_addr
);
1185 if (fmb_addr
& FMBK_MASK
) {
1186 cc
= ZPCI_PCI_LS_ERR
;
1187 s390_pci_generate_error_event(ERR_EVENT_FMBPRO
, pbdev
->fh
,
1188 pbdev
->fid
, fmb_addr
, 0);
1189 fmb_timer_free(pbdev
);
1194 /* Stop updating FMB. */
1195 fmb_timer_free(pbdev
);
1199 if (!pbdev
->fmb_timer
) {
1200 pbdev
->fmb_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
1202 } else if (timer_pending(pbdev
->fmb_timer
)) {
1203 /* Remove pending timer to update FMB address. */
1204 timer_del(pbdev
->fmb_timer
);
1206 pbdev
->fmb_addr
= fmb_addr
;
1207 timer_mod(pbdev
->fmb_timer
,
1208 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) +
1209 pbdev
->pci_group
->zpci_group
.mui
);
1213 s390_program_interrupt(&cpu
->env
, PGM_OPERAND
, ra
);
1214 cc
= ZPCI_PCI_LS_ERR
;
1221 int stpcifc_service_call(S390CPU
*cpu
, uint8_t r1
, uint64_t fiba
, uint8_t ar
,
1224 CPUS390XState
*env
= &cpu
->env
;
1228 S390PCIBusDevice
*pbdev
;
1230 uint64_t cc
= ZPCI_PCI_LS_OK
;
1232 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
1233 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
1237 fh
= env
->regs
[r1
] >> 32;
1238 dmaas
= (env
->regs
[r1
] >> 16) & 0xff;
1241 setcc(cpu
, ZPCI_PCI_LS_ERR
);
1242 s390_set_status_code(env
, r1
, ZPCI_STPCIFC_ST_INVAL_DMAAS
);
1247 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
1251 pbdev
= s390_pci_find_dev_by_idx(s390_get_phb(), fh
& FH_MASK_INDEX
);
1253 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1257 memset(&fib
, 0, sizeof(fib
));
1259 switch (pbdev
->state
) {
1260 case ZPCI_FS_RESERVED
:
1261 case ZPCI_FS_STANDBY
:
1262 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1264 case ZPCI_FS_DISABLED
:
1265 if (fh
& FH_MASK_ENABLE
) {
1266 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1270 /* BLOCKED bit is set to one coincident with the setting of ERROR bit.
1271 * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */
1275 case ZPCI_FS_BLOCKED
:
1278 case ZPCI_FS_ENABLED
:
1280 if (pbdev
->iommu
->enabled
) {
1283 if (!(fh
& FH_MASK_ENABLE
)) {
1284 env
->regs
[r1
] |= 1ULL << 63;
1287 case ZPCI_FS_PERMANENT_ERROR
:
1288 setcc(cpu
, ZPCI_PCI_LS_ERR
);
1289 s390_set_status_code(env
, r1
, ZPCI_STPCIFC_ST_PERM_ERROR
);
1293 stq_p(&fib
.pba
, pbdev
->iommu
->pba
);
1294 stq_p(&fib
.pal
, pbdev
->iommu
->pal
);
1295 stq_p(&fib
.iota
, pbdev
->iommu
->g_iota
);
1296 stq_p(&fib
.aibv
, pbdev
->routes
.adapter
.ind_addr
);
1297 stq_p(&fib
.aisb
, pbdev
->routes
.adapter
.summary_addr
);
1298 stq_p(&fib
.fmb_addr
, pbdev
->fmb_addr
);
1300 data
= ((uint32_t)pbdev
->isc
<< 28) | ((uint32_t)pbdev
->noi
<< 16) |
1301 ((uint32_t)pbdev
->routes
.adapter
.ind_offset
<< 8) |
1302 ((uint32_t)pbdev
->sum
<< 7) | pbdev
->routes
.adapter
.summary_offset
;
1303 stl_p(&fib
.data
, data
);
1306 if (s390_cpu_virt_mem_write(cpu
, fiba
, ar
, (uint8_t *)&fib
, sizeof(fib
))) {
1307 s390_cpu_virt_mem_handle_exc(cpu
, ra
);