xref: /openbmc/linux/include/linux/virtio_config.h (revision 6846d656)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_CONFIG_H
3 #define _LINUX_VIRTIO_CONFIG_H
4 
5 #include <linux/err.h>
6 #include <linux/bug.h>
7 #include <linux/virtio.h>
8 #include <linux/virtio_byteorder.h>
9 #include <linux/compiler_types.h>
10 #include <uapi/linux/virtio_config.h>
11 
12 struct irq_affinity;
13 
14 struct virtio_shm_region {
15 	u64 addr;
16 	u64 len;
17 };
18 
19 /**
20  * virtio_config_ops - operations for configuring a virtio device
21  * Note: Do not assume that a transport implements all of the operations
22  *       getting/setting a value as a simple read/write! Generally speaking,
23  *       any of @get/@set, @get_status/@set_status, or @get_features/
24  *       @finalize_features are NOT safe to be called from an atomic
25  *       context.
26  * @enable_cbs: enable the callbacks
27  *      vdev: the virtio_device
28  * @get: read the value of a configuration field
29  *	vdev: the virtio_device
30  *	offset: the offset of the configuration field
31  *	buf: the buffer to write the field value into.
32  *	len: the length of the buffer
33  * @set: write the value of a configuration field
34  *	vdev: the virtio_device
35  *	offset: the offset of the configuration field
36  *	buf: the buffer to read the field value from.
37  *	len: the length of the buffer
38  * @generation: config generation counter (optional)
39  *	vdev: the virtio_device
40  *	Returns the config generation counter
41  * @get_status: read the status byte
42  *	vdev: the virtio_device
43  *	Returns the status byte
44  * @set_status: write the status byte
45  *	vdev: the virtio_device
46  *	status: the new status byte
47  * @reset: reset the device
48  *	vdev: the virtio device
49  *	After this, status and feature negotiation must be done again
50  *	Device must not be reset from its vq/config callbacks, or in
51  *	parallel with being added/removed.
52  * @find_vqs: find virtqueues and instantiate them.
53  *	vdev: the virtio_device
54  *	nvqs: the number of virtqueues to find
55  *	vqs: on success, includes new virtqueues
56  *	callbacks: array of callbacks, for each virtqueue
57  *		include a NULL entry for vqs that do not need a callback
58  *	names: array of virtqueue names (mainly for debugging)
59  *		include a NULL entry for vqs unused by driver
60  *	Returns 0 on success or error status
61  * @del_vqs: free virtqueues found by find_vqs().
62  * @get_features: get the array of feature bits for this device.
63  *	vdev: the virtio_device
64  *	Returns the first 64 feature bits (all we currently need).
65  * @finalize_features: confirm what device features we'll be using.
66  *	vdev: the virtio_device
67  *	This sends the driver feature bits to the device: it can change
68  *	the dev->feature bits if it wants.
69  * Note: despite the name this can be called any number of times.
70  *	Returns 0 on success or error status
71  * @bus_name: return the bus name associated with the device (optional)
72  *	vdev: the virtio_device
73  *      This returns a pointer to the bus name a la pci_name from which
74  *      the caller can then copy.
75  * @set_vq_affinity: set the affinity for a virtqueue (optional).
76  * @get_vq_affinity: get the affinity for a virtqueue (optional).
77  * @get_shm_region: get a shared memory region based on the index.
78  */
79 typedef void vq_callback_t(struct virtqueue *);
80 struct virtio_config_ops {
81 	void (*enable_cbs)(struct virtio_device *vdev);
82 	void (*get)(struct virtio_device *vdev, unsigned offset,
83 		    void *buf, unsigned len);
84 	void (*set)(struct virtio_device *vdev, unsigned offset,
85 		    const void *buf, unsigned len);
86 	u32 (*generation)(struct virtio_device *vdev);
87 	u8 (*get_status)(struct virtio_device *vdev);
88 	void (*set_status)(struct virtio_device *vdev, u8 status);
89 	void (*reset)(struct virtio_device *vdev);
90 	int (*find_vqs)(struct virtio_device *, unsigned nvqs,
91 			struct virtqueue *vqs[], vq_callback_t *callbacks[],
92 			const char * const names[], const bool *ctx,
93 			struct irq_affinity *desc);
94 	void (*del_vqs)(struct virtio_device *);
95 	u64 (*get_features)(struct virtio_device *vdev);
96 	int (*finalize_features)(struct virtio_device *vdev);
97 	const char *(*bus_name)(struct virtio_device *vdev);
98 	int (*set_vq_affinity)(struct virtqueue *vq,
99 			       const struct cpumask *cpu_mask);
100 	const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
101 			int index);
102 	bool (*get_shm_region)(struct virtio_device *vdev,
103 			       struct virtio_shm_region *region, u8 id);
104 };
105 
106 /* If driver didn't advertise the feature, it will never appear. */
107 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
108 					 unsigned int fbit);
109 
110 /**
111  * __virtio_test_bit - helper to test feature bits. For use by transports.
112  *                     Devices should normally use virtio_has_feature,
113  *                     which includes more checks.
114  * @vdev: the device
115  * @fbit: the feature bit
116  */
117 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
118 				     unsigned int fbit)
119 {
120 	/* Did you forget to fix assumptions on max features? */
121 	if (__builtin_constant_p(fbit))
122 		BUILD_BUG_ON(fbit >= 64);
123 	else
124 		BUG_ON(fbit >= 64);
125 
126 	return vdev->features & BIT_ULL(fbit);
127 }
128 
129 /**
130  * __virtio_set_bit - helper to set feature bits. For use by transports.
131  * @vdev: the device
132  * @fbit: the feature bit
133  */
134 static inline void __virtio_set_bit(struct virtio_device *vdev,
135 				    unsigned int fbit)
136 {
137 	/* Did you forget to fix assumptions on max features? */
138 	if (__builtin_constant_p(fbit))
139 		BUILD_BUG_ON(fbit >= 64);
140 	else
141 		BUG_ON(fbit >= 64);
142 
143 	vdev->features |= BIT_ULL(fbit);
144 }
145 
146 /**
147  * __virtio_clear_bit - helper to clear feature bits. For use by transports.
148  * @vdev: the device
149  * @fbit: the feature bit
150  */
151 static inline void __virtio_clear_bit(struct virtio_device *vdev,
152 				      unsigned int fbit)
153 {
154 	/* Did you forget to fix assumptions on max features? */
155 	if (__builtin_constant_p(fbit))
156 		BUILD_BUG_ON(fbit >= 64);
157 	else
158 		BUG_ON(fbit >= 64);
159 
160 	vdev->features &= ~BIT_ULL(fbit);
161 }
162 
163 /**
164  * virtio_has_feature - helper to determine if this device has this feature.
165  * @vdev: the device
166  * @fbit: the feature bit
167  */
168 static inline bool virtio_has_feature(const struct virtio_device *vdev,
169 				      unsigned int fbit)
170 {
171 	if (fbit < VIRTIO_TRANSPORT_F_START)
172 		virtio_check_driver_offered_feature(vdev, fbit);
173 
174 	return __virtio_test_bit(vdev, fbit);
175 }
176 
177 /**
178  * virtio_has_dma_quirk - determine whether this device has the DMA quirk
179  * @vdev: the device
180  */
181 static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)
182 {
183 	/*
184 	 * Note the reverse polarity of the quirk feature (compared to most
185 	 * other features), this is for compatibility with legacy systems.
186 	 */
187 	return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);
188 }
189 
190 static inline
191 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
192 					vq_callback_t *c, const char *n)
193 {
194 	vq_callback_t *callbacks[] = { c };
195 	const char *names[] = { n };
196 	struct virtqueue *vq;
197 	int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
198 					 NULL);
199 	if (err < 0)
200 		return ERR_PTR(err);
201 	return vq;
202 }
203 
204 static inline
205 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
206 			struct virtqueue *vqs[], vq_callback_t *callbacks[],
207 			const char * const names[],
208 			struct irq_affinity *desc)
209 {
210 	return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
211 }
212 
213 static inline
214 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
215 			struct virtqueue *vqs[], vq_callback_t *callbacks[],
216 			const char * const names[], const bool *ctx,
217 			struct irq_affinity *desc)
218 {
219 	return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
220 				      desc);
221 }
222 
223 /**
224  * virtio_device_ready - enable vq use in probe function
225  * @vdev: the device
226  *
227  * Driver must call this to use vqs in the probe function.
228  *
229  * Note: vqs are enabled automatically after probe returns.
230  */
231 static inline
232 void virtio_device_ready(struct virtio_device *dev)
233 {
234 	unsigned status = dev->config->get_status(dev);
235 
236 	if (dev->config->enable_cbs)
237                   dev->config->enable_cbs(dev);
238 
239 	BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
240 	dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
241 }
242 
243 static inline
244 const char *virtio_bus_name(struct virtio_device *vdev)
245 {
246 	if (!vdev->config->bus_name)
247 		return "virtio";
248 	return vdev->config->bus_name(vdev);
249 }
250 
251 /**
252  * virtqueue_set_affinity - setting affinity for a virtqueue
253  * @vq: the virtqueue
254  * @cpu: the cpu no.
255  *
256  * Pay attention the function are best-effort: the affinity hint may not be set
257  * due to config support, irq type and sharing.
258  *
259  */
260 static inline
261 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
262 {
263 	struct virtio_device *vdev = vq->vdev;
264 	if (vdev->config->set_vq_affinity)
265 		return vdev->config->set_vq_affinity(vq, cpu_mask);
266 	return 0;
267 }
268 
269 static inline
270 bool virtio_get_shm_region(struct virtio_device *vdev,
271 			   struct virtio_shm_region *region, u8 id)
272 {
273 	if (!vdev->config->get_shm_region)
274 		return false;
275 	return vdev->config->get_shm_region(vdev, region, id);
276 }
277 
278 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
279 {
280 	return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
281 		virtio_legacy_is_little_endian();
282 }
283 
284 /* Memory accessors */
285 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
286 {
287 	return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
288 }
289 
290 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
291 {
292 	return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
293 }
294 
295 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
296 {
297 	return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
298 }
299 
300 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
301 {
302 	return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
303 }
304 
305 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
306 {
307 	return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
308 }
309 
310 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
311 {
312 	return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
313 }
314 
315 #define virtio_to_cpu(vdev, x) \
316 	_Generic((x), \
317 		__u8: (x), \
318 		__virtio16: virtio16_to_cpu((vdev), (x)), \
319 		__virtio32: virtio32_to_cpu((vdev), (x)), \
320 		__virtio64: virtio64_to_cpu((vdev), (x)) \
321 		)
322 
323 #define cpu_to_virtio(vdev, x, m) \
324 	_Generic((m), \
325 		__u8: (x), \
326 		__virtio16: cpu_to_virtio16((vdev), (x)), \
327 		__virtio32: cpu_to_virtio32((vdev), (x)), \
328 		__virtio64: cpu_to_virtio64((vdev), (x)) \
329 		)
330 
331 #define __virtio_native_type(structname, member) \
332 	typeof(virtio_to_cpu(NULL, ((structname*)0)->member))
333 
334 /* Config space accessors. */
335 #define virtio_cread(vdev, structname, member, ptr)			\
336 	do {								\
337 		typeof(((structname*)0)->member) virtio_cread_v;	\
338 									\
339 		might_sleep();						\
340 		/* Sanity check: must match the member's type */	\
341 		typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \
342 									\
343 		switch (sizeof(virtio_cread_v)) {			\
344 		case 1:							\
345 		case 2:							\
346 		case 4:							\
347 			vdev->config->get((vdev), 			\
348 					  offsetof(structname, member), \
349 					  &virtio_cread_v,		\
350 					  sizeof(virtio_cread_v));	\
351 			break;						\
352 		default:						\
353 			__virtio_cread_many((vdev), 			\
354 					  offsetof(structname, member), \
355 					  &virtio_cread_v,		\
356 					  1,				\
357 					  sizeof(virtio_cread_v));	\
358 			break;						\
359 		}							\
360 		*(ptr) = virtio_to_cpu(vdev, virtio_cread_v);		\
361 	} while(0)
362 
363 /* Config space accessors. */
364 #define virtio_cwrite(vdev, structname, member, ptr)			\
365 	do {								\
366 		typeof(((structname*)0)->member) virtio_cwrite_v =	\
367 			cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \
368 									\
369 		might_sleep();						\
370 		/* Sanity check: must match the member's type */	\
371 		typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \
372 									\
373 		vdev->config->set((vdev), offsetof(structname, member),	\
374 				  &virtio_cwrite_v,			\
375 				  sizeof(virtio_cwrite_v));		\
376 	} while(0)
377 
378 /*
379  * Nothing virtio-specific about these, but let's worry about generalizing
380  * these later.
381  */
382 #define virtio_le_to_cpu(x) \
383 	_Generic((x), \
384 		__u8: (u8)(x), \
385 		 __le16: (u16)le16_to_cpu(x), \
386 		 __le32: (u32)le32_to_cpu(x), \
387 		 __le64: (u64)le64_to_cpu(x) \
388 		)
389 
390 #define virtio_cpu_to_le(x, m) \
391 	_Generic((m), \
392 		 __u8: (x), \
393 		 __le16: cpu_to_le16(x), \
394 		 __le32: cpu_to_le32(x), \
395 		 __le64: cpu_to_le64(x) \
396 		)
397 
398 /* LE (e.g. modern) Config space accessors. */
399 #define virtio_cread_le(vdev, structname, member, ptr)			\
400 	do {								\
401 		typeof(((structname*)0)->member) virtio_cread_v;	\
402 									\
403 		might_sleep();						\
404 		/* Sanity check: must match the member's type */	\
405 		typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \
406 									\
407 		switch (sizeof(virtio_cread_v)) {			\
408 		case 1:							\
409 		case 2:							\
410 		case 4:							\
411 			vdev->config->get((vdev), 			\
412 					  offsetof(structname, member), \
413 					  &virtio_cread_v,		\
414 					  sizeof(virtio_cread_v));	\
415 			break;						\
416 		default:						\
417 			__virtio_cread_many((vdev), 			\
418 					  offsetof(structname, member), \
419 					  &virtio_cread_v,		\
420 					  1,				\
421 					  sizeof(virtio_cread_v));	\
422 			break;						\
423 		}							\
424 		*(ptr) = virtio_le_to_cpu(virtio_cread_v);		\
425 	} while(0)
426 
427 #define virtio_cwrite_le(vdev, structname, member, ptr)			\
428 	do {								\
429 		typeof(((structname*)0)->member) virtio_cwrite_v =	\
430 			virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \
431 									\
432 		might_sleep();						\
433 		/* Sanity check: must match the member's type */	\
434 		typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \
435 									\
436 		vdev->config->set((vdev), offsetof(structname, member),	\
437 				  &virtio_cwrite_v,			\
438 				  sizeof(virtio_cwrite_v));		\
439 	} while(0)
440 
441 
442 /* Read @count fields, @bytes each. */
443 static inline void __virtio_cread_many(struct virtio_device *vdev,
444 				       unsigned int offset,
445 				       void *buf, size_t count, size_t bytes)
446 {
447 	u32 old, gen = vdev->config->generation ?
448 		vdev->config->generation(vdev) : 0;
449 	int i;
450 
451 	might_sleep();
452 	do {
453 		old = gen;
454 
455 		for (i = 0; i < count; i++)
456 			vdev->config->get(vdev, offset + bytes * i,
457 					  buf + i * bytes, bytes);
458 
459 		gen = vdev->config->generation ?
460 			vdev->config->generation(vdev) : 0;
461 	} while (gen != old);
462 }
463 
464 static inline void virtio_cread_bytes(struct virtio_device *vdev,
465 				      unsigned int offset,
466 				      void *buf, size_t len)
467 {
468 	__virtio_cread_many(vdev, offset, buf, len, 1);
469 }
470 
471 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
472 {
473 	u8 ret;
474 
475 	might_sleep();
476 	vdev->config->get(vdev, offset, &ret, sizeof(ret));
477 	return ret;
478 }
479 
480 static inline void virtio_cwrite8(struct virtio_device *vdev,
481 				  unsigned int offset, u8 val)
482 {
483 	might_sleep();
484 	vdev->config->set(vdev, offset, &val, sizeof(val));
485 }
486 
487 static inline u16 virtio_cread16(struct virtio_device *vdev,
488 				 unsigned int offset)
489 {
490 	__virtio16 ret;
491 
492 	might_sleep();
493 	vdev->config->get(vdev, offset, &ret, sizeof(ret));
494 	return virtio16_to_cpu(vdev, ret);
495 }
496 
497 static inline void virtio_cwrite16(struct virtio_device *vdev,
498 				   unsigned int offset, u16 val)
499 {
500 	__virtio16 v;
501 
502 	might_sleep();
503 	v = cpu_to_virtio16(vdev, val);
504 	vdev->config->set(vdev, offset, &v, sizeof(v));
505 }
506 
507 static inline u32 virtio_cread32(struct virtio_device *vdev,
508 				 unsigned int offset)
509 {
510 	__virtio32 ret;
511 
512 	might_sleep();
513 	vdev->config->get(vdev, offset, &ret, sizeof(ret));
514 	return virtio32_to_cpu(vdev, ret);
515 }
516 
517 static inline void virtio_cwrite32(struct virtio_device *vdev,
518 				   unsigned int offset, u32 val)
519 {
520 	__virtio32 v;
521 
522 	might_sleep();
523 	v = cpu_to_virtio32(vdev, val);
524 	vdev->config->set(vdev, offset, &v, sizeof(v));
525 }
526 
527 static inline u64 virtio_cread64(struct virtio_device *vdev,
528 				 unsigned int offset)
529 {
530 	__virtio64 ret;
531 
532 	__virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
533 	return virtio64_to_cpu(vdev, ret);
534 }
535 
536 static inline void virtio_cwrite64(struct virtio_device *vdev,
537 				   unsigned int offset, u64 val)
538 {
539 	__virtio64 v;
540 
541 	might_sleep();
542 	v = cpu_to_virtio64(vdev, val);
543 	vdev->config->set(vdev, offset, &v, sizeof(v));
544 }
545 
546 /* Conditional config space accessors. */
547 #define virtio_cread_feature(vdev, fbit, structname, member, ptr)	\
548 	({								\
549 		int _r = 0;						\
550 		if (!virtio_has_feature(vdev, fbit))			\
551 			_r = -ENOENT;					\
552 		else							\
553 			virtio_cread((vdev), structname, member, ptr);	\
554 		_r;							\
555 	})
556 
557 /* Conditional config space accessors. */
558 #define virtio_cread_le_feature(vdev, fbit, structname, member, ptr)	\
559 	({								\
560 		int _r = 0;						\
561 		if (!virtio_has_feature(vdev, fbit))			\
562 			_r = -ENOENT;					\
563 		else							\
564 			virtio_cread_le((vdev), structname, member, ptr); \
565 		_r;							\
566 	})
567 
568 #ifdef CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS
569 int arch_has_restricted_virtio_memory_access(void);
570 #else
571 static inline int arch_has_restricted_virtio_memory_access(void)
572 {
573 	return 0;
574 }
575 #endif /* CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS */
576 
577 #endif /* _LINUX_VIRTIO_CONFIG_H */
578