Commit a4235ec0 authored by Michael S. Tsirkin's avatar Michael S. Tsirkin

virtio: allow __virtioXX, __leXX in config space

Currently all config space fields are of the type __uXX.
This confuses people and some drivers (notably vdpa)
access them using CPU endian-ness - which only
works well for legacy or LE platforms.

Update virtio_cread/virtio_cwrite macros to allow __virtioXX
and __leXX field types. Follow-up patches will convert
config space to use these types.
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Acked-by: default avatarCornelia Huck <cohuck@redhat.com>
parent 54871968
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <linux/bug.h> #include <linux/bug.h>
#include <linux/virtio.h> #include <linux/virtio.h>
#include <linux/virtio_byteorder.h> #include <linux/virtio_byteorder.h>
#include <linux/compiler_types.h>
#include <uapi/linux/virtio_config.h> #include <uapi/linux/virtio_config.h>
struct irq_affinity; struct irq_affinity;
...@@ -287,12 +288,57 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) ...@@ -287,12 +288,57 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
} }
/*
* Only the checker differentiates between __virtioXX and __uXX types. But we
* try to share as much code as we can with the regular GCC build.
*/
#if !defined(CONFIG_CC_IS_GCC) && !defined(__CHECKER__)
/* Not a checker - we can keep things simple */
#define __virtio_native_typeof(x) typeof(x)
#else
/*
* We build this out of a couple of helper macros in a vain attempt to
* help you keep your lunch down while reading it.
*/
#define __virtio_pick_value(x, type, then, otherwise) \
__builtin_choose_expr(__same_type(x, type), then, otherwise)
#define __virtio_pick_type(x, type, then, otherwise) \
__virtio_pick_value(x, type, (then)0, otherwise)
#define __virtio_pick_endian(x, x16, x32, x64, otherwise) \
__virtio_pick_type(x, x16, __u16, \
__virtio_pick_type(x, x32, __u32, \
__virtio_pick_type(x, x64, __u64, \
otherwise)))
#define __virtio_native_typeof(x) typeof( \
__virtio_pick_type(x, __u8, __u8, \
__virtio_pick_endian(x, __virtio16, __virtio32, __virtio64, \
__virtio_pick_endian(x, __le16, __le32, __le64, \
__virtio_pick_endian(x, __u16, __u32, __u64, \
/* No other type allowed */ \
(void)0)))))
#endif
#define __virtio_native_type(structname, member) \
__virtio_native_typeof(((structname*)0)->member)
#define __virtio_typecheck(structname, member, val) \
/* Must match the member's type, and be integer */ \
typecheck(__virtio_native_type(structname, member), (val))
/* Config space accessors. */ /* Config space accessors. */
#define virtio_cread(vdev, structname, member, ptr) \ #define virtio_cread(vdev, structname, member, ptr) \
do { \ do { \
might_sleep(); \ might_sleep(); \
/* Must match the member's type, and be integer */ \ /* Must match the member's type, and be integer */ \
if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \ if (!__virtio_typecheck(structname, member, *(ptr))) \
(*ptr) = 1; \ (*ptr) = 1; \
\ \
switch (sizeof(*ptr)) { \ switch (sizeof(*ptr)) { \
...@@ -322,7 +368,7 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) ...@@ -322,7 +368,7 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
do { \ do { \
might_sleep(); \ might_sleep(); \
/* Must match the member's type, and be integer */ \ /* Must match the member's type, and be integer */ \
if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \ if (!__virtio_typecheck(structname, member, *(ptr))) \
BUG_ON((*ptr) == 1); \ BUG_ON((*ptr) == 1); \
\ \
switch (sizeof(*ptr)) { \ switch (sizeof(*ptr)) { \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment