All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] virtio_mmio: fix endian-ness for mmio
@ 2015-03-05 21:54 ` Michael S. Tsirkin
  0 siblings, 0 replies; 12+ messages in thread
From: Michael S. Tsirkin @ 2015-03-05 21:54 UTC (permalink / raw)
  To: linux-kernel; +Cc: Rusty Russell, virtualization, Pawel Moll

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>
---

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

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH] virtio_mmio: fix endian-ness for mmio
@ 2015-03-05 21:54 ` Michael S. Tsirkin
  0 siblings, 0 replies; 12+ messages in thread
From: Michael S. Tsirkin @ 2015-03-05 21:54 UTC (permalink / raw)
  To: linux-kernel; +Cc: Pawel Moll, virtualization

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>
---

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

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
  2015-03-05 21:54 ` Michael S. Tsirkin
@ 2015-03-09  8:39   ` Michael S. Tsirkin
  -1 siblings, 0 replies; 12+ messages in thread
From: Michael S. Tsirkin @ 2015-03-09  8:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Rusty Russell, virtualization, Pawel Moll

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
@ 2015-03-09  8:39   ` Michael S. Tsirkin
  0 siblings, 0 replies; 12+ messages in thread
From: Michael S. Tsirkin @ 2015-03-09  8:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Pawel Moll, virtualization

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
  2015-03-05 21:54 ` Michael S. Tsirkin
  (?)
  (?)
@ 2015-03-12  2:03 ` Rusty Russell
  2015-03-12  7:20     ` Michael S. Tsirkin
  -1 siblings, 1 reply; 12+ messages in thread
From: Rusty Russell @ 2015-03-12  2:03 UTC (permalink / raw)
  To: Michael S. Tsirkin, linux-kernel; +Cc: virtualization, Pawel Moll

"Michael S. Tsirkin" <mst@redhat.com> writes:
> 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.

AFAICT, it doesn't need to.  The endian correction is done by the
callers.

The only reason that virtio_pci_modern() does it is because readl() etc
do endian conversion, and we don't want them to.  And the PCI part of
the spec says to use "natural" accessors, so we don't do byte-at-a-time.

Cheers,
Rusty.

> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>
> 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
  2015-03-05 21:54 ` Michael S. Tsirkin
                   ` (2 preceding siblings ...)
  (?)
@ 2015-03-12  2:03 ` Rusty Russell
  -1 siblings, 0 replies; 12+ messages in thread
From: Rusty Russell @ 2015-03-12  2:03 UTC (permalink / raw)
  To: Michael S. Tsirkin, linux-kernel; +Cc: Pawel Moll, virtualization

"Michael S. Tsirkin" <mst@redhat.com> writes:
> 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.

AFAICT, it doesn't need to.  The endian correction is done by the
callers.

The only reason that virtio_pci_modern() does it is because readl() etc
do endian conversion, and we don't want them to.  And the PCI part of
the spec says to use "natural" accessors, so we don't do byte-at-a-time.

Cheers,
Rusty.

> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>
> 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
  2015-03-12  2:03 ` Rusty Russell
