xref: /openbmc/linux/arch/s390/include/asm/uv.h (revision ba936421)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Ultravisor Interfaces
4  *
5  * Copyright IBM Corp. 2019
6  *
7  * Author(s):
8  *	Vasily Gorbik <gor@linux.ibm.com>
9  *	Janosch Frank <frankja@linux.ibm.com>
10  */
11 #ifndef _ASM_S390_UV_H
12 #define _ASM_S390_UV_H
13 
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/sched.h>
18 #include <asm/page.h>
19 #include <asm/gmap.h>
20 
21 #define UVC_CC_OK	0
22 #define UVC_CC_ERROR	1
23 #define UVC_CC_BUSY	2
24 #define UVC_CC_PARTIAL	3
25 
26 #define UVC_RC_EXECUTED		0x0001
27 #define UVC_RC_INV_CMD		0x0002
28 #define UVC_RC_INV_STATE	0x0003
29 #define UVC_RC_INV_LEN		0x0005
30 #define UVC_RC_NO_RESUME	0x0007
31 #define UVC_RC_NEED_DESTROY	0x8000
32 
33 #define UVC_CMD_QUI			0x0001
34 #define UVC_CMD_INIT_UV			0x000f
35 #define UVC_CMD_CREATE_SEC_CONF		0x0100
36 #define UVC_CMD_DESTROY_SEC_CONF	0x0101
37 #define UVC_CMD_CREATE_SEC_CPU		0x0120
38 #define UVC_CMD_DESTROY_SEC_CPU		0x0121
39 #define UVC_CMD_CONV_TO_SEC_STOR	0x0200
40 #define UVC_CMD_CONV_FROM_SEC_STOR	0x0201
41 #define UVC_CMD_DESTR_SEC_STOR		0x0202
42 #define UVC_CMD_SET_SEC_CONF_PARAMS	0x0300
43 #define UVC_CMD_UNPACK_IMG		0x0301
44 #define UVC_CMD_VERIFY_IMG		0x0302
45 #define UVC_CMD_CPU_RESET		0x0310
46 #define UVC_CMD_CPU_RESET_INITIAL	0x0311
47 #define UVC_CMD_PREPARE_RESET		0x0320
48 #define UVC_CMD_CPU_RESET_CLEAR		0x0321
49 #define UVC_CMD_CPU_SET_STATE		0x0330
50 #define UVC_CMD_SET_UNSHARE_ALL		0x0340
51 #define UVC_CMD_PIN_PAGE_SHARED		0x0341
52 #define UVC_CMD_UNPIN_PAGE_SHARED	0x0342
53 #define UVC_CMD_SET_SHARED_ACCESS	0x1000
54 #define UVC_CMD_REMOVE_SHARED_ACCESS	0x1001
55 
56 /* Bits in installed uv calls */
57 enum uv_cmds_inst {
58 	BIT_UVC_CMD_QUI = 0,
59 	BIT_UVC_CMD_INIT_UV = 1,
60 	BIT_UVC_CMD_CREATE_SEC_CONF = 2,
61 	BIT_UVC_CMD_DESTROY_SEC_CONF = 3,
62 	BIT_UVC_CMD_CREATE_SEC_CPU = 4,
63 	BIT_UVC_CMD_DESTROY_SEC_CPU = 5,
64 	BIT_UVC_CMD_CONV_TO_SEC_STOR = 6,
65 	BIT_UVC_CMD_CONV_FROM_SEC_STOR = 7,
66 	BIT_UVC_CMD_SET_SHARED_ACCESS = 8,
67 	BIT_UVC_CMD_REMOVE_SHARED_ACCESS = 9,
68 	BIT_UVC_CMD_SET_SEC_PARMS = 11,
69 	BIT_UVC_CMD_UNPACK_IMG = 13,
70 	BIT_UVC_CMD_VERIFY_IMG = 14,
71 	BIT_UVC_CMD_CPU_RESET = 15,
72 	BIT_UVC_CMD_CPU_RESET_INITIAL = 16,
73 	BIT_UVC_CMD_CPU_SET_STATE = 17,
74 	BIT_UVC_CMD_PREPARE_RESET = 18,
75 	BIT_UVC_CMD_CPU_PERFORM_CLEAR_RESET = 19,
76 	BIT_UVC_CMD_UNSHARE_ALL = 20,
77 	BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
78 	BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
79 };
80 
81 enum uv_feat_ind {
82 	BIT_UV_FEAT_MISC = 0,
83 	BIT_UV_FEAT_AIV = 1,
84 };
85 
86 struct uv_cb_header {
87 	u16 len;
88 	u16 cmd;	/* Command Code */
89 	u16 rc;		/* Response Code */
90 	u16 rrc;	/* Return Reason Code */
91 } __packed __aligned(8);
92 
93 /* Query Ultravisor Information */
94 struct uv_cb_qui {
95 	struct uv_cb_header header;		/* 0x0000 */
96 	u64 reserved08;				/* 0x0008 */
97 	u64 inst_calls_list[4];			/* 0x0010 */
98 	u64 reserved30[2];			/* 0x0030 */
99 	u64 uv_base_stor_len;			/* 0x0040 */
100 	u64 reserved48;				/* 0x0048 */
101 	u64 conf_base_phys_stor_len;		/* 0x0050 */
102 	u64 conf_base_virt_stor_len;		/* 0x0058 */
103 	u64 conf_virt_var_stor_len;		/* 0x0060 */
104 	u64 cpu_stor_len;			/* 0x0068 */
105 	u32 reserved70[3];			/* 0x0070 */
106 	u32 max_num_sec_conf;			/* 0x007c */
107 	u64 max_guest_stor_addr;		/* 0x0080 */
108 	u8  reserved88[158 - 136];		/* 0x0088 */
109 	u16 max_guest_cpu_id;			/* 0x009e */
110 	u64 uv_feature_indications;		/* 0x00a0 */
111 	u8  reserveda8[200 - 168];		/* 0x00a8 */
112 } __packed __aligned(8);
113 
114 /* Initialize Ultravisor */
115 struct uv_cb_init {
116 	struct uv_cb_header header;
117 	u64 reserved08[2];
118 	u64 stor_origin;
119 	u64 stor_len;
120 	u64 reserved28[4];
121 } __packed __aligned(8);
122 
123 /* Create Guest Configuration */
124 struct uv_cb_cgc {
125 	struct uv_cb_header header;
126 	u64 reserved08[2];
127 	u64 guest_handle;
128 	u64 conf_base_stor_origin;
129 	u64 conf_virt_stor_origin;
130 	u64 reserved30;
131 	u64 guest_stor_origin;
132 	u64 guest_stor_len;
133 	u64 guest_sca;
134 	u64 guest_asce;
135 	u64 reserved58[5];
136 } __packed __aligned(8);
137 
138 /* Create Secure CPU */
139 struct uv_cb_csc {
140 	struct uv_cb_header header;
141 	u64 reserved08[2];
142 	u64 cpu_handle;
143 	u64 guest_handle;
144 	u64 stor_origin;
145 	u8  reserved30[6];
146 	u16 num;
147 	u64 state_origin;
148 	u64 reserved40[4];
149 } __packed __aligned(8);
150 
151 /* Convert to Secure */
152 struct uv_cb_cts {
153 	struct uv_cb_header header;
154 	u64 reserved08[2];
155 	u64 guest_handle;
156 	u64 gaddr;
157 } __packed __aligned(8);
158 
159 /* Convert from Secure / Pin Page Shared */
160 struct uv_cb_cfs {
161 	struct uv_cb_header header;
162 	u64 reserved08[2];
163 	u64 paddr;
164 } __packed __aligned(8);
165 
166 /* Set Secure Config Parameter */
167 struct uv_cb_ssc {
168 	struct uv_cb_header header;
169 	u64 reserved08[2];
170 	u64 guest_handle;
171 	u64 sec_header_origin;
172 	u32 sec_header_len;
173 	u32 reserved2c;
174 	u64 reserved30[4];
175 } __packed __aligned(8);
176 
177 /* Unpack */
178 struct uv_cb_unp {
179 	struct uv_cb_header header;
180 	u64 reserved08[2];
181 	u64 guest_handle;
182 	u64 gaddr;
183 	u64 tweak[2];
184 	u64 reserved38[3];
185 } __packed __aligned(8);
186 
187 #define PV_CPU_STATE_OPR	1
188 #define PV_CPU_STATE_STP	2
189 #define PV_CPU_STATE_CHKSTP	3
190 #define PV_CPU_STATE_OPR_LOAD	5
191 
192 struct uv_cb_cpu_set_state {
193 	struct uv_cb_header header;
194 	u64 reserved08[2];
195 	u64 cpu_handle;
196 	u8  reserved20[7];
197 	u8  state;
198 	u64 reserved28[5];
199 };
200 
201 /*
202  * A common UV call struct for calls that take no payload
203  * Examples:
204  * Destroy cpu/config
205  * Verify
206  */
207 struct uv_cb_nodata {
208 	struct uv_cb_header header;
209 	u64 reserved08[2];
210 	u64 handle;
211 	u64 reserved20[4];
212 } __packed __aligned(8);
213 
214 /* Set Shared Access */
215 struct uv_cb_share {
216 	struct uv_cb_header header;
217 	u64 reserved08[3];
218 	u64 paddr;
219 	u64 reserved28;
220 } __packed __aligned(8);
221 
222 static inline int __uv_call(unsigned long r1, unsigned long r2)
223 {
224 	int cc;
225 
226 	asm volatile(
227 		"	.insn rrf,0xB9A40000,%[r1],%[r2],0,0\n"
228 		"	ipm	%[cc]\n"
229 		"	srl	%[cc],28\n"
230 		: [cc] "=d" (cc)
231 		: [r1] "a" (r1), [r2] "a" (r2)
232 		: "memory", "cc");
233 	return cc;
234 }
235 
236 static inline int uv_call(unsigned long r1, unsigned long r2)
237 {
238 	int cc;
239 
240 	do {
241 		cc = __uv_call(r1, r2);
242 	} while (cc > 1);
243 	return cc;
244 }
245 
246 /* Low level uv_call that avoids stalls for long running busy conditions  */
247 static inline int uv_call_sched(unsigned long r1, unsigned long r2)
248 {
249 	int cc;
250 
251 	do {
252 		cc = __uv_call(r1, r2);
253 		cond_resched();
254 	} while (cc > 1);
255 	return cc;
256 }
257 
258 /*
259  * special variant of uv_call that only transports the cpu or guest
260  * handle and the command, like destroy or verify.
261  */
262 static inline int uv_cmd_nodata(u64 handle, u16 cmd, u16 *rc, u16 *rrc)
263 {
264 	struct uv_cb_nodata uvcb = {
265 		.header.cmd = cmd,
266 		.header.len = sizeof(uvcb),
267 		.handle = handle,
268 	};
269 	int cc;
270 
271 	WARN(!handle, "No handle provided to Ultravisor call cmd %x\n", cmd);
272 	cc = uv_call_sched(0, (u64)&uvcb);
273 	*rc = uvcb.header.rc;
274 	*rrc = uvcb.header.rrc;
275 	return cc ? -EINVAL : 0;
276 }
277 
278 struct uv_info {
279 	unsigned long inst_calls_list[4];
280 	unsigned long uv_base_stor_len;
281 	unsigned long guest_base_stor_len;
282 	unsigned long guest_virt_base_stor_len;
283 	unsigned long guest_virt_var_stor_len;
284 	unsigned long guest_cpu_stor_len;
285 	unsigned long max_sec_stor_addr;
286 	unsigned int max_num_sec_conf;
287 	unsigned short max_guest_cpu_id;
288 	unsigned long uv_feature_indications;
289 };
290 
291 extern struct uv_info uv_info;
292 
293 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
294 extern int prot_virt_guest;
295 
296 static inline int is_prot_virt_guest(void)
297 {
298 	return prot_virt_guest;
299 }
300 
301 static inline int share(unsigned long addr, u16 cmd)
302 {
303 	struct uv_cb_share uvcb = {
304 		.header.cmd = cmd,
305 		.header.len = sizeof(uvcb),
306 		.paddr = addr
307 	};
308 
309 	if (!is_prot_virt_guest())
310 		return -EOPNOTSUPP;
311 	/*
312 	 * Sharing is page wise, if we encounter addresses that are
313 	 * not page aligned, we assume something went wrong. If
314 	 * malloced structs are passed to this function, we could leak
315 	 * data to the hypervisor.
316 	 */
317 	BUG_ON(addr & ~PAGE_MASK);
318 
319 	if (!uv_call(0, (u64)&uvcb))
320 		return 0;
321 	return -EINVAL;
322 }
323 
324 /*
325  * Guest 2 request to the Ultravisor to make a page shared with the
326  * hypervisor for IO.
327  *
328  * @addr: Real or absolute address of the page to be shared
329  */
330 static inline int uv_set_shared(unsigned long addr)
331 {
332 	return share(addr, UVC_CMD_SET_SHARED_ACCESS);
333 }
334 
335 /*
336  * Guest 2 request to the Ultravisor to make a page unshared.
337  *
338  * @addr: Real or absolute address of the page to be unshared
339  */
340 static inline int uv_remove_shared(unsigned long addr)
341 {
342 	return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS);
343 }
344 
345 #else
346 #define is_prot_virt_guest() 0
347 static inline int uv_set_shared(unsigned long addr) { return 0; }
348 static inline int uv_remove_shared(unsigned long addr) { return 0; }
349 #endif
350 
351 #if IS_ENABLED(CONFIG_KVM)
352 extern int prot_virt_host;
353 
354 static inline int is_prot_virt_host(void)
355 {
356 	return prot_virt_host;
357 }
358 
359 int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb);
360 int uv_destroy_owned_page(unsigned long paddr);
361 int uv_convert_from_secure(unsigned long paddr);
362 int uv_convert_owned_from_secure(unsigned long paddr);
363 int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr);
364 
365 void setup_uv(void);
366 #else
367 #define is_prot_virt_host() 0
368 static inline void setup_uv(void) {}
369 
370 static inline int uv_destroy_owned_page(unsigned long paddr)
371 {
372 	return 0;
373 }
374 
375 static inline int uv_convert_from_secure(unsigned long paddr)
376 {
377 	return 0;
378 }
379 
380 static inline int uv_convert_owned_from_secure(unsigned long paddr)
381 {
382 	return 0;
383 }
384 #endif
385 
386 #endif /* _ASM_S390_UV_H */
387