[SLOF] [PATCH v3 19/22] virtio: 1.0 helper to read 16/32/64 bit value

Thomas Huth thuth at redhat.com
Wed Jan 27 21:29:53 AEDT 2016


On 22.01.2016 11:55, Nikunj A Dadhania wrote:
> To avoid cluttering the driver code with modern/legacy code introduce
> virtio_cpu_to_modern{16,32,64} and virtio_modern{16,32,64}_to_cpu
> 
> Signed-off-by: Nikunj A Dadhania <nikunj at linux.vnet.ibm.com>
> Suggested-by: Thomas Huth <thuth at redhat.com>
> ---
>  lib/libvirtio/virtio.c | 30 ++++++++++++++++++++++++++++++
>  lib/libvirtio/virtio.h |  6 ++++++
>  2 files changed, 36 insertions(+)
> 
> diff --git a/lib/libvirtio/virtio.c b/lib/libvirtio/virtio.c
> index 0264baa..7bf6f42 100644
> --- a/lib/libvirtio/virtio.c
> +++ b/lib/libvirtio/virtio.c
> @@ -107,6 +107,36 @@ static uint64_t virtio_pci_read64(void *addr)
>  	return (hi << 32) | lo;
>  }
>  
> +uint16_t virtio_cpu_to_modern16(struct virtio_device *dev, uint16_t val)
> +{
> +	return dev->is_modern ? cpu_to_le16(val) : val;
> +}
> +
> +uint32_t virtio_cpu_to_modern32(struct virtio_device *dev, uint32_t val)
> +{
> +	return dev->is_modern ? cpu_to_le32(val) : val;
> +}
> +
> +uint64_t virtio_cpu_to_modern64(struct virtio_device *dev, uint64_t val)
> +{
> +	return dev->is_modern ? cpu_to_le64(val) : val;
> +}
> +
> +uint16_t virtio_modern16_to_cpu(struct virtio_device *dev, uint16_t val)
> +{
> +	return dev->is_modern ? le16_to_cpu(val) : val;
> +}
> +
> +uint32_t virtio_modern32_to_cpu(struct virtio_device *dev, uint32_t val)
> +{
> +	return dev->is_modern ? le32_to_cpu(val) : val;
> +}
> +
> +uint64_t virtio_modern64_to_cpu(struct virtio_device *dev, uint64_t val)
> +{
> +	return dev->is_modern ? le64_to_cpu(val) : val;
> +}
> +
>  static void virtio_process_cap(struct virtio_device *dev, uint8_t cap_ptr)
>  {
>  	struct virtio_cap *cap;
> diff --git a/lib/libvirtio/virtio.h b/lib/libvirtio/virtio.h
> index baf3961..ead2ec8 100644
> --- a/lib/libvirtio/virtio.h
> +++ b/lib/libvirtio/virtio.h
> @@ -131,5 +131,11 @@ extern uint64_t virtio_get_config(struct virtio_device *dev, int offset, int siz
>  extern int __virtio_read_config(struct virtio_device *dev, void *dst,
>  				int offset, int len);
>  
> +extern uint16_t virtio_cpu_to_modern16(struct virtio_device *dev, uint16_t val);
> +extern uint32_t virtio_cpu_to_modern32(struct virtio_device *dev, uint32_t val);
> +extern uint64_t virtio_cpu_to_modern64(struct virtio_device *dev, uint64_t val);
> +extern uint16_t virtio_modern16_to_cpu(struct virtio_device *dev, uint16_t val);
> +extern uint32_t virtio_modern32_to_cpu(struct virtio_device *dev, uint32_t val);
> +extern uint64_t virtio_modern64_to_cpu(struct virtio_device *dev, uint64_t val);

Patch looks fine ... but I'd maybe move the functions into the virtio.h
header file instead and declare them as "static inline", since the
functions are reasonable small and the compiler might then be able to
optimize all those single checks for dev->is_modern.

 Thomas



More information about the SLOF mailing list