Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3489875pxk; Mon, 5 Oct 2020 10:58:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJya08SUDBy8sa5gIhcAxyuxlNbtmqof96luG/njebs/aDhV+pYl/WCZBpsAzs+BIjK4xUF1 X-Received: by 2002:a17:906:49c6:: with SMTP id w6mr887091ejv.445.1601920704708; Mon, 05 Oct 2020 10:58:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601920704; cv=none; d=google.com; s=arc-20160816; b=PsMPuqiJvX5Yjrjhdi1MfQOD4u7KObnyy4m6LAKLDO70fiZpTcT4sKxqeuAr/DcoB0 j5KWR5sPsGVK/NOE0qRVy/QHYjzvCkuuEiFVBWOMgc7hK/4CAWshjNKZhSYJKL38J/rk RfFWgahcIP3SqqgdFuBnCbYXOPr4XKBXNyL5Nz6YQTITADdav76wdjnCrg5IMuynFOah jf0/gbbdXmpiPpmz4bBPyGBf6B8xUZLp4oMWmhBQTKfBHlcStBzxaw5qhumlLF8T3TRV Wg1li0SSq/EJfRV2rhIU3u1ytx4SvnxmkxaP24GoOaenlMYylJV8OP/0sNLsGpaK9KrY UW5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=ZJo3TdNWJ//lUho7c5BPu6bgsXzQP4JuRSSfq4ApmDY=; b=jxJpUbqBsQh7x23CbK+uB4SiqG5Cm4xB9BiDe/6rZeBfD6hj9AhvlRiQx70JQ+CboW Bg0cOdiVI4IB7cKxwg5QTNlT8S3Nox1GzYG50nvI4b7k4b9M9deYeQ8rDDmJbjn4eiXR 93UKk/tz/GdKMlvnCXhxaydYC0OP/e9zLwUD99/EK32jcLKNOUVULgykuS2o7Bgaqc+Y 5EkU5c9rM+TiWqGXFs6CghnfAVEyZ1bjzMSMkx5c1bm/E03dMIZ+n0zwle4AXS7lURaB WPwRXr9btjVY8lJ6iUY6oLBI4sn9RN9udmMRYkBH4qdZyZpiqe5LdwRn+5QFQ+D5LMk1 3h4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w11si206978ejy.632.2020.10.05.10.58.02; Mon, 05 Oct 2020 10:58:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729024AbgJERhY (ORCPT + 99 others); Mon, 5 Oct 2020 13:37:24 -0400 Received: from inva021.nxp.com ([92.121.34.21]:44692 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727344AbgJERhB (ORCPT ); Mon, 5 Oct 2020 13:37:01 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id B416A20159F; Mon, 5 Oct 2020 19:36:59 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id A76C62015AB; Mon, 5 Oct 2020 19:36:59 +0200 (CEST) Received: from fsr-ub1864-111.ea.freescale.net (fsr-ub1864-111.ea.freescale.net [10.171.82.141]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id 58A942032B; Mon, 5 Oct 2020 19:36:59 +0200 (CEST) From: Diana Craciun To: Alex Williamson , kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, bharatb.linux@gmail.com, laurentiu.tudor@nxp.com, Diana Craciun , Bharat Bhushan Subject: [PATCH v6 05/10] vfio/fsl-mc: Allow userspace to MMAP fsl-mc device MMIO regions Date: Mon, 5 Oct 2020 20:36:49 +0300 Message-Id: <20201005173654.31773-6-diana.craciun@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201005173654.31773-1-diana.craciun@oss.nxp.com> References: <20201005173654.31773-1-diana.craciun@oss.nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow userspace to mmap device regions for direct access of fsl-mc devices. Signed-off-by: Bharat Bhushan Signed-off-by: Diana Craciun --- drivers/vfio/fsl-mc/vfio_fsl_mc.c | 68 ++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c index 05dace5ddc2c..55190a2730fb 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c @@ -30,11 +30,20 @@ static int vfio_fsl_mc_regions_init(struct vfio_fsl_mc_device *vdev) for (i = 0; i < count; i++) { struct resource *res = &mc_dev->regions[i]; + int no_mmap = is_fsl_mc_bus_dprc(mc_dev); vdev->regions[i].addr = res->start; vdev->regions[i].size = resource_size(res); - vdev->regions[i].flags = 0; vdev->regions[i].type = mc_dev->regions[i].flags & IORESOURCE_BITS; + /* + * Only regions addressed with PAGE granularity may be + * MMAPed securely. + */ + if (!no_mmap && !(vdev->regions[i].addr & ~PAGE_MASK) && + !(vdev->regions[i].size & ~PAGE_MASK)) + vdev->regions[i].flags |= + VFIO_REGION_INFO_FLAG_MMAP; + } return 0; @@ -163,9 +172,64 @@ static ssize_t vfio_fsl_mc_write(void *device_data, const char __user *buf, return -EINVAL; } +static int vfio_fsl_mc_mmap_mmio(struct vfio_fsl_mc_region region, + struct vm_area_struct *vma) +{ + u64 size = vma->vm_end - vma->vm_start; + u64 pgoff, base; + u8 region_cacheable; + + pgoff = vma->vm_pgoff & + ((1U << (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT)) - 1); + base = pgoff << PAGE_SHIFT; + + if (region.size < PAGE_SIZE || base + size > region.size) + return -EINVAL; + + region_cacheable = (region.type & FSL_MC_REGION_CACHEABLE) && + (region.type & FSL_MC_REGION_SHAREABLE); + if (!region_cacheable) + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); + + vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff; + + return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, + size, vma->vm_page_prot); +} + static int vfio_fsl_mc_mmap(void *device_data, struct vm_area_struct *vma) { - return -EINVAL; + struct vfio_fsl_mc_device *vdev = device_data; + struct fsl_mc_device *mc_dev = vdev->mc_dev; + int index; + + index = vma->vm_pgoff >> (VFIO_FSL_MC_OFFSET_SHIFT - PAGE_SHIFT); + + 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)) + return -EINVAL; + if (index >= mc_dev->obj_desc.region_count) + return -EINVAL; + + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP)) + return -EINVAL; + + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ) + && (vma->vm_flags & VM_READ)) + return -EINVAL; + + if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE) + && (vma->vm_flags & VM_WRITE)) + return -EINVAL; + + vma->vm_private_data = mc_dev; + + return vfio_fsl_mc_mmap_mmio(vdev->regions[index], vma); } static const struct vfio_device_ops vfio_fsl_mc_ops = { -- 2.17.1