xref: /linux/arch/powerpc/include/asm/guest-state-buffer.h (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Interface based on include/net/netlink.h
4  */
5 #ifndef _ASM_POWERPC_GUEST_STATE_BUFFER_H
6 #define _ASM_POWERPC_GUEST_STATE_BUFFER_H
7 
8 #include "asm/hvcall.h"
9 #include <linux/gfp.h>
10 #include <linux/bitmap.h>
11 #include <asm/plpar_wrappers.h>
12 
13 /**************************************************************************
14  * Guest State Buffer Constants
15  **************************************************************************/
16 /* Element without a value and any length */
17 #define KVMPPC_GSID_BLANK			0x0000
18 /* Size required for the L0's internal VCPU representation */
19 #define KVMPPC_GSID_HOST_STATE_SIZE		0x0001
20  /* Minimum size for the H_GUEST_RUN_VCPU output buffer */
21 #define KVMPPC_GSID_RUN_OUTPUT_MIN_SIZE		0x0002
22  /* "Logical" PVR value as defined in the PAPR */
23 #define KVMPPC_GSID_LOGICAL_PVR			0x0003
24  /* L0 relative timebase offset */
25 #define KVMPPC_GSID_TB_OFFSET			0x0004
26  /* Partition Scoped Page Table Info */
27 #define KVMPPC_GSID_PARTITION_TABLE		0x0005
28  /* Process Table Info */
29 #define KVMPPC_GSID_PROCESS_TABLE		0x0006
30 
31 /* H_GUEST_RUN_VCPU input buffer Info */
32 #define KVMPPC_GSID_RUN_INPUT			0x0C00
33 /* H_GUEST_RUN_VCPU output buffer Info */
34 #define KVMPPC_GSID_RUN_OUTPUT			0x0C01
35 #define KVMPPC_GSID_VPA				0x0C02
36 
37 #define KVMPPC_GSID_GPR(x)			(0x1000 + (x))
38 #define KVMPPC_GSID_HDEC_EXPIRY_TB		0x1020
39 #define KVMPPC_GSID_NIA				0x1021
40 #define KVMPPC_GSID_MSR				0x1022
41 #define KVMPPC_GSID_LR				0x1023
42 #define KVMPPC_GSID_XER				0x1024
43 #define KVMPPC_GSID_CTR				0x1025
44 #define KVMPPC_GSID_CFAR			0x1026
45 #define KVMPPC_GSID_SRR0			0x1027
46 #define KVMPPC_GSID_SRR1			0x1028
47 #define KVMPPC_GSID_DAR				0x1029
48 #define KVMPPC_GSID_DEC_EXPIRY_TB		0x102A
49 #define KVMPPC_GSID_VTB				0x102B
50 #define KVMPPC_GSID_LPCR			0x102C
51 #define KVMPPC_GSID_HFSCR			0x102D
52 #define KVMPPC_GSID_FSCR			0x102E
53 #define KVMPPC_GSID_FPSCR			0x102F
54 #define KVMPPC_GSID_DAWR0			0x1030
55 #define KVMPPC_GSID_DAWR1			0x1031
56 #define KVMPPC_GSID_CIABR			0x1032
57 #define KVMPPC_GSID_PURR			0x1033
58 #define KVMPPC_GSID_SPURR			0x1034
59 #define KVMPPC_GSID_IC				0x1035
60 #define KVMPPC_GSID_SPRG0			0x1036
61 #define KVMPPC_GSID_SPRG1			0x1037
62 #define KVMPPC_GSID_SPRG2			0x1038
63 #define KVMPPC_GSID_SPRG3			0x1039
64 #define KVMPPC_GSID_PPR				0x103A
65 #define KVMPPC_GSID_MMCR(x)			(0x103B + (x))
66 #define KVMPPC_GSID_MMCRA			0x103F
67 #define KVMPPC_GSID_SIER(x)			(0x1040 + (x))
68 #define KVMPPC_GSID_BESCR			0x1043
69 #define KVMPPC_GSID_EBBHR			0x1044
70 #define KVMPPC_GSID_EBBRR			0x1045
71 #define KVMPPC_GSID_AMR				0x1046
72 #define KVMPPC_GSID_IAMR			0x1047
73 #define KVMPPC_GSID_AMOR			0x1048
74 #define KVMPPC_GSID_UAMOR			0x1049
75 #define KVMPPC_GSID_SDAR			0x104A
76 #define KVMPPC_GSID_SIAR			0x104B
77 #define KVMPPC_GSID_DSCR			0x104C
78 #define KVMPPC_GSID_TAR				0x104D
79 #define KVMPPC_GSID_DEXCR			0x104E
80 #define KVMPPC_GSID_HDEXCR			0x104F
81 #define KVMPPC_GSID_HASHKEYR			0x1050
82 #define KVMPPC_GSID_HASHPKEYR			0x1051
83 #define KVMPPC_GSID_CTRL			0x1052
84 
85 #define KVMPPC_GSID_CR				0x2000
86 #define KVMPPC_GSID_PIDR			0x2001
87 #define KVMPPC_GSID_DSISR			0x2002
88 #define KVMPPC_GSID_VSCR			0x2003
89 #define KVMPPC_GSID_VRSAVE			0x2004
90 #define KVMPPC_GSID_DAWRX0			0x2005
91 #define KVMPPC_GSID_DAWRX1			0x2006
92 #define KVMPPC_GSID_PMC(x)			(0x2007 + (x))
93 #define KVMPPC_GSID_WORT			0x200D
94 #define KVMPPC_GSID_PSPB			0x200E
95 
96 #define KVMPPC_GSID_VSRS(x)			(0x3000 + (x))
97 
98 #define KVMPPC_GSID_HDAR			0xF000
99 #define KVMPPC_GSID_HDSISR			0xF001
100 #define KVMPPC_GSID_HEIR			0xF002
101 #define KVMPPC_GSID_ASDR			0xF003
102 
103 #define KVMPPC_GSE_GUESTWIDE_START KVMPPC_GSID_BLANK
104 #define KVMPPC_GSE_GUESTWIDE_END KVMPPC_GSID_PROCESS_TABLE
105 #define KVMPPC_GSE_GUESTWIDE_COUNT \
106 	(KVMPPC_GSE_GUESTWIDE_END - KVMPPC_GSE_GUESTWIDE_START + 1)
107 
108 #define KVMPPC_GSE_META_START KVMPPC_GSID_RUN_INPUT
109 #define KVMPPC_GSE_META_END KVMPPC_GSID_VPA
110 #define KVMPPC_GSE_META_COUNT (KVMPPC_GSE_META_END - KVMPPC_GSE_META_START + 1)
111 
112 #define KVMPPC_GSE_DW_REGS_START KVMPPC_GSID_GPR(0)
113 #define KVMPPC_GSE_DW_REGS_END KVMPPC_GSID_CTRL
114 #define KVMPPC_GSE_DW_REGS_COUNT \
115 	(KVMPPC_GSE_DW_REGS_END - KVMPPC_GSE_DW_REGS_START + 1)
116 
117 #define KVMPPC_GSE_W_REGS_START KVMPPC_GSID_CR
118 #define KVMPPC_GSE_W_REGS_END KVMPPC_GSID_PSPB
119 #define KVMPPC_GSE_W_REGS_COUNT \
120 	(KVMPPC_GSE_W_REGS_END - KVMPPC_GSE_W_REGS_START + 1)
121 
122 #define KVMPPC_GSE_VSRS_START KVMPPC_GSID_VSRS(0)
123 #define KVMPPC_GSE_VSRS_END KVMPPC_GSID_VSRS(63)
124 #define KVMPPC_GSE_VSRS_COUNT (KVMPPC_GSE_VSRS_END - KVMPPC_GSE_VSRS_START + 1)
125 
126 #define KVMPPC_GSE_INTR_REGS_START KVMPPC_GSID_HDAR
127 #define KVMPPC_GSE_INTR_REGS_END KVMPPC_GSID_ASDR
128 #define KVMPPC_GSE_INTR_REGS_COUNT \
129 	(KVMPPC_GSE_INTR_REGS_END - KVMPPC_GSE_INTR_REGS_START + 1)
130 
131 #define KVMPPC_GSE_IDEN_COUNT                                 \
132 	(KVMPPC_GSE_GUESTWIDE_COUNT + KVMPPC_GSE_META_COUNT + \
133 	 KVMPPC_GSE_DW_REGS_COUNT + KVMPPC_GSE_W_REGS_COUNT + \
134 	 KVMPPC_GSE_VSRS_COUNT + KVMPPC_GSE_INTR_REGS_COUNT)
135 
136 /**
137  * Ranges of guest state buffer elements
138  */
139 enum {
140 	KVMPPC_GS_CLASS_GUESTWIDE = 0x01,
141 	KVMPPC_GS_CLASS_META = 0x02,
142 	KVMPPC_GS_CLASS_DWORD_REG = 0x04,
143 	KVMPPC_GS_CLASS_WORD_REG = 0x08,
144 	KVMPPC_GS_CLASS_VECTOR = 0x10,
145 	KVMPPC_GS_CLASS_INTR = 0x20,
146 };
147 
148 /**
149  * Types of guest state buffer elements
150  */
151 enum {
152 	KVMPPC_GSE_BE32,
153 	KVMPPC_GSE_BE64,
154 	KVMPPC_GSE_VEC128,
155 	KVMPPC_GSE_PARTITION_TABLE,
156 	KVMPPC_GSE_PROCESS_TABLE,
157 	KVMPPC_GSE_BUFFER,
158 	__KVMPPC_GSE_TYPE_MAX,
159 };
160 
161 /**
162  * Flags for guest state elements
163  */
164 enum {
165 	KVMPPC_GS_FLAGS_WIDE = 0x01,
166 };
167 
168 /**
169  * struct kvmppc_gs_part_table - deserialized partition table information
170  * element
171  * @address: start of the partition table
172  * @ea_bits: number of bits in the effective address
173  * @gpd_size: root page directory size
174  */
175 struct kvmppc_gs_part_table {
176 	u64 address;
177 	u64 ea_bits;
178 	u64 gpd_size;
179 };
180 
181 /**
182  * struct kvmppc_gs_proc_table - deserialized process table information element
183  * @address: start of the process table
184  * @gpd_size: process table size
185  */
186 struct kvmppc_gs_proc_table {
187 	u64 address;
188 	u64 gpd_size;
189 };
190 
191 /**
192  * struct kvmppc_gs_buff_info - deserialized meta guest state buffer information
193  * @address: start of the guest state buffer
194  * @size: size of the guest state buffer
195  */
196 struct kvmppc_gs_buff_info {
197 	u64 address;
198 	u64 size;
199 };
200 
201 /**
202  * struct kvmppc_gs_header - serialized guest state buffer header
203  * @nelem: count of guest state elements in the buffer
204  * @data: start of the stream of elements in the buffer
205  */
206 struct kvmppc_gs_header {
207 	__be32 nelems;
208 	char data[];
209 } __packed;
210 
211 /**
212  * struct kvmppc_gs_elem - serialized guest state buffer element
213  * @iden: Guest State ID
214  * @len: length of data
215  * @data: the guest state buffer element's value
216  */
217 struct kvmppc_gs_elem {
218 	__be16 iden;
219 	__be16 len;
220 	char data[];
221 } __packed;
222 
223 /**
224  * struct kvmppc_gs_buff - a guest state buffer with metadata.
225  * @capacity: total length of the buffer
226  * @len: current length of the elements and header
227  * @guest_id: guest id associated with the buffer
228  * @vcpu_id: vcpu_id associated with the buffer
229  * @hdr: the serialised guest state buffer
230  */
231 struct kvmppc_gs_buff {
232 	size_t capacity;
233 	size_t len;
234 	unsigned long guest_id;
235 	unsigned long vcpu_id;
236 	struct kvmppc_gs_header *hdr;
237 };
238 
239 /**
240  * struct kvmppc_gs_bitmap - a bitmap for element ids
241  * @bitmap: a bitmap large enough for all Guest State IDs
242  */
243 struct kvmppc_gs_bitmap {
244 	/* private: */
245 	DECLARE_BITMAP(bitmap, KVMPPC_GSE_IDEN_COUNT);
246 };
247 
248 /**
249  * struct kvmppc_gs_parser - a map of element ids to locations in a buffer
250  * @iterator: bitmap used for iterating
251  * @gses: contains the pointers to elements
252  *
253  * A guest state parser is used for deserialising a guest state buffer.
254  * Given a buffer, it then allows looking up guest state elements using
255  * a guest state id.
256  */
257 struct kvmppc_gs_parser {
258 	/* private: */
259 	struct kvmppc_gs_bitmap iterator;
260 	struct kvmppc_gs_elem *gses[KVMPPC_GSE_IDEN_COUNT];
261 };
262 
263 enum {
264 	GSM_GUEST_WIDE = 0x1,
265 	GSM_SEND = 0x2,
266 	GSM_RECEIVE = 0x4,
267 	GSM_GSB_OWNER = 0x8,
268 };
269 
270 struct kvmppc_gs_msg;
271 
272 /**
273  * struct kvmppc_gs_msg_ops - guest state message behavior
274  * @get_size: maximum size required for the message data
275  * @fill_info: serializes to the guest state buffer format
276  * @refresh_info: dserializes from the guest state buffer format
277  */
278 struct kvmppc_gs_msg_ops {
279 	size_t (*get_size)(struct kvmppc_gs_msg *gsm);
280 	int (*fill_info)(struct kvmppc_gs_buff *gsb, struct kvmppc_gs_msg *gsm);
281 	int (*refresh_info)(struct kvmppc_gs_msg *gsm,
282 			    struct kvmppc_gs_buff *gsb);
283 };
284 
285 /**
286  * struct kvmppc_gs_msg - a guest state message
287  * @bitmap: the guest state ids that should be included
288  * @ops: modify message behavior for reading and writing to buffers
289  * @flags: guest wide or thread wide
290  * @data: location where buffer data will be written to or from.
291  *
292  * A guest state message is allows flexibility in sending in receiving data
293  * in a guest state buffer format.
294  */
295 struct kvmppc_gs_msg {
296 	struct kvmppc_gs_bitmap bitmap;
297 	struct kvmppc_gs_msg_ops *ops;
298 	unsigned long flags;
299 	void *data;
300 };
301 
302 /**************************************************************************
303  * Guest State IDs
304  **************************************************************************/
305 
306 u16 kvmppc_gsid_size(u16 iden);
307 unsigned long kvmppc_gsid_flags(u16 iden);
308 u64 kvmppc_gsid_mask(u16 iden);
309 
310 /**************************************************************************
311  * Guest State Buffers
312  **************************************************************************/
313 struct kvmppc_gs_buff *kvmppc_gsb_new(size_t size, unsigned long guest_id,
314 				      unsigned long vcpu_id, gfp_t flags);
315 void kvmppc_gsb_free(struct kvmppc_gs_buff *gsb);
316 void *kvmppc_gsb_put(struct kvmppc_gs_buff *gsb, size_t size);
317 int kvmppc_gsb_send(struct kvmppc_gs_buff *gsb, unsigned long flags);
318 int kvmppc_gsb_recv(struct kvmppc_gs_buff *gsb, unsigned long flags);
319 
320 /**
321  * kvmppc_gsb_header() - the header of a guest state buffer
322  * @gsb: guest state buffer
323  *
324  * Returns a pointer to the buffer header.
325  */
326 static inline struct kvmppc_gs_header *
327 kvmppc_gsb_header(struct kvmppc_gs_buff *gsb)
328 {
329 	return gsb->hdr;
330 }
331 
332 /**
333  * kvmppc_gsb_data() - the elements of a guest state buffer
334  * @gsb: guest state buffer
335  *
336  * Returns a pointer to the first element of the buffer data.
337  */
338 static inline struct kvmppc_gs_elem *kvmppc_gsb_data(struct kvmppc_gs_buff *gsb)
339 {
340 	return (struct kvmppc_gs_elem *)kvmppc_gsb_header(gsb)->data;
341 }
342 
343 /**
344  * kvmppc_gsb_len() - the current length of a guest state buffer
345  * @gsb: guest state buffer
346  *
347  * Returns the length including the header of a buffer.
348  */
349 static inline size_t kvmppc_gsb_len(struct kvmppc_gs_buff *gsb)
350 {
351 	return gsb->len;
352 }
353 
354 /**
355  * kvmppc_gsb_capacity() - the capacity of a guest state buffer
356  * @gsb: guest state buffer
357  *
358  * Returns the capacity of a buffer.
359  */
360 static inline size_t kvmppc_gsb_capacity(struct kvmppc_gs_buff *gsb)
361 {
362 	return gsb->capacity;
363 }
364 
365 /**
366  * kvmppc_gsb_paddress() - the physical address of buffer
367  * @gsb: guest state buffer
368  *
369  * Returns the physical address of the buffer.
370  */
371 static inline u64 kvmppc_gsb_paddress(struct kvmppc_gs_buff *gsb)
372 {
373 	return __pa(kvmppc_gsb_header(gsb));
374 }
375 
376 /**
377  * kvmppc_gsb_nelems() - the number of elements in a buffer
378  * @gsb: guest state buffer
379  *
380  * Returns the number of elements in a buffer
381  */
382 static inline u32 kvmppc_gsb_nelems(struct kvmppc_gs_buff *gsb)
383 {
384 	return be32_to_cpu(kvmppc_gsb_header(gsb)->nelems);
385 }
386 
387 /**
388  * kvmppc_gsb_reset() - empty a guest state buffer
389  * @gsb: guest state buffer
390  *
391  * Reset the number of elements and length of buffer to empty.
392  */
393 static inline void kvmppc_gsb_reset(struct kvmppc_gs_buff *gsb)
394 {
395 	kvmppc_gsb_header(gsb)->nelems = cpu_to_be32(0);
396 	gsb->len = sizeof(struct kvmppc_gs_header);
397 }
398 
399 /**
400  * kvmppc_gsb_data_len() - the length of a buffer excluding the header
401  * @gsb: guest state buffer
402  *
403  * Returns the length of a buffer excluding the header
404  */
405 static inline size_t kvmppc_gsb_data_len(struct kvmppc_gs_buff *gsb)
406 {
407 	return gsb->len - sizeof(struct kvmppc_gs_header);
408 }
409 
410 /**
411  * kvmppc_gsb_data_cap() - the capacity of a buffer excluding the header
412  * @gsb: guest state buffer
413  *
414  * Returns the capacity of a buffer excluding the header
415  */
416 static inline size_t kvmppc_gsb_data_cap(struct kvmppc_gs_buff *gsb)
417 {
418 	return gsb->capacity - sizeof(struct kvmppc_gs_header);
419 }
420 
421 /**
422  * kvmppc_gsb_for_each_elem - iterate over the elements in a buffer
423  * @i: loop counter
424  * @pos: set to current element
425  * @gsb: guest state buffer
426  * @rem: initialized to buffer capacity, holds bytes currently remaining in
427  *  stream
428  */
429 #define kvmppc_gsb_for_each_elem(i, pos, gsb, rem)               \
430 	kvmppc_gse_for_each_elem(i, kvmppc_gsb_nelems(gsb), pos, \
431 				 kvmppc_gsb_data(gsb),           \
432 				 kvmppc_gsb_data_cap(gsb), rem)
433 
434 /**************************************************************************
435  * Guest State Elements
436  **************************************************************************/
437 
438 /**
439  * kvmppc_gse_iden() - guest state ID of element
440  * @gse: guest state element
441  *
442  * Return the guest state ID in host endianness.
443  */
444 static inline u16 kvmppc_gse_iden(const struct kvmppc_gs_elem *gse)
445 {
446 	return be16_to_cpu(gse->iden);
447 }
448 
449 /**
450  * kvmppc_gse_len() - length of guest state element data
451  * @gse: guest state element
452  *
453  * Returns the length of guest state element data
454  */
455 static inline u16 kvmppc_gse_len(const struct kvmppc_gs_elem *gse)
456 {
457 	return be16_to_cpu(gse->len);
458 }
459 
460 /**
461  * kvmppc_gse_total_len() - total length of guest state element
462  * @gse: guest state element
463  *
464  * Returns the length of the data plus the ID and size header.
465  */
466 static inline u16 kvmppc_gse_total_len(const struct kvmppc_gs_elem *gse)
467 {
468 	return be16_to_cpu(gse->len) + sizeof(*gse);
469 }
470 
471 /**
472  * kvmppc_gse_total_size() - space needed for a given data length
473  * @size: data length
474  *
475  * Returns size plus the space needed for the ID and size header.
476  */
477 static inline u16 kvmppc_gse_total_size(u16 size)
478 {
479 	return sizeof(struct kvmppc_gs_elem) + size;
480 }
481 
482 /**
483  * kvmppc_gse_data() - pointer to data of a guest state element
484  * @gse: guest state element
485  *
486  * Returns a pointer to the beginning of guest state element data.
487  */
488 static inline void *kvmppc_gse_data(const struct kvmppc_gs_elem *gse)
489 {
490 	return (void *)gse->data;
491 }
492 
493 /**
494  * kvmppc_gse_ok() - checks space exists for guest state element
495  * @gse: guest state element
496  * @remaining: bytes of space remaining
497  *
498  * Returns true if the guest state element can fit in remaining space.
499  */
500 static inline bool kvmppc_gse_ok(const struct kvmppc_gs_elem *gse,
501 				 int remaining)
502 {
503 	return remaining >= kvmppc_gse_total_len(gse);
504 }
505 
506 /**
507  * kvmppc_gse_next() - iterate to the next guest state element in a stream
508  * @gse: stream of guest state elements
509  * @remaining: length of the guest element stream
510  *
511  * Returns the next guest state element in a stream of elements. The length of
512  * the stream is updated in remaining.
513  */
514 static inline struct kvmppc_gs_elem *
515 kvmppc_gse_next(const struct kvmppc_gs_elem *gse, int *remaining)
516 {
517 	int len = sizeof(*gse) + kvmppc_gse_len(gse);
518 
519 	*remaining -= len;
520 	return (struct kvmppc_gs_elem *)(gse->data + kvmppc_gse_len(gse));
521 }
522 
523 /**
524  * kvmppc_gse_for_each_elem - iterate over a stream of guest state elements
525  * @i: loop counter
526  * @max: number of elements
527  * @pos: set to current element
528  * @head: head of elements
529  * @len: length of the stream
530  * @rem: initialized to len, holds bytes currently remaining elements
531  */
532 #define kvmppc_gse_for_each_elem(i, max, pos, head, len, rem)                  \
533 	for (i = 0, pos = head, rem = len; kvmppc_gse_ok(pos, rem) && i < max; \
534 	     pos = kvmppc_gse_next(pos, &(rem)), i++)
535 
536 int __kvmppc_gse_put(struct kvmppc_gs_buff *gsb, u16 iden, u16 size,
537 		     const void *data);
538 int kvmppc_gse_parse(struct kvmppc_gs_parser *gsp, struct kvmppc_gs_buff *gsb);
539 
540 /**
541  * kvmppc_gse_put_be32() - add a be32 guest state element to a buffer
542  * @gsb: guest state buffer to add element to
543  * @iden: guest state ID
544  * @val: big endian value
545  */
546 static inline int kvmppc_gse_put_be32(struct kvmppc_gs_buff *gsb, u16 iden,
547 				      __be32 val)
548 {
549 	__be32 tmp;
550 
551 	tmp = val;
552 	return __kvmppc_gse_put(gsb, iden, sizeof(__be32), &tmp);
553 }
554 
555 /**
556  * kvmppc_gse_put_u32() - add a host endian 32bit int guest state element to a
557  * buffer
558  * @gsb: guest state buffer to add element to
559  * @iden: guest state ID
560  * @val: host endian value
561  */
562 static inline int kvmppc_gse_put_u32(struct kvmppc_gs_buff *gsb, u16 iden,
563 				     u32 val)
564 {
565 	__be32 tmp;
566 
567 	val &= kvmppc_gsid_mask(iden);
568 	tmp = cpu_to_be32(val);
569 	return kvmppc_gse_put_be32(gsb, iden, tmp);
570 }
571 
572 /**
573  * kvmppc_gse_put_be64() - add a be64 guest state element to a buffer
574  * @gsb: guest state buffer to add element to
575  * @iden: guest state ID
576  * @val: big endian value
577  */
578 static inline int kvmppc_gse_put_be64(struct kvmppc_gs_buff *gsb, u16 iden,
579 				      __be64 val)
580 {
581 	__be64 tmp;
582 
583 	tmp = val;
584 	return __kvmppc_gse_put(gsb, iden, sizeof(__be64), &tmp);
585 }
586 
587 /**
588  * kvmppc_gse_put_u64() - add a host endian 64bit guest state element to a
589  * buffer
590  * @gsb: guest state buffer to add element to
591  * @iden: guest state ID
592  * @val: host endian value
593  */
594 static inline int kvmppc_gse_put_u64(struct kvmppc_gs_buff *gsb, u16 iden,
595 				     u64 val)
596 {
597 	__be64 tmp;
598 
599 	val &= kvmppc_gsid_mask(iden);
600 	tmp = cpu_to_be64(val);
601 	return kvmppc_gse_put_be64(gsb, iden, tmp);
602 }
603 
604 /**
605  * __kvmppc_gse_put_reg() - add a register type guest state element to a buffer
606  * @gsb: guest state buffer to add element to
607  * @iden: guest state ID
608  * @val: host endian value
609  *
610  * Adds a register type guest state element. Uses the guest state ID for
611  * determining the length of the guest element. If the guest state ID has
612  * bits that can not be set they will be cleared.
613  */
614 static inline int __kvmppc_gse_put_reg(struct kvmppc_gs_buff *gsb, u16 iden,
615 				       u64 val)
616 {
617 	val &= kvmppc_gsid_mask(iden);
618 	if (kvmppc_gsid_size(iden) == sizeof(u64))
619 		return kvmppc_gse_put_u64(gsb, iden, val);
620 
621 	if (kvmppc_gsid_size(iden) == sizeof(u32)) {
622 		u32 tmp;
623 
624 		tmp = (u32)val;
625 		if (tmp != val)
626 			return -EINVAL;
627 
628 		return kvmppc_gse_put_u32(gsb, iden, tmp);
629 	}
630 	return -EINVAL;
631 }
632 
633 /**
634  * kvmppc_gse_put_vector128() - add a vector guest state element to a buffer
635  * @gsb: guest state buffer to add element to
636  * @iden: guest state ID
637  * @val: 16 byte vector value
638  */
639 static inline int kvmppc_gse_put_vector128(struct kvmppc_gs_buff *gsb, u16 iden,
640 					   vector128 *val)
641 {
642 	__be64 tmp[2] = { 0 };
643 	union {
644 		__vector128 v;
645 		u64 dw[2];
646 	} u;
647 
648 	u.v = *val;
649 	tmp[0] = cpu_to_be64(u.dw[TS_FPROFFSET]);
650 #ifdef CONFIG_VSX
651 	tmp[1] = cpu_to_be64(u.dw[TS_VSRLOWOFFSET]);
652 #endif
653 	return __kvmppc_gse_put(gsb, iden, sizeof(tmp), &tmp);
654 }
655 
656 /**
657  * kvmppc_gse_put_part_table() - add a partition table guest state element to a
658  * buffer
659  * @gsb: guest state buffer to add element to
660  * @iden: guest state ID
661  * @val: partition table value
662  */
663 static inline int kvmppc_gse_put_part_table(struct kvmppc_gs_buff *gsb,
664 					    u16 iden,
665 					    struct kvmppc_gs_part_table val)
666 {
667 	__be64 tmp[3];
668 
669 	tmp[0] = cpu_to_be64(val.address);
670 	tmp[1] = cpu_to_be64(val.ea_bits);
671 	tmp[2] = cpu_to_be64(val.gpd_size);
672 	return __kvmppc_gse_put(gsb, KVMPPC_GSID_PARTITION_TABLE, sizeof(tmp),
673 				&tmp);
674 }
675 
676 /**
677  * kvmppc_gse_put_proc_table() - add a process table guest state element to a
678  * buffer
679  * @gsb: guest state buffer to add element to
680  * @iden: guest state ID
681  * @val: process table value
682  */
683 static inline int kvmppc_gse_put_proc_table(struct kvmppc_gs_buff *gsb,
684 					    u16 iden,
685 					    struct kvmppc_gs_proc_table val)
686 {
687 	__be64 tmp[2];
688 
689 	tmp[0] = cpu_to_be64(val.address);
690 	tmp[1] = cpu_to_be64(val.gpd_size);
691 	return __kvmppc_gse_put(gsb, KVMPPC_GSID_PROCESS_TABLE, sizeof(tmp),
692 				&tmp);
693 }
694 
695 /**
696  * kvmppc_gse_put_buff_info() - adds a GSB description guest state element to a
697  * buffer
698  * @gsb: guest state buffer to add element to
699  * @iden: guest state ID
700  * @val: guest state buffer description value
701  */
702 static inline int kvmppc_gse_put_buff_info(struct kvmppc_gs_buff *gsb, u16 iden,
703 					   struct kvmppc_gs_buff_info val)
704 {
705 	__be64 tmp[2];
706 
707 	tmp[0] = cpu_to_be64(val.address);
708 	tmp[1] = cpu_to_be64(val.size);
709 	return __kvmppc_gse_put(gsb, iden, sizeof(tmp), &tmp);
710 }
711 
712 int __kvmppc_gse_put(struct kvmppc_gs_buff *gsb, u16 iden, u16 size,
713 		     const void *data);
714 
715 /**
716  * kvmppc_gse_get_be32() - return the data of a be32 element
717  * @gse: guest state element
718  */
719 static inline __be32 kvmppc_gse_get_be32(const struct kvmppc_gs_elem *gse)
720 {
721 	if (WARN_ON(kvmppc_gse_len(gse) != sizeof(__be32)))
722 		return 0;
723 	return *(__be32 *)kvmppc_gse_data(gse);
724 }
725 
726 /**
727  * kvmppc_gse_get_u32() - return the data of a be32 element in host endianness
728  * @gse: guest state element
729  */
730 static inline u32 kvmppc_gse_get_u32(const struct kvmppc_gs_elem *gse)
731 {
732 	return be32_to_cpu(kvmppc_gse_get_be32(gse));
733 }
734 
735 /**
736  * kvmppc_gse_get_be64() - return the data of a be64 element
737  * @gse: guest state element
738  */
739 static inline __be64 kvmppc_gse_get_be64(const struct kvmppc_gs_elem *gse)
740 {
741 	if (WARN_ON(kvmppc_gse_len(gse) != sizeof(__be64)))
742 		return 0;
743 	return *(__be64 *)kvmppc_gse_data(gse);
744 }
745 
746 /**
747  * kvmppc_gse_get_u64() - return the data of a be64 element in host endianness
748  * @gse: guest state element
749  */
750 static inline u64 kvmppc_gse_get_u64(const struct kvmppc_gs_elem *gse)
751 {
752 	return be64_to_cpu(kvmppc_gse_get_be64(gse));
753 }
754 
755 /**
756  * kvmppc_gse_get_vector128() - return the data of a vector element
757  * @gse: guest state element
758  */
759 static inline void kvmppc_gse_get_vector128(const struct kvmppc_gs_elem *gse,
760 					    vector128 *v)
761 {
762 	union {
763 		__vector128 v;
764 		u64 dw[2];
765 	} u = { 0 };
766 	__be64 *src;
767 
768 	if (WARN_ON(kvmppc_gse_len(gse) != sizeof(__vector128)))
769 		*v = u.v;
770 
771 	src = (__be64 *)kvmppc_gse_data(gse);
772 	u.dw[TS_FPROFFSET] = be64_to_cpu(src[0]);
773 #ifdef CONFIG_VSX
774 	u.dw[TS_VSRLOWOFFSET] = be64_to_cpu(src[1]);
775 #endif
776 	*v = u.v;
777 }
778 
779 /**************************************************************************
780  * Guest State Bitmap
781  **************************************************************************/
782 
783 bool kvmppc_gsbm_test(struct kvmppc_gs_bitmap *gsbm, u16 iden);
784 void kvmppc_gsbm_set(struct kvmppc_gs_bitmap *gsbm, u16 iden);
785 void kvmppc_gsbm_clear(struct kvmppc_gs_bitmap *gsbm, u16 iden);
786 u16 kvmppc_gsbm_next(struct kvmppc_gs_bitmap *gsbm, u16 prev);
787 
788 /**
789  * kvmppc_gsbm_zero - zero the entire bitmap
790  * @gsbm: guest state buffer bitmap
791  */
792 static inline void kvmppc_gsbm_zero(struct kvmppc_gs_bitmap *gsbm)
793 {
794 	bitmap_zero(gsbm->bitmap, KVMPPC_GSE_IDEN_COUNT);
795 }
796 
797 /**
798  * kvmppc_gsbm_fill - fill the entire bitmap
799  * @gsbm: guest state buffer bitmap
800  */
801 static inline void kvmppc_gsbm_fill(struct kvmppc_gs_bitmap *gsbm)
802 {
803 	bitmap_fill(gsbm->bitmap, KVMPPC_GSE_IDEN_COUNT);
804 	clear_bit(0, gsbm->bitmap);
805 }
806 
807 /**
808  * kvmppc_gsbm_for_each - iterate the present guest state IDs
809  * @gsbm: guest state buffer bitmap
810  * @iden: current guest state ID
811  */
812 #define kvmppc_gsbm_for_each(gsbm, iden)                  \
813 	for (iden = kvmppc_gsbm_next(gsbm, 0); iden != 0; \
814 	     iden = kvmppc_gsbm_next(gsbm, iden))
815 
816 /**************************************************************************
817  * Guest State Parser
818  **************************************************************************/
819 
820 void kvmppc_gsp_insert(struct kvmppc_gs_parser *gsp, u16 iden,
821 		       struct kvmppc_gs_elem *gse);
822 struct kvmppc_gs_elem *kvmppc_gsp_lookup(struct kvmppc_gs_parser *gsp,
823 					 u16 iden);
824 
825 /**
826  * kvmppc_gsp_for_each - iterate the <guest state IDs, guest state element>
827  * pairs
828  * @gsp: guest state buffer bitmap
829  * @iden: current guest state ID
830  * @gse: guest state element
831  */
832 #define kvmppc_gsp_for_each(gsp, iden, gse)                              \
833 	for (iden = kvmppc_gsbm_next(&(gsp)->iterator, 0),               \
834 	    gse = kvmppc_gsp_lookup((gsp), iden);                        \
835 	     iden != 0; iden = kvmppc_gsbm_next(&(gsp)->iterator, iden), \
836 	    gse = kvmppc_gsp_lookup((gsp), iden))
837 
838 /**************************************************************************
839  * Guest State Message
840  **************************************************************************/
841 
842 /**
843  * kvmppc_gsm_for_each - iterate the guest state IDs included in a guest state
844  * message
845  * @gsp: guest state buffer bitmap
846  * @iden: current guest state ID
847  * @gse: guest state element
848  */
849 #define kvmppc_gsm_for_each(gsm, iden)                            \
850 	for (iden = kvmppc_gsbm_next(&gsm->bitmap, 0); iden != 0; \
851 	     iden = kvmppc_gsbm_next(&gsm->bitmap, iden))
852 
853 int kvmppc_gsm_init(struct kvmppc_gs_msg *mgs, struct kvmppc_gs_msg_ops *ops,
854 		    void *data, unsigned long flags);
855 
856 struct kvmppc_gs_msg *kvmppc_gsm_new(struct kvmppc_gs_msg_ops *ops, void *data,
857 				     unsigned long flags, gfp_t gfp_flags);
858 void kvmppc_gsm_free(struct kvmppc_gs_msg *gsm);
859 size_t kvmppc_gsm_size(struct kvmppc_gs_msg *gsm);
860 int kvmppc_gsm_fill_info(struct kvmppc_gs_msg *gsm, struct kvmppc_gs_buff *gsb);
861 int kvmppc_gsm_refresh_info(struct kvmppc_gs_msg *gsm,
862 			    struct kvmppc_gs_buff *gsb);
863 
864 /**
865  * kvmppc_gsm_include - indicate a guest state ID should be included when
866  * serializing
867  * @gsm: guest state message
868  * @iden: guest state ID
869  */
870 static inline void kvmppc_gsm_include(struct kvmppc_gs_msg *gsm, u16 iden)
871 {
872 	kvmppc_gsbm_set(&gsm->bitmap, iden);
873 }
874 
875 /**
876  * kvmppc_gsm_includes - check if a guest state ID will be included when
877  * serializing
878  * @gsm: guest state message
879  * @iden: guest state ID
880  */
881 static inline bool kvmppc_gsm_includes(struct kvmppc_gs_msg *gsm, u16 iden)
882 {
883 	return kvmppc_gsbm_test(&gsm->bitmap, iden);
884 }
885 
886 /**
887  * kvmppc_gsm_includes - indicate all guest state IDs should be included when
888  * serializing
889  * @gsm: guest state message
890  * @iden: guest state ID
891  */
892 static inline void kvmppc_gsm_include_all(struct kvmppc_gs_msg *gsm)
893 {
894 	kvmppc_gsbm_fill(&gsm->bitmap);
895 }
896 
897 /**
898  * kvmppc_gsm_include - clear the guest state IDs that should be included when
899  * serializing
900  * @gsm: guest state message
901  */
902 static inline void kvmppc_gsm_reset(struct kvmppc_gs_msg *gsm)
903 {
904 	kvmppc_gsbm_zero(&gsm->bitmap);
905 }
906 
907 /**
908  * kvmppc_gsb_receive_data - flexibly update values from a guest state buffer
909  * @gsb: guest state buffer
910  * @gsm: guest state message
911  *
912  * Requests updated values for the guest state values included in the guest
913  * state message. The guest state message will then deserialize the guest state
914  * buffer.
915  */
916 static inline int kvmppc_gsb_receive_data(struct kvmppc_gs_buff *gsb,
917 					  struct kvmppc_gs_msg *gsm)
918 {
919 	int rc;
920 
921 	kvmppc_gsb_reset(gsb);
922 	rc = kvmppc_gsm_fill_info(gsm, gsb);
923 	if (rc < 0)
924 		return rc;
925 
926 	rc = kvmppc_gsb_recv(gsb, gsm->flags);
927 	if (rc < 0)
928 		return rc;
929 
930 	rc = kvmppc_gsm_refresh_info(gsm, gsb);
931 	if (rc < 0)
932 		return rc;
933 	return 0;
934 }
935 
936 /**
937  * kvmppc_gsb_recv - receive a single guest state ID
938  * @gsb: guest state buffer
939  * @gsm: guest state message
940  * @iden: guest state identity
941  */
942 static inline int kvmppc_gsb_receive_datum(struct kvmppc_gs_buff *gsb,
943 					   struct kvmppc_gs_msg *gsm, u16 iden)
944 {
945 	int rc;
946 
947 	kvmppc_gsm_include(gsm, iden);
948 	rc = kvmppc_gsb_receive_data(gsb, gsm);
949 	if (rc < 0)
950 		return rc;
951 	kvmppc_gsm_reset(gsm);
952 	return 0;
953 }
954 
955 /**
956  * kvmppc_gsb_send_data - flexibly send values from a guest state buffer
957  * @gsb: guest state buffer
958  * @gsm: guest state message
959  *
960  * Sends the guest state values included in the guest state message.
961  */
962 static inline int kvmppc_gsb_send_data(struct kvmppc_gs_buff *gsb,
963 				       struct kvmppc_gs_msg *gsm)
964 {
965 	int rc;
966 
967 	kvmppc_gsb_reset(gsb);
968 	rc = kvmppc_gsm_fill_info(gsm, gsb);
969 	if (rc < 0)
970 		return rc;
971 	rc = kvmppc_gsb_send(gsb, gsm->flags);
972 
973 	return rc;
974 }
975 
976 /**
977  * kvmppc_gsb_recv - send a single guest state ID
978  * @gsb: guest state buffer
979  * @gsm: guest state message
980  * @iden: guest state identity
981  */
982 static inline int kvmppc_gsb_send_datum(struct kvmppc_gs_buff *gsb,
983 					struct kvmppc_gs_msg *gsm, u16 iden)
984 {
985 	int rc;
986 
987 	kvmppc_gsm_include(gsm, iden);
988 	rc = kvmppc_gsb_send_data(gsb, gsm);
989 	if (rc < 0)
990 		return rc;
991 	kvmppc_gsm_reset(gsm);
992 	return 0;
993 }
994 
995 #endif /* _ASM_POWERPC_GUEST_STATE_BUFFER_H */
996