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"
16 #include "exec/memop.h"
17 #include "exec/memory-internal.h"
18 #include "qemu/error-report.h"
19 #include "sysemu/hw_accel.h"
20 #include "hw/s390x/s390-pci-inst.h"
21 #include "hw/s390x/s390-pci-bus.h"
22 #include "hw/s390x/tod.h"
24 #ifndef DEBUG_S390PCI_INST
25 #define DEBUG_S390PCI_INST 0
28 #define DPRINTF(fmt, ...) \
30 if (DEBUG_S390PCI_INST) { \
31 fprintf(stderr, "s390pci-inst: " fmt, ## __VA_ARGS__); \
35 static inline void inc_dma_avail(S390PCIIOMMU
*iommu
)
37 if (iommu
->dma_limit
) {
38 iommu
->dma_limit
->avail
++;
42 static inline void dec_dma_avail(S390PCIIOMMU
*iommu
)
44 if (iommu
->dma_limit
) {
45 iommu
->dma_limit
->avail
--;
49 static void s390_set_status_code(CPUS390XState
*env
,
50 uint8_t r
, uint64_t status_code
)
52 env
->regs
[r
] &= ~0xff000000ULL
;
53 env
->regs
[r
] |= (status_code
& 0xff) << 24;
56 static int list_pci(ClpReqRspListPci
*rrb
, uint8_t *cc
)
58 S390PCIBusDevice
*pbdev
= NULL
;
59 S390pciState
*s
= s390_get_phb();
60 uint32_t res_code
, initial_l2
, g_l2
;
62 uint64_t resume_token
;
65 if (lduw_p(&rrb
->request
.hdr
.len
) != 32) {
66 res_code
= CLP_RC_LEN
;
71 if ((ldl_p(&rrb
->request
.fmt
) & CLP_MASK_FMT
) != 0) {
72 res_code
= CLP_RC_FMT
;
77 if ((ldl_p(&rrb
->request
.fmt
) & ~CLP_MASK_FMT
) != 0 ||
78 ldq_p(&rrb
->request
.reserved1
) != 0) {
79 res_code
= CLP_RC_RESNOT0
;
84 resume_token
= ldq_p(&rrb
->request
.resume_token
);
87 pbdev
= s390_pci_find_dev_by_idx(s
, resume_token
);
89 res_code
= CLP_RC_LISTPCI_BADRT
;
94 pbdev
= s390_pci_find_next_avail_dev(s
, NULL
);
97 if (lduw_p(&rrb
->response
.hdr
.len
) < 48) {
103 initial_l2
= lduw_p(&rrb
->response
.hdr
.len
);
104 if ((initial_l2
- LIST_PCI_HDR_LEN
) % sizeof(ClpFhListEntry
)
106 res_code
= CLP_RC_LEN
;
112 stl_p(&rrb
->response
.fmt
, 0);
113 stq_p(&rrb
->response
.reserved1
, 0);
114 stl_p(&rrb
->response
.mdd
, FH_MASK_SHM
);
115 stw_p(&rrb
->response
.max_fn
, PCI_MAX_FUNCTIONS
);
116 rrb
->response
.flags
= UID_CHECKING_ENABLED
;
117 rrb
->response
.entry_size
= sizeof(ClpFhListEntry
);
120 g_l2
= LIST_PCI_HDR_LEN
;
121 while (g_l2
< initial_l2
&& pbdev
) {
122 stw_p(&rrb
->response
.fh_list
[i
].device_id
,
123 pci_get_word(pbdev
->pdev
->config
+ PCI_DEVICE_ID
));
124 stw_p(&rrb
->response
.fh_list
[i
].vendor_id
,
125 pci_get_word(pbdev
->pdev
->config
+ PCI_VENDOR_ID
));
126 /* Ignore RESERVED devices. */
127 stl_p(&rrb
->response
.fh_list
[i
].config
,
128 pbdev
->state
== ZPCI_FS_STANDBY
? 0 : 1 << 31);
129 stl_p(&rrb
->response
.fh_list
[i
].fid
, pbdev
->fid
);
130 stl_p(&rrb
->response
.fh_list
[i
].fh
, pbdev
->fh
);
132 g_l2
+= sizeof(ClpFhListEntry
);
133 /* Add endian check for DPRINTF? */
134 DPRINTF("g_l2 %d vendor id 0x%x device id 0x%x fid 0x%x fh 0x%x\n",
136 lduw_p(&rrb
->response
.fh_list
[i
].vendor_id
),
137 lduw_p(&rrb
->response
.fh_list
[i
].device_id
),
138 ldl_p(&rrb
->response
.fh_list
[i
].fid
),
139 ldl_p(&rrb
->response
.fh_list
[i
].fh
));
140 pbdev
= s390_pci_find_next_avail_dev(s
, pbdev
);
147 resume_token
= pbdev
->fh
& FH_MASK_INDEX
;
149 stq_p(&rrb
->response
.resume_token
, resume_token
);
150 stw_p(&rrb
->response
.hdr
.len
, g_l2
);
151 stw_p(&rrb
->response
.hdr
.rsp
, CLP_RC_OK
);
154 DPRINTF("list pci failed rc 0x%x\n", rc
);
155 stw_p(&rrb
->response
.hdr
.rsp
, res_code
);
160 int clp_service_call(S390CPU
*cpu
, uint8_t r2
, uintptr_t ra
)
164 S390PCIBusDevice
*pbdev
;
167 uint8_t buffer
[4096 * 2];
169 CPUS390XState
*env
= &cpu
->env
;
170 S390pciState
*s
= s390_get_phb();
173 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
174 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
178 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
, sizeof(*reqh
))) {
179 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
182 reqh
= (ClpReqHdr
*)buffer
;
183 req_len
= lduw_p(&reqh
->len
);
184 if (req_len
< 16 || req_len
> 8184 || (req_len
% 8 != 0)) {
185 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
189 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
,
190 req_len
+ sizeof(*resh
))) {
191 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
194 resh
= (ClpRspHdr
*)(buffer
+ req_len
);
195 res_len
= lduw_p(&resh
->len
);
196 if (res_len
< 8 || res_len
> 8176 || (res_len
% 8 != 0)) {
197 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
200 if ((req_len
+ res_len
) > 8192) {
201 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
205 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
,
206 req_len
+ res_len
)) {
207 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
212 stw_p(&resh
->rsp
, CLP_RC_LEN
);
216 switch (lduw_p(&reqh
->cmd
)) {
218 ClpReqRspListPci
*rrb
= (ClpReqRspListPci
*)buffer
;
222 case CLP_SET_PCI_FN
: {
223 ClpReqSetPci
*reqsetpci
= (ClpReqSetPci
*)reqh
;
224 ClpRspSetPci
*ressetpci
= (ClpRspSetPci
*)resh
;
226 pbdev
= s390_pci_find_dev_by_fh(s
, ldl_p(&reqsetpci
->fh
));
228 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FH
);
232 switch (reqsetpci
->oc
) {
233 case CLP_SET_ENABLE_PCI_FN
:
234 switch (reqsetpci
->ndas
) {
236 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_DMAAS
);
241 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_RES
);
245 if (pbdev
->fh
& FH_MASK_ENABLE
) {
246 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
250 pbdev
->fh
|= FH_MASK_ENABLE
;
251 pbdev
->state
= ZPCI_FS_ENABLED
;
252 stl_p(&ressetpci
->fh
, pbdev
->fh
);
253 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_OK
);
255 case CLP_SET_DISABLE_PCI_FN
:
256 if (!(pbdev
->fh
& FH_MASK_ENABLE
)) {
257 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
260 device_legacy_reset(DEVICE(pbdev
));
261 pbdev
->fh
&= ~FH_MASK_ENABLE
;
262 pbdev
->state
= ZPCI_FS_DISABLED
;
263 stl_p(&ressetpci
->fh
, pbdev
->fh
);
264 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_OK
);
267 DPRINTF("unknown set pci command\n");
268 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
273 case CLP_QUERY_PCI_FN
: {
274 ClpReqQueryPci
*reqquery
= (ClpReqQueryPci
*)reqh
;
275 ClpRspQueryPci
*resquery
= (ClpRspQueryPci
*)resh
;
277 pbdev
= s390_pci_find_dev_by_fh(s
, ldl_p(&reqquery
->fh
));
279 DPRINTF("query pci no pci dev\n");
280 stw_p(&resquery
->hdr
.rsp
, CLP_RC_SETPCIFN_FH
);
284 memcpy(resquery
, &pbdev
->zpci_fn
, sizeof(*resquery
));
286 for (i
= 0; i
< PCI_BAR_COUNT
; i
++) {
287 uint32_t data
= pci_get_long(pbdev
->pdev
->config
+
288 PCI_BASE_ADDRESS_0
+ (i
* 4));
290 stl_p(&resquery
->bar
[i
], data
);
291 resquery
->bar_size
[i
] = pbdev
->pdev
->io_regions
[i
].size
?
292 ctz64(pbdev
->pdev
->io_regions
[i
].size
) : 0;
293 DPRINTF("bar %d addr 0x%x size 0x%" PRIx64
"barsize 0x%x\n", i
,
294 ldl_p(&resquery
->bar
[i
]),
295 pbdev
->pdev
->io_regions
[i
].size
,
296 resquery
->bar_size
[i
]);
299 stw_p(&resquery
->hdr
.rsp
, CLP_RC_OK
);
302 case CLP_QUERY_PCI_FNGRP
: {
303 ClpRspQueryPciGrp
*resgrp
= (ClpRspQueryPciGrp
*)resh
;
305 ClpReqQueryPciGrp
*reqgrp
= (ClpReqQueryPciGrp
*)reqh
;
308 group
= s390_group_find(reqgrp
->g
);
310 /* We do not allow access to unknown groups */
311 /* The group must have been obtained with a vfio device */
312 stw_p(&resgrp
->hdr
.rsp
, CLP_RC_QUERYPCIFG_PFGID
);
315 memcpy(resgrp
, &group
->zpci_group
, sizeof(ClpRspQueryPciGrp
));
316 stw_p(&resgrp
->hdr
.rsp
, CLP_RC_OK
);
320 DPRINTF("unknown clp command\n");
321 stw_p(&resh
->rsp
, CLP_RC_CMD
);
326 if (s390_cpu_virt_mem_write(cpu
, env
->regs
[r2
], r2
, buffer
,
327 req_len
+ res_len
)) {
328 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
336 * Swap data contained in s390x big endian registers to little endian
339 * @ptr: a pointer to a uint64_t data field
340 * @len: the length of the valid data, must be 1,2,4 or 8
342 static int zpci_endian_swap(uint64_t *ptr
, uint8_t len
)
344 uint64_t data
= *ptr
;
350 data
= bswap16(data
);
353 data
= bswap32(data
);
356 data
= bswap64(data
);
365 static MemoryRegion
*s390_get_subregion(MemoryRegion
*mr
, uint64_t offset
,
368 MemoryRegion
*subregion
;
369 uint64_t subregion_size
;
371 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
372 subregion_size
= int128_get64(subregion
->size
);
373 if ((offset
>= subregion
->addr
) &&
374 (offset
+ len
) <= (subregion
->addr
+ subregion_size
)) {
382 static MemTxResult
zpci_read_bar(S390PCIBusDevice
*pbdev
, uint8_t pcias
,
383 uint64_t offset
, uint64_t *data
, uint8_t len
)
387 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
388 mr
= s390_get_subregion(mr
, offset
, len
);
390 return memory_region_dispatch_read(mr
, offset
, data
,
391 size_memop(len
) | MO_BE
,
392 MEMTXATTRS_UNSPECIFIED
);
395 int pcilg_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
, uintptr_t ra
)
397 CPUS390XState
*env
= &cpu
->env
;
398 S390PCIBusDevice
*pbdev
;
406 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
407 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
412 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
416 fh
= env
->regs
[r2
] >> 32;
417 pcias
= (env
->regs
[r2
] >> 16) & 0xf;
418 len
= env
->regs
[r2
] & 0xf;
419 offset
= env
->regs
[r2
+ 1];
421 if (!(fh
& FH_MASK_ENABLE
)) {
422 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
426 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
428 DPRINTF("pcilg no pci dev\n");
429 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
433 switch (pbdev
->state
) {
434 case ZPCI_FS_PERMANENT_ERROR
:
436 setcc(cpu
, ZPCI_PCI_LS_ERR
);
437 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_BLOCKED
);
444 case ZPCI_IO_BAR_MIN
...ZPCI_IO_BAR_MAX
:
445 if (!len
|| (len
> (8 - (offset
& 0x7)))) {
446 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
449 result
= zpci_read_bar(pbdev
, pcias
, offset
, &data
, len
);
450 if (result
!= MEMTX_OK
) {
451 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
455 case ZPCI_CONFIG_BAR
:
456 if (!len
|| (len
> (4 - (offset
& 0x3))) || len
== 3) {
457 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
460 data
= pci_host_config_read_common(
461 pbdev
->pdev
, offset
, pci_config_size(pbdev
->pdev
), len
);
463 if (zpci_endian_swap(&data
, len
)) {
464 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
469 DPRINTF("pcilg invalid space\n");
470 setcc(cpu
, ZPCI_PCI_LS_ERR
);
471 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_INVAL_AS
);
475 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_LD
]++;
477 env
->regs
[r1
] = data
;
478 setcc(cpu
, ZPCI_PCI_LS_OK
);
482 static MemTxResult
zpci_write_bar(S390PCIBusDevice
*pbdev
, uint8_t pcias
,
483 uint64_t offset
, uint64_t data
, uint8_t len
)
487 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
488 mr
= s390_get_subregion(mr
, offset
, len
);
490 return memory_region_dispatch_write(mr
, offset
, data
,
491 size_memop(len
) | MO_BE
,
492 MEMTXATTRS_UNSPECIFIED
);
495 int pcistg_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
, uintptr_t ra
)
497 CPUS390XState
*env
= &cpu
->env
;
498 uint64_t offset
, data
;
499 S390PCIBusDevice
*pbdev
;
505 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
506 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
511 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
515 fh
= env
->regs
[r2
] >> 32;
516 pcias
= (env
->regs
[r2
] >> 16) & 0xf;
517 len
= env
->regs
[r2
] & 0xf;
518 offset
= env
->regs
[r2
+ 1];
519 data
= env
->regs
[r1
];
521 if (!(fh
& FH_MASK_ENABLE
)) {
522 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
526 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
528 DPRINTF("pcistg no pci dev\n");
529 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
533 switch (pbdev
->state
) {
534 /* ZPCI_FS_RESERVED, ZPCI_FS_STANDBY and ZPCI_FS_DISABLED
535 * are already covered by the FH_MASK_ENABLE check above
537 case ZPCI_FS_PERMANENT_ERROR
:
539 setcc(cpu
, ZPCI_PCI_LS_ERR
);
540 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_BLOCKED
);
547 /* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */
548 case ZPCI_IO_BAR_MIN
...ZPCI_IO_BAR_MAX
:
550 * A length of 0 is invalid and length should not cross a double word
552 if (!len
|| (len
> (8 - (offset
& 0x7)))) {
553 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
557 result
= zpci_write_bar(pbdev
, pcias
, offset
, data
, len
);
558 if (result
!= MEMTX_OK
) {
559 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
563 case ZPCI_CONFIG_BAR
:
564 /* ZPCI uses the pseudo BAR number 15 as configuration space */
565 /* possible access lengths are 1,2,4 and must not cross a word */
566 if (!len
|| (len
> (4 - (offset
& 0x3))) || len
== 3) {
567 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
570 /* len = 1,2,4 so we do not need to test */
571 zpci_endian_swap(&data
, len
);
572 pci_host_config_write_common(pbdev
->pdev
, offset
,
573 pci_config_size(pbdev
->pdev
),
577 DPRINTF("pcistg invalid space\n");
578 setcc(cpu
, ZPCI_PCI_LS_ERR
);
579 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_INVAL_AS
);
583 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_ST
]++;
585 setcc(cpu
, ZPCI_PCI_LS_OK
);
589 static uint32_t s390_pci_update_iotlb(S390PCIIOMMU
*iommu
,
590 S390IOTLBEntry
*entry
)
592 S390IOTLBEntry
*cache
= g_hash_table_lookup(iommu
->iotlb
, &entry
->iova
);
593 IOMMUTLBEntry notify
= {
594 .target_as
= &address_space_memory
,
596 .translated_addr
= entry
->translated_addr
,
598 .addr_mask
= ~PAGE_MASK
,
601 if (entry
->perm
== IOMMU_NONE
) {
605 g_hash_table_remove(iommu
->iotlb
, &entry
->iova
);
606 inc_dma_avail(iommu
);
609 if (cache
->perm
== entry
->perm
&&
610 cache
->translated_addr
== entry
->translated_addr
) {
614 notify
.perm
= IOMMU_NONE
;
615 memory_region_notify_iommu(&iommu
->iommu_mr
, 0, notify
);
616 notify
.perm
= entry
->perm
;
619 cache
= g_new(S390IOTLBEntry
, 1);
620 cache
->iova
= entry
->iova
;
621 cache
->translated_addr
= entry
->translated_addr
;
622 cache
->len
= PAGE_SIZE
;
623 cache
->perm
= entry
->perm
;
624 g_hash_table_replace(iommu
->iotlb
, &cache
->iova
, cache
);
625 dec_dma_avail(iommu
);
628 memory_region_notify_iommu(&iommu
->iommu_mr
, 0, notify
);
631 return iommu
->dma_limit
? iommu
->dma_limit
->avail
: 1;
634 int rpcit_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
, uintptr_t ra
)
636 CPUS390XState
*env
= &cpu
->env
;
639 S390PCIBusDevice
*pbdev
;
641 S390IOTLBEntry entry
;
645 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
646 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
651 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
655 fh
= env
->regs
[r1
] >> 32;
656 start
= env
->regs
[r2
];
657 end
= start
+ env
->regs
[r2
+ 1];
659 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
661 DPRINTF("rpcit no pci dev\n");
662 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
666 switch (pbdev
->state
) {
667 case ZPCI_FS_RESERVED
:
668 case ZPCI_FS_STANDBY
:
669 case ZPCI_FS_DISABLED
:
670 case ZPCI_FS_PERMANENT_ERROR
:
671 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
674 setcc(cpu
, ZPCI_PCI_LS_ERR
);
675 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_ERROR_RECOVER
);
681 iommu
= pbdev
->iommu
;
682 if (iommu
->dma_limit
) {
683 dma_avail
= iommu
->dma_limit
->avail
;
687 if (!iommu
->g_iota
) {
688 error
= ERR_EVENT_INVALAS
;
692 if (end
< iommu
->pba
|| start
> iommu
->pal
) {
693 error
= ERR_EVENT_OORANGE
;
697 while (start
< end
) {
698 error
= s390_guest_io_table_walk(iommu
->g_iota
, start
, &entry
);
704 while (entry
.iova
< start
&& entry
.iova
< end
&&
705 (dma_avail
> 0 || entry
.perm
== IOMMU_NONE
)) {
706 dma_avail
= s390_pci_update_iotlb(iommu
, &entry
);
707 entry
.iova
+= PAGE_SIZE
;
708 entry
.translated_addr
+= PAGE_SIZE
;
713 pbdev
->state
= ZPCI_FS_ERROR
;
714 setcc(cpu
, ZPCI_PCI_LS_ERR
);
715 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_FUNC_IN_ERR
);
716 s390_pci_generate_error_event(error
, pbdev
->fh
, pbdev
->fid
, start
, 0);
718 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_RPCIT
]++;
720 setcc(cpu
, ZPCI_PCI_LS_OK
);
722 /* vfio DMA mappings are exhausted, trigger a RPCIT */
723 setcc(cpu
, ZPCI_PCI_LS_ERR
);
724 s390_set_status_code(env
, r1
, ZPCI_RPCIT_ST_INSUFF_RES
);
730 int pcistb_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r3
, uint64_t gaddr
,
731 uint8_t ar
, uintptr_t ra
)
733 CPUS390XState
*env
= &cpu
->env
;
734 S390PCIBusDevice
*pbdev
;
744 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
745 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
749 fh
= env
->regs
[r1
] >> 32;
750 pcias
= (env
->regs
[r1
] >> 16) & 0xf;
751 len
= env
->regs
[r1
] & 0xff;
752 offset
= env
->regs
[r3
];
754 if (!(fh
& FH_MASK_ENABLE
)) {
755 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
759 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
761 DPRINTF("pcistb no pci dev fh 0x%x\n", fh
);
762 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
766 switch (pbdev
->state
) {
767 case ZPCI_FS_PERMANENT_ERROR
:
769 setcc(cpu
, ZPCI_PCI_LS_ERR
);
770 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_BLOCKED
);
776 if (pcias
> ZPCI_IO_BAR_MAX
) {
777 DPRINTF("pcistb invalid space\n");
778 setcc(cpu
, ZPCI_PCI_LS_ERR
);
779 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_INVAL_AS
);
783 /* Verify the address, offset and length */
784 /* offset must be a multiple of 8 */
786 goto specification_error
;
788 /* Length must be greater than 8, a multiple of 8 */
789 /* and not greater than maxstbl */
790 if ((len
<= 8) || (len
% 8) ||
791 (len
> pbdev
->pci_group
->zpci_group
.maxstbl
)) {
792 goto specification_error
;
794 /* Do not cross a 4K-byte boundary */
795 if (((offset
& 0xfff) + len
) > 0x1000) {
796 goto specification_error
;
798 /* Guest address must be double word aligned */
799 if (gaddr
& 0x07UL
) {
800 goto specification_error
;
803 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
804 mr
= s390_get_subregion(mr
, offset
, len
);
807 if (!memory_region_access_valid(mr
, offset
, len
, true,
808 MEMTXATTRS_UNSPECIFIED
)) {
809 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
813 if (s390_cpu_virt_mem_read(cpu
, gaddr
, ar
, buffer
, len
)) {
814 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
818 for (i
= 0; i
< len
/ 8; i
++) {
819 result
= memory_region_dispatch_write(mr
, offset
+ i
* 8,
820 ldq_p(buffer
+ i
* 8),
821 MO_64
, MEMTXATTRS_UNSPECIFIED
);
822 if (result
!= MEMTX_OK
) {
823 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
828 pbdev
->fmb
.counter
[ZPCI_FMB_CNT_STB
]++;
830 setcc(cpu
, ZPCI_PCI_LS_OK
);
834 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
838 static int reg_irqs(CPUS390XState
*env
, S390PCIBusDevice
*pbdev
, ZpciFib fib
)
841 uint8_t isc
= FIB_DATA_ISC(ldl_p(&fib
.data
));
843 pbdev
->routes
.adapter
.adapter_id
= css_get_adapter_id(
844 CSS_IO_ADAPTER_PCI
, isc
);
845 pbdev
->summary_ind
= get_indicator(ldq_p(&fib
.aisb
), sizeof(uint64_t));
846 len
= BITS_TO_LONGS(FIB_DATA_NOI(ldl_p(&fib
.data
))) * sizeof(unsigned long);
847 pbdev
->indicator
= get_indicator(ldq_p(&fib
.aibv
), len
);
849 ret
= map_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
854 ret
= map_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
859 pbdev
->routes
.adapter
.summary_addr
= ldq_p(&fib
.aisb
);
860 pbdev
->routes
.adapter
.summary_offset
= FIB_DATA_AISBO(ldl_p(&fib
.data
));
861 pbdev
->routes
.adapter
.ind_addr
= ldq_p(&fib
.aibv
);
862 pbdev
->routes
.adapter
.ind_offset
= FIB_DATA_AIBVO(ldl_p(&fib
.data
));
864 pbdev
->noi
= FIB_DATA_NOI(ldl_p(&fib
.data
));
865 pbdev
->sum
= FIB_DATA_SUM(ldl_p(&fib
.data
));
867 DPRINTF("reg_irqs adapter id %d\n", pbdev
->routes
.adapter
.adapter_id
);
870 release_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
871 release_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
872 pbdev
->summary_ind
= NULL
;
873 pbdev
->indicator
= NULL
;
877 int pci_dereg_irqs(S390PCIBusDevice
*pbdev
)
879 release_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
880 release_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
882 pbdev
->summary_ind
= NULL
;
883 pbdev
->indicator
= NULL
;
884 pbdev
->routes
.adapter
.summary_addr
= 0;
885 pbdev
->routes
.adapter
.summary_offset
= 0;
886 pbdev
->routes
.adapter
.ind_addr
= 0;
887 pbdev
->routes
.adapter
.ind_offset
= 0;
892 DPRINTF("dereg_irqs adapter id %d\n", pbdev
->routes
.adapter
.adapter_id
);
896 static int reg_ioat(CPUS390XState
*env
, S390PCIIOMMU
*iommu
, ZpciFib fib
,
899 uint64_t pba
= ldq_p(&fib
.pba
);
900 uint64_t pal
= ldq_p(&fib
.pal
);
901 uint64_t g_iota
= ldq_p(&fib
.iota
);
902 uint8_t dt
= (g_iota
>> 2) & 0x7;
903 uint8_t t
= (g_iota
>> 11) & 0x1;
907 if (pba
> pal
|| pba
< ZPCI_SDMA_ADDR
|| pal
> ZPCI_EDMA_ADDR
) {
908 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
912 /* currently we only support designation type 1 with translation */
913 if (!(dt
== ZPCI_IOTA_RTTO
&& t
)) {
914 error_report("unsupported ioat dt %d t %d", dt
, t
);
915 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
921 iommu
->g_iota
= g_iota
;
923 s390_pci_iommu_enable(iommu
);
928 void pci_dereg_ioat(S390PCIIOMMU
*iommu
)
930 s390_pci_iommu_disable(iommu
);
936 void fmb_timer_free(S390PCIBusDevice
*pbdev
)
938 if (pbdev
->fmb_timer
) {
939 timer_del(pbdev
->fmb_timer
);
940 timer_free(pbdev
->fmb_timer
);
941 pbdev
->fmb_timer
= NULL
;
944 memset(&pbdev
->fmb
, 0, sizeof(ZpciFmb
));
947 static int fmb_do_update(S390PCIBusDevice
*pbdev
, int offset
, uint64_t val
,
951 uint64_t dst
= pbdev
->fmb_addr
+ offset
;
955 address_space_stq_be(&address_space_memory
, dst
, val
,
956 MEMTXATTRS_UNSPECIFIED
,
960 address_space_stl_be(&address_space_memory
, dst
, val
,
961 MEMTXATTRS_UNSPECIFIED
,
965 address_space_stw_be(&address_space_memory
, dst
, val
,
966 MEMTXATTRS_UNSPECIFIED
,
970 address_space_stb(&address_space_memory
, dst
, val
,
971 MEMTXATTRS_UNSPECIFIED
,
978 if (ret
!= MEMTX_OK
) {
979 s390_pci_generate_error_event(ERR_EVENT_FMBA
, pbdev
->fh
, pbdev
->fid
,
981 fmb_timer_free(pbdev
);
987 static void fmb_update(void *opaque
)
989 S390PCIBusDevice
*pbdev
= opaque
;
990 int64_t t
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
994 pbdev
->fmb
.last_update
*= 2;
995 pbdev
->fmb
.last_update
|= UPDATE_U_BIT
;
996 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, last_update
),
997 pbdev
->fmb
.last_update
,
998 sizeof(pbdev
->fmb
.last_update
))) {
1002 /* Update FMB sample count */
1003 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, sample
),
1004 pbdev
->fmb
.sample
++,
1005 sizeof(pbdev
->fmb
.sample
))) {
1009 /* Update FMB counters */
1010 for (i
= 0; i
< ZPCI_FMB_CNT_MAX
; i
++) {
1011 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, counter
[i
]),
1012 pbdev
->fmb
.counter
[i
],
1013 sizeof(pbdev
->fmb
.counter
[0]))) {
1018 /* Clear U bit and update the time */
1019 pbdev
->fmb
.last_update
= time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
1020 pbdev
->fmb
.last_update
*= 2;
1021 if (fmb_do_update(pbdev
, offsetof(ZpciFmb
, last_update
),
1022 pbdev
->fmb
.last_update
,
1023 sizeof(pbdev
->fmb
.last_update
))) {
1026 timer_mod(pbdev
->fmb_timer
, t
+ DEFAULT_MUI
);
1029 int mpcifc_service_call(S390CPU
*cpu
, uint8_t r1
, uint64_t fiba
, uint8_t ar
,
1032 CPUS390XState
*env
= &cpu
->env
;
1036 S390PCIBusDevice
*pbdev
;
1037 uint64_t cc
= ZPCI_PCI_LS_OK
;
1039 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
1040 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
1044 oc
= env
->regs
[r1
] & 0xff;
1045 dmaas
= (env
->regs
[r1
] >> 16) & 0xff;
1046 fh
= env
->regs
[r1
] >> 32;
1049 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
1053 pbdev
= s390_pci_find_dev_by_fh(s390_get_phb(), fh
);
1055 DPRINTF("mpcifc no pci dev fh 0x%x\n", fh
);
1056 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1060 switch (pbdev
->state
) {
1061 case ZPCI_FS_RESERVED
:
1062 case ZPCI_FS_STANDBY
:
1063 case ZPCI_FS_DISABLED
:
1064 case ZPCI_FS_PERMANENT_ERROR
:
1065 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1071 if (s390_cpu_virt_mem_read(cpu
, fiba
, ar
, (uint8_t *)&fib
, sizeof(fib
))) {
1072 s390_cpu_virt_mem_handle_exc(cpu
, ra
);
1077 s390_program_interrupt(env
, PGM_OPERAND
, ra
);
1082 case ZPCI_MOD_FC_REG_INT
:
1083 if (pbdev
->summary_ind
) {
1084 cc
= ZPCI_PCI_LS_ERR
;
1085 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1086 } else if (reg_irqs(env
, pbdev
, fib
)) {
1087 cc
= ZPCI_PCI_LS_ERR
;
1088 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_RES_NOT_AVAIL
);
1091 case ZPCI_MOD_FC_DEREG_INT
:
1092 if (!pbdev
->summary_ind
) {
1093 cc
= ZPCI_PCI_LS_ERR
;
1094 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1096 pci_dereg_irqs(pbdev
);
1099 case ZPCI_MOD_FC_REG_IOAT
:
1101 cc
= ZPCI_PCI_LS_ERR
;
1102 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
1103 } else if (pbdev
->iommu
->enabled
) {
1104 cc
= ZPCI_PCI_LS_ERR
;
1105 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1106 } else if (reg_ioat(env
, pbdev
->iommu
, fib
, ra
)) {
1107 cc
= ZPCI_PCI_LS_ERR
;
1108 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_INSUF_RES
);
1111 case ZPCI_MOD_FC_DEREG_IOAT
:
1113 cc
= ZPCI_PCI_LS_ERR
;
1114 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
1115 } else if (!pbdev
->iommu
->enabled
) {
1116 cc
= ZPCI_PCI_LS_ERR
;
1117 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1119 pci_dereg_ioat(pbdev
->iommu
);
1122 case ZPCI_MOD_FC_REREG_IOAT
:
1124 cc
= ZPCI_PCI_LS_ERR
;
1125 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
1126 } else if (!pbdev
->iommu
->enabled
) {
1127 cc
= ZPCI_PCI_LS_ERR
;
1128 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1130 pci_dereg_ioat(pbdev
->iommu
);
1131 if (reg_ioat(env
, pbdev
->iommu
, fib
, ra
)) {
1132 cc
= ZPCI_PCI_LS_ERR
;
1133 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_INSUF_RES
);
1137 case ZPCI_MOD_FC_RESET_ERROR
:
1138 switch (pbdev
->state
) {
1139 case ZPCI_FS_BLOCKED
:
1141 pbdev
->state
= ZPCI_FS_ENABLED
;
1144 cc
= ZPCI_PCI_LS_ERR
;
1145 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1148 case ZPCI_MOD_FC_RESET_BLOCK
:
1149 switch (pbdev
->state
) {
1151 pbdev
->state
= ZPCI_FS_BLOCKED
;
1154 cc
= ZPCI_PCI_LS_ERR
;
1155 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
1158 case ZPCI_MOD_FC_SET_MEASURE
: {
1159 uint64_t fmb_addr
= ldq_p(&fib
.fmb_addr
);
1161 if (fmb_addr
& FMBK_MASK
) {
1162 cc
= ZPCI_PCI_LS_ERR
;
1163 s390_pci_generate_error_event(ERR_EVENT_FMBPRO
, pbdev
->fh
,
1164 pbdev
->fid
, fmb_addr
, 0);
1165 fmb_timer_free(pbdev
);
1170 /* Stop updating FMB. */
1171 fmb_timer_free(pbdev
);
1175 if (!pbdev
->fmb_timer
) {
1176 pbdev
->fmb_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
1178 } else if (timer_pending(pbdev
->fmb_timer
)) {
1179 /* Remove pending timer to update FMB address. */
1180 timer_del(pbdev
->fmb_timer
);
1182 pbdev
->fmb_addr
= fmb_addr
;
1183 timer_mod(pbdev
->fmb_timer
,
1184 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + DEFAULT_MUI
);
1188 s390_program_interrupt(&cpu
->env
, PGM_OPERAND
, ra
);
1189 cc
= ZPCI_PCI_LS_ERR
;
1196 int stpcifc_service_call(S390CPU
*cpu
, uint8_t r1
, uint64_t fiba
, uint8_t ar
,
1199 CPUS390XState
*env
= &cpu
->env
;
1203 S390PCIBusDevice
*pbdev
;
1205 uint64_t cc
= ZPCI_PCI_LS_OK
;
1207 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
1208 s390_program_interrupt(env
, PGM_PRIVILEGED
, ra
);
1212 fh
= env
->regs
[r1
] >> 32;
1213 dmaas
= (env
->regs
[r1
] >> 16) & 0xff;
1216 setcc(cpu
, ZPCI_PCI_LS_ERR
);
1217 s390_set_status_code(env
, r1
, ZPCI_STPCIFC_ST_INVAL_DMAAS
);
1222 s390_program_interrupt(env
, PGM_SPECIFICATION
, ra
);
1226 pbdev
= s390_pci_find_dev_by_idx(s390_get_phb(), fh
& FH_MASK_INDEX
);
1228 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1232 memset(&fib
, 0, sizeof(fib
));
1234 switch (pbdev
->state
) {
1235 case ZPCI_FS_RESERVED
:
1236 case ZPCI_FS_STANDBY
:
1237 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1239 case ZPCI_FS_DISABLED
:
1240 if (fh
& FH_MASK_ENABLE
) {
1241 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
1245 /* BLOCKED bit is set to one coincident with the setting of ERROR bit.
1246 * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */
1250 case ZPCI_FS_BLOCKED
:
1253 case ZPCI_FS_ENABLED
:
1255 if (pbdev
->iommu
->enabled
) {
1258 if (!(fh
& FH_MASK_ENABLE
)) {
1259 env
->regs
[r1
] |= 1ULL << 63;
1262 case ZPCI_FS_PERMANENT_ERROR
:
1263 setcc(cpu
, ZPCI_PCI_LS_ERR
);
1264 s390_set_status_code(env
, r1
, ZPCI_STPCIFC_ST_PERM_ERROR
);
1268 stq_p(&fib
.pba
, pbdev
->iommu
->pba
);
1269 stq_p(&fib
.pal
, pbdev
->iommu
->pal
);
1270 stq_p(&fib
.iota
, pbdev
->iommu
->g_iota
);
1271 stq_p(&fib
.aibv
, pbdev
->routes
.adapter
.ind_addr
);
1272 stq_p(&fib
.aisb
, pbdev
->routes
.adapter
.summary_addr
);
1273 stq_p(&fib
.fmb_addr
, pbdev
->fmb_addr
);
1275 data
= ((uint32_t)pbdev
->isc
<< 28) | ((uint32_t)pbdev
->noi
<< 16) |
1276 ((uint32_t)pbdev
->routes
.adapter
.ind_offset
<< 8) |
1277 ((uint32_t)pbdev
->sum
<< 7) | pbdev
->routes
.adapter
.summary_offset
;
1278 stl_p(&fib
.data
, data
);
1281 if (s390_cpu_virt_mem_write(cpu
, fiba
, ar
, (uint8_t *)&fib
, sizeof(fib
))) {
1282 s390_cpu_virt_mem_handle_exc(cpu
, ra
);