@ 2015-03-12  7:20     ` Michael S. Tsirkin
  0 siblings, 0 replies; 12+ messages in thread
From: Michael S. Tsirkin @ 2015-03-12  7:20 UTC (permalink / raw)
  To: Rusty Russell; +Cc: linux-kernel, virtualization, Pawel Moll

On Thu, Mar 12, 2015 at 12:33:36PM +1030, Rusty Russell wrote:
> "Michael S. Tsirkin" <mst@redhat.com> writes:
> > 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.
> 
> AFAICT, it doesn't need to.  The endian correction is done by the
> callers.
> 
> The only reason that virtio_pci_modern() does it is because readl() etc
> do endian conversion, and we don't want them to.  And the PCI part of
> the spec says to use "natural" accessors, so we don't do byte-at-a-time.
> 
> Cheers,
> Rusty.

You are right, the endina-ness is not an issue, so the commit log I
wrote is wrong, but I still think the patch is required, because MMIO
spec says the same as PCI.
	The driver MUST only use 32 bit wide and aligned reads and writes to
	access the control registers described
	in table 4.1. For the device-specific configuration space, the driver
	MUST use 8 bit wide accesses for 8 bit
	wide fields, 16 bit wide and aligned accesses for 16 bit wide fields and
	32 bit wide and aligned accesses for
	32 and 64 bit wide fields.


Here's a better commit log:

--->

virtio_mmio: fix access width for mmio

Going over the virtio mmio code, I noticed that it doesn't correctly
access modern device config values using "natural" accessors: it uses
readb to get/set them byte by byte, while the virtio 1.0 spec explicitly states:

	4.2.2.2 Driver Requirements: MMIO Device Register Layout

	...

	The driver MUST only use 32 bit wide and aligned reads and writes to
	access the control registers described in table 4.1.
	For the device-specific configuration space, the driver MUST use
	8 bit wide accesses for 8 bit wide fields, 16 bit wide and aligned
	accesses for 16 bit wide fields and 32 bit wide and aligned accesses for
	32 and 64 bit wide fields.

Borrow code from virtio_pci_modern to do this correctly.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

Makes sense now, right?
Want me to repost or can you just tweak the commit log?


> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> >
> > 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
@ 2015-03-12  7:20     ` Michael S. Tsirkin
  0 siblings, 0 replies; 12+ messages in thread
From: Michael S. Tsirkin @ 2015-03-12  7:20 UTC (permalink / raw)
  To: Rusty Russell; +Cc: linux-kernel, Pawel Moll, virtualization

On Thu, Mar 12, 2015 at 12:33:36PM +1030, Rusty Russell wrote:
> "Michael S. Tsirkin" <mst@redhat.com> writes:
> > 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.
> 
> AFAICT, it doesn't need to.  The endian correction is done by the
> callers.
> 
> The only reason that virtio_pci_modern() does it is because readl() etc
> do endian conversion, and we don't want them to.  And the PCI part of
> the spec says to use "natural" accessors, so we don't do byte-at-a-time.
> 
> Cheers,
> Rusty.

You are right, the endina-ness is not an issue, so the commit log I
wrote is wrong, but I still think the patch is required, because MMIO
spec says the same as PCI.
	The driver MUST only use 32 bit wide and aligned reads and writes to
	access the control registers described
	in table 4.1. For the device-specific configuration space, the driver
	MUST use 8 bit wide accesses for 8 bit
	wide fields, 16 bit wide and aligned accesses for 16 bit wide fields and
	32 bit wide and aligned accesses for
	32 and 64 bit wide fields.


Here's a better commit log:

--->

virtio_mmio: fix access width for mmio

Going over the virtio mmio code, I noticed that it doesn't correctly
access modern device config values using "natural" accessors: it uses
readb to get/set them byte by byte, while the virtio 1.0 spec explicitly states:

	4.2.2.2 Driver Requirements: MMIO Device Register Layout

	...

	The driver MUST only use 32 bit wide and aligned reads and writes to
	access the control registers described in table 4.1.
	For the device-specific configuration space, the driver MUST use
	8 bit wide accesses for 8 bit wide fields, 16 bit wide and aligned
	accesses for 16 bit wide fields and 32 bit wide and aligned accesses for
	32 and 64 bit wide fields.

Borrow code from virtio_pci_modern to do this correctly.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

Makes sense now, right?
Want me to repost or can you just tweak the commit log?


> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> >
> > 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
  2015-03-12  7:20     ` Michael S. Tsirkin
@ 2015-03-13  1:22       ` Rusty Russell
  -1 siblings, 0 replies; 12+ messages in thread
From: Rusty Russell @ 2015-03-13  1:22 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: linux-kernel, virtualization, Pawel Moll

