From patchwork Tue Jun 21 01:14:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Kardashevskiy X-Patchwork-Id: 638354 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rYVD06qdyz9spN for ; Tue, 21 Jun 2016 11:16:00 +1000 (AEST) Received: from localhost ([::1]:47371 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFAII-000384-Ql for incoming@patchwork.ozlabs.org; Mon, 20 Jun 2016 21:15:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57812) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFAGn-0001so-6Y for qemu-devel@nongnu.org; Mon, 20 Jun 2016 21:14:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bFAGh-00039A-SX for qemu-devel@nongnu.org; Mon, 20 Jun 2016 21:14:24 -0400 Received: from ozlabs.ru ([83.169.36.222]:43839) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFAGh-000394-IQ; Mon, 20 Jun 2016 21:14:19 -0400 Received: from vpl2.ozlabs.ibm.com (localhost [IPv6:::1]) by ozlabs.ru (Postfix) with ESMTP id D05451205AB; Tue, 21 Jun 2016 03:14:16 +0200 (CEST) From: Alexey Kardashevskiy To: qemu-devel@nongnu.org Date: Tue, 21 Jun 2016 11:14:01 +1000 Message-Id: <1466471645-5396-2-git-send-email-aik@ozlabs.ru> X-Mailer: git-send-email 2.5.0.rc3 In-Reply-To: <1466471645-5396-1-git-send-email-aik@ozlabs.ru> References: <1466471645-5396-1-git-send-email-aik@ozlabs.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 83.169.36.222 Subject: [Qemu-devel] [PATCH qemu v18 1/5] memory: Add reporting of supported page sizes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexey Kardashevskiy , Alex Williamson , qemu-ppc@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Every IOMMU has some granularity which MemoryRegionIOMMUOps::translate uses when translating, however this information is not available outside the translate context for various checks. This adds a get_min_page_size callback to MemoryRegionIOMMUOps and a wrapper for it so IOMMU users (such as VFIO) can know the minimum actual page size supported by an IOMMU. As IOMMU MR represents a guest IOMMU, this uses TARGET_PAGE_SIZE as fallback. This removes vfio_container_granularity() and uses new helper in memory_region_iommu_replay() when replaying IOMMU mappings on added IOMMU memory region. Signed-off-by: Alexey Kardashevskiy Reviewed-by: David Gibson Acked-by: Alex Williamson --- Changes: v18: * fixed unnecessary line wrap * s/get_page_sizes/get_min_page_size/ v16: * used memory_region_iommu_get_page_sizes() instead of mr->iommu_ops->get_page_sizes() in memory_region_iommu_replay() v15: * s/qemu_real_host_page_size/TARGET_PAGE_SIZE/ in memory_region_iommu_get_page_sizes v14: * removed vfio_container_granularity(), changed memory_region_iommu_replay() v4: * s/1<bus_offset, tcet->page_shift); } +static uint64_t spapr_tce_get_min_page_size(MemoryRegion *iommu) +{ + sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu); + + return 1ULL << tcet->page_shift; +} + static int spapr_tce_table_post_load(void *opaque, int version_id) { sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque); @@ -228,6 +235,7 @@ static const VMStateDescription vmstate_spapr_tce_table = { static MemoryRegionIOMMUOps spapr_iommu_ops = { .translate = spapr_tce_translate_iommu, + .get_min_page_size = spapr_tce_get_min_page_size, }; static int spapr_tce_table_realize(DeviceState *dev) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 1898f1f..27cc159 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -321,11 +321,6 @@ out: rcu_read_unlock(); } -static hwaddr vfio_container_granularity(VFIOContainer *container) -{ - return (hwaddr)1 << ctz64(container->iova_pgsizes); -} - static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) { @@ -392,9 +387,7 @@ static void vfio_listener_region_add(MemoryListener *listener, QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); - memory_region_iommu_replay(giommu->iommu, &giommu->n, - vfio_container_granularity(container), - false); + memory_region_iommu_replay(giommu->iommu, &giommu->n, false); return; } diff --git a/include/exec/memory.h b/include/exec/memory.h index 4ab6800..e3829f7 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -151,6 +151,8 @@ typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps; struct MemoryRegionIOMMUOps { /* Return a TLB entry that contains a given address. */ IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write); + /* Returns minimum supported page size */ + uint64_t (*get_min_page_size)(MemoryRegion *iommu); }; typedef struct CoalescedMemoryRange CoalescedMemoryRange; @@ -573,6 +575,16 @@ static inline bool memory_region_is_iommu(MemoryRegion *mr) /** + * memory_region_iommu_get_min_page_size: get minimum supported page size + * for an iommu + * + * Returns minimum supported page size for an iommu. + * + * @mr: the memory region being queried + */ +uint64_t memory_region_iommu_get_min_page_size(MemoryRegion *mr); + +/** * memory_region_notify_iommu: notify a change in an IOMMU translation entry. * * @mr: the memory region that was changed @@ -596,16 +608,15 @@ void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n); /** * memory_region_iommu_replay: replay existing IOMMU translations to - * a notifier + * a notifier with the minimum page granularity returned by + * mr->iommu_ops->get_page_size(). * * @mr: the memory region to observe * @n: the notifier to which to replay iommu mappings - * @granularity: Minimum page granularity to replay notifications for * @is_write: Whether to treat the replay as a translate "write" * through the iommu */ -void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n, - hwaddr granularity, bool is_write); +void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n, bool is_write); /** * memory_region_unregister_iommu_notifier: unregister a notifier for diff --git a/memory.c b/memory.c index 8ba496d..fe44e29 100644 --- a/memory.c +++ b/memory.c @@ -1502,12 +1502,22 @@ void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n) notifier_list_add(&mr->iommu_notify, n); } -void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n, - hwaddr granularity, bool is_write) +uint64_t memory_region_iommu_get_min_page_size(MemoryRegion *mr) { - hwaddr addr; + assert(memory_region_is_iommu(mr)); + if (mr->iommu_ops && mr->iommu_ops->get_min_page_size) { + return mr->iommu_ops->get_min_page_size(mr); + } + return TARGET_PAGE_SIZE; +} + +void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n, bool is_write) +{ + hwaddr addr, granularity; IOMMUTLBEntry iotlb; + granularity = (hwaddr)1 << ctz64(memory_region_iommu_get_min_page_size(mr)); + for (addr = 0; addr < memory_region_size(mr); addr += granularity) { iotlb = mr->iommu_ops->translate(mr, addr, is_write); if (iotlb.perm != IOMMU_NONE) {