From patchwork Mon Sep 30 15:28:40 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonios Motakis X-Patchwork-Id: 2966231 Return-Path: X-Original-To: patchwork-linux-samsung-soc@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id A0730BFF0B for ; Mon, 30 Sep 2013 15:29:49 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 042F4200B4 for ; Mon, 30 Sep 2013 15:29:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 68EB220268 for ; Mon, 30 Sep 2013 15:29:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756164Ab3I3P3Z (ORCPT ); Mon, 30 Sep 2013 11:29:25 -0400 Received: from mail-wg0-f44.google.com ([74.125.82.44]:63466 "EHLO mail-wg0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756152Ab3I3P3W (ORCPT ); Mon, 30 Sep 2013 11:29:22 -0400 Received: by mail-wg0-f44.google.com with SMTP id b13so5910130wgh.35 for ; Mon, 30 Sep 2013 08:29:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Yikr1Er80vCxZoCjQHPhALZuhb5bvfnUzgaME08YKC4=; b=JVaISPctBWuZSA23FYh8UpDwJUjpGYaDBicOhHj+t5AgjYpvHle3imU73JG7darL/Z QeqyuUy9Q2etv2Fd9cwwowmwu2EF2T1c3I26jFzzcG33V09U4aS/BqweM+T4VxD5Blul c8bcz2I77bJLSNQVQNTp1BKsPnznzKGQOyv9J2t0imNueIOiCaCsnS9J2JibWe6l60c1 lQYpR9gN/OO0JCp0CqjUb/FWfrEVbvxfXkbybtUoW+i6D1s31vYFp+BXP9YpsTTzDjY9 /wkoOyH+XvaBPq6rhdIA9t9wUeBRL9vUHZ1FE8YkWomwmcyPJw33ElEsU28Q1U4qPQBl Y2bw== X-Gm-Message-State: ALoCoQmDFciPZAtvFxMBdzkUxVmzA6/mw5OZedqqbfKkhJo8msodXc+fpcYEMoAGbqyy5LHrDzVD X-Received: by 10.194.5.35 with SMTP id p3mr2186034wjp.47.1380554961166; Mon, 30 Sep 2013 08:29:21 -0700 (PDT) Received: from localhost.localdomain (home.tvelocity.eu. [82.67.68.96]) by mx.google.com with ESMTPSA id ey4sm2015579wic.11.1969.12.31.16.00.00 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 30 Sep 2013 08:29:20 -0700 (PDT) From: Antonios Motakis To: kvmarm@lists.cs.columbia.edu, alex.williamson@redhat.com Cc: iommu@lists.linux-foundation.org, linux-samsung-soc@vger.kernel.org, kvm@vger.kernel.org, agraf@suse.de, B08248@freescale.com, tech@virtualopensystems.com, Antonios Motakis Subject: [PATCH 4/7] VFIO: DT: Support MMAP of MMIO regions Date: Mon, 30 Sep 2013 17:28:40 +0200 Message-Id: <1380554923-17818-5-git-send-email-a.motakis@virtualopensystems.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1380554923-17818-1-git-send-email-a.motakis@virtualopensystems.com> References: <1380554923-17818-1-git-send-email-a.motakis@virtualopensystems.com> Sender: linux-samsung-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-samsung-soc@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Antonios Motakis --- drivers/vfio/vfio_platform.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/drivers/vfio/vfio_platform.c b/drivers/vfio/vfio_platform.c index a0abcfa..6364316 100644 --- a/drivers/vfio/vfio_platform.c +++ b/drivers/vfio/vfio_platform.c @@ -103,6 +103,10 @@ static long vfio_platform_ioctl(void *device_data, info.offset = res.start; /* map phys addr with offset */ info.size = resource_size(&res); info.flags = 0; + /* Only regions addressed with PAGE granularity can be MMAPed + * securely. */ + if (!(info.offset & ~PAGE_MASK) && !(info.size & ~PAGE_MASK)) + info.flags |= VFIO_REGION_INFO_FLAG_MMAP; return copy_to_user((void __user *)arg, &info, minsz); @@ -134,6 +138,18 @@ static long vfio_platform_ioctl(void *device_data, return -ENOTTY; } +static bool is_in_resource(struct resource res, u64 addr, u64 size) +{ + if (addr < res.start) + return false; + if (addr > res.end) + return false; + if (addr + size - 1 > res.end) + return false; + + return true; +} + static ssize_t vfio_platform_read(void *device_data, char __user *buf, size_t count, loff_t *ppos) { @@ -148,6 +164,34 @@ static ssize_t vfio_platform_write(void *device_data, const char __user *buf, static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma) { + struct vfio_platform_device *vdev = device_data; + struct device_node *of_node = vdev->pdev->dev.of_node; + u64 req_len = vma->vm_end - vma->vm_start; + u64 addr = vma->vm_pgoff << PAGE_SHIFT; + struct resource res; + 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; !of_address_to_resource(of_node, i, &res); i++) { + if (!is_in_resource(res, addr, req_len)) + continue; + + vma->vm_private_data = vdev; + vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); + + return remap_pfn_range(vma, vma->vm_start, addr, + req_len, vma->vm_page_prot); + } + return -EINVAL; }