diff options
Diffstat (limited to 'include/linux/virtio_config.h')
| -rw-r--r-- | include/linux/virtio_config.h | 187 | 
1 files changed, 134 insertions, 53 deletions
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index bb4cc4910750..8fe857e27ef3 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -6,6 +6,7 @@  #include <linux/bug.h>  #include <linux/virtio.h>  #include <linux/virtio_byteorder.h> +#include <linux/compiler_types.h>  #include <uapi/linux/virtio_config.h>  struct irq_affinity; @@ -162,16 +163,16 @@ static inline bool virtio_has_feature(const struct virtio_device *vdev,  }  /** - * virtio_has_iommu_quirk - determine whether this device has the iommu quirk + * virtio_has_dma_quirk - determine whether this device has the DMA quirk   * @vdev: the device   */ -static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev) +static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev)  {  	/*  	 * Note the reverse polarity of the quirk feature (compared to most  	 * other features), this is for compatibility with legacy systems.  	 */ -	return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); +	return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM);  }  static inline @@ -287,70 +288,133 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)  	return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);  } +#define virtio_to_cpu(vdev, x) \ +	_Generic((x), \ +		__u8: (x), \ +		__virtio16: virtio16_to_cpu((vdev), (x)), \ +		__virtio32: virtio32_to_cpu((vdev), (x)), \ +		__virtio64: virtio64_to_cpu((vdev), (x)) \ +		) + +#define cpu_to_virtio(vdev, x, m) \ +	_Generic((m), \ +		__u8: (x), \ +		__virtio16: cpu_to_virtio16((vdev), (x)), \ +		__virtio32: cpu_to_virtio32((vdev), (x)), \ +		__virtio64: cpu_to_virtio64((vdev), (x)) \ +		) + +#define __virtio_native_type(structname, member) \ +	typeof(virtio_to_cpu(NULL, ((structname*)0)->member)) +  /* Config space accessors. */  #define virtio_cread(vdev, structname, member, ptr)			\  	do {								\ +		typeof(((structname*)0)->member) virtio_cread_v;	\ +									\  		might_sleep();						\ -		/* Must match the member's type, and be integer */	\ -		if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \ -			(*ptr) = 1;					\ +		/* Sanity check: must match the member's type */	\ +		typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \  									\ -		switch (sizeof(*ptr)) {					\ +		switch (sizeof(virtio_cread_v)) {			\  		case 1:							\ -			*(ptr) = virtio_cread8(vdev,			\ -					       offsetof(structname, member)); \ -			break;						\  		case 2:							\ -			*(ptr) = virtio_cread16(vdev,			\ -						offsetof(structname, member)); \ -			break;						\  		case 4:							\ -			*(ptr) = virtio_cread32(vdev,			\ -						offsetof(structname, member)); \ -			break;						\ -		case 8:							\ -			*(ptr) = virtio_cread64(vdev,			\ -						offsetof(structname, member)); \ +			vdev->config->get((vdev), 			\ +					  offsetof(structname, member), \ +					  &virtio_cread_v,		\ +					  sizeof(virtio_cread_v));	\  			break;						\  		default:						\ -			BUG();						\ +			__virtio_cread_many((vdev), 			\ +					  offsetof(structname, member), \ +					  &virtio_cread_v,		\ +					  1,				\ +					  sizeof(virtio_cread_v));	\ +			break;						\  		}							\ +		*(ptr) = virtio_to_cpu(vdev, virtio_cread_v);		\  	} while(0)  /* Config space accessors. */  #define virtio_cwrite(vdev, structname, member, ptr)			\  	do {								\ +		typeof(((structname*)0)->member) virtio_cwrite_v =	\ +			cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \ +									\ +		might_sleep();						\ +		/* Sanity check: must match the member's type */	\ +		typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \ +									\ +		vdev->config->set((vdev), offsetof(structname, member),	\ +				  &virtio_cwrite_v,			\ +				  sizeof(virtio_cwrite_v));		\ +	} while(0) + +/* + * Nothing virtio-specific about these, but let's worry about generalizing + * these later. + */ +#define virtio_le_to_cpu(x) \ +	_Generic((x), \ +		__u8: (u8)(x), \ +		 __le16: (u16)le16_to_cpu(x), \ +		 __le32: (u32)le32_to_cpu(x), \ +		 __le64: (u64)le64_to_cpu(x) \ +		) + +#define virtio_cpu_to_le(x, m) \ +	_Generic((m), \ +		 __u8: (x), \ +		 __le16: cpu_to_le16(x), \ +		 __le32: cpu_to_le32(x), \ +		 __le64: cpu_to_le64(x) \ +		) + +/* LE (e.g. modern) Config space accessors. */ +#define virtio_cread_le(vdev, structname, member, ptr)			\ +	do {								\ +		typeof(((structname*)0)->member) virtio_cread_v;	\ +									\  		might_sleep();						\ -		/* Must match the member's type, and be integer */	\ -		if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \ -			BUG_ON((*ptr) == 1);				\ +		/* Sanity check: must match the member's type */	\ +		typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \  									\ -		switch (sizeof(*ptr)) {					\ +		switch (sizeof(virtio_cread_v)) {			\  		case 1:							\ -			virtio_cwrite8(vdev,				\ -				       offsetof(structname, member),	\ -				       *(ptr));				\ -			break;						\  		case 2:							\ -			virtio_cwrite16(vdev,				\ -					offsetof(structname, member),	\ -					*(ptr));			\ -			break;						\  		case 4:							\ -			virtio_cwrite32(vdev,				\ -					offsetof(structname, member),	\ -					*(ptr));			\ -			break;						\ -		case 8:							\ -			virtio_cwrite64(vdev,				\ -					offsetof(structname, member),	\ -					*(ptr));			\ +			vdev->config->get((vdev), 			\ +					  offsetof(structname, member), \ +					  &virtio_cread_v,		\ +					  sizeof(virtio_cread_v));	\  			break;						\  		default:						\ -			BUG();						\ +			__virtio_cread_many((vdev), 			\ +					  offsetof(structname, member), \ +					  &virtio_cread_v,		\ +					  1,				\ +					  sizeof(virtio_cread_v));	\ +			break;						\  		}							\ +		*(ptr) = virtio_le_to_cpu(virtio_cread_v);		\ +	} while(0) + +#define virtio_cwrite_le(vdev, structname, member, ptr)			\ +	do {								\ +		typeof(((structname*)0)->member) virtio_cwrite_v =	\ +			virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \ +									\ +		might_sleep();						\ +		/* Sanity check: must match the member's type */	\ +		typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \ +									\ +		vdev->config->set((vdev), offsetof(structname, member),	\ +				  &virtio_cwrite_v,			\ +				  sizeof(virtio_cwrite_v));		\  	} while(0) +  /* Read @count fields, @bytes each. */  static inline void __virtio_cread_many(struct virtio_device *vdev,  				       unsigned int offset, @@ -399,53 +463,60 @@ static inline void virtio_cwrite8(struct virtio_device *vdev,  static inline u16 virtio_cread16(struct virtio_device *vdev,  				 unsigned int offset)  { -	u16 ret; +	__virtio16 ret;  	might_sleep();  	vdev->config->get(vdev, offset, &ret, sizeof(ret)); -	return virtio16_to_cpu(vdev, (__force __virtio16)ret); +	return virtio16_to_cpu(vdev, ret);  }  static inline void virtio_cwrite16(struct virtio_device *vdev,  				   unsigned int offset, u16 val)  { +	__virtio16 v; +  	might_sleep(); -	val = (__force u16)cpu_to_virtio16(vdev, val); -	vdev->config->set(vdev, offset, &val, sizeof(val)); +	v = cpu_to_virtio16(vdev, val); +	vdev->config->set(vdev, offset, &v, sizeof(v));  }  static inline u32 virtio_cread32(struct virtio_device *vdev,  				 unsigned int offset)  { -	u32 ret; +	__virtio32 ret;  	might_sleep();  	vdev->config->get(vdev, offset, &ret, sizeof(ret)); -	return virtio32_to_cpu(vdev, (__force __virtio32)ret); +	return virtio32_to_cpu(vdev, ret);  }  static inline void virtio_cwrite32(struct virtio_device *vdev,  				   unsigned int offset, u32 val)  { +	__virtio32 v; +  	might_sleep(); -	val = (__force u32)cpu_to_virtio32(vdev, val); -	vdev->config->set(vdev, offset, &val, sizeof(val)); +	v = cpu_to_virtio32(vdev, val); +	vdev->config->set(vdev, offset, &v, sizeof(v));  }  static inline u64 virtio_cread64(struct virtio_device *vdev,  				 unsigned int offset)  { -	u64 ret; +	__virtio64 ret; +  	__virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); -	return virtio64_to_cpu(vdev, (__force __virtio64)ret); +	return virtio64_to_cpu(vdev, ret);  }  static inline void virtio_cwrite64(struct virtio_device *vdev,  				   unsigned int offset, u64 val)  { +	__virtio64 v; +  	might_sleep(); -	val = (__force u64)cpu_to_virtio64(vdev, val); -	vdev->config->set(vdev, offset, &val, sizeof(val)); +	v = cpu_to_virtio64(vdev, val); +	vdev->config->set(vdev, offset, &v, sizeof(v));  }  /* Conditional config space accessors. */ @@ -459,4 +530,14 @@ static inline void virtio_cwrite64(struct virtio_device *vdev,  		_r;							\  	}) +/* Conditional config space accessors. */ +#define virtio_cread_le_feature(vdev, fbit, structname, member, ptr)	\ +	({								\ +		int _r = 0;						\ +		if (!virtio_has_feature(vdev, fbit))			\ +			_r = -ENOENT;					\ +		else							\ +			virtio_cread_le((vdev), structname, member, ptr); \ +		_r;							\ +	})  #endif /* _LINUX_VIRTIO_CONFIG_H */  |