Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1414271ybj; Fri, 8 May 2020 00:22:44 -0700 (PDT) X-Google-Smtp-Source: APiQypI+Hpbwq5Bn19APTb9+euV0ENnCKeRwvXLe2j5Pun/V/cCnKCkPWv6NMDH2ilWy5vOYgv7e X-Received: by 2002:aa7:de0b:: with SMTP id h11mr1010941edv.133.1588922564247; Fri, 08 May 2020 00:22:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588922564; cv=none; d=google.com; s=arc-20160816; b=nd/9v1OW1IW+0DTm2mBKOGSHZW4758W1KRAdrE58vs2SHIbAQnnHAr6fFaC6VcIJzj xyfLSOmrBbMmd5poVxqvaKz6GPXNCy3RVRKeVMPi2aL2tZgaiD3YTuaHKnYp9++aWqFd VIbf+6CKMLQOgMzSvyagoNean8HzUX7fCGUVM+Qfbie2g5gF4aA8fWCqO30hKmzDyU05 jzLPXKEFk9IgyuahtCe+hms5kWEYtG30bnxcDD0+RJWbeguvsaRYiAdN82/2nZ6Fscl2 zDRSJ42PhQujQHzEBUIHWMpvIRhxQ6DJkCxPuFAFlsLjnFJLS+KuD102WcE4zyi1dkQE P2JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=24pZnMNCM3bI/HinkPEFtIgw3hVhdiSBC6s01shkAbc=; b=RKbJIcEp+ZztFW7n0hjBBz21uO3M5lKmmd3J5FZwRPNrG2AqN3sBvMCM4Y/vrvDTZz N8VX8+0f0vsvEzLCKoHkItkWDTc8ae3SBQjcNUOqwawbPMmlS+1IBWR5pQwiL8QttWNb EDWxoN98CbecpI0HCA1BVsyVm90+tx5YhkZ9NUC6rjLhUIekr4+9R7rGeVfKoKHSTA+4 FtQBX8zPplXR2XmoKIDRrtPrfnrSeNpVheknJO9B1AaODc4VM5V1CXiNQ8X0a8VREj6x gxHq7LWPCeY8jCF/HvG09V48j+QjWAfODihebewch1leiXtwyhLCSipUuv2ernc7YSW9 pAjQ== 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 g15si503237edu.131.2020.05.08.00.22.20; Fri, 08 May 2020 00:22:44 -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 S1727097AbgEHHUx (ORCPT + 99 others); Fri, 8 May 2020 03:20:53 -0400 Received: from inva021.nxp.com ([92.121.34.21]:59230 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726809AbgEHHUs (ORCPT ); Fri, 8 May 2020 03:20:48 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 8AA212012D8; Fri, 8 May 2020 09:20:46 +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 7E8182012D6; Fri, 8 May 2020 09:20:46 +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 F1514204BD; Fri, 8 May 2020 09:20:45 +0200 (CEST) From: Diana Craciun To: alex.williamson@redhat.com, kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, laurentiu.tudor@nxp.com, bharatb.linux@gmail.com, Diana Craciun , Bharat Bhushan Subject: [PATCH v2 5/9] vfio/fsl-mc: Allow userspace to MMAP fsl-mc device MMIO regions Date: Fri, 8 May 2020 10:20:35 +0300 Message-Id: <20200508072039.18146-6-diana.craciun@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200508072039.18146-1-diana.craciun@oss.nxp.com> References: <20200508072039.18146-1-diana.craciun@oss.nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-kernel-owner@vger.kernel.org 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 | 60 ++++++++++++++++++++++- drivers/vfio/fsl-mc/vfio_fsl_mc_private.h | 2 + 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c index c162fa27c02c..a92c6c97c29a 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c @@ -33,7 +33,11 @@ static int vfio_fsl_mc_regions_init(struct vfio_fsl_mc_device *vdev) vdev->regions[i].addr = res->start; vdev->regions[i].size = PAGE_ALIGN((resource_size(res))); - vdev->regions[i].flags = 0; + vdev->regions[i].flags = VFIO_REGION_INFO_FLAG_MMAP; + vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ; + if (!(mc_dev->regions[i].flags & IORESOURCE_READONLY)) + vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_WRITE; + vdev->regions[i].type = mc_dev->regions[i].flags & IORESOURCE_BITS; } vdev->num_regions = mc_dev->obj_desc.region_count; @@ -164,9 +168,61 @@ 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; + + 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; + + if (!(region.type & VFIO_DPRC_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 >= vdev->num_regions) + 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 = { diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h index 818dfd3df4db..89d2e2a602d8 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h @@ -15,6 +15,8 @@ #define VFIO_FSL_MC_INDEX_TO_OFFSET(index) \ ((u64)(index) << VFIO_FSL_MC_OFFSET_SHIFT) +#define VFIO_DPRC_REGION_CACHEABLE 0x00000001 + struct vfio_fsl_mc_region { u32 flags; u32 type; -- 2.17.1