Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp261739ybt; Mon, 6 Jul 2020 08:43:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxl/Swmf3BI96pV0fPBhA/fipabuz4YBQyR1YWREXQhXSORXGyRiY27sB3XKYDLHrM80tb X-Received: by 2002:a50:ee8a:: with SMTP id f10mr36674526edr.383.1594050192040; Mon, 06 Jul 2020 08:43:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594050192; cv=none; d=google.com; s=arc-20160816; b=oHd8Ta0FCksMsZWgitKPtESsySYImR2t+KyLzd1jYVbnSx3SR8j+NYEchBzwwHof3U FPsck3So3lp+ReVBLgSGlo547wbaZq2adVs+QThFccOPRj9N0vS6uFzyz/fCAOfSErRi ME8jMXlO0o+EyNkReYniC5O2EYlt6J7JtKL2pEcCmXnK3L3tYMAdpbNuZrepSRrlf6U+ 7vhcWZeDP0opJNoX6Cs9hxUe2ArUz+nw9jQilwjlkB5yPgd3pEkfrW94VraA5VPtOQM7 5Bo0dF/NDN69ux9Ue0wWrZn86BwsjaGjLhOFjKuLBKsVv7ZIgfBXFq9wKMFSPAyBjlE6 HwbQ== 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=J27AiiF6wupaRUOAzJU2irGcrybtp0MvbV1WhP0hKFw=; b=Ua5JJ44s6T4UioLVsxgOO10rlMbCH/jL/rEB54klHbUhSaLHznG9p+0TwUmjxPD9A7 t7WnnjYIhDtv2SmIJkPIn9GiD7mLZfDSY9fnKW7ajwqE7D5kDzfvYTYxP/g1RAE9Dy/f RnUXuNlKn8ySuFdBBrh4qo825er3NIRPokdlDXYnLO2R7ak6avMVyJyxWsdxISuNgtxt m9mhNUbyGscc9442cj2gBN0RYhmlLKz5wCXrSPVd6ku7CJFWUxKsuKU93j6BBre0NCU9 EaN9szxq2f+GfI/ftaX7aUB60tCz5DbXxU9+uIReLbYwLUR3ueazXTw1VKZnBox2iDrU gOog== 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 du6si17111733ejc.189.2020.07.06.08.42.49; Mon, 06 Jul 2020 08:43:12 -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 S1729540AbgGFPmU (ORCPT + 99 others); Mon, 6 Jul 2020 11:42:20 -0400 Received: from inva021.nxp.com ([92.121.34.21]:36224 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729377AbgGFPmQ (ORCPT ); Mon, 6 Jul 2020 11:42:16 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 3CAE42004F1; Mon, 6 Jul 2020 17:42:14 +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 3111B2006D2; Mon, 6 Jul 2020 17:42:14 +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 D8126204E6; Mon, 6 Jul 2020 17:42:13 +0200 (CEST) From: Diana Craciun To: alex.williamson@redhat.com, kvm@vger.kernel.org Cc: bharatb.linux@gmail.com, linux-kernel@vger.kernel.org, laurentiu.tudor@nxp.com, Diana Craciun , Bharat Bhushan Subject: [PATCH v3 5/9] vfio/fsl-mc: Allow userspace to MMAP fsl-mc device MMIO regions Date: Mon, 6 Jul 2020 18:41:49 +0300 Message-Id: <20200706154153.11477-6-diana.craciun@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200706154153.11477-1-diana.craciun@oss.nxp.com> References: <20200706154153.11477-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 | 59 +++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c index 10bd9f78b8de..37ccc7d2acbc 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c @@ -33,7 +33,8 @@ static int vfio_fsl_mc_regions_init(struct vfio_fsl_mc_device *vdev) vdev->regions[i].addr = res->start; vdev->regions[i].size = resource_size(res); - vdev->regions[i].flags = 0; + vdev->regions[i].flags = VFIO_REGION_INFO_FLAG_MMAP; + vdev->regions[i].type = mc_dev->regions[i].flags & IORESOURCE_BITS; } vdev->num_regions = mc_dev->obj_desc.region_count; @@ -164,9 +165,63 @@ 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 >= 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 = { -- 2.17.1