2 * I/O instructions for S/390
4 * Copyright 2012, 2015 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
12 #include <sys/types.h>
17 #include "hw/s390x/s390-pci-bus.h"
19 int ioinst_disassemble_sch_ident(uint32_t value
, int *m
, int *cssid
, int *ssid
,
22 if (!IOINST_SCHID_ONE(value
)) {
25 if (!IOINST_SCHID_M(value
)) {
26 if (IOINST_SCHID_CSSID(value
)) {
32 *cssid
= IOINST_SCHID_CSSID(value
);
35 *ssid
= IOINST_SCHID_SSID(value
);
36 *schid
= IOINST_SCHID_NR(value
);
40 void ioinst_handle_xsch(S390CPU
*cpu
, uint64_t reg1
)
42 int cssid
, ssid
, schid
, m
;
47 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
48 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
51 trace_ioinst_sch_id("xsch", cssid
, ssid
, schid
);
52 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
53 if (sch
&& css_subch_visible(sch
)) {
54 ret
= css_do_xsch(sch
);
73 void ioinst_handle_csch(S390CPU
*cpu
, uint64_t reg1
)
75 int cssid
, ssid
, schid
, m
;
80 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
81 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
84 trace_ioinst_sch_id("csch", cssid
, ssid
, schid
);
85 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
86 if (sch
&& css_subch_visible(sch
)) {
87 ret
= css_do_csch(sch
);
97 void ioinst_handle_hsch(S390CPU
*cpu
, uint64_t reg1
)
99 int cssid
, ssid
, schid
, m
;
104 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
105 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
108 trace_ioinst_sch_id("hsch", cssid
, ssid
, schid
);
109 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
110 if (sch
&& css_subch_visible(sch
)) {
111 ret
= css_do_hsch(sch
);
130 static int ioinst_schib_valid(SCHIB
*schib
)
132 if ((schib
->pmcw
.flags
& PMCW_FLAGS_MASK_INVALID
) ||
133 (schib
->pmcw
.chars
& PMCW_CHARS_MASK_INVALID
)) {
136 /* Disallow extended measurements for now. */
137 if (schib
->pmcw
.chars
& PMCW_CHARS_MASK_XMWME
) {
143 void ioinst_handle_msch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
145 int cssid
, ssid
, schid
, m
;
151 CPUS390XState
*env
= &cpu
->env
;
153 addr
= decode_basedisp_s(env
, ipb
);
155 program_interrupt(env
, PGM_SPECIFICATION
, 2);
158 if (s390_cpu_virt_mem_read(cpu
, addr
, &schib
, sizeof(schib
))) {
161 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
162 !ioinst_schib_valid(&schib
)) {
163 program_interrupt(env
, PGM_OPERAND
, 2);
166 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
167 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
168 if (sch
&& css_subch_visible(sch
)) {
169 ret
= css_do_msch(sch
, &schib
);
188 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
190 dest
->intparm
= be32_to_cpu(src
->intparm
);
191 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
192 dest
->lpm
= src
->lpm
;
193 dest
->ctrl1
= src
->ctrl1
;
194 dest
->cpa
= be32_to_cpu(src
->cpa
);
197 static int ioinst_orb_valid(ORB
*orb
)
199 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
200 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
203 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
209 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
211 int cssid
, ssid
, schid
, m
;
217 CPUS390XState
*env
= &cpu
->env
;
219 addr
= decode_basedisp_s(env
, ipb
);
221 program_interrupt(env
, PGM_SPECIFICATION
, 2);
224 if (s390_cpu_virt_mem_read(cpu
, addr
, &orig_orb
, sizeof(orb
))) {
227 copy_orb_from_guest(&orb
, &orig_orb
);
228 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
229 !ioinst_orb_valid(&orb
)) {
230 program_interrupt(env
, PGM_OPERAND
, 2);
233 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
234 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
235 if (sch
&& css_subch_visible(sch
)) {
236 ret
= css_do_ssch(sch
, &orb
);
255 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
260 hwaddr len
= sizeof(*crw
);
261 CPUS390XState
*env
= &cpu
->env
;
263 addr
= decode_basedisp_s(env
, ipb
);
265 program_interrupt(env
, PGM_SPECIFICATION
, 2);
268 crw
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
269 if (!crw
|| len
!= sizeof(*crw
)) {
270 program_interrupt(env
, PGM_ADDRESSING
, 2);
273 cc
= css_do_stcrw(crw
);
274 /* 0 - crw stored, 1 - zeroes stored */
278 s390_cpu_physical_memory_unmap(env
, crw
, len
, 1);
281 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
283 int cssid
, ssid
, schid
, m
;
288 hwaddr len
= sizeof(*schib
);
289 CPUS390XState
*env
= &cpu
->env
;
291 addr
= decode_basedisp_s(env
, ipb
);
293 program_interrupt(env
, PGM_SPECIFICATION
, 2);
296 schib
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
297 if (!schib
|| len
!= sizeof(*schib
)) {
298 program_interrupt(env
, PGM_ADDRESSING
, 2);
302 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
303 program_interrupt(env
, PGM_OPERAND
, 2);
306 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
307 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
309 if (css_subch_visible(sch
)) {
310 css_do_stsch(sch
, schib
);
313 /* Indicate no more subchannels in this css/ss */
317 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
318 cc
= 3; /* No more subchannels in this css/ss */
320 /* Store an empty schib. */
321 memset(schib
, 0, sizeof(*schib
));
328 s390_cpu_physical_memory_unmap(env
, schib
, len
, 1);
331 int ioinst_handle_tsch(CPUS390XState
*env
, uint64_t reg1
, uint32_t ipb
)
333 int cssid
, ssid
, schid
, m
;
339 hwaddr len
= sizeof(*irb
);
341 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
342 program_interrupt(env
, PGM_OPERAND
, 2);
345 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
346 addr
= decode_basedisp_s(env
, ipb
);
348 program_interrupt(env
, PGM_SPECIFICATION
, 2);
351 irb
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
352 if (!irb
|| len
!= sizeof(*irb
)) {
353 program_interrupt(env
, PGM_ADDRESSING
, 2);
357 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
358 if (sch
&& css_subch_visible(sch
)) {
359 ret
= css_do_tsch(sch
, irb
);
360 /* 0 - status pending, 1 - not status pending */
366 s390_cpu_physical_memory_unmap(env
, irb
, sizeof(*irb
), 1);
370 typedef struct ChscReq
{
376 } QEMU_PACKED ChscReq
;
378 typedef struct ChscResp
{
383 } QEMU_PACKED ChscResp
;
385 #define CHSC_MIN_RESP_LEN 0x0008
387 #define CHSC_SCPD 0x0002
388 #define CHSC_SCSC 0x0010
389 #define CHSC_SDA 0x0031
390 #define CHSC_SEI 0x000e
392 #define CHSC_SCPD_0_M 0x20000000
393 #define CHSC_SCPD_0_C 0x10000000
394 #define CHSC_SCPD_0_FMT 0x0f000000
395 #define CHSC_SCPD_0_CSSID 0x00ff0000
396 #define CHSC_SCPD_0_RFMT 0x00000f00
397 #define CHSC_SCPD_0_RES 0xc000f000
398 #define CHSC_SCPD_1_RES 0xffffff00
399 #define CHSC_SCPD_01_CHPID 0x000000ff
400 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
402 uint16_t len
= be16_to_cpu(req
->len
);
403 uint32_t param0
= be32_to_cpu(req
->param0
);
404 uint32_t param1
= be32_to_cpu(req
->param1
);
408 uint8_t f_chpid
, l_chpid
;
412 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
413 if ((rfmt
== 0) || (rfmt
== 1)) {
414 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
416 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
417 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
421 if (param0
& CHSC_SCPD_0_FMT
) {
425 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
426 m
= param0
& CHSC_SCPD_0_M
;
428 if (!m
|| !css_present(cssid
)) {
433 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
434 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
435 if (l_chpid
< f_chpid
) {
439 /* css_collect_chp_desc() is endian-aware */
440 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
442 res
->code
= cpu_to_be16(0x0001);
443 res
->len
= cpu_to_be16(8 + desc_size
);
444 res
->param
= cpu_to_be32(rfmt
);
448 res
->code
= cpu_to_be16(resp_code
);
449 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
450 res
->param
= cpu_to_be32(rfmt
);
453 #define CHSC_SCSC_0_M 0x20000000
454 #define CHSC_SCSC_0_FMT 0x000f0000
455 #define CHSC_SCSC_0_CSSID 0x0000ff00
456 #define CHSC_SCSC_0_RES 0xdff000ff
457 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
459 uint16_t len
= be16_to_cpu(req
->len
);
460 uint32_t param0
= be32_to_cpu(req
->param0
);
463 uint32_t general_chars
[510];
464 uint32_t chsc_chars
[508];
471 if (param0
& CHSC_SCSC_0_FMT
) {
475 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
477 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
482 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
486 res
->code
= cpu_to_be16(0x0001);
487 res
->len
= cpu_to_be16(4080);
490 memset(general_chars
, 0, sizeof(general_chars
));
491 memset(chsc_chars
, 0, sizeof(chsc_chars
));
493 general_chars
[0] = cpu_to_be32(0x03000000);
494 general_chars
[1] = cpu_to_be32(0x00059000);
496 chsc_chars
[0] = cpu_to_be32(0x40000000);
497 chsc_chars
[3] = cpu_to_be32(0x00040000);
499 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
500 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
504 res
->code
= cpu_to_be16(resp_code
);
505 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
509 #define CHSC_SDA_0_FMT 0x0f000000
510 #define CHSC_SDA_0_OC 0x0000ffff
511 #define CHSC_SDA_0_RES 0xf0ff0000
512 #define CHSC_SDA_OC_MCSSE 0x0
513 #define CHSC_SDA_OC_MSS 0x2
514 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
516 uint16_t resp_code
= 0x0001;
517 uint16_t len
= be16_to_cpu(req
->len
);
518 uint32_t param0
= be32_to_cpu(req
->param0
);
522 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
527 if (param0
& CHSC_SDA_0_FMT
) {
532 oc
= param0
& CHSC_SDA_0_OC
;
534 case CHSC_SDA_OC_MCSSE
:
535 ret
= css_enable_mcsse();
536 if (ret
== -EINVAL
) {
541 case CHSC_SDA_OC_MSS
:
542 ret
= css_enable_mss();
543 if (ret
== -EINVAL
) {
554 res
->code
= cpu_to_be16(resp_code
);
555 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
559 static int chsc_sei_nt0_get_event(void *res
)
565 static int chsc_sei_nt0_have_event(void)
571 #define CHSC_SEI_NT0 (1ULL << 63)
572 #define CHSC_SEI_NT2 (1ULL << 61)
573 static void ioinst_handle_chsc_sei(ChscReq
*req
, ChscResp
*res
)
575 uint64_t selection_mask
= ldq_p(&req
->param1
);
576 uint8_t *res_flags
= (uint8_t *)res
->data
;
580 /* regarding architecture nt0 can not be masked */
581 have_event
= !chsc_sei_nt0_get_event(res
);
582 have_more
= chsc_sei_nt0_have_event();
584 if (selection_mask
& CHSC_SEI_NT2
) {
586 have_event
= !chsc_sei_nt2_get_event(res
);
590 have_more
= chsc_sei_nt2_have_event();
595 res
->code
= cpu_to_be16(0x0001);
597 (*res_flags
) |= 0x80;
599 (*res_flags
) &= ~0x80;
602 res
->code
= cpu_to_be16(0x0004);
606 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
608 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
609 res
->code
= cpu_to_be16(0x0004);
613 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
621 hwaddr map_size
= TARGET_PAGE_SIZE
;
622 CPUS390XState
*env
= &cpu
->env
;
624 trace_ioinst("chsc");
625 reg
= (ipb
>> 20) & 0x00f;
626 addr
= env
->regs
[reg
];
629 program_interrupt(env
, PGM_SPECIFICATION
, 2);
632 req
= s390_cpu_physical_memory_map(env
, addr
, &map_size
, 1);
633 if (!req
|| map_size
!= TARGET_PAGE_SIZE
) {
634 program_interrupt(env
, PGM_ADDRESSING
, 2);
637 len
= be16_to_cpu(req
->len
);
638 /* Length field valid? */
639 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
640 program_interrupt(env
, PGM_OPERAND
, 2);
643 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
644 res
= (void *)((char *)req
+ len
);
645 command
= be16_to_cpu(req
->command
);
646 trace_ioinst_chsc_cmd(command
, len
);
649 ioinst_handle_chsc_scsc(req
, res
);
652 ioinst_handle_chsc_scpd(req
, res
);
655 ioinst_handle_chsc_sda(req
, res
);
658 ioinst_handle_chsc_sei(req
, res
);
661 ioinst_handle_chsc_unimplemented(res
);
665 setcc(cpu
, 0); /* Command execution complete */
667 s390_cpu_physical_memory_unmap(env
, req
, map_size
, 1);
670 int ioinst_handle_tpi(CPUS390XState
*env
, uint32_t ipb
)
675 hwaddr len
, orig_len
;
679 addr
= decode_basedisp_s(env
, ipb
);
681 program_interrupt(env
, PGM_SPECIFICATION
, 2);
685 lowcore
= addr
? 0 : 1;
686 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
688 int_code
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
689 if (!int_code
|| (len
!= orig_len
)) {
690 program_interrupt(env
, PGM_ADDRESSING
, 2);
694 ret
= css_do_tpi(int_code
, lowcore
);
696 s390_cpu_physical_memory_unmap(env
, int_code
, len
, 1);
700 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
701 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
702 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
703 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
705 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
711 CPUS390XState
*env
= &cpu
->env
;
713 trace_ioinst("schm");
715 if (SCHM_REG1_RES(reg1
)) {
716 program_interrupt(env
, PGM_OPERAND
, 2);
720 mbk
= SCHM_REG1_MBK(reg1
);
721 update
= SCHM_REG1_UPD(reg1
);
722 dct
= SCHM_REG1_DCT(reg1
);
724 if (update
&& (reg2
& 0x000000000000001f)) {
725 program_interrupt(env
, PGM_OPERAND
, 2);
729 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
732 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
734 int cssid
, ssid
, schid
, m
;
739 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
740 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
743 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
744 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
745 if (sch
&& css_subch_visible(sch
)) {
746 ret
= css_do_rsch(sch
);
765 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
766 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
767 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
768 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
774 CPUS390XState
*env
= &cpu
->env
;
776 if (RCHP_REG1_RES(reg1
)) {
777 program_interrupt(env
, PGM_OPERAND
, 2);
781 cssid
= RCHP_REG1_CSSID(reg1
);
782 chpid
= RCHP_REG1_CHPID(reg1
);
784 trace_ioinst_chp_id("rchp", cssid
, chpid
);
786 ret
= css_do_rchp(cssid
, chpid
);
799 /* Invalid channel subsystem. */
800 program_interrupt(env
, PGM_OPERAND
, 2);
806 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
807 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
809 /* We do not provide address limit checking, so let's suppress it. */
810 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
811 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);