2 * I/O instructions for S/390
4 * Copyright 2012 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>
18 int ioinst_disassemble_sch_ident(uint32_t value
, int *m
, int *cssid
, int *ssid
,
21 if (!IOINST_SCHID_ONE(value
)) {
24 if (!IOINST_SCHID_M(value
)) {
25 if (IOINST_SCHID_CSSID(value
)) {
31 *cssid
= IOINST_SCHID_CSSID(value
);
34 *ssid
= IOINST_SCHID_SSID(value
);
35 *schid
= IOINST_SCHID_NR(value
);
39 void ioinst_handle_xsch(S390CPU
*cpu
, uint64_t reg1
)
41 int cssid
, ssid
, schid
, m
;
46 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
47 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
50 trace_ioinst_sch_id("xsch", cssid
, ssid
, schid
);
51 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
52 if (sch
&& css_subch_visible(sch
)) {
53 ret
= css_do_xsch(sch
);
72 void ioinst_handle_csch(S390CPU
*cpu
, uint64_t reg1
)
74 int cssid
, ssid
, schid
, m
;
79 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
80 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
83 trace_ioinst_sch_id("csch", cssid
, ssid
, schid
);
84 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
85 if (sch
&& css_subch_visible(sch
)) {
86 ret
= css_do_csch(sch
);
96 void ioinst_handle_hsch(S390CPU
*cpu
, uint64_t reg1
)
98 int cssid
, ssid
, schid
, m
;
103 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
104 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
107 trace_ioinst_sch_id("hsch", cssid
, ssid
, schid
);
108 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
109 if (sch
&& css_subch_visible(sch
)) {
110 ret
= css_do_hsch(sch
);
129 static int ioinst_schib_valid(SCHIB
*schib
)
131 if ((schib
->pmcw
.flags
& PMCW_FLAGS_MASK_INVALID
) ||
132 (schib
->pmcw
.chars
& PMCW_CHARS_MASK_INVALID
)) {
135 /* Disallow extended measurements for now. */
136 if (schib
->pmcw
.chars
& PMCW_CHARS_MASK_XMWME
) {
142 void ioinst_handle_msch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
144 int cssid
, ssid
, schid
, m
;
150 hwaddr len
= sizeof(*schib
);
151 CPUS390XState
*env
= &cpu
->env
;
153 addr
= decode_basedisp_s(env
, ipb
);
155 program_interrupt(env
, PGM_SPECIFICATION
, 2);
158 schib
= s390_cpu_physical_memory_map(env
, addr
, &len
, 0);
159 if (!schib
|| len
!= sizeof(*schib
)) {
160 program_interrupt(env
, PGM_ADDRESSING
, 2);
163 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
164 !ioinst_schib_valid(schib
)) {
165 program_interrupt(env
, PGM_OPERAND
, 2);
168 trace_ioinst_sch_id("msch", cssid
, ssid
, schid
);
169 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
170 if (sch
&& css_subch_visible(sch
)) {
171 ret
= css_do_msch(sch
, schib
);
190 s390_cpu_physical_memory_unmap(env
, schib
, len
, 0);
193 static void copy_orb_from_guest(ORB
*dest
, const ORB
*src
)
195 dest
->intparm
= be32_to_cpu(src
->intparm
);
196 dest
->ctrl0
= be16_to_cpu(src
->ctrl0
);
197 dest
->lpm
= src
->lpm
;
198 dest
->ctrl1
= src
->ctrl1
;
199 dest
->cpa
= be32_to_cpu(src
->cpa
);
202 static int ioinst_orb_valid(ORB
*orb
)
204 if ((orb
->ctrl0
& ORB_CTRL0_MASK_INVALID
) ||
205 (orb
->ctrl1
& ORB_CTRL1_MASK_INVALID
)) {
208 if ((orb
->cpa
& HIGH_ORDER_BIT
) != 0) {
214 void ioinst_handle_ssch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
216 int cssid
, ssid
, schid
, m
;
222 hwaddr len
= sizeof(*orig_orb
);
223 CPUS390XState
*env
= &cpu
->env
;
225 addr
= decode_basedisp_s(env
, ipb
);
227 program_interrupt(env
, PGM_SPECIFICATION
, 2);
230 orig_orb
= s390_cpu_physical_memory_map(env
, addr
, &len
, 0);
231 if (!orig_orb
|| len
!= sizeof(*orig_orb
)) {
232 program_interrupt(env
, PGM_ADDRESSING
, 2);
235 copy_orb_from_guest(&orb
, orig_orb
);
236 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
) ||
237 !ioinst_orb_valid(&orb
)) {
238 program_interrupt(env
, PGM_OPERAND
, 2);
241 trace_ioinst_sch_id("ssch", cssid
, ssid
, schid
);
242 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
243 if (sch
&& css_subch_visible(sch
)) {
244 ret
= css_do_ssch(sch
, &orb
);
263 s390_cpu_physical_memory_unmap(env
, orig_orb
, len
, 0);
266 void ioinst_handle_stcrw(S390CPU
*cpu
, uint32_t ipb
)
271 hwaddr len
= sizeof(*crw
);
272 CPUS390XState
*env
= &cpu
->env
;
274 addr
= decode_basedisp_s(env
, ipb
);
276 program_interrupt(env
, PGM_SPECIFICATION
, 2);
279 crw
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
280 if (!crw
|| len
!= sizeof(*crw
)) {
281 program_interrupt(env
, PGM_ADDRESSING
, 2);
284 cc
= css_do_stcrw(crw
);
285 /* 0 - crw stored, 1 - zeroes stored */
289 s390_cpu_physical_memory_unmap(env
, crw
, len
, 1);
292 void ioinst_handle_stsch(S390CPU
*cpu
, uint64_t reg1
, uint32_t ipb
)
294 int cssid
, ssid
, schid
, m
;
299 hwaddr len
= sizeof(*schib
);
300 CPUS390XState
*env
= &cpu
->env
;
302 addr
= decode_basedisp_s(env
, ipb
);
304 program_interrupt(env
, PGM_SPECIFICATION
, 2);
307 schib
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
308 if (!schib
|| len
!= sizeof(*schib
)) {
309 program_interrupt(env
, PGM_ADDRESSING
, 2);
313 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
314 program_interrupt(env
, PGM_OPERAND
, 2);
317 trace_ioinst_sch_id("stsch", cssid
, ssid
, schid
);
318 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
320 if (css_subch_visible(sch
)) {
321 css_do_stsch(sch
, schib
);
324 /* Indicate no more subchannels in this css/ss */
328 if (css_schid_final(m
, cssid
, ssid
, schid
)) {
329 cc
= 3; /* No more subchannels in this css/ss */
331 /* Store an empty schib. */
332 memset(schib
, 0, sizeof(*schib
));
339 s390_cpu_physical_memory_unmap(env
, schib
, len
, 1);
342 int ioinst_handle_tsch(CPUS390XState
*env
, uint64_t reg1
, uint32_t ipb
)
344 int cssid
, ssid
, schid
, m
;
350 hwaddr len
= sizeof(*irb
);
352 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
353 program_interrupt(env
, PGM_OPERAND
, 2);
356 trace_ioinst_sch_id("tsch", cssid
, ssid
, schid
);
357 addr
= decode_basedisp_s(env
, ipb
);
359 program_interrupt(env
, PGM_SPECIFICATION
, 2);
362 irb
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
363 if (!irb
|| len
!= sizeof(*irb
)) {
364 program_interrupt(env
, PGM_ADDRESSING
, 2);
368 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
369 if (sch
&& css_subch_visible(sch
)) {
370 ret
= css_do_tsch(sch
, irb
);
371 /* 0 - status pending, 1 - not status pending */
377 s390_cpu_physical_memory_unmap(env
, irb
, sizeof(*irb
), 1);
381 typedef struct ChscReq
{
387 } QEMU_PACKED ChscReq
;
389 typedef struct ChscResp
{
394 } QEMU_PACKED ChscResp
;
396 #define CHSC_MIN_RESP_LEN 0x0008
398 #define CHSC_SCPD 0x0002
399 #define CHSC_SCSC 0x0010
400 #define CHSC_SDA 0x0031
402 #define CHSC_SCPD_0_M 0x20000000
403 #define CHSC_SCPD_0_C 0x10000000
404 #define CHSC_SCPD_0_FMT 0x0f000000
405 #define CHSC_SCPD_0_CSSID 0x00ff0000
406 #define CHSC_SCPD_0_RFMT 0x00000f00
407 #define CHSC_SCPD_0_RES 0xc000f000
408 #define CHSC_SCPD_1_RES 0xffffff00
409 #define CHSC_SCPD_01_CHPID 0x000000ff
410 static void ioinst_handle_chsc_scpd(ChscReq
*req
, ChscResp
*res
)
412 uint16_t len
= be16_to_cpu(req
->len
);
413 uint32_t param0
= be32_to_cpu(req
->param0
);
414 uint32_t param1
= be32_to_cpu(req
->param1
);
418 uint8_t f_chpid
, l_chpid
;
422 rfmt
= (param0
& CHSC_SCPD_0_RFMT
) >> 8;
423 if ((rfmt
== 0) || (rfmt
== 1)) {
424 rfmt
= !!(param0
& CHSC_SCPD_0_C
);
426 if ((len
!= 0x0010) || (param0
& CHSC_SCPD_0_RES
) ||
427 (param1
& CHSC_SCPD_1_RES
) || req
->param2
) {
431 if (param0
& CHSC_SCPD_0_FMT
) {
435 cssid
= (param0
& CHSC_SCPD_0_CSSID
) >> 16;
436 m
= param0
& CHSC_SCPD_0_M
;
438 if (!m
|| !css_present(cssid
)) {
443 f_chpid
= param0
& CHSC_SCPD_01_CHPID
;
444 l_chpid
= param1
& CHSC_SCPD_01_CHPID
;
445 if (l_chpid
< f_chpid
) {
449 /* css_collect_chp_desc() is endian-aware */
450 desc_size
= css_collect_chp_desc(m
, cssid
, f_chpid
, l_chpid
, rfmt
,
452 res
->code
= cpu_to_be16(0x0001);
453 res
->len
= cpu_to_be16(8 + desc_size
);
454 res
->param
= cpu_to_be32(rfmt
);
458 res
->code
= cpu_to_be16(resp_code
);
459 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
460 res
->param
= cpu_to_be32(rfmt
);
463 #define CHSC_SCSC_0_M 0x20000000
464 #define CHSC_SCSC_0_FMT 0x000f0000
465 #define CHSC_SCSC_0_CSSID 0x0000ff00
466 #define CHSC_SCSC_0_RES 0xdff000ff
467 static void ioinst_handle_chsc_scsc(ChscReq
*req
, ChscResp
*res
)
469 uint16_t len
= be16_to_cpu(req
->len
);
470 uint32_t param0
= be32_to_cpu(req
->param0
);
473 uint32_t general_chars
[510];
474 uint32_t chsc_chars
[508];
481 if (param0
& CHSC_SCSC_0_FMT
) {
485 cssid
= (param0
& CHSC_SCSC_0_CSSID
) >> 8;
487 if (!(param0
& CHSC_SCSC_0_M
) || !css_present(cssid
)) {
492 if ((param0
& CHSC_SCSC_0_RES
) || req
->param1
|| req
->param2
) {
496 res
->code
= cpu_to_be16(0x0001);
497 res
->len
= cpu_to_be16(4080);
500 memset(general_chars
, 0, sizeof(general_chars
));
501 memset(chsc_chars
, 0, sizeof(chsc_chars
));
503 general_chars
[0] = cpu_to_be32(0x03000000);
504 general_chars
[1] = cpu_to_be32(0x00059000);
506 chsc_chars
[0] = cpu_to_be32(0x40000000);
507 chsc_chars
[3] = cpu_to_be32(0x00040000);
509 memcpy(res
->data
, general_chars
, sizeof(general_chars
));
510 memcpy(res
->data
+ sizeof(general_chars
), chsc_chars
, sizeof(chsc_chars
));
514 res
->code
= cpu_to_be16(resp_code
);
515 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
519 #define CHSC_SDA_0_FMT 0x0f000000
520 #define CHSC_SDA_0_OC 0x0000ffff
521 #define CHSC_SDA_0_RES 0xf0ff0000
522 #define CHSC_SDA_OC_MCSSE 0x0
523 #define CHSC_SDA_OC_MSS 0x2
524 static void ioinst_handle_chsc_sda(ChscReq
*req
, ChscResp
*res
)
526 uint16_t resp_code
= 0x0001;
527 uint16_t len
= be16_to_cpu(req
->len
);
528 uint32_t param0
= be32_to_cpu(req
->param0
);
532 if ((len
!= 0x0400) || (param0
& CHSC_SDA_0_RES
)) {
537 if (param0
& CHSC_SDA_0_FMT
) {
542 oc
= param0
& CHSC_SDA_0_OC
;
544 case CHSC_SDA_OC_MCSSE
:
545 ret
= css_enable_mcsse();
546 if (ret
== -EINVAL
) {
551 case CHSC_SDA_OC_MSS
:
552 ret
= css_enable_mss();
553 if (ret
== -EINVAL
) {
564 res
->code
= cpu_to_be16(resp_code
);
565 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
569 static void ioinst_handle_chsc_unimplemented(ChscResp
*res
)
571 res
->len
= cpu_to_be16(CHSC_MIN_RESP_LEN
);
572 res
->code
= cpu_to_be16(0x0004);
576 void ioinst_handle_chsc(S390CPU
*cpu
, uint32_t ipb
)
584 hwaddr map_size
= TARGET_PAGE_SIZE
;
585 CPUS390XState
*env
= &cpu
->env
;
587 trace_ioinst("chsc");
588 reg
= (ipb
>> 20) & 0x00f;
589 addr
= env
->regs
[reg
];
592 program_interrupt(env
, PGM_SPECIFICATION
, 2);
595 req
= s390_cpu_physical_memory_map(env
, addr
, &map_size
, 1);
596 if (!req
|| map_size
!= TARGET_PAGE_SIZE
) {
597 program_interrupt(env
, PGM_ADDRESSING
, 2);
600 len
= be16_to_cpu(req
->len
);
601 /* Length field valid? */
602 if ((len
< 16) || (len
> 4088) || (len
& 7)) {
603 program_interrupt(env
, PGM_OPERAND
, 2);
606 memset((char *)req
+ len
, 0, TARGET_PAGE_SIZE
- len
);
607 res
= (void *)((char *)req
+ len
);
608 command
= be16_to_cpu(req
->command
);
609 trace_ioinst_chsc_cmd(command
, len
);
612 ioinst_handle_chsc_scsc(req
, res
);
615 ioinst_handle_chsc_scpd(req
, res
);
618 ioinst_handle_chsc_sda(req
, res
);
621 ioinst_handle_chsc_unimplemented(res
);
626 s390_cpu_physical_memory_unmap(env
, req
, map_size
, 1);
629 int ioinst_handle_tpi(CPUS390XState
*env
, uint32_t ipb
)
634 hwaddr len
, orig_len
;
638 addr
= decode_basedisp_s(env
, ipb
);
640 program_interrupt(env
, PGM_SPECIFICATION
, 2);
644 lowcore
= addr
? 0 : 1;
645 len
= lowcore
? 8 /* two words */ : 12 /* three words */;
647 int_code
= s390_cpu_physical_memory_map(env
, addr
, &len
, 1);
648 if (!int_code
|| (len
!= orig_len
)) {
649 program_interrupt(env
, PGM_ADDRESSING
, 2);
653 ret
= css_do_tpi(int_code
, lowcore
);
655 s390_cpu_physical_memory_unmap(env
, int_code
, len
, 1);
659 #define SCHM_REG1_RES(_reg) (_reg & 0x000000000ffffffc)
660 #define SCHM_REG1_MBK(_reg) ((_reg & 0x00000000f0000000) >> 28)
661 #define SCHM_REG1_UPD(_reg) ((_reg & 0x0000000000000002) >> 1)
662 #define SCHM_REG1_DCT(_reg) (_reg & 0x0000000000000001)
664 void ioinst_handle_schm(S390CPU
*cpu
, uint64_t reg1
, uint64_t reg2
,
670 CPUS390XState
*env
= &cpu
->env
;
672 trace_ioinst("schm");
674 if (SCHM_REG1_RES(reg1
)) {
675 program_interrupt(env
, PGM_OPERAND
, 2);
679 mbk
= SCHM_REG1_MBK(reg1
);
680 update
= SCHM_REG1_UPD(reg1
);
681 dct
= SCHM_REG1_DCT(reg1
);
683 if (update
&& (reg2
& 0x000000000000001f)) {
684 program_interrupt(env
, PGM_OPERAND
, 2);
688 css_do_schm(mbk
, update
, dct
, update
? reg2
: 0);
691 void ioinst_handle_rsch(S390CPU
*cpu
, uint64_t reg1
)
693 int cssid
, ssid
, schid
, m
;
698 if (ioinst_disassemble_sch_ident(reg1
, &m
, &cssid
, &ssid
, &schid
)) {
699 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);
702 trace_ioinst_sch_id("rsch", cssid
, ssid
, schid
);
703 sch
= css_find_subch(m
, cssid
, ssid
, schid
);
704 if (sch
&& css_subch_visible(sch
)) {
705 ret
= css_do_rsch(sch
);
724 #define RCHP_REG1_RES(_reg) (_reg & 0x00000000ff00ff00)
725 #define RCHP_REG1_CSSID(_reg) ((_reg & 0x0000000000ff0000) >> 16)
726 #define RCHP_REG1_CHPID(_reg) (_reg & 0x00000000000000ff)
727 void ioinst_handle_rchp(S390CPU
*cpu
, uint64_t reg1
)
733 CPUS390XState
*env
= &cpu
->env
;
735 if (RCHP_REG1_RES(reg1
)) {
736 program_interrupt(env
, PGM_OPERAND
, 2);
740 cssid
= RCHP_REG1_CSSID(reg1
);
741 chpid
= RCHP_REG1_CHPID(reg1
);
743 trace_ioinst_chp_id("rchp", cssid
, chpid
);
745 ret
= css_do_rchp(cssid
, chpid
);
758 /* Invalid channel subsystem. */
759 program_interrupt(env
, PGM_OPERAND
, 2);
765 #define SAL_REG1_INVALID(_reg) (_reg & 0x0000000080000000)
766 void ioinst_handle_sal(S390CPU
*cpu
, uint64_t reg1
)
768 /* We do not provide address limit checking, so let's suppress it. */
769 if (SAL_REG1_INVALID(reg1
) || reg1
& 0x000000000000ffff) {
770 program_interrupt(&cpu
->env
, PGM_OPERAND
, 2);