Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp491489pxy; Fri, 30 Apr 2021 09:33:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNDA+1uPz6Q3Y2Euw9hBG2W9LIaaJ586govtKU7Zip04aOLhjBU7WyBArf9KAHYYDqEDyU X-Received: by 2002:a65:4082:: with SMTP id t2mr5448394pgp.396.1619800418998; Fri, 30 Apr 2021 09:33:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619800418; cv=none; d=google.com; s=arc-20160816; b=rB93kEaIgqUx+APS1KgBRZu9mcYMy5gnQcAiakTKUILEM1yr1ykuVBFExj5y4yoVRg 3zw3YPa1zIakox+Z+pfziJAdvtae/G3wTtd/XzZ7A0S89nD0WGsxbMGGTF99qUvBxVnj 4PxPQRvtWG2zz35Vs3qITEEn3u1+j6v/EXMt94cE2iV6zYIxH3KzItZEiDdC0ta/2Reh 1P9S/C1XjOVPC4ex/RGWoEbWmTIy+qA2eA+qDYFfh6nAUEV9OvbOHuCZzZ3kq6h6LsCV uneIsROmPGkGcy7BGQzBL6Joz/uyQhlmiCs595DWe0/1sK7XpvxoO9UeY02W7pZ+QgTY 5S+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Q3DTkzRxiL4cvjvAEnGwxVmcrQ9s6v5oJ6rrdiNfEWQ=; b=U1t9IRp2prSVMpfqibDQt+yYblqhUNjYR6RcQ7y0o1SUnA2OqC8bTOjY71o87f2XE4 HwjrIzbD429L50+cI8ima4iLIO1wQb6QCPntJRDp8PhIhletAFA3baSj+OigbRatgI9s ysxkuiibN3tQx/4Lp1PuwwADtAeEargY6IXqNc4biB+eZgZDI+Xwb4tQ/8myuWubS0+g iowINyy6/F/TgFeyPjlNarHfE4olwBe7EbmcgW+EY4xMf5f21O5BVKYlv/N7oVfrDY3a efsgKFq3EY7igT2Bz20Oo429jqgcWNIBc0MyKtbx8E1tJ+EytvluY3yAGG03oTZ5Cgnq LeHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RnJznbf6; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p24si3996434pgk.480.2021.04.30.09.33.26; Fri, 30 Apr 2021 09:33:38 -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=@gmail.com header.s=20161025 header.b=RnJznbf6; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231384AbhD3QdZ (ORCPT + 99 others); Fri, 30 Apr 2021 12:33:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231202AbhD3QdP (ORCPT ); Fri, 30 Apr 2021 12:33:15 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A26AC06138E for ; Fri, 30 Apr 2021 09:32:26 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id a2so1455932qkh.11 for ; Fri, 30 Apr 2021 09:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q3DTkzRxiL4cvjvAEnGwxVmcrQ9s6v5oJ6rrdiNfEWQ=; b=RnJznbf63Ma9ym79l+9H98O/cUg655H2OJggOoy7WQiIGcwxq2Yl3h4u/Y/0ZavB50 N6Lzg6cu8My2V4eIo0+NyPnskXtvVAq3KfdI4I2wPicdJ8Bov27ma5TFQJ/lTFkVLbGQ bBHXoAdSlP6zKzI4OJzp6/VG75FvRAuz9bRgDFf72/GoZ1wVzR0kF/NmDytAVpHRJGQb 6n/G5f+10j2UdWEH3FHz7a5s3D8ezvo5rFaU8rVt5ZwgyXEFmthvwtG7NrnLKjdLTdzg yCt3UQSArrwYA2+J0CD8JlQ7hDohAjKHHVjz3XfL7UM0euA0zKPfBaxK7byIEJmZ+YHx VC1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q3DTkzRxiL4cvjvAEnGwxVmcrQ9s6v5oJ6rrdiNfEWQ=; b=F6OOKIacNqk5pH+fE4LNv4pFVIUwTPz4ABMamlt6WpT0Dve2Cu/aRhHP0iAU+TdnrP w3gKS3ZbIDyajhvtNCR8lJK0eNG/lYgQkFHjKHsDkQ0O3GWm3MYiUFYI4q+I8bwVmwy2 bk1MXYXcTjGzkFsP1aCa5SpowgIvxZxRjINEWgKKZbTVWA9B73y8NUx4Toyf26Pao2QV rBTBg1/Q4IlXrlGr3L5eqHenl17Hnc8QwoIf0IExyAwLTWm1eqILb3rtKUIUV6toiVJf rDCGYHezrehvHgLY2/H/rGVPVfg/hGP3Pa1Q6KdQfcf1O6zJYvucwZg8tuklvUBSg38E 375w== X-Gm-Message-State: AOAM532GGTQgzo/m2WpIVZvCgaXiO2dQdKoWF/Oeyg4dusoLFesFPtAR RNS6g0VavzYXjiuzRx7cAw0= X-Received: by 2002:a37:7e03:: with SMTP id z3mr6395090qkc.367.1619800345799; Fri, 30 Apr 2021 09:32:25 -0700 (PDT) Received: from li-908e0a4c-2250-11b2-a85c-f027e903211b.ibm.com.com ([2804:14c:482:7b04::1001]) by smtp.gmail.com with ESMTPSA id p1sm2624334qtu.89.2021.04.30.09.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Apr 2021 09:32:25 -0700 (PDT) From: Leonardo Bras To: Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Joel Stanley , Christophe Leroy , Leonardo Bras , Alexey Kardashevskiy , Nicolin Chen , Niklas Schnelle Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 06/11] powerpc/pseries/iommu: Add ddw_property_create() and refactor enable_ddw() Date: Fri, 30 Apr 2021 13:31:41 -0300 Message-Id: <20210430163145.146984-7-leobras.c@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210430163145.146984-1-leobras.c@gmail.com> References: <20210430163145.146984-1-leobras.c@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Code used to create a ddw property that was previously scattered in enable_ddw() is now gathered in ddw_property_create(), which deals with allocation and filling the property, letting it ready for of_property_add(), which now occurs in sequence. This created an opportunity to reorganize the second part of enable_ddw(): Without this patch enable_ddw() does, in order: kzalloc() property & members, create_ddw(), fill ddwprop inside property, ddw_list_new_entry(), do tce_setrange_multi_pSeriesLP_walk in all memory, of_add_property(), and list_add(). With this patch enable_ddw() does, in order: create_ddw(), ddw_property_create(), of_add_property(), ddw_list_new_entry(), do tce_setrange_multi_pSeriesLP_walk in all memory, and list_add(). This change requires of_remove_property() in case anything fails after of_add_property(), but we get to do tce_setrange_multi_pSeriesLP_walk in all memory, which looks the most expensive operation, only if everything else succeeds. Signed-off-by: Leonardo Bras --- arch/powerpc/platforms/pseries/iommu.c | 93 ++++++++++++++++---------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/arch/powerpc/platforms/pseries/iommu.c b/arch/powerpc/platforms/pseries/iommu.c index 955cf095416c..5a70ecd579b8 100644 --- a/arch/powerpc/platforms/pseries/iommu.c +++ b/arch/powerpc/platforms/pseries/iommu.c @@ -1122,6 +1122,35 @@ static void reset_dma_window(struct pci_dev *dev, struct device_node *par_dn) ret); } +static struct property *ddw_property_create(const char *propname, u32 liobn, u64 dma_addr, + u32 page_shift, u32 window_shift) +{ + struct dynamic_dma_window_prop *ddwprop; + struct property *win64; + + win64 = kzalloc(sizeof(*win64), GFP_KERNEL); + if (!win64) + return NULL; + + win64->name = kstrdup(propname, GFP_KERNEL); + ddwprop = kzalloc(sizeof(*ddwprop), GFP_KERNEL); + win64->value = ddwprop; + win64->length = sizeof(*ddwprop); + if (!win64->name || !win64->value) { + kfree(win64->name); + kfree(win64->value); + kfree(win64); + return NULL; + } + + ddwprop->liobn = cpu_to_be32(liobn); + ddwprop->dma_base = cpu_to_be64(dma_addr); + ddwprop->tce_shift = cpu_to_be32(page_shift); + ddwprop->window_shift = cpu_to_be32(window_shift); + + return win64; +} + /* Return largest page shift based on "IO Page Sizes" output of ibm,query-pe-dma-window. */ static int iommu_get_page_shift(u32 query_page_size) { @@ -1167,11 +1196,11 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn) struct ddw_query_response query; struct ddw_create_response create; int page_shift; + u64 win_addr; struct device_node *dn; u32 ddw_avail[DDW_APPLICABLE_SIZE]; struct direct_window *window; struct property *win64 = NULL; - struct dynamic_dma_window_prop *ddwprop; struct failed_ddw_pdn *fpdn; bool default_win_removed = false; bool pmem_present; @@ -1286,65 +1315,54 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn) 1ULL << page_shift); goto out_failed; } - win64 = kzalloc(sizeof(struct property), GFP_KERNEL); - if (!win64) { - dev_info(&dev->dev, - "couldn't allocate property for 64bit dma window\n"); - goto out_failed; - } - win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL); - win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL); - win64->length = sizeof(*ddwprop); - if (!win64->name || !win64->value) { - dev_info(&dev->dev, - "couldn't allocate property name and value\n"); - goto out_free_prop; - } ret = create_ddw(dev, ddw_avail, &create, page_shift, len); if (ret != 0) - goto out_free_prop; - - ddwprop->liobn = cpu_to_be32(create.liobn); - ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) | - create.addr_lo); - ddwprop->tce_shift = cpu_to_be32(page_shift); - ddwprop->window_shift = cpu_to_be32(len); + goto out_failed; dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %pOF\n", create.liobn, dn); - window = ddw_list_new_entry(pdn, ddwprop); + win_addr = ((u64)create.addr_hi << 32) | create.addr_lo; + win64 = ddw_property_create(DIRECT64_PROPNAME, create.liobn, win_addr, + page_shift, len); + if (!win64) { + dev_info(&dev->dev, + "couldn't allocate property, property name, or value\n"); + goto out_remove_win; + } + + ret = of_add_property(pdn, win64); + if (ret) { + dev_err(&dev->dev, "unable to add dma window property for %pOF: %d", + pdn, ret); + goto out_free_prop; + } + + window = ddw_list_new_entry(pdn, win64->value); if (!window) - goto out_clear_window; + goto out_del_prop; ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT, win64->value, tce_setrange_multi_pSeriesLP_walk); if (ret) { dev_info(&dev->dev, "failed to map direct window for %pOF: %d\n", dn, ret); - goto out_free_window; - } - - ret = of_add_property(pdn, win64); - if (ret) { - dev_err(&dev->dev, "unable to add dma window property for %pOF: %d", - pdn, ret); - goto out_free_window; + goto out_del_list; } spin_lock(&direct_window_list_lock); list_add(&window->list, &direct_window_list); spin_unlock(&direct_window_list_lock); - dev->dev.archdata.dma_offset = be64_to_cpu(ddwprop->dma_base); + dev->dev.archdata.dma_offset = win_addr; goto out_unlock; -out_free_window: +out_del_list: kfree(window); -out_clear_window: - remove_ddw(pdn, true); +out_del_prop: + of_remove_property(pdn, win64); out_free_prop: kfree(win64->name); @@ -1352,6 +1370,9 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn) kfree(win64); win64 = NULL; +out_remove_win: + remove_ddw(pdn, true); + out_failed: if (default_win_removed) reset_dma_window(dev, pdn); -- 2.30.2