Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp648625pxk; Thu, 3 Sep 2020 09:06:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuKWsuiMfc3hOjcN47+bNkjqaBx1QZxQZHjZ8Pu4UU60FXcSgq4tgLHhOJQHE7+CRIINfT X-Received: by 2002:a05:6402:486:: with SMTP id k6mr3868974edv.83.1599149211795; Thu, 03 Sep 2020 09:06:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599149211; cv=none; d=google.com; s=arc-20160816; b=STJMv7qK+PJW2FavN50LocufZjaIjXzEt1LIlyY966EbR0RoFVkgb1WZvLnEaGG6vH sTc+GIyt1ryxqw80IBixkZx36CqcGSvHn87iJKeyUKEfwI2DGXdAAG+0/eYb22Q73sDx nNWK+69cLcocaEZPeMvo1yUahezExeZO5JbGHqf9qpNP0TySetcWd91yiTm8dzWQ+siK JyNxBZzNi6YANCCQ/yqrs0if31EsIaKwRCtkgW3td2NYhB7oKoSZjaduGo1sRz0NZDy/ hsHEWdr6PdeoL83AiI7oE4bTvcweKGpSgaQxgKBOevGcdV+u2kGUyouqQEpdjPt0cWpX zNsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=Lywqd6voRznuMzn+og25jGwd7RuHrKT23MBfJquoAhQ=; b=sQHSeQYRHWTG9Wn0muQuburCuXnXK7h3b8Aqrk2+x03Lw3GXSgxOh2tfJacDRcszpQ SZsjAoAm4OPwiHXuuRDZaV7lwSwJ7LDr9KV4HyEm/7pQEHiwpWSOG5C+lyOcGPiaQ9Dh 0nghI3fnCWP0R/C//ucBYSzevsGFQ7tEfrjDteg3u7l3hooYLEnqT0oCbHyGV5VvMoN5 8nY3UyMtR8FGnnkQ+iQEXZixjn1KcDdRX8dwyOmFyO4tybNn5ZRQrtM+01GmCmGjigAm s99LFuXJ4fPDybv1eD/mRJ2Snxtt/JNm/6W57YuyhKxBfVBvJhaJhOmGxWAxDeRUgyy5 pBWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ctGk2S+d; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r21si2168289ejs.399.2020.09.03.09.06.28; Thu, 03 Sep 2020 09:06:51 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ctGk2S+d; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728575AbgICQFZ (ORCPT + 99 others); Thu, 3 Sep 2020 12:05:25 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:34382 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727065AbgICQFY (ORCPT ); Thu, 3 Sep 2020 12:05:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599149122; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lywqd6voRznuMzn+og25jGwd7RuHrKT23MBfJquoAhQ=; b=ctGk2S+dpSCPwZb4cxpVo9StsbkhaWIyeJkVKrmFfpp1VnQG6ktgeCKOjaUIhu0/b3f0E9 iQ1WnxDmrN4Nd0GQY+sGTg3jD7sdc7QOPeeVtCxD3ubul+LemGc7h7LLjEHi33EJsllFBP 7W0ErRB853TLXtPpxN1NeFZvKQWUvcY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-206-h62wHkyWM16EAmB4I-Mwkw-1; Thu, 03 Sep 2020 12:05:20 -0400 X-MC-Unique: h62wHkyWM16EAmB4I-Mwkw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6ECEC107464A; Thu, 3 Sep 2020 16:05:19 +0000 (UTC) Received: from [10.36.112.51] (ovpn-112-51.ams2.redhat.com [10.36.112.51]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EF4D65D9CC; Thu, 3 Sep 2020 16:05:14 +0000 (UTC) Subject: Re: [PATCH v4 05/10] vfio/fsl-mc: Allow userspace to MMAP fsl-mc device MMIO regions To: Diana Craciun , alex.williamson@redhat.com, kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, bharatb.linux@gmail.com, laurentiu.tudor@nxp.com, Bharat Bhushan References: <20200826093315.5279-1-diana.craciun@oss.nxp.com> <20200826093315.5279-6-diana.craciun@oss.nxp.com> From: Auger Eric Message-ID: Date: Thu, 3 Sep 2020 18:05:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20200826093315.5279-6-diana.craciun@oss.nxp.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Diana, On 8/26/20 11:33 AM, Diana Craciun wrote: > 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 +++++++++++++++++++++++++++++-- > 1 file changed, 58 insertions(+), 2 deletions(-) > > diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c > index 093b8d68496c..64d5c1fff51f 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; Is the region always mmappable or does it depend on the mc_dev->regions[i].flags. Also on VFIO platform we checked some alignment addr/size constraints. > + vdev->regions[i].type = mc_dev->regions[i].flags & IORESOURCE_BITS; > } > > vdev->num_regions = mc_dev->obj_desc.region_count; > @@ -164,9 +165,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); I see in fsl-mc-bus.c that IORESOURCE_CACHEABLE and IORESOURCE_MEM are set on the regions flag? > + 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 = { > Thanks Eric