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 "qemu-common.h"
17 #include "s390-pci-inst.h"
18 #include "s390-pci-bus.h"
19 #include "exec/memory-internal.h"
20 #include "qemu/error-report.h"
22 /* #define DEBUG_S390PCI_INST */
23 #ifdef DEBUG_S390PCI_INST
24 #define DPRINTF(fmt, ...) \
25 do { fprintf(stderr, "s390pci-inst: " fmt, ## __VA_ARGS__); } while (0)
27 #define DPRINTF(fmt, ...) \
31 static void s390_set_status_code(CPUS390XState
*env
,
32 uint8_t r
, uint64_t status_code
)
34 env
->regs
[r
] &= ~0xff000000ULL
;
35 env
->regs
[r
] |= (status_code
& 0xff) << 24;
38 static int list_pci(ClpReqRspListPci
*rrb
, uint8_t *cc
)
40 S390PCIBusDevice
*pbdev
= NULL
;
41 uint32_t res_code
, initial_l2
, g_l2
;
43 uint64_t resume_token
;
46 if (lduw_p(&rrb
->request
.hdr
.len
) != 32) {
47 res_code
= CLP_RC_LEN
;
52 if ((ldl_p(&rrb
->request
.fmt
) & CLP_MASK_FMT
) != 0) {
53 res_code
= CLP_RC_FMT
;
58 if ((ldl_p(&rrb
->request
.fmt
) & ~CLP_MASK_FMT
) != 0 ||
59 ldq_p(&rrb
->request
.reserved1
) != 0) {
60 res_code
= CLP_RC_RESNOT0
;
65 resume_token
= ldq_p(&rrb
->request
.resume_token
);
68 pbdev
= s390_pci_find_dev_by_idx(resume_token
);
70 res_code
= CLP_RC_LISTPCI_BADRT
;
75 pbdev
= s390_pci_find_next_avail_dev(NULL
);
78 if (lduw_p(&rrb
->response
.hdr
.len
) < 48) {
84 initial_l2
= lduw_p(&rrb
->response
.hdr
.len
);
85 if ((initial_l2
- LIST_PCI_HDR_LEN
) % sizeof(ClpFhListEntry
)
87 res_code
= CLP_RC_LEN
;
93 stl_p(&rrb
->response
.fmt
, 0);
94 stq_p(&rrb
->response
.reserved1
, 0);
95 stl_p(&rrb
->response
.mdd
, FH_MASK_SHM
);
96 stw_p(&rrb
->response
.max_fn
, PCI_MAX_FUNCTIONS
);
97 rrb
->response
.flags
= UID_CHECKING_ENABLED
;
98 rrb
->response
.entry_size
= sizeof(ClpFhListEntry
);
101 g_l2
= LIST_PCI_HDR_LEN
;
102 while (g_l2
< initial_l2
&& pbdev
) {
103 stw_p(&rrb
->response
.fh_list
[i
].device_id
,
104 pci_get_word(pbdev
->pdev
->config
+ PCI_DEVICE_ID
));
105 stw_p(&rrb
->response
.fh_list
[i
].vendor_id
,
106 pci_get_word(pbdev
->pdev
->config
+ PCI_VENDOR_ID
));
107 /* Ignore RESERVED devices. */
108 stl_p(&rrb
->response
.fh_list
[i
].config
,
109 pbdev
->state
== ZPCI_FS_STANDBY
? 0 : 1 << 31);
110 stl_p(&rrb
->response
.fh_list
[i
].fid
, pbdev
->fid
);
111 stl_p(&rrb
->response
.fh_list
[i
].fh
, pbdev
->fh
);
113 g_l2
+= sizeof(ClpFhListEntry
);
114 /* Add endian check for DPRINTF? */
115 DPRINTF("g_l2 %d vendor id 0x%x device id 0x%x fid 0x%x fh 0x%x\n",
117 lduw_p(&rrb
->response
.fh_list
[i
].vendor_id
),
118 lduw_p(&rrb
->response
.fh_list
[i
].device_id
),
119 ldl_p(&rrb
->response
.fh_list
[i
].fid
),
120 ldl_p(&rrb
->response
.fh_list
[i
].fh
));
121 pbdev
= s390_pci_find_next_avail_dev(pbdev
);
128 resume_token
= pbdev
->fh
& FH_MASK_INDEX
;
130 stq_p(&rrb
->response
.resume_token
, resume_token
);
131 stw_p(&rrb
->response
.hdr
.len
, g_l2
);
132 stw_p(&rrb
->response
.hdr
.rsp
, CLP_RC_OK
);
135 DPRINTF("list pci failed rc 0x%x\n", rc
);
136 stw_p(&rrb
->response
.hdr
.rsp
, res_code
);
141 int clp_service_call(S390CPU
*cpu
, uint8_t r2
)
145 S390PCIBusDevice
*pbdev
;
148 uint8_t buffer
[4096 * 2];
150 CPUS390XState
*env
= &cpu
->env
;
153 cpu_synchronize_state(CPU(cpu
));
155 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
156 program_interrupt(env
, PGM_PRIVILEGED
, 4);
160 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
, sizeof(*reqh
))) {
163 reqh
= (ClpReqHdr
*)buffer
;
164 req_len
= lduw_p(&reqh
->len
);
165 if (req_len
< 16 || req_len
> 8184 || (req_len
% 8 != 0)) {
166 program_interrupt(env
, PGM_OPERAND
, 4);
170 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
,
171 req_len
+ sizeof(*resh
))) {
174 resh
= (ClpRspHdr
*)(buffer
+ req_len
);
175 res_len
= lduw_p(&resh
->len
);
176 if (res_len
< 8 || res_len
> 8176 || (res_len
% 8 != 0)) {
177 program_interrupt(env
, PGM_OPERAND
, 4);
180 if ((req_len
+ res_len
) > 8192) {
181 program_interrupt(env
, PGM_OPERAND
, 4);
185 if (s390_cpu_virt_mem_read(cpu
, env
->regs
[r2
], r2
, buffer
,
186 req_len
+ res_len
)) {
191 stw_p(&resh
->rsp
, CLP_RC_LEN
);
195 switch (lduw_p(&reqh
->cmd
)) {
197 ClpReqRspListPci
*rrb
= (ClpReqRspListPci
*)buffer
;
201 case CLP_SET_PCI_FN
: {
202 ClpReqSetPci
*reqsetpci
= (ClpReqSetPci
*)reqh
;
203 ClpRspSetPci
*ressetpci
= (ClpRspSetPci
*)resh
;
205 pbdev
= s390_pci_find_dev_by_fh(ldl_p(&reqsetpci
->fh
));
207 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FH
);
211 switch (reqsetpci
->oc
) {
212 case CLP_SET_ENABLE_PCI_FN
:
213 switch (reqsetpci
->ndas
) {
215 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_DMAAS
);
220 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_RES
);
224 if (pbdev
->fh
& FH_MASK_ENABLE
) {
225 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
229 pbdev
->fh
|= FH_MASK_ENABLE
;
230 pbdev
->state
= ZPCI_FS_ENABLED
;
231 stl_p(&ressetpci
->fh
, pbdev
->fh
);
232 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_OK
);
234 case CLP_SET_DISABLE_PCI_FN
:
235 if (!(pbdev
->fh
& FH_MASK_ENABLE
)) {
236 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
239 device_reset(DEVICE(pbdev
));
240 pbdev
->fh
&= ~FH_MASK_ENABLE
;
241 pbdev
->state
= ZPCI_FS_DISABLED
;
242 stl_p(&ressetpci
->fh
, pbdev
->fh
);
243 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_OK
);
246 DPRINTF("unknown set pci command\n");
247 stw_p(&ressetpci
->hdr
.rsp
, CLP_RC_SETPCIFN_FHOP
);
252 case CLP_QUERY_PCI_FN
: {
253 ClpReqQueryPci
*reqquery
= (ClpReqQueryPci
*)reqh
;
254 ClpRspQueryPci
*resquery
= (ClpRspQueryPci
*)resh
;
256 pbdev
= s390_pci_find_dev_by_fh(ldl_p(&reqquery
->fh
));
258 DPRINTF("query pci no pci dev\n");
259 stw_p(&resquery
->hdr
.rsp
, CLP_RC_SETPCIFN_FH
);
263 for (i
= 0; i
< PCI_BAR_COUNT
; i
++) {
264 uint32_t data
= pci_get_long(pbdev
->pdev
->config
+
265 PCI_BASE_ADDRESS_0
+ (i
* 4));
267 stl_p(&resquery
->bar
[i
], data
);
268 resquery
->bar_size
[i
] = pbdev
->pdev
->io_regions
[i
].size
?
269 ctz64(pbdev
->pdev
->io_regions
[i
].size
) : 0;
270 DPRINTF("bar %d addr 0x%x size 0x%" PRIx64
"barsize 0x%x\n", i
,
271 ldl_p(&resquery
->bar
[i
]),
272 pbdev
->pdev
->io_regions
[i
].size
,
273 resquery
->bar_size
[i
]);
276 stq_p(&resquery
->sdma
, ZPCI_SDMA_ADDR
);
277 stq_p(&resquery
->edma
, ZPCI_EDMA_ADDR
);
278 stl_p(&resquery
->fid
, pbdev
->fid
);
279 stw_p(&resquery
->pchid
, 0);
280 stw_p(&resquery
->ug
, 1);
281 stl_p(&resquery
->uid
, pbdev
->uid
);
282 stw_p(&resquery
->hdr
.rsp
, CLP_RC_OK
);
285 case CLP_QUERY_PCI_FNGRP
: {
286 ClpRspQueryPciGrp
*resgrp
= (ClpRspQueryPciGrp
*)resh
;
288 stq_p(&resgrp
->dasm
, 0);
289 stq_p(&resgrp
->msia
, ZPCI_MSI_ADDR
);
290 stw_p(&resgrp
->mui
, 0);
291 stw_p(&resgrp
->i
, 128);
294 stw_p(&resgrp
->hdr
.rsp
, CLP_RC_OK
);
298 DPRINTF("unknown clp command\n");
299 stw_p(&resh
->rsp
, CLP_RC_CMD
);
304 if (s390_cpu_virt_mem_write(cpu
, env
->regs
[r2
], r2
, buffer
,
305 req_len
+ res_len
)) {
312 int pcilg_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
)
314 CPUS390XState
*env
= &cpu
->env
;
315 S390PCIBusDevice
*pbdev
;
322 cpu_synchronize_state(CPU(cpu
));
324 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
325 program_interrupt(env
, PGM_PRIVILEGED
, 4);
330 program_interrupt(env
, PGM_SPECIFICATION
, 4);
334 fh
= env
->regs
[r2
] >> 32;
335 pcias
= (env
->regs
[r2
] >> 16) & 0xf;
336 len
= env
->regs
[r2
] & 0xf;
337 offset
= env
->regs
[r2
+ 1];
339 pbdev
= s390_pci_find_dev_by_fh(fh
);
341 DPRINTF("pcilg no pci dev\n");
342 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
346 switch (pbdev
->state
) {
347 case ZPCI_FS_RESERVED
:
348 case ZPCI_FS_STANDBY
:
349 case ZPCI_FS_DISABLED
:
350 case ZPCI_FS_PERMANENT_ERROR
:
351 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
354 setcc(cpu
, ZPCI_PCI_LS_ERR
);
355 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_BLOCKED
);
362 if ((8 - (offset
& 0x7)) < len
) {
363 program_interrupt(env
, PGM_OPERAND
, 4);
366 MemoryRegion
*mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
367 memory_region_dispatch_read(mr
, offset
, &data
, len
,
368 MEMTXATTRS_UNSPECIFIED
);
369 } else if (pcias
== 15) {
370 if ((4 - (offset
& 0x3)) < len
) {
371 program_interrupt(env
, PGM_OPERAND
, 4);
374 data
= pci_host_config_read_common(
375 pbdev
->pdev
, offset
, pci_config_size(pbdev
->pdev
), len
);
381 data
= bswap16(data
);
384 data
= bswap32(data
);
387 data
= bswap64(data
);
390 program_interrupt(env
, PGM_OPERAND
, 4);
394 DPRINTF("invalid space\n");
395 setcc(cpu
, ZPCI_PCI_LS_ERR
);
396 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_INVAL_AS
);
400 env
->regs
[r1
] = data
;
401 setcc(cpu
, ZPCI_PCI_LS_OK
);
405 static void update_msix_table_msg_data(S390PCIBusDevice
*pbdev
, uint64_t offset
,
406 uint64_t *data
, uint8_t len
)
411 if (offset
% PCI_MSIX_ENTRY_SIZE
!= 8) {
416 DPRINTF("access msix table msg data but len is %d\n", len
);
420 msg_data
= (uint8_t *)data
- offset
% PCI_MSIX_ENTRY_SIZE
+
421 PCI_MSIX_ENTRY_VECTOR_CTRL
;
422 val
= pci_get_long(msg_data
) |
423 ((pbdev
->fh
& FH_MASK_INDEX
) << ZPCI_MSI_VEC_BITS
);
424 pci_set_long(msg_data
, val
);
425 DPRINTF("update msix msg_data to 0x%" PRIx64
"\n", *data
);
428 static int trap_msix(S390PCIBusDevice
*pbdev
, uint64_t offset
, uint8_t pcias
)
430 if (pbdev
->msix
.available
&& pbdev
->msix
.table_bar
== pcias
&&
431 offset
>= pbdev
->msix
.table_offset
&&
432 offset
<= pbdev
->msix
.table_offset
+
433 (pbdev
->msix
.entries
- 1) * PCI_MSIX_ENTRY_SIZE
) {
440 int pcistg_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
)
442 CPUS390XState
*env
= &cpu
->env
;
443 uint64_t offset
, data
;
444 S390PCIBusDevice
*pbdev
;
449 cpu_synchronize_state(CPU(cpu
));
451 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
452 program_interrupt(env
, PGM_PRIVILEGED
, 4);
457 program_interrupt(env
, PGM_SPECIFICATION
, 4);
461 fh
= env
->regs
[r2
] >> 32;
462 pcias
= (env
->regs
[r2
] >> 16) & 0xf;
463 len
= env
->regs
[r2
] & 0xf;
464 offset
= env
->regs
[r2
+ 1];
466 pbdev
= s390_pci_find_dev_by_fh(fh
);
468 DPRINTF("pcistg no pci dev\n");
469 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
473 switch (pbdev
->state
) {
474 case ZPCI_FS_RESERVED
:
475 case ZPCI_FS_STANDBY
:
476 case ZPCI_FS_DISABLED
:
477 case ZPCI_FS_PERMANENT_ERROR
:
478 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
481 setcc(cpu
, ZPCI_PCI_LS_ERR
);
482 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_BLOCKED
);
488 data
= env
->regs
[r1
];
490 if ((8 - (offset
& 0x7)) < len
) {
491 program_interrupt(env
, PGM_OPERAND
, 4);
495 if (trap_msix(pbdev
, offset
, pcias
)) {
496 offset
= offset
- pbdev
->msix
.table_offset
;
497 mr
= &pbdev
->pdev
->msix_table_mmio
;
498 update_msix_table_msg_data(pbdev
, offset
, &data
, len
);
500 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
503 memory_region_dispatch_write(mr
, offset
, data
, len
,
504 MEMTXATTRS_UNSPECIFIED
);
505 } else if (pcias
== 15) {
506 if ((4 - (offset
& 0x3)) < len
) {
507 program_interrupt(env
, PGM_OPERAND
, 4);
514 data
= bswap16(data
);
517 data
= bswap32(data
);
520 data
= bswap64(data
);
523 program_interrupt(env
, PGM_OPERAND
, 4);
527 pci_host_config_write_common(pbdev
->pdev
, offset
,
528 pci_config_size(pbdev
->pdev
),
531 DPRINTF("pcistg invalid space\n");
532 setcc(cpu
, ZPCI_PCI_LS_ERR
);
533 s390_set_status_code(env
, r2
, ZPCI_PCI_ST_INVAL_AS
);
537 setcc(cpu
, ZPCI_PCI_LS_OK
);
541 int rpcit_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r2
)
543 CPUS390XState
*env
= &cpu
->env
;
545 S390PCIBusDevice
*pbdev
;
550 cpu_synchronize_state(CPU(cpu
));
552 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
553 program_interrupt(env
, PGM_PRIVILEGED
, 4);
558 program_interrupt(env
, PGM_SPECIFICATION
, 4);
562 fh
= env
->regs
[r1
] >> 32;
563 start
= env
->regs
[r2
];
564 end
= start
+ env
->regs
[r2
+ 1];
566 pbdev
= s390_pci_find_dev_by_fh(fh
);
568 DPRINTF("rpcit no pci dev\n");
569 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
573 switch (pbdev
->state
) {
574 case ZPCI_FS_RESERVED
:
575 case ZPCI_FS_STANDBY
:
576 case ZPCI_FS_DISABLED
:
577 case ZPCI_FS_PERMANENT_ERROR
:
578 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
581 setcc(cpu
, ZPCI_PCI_LS_ERR
);
582 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_ERROR_RECOVER
);
588 if (!pbdev
->g_iota
) {
589 pbdev
->state
= ZPCI_FS_ERROR
;
590 setcc(cpu
, ZPCI_PCI_LS_ERR
);
591 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_INSUF_RES
);
592 s390_pci_generate_error_event(ERR_EVENT_INVALAS
, pbdev
->fh
, pbdev
->fid
,
597 if (end
< pbdev
->pba
|| start
> pbdev
->pal
) {
598 pbdev
->state
= ZPCI_FS_ERROR
;
599 setcc(cpu
, ZPCI_PCI_LS_ERR
);
600 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_INSUF_RES
);
601 s390_pci_generate_error_event(ERR_EVENT_OORANGE
, pbdev
->fh
, pbdev
->fid
,
606 mr
= &pbdev
->iommu_mr
;
607 while (start
< end
) {
608 entry
= mr
->iommu_ops
->translate(mr
, start
, 0);
610 if (!entry
.translated_addr
) {
611 pbdev
->state
= ZPCI_FS_ERROR
;
612 setcc(cpu
, ZPCI_PCI_LS_ERR
);
613 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_INSUF_RES
);
614 s390_pci_generate_error_event(ERR_EVENT_SERR
, pbdev
->fh
, pbdev
->fid
,
615 start
, ERR_EVENT_Q_BIT
);
619 memory_region_notify_iommu(mr
, entry
);
620 start
+= entry
.addr_mask
+ 1;
623 setcc(cpu
, ZPCI_PCI_LS_OK
);
628 int pcistb_service_call(S390CPU
*cpu
, uint8_t r1
, uint8_t r3
, uint64_t gaddr
,
631 CPUS390XState
*env
= &cpu
->env
;
632 S390PCIBusDevice
*pbdev
;
640 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
641 program_interrupt(env
, PGM_PRIVILEGED
, 6);
645 fh
= env
->regs
[r1
] >> 32;
646 pcias
= (env
->regs
[r1
] >> 16) & 0xf;
647 len
= env
->regs
[r1
] & 0xff;
650 DPRINTF("pcistb invalid space\n");
651 setcc(cpu
, ZPCI_PCI_LS_ERR
);
652 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_INVAL_AS
);
663 program_interrupt(env
, PGM_SPECIFICATION
, 6);
667 pbdev
= s390_pci_find_dev_by_fh(fh
);
669 DPRINTF("pcistb no pci dev fh 0x%x\n", fh
);
670 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
674 switch (pbdev
->state
) {
675 case ZPCI_FS_RESERVED
:
676 case ZPCI_FS_STANDBY
:
677 case ZPCI_FS_DISABLED
:
678 case ZPCI_FS_PERMANENT_ERROR
:
679 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
682 setcc(cpu
, ZPCI_PCI_LS_ERR
);
683 s390_set_status_code(env
, r1
, ZPCI_PCI_ST_BLOCKED
);
689 mr
= pbdev
->pdev
->io_regions
[pcias
].memory
;
690 if (!memory_region_access_valid(mr
, env
->regs
[r3
], len
, true)) {
691 program_interrupt(env
, PGM_ADDRESSING
, 6);
695 if (s390_cpu_virt_mem_read(cpu
, gaddr
, ar
, buffer
, len
)) {
699 for (i
= 0; i
< len
/ 8; i
++) {
700 memory_region_dispatch_write(mr
, env
->regs
[r3
] + i
* 8,
701 ldq_p(buffer
+ i
* 8), 8,
702 MEMTXATTRS_UNSPECIFIED
);
705 setcc(cpu
, ZPCI_PCI_LS_OK
);
709 static int reg_irqs(CPUS390XState
*env
, S390PCIBusDevice
*pbdev
, ZpciFib fib
)
713 ret
= css_register_io_adapter(S390_PCIPT_ADAPTER
,
714 FIB_DATA_ISC(ldl_p(&fib
.data
)), true, false,
715 &pbdev
->routes
.adapter
.adapter_id
);
718 pbdev
->summary_ind
= get_indicator(ldq_p(&fib
.aisb
), sizeof(uint64_t));
719 len
= BITS_TO_LONGS(FIB_DATA_NOI(ldl_p(&fib
.data
))) * sizeof(unsigned long);
720 pbdev
->indicator
= get_indicator(ldq_p(&fib
.aibv
), len
);
722 ret
= map_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
727 ret
= map_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
732 pbdev
->routes
.adapter
.summary_addr
= ldq_p(&fib
.aisb
);
733 pbdev
->routes
.adapter
.summary_offset
= FIB_DATA_AISBO(ldl_p(&fib
.data
));
734 pbdev
->routes
.adapter
.ind_addr
= ldq_p(&fib
.aibv
);
735 pbdev
->routes
.adapter
.ind_offset
= FIB_DATA_AIBVO(ldl_p(&fib
.data
));
736 pbdev
->isc
= FIB_DATA_ISC(ldl_p(&fib
.data
));
737 pbdev
->noi
= FIB_DATA_NOI(ldl_p(&fib
.data
));
738 pbdev
->sum
= FIB_DATA_SUM(ldl_p(&fib
.data
));
740 DPRINTF("reg_irqs adapter id %d\n", pbdev
->routes
.adapter
.adapter_id
);
743 release_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
744 release_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
745 pbdev
->summary_ind
= NULL
;
746 pbdev
->indicator
= NULL
;
750 int pci_dereg_irqs(S390PCIBusDevice
*pbdev
)
752 release_indicator(&pbdev
->routes
.adapter
, pbdev
->summary_ind
);
753 release_indicator(&pbdev
->routes
.adapter
, pbdev
->indicator
);
755 pbdev
->summary_ind
= NULL
;
756 pbdev
->indicator
= NULL
;
757 pbdev
->routes
.adapter
.summary_addr
= 0;
758 pbdev
->routes
.adapter
.summary_offset
= 0;
759 pbdev
->routes
.adapter
.ind_addr
= 0;
760 pbdev
->routes
.adapter
.ind_offset
= 0;
765 DPRINTF("dereg_irqs adapter id %d\n", pbdev
->routes
.adapter
.adapter_id
);
769 static int reg_ioat(CPUS390XState
*env
, S390PCIBusDevice
*pbdev
, ZpciFib fib
)
771 uint64_t pba
= ldq_p(&fib
.pba
);
772 uint64_t pal
= ldq_p(&fib
.pal
);
773 uint64_t g_iota
= ldq_p(&fib
.iota
);
774 uint8_t dt
= (g_iota
>> 2) & 0x7;
775 uint8_t t
= (g_iota
>> 11) & 0x1;
777 if (pba
> pal
|| pba
< ZPCI_SDMA_ADDR
|| pal
> ZPCI_EDMA_ADDR
) {
778 program_interrupt(env
, PGM_OPERAND
, 6);
782 /* currently we only support designation type 1 with translation */
783 if (!(dt
== ZPCI_IOTA_RTTO
&& t
)) {
784 error_report("unsupported ioat dt %d t %d", dt
, t
);
785 program_interrupt(env
, PGM_OPERAND
, 6);
791 pbdev
->g_iota
= g_iota
;
793 s390_pci_iommu_enable(pbdev
);
798 void pci_dereg_ioat(S390PCIBusDevice
*pbdev
)
800 s390_pci_iommu_disable(pbdev
);
806 int mpcifc_service_call(S390CPU
*cpu
, uint8_t r1
, uint64_t fiba
, uint8_t ar
)
808 CPUS390XState
*env
= &cpu
->env
;
812 S390PCIBusDevice
*pbdev
;
813 uint64_t cc
= ZPCI_PCI_LS_OK
;
815 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
816 program_interrupt(env
, PGM_PRIVILEGED
, 6);
820 oc
= env
->regs
[r1
] & 0xff;
821 dmaas
= (env
->regs
[r1
] >> 16) & 0xff;
822 fh
= env
->regs
[r1
] >> 32;
825 program_interrupt(env
, PGM_SPECIFICATION
, 6);
829 pbdev
= s390_pci_find_dev_by_fh(fh
);
831 DPRINTF("mpcifc no pci dev fh 0x%x\n", fh
);
832 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
836 switch (pbdev
->state
) {
837 case ZPCI_FS_RESERVED
:
838 case ZPCI_FS_STANDBY
:
839 case ZPCI_FS_DISABLED
:
840 case ZPCI_FS_PERMANENT_ERROR
:
841 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
847 if (s390_cpu_virt_mem_read(cpu
, fiba
, ar
, (uint8_t *)&fib
, sizeof(fib
))) {
852 program_interrupt(env
, PGM_OPERAND
, 6);
857 case ZPCI_MOD_FC_REG_INT
:
858 if (pbdev
->summary_ind
) {
859 cc
= ZPCI_PCI_LS_ERR
;
860 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
861 } else if (reg_irqs(env
, pbdev
, fib
)) {
862 cc
= ZPCI_PCI_LS_ERR
;
863 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_RES_NOT_AVAIL
);
866 case ZPCI_MOD_FC_DEREG_INT
:
867 if (!pbdev
->summary_ind
) {
868 cc
= ZPCI_PCI_LS_ERR
;
869 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
871 pci_dereg_irqs(pbdev
);
874 case ZPCI_MOD_FC_REG_IOAT
:
876 cc
= ZPCI_PCI_LS_ERR
;
877 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
878 } else if (pbdev
->iommu_enabled
) {
879 cc
= ZPCI_PCI_LS_ERR
;
880 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
881 } else if (reg_ioat(env
, pbdev
, fib
)) {
882 cc
= ZPCI_PCI_LS_ERR
;
883 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_INSUF_RES
);
886 case ZPCI_MOD_FC_DEREG_IOAT
:
888 cc
= ZPCI_PCI_LS_ERR
;
889 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
890 } else if (!pbdev
->iommu_enabled
) {
891 cc
= ZPCI_PCI_LS_ERR
;
892 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
894 pci_dereg_ioat(pbdev
);
897 case ZPCI_MOD_FC_REREG_IOAT
:
899 cc
= ZPCI_PCI_LS_ERR
;
900 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_DMAAS_INVAL
);
901 } else if (!pbdev
->iommu_enabled
) {
902 cc
= ZPCI_PCI_LS_ERR
;
903 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
905 pci_dereg_ioat(pbdev
);
906 if (reg_ioat(env
, pbdev
, fib
)) {
907 cc
= ZPCI_PCI_LS_ERR
;
908 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_INSUF_RES
);
912 case ZPCI_MOD_FC_RESET_ERROR
:
913 switch (pbdev
->state
) {
914 case ZPCI_FS_BLOCKED
:
916 pbdev
->state
= ZPCI_FS_ENABLED
;
919 cc
= ZPCI_PCI_LS_ERR
;
920 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
923 case ZPCI_MOD_FC_RESET_BLOCK
:
924 switch (pbdev
->state
) {
926 pbdev
->state
= ZPCI_FS_BLOCKED
;
929 cc
= ZPCI_PCI_LS_ERR
;
930 s390_set_status_code(env
, r1
, ZPCI_MOD_ST_SEQUENCE
);
933 case ZPCI_MOD_FC_SET_MEASURE
:
934 pbdev
->fmb_addr
= ldq_p(&fib
.fmb_addr
);
937 program_interrupt(&cpu
->env
, PGM_OPERAND
, 6);
938 cc
= ZPCI_PCI_LS_ERR
;
945 int stpcifc_service_call(S390CPU
*cpu
, uint8_t r1
, uint64_t fiba
, uint8_t ar
)
947 CPUS390XState
*env
= &cpu
->env
;
951 S390PCIBusDevice
*pbdev
;
953 uint64_t cc
= ZPCI_PCI_LS_OK
;
955 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
956 program_interrupt(env
, PGM_PRIVILEGED
, 6);
960 fh
= env
->regs
[r1
] >> 32;
961 dmaas
= (env
->regs
[r1
] >> 16) & 0xff;
964 setcc(cpu
, ZPCI_PCI_LS_ERR
);
965 s390_set_status_code(env
, r1
, ZPCI_STPCIFC_ST_INVAL_DMAAS
);
970 program_interrupt(env
, PGM_SPECIFICATION
, 6);
974 pbdev
= s390_pci_find_dev_by_idx(fh
& FH_MASK_INDEX
);
976 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
980 memset(&fib
, 0, sizeof(fib
));
982 switch (pbdev
->state
) {
983 case ZPCI_FS_RESERVED
:
984 case ZPCI_FS_STANDBY
:
985 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
987 case ZPCI_FS_DISABLED
:
988 if (fh
& FH_MASK_ENABLE
) {
989 setcc(cpu
, ZPCI_PCI_LS_INVAL_HANDLE
);
993 /* BLOCKED bit is set to one coincident with the setting of ERROR bit.
994 * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */
997 case ZPCI_FS_BLOCKED
:
999 case ZPCI_FS_ENABLED
:
1001 if (pbdev
->iommu_enabled
) {
1004 if (!(fh
& FH_MASK_ENABLE
)) {
1005 env
->regs
[r1
] |= 1ULL << 63;
1008 case ZPCI_FS_PERMANENT_ERROR
:
1009 setcc(cpu
, ZPCI_PCI_LS_ERR
);
1010 s390_set_status_code(env
, r1
, ZPCI_STPCIFC_ST_PERM_ERROR
);
1014 stq_p(&fib
.pba
, pbdev
->pba
);
1015 stq_p(&fib
.pal
, pbdev
->pal
);
1016 stq_p(&fib
.iota
, pbdev
->g_iota
);
1017 stq_p(&fib
.aibv
, pbdev
->routes
.adapter
.ind_addr
);
1018 stq_p(&fib
.aisb
, pbdev
->routes
.adapter
.summary_addr
);
1019 stq_p(&fib
.fmb_addr
, pbdev
->fmb_addr
);
1021 data
= ((uint32_t)pbdev
->isc
<< 28) | ((uint32_t)pbdev
->noi
<< 16) |
1022 ((uint32_t)pbdev
->routes
.adapter
.ind_offset
<< 8) |
1023 ((uint32_t)pbdev
->sum
<< 7) | pbdev
->routes
.adapter
.summary_offset
;
1024 stl_p(&fib
.data
, data
);
1027 if (s390_cpu_virt_mem_write(cpu
, fiba
, ar
, (uint8_t *)&fib
, sizeof(fib
))) {