mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Antonios Motakis <a.motakis@virtualopensystems.com>
To: alex.williamson@redhat.com, kvmarm@lists.cs.columbia.edu,
	iommu@lists.linux-foundation.org
Cc: tech@virtualopensystems.com, a.rigo@virtualopensystems.com,
	B08248@freescale.com, kim.phillips@linaro.org,
	Antonios Motakis <a.motakis@virtualopensystems.com>,
	kvm@vger.kernel.org (open list:VFIO DRIVER),
	linux-kernel@vger.kernel.org (open list)
Subject: [PATCH 6/9] VFIO_PLATFORM: Support MMAP of MMIO regions
Date: Wed, 11 Dec 2013 21:58:30 +0100	[thread overview]
Message-ID: <1386795513-32426-7-git-send-email-a.motakis@virtualopensystems.com> (raw)
In-Reply-To: <1386795513-32426-1-git-send-email-a.motakis@virtualopensystems.com>

Allow to memory map the MMIO regions of the device so userspace can
directly access them.

Signed-off-by: Antonios Motakis <a.motakis@virtualopensystems.com>
Tested-by: Alvise Rigo <a.rigo@virtualopensystems.com>
---
 drivers/vfio/platform/vfio_platform.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/drivers/vfio/platform/vfio_platform.c b/drivers/vfio/platform/vfio_platform.c
index d0b2941..9786660 100644
--- a/drivers/vfio/platform/vfio_platform.c
+++ b/drivers/vfio/platform/vfio_platform.c
@@ -58,6 +58,11 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
 		region.flags = VFIO_REGION_INFO_FLAG_READ
 				| VFIO_REGION_INFO_FLAG_WRITE;
 
+		/* Only regions addressed with PAGE granularity can be MMAPed
+		 * securely. */
+		if (!(region.addr & ~PAGE_MASK) && !(region.size & ~PAGE_MASK))
+			region.flags |= VFIO_REGION_INFO_FLAG_MMAP;
+
 		vdev->region[i] = region;
 	}
 
@@ -283,6 +288,34 @@ err:
 
 static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
 {
+	struct vfio_platform_device *vdev = device_data;
+	u64 req_len = vma->vm_end - vma->vm_start;
+	u64 addr = vma->vm_pgoff << PAGE_SHIFT;
+	int i;
+
+	if (vma->vm_end < vma->vm_start)
+		return -EINVAL;
+	if (vma->vm_start & ~PAGE_MASK)
+		return -EINVAL;
+	if (vma->vm_end & ~PAGE_MASK)
+		return -EINVAL;
+	if ((vma->vm_flags & VM_SHARED) == 0)
+		return -EINVAL;
+
+	for (i = 0; i < vdev->num_regions; i++) {
+		struct vfio_platform_region region = vdev->region[i];
+
+		if ((addr < region.addr)
+		     || (addr + req_len - 1) >= (region.addr + region.size))
+			continue;
+
+		vma->vm_private_data = vdev;
+		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+		return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+				       req_len, vma->vm_page_prot);
+	}
+
 	return -EINVAL;
 }
 
-- 
1.8.3.2


  parent reply	other threads:[~2013-12-11 21:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1386795513-32426-1-git-send-email-a.motakis@virtualopensystems.com>
2013-12-11 20:58 ` [PATCH 1/9] VFIO_IOMMU_TYPE1: Introduce the VFIO_DMA_MAP_FLAG_EXEC flag Antonios Motakis
2013-12-11 20:58 ` [PATCH 2/9] VFIO_IOMMU_TYPE1: workaround to build for platform devices Antonios Motakis
2013-12-11 20:58 ` [PATCH 3/9] VFIO_PLATFORM: Initial skeleton of VFIO support " Antonios Motakis
2013-12-11 20:58 ` [PATCH 4/9] VFIO_PLATFORM: Return info for device and its memory mapped IO regions Antonios Motakis
2013-12-11 20:58 ` [PATCH 5/9] VFIO_PLATFORM: Read and write support for the device fd Antonios Motakis
2013-12-11 20:58 ` Antonios Motakis [this message]
2013-12-11 20:58 ` [PATCH 7/9] VFIO_PLATFORM: Return IRQ info Antonios Motakis
2013-12-11 20:58 ` [PATCH 8/9] VFIO_PLATFORM: Initial interrupts support Antonios Motakis
2013-12-11 20:58 ` [PATCH 9/9] VFIO_PLATFORM: Support for maskable and automasked interrupts Antonios Motakis

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=1386795513-32426-7-git-send-email-a.motakis@virtualopensystems.com \
    --to=a.motakis@virtualopensystems.com \
    --cc=B08248@freescale.com \
    --cc=a.rigo@virtualopensystems.com \
    --cc=alex.williamson@redhat.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=kim.phillips@linaro.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tech@virtualopensystems.com \
    /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

all inboxes | Powered by JetHome®