xref: /linux/arch/s390/include/asm/ap.h (revision d4c53ae8e4948f7f733f24cd863da31c8e9379a7)
19fa1db4cSMartin Schwidefsky /* SPDX-License-Identifier: GPL-2.0 */
2e7fc5146STony Krowiak /*
3e7fc5146STony Krowiak  * Adjunct processor (AP) interfaces
4e7fc5146STony Krowiak  *
5e7fc5146STony Krowiak  * Copyright IBM Corp. 2017
6e7fc5146STony Krowiak  *
7e7fc5146STony Krowiak  * Author(s): Tony Krowiak <akrowia@linux.vnet.ibm.com>
8e7fc5146STony Krowiak  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
9e7fc5146STony Krowiak  *	      Harald Freudenberger <freude@de.ibm.com>
10e7fc5146STony Krowiak  */
11e7fc5146STony Krowiak 
12e7fc5146STony Krowiak #ifndef _ASM_S390_AP_H_
13e7fc5146STony Krowiak #define _ASM_S390_AP_H_
14e7fc5146STony Krowiak 
157a334a28SHeiko Carstens #include <linux/io.h>
16d09a307fSHeiko Carstens #include <asm/asm-extable.h>
177a334a28SHeiko Carstens 
18e7fc5146STony Krowiak /**
19e7fc5146STony Krowiak  * The ap_qid_t identifier of an ap queue.
20e7fc5146STony Krowiak  * If the AP facilities test (APFT) facility is available,
21e7fc5146STony Krowiak  * card and queue index are 8 bit values, otherwise
22e7fc5146STony Krowiak  * card index is 6 bit and queue index a 4 bit value.
23e7fc5146STony Krowiak  */
24e7fc5146STony Krowiak typedef unsigned int ap_qid_t;
25e7fc5146STony Krowiak 
26af4a7227SHarald Freudenberger #define AP_MKQID(_card, _queue) (((_card) & 0xff) << 8 | ((_queue) & 0xff))
27af4a7227SHarald Freudenberger #define AP_QID_CARD(_qid) (((_qid) >> 8) & 0xff)
28af4a7227SHarald Freudenberger #define AP_QID_QUEUE(_qid) ((_qid) & 0xff)
29e7fc5146STony Krowiak 
30e7fc5146STony Krowiak /**
31e7fc5146STony Krowiak  * struct ap_queue_status - Holds the AP queue status.
32e7fc5146STony Krowiak  * @queue_empty: Shows if queue is empty
33e7fc5146STony Krowiak  * @replies_waiting: Waiting replies
34e7fc5146STony Krowiak  * @queue_full: Is 1 if the queue is full
35e7fc5146STony Krowiak  * @irq_enabled: Shows if interrupts are enabled for the AP
36e7fc5146STony Krowiak  * @response_code: Holds the 8 bit response code
37e7fc5146STony Krowiak  *
38e7fc5146STony Krowiak  * The ap queue status word is returned by all three AP functions
39e7fc5146STony Krowiak  * (PQAP, NQAP and DQAP).  There's a set of flags in the first
40e7fc5146STony Krowiak  * byte, followed by a 1 byte response code.
41e7fc5146STony Krowiak  */
42e7fc5146STony Krowiak struct ap_queue_status {
43e7fc5146STony Krowiak 	unsigned int queue_empty	: 1;
44e7fc5146STony Krowiak 	unsigned int replies_waiting	: 1;
45e7fc5146STony Krowiak 	unsigned int queue_full		: 1;
462d72eaf0SHarald Freudenberger 	unsigned int			: 3;
472d72eaf0SHarald Freudenberger 	unsigned int async		: 1;
48e7fc5146STony Krowiak 	unsigned int irq_enabled	: 1;
49e7fc5146STony Krowiak 	unsigned int response_code	: 8;
502d72eaf0SHarald Freudenberger 	unsigned int			: 16;
51e7fc5146STony Krowiak };
52e7fc5146STony Krowiak 
53ebf95e88SHarald Freudenberger /*
54ebf95e88SHarald Freudenberger  * AP queue status reg union to access the reg1
55ebf95e88SHarald Freudenberger  * register with the lower 32 bits comprising the
56ebf95e88SHarald Freudenberger  * ap queue status.
57ebf95e88SHarald Freudenberger  */
58ebf95e88SHarald Freudenberger union ap_queue_status_reg {
59ebf95e88SHarald Freudenberger 	unsigned long value;
60ebf95e88SHarald Freudenberger 	struct {
61ebf95e88SHarald Freudenberger 		u32 _pad;
62ebf95e88SHarald Freudenberger 		struct ap_queue_status status;
63ebf95e88SHarald Freudenberger 	};
64ebf95e88SHarald Freudenberger };
65ebf95e88SHarald Freudenberger 
66e7fc5146STony Krowiak /**
67f1b0a434SHarald Freudenberger  * ap_intructions_available() - Test if AP instructions are available.
68f1b0a434SHarald Freudenberger  *
699b97e9f5SHarald Freudenberger  * Returns true if the AP instructions are installed, otherwise false.
70f1b0a434SHarald Freudenberger  */
719b97e9f5SHarald Freudenberger static inline bool ap_instructions_available(void)
72f1b0a434SHarald Freudenberger {
73b9639b31SHeiko Carstens 	unsigned long reg0 = AP_MKQID(0, 0);
74b9639b31SHeiko Carstens 	unsigned long reg1 = 0;
75f1b0a434SHarald Freudenberger 
76f1b0a434SHarald Freudenberger 	asm volatile(
77b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"		/* qid into gr0 */
78b9639b31SHeiko Carstens 		"	lghi	1,0\n"			/* 0 into gr1 */
79b9639b31SHeiko Carstens 		"	lghi	2,0\n"			/* 0 into gr2 */
802d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(TAPQ) */
81b9639b31SHeiko Carstens 		"0:	la	%[reg1],1\n"		/* 1 into reg1 */
82f1b0a434SHarald Freudenberger 		"1:\n"
83f1b0a434SHarald Freudenberger 		EX_TABLE(0b, 1b)
84b9639b31SHeiko Carstens 		: [reg1] "+&d" (reg1)
85b9639b31SHeiko Carstens 		: [reg0] "d" (reg0)
86b9639b31SHeiko Carstens 		: "cc", "0", "1", "2");
879b97e9f5SHarald Freudenberger 	return reg1 != 0;
88f1b0a434SHarald Freudenberger }
89f1b0a434SHarald Freudenberger 
90211c06d8SHarald Freudenberger /* TAPQ register GR2 response struct */
91*d4c53ae8SHarald Freudenberger struct ap_tapq_hwinfo {
92211c06d8SHarald Freudenberger 	union {
93211c06d8SHarald Freudenberger 		unsigned long value;
94211c06d8SHarald Freudenberger 		struct {
95211c06d8SHarald Freudenberger 			unsigned int fac    : 32; /* facility bits */
96211c06d8SHarald Freudenberger 			unsigned int apinfo : 32; /* ap type, ... */
97211c06d8SHarald Freudenberger 		};
98211c06d8SHarald Freudenberger 		struct {
99*d4c53ae8SHarald Freudenberger 			unsigned int apsc  :  1; /* APSC */
100*d4c53ae8SHarald Freudenberger 			unsigned int mex4k :  1; /* AP4KM */
101*d4c53ae8SHarald Freudenberger 			unsigned int crt4k :  1; /* AP4KC */
102*d4c53ae8SHarald Freudenberger 			unsigned int cca   :  1; /* D */
103*d4c53ae8SHarald Freudenberger 			unsigned int accel :  1; /* A */
104*d4c53ae8SHarald Freudenberger 			unsigned int ep11  :  1; /* X */
105*d4c53ae8SHarald Freudenberger 			unsigned int apxa  :  1; /* APXA */
106211c06d8SHarald Freudenberger 			unsigned int	   :  1;
107211c06d8SHarald Freudenberger 			unsigned int class :  8;
108211c06d8SHarald Freudenberger 			unsigned int bs	   :  2; /* SE bind/assoc */
109211c06d8SHarald Freudenberger 			unsigned int	   : 14;
110211c06d8SHarald Freudenberger 			unsigned int at	   :  8; /* ap type */
111211c06d8SHarald Freudenberger 			unsigned int nd	   :  8; /* nr of domains */
112211c06d8SHarald Freudenberger 			unsigned int	   :  4;
113211c06d8SHarald Freudenberger 			unsigned int ml	   :  4; /* apxl ml */
114211c06d8SHarald Freudenberger 			unsigned int	   :  4;
115211c06d8SHarald Freudenberger 			unsigned int qd	   :  4; /* queue depth */
116211c06d8SHarald Freudenberger 		};
117211c06d8SHarald Freudenberger 	};
118211c06d8SHarald Freudenberger };
119211c06d8SHarald Freudenberger 
1202d72eaf0SHarald Freudenberger /*
1212d72eaf0SHarald Freudenberger  * Convenience defines to be used with the bs field from struct ap_tapq_gr2
1222d72eaf0SHarald Freudenberger  */
1232d72eaf0SHarald Freudenberger #define AP_BS_Q_USABLE		      0
1242d72eaf0SHarald Freudenberger #define AP_BS_Q_USABLE_NO_SECURE_KEY  1
1252d72eaf0SHarald Freudenberger #define AP_BS_Q_AVAIL_FOR_BINDING     2
1262d72eaf0SHarald Freudenberger #define AP_BS_Q_UNUSABLE	      3
1272d72eaf0SHarald Freudenberger 
128f1b0a434SHarald Freudenberger /**
129f1b0a434SHarald Freudenberger  * ap_tapq(): Test adjunct processor queue.
130f1b0a434SHarald Freudenberger  * @qid: The AP queue number
131*d4c53ae8SHarald Freudenberger  * @info: Pointer to tapq hwinfo struct
132f1b0a434SHarald Freudenberger  *
133f1b0a434SHarald Freudenberger  * Returns AP queue status structure.
134f1b0a434SHarald Freudenberger  */
135*d4c53ae8SHarald Freudenberger static inline struct ap_queue_status ap_tapq(ap_qid_t qid,
136*d4c53ae8SHarald Freudenberger 					     struct ap_tapq_hwinfo *info)
137f1b0a434SHarald Freudenberger {
138ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
139b9639b31SHeiko Carstens 	unsigned long reg2;
140f1b0a434SHarald Freudenberger 
141b9639b31SHeiko Carstens 	asm volatile(
142b9639b31SHeiko Carstens 		"	lgr	0,%[qid]\n"		/* qid into gr0 */
143b9639b31SHeiko Carstens 		"	lghi	2,0\n"			/* 0 into gr2 */
1442d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(TAPQ) */
145b9639b31SHeiko Carstens 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
146b9639b31SHeiko Carstens 		"	lgr	%[reg2],2\n"		/* gr2 into reg2 */
147ebf95e88SHarald Freudenberger 		: [reg1] "=&d" (reg1.value), [reg2] "=&d" (reg2)
148b9639b31SHeiko Carstens 		: [qid] "d" (qid)
149b9639b31SHeiko Carstens 		: "cc", "0", "1", "2");
150f1b0a434SHarald Freudenberger 	if (info)
151211c06d8SHarald Freudenberger 		info->value = reg2;
152ebf95e88SHarald Freudenberger 	return reg1.status;
153f1b0a434SHarald Freudenberger }
154f1b0a434SHarald Freudenberger 
155f1b0a434SHarald Freudenberger /**
156e7fc5146STony Krowiak  * ap_test_queue(): Test adjunct processor queue.
157e7fc5146STony Krowiak  * @qid: The AP queue number
158e7fc5146STony Krowiak  * @tbit: Test facilities bit
159211c06d8SHarald Freudenberger  * @info: Ptr to tapq gr2 struct
160e7fc5146STony Krowiak  *
161e7fc5146STony Krowiak  * Returns AP queue status structure.
162e7fc5146STony Krowiak  */
163211c06d8SHarald Freudenberger static inline struct ap_queue_status ap_test_queue(ap_qid_t qid, int tbit,
164*d4c53ae8SHarald Freudenberger 						   struct ap_tapq_hwinfo *info)
165f1b0a434SHarald Freudenberger {
166f1b0a434SHarald Freudenberger 	if (tbit)
167f1b0a434SHarald Freudenberger 		qid |= 1UL << 23; /* set T bit*/
168f1b0a434SHarald Freudenberger 	return ap_tapq(qid, info);
169f1b0a434SHarald Freudenberger }
170e7fc5146STony Krowiak 
171f1b0a434SHarald Freudenberger /**
172f1b0a434SHarald Freudenberger  * ap_pqap_rapq(): Reset adjunct processor queue.
173f1b0a434SHarald Freudenberger  * @qid: The AP queue number
1744bdf3c39SHarald Freudenberger  * @fbit: if != 0 set F bit
175f1b0a434SHarald Freudenberger  *
176f1b0a434SHarald Freudenberger  * Returns AP queue status structure.
177f1b0a434SHarald Freudenberger  */
1784bdf3c39SHarald Freudenberger static inline struct ap_queue_status ap_rapq(ap_qid_t qid, int fbit)
179f1b0a434SHarald Freudenberger {
180b9639b31SHeiko Carstens 	unsigned long reg0 = qid | (1UL << 24);  /* fc 1UL is RAPQ */
181ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
182f1b0a434SHarald Freudenberger 
1834bdf3c39SHarald Freudenberger 	if (fbit)
1844bdf3c39SHarald Freudenberger 		reg0 |= 1UL << 22;
1854bdf3c39SHarald Freudenberger 
186f1b0a434SHarald Freudenberger 	asm volatile(
187b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"		/* qid arg into gr0 */
1882d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(RAPQ) */
189b9639b31SHeiko Carstens 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
190ebf95e88SHarald Freudenberger 		: [reg1] "=&d" (reg1.value)
191b9639b31SHeiko Carstens 		: [reg0] "d" (reg0)
192b9639b31SHeiko Carstens 		: "cc", "0", "1");
193ebf95e88SHarald Freudenberger 	return reg1.status;
194f1b0a434SHarald Freudenberger }
195f1b0a434SHarald Freudenberger 
196f1b0a434SHarald Freudenberger /**
197f1b0a434SHarald Freudenberger  * ap_pqap_zapq(): Reset and zeroize adjunct processor queue.
198f1b0a434SHarald Freudenberger  * @qid: The AP queue number
1994bdf3c39SHarald Freudenberger  * @fbit: if != 0 set F bit
200f1b0a434SHarald Freudenberger  *
201f1b0a434SHarald Freudenberger  * Returns AP queue status structure.
202f1b0a434SHarald Freudenberger  */
2034bdf3c39SHarald Freudenberger static inline struct ap_queue_status ap_zapq(ap_qid_t qid, int fbit)
204f1b0a434SHarald Freudenberger {
205b9639b31SHeiko Carstens 	unsigned long reg0 = qid | (2UL << 24);  /* fc 2UL is ZAPQ */
206ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
207f1b0a434SHarald Freudenberger 
2084bdf3c39SHarald Freudenberger 	if (fbit)
2094bdf3c39SHarald Freudenberger 		reg0 |= 1UL << 22;
2104bdf3c39SHarald Freudenberger 
211f1b0a434SHarald Freudenberger 	asm volatile(
212b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"		/* qid arg into gr0 */
2132d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(ZAPQ) */
214b9639b31SHeiko Carstens 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
215ebf95e88SHarald Freudenberger 		: [reg1] "=&d" (reg1.value)
216b9639b31SHeiko Carstens 		: [reg0] "d" (reg0)
217b9639b31SHeiko Carstens 		: "cc", "0", "1");
218ebf95e88SHarald Freudenberger 	return reg1.status;
219f1b0a434SHarald Freudenberger }
220f1b0a434SHarald Freudenberger 
221f1b0a434SHarald Freudenberger /**
222f1b0a434SHarald Freudenberger  * struct ap_config_info - convenience struct for AP crypto
223f1b0a434SHarald Freudenberger  * config info as returned by the ap_qci() function.
224f1b0a434SHarald Freudenberger  */
225050349b5SHarald Freudenberger struct ap_config_info {
226050349b5SHarald Freudenberger 	unsigned int apsc	 : 1;	/* S bit */
227050349b5SHarald Freudenberger 	unsigned int apxa	 : 1;	/* N bit */
228050349b5SHarald Freudenberger 	unsigned int qact	 : 1;	/* C bit */
229050349b5SHarald Freudenberger 	unsigned int rc8a	 : 1;	/* R bit */
230f6047040SHarald Freudenberger 	unsigned int		 : 4;
231f6047040SHarald Freudenberger 	unsigned int apsb	 : 1;	/* B bit */
232f6047040SHarald Freudenberger 	unsigned int		 : 23;
233f6047040SHarald Freudenberger 	unsigned char na;		/* max # of APs - 1 */
234f6047040SHarald Freudenberger 	unsigned char nd;		/* max # of Domains - 1 */
235f6047040SHarald Freudenberger 	unsigned char _reserved0[10];
236050349b5SHarald Freudenberger 	unsigned int apm[8];		/* AP ID mask */
2377379e652SHarald Freudenberger 	unsigned int aqm[8];		/* AP (usage) queue mask */
2387379e652SHarald Freudenberger 	unsigned int adm[8];		/* AP (control) domain mask */
239f6047040SHarald Freudenberger 	unsigned char _reserved1[16];
240050349b5SHarald Freudenberger } __aligned(8);
241050349b5SHarald Freudenberger 
242f1b0a434SHarald Freudenberger /**
243f1b0a434SHarald Freudenberger  * ap_qci(): Get AP configuration data
244050349b5SHarald Freudenberger  *
245f1b0a434SHarald Freudenberger  * Returns 0 on success, or -EOPNOTSUPP.
246050349b5SHarald Freudenberger  */
247f1b0a434SHarald Freudenberger static inline int ap_qci(struct ap_config_info *config)
248f1b0a434SHarald Freudenberger {
249b9639b31SHeiko Carstens 	unsigned long reg0 = 4UL << 24;  /* fc 4UL is QCI */
250b9639b31SHeiko Carstens 	unsigned long reg1 = -EOPNOTSUPP;
251b9639b31SHeiko Carstens 	struct ap_config_info *reg2 = config;
252f1b0a434SHarald Freudenberger 
253f1b0a434SHarald Freudenberger 	asm volatile(
254b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"		/* QCI fc into gr0 */
255b9639b31SHeiko Carstens 		"	lgr	2,%[reg2]\n"		/* ptr to config into gr2 */
2562d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(QCI) */
257b9639b31SHeiko Carstens 		"0:	la	%[reg1],0\n"		/* good case, QCI fc available */
258f1b0a434SHarald Freudenberger 		"1:\n"
259f1b0a434SHarald Freudenberger 		EX_TABLE(0b, 1b)
260b9639b31SHeiko Carstens 		: [reg1] "+&d" (reg1)
261b9639b31SHeiko Carstens 		: [reg0] "d" (reg0), [reg2] "d" (reg2)
262b9639b31SHeiko Carstens 		: "cc", "memory", "0", "2");
263f1b0a434SHarald Freudenberger 
264f1b0a434SHarald Freudenberger 	return reg1;
265f1b0a434SHarald Freudenberger }
266050349b5SHarald Freudenberger 
26746fde9a9SHarald Freudenberger /*
26846fde9a9SHarald Freudenberger  * struct ap_qirq_ctrl - convenient struct for easy invocation
269f1b0a434SHarald Freudenberger  * of the ap_aqic() function. This struct is passed as GR1
270f1b0a434SHarald Freudenberger  * parameter to the PQAP(AQIC) instruction. For details please
271f1b0a434SHarald Freudenberger  * see the AR documentation.
27246fde9a9SHarald Freudenberger  */
273ebf95e88SHarald Freudenberger union ap_qirq_ctrl {
274ebf95e88SHarald Freudenberger 	unsigned long value;
275ebf95e88SHarald Freudenberger 	struct {
276ebf95e88SHarald Freudenberger 		unsigned int	   : 8;
27746fde9a9SHarald Freudenberger 		unsigned int zone  : 8;	/* zone info */
27846fde9a9SHarald Freudenberger 		unsigned int ir	   : 1;	/* ir flag: enable (1) or disable (0) irq */
279ebf95e88SHarald Freudenberger 		unsigned int	   : 4;
28046fde9a9SHarald Freudenberger 		unsigned int gisc  : 3;	/* guest isc field */
281ebf95e88SHarald Freudenberger 		unsigned int	   : 6;
28246fde9a9SHarald Freudenberger 		unsigned int gf	   : 2;	/* gisa format */
283ebf95e88SHarald Freudenberger 		unsigned int	   : 1;
28446fde9a9SHarald Freudenberger 		unsigned int gisa  : 27;	/* gisa origin */
285ebf95e88SHarald Freudenberger 		unsigned int	   : 1;
28646fde9a9SHarald Freudenberger 		unsigned int isc   : 3;	/* irq sub class */
28746fde9a9SHarald Freudenberger 	};
288ebf95e88SHarald Freudenberger };
28946fde9a9SHarald Freudenberger 
29046fde9a9SHarald Freudenberger /**
291f1b0a434SHarald Freudenberger  * ap_aqic(): Control interruption for a specific AP.
29246fde9a9SHarald Freudenberger  * @qid: The AP queue number
293f1b0a434SHarald Freudenberger  * @qirqctrl: struct ap_qirq_ctrl (64 bit value)
29410e19d49SNicolin Chen  * @pa_ind: Physical address of the notification indicator byte
29546fde9a9SHarald Freudenberger  *
29646fde9a9SHarald Freudenberger  * Returns AP queue status.
29746fde9a9SHarald Freudenberger  */
298f1b0a434SHarald Freudenberger static inline struct ap_queue_status ap_aqic(ap_qid_t qid,
299ebf95e88SHarald Freudenberger 					     union ap_qirq_ctrl qirqctrl,
30010e19d49SNicolin Chen 					     phys_addr_t pa_ind)
301f1b0a434SHarald Freudenberger {
302b9639b31SHeiko Carstens 	unsigned long reg0 = qid | (3UL << 24);  /* fc 3UL is AQIC */
303ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
30410e19d49SNicolin Chen 	unsigned long reg2 = pa_ind;
305f1b0a434SHarald Freudenberger 
306ebf95e88SHarald Freudenberger 	reg1.value = qirqctrl.value;
307159491f3SHarald Freudenberger 
308f1b0a434SHarald Freudenberger 	asm volatile(
309b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"		/* qid param into gr0 */
310b9639b31SHeiko Carstens 		"	lgr	1,%[reg1]\n"		/* irq ctrl into gr1 */
311b9639b31SHeiko Carstens 		"	lgr	2,%[reg2]\n"		/* ni addr into gr2 */
3122d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(AQIC) */
313b9639b31SHeiko Carstens 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
314ebf95e88SHarald Freudenberger 		: [reg1] "+&d" (reg1.value)
315b9639b31SHeiko Carstens 		: [reg0] "d" (reg0), [reg2] "d" (reg2)
316394740d7SHalil Pasic 		: "cc", "memory", "0", "1", "2");
317159491f3SHarald Freudenberger 
318159491f3SHarald Freudenberger 	return reg1.status;
319f1b0a434SHarald Freudenberger }
320f1b0a434SHarald Freudenberger 
321f1b0a434SHarald Freudenberger /*
322f1b0a434SHarald Freudenberger  * union ap_qact_ap_info - used together with the
323f1b0a434SHarald Freudenberger  * ap_aqic() function to provide a convenient way
324f1b0a434SHarald Freudenberger  * to handle the ap info needed by the qact function.
325f1b0a434SHarald Freudenberger  */
326f1b0a434SHarald Freudenberger union ap_qact_ap_info {
327f1b0a434SHarald Freudenberger 	unsigned long val;
328f1b0a434SHarald Freudenberger 	struct {
329f1b0a434SHarald Freudenberger 		unsigned int	  : 3;
330f1b0a434SHarald Freudenberger 		unsigned int mode : 3;
331f1b0a434SHarald Freudenberger 		unsigned int	  : 26;
332f1b0a434SHarald Freudenberger 		unsigned int cat  : 8;
333f1b0a434SHarald Freudenberger 		unsigned int	  : 8;
334f1b0a434SHarald Freudenberger 		unsigned char ver[2];
335f1b0a434SHarald Freudenberger 	};
336f1b0a434SHarald Freudenberger };
337f1b0a434SHarald Freudenberger 
338f1b0a434SHarald Freudenberger /**
339cada938aSHeiko Carstens  * ap_qact(): Query AP compatibility type.
340f1b0a434SHarald Freudenberger  * @qid: The AP queue number
341f1b0a434SHarald Freudenberger  * @apinfo: On input the info about the AP queue. On output the
342f1b0a434SHarald Freudenberger  *	    alternate AP queue info provided by the qact function
343f1b0a434SHarald Freudenberger  *	    in GR2 is stored in.
344f1b0a434SHarald Freudenberger  *
345f1b0a434SHarald Freudenberger  * Returns AP queue status. Check response_code field for failures.
346f1b0a434SHarald Freudenberger  */
347f1b0a434SHarald Freudenberger static inline struct ap_queue_status ap_qact(ap_qid_t qid, int ifbit,
348f1b0a434SHarald Freudenberger 					     union ap_qact_ap_info *apinfo)
349f1b0a434SHarald Freudenberger {
350b9639b31SHeiko Carstens 	unsigned long reg0 = qid | (5UL << 24) | ((ifbit & 0x01) << 22);
351ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
352b9639b31SHeiko Carstens 	unsigned long reg2;
353f1b0a434SHarald Freudenberger 
354159491f3SHarald Freudenberger 	reg1.value = apinfo->val;
355159491f3SHarald Freudenberger 
356f1b0a434SHarald Freudenberger 	asm volatile(
357b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"		/* qid param into gr0 */
358b9639b31SHeiko Carstens 		"	lgr	1,%[reg1]\n"		/* qact in info into gr1 */
3592d6c0008SHeiko Carstens 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(QACT) */
360b9639b31SHeiko Carstens 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
361b9639b31SHeiko Carstens 		"	lgr	%[reg2],2\n"		/* qact out info into reg2 */
362ebf95e88SHarald Freudenberger 		: [reg1] "+&d" (reg1.value), [reg2] "=&d" (reg2)
363b9639b31SHeiko Carstens 		: [reg0] "d" (reg0)
364b9639b31SHeiko Carstens 		: "cc", "0", "1", "2");
365f1b0a434SHarald Freudenberger 	apinfo->val = reg2;
366159491f3SHarald Freudenberger 	return reg1.status;
367f1b0a434SHarald Freudenberger }
368f1b0a434SHarald Freudenberger 
369c81cf436SHarald Freudenberger /*
370c81cf436SHarald Freudenberger  * ap_bapq(): SE bind AP queue.
371c81cf436SHarald Freudenberger  * @qid: The AP queue number
372c81cf436SHarald Freudenberger  *
373c81cf436SHarald Freudenberger  * Returns AP queue status structure.
374c81cf436SHarald Freudenberger  *
375c81cf436SHarald Freudenberger  * Invoking this function in a non-SE environment
376c81cf436SHarald Freudenberger  * may case a specification exception.
377c81cf436SHarald Freudenberger  */
378c81cf436SHarald Freudenberger static inline struct ap_queue_status ap_bapq(ap_qid_t qid)
379c81cf436SHarald Freudenberger {
380c81cf436SHarald Freudenberger 	unsigned long reg0 = qid | (7UL << 24);  /* fc 7 is BAPQ */
381c81cf436SHarald Freudenberger 	union ap_queue_status_reg reg1;
382c81cf436SHarald Freudenberger 
383c81cf436SHarald Freudenberger 	asm volatile(
384c81cf436SHarald Freudenberger 		"	lgr	0,%[reg0]\n"		/* qid arg into gr0 */
385c81cf436SHarald Freudenberger 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(BAPQ) */
386c81cf436SHarald Freudenberger 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
387c81cf436SHarald Freudenberger 		: [reg1] "=&d" (reg1.value)
388c81cf436SHarald Freudenberger 		: [reg0] "d" (reg0)
389c81cf436SHarald Freudenberger 		: "cc", "0", "1");
390c81cf436SHarald Freudenberger 
391c81cf436SHarald Freudenberger 	return reg1.status;
392c81cf436SHarald Freudenberger }
393c81cf436SHarald Freudenberger 
394c81cf436SHarald Freudenberger /*
395c81cf436SHarald Freudenberger  * ap_aapq(): SE associate AP queue.
396c81cf436SHarald Freudenberger  * @qid: The AP queue number
397c81cf436SHarald Freudenberger  * @sec_idx: The secret index
398c81cf436SHarald Freudenberger  *
399c81cf436SHarald Freudenberger  * Returns AP queue status structure.
400c81cf436SHarald Freudenberger  *
401c81cf436SHarald Freudenberger  * Invoking this function in a non-SE environment
402c81cf436SHarald Freudenberger  * may case a specification exception.
403c81cf436SHarald Freudenberger  */
404c81cf436SHarald Freudenberger static inline struct ap_queue_status ap_aapq(ap_qid_t qid, unsigned int sec_idx)
405c81cf436SHarald Freudenberger {
406c81cf436SHarald Freudenberger 	unsigned long reg0 = qid | (8UL << 24);  /* fc 8 is AAPQ */
407c81cf436SHarald Freudenberger 	unsigned long reg2 = sec_idx;
408c81cf436SHarald Freudenberger 	union ap_queue_status_reg reg1;
409c81cf436SHarald Freudenberger 
410c81cf436SHarald Freudenberger 	asm volatile(
411c81cf436SHarald Freudenberger 		"	lgr	0,%[reg0]\n"		/* qid arg into gr0 */
412c81cf436SHarald Freudenberger 		"	lgr	2,%[reg2]\n"		/* secret index into gr2 */
413c81cf436SHarald Freudenberger 		"	.insn	rre,0xb2af0000,0,0\n"	/* PQAP(AAPQ) */
414c81cf436SHarald Freudenberger 		"	lgr	%[reg1],1\n"		/* gr1 (status) into reg1 */
415c81cf436SHarald Freudenberger 		: [reg1] "=&d" (reg1.value)
416c81cf436SHarald Freudenberger 		: [reg0] "d" (reg0), [reg2] "d" (reg2)
417c81cf436SHarald Freudenberger 		: "cc", "0", "1", "2");
418c81cf436SHarald Freudenberger 
419c81cf436SHarald Freudenberger 	return reg1.status;
420c81cf436SHarald Freudenberger }
421c81cf436SHarald Freudenberger 
422f1b0a434SHarald Freudenberger /**
423f1b0a434SHarald Freudenberger  * ap_nqap(): Send message to adjunct processor queue.
424f1b0a434SHarald Freudenberger  * @qid: The AP queue number
425f1b0a434SHarald Freudenberger  * @psmid: The program supplied message identifier
426f1b0a434SHarald Freudenberger  * @msg: The message text
427f1b0a434SHarald Freudenberger  * @length: The message length
428f1b0a434SHarald Freudenberger  *
429f1b0a434SHarald Freudenberger  * Returns AP queue status structure.
430f1b0a434SHarald Freudenberger  * Condition code 1 on NQAP can't happen because the L bit is 1.
431f1b0a434SHarald Freudenberger  * Condition code 2 on NQAP also means the send is incomplete,
432f1b0a434SHarald Freudenberger  * because a segment boundary was reached. The NQAP is repeated.
433f1b0a434SHarald Freudenberger  */
434f1b0a434SHarald Freudenberger static inline struct ap_queue_status ap_nqap(ap_qid_t qid,
435f1b0a434SHarald Freudenberger 					     unsigned long long psmid,
436f1b0a434SHarald Freudenberger 					     void *msg, size_t length)
437f1b0a434SHarald Freudenberger {
438b9639b31SHeiko Carstens 	unsigned long reg0 = qid | 0x40000000UL;  /* 0x4... is last msg part */
439b9639b31SHeiko Carstens 	union register_pair nqap_r1, nqap_r2;
440ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
441b9639b31SHeiko Carstens 
442b9639b31SHeiko Carstens 	nqap_r1.even = (unsigned int)(psmid >> 32);
443b9639b31SHeiko Carstens 	nqap_r1.odd  = psmid & 0xffffffff;
444b9639b31SHeiko Carstens 	nqap_r2.even = (unsigned long)msg;
445b9639b31SHeiko Carstens 	nqap_r2.odd  = (unsigned long)length;
446f1b0a434SHarald Freudenberger 
447f1b0a434SHarald Freudenberger 	asm volatile (
448b9639b31SHeiko Carstens 		"	lgr	0,%[reg0]\n"  /* qid param in gr0 */
449b9639b31SHeiko Carstens 		"0:	.insn	rre,0xb2ad0000,%[nqap_r1],%[nqap_r2]\n"
450b9639b31SHeiko Carstens 		"	brc	2,0b\n"       /* handle partial completion */
451b9639b31SHeiko Carstens 		"	lgr	%[reg1],1\n"  /* gr1 (status) into reg1 */
452ebf95e88SHarald Freudenberger 		: [reg0] "+&d" (reg0), [reg1] "=&d" (reg1.value),
453b9639b31SHeiko Carstens 		  [nqap_r2] "+&d" (nqap_r2.pair)
454b9639b31SHeiko Carstens 		: [nqap_r1] "d" (nqap_r1.pair)
455b9639b31SHeiko Carstens 		: "cc", "memory", "0", "1");
456ebf95e88SHarald Freudenberger 	return reg1.status;
457f1b0a434SHarald Freudenberger }
458f1b0a434SHarald Freudenberger 
459f1b0a434SHarald Freudenberger /**
460f1b0a434SHarald Freudenberger  * ap_dqap(): Receive message from adjunct processor queue.
461f1b0a434SHarald Freudenberger  * @qid: The AP queue number
462f1b0a434SHarald Freudenberger  * @psmid: Pointer to program supplied message identifier
4638794c596SHarald Freudenberger  * @msg: Pointer to message buffer
4648794c596SHarald Freudenberger  * @msglen: Message buffer size
4658794c596SHarald Freudenberger  * @length: Pointer to length of actually written bytes
4668794c596SHarald Freudenberger  * @reslength: Residual length on return
4678794c596SHarald Freudenberger  * @resgr0: input: gr0 value (only used if != 0), output: residual gr0 content
468f1b0a434SHarald Freudenberger  *
469f1b0a434SHarald Freudenberger  * Returns AP queue status structure.
470f1b0a434SHarald Freudenberger  * Condition code 1 on DQAP means the receive has taken place
471f1b0a434SHarald Freudenberger  * but only partially.	The response is incomplete, hence the
472f1b0a434SHarald Freudenberger  * DQAP is repeated.
473f1b0a434SHarald Freudenberger  * Condition code 2 on DQAP also means the receive is incomplete,
474f1b0a434SHarald Freudenberger  * this time because a segment boundary was reached. Again, the
475f1b0a434SHarald Freudenberger  * DQAP is repeated.
476f1b0a434SHarald Freudenberger  * Note that gpr2 is used by the DQAP instruction to keep track of
477f1b0a434SHarald Freudenberger  * any 'residual' length, in case the instruction gets interrupted.
478f1b0a434SHarald Freudenberger  * Hence it gets zeroed before the instruction.
4791f0d22deSHarald Freudenberger  * If the message does not fit into the buffer, this function will
4801f0d22deSHarald Freudenberger  * return with a truncated message and the reply in the firmware queue
4811f0d22deSHarald Freudenberger  * is not removed. This is indicated to the caller with an
4821f0d22deSHarald Freudenberger  * ap_queue_status response_code value of all bits on (0xFF) and (if
4831f0d22deSHarald Freudenberger  * the reslength ptr is given) the remaining length is stored in
4841f0d22deSHarald Freudenberger  * *reslength and (if the resgr0 ptr is given) the updated gr0 value
4851f0d22deSHarald Freudenberger  * for further processing of this msg entry is stored in *resgr0. The
4861f0d22deSHarald Freudenberger  * caller needs to detect this situation and should invoke ap_dqap
4871f0d22deSHarald Freudenberger  * with a valid resgr0 ptr and a value in there != 0 to indicate that
4881f0d22deSHarald Freudenberger  * *resgr0 is to be used instead of qid to further process this entry.
489f1b0a434SHarald Freudenberger  */
490f1b0a434SHarald Freudenberger static inline struct ap_queue_status ap_dqap(ap_qid_t qid,
491003d248fSHarald Freudenberger 					     unsigned long *psmid,
4928794c596SHarald Freudenberger 					     void *msg, size_t msglen,
4938794c596SHarald Freudenberger 					     size_t *length,
4941f0d22deSHarald Freudenberger 					     size_t *reslength,
4951f0d22deSHarald Freudenberger 					     unsigned long *resgr0)
496f1b0a434SHarald Freudenberger {
4974516f355SHarald Freudenberger 	unsigned long reg0 = resgr0 && *resgr0 ? *resgr0 : qid | 0x80000000UL;
498ebf95e88SHarald Freudenberger 	union ap_queue_status_reg reg1;
4994516f355SHarald Freudenberger 	unsigned long reg2;
5004516f355SHarald Freudenberger 	union register_pair rp1, rp2;
5014516f355SHarald Freudenberger 
5024516f355SHarald Freudenberger 	rp1.even = 0UL;
5034516f355SHarald Freudenberger 	rp1.odd  = 0UL;
5044516f355SHarald Freudenberger 	rp2.even = (unsigned long)msg;
5058794c596SHarald Freudenberger 	rp2.odd  = (unsigned long)msglen;
506f1b0a434SHarald Freudenberger 
507f1b0a434SHarald Freudenberger 	asm volatile(
5084516f355SHarald Freudenberger 		"	lgr	0,%[reg0]\n"   /* qid param into gr0 */
5094516f355SHarald Freudenberger 		"	lghi	2,0\n"	       /* 0 into gr2 (res length) */
5104516f355SHarald Freudenberger 		"0:	ltgr	%N[rp2],%N[rp2]\n" /* check buf len */
5114516f355SHarald Freudenberger 		"	jz	2f\n"	       /* go out if buf len is 0 */
5124516f355SHarald Freudenberger 		"1:	.insn	rre,0xb2ae0000,%[rp1],%[rp2]\n"
5134516f355SHarald Freudenberger 		"	brc	6,0b\n"        /* handle partial complete */
5144516f355SHarald Freudenberger 		"2:	lgr	%[reg0],0\n"   /* gr0 (qid + info) into reg0 */
5154516f355SHarald Freudenberger 		"	lgr	%[reg1],1\n"   /* gr1 (status) into reg1 */
5164516f355SHarald Freudenberger 		"	lgr	%[reg2],2\n"   /* gr2 (res length) into reg2 */
517ebf95e88SHarald Freudenberger 		: [reg0] "+&d" (reg0), [reg1] "=&d" (reg1.value),
518ebf95e88SHarald Freudenberger 		  [reg2] "=&d" (reg2), [rp1] "+&d" (rp1.pair),
519ebf95e88SHarald Freudenberger 		  [rp2] "+&d" (rp2.pair)
5204516f355SHarald Freudenberger 		:
5214516f355SHarald Freudenberger 		: "cc", "memory", "0", "1", "2");
5221f0d22deSHarald Freudenberger 
5231f0d22deSHarald Freudenberger 	if (reslength)
5241f0d22deSHarald Freudenberger 		*reslength = reg2;
5254516f355SHarald Freudenberger 	if (reg2 != 0 && rp2.odd == 0) {
5261f0d22deSHarald Freudenberger 		/*
5271f0d22deSHarald Freudenberger 		 * Partially complete, status in gr1 is not set.
5281f0d22deSHarald Freudenberger 		 * Signal the caller that this dqap is only partially received
5291f0d22deSHarald Freudenberger 		 * with a special status response code 0xFF and *resgr0 updated
5301f0d22deSHarald Freudenberger 		 */
531ebf95e88SHarald Freudenberger 		reg1.status.response_code = 0xFF;
5321f0d22deSHarald Freudenberger 		if (resgr0)
5331f0d22deSHarald Freudenberger 			*resgr0 = reg0;
5341f0d22deSHarald Freudenberger 	} else {
535003d248fSHarald Freudenberger 		*psmid = (rp1.even << 32) + rp1.odd;
5361f0d22deSHarald Freudenberger 		if (resgr0)
5371f0d22deSHarald Freudenberger 			*resgr0 = 0;
5381f0d22deSHarald Freudenberger 	}
5391f0d22deSHarald Freudenberger 
5408794c596SHarald Freudenberger 	/* update *length with the nr of bytes stored into the msg buffer */
5418794c596SHarald Freudenberger 	if (length)
5428794c596SHarald Freudenberger 		*length = msglen - rp2.odd;
5438794c596SHarald Freudenberger 
544ebf95e88SHarald Freudenberger 	return reg1.status;
545f1b0a434SHarald Freudenberger }
54646fde9a9SHarald Freudenberger 
5470d9c038fSTony Krowiak /*
5480d9c038fSTony Krowiak  * Interface to tell the AP bus code that a configuration
5490d9c038fSTony Krowiak  * change has happened. The bus code should at least do
5500d9c038fSTony Krowiak  * an ap bus resource rescan.
5510d9c038fSTony Krowiak  */
5520d9c038fSTony Krowiak #if IS_ENABLED(CONFIG_ZCRYPT)
5530d9c038fSTony Krowiak void ap_bus_cfg_chg(void);
5540d9c038fSTony Krowiak #else
555d09cb482SChengyang Fan static inline void ap_bus_cfg_chg(void){}
5560d9c038fSTony Krowiak #endif
5570d9c038fSTony Krowiak 
558e7fc5146STony Krowiak #endif /* _ASM_S390_AP_H_ */
559