"Michael S. Tsirkin" <mst@redhat.com> writes:
> On Thu, Mar 12, 2015 at 12:33:36PM +1030, Rusty Russell wrote:
>> "Michael S. Tsirkin" <mst@redhat.com> writes:
>> > 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.
>> 
>> AFAICT, it doesn't need to.  The endian correction is done by the
>> callers.
>> 
>> The only reason that virtio_pci_modern() does it is because readl() etc
>> do endian conversion, and we don't want them to.  And the PCI part of
>> the spec says to use "natural" accessors, so we don't do byte-at-a-time.
>> 
>> Cheers,
>> Rusty.
>
> You are right, the endina-ness is not an issue, so the commit log I
> wrote is wrong, but I still think the patch is required, because MMIO
> spec says the same as PCI.
> 	The driver MUST only use 32 bit wide and aligned reads and writes to
> 	access the control registers described
> 	in table 4.1. For the device-specific configuration space, the driver
> 	MUST use 8 bit wide accesses for 8 bit
> 	wide fields, 16 bit wide and aligned accesses for 16 bit wide fields and
> 	32 bit wide and aligned accesses for
> 	32 and 64 bit wide fields.

Ah, good point.

I'm sure Pawel is on a beach somewhere sipping cocktails, so I'll apply
this immediately (with your updated commit message) to my
pending-rebases and give him the weekend to Ack.

Thanks,
Rusty.


>
>
> Here's a better commit log:
>
> --->
>
> virtio_mmio: fix access width for mmio
>
> Going over the virtio mmio code, I noticed that it doesn't correctly
> access modern device config values using "natural" accessors: it uses
> readb to get/set them byte by byte, while the virtio 1.0 spec explicitly states:
>
> 	4.2.2.2 Driver Requirements: MMIO Device Register Layout
>
> 	...
>
> 	The driver MUST only use 32 bit wide and aligned reads and writes to
> 	access the control registers described in table 4.1.
> 	For the device-specific configuration space, the driver MUST use
> 	8 bit wide accesses for 8 bit wide fields, 16 bit wide and aligned
> 	accesses for 16 bit wide fields and 32 bit wide and aligned accesses for
> 	32 and 64 bit wide fields.
>
> Borrow code from virtio_pci_modern to do this correctly.
>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
>
> Makes sense now, right?
> Want me to repost or can you just tweak the commit log?
>
>
>> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
>> > ---
>> >
>> > 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
@ 2015-03-13  1:22       ` Rusty Russell
  0 siblings, 0 replies; 12+ messages in thread
From: Rusty Russell @ 2015-03-13  1:22 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: linux-kernel, Pawel Moll, virtualization

"Michael S. Tsirkin" <mst@redhat.com> writes:
> On Thu, Mar 12, 2015 at 12:33:36PM +1030, Rusty Russell wrote:
>> "Michael S. Tsirkin" <mst@redhat.com> writes:
>> > 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.
>> 
>> AFAICT, it doesn't need to.  The endian correction is done by the
>> callers.
>> 
>> The only reason that virtio_pci_modern() does it is because readl() etc
>> do endian conversion, and we don't want them to.  And the PCI part of
>> the spec says to use "natural" accessors, so we don't do byte-at-a-time.
>> 
>> Cheers,
>> Rusty.
>
> You are right, the endina-ness is not an issue, so the commit log I
> wrote is wrong, but I still think the patch is required, because MMIO
> spec says the same as PCI.
> 	The driver MUST only use 32 bit wide and aligned reads and writes to
> 	access the control registers described
> 	in table 4.1. For the device-specific configuration space, the driver
> 	MUST use 8 bit wide accesses for 8 bit
> 	wide fields, 16 bit wide and aligned accesses for 16 bit wide fields and
> 	32 bit wide and aligned accesses for
> 	32 and 64 bit wide fields.

Ah, good point.

I'm sure Pawel is on a beach somewhere sipping cocktails, so I'll apply
this immediately (with your updated commit message) to my
pending-rebases and give him the weekend to Ack.

Thanks,
Rusty.


>
>
> Here's a better commit log:
>
> --->
>
> virtio_mmio: fix access width for mmio
>
> Going over the virtio mmio code, I noticed that it doesn't correctly
> access modern device config values using "natural" accessors: it uses
> readb to get/set them byte by byte, while the virtio 1.0 spec explicitly states:
>
> 	4.2.2.2 Driver Requirements: MMIO Device Register Layout
>
> 	...
>
> 	The driver MUST only use 32 bit wide and aligned reads and writes to
> 	access the control registers described in table 4.1.
> 	For the device-specific configuration space, the driver MUST use
> 	8 bit wide accesses for 8 bit wide fields, 16 bit wide and aligned
> 	accesses for 16 bit wide fields and 32 bit wide and aligned accesses for
> 	32 and 64 bit wide fields.
>
> Borrow code from virtio_pci_modern to do this correctly.
>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
>
> Makes sense now, right?
> Want me to repost or can you just tweak the commit log?
>
>
>> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
>> > ---
>> >
>> > 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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
  2015-03-13  1:22       ` Rusty Russell
