virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Pawel Moll <pawel.moll@arm.com>,
	virtualization@lists.linux-foundation.org
Subject: Re: [PATCH] virtio_mmio: fix endian-ness for mmio
Date: Mon, 9 Mar 2015 09:39:41 +0100	[thread overview]
Message-ID: <20150309083941.GA22567@redhat.com> (raw)
In-Reply-To: <1425592060-18474-1-git-send-email-mst@redhat.com>

On Thu, Mar 05, 2015 at 10:54:31PM +0100, Michael S. Tsirkin wrote:
> Going over the virtio mmio code, I noticed that it doesn't correctly
> return device config values in LE format when using virtio 1.0.
> Borrow code from virtio_pci_modern to do this correctly.
> 
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

Pawel, could you review and ack please?
It'd be unfortunate if we released a version
with incorrect endian-ness.

> ---
> 
> Note: untested: QEMU doesn't support virtio 1.0 for virtio-mmio.
> Pawel, could you please confirm this patch makes sense?
> 
>  drivers/virtio/virtio_mmio.c | 79 +++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 71 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c
> index cad5698..0375456 100644
> --- a/drivers/virtio/virtio_mmio.c
> +++ b/drivers/virtio/virtio_mmio.c
> @@ -156,22 +156,85 @@ static void vm_get(struct virtio_device *vdev, unsigned offset,
>  		   void *buf, unsigned len)
>  {
>  	struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
> -	u8 *ptr = buf;
> -	int i;
> +	void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
> +	u8 b;
> +	__le16 w;
> +	__le32 l;
>  
> -	for (i = 0; i < len; i++)
> -		ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
> +	if (vm_dev->version == 1) {
> +		u8 *ptr = buf;
> +		int i;
> +
> +		for (i = 0; i < len; i++)
> +			ptr[i] = readb(base + offset + i);
> +		return;
> +	}
> +
> +	switch (len) {
> +	case 1:
> +		b = readb(base + offset);
> +		memcpy(buf, &b, sizeof b);
> +		break;
> +	case 2:
> +		w = cpu_to_le16(readw(base + offset));
> +		memcpy(buf, &w, sizeof w);
> +		break;
> +	case 4:
> +		l = cpu_to_le32(readl(base + offset));
> +		memcpy(buf, &l, sizeof l);
> +		break;
> +	case 8:
> +		l = cpu_to_le32(readl(base + offset));
> +		memcpy(buf, &l, sizeof l);
> +		l = cpu_to_le32(ioread32(base + offset + sizeof l));
> +		memcpy(buf + sizeof l, &l, sizeof l);
> +		break;
> +	default:
> +		BUG();
> +	}
>  }
>  
>  static void vm_set(struct virtio_device *vdev, unsigned offset,
>  		   const void *buf, unsigned len)
>  {
>  	struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
> -	const u8 *ptr = buf;
> -	int i;
> +	void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
> +	u8 b;
> +	__le16 w;
> +	__le32 l;
>  
> -	for (i = 0; i < len; i++)
> -		writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
> +	if (vm_dev->version == 1) {
> +		const u8 *ptr = buf;
> +		int i;
> +
> +		for (i = 0; i < len; i++)
> +			writeb(ptr[i], base + offset + i);
> +
> +		return;
> +	}
> +
> +	switch (len) {
> +	case 1:
> +		memcpy(&b, buf, sizeof b);
> +		writeb(b, base + offset);
> +		break;
> +	case 2:
> +		memcpy(&w, buf, sizeof w);
> +		writew(le16_to_cpu(w), base + offset);
> +		break;
> +	case 4:
> +		memcpy(&l, buf, sizeof l);
> +		writel(le32_to_cpu(l), base + offset);
> +		break;
> +	case 8:
> +		memcpy(&l, buf, sizeof l);
> +		writel(le32_to_cpu(l), base + offset);
> +		memcpy(&l, buf + sizeof l, sizeof l);
> +		writel(le32_to_cpu(l), base + offset + sizeof l);
> +		break;
> +	default:
> +		BUG();
> +	}
>  }
>  
>  static u8 vm_get_status(struct virtio_device *vdev)
> -- 
> MST

  reply	other threads:[~2015-03-09  8:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-05 21:54 [PATCH] virtio_mmio: fix endian-ness for mmio Michael S. Tsirkin
2015-03-09  8:39 ` Michael S. Tsirkin [this message]
2015-03-12  2:03 ` Rusty Russell
     [not found] ` <871tkvx98n.fsf@rustcorp.com.au>
2015-03-12  7:20   ` Michael S. Tsirkin
2015-03-13  1:22     ` Rusty Russell
2015-03-23 11:49       ` Pawel Moll

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150309083941.GA22567@redhat.com \
    --to=mst@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pawel.moll@arm.com \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).