From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932100AbbC0O5T (ORCPT ); Fri, 27 Mar 2015 10:57:19 -0400 Received: from e23smtp06.au.ibm.com ([202.81.31.148]:37602 "EHLO e23smtp06.au.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753408AbbC0O46 (ORCPT ); Fri, 27 Mar 2015 10:56:58 -0400 From: Alexey Kardashevskiy To: linuxppc-dev@lists.ozlabs.org Cc: Alexey Kardashevskiy , Benjamin Herrenschmidt , Paul Mackerras , Alex Williamson , linux-kernel@vger.kernel.org Subject: [PATCH kernel v7 27/31] powerpc/iommu/ioda2: Add get_table_size() to calculate the size of fiture table Date: Sat, 28 Mar 2015 01:55:11 +1100 Message-Id: <1427468115-2224-28-git-send-email-aik@ozlabs.ru> X-Mailer: git-send-email 2.0.0 In-Reply-To: <1427468115-2224-1-git-send-email-aik@ozlabs.ru> References: <1427468115-2224-1-git-send-email-aik@ozlabs.ru> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15032714-0021-0000-0000-000000FE32FB Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds a way for the IOMMU user to know how much a new table will use so it can be accounted in the locked_vm limit before allocation happens. This stores the allocated table size in pnv_pci_ioda2_create_table() so the locked_vm counter can be updated correctly when a table is being disposed. Signed-off-by: Alexey Kardashevskiy --- arch/powerpc/include/asm/iommu.h | 5 +++ arch/powerpc/platforms/powernv/pci-ioda.c | 54 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/arch/powerpc/include/asm/iommu.h b/arch/powerpc/include/asm/iommu.h index a768a4d..9027b9e 100644 --- a/arch/powerpc/include/asm/iommu.h +++ b/arch/powerpc/include/asm/iommu.h @@ -94,6 +94,7 @@ struct iommu_table { unsigned long it_size; /* Size of iommu table in entries */ unsigned long it_indirect_levels; unsigned long it_level_size; + unsigned long it_allocated_size; unsigned long it_offset; /* Offset into global table */ unsigned long it_base; /* mapped address of tce table */ unsigned long it_index; /* which iommu table this is */ @@ -159,6 +160,10 @@ struct iommu_table_group_ops { void (*set_ownership)(struct iommu_table_group *table_group, bool enable); + unsigned long (*get_table_size)( + __u32 page_shift, + __u64 window_size, + __u32 levels); long (*create_table)(struct iommu_table_group *table_group, int num, __u32 page_shift, diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c b/arch/powerpc/platforms/powernv/pci-ioda.c index 036f3c1..e3ee87d 100644 --- a/arch/powerpc/platforms/powernv/pci-ioda.c +++ b/arch/powerpc/platforms/powernv/pci-ioda.c @@ -1373,6 +1373,57 @@ static void pnv_free_tce_table(unsigned long addr, unsigned size, free_pages(addr, get_order(size << 3)); } +static unsigned long pnv_get_tce_table_size(unsigned shift, unsigned levels, + unsigned long *left) +{ + unsigned long ret, chunk = 1UL << shift, i; + + ret = chunk; + + if (!*left) + return 0; + + --levels; + if (!levels) { + /* This is last level, actual TCEs */ + *left -= min(*left, chunk); + return chunk; + } + + for (i = 0; i < (chunk >> 3); ++i) { + ret += pnv_get_tce_table_size(shift, levels, left); + if (!*left) + break; + } + + return ret; +} + +static unsigned long pnv_ioda2_get_table_size(__u32 page_shift, __u64 window_size, + __u32 levels) +{ + unsigned long tce_table_size, shift, ret; + + if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS)) + return -EINVAL; + + if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size)) + return -EINVAL; + + tce_table_size = (window_size >> page_shift) * 8; + tce_table_size = max(0x1000UL, tce_table_size); + + /* Allocate TCE table */ + shift = ROUND_UP(ilog2(window_size) - page_shift, levels) / levels; + shift += 3; + shift = max_t(unsigned, shift, IOMMU_PAGE_SHIFT_4K); + + ret = tce_table_size; /* tbl->it_userspace */ + ret += pnv_get_tce_table_size(shift, levels, &tce_table_size); + + return ret; +} + static __be64 *pnv_alloc_tce_table(int nid, unsigned shift, unsigned levels, unsigned long *left) { @@ -1452,6 +1503,8 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group, return -ENOMEM; tbl->it_indirect_levels = levels - 1; + tbl->it_allocated_size = pnv_ioda2_get_table_size(page_shift, + window_size, levels); /* Setup linux iommu table */ pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, @@ -1679,6 +1732,7 @@ static long pnv_pci_ioda2_create_table_with_uas( static struct iommu_table_group_ops pnv_pci_ioda2_ops = { .set_ownership = pnv_ioda2_set_ownership, + .get_table_size = pnv_ioda2_get_table_size, .create_table = pnv_pci_ioda2_create_table_with_uas, .set_window = pnv_pci_ioda2_set_window, .unset_window = pnv_pci_ioda2_unset_window, -- 2.0.0