@ 2015-03-23 11:49         ` Pawel Moll
  -1 siblings, 0 replies; 12+ messages in thread
From: Pawel Moll @ 2015-03-23 11:49 UTC (permalink / raw)
  To: Rusty Russell, Michael S. Tsirkin; +Cc: linux-kernel, virtualization

On Fri, 2015-03-13 at 01:22 +0000, Rusty Russell wrote:
> I'm sure Pawel is on a beach somewhere sipping cocktails, 

Ehm... There's still a some more prohibition for me (granted, by choice,
just to share the "pain" ;-), so it wasn't exactly 

> so I'll apply
> this immediately (with your updated commit message) to my
> pending-rebases and give him the weekend to Ack.

Not that it matters any more, but a belated

Acked-by: Pawel Moll <pawel.moll@arm.com>

Thanks, Michael, for fixing this! (although, I must admit, I haven't
tested it yet - all in right time :-)

Pawel


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] virtio_mmio: fix endian-ness for mmio
@ 2015-03-23 11:49         ` Pawel Moll
  0 siblings, 0 replies; 12+ messages in thread
From: Pawel Moll @ 2015-03-23 11:49 UTC (permalink / raw)
  To: Rusty Russell, Michael S. Tsirkin; +Cc: linux-kernel, virtualization

On Fri, 2015-03-13 at 01:22 +0000, Rusty Russell wrote:
> I'm sure Pawel is on a beach somewhere sipping cocktails, 

Ehm... There's still a some more prohibition for me (granted, by choice,
just to share the "pain" ;-), so it wasn't exactly 

> so I'll apply
> this immediately (with your updated commit message) to my
> pending-rebases and give him the weekend to Ack.

Not that it matters any more, but a belated

Acked-by: Pawel Moll <pawel.moll@arm.com>

Thanks, Michael, for fixing this! (although, I must admit, I haven't
tested it yet - all in right time :-)

Pawel

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-03-23 11:49 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-05 21:54 [PATCH] virtio_mmio: fix endian-ness for mmio Michael S. Tsirkin
2015-03-05 21:54 ` Michael S. Tsirkin
2015-03-09  8:39 ` Michael S. Tsirkin
2015-03-09  8:39   ` Michael S. Tsirkin
2015-03-12  2:03 ` Rusty Russell
2015-03-12  7:20   ` Michael S. Tsirkin
2015-03-12  7:20     ` Michael S. Tsirkin
2015-03-13  1:22     ` Rusty Russell
2015-03-13  1:22       ` Rusty Russell
2015-03-23 11:49       ` Pawel Moll
2015-03-23 11:49         ` Pawel Moll
2015-03-12  2:03 ` Rusty Russell

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.