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 }; 84 85 struct uv_cb_header { 86 u16 len; 87 u16 cmd; /* Command Code */ 88 u16 rc; /* Response Code */ 89 u16 rrc; /* Return Reason Code */ 90 } __packed __aligned(8); 91 92 /* Query Ultravisor Information */ 93 struct uv_cb_qui { 94 struct uv_cb_header header; /* 0x0000 */ 95 u64 reserved08; /* 0x0008 */ 96 u64 inst_calls_list[4]; /* 0x0010 */ 97 u64 reserved30[2]; /* 0x0030 */ 98 u64 uv_base_stor_len; /* 0x0040 */ 99 u64 reserved48; /* 0x0048 */ 100 u64 conf_base_phys_stor_len; /* 0x0050 */ 101 u64 conf_base_virt_stor_len; /* 0x0058 */ 102 u64 conf_virt_var_stor_len; /* 0x0060 */ 103 u64 cpu_stor_len; /* 0x0068 */ 104 u32 reserved70[3]; /* 0x0070 */ 105 u32 max_num_sec_conf; /* 0x007c */ 106 u64 max_guest_stor_addr; /* 0x0080 */ 107 u8 reserved88[158 - 136]; /* 0x0088 */ 108 u16 max_guest_cpu_id; /* 0x009e */ 109 u64 uv_feature_indications; /* 0x00a0 */ 110 u8 reserveda8[200 - 168]; /* 0x00a8 */ 111 } __packed __aligned(8); 112 113 /* Initialize Ultravisor */ 114 struct uv_cb_init { 115 struct uv_cb_header header; 116 u64 reserved08[2]; 117 u64 stor_origin; 118 u64 stor_len; 119 u64 reserved28[4]; 120 } __packed __aligned(8); 121 122 /* Create Guest Configuration */ 123 struct uv_cb_cgc { 124 struct uv_cb_header header; 125 u64 reserved08[2]; 126 u64 guest_handle; 127 u64 conf_base_stor_origin; 128 u64 conf_virt_stor_origin; 129 u64 reserved30; 130 u64 guest_stor_origin; 131 u64 guest_stor_len; 132 u64 guest_sca; 133 u64 guest_asce; 134 u64 reserved58[5]; 135 } __packed __aligned(8); 136 137 /* Create Secure CPU */ 138 struct uv_cb_csc { 139 struct uv_cb_header header; 140 u64 reserved08[2]; 141 u64 cpu_handle; 142 u64 guest_handle; 143 u64 stor_origin; 144 u8 reserved30[6]; 145 u16 num; 146 u64 state_origin; 147 u64 reserved40[4]; 148 } __packed __aligned(8); 149 150 /* Convert to Secure */ 151 struct uv_cb_cts { 152 struct uv_cb_header header; 153 u64 reserved08[2]; 154 u64 guest_handle; 155 u64 gaddr; 156 } __packed __aligned(8); 157 158 /* Convert from Secure / Pin Page Shared */ 159 struct uv_cb_cfs { 160 struct uv_cb_header header; 161 u64 reserved08[2]; 162 u64 paddr; 163 } __packed __aligned(8); 164 165 /* Set Secure Config Parameter */ 166 struct uv_cb_ssc { 167 struct uv_cb_header header; 168 u64 reserved08[2]; 169 u64 guest_handle; 170 u64 sec_header_origin; 171 u32 sec_header_len; 172 u32 reserved2c; 173 u64 reserved30[4]; 174 } __packed __aligned(8); 175 176 /* Unpack */ 177 struct uv_cb_unp { 178 struct uv_cb_header header; 179 u64 reserved08[2]; 180 u64 guest_handle; 181 u64 gaddr; 182 u64 tweak[2]; 183 u64 reserved38[3]; 184 } __packed __aligned(8); 185 186 #define PV_CPU_STATE_OPR 1 187 #define PV_CPU_STATE_STP 2 188 #define PV_CPU_STATE_CHKSTP 3 189 #define PV_CPU_STATE_OPR_LOAD 5 190 191 struct uv_cb_cpu_set_state { 192 struct uv_cb_header header; 193 u64 reserved08[2]; 194 u64 cpu_handle; 195 u8 reserved20[7]; 196 u8 state; 197 u64 reserved28[5]; 198 }; 199 200 /* 201 * A common UV call struct for calls that take no payload 202 * Examples: 203 * Destroy cpu/config 204 * Verify 205 */ 206 struct uv_cb_nodata { 207 struct uv_cb_header header; 208 u64 reserved08[2]; 209 u64 handle; 210 u64 reserved20[4]; 211 } __packed __aligned(8); 212 213 /* Set Shared Access */ 214 struct uv_cb_share { 215 struct uv_cb_header header; 216 u64 reserved08[3]; 217 u64 paddr; 218 u64 reserved28; 219 } __packed __aligned(8); 220 221 static inline int __uv_call(unsigned long r1, unsigned long r2) 222 { 223 int cc; 224 225 asm volatile( 226 " .insn rrf,0xB9A40000,%[r1],%[r2],0,0\n" 227 " ipm %[cc]\n" 228 " srl %[cc],28\n" 229 : [cc] "=d" (cc) 230 : [r1] "a" (r1), [r2] "a" (r2) 231 : "memory", "cc"); 232 return cc; 233 } 234 235 static inline int uv_call(unsigned long r1, unsigned long r2) 236 { 237 int cc; 238 239 do { 240 cc = __uv_call(r1, r2); 241 } while (cc > 1); 242 return cc; 243 } 244 245 /* Low level uv_call that avoids stalls for long running busy conditions */ 246 static inline int uv_call_sched(unsigned long r1, unsigned long r2) 247 { 248 int cc; 249 250 do { 251 cc = __uv_call(r1, r2); 252 cond_resched(); 253 } while (cc > 1); 254 return cc; 255 } 256 257 /* 258 * special variant of uv_call that only transports the cpu or guest 259 * handle and the command, like destroy or verify. 260 */ 261 static inline int uv_cmd_nodata(u64 handle, u16 cmd, u16 *rc, u16 *rrc) 262 { 263 struct uv_cb_nodata uvcb = { 264 .header.cmd = cmd, 265 .header.len = sizeof(uvcb), 266 .handle = handle, 267 }; 268 int cc; 269 270 WARN(!handle, "No handle provided to Ultravisor call cmd %x\n", cmd); 271 cc = uv_call_sched(0, (u64)&uvcb); 272 *rc = uvcb.header.rc; 273 *rrc = uvcb.header.rrc; 274 return cc ? -EINVAL : 0; 275 } 276 277 struct uv_info { 278 unsigned long inst_calls_list[4]; 279 unsigned long uv_base_stor_len; 280 unsigned long guest_base_stor_len; 281 unsigned long guest_virt_base_stor_len; 282 unsigned long guest_virt_var_stor_len; 283 unsigned long guest_cpu_stor_len; 284 unsigned long max_sec_stor_addr; 285 unsigned int max_num_sec_conf; 286 unsigned short max_guest_cpu_id; 287 unsigned long uv_feature_indications; 288 }; 289 290 extern struct uv_info uv_info; 291 292 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST 293 extern int prot_virt_guest; 294 295 static inline int is_prot_virt_guest(void) 296 { 297 return prot_virt_guest; 298 } 299 300 static inline int share(unsigned long addr, u16 cmd) 301 { 302 struct uv_cb_share uvcb = { 303 .header.cmd = cmd, 304 .header.len = sizeof(uvcb), 305 .paddr = addr 306 }; 307 308 if (!is_prot_virt_guest()) 309 return -EOPNOTSUPP; 310 /* 311 * Sharing is page wise, if we encounter addresses that are 312 * not page aligned, we assume something went wrong. If 313 * malloced structs are passed to this function, we could leak 314 * data to the hypervisor. 315 */ 316 BUG_ON(addr & ~PAGE_MASK); 317 318 if (!uv_call(0, (u64)&uvcb)) 319 return 0; 320 return -EINVAL; 321 } 322 323 /* 324 * Guest 2 request to the Ultravisor to make a page shared with the 325 * hypervisor for IO. 326 * 327 * @addr: Real or absolute address of the page to be shared 328 */ 329 static inline int uv_set_shared(unsigned long addr) 330 { 331 return share(addr, UVC_CMD_SET_SHARED_ACCESS); 332 } 333 334 /* 335 * Guest 2 request to the Ultravisor to make a page unshared. 336 * 337 * @addr: Real or absolute address of the page to be unshared 338 */ 339 static inline int uv_remove_shared(unsigned long addr) 340 { 341 return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS); 342 } 343 344 #else 345 #define is_prot_virt_guest() 0 346 static inline int uv_set_shared(unsigned long addr) { return 0; } 347 static inline int uv_remove_shared(unsigned long addr) { return 0; } 348 #endif 349 350 #if IS_ENABLED(CONFIG_KVM) 351 extern int prot_virt_host; 352 353 static inline int is_prot_virt_host(void) 354 { 355 return prot_virt_host; 356 } 357 358 int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb); 359 int uv_destroy_owned_page(unsigned long paddr); 360 int uv_convert_from_secure(unsigned long paddr); 361 int uv_convert_owned_from_secure(unsigned long paddr); 362 int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr); 363 364 void setup_uv(void); 365 #else 366 #define is_prot_virt_host() 0 367 static inline void setup_uv(void) {} 368 369 static inline int uv_destroy_owned_page(unsigned long paddr) 370 { 371 return 0; 372 } 373 374 static inline int uv_convert_from_secure(unsigned long paddr) 375 { 376 return 0; 377 } 378 379 static inline int uv_convert_owned_from_secure(unsigned long paddr) 380 { 381 return 0; 382 } 383 #endif 384 385 #endif /* _ASM_S390_UV_H */ 386