Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp569680rwb; Fri, 13 Jan 2023 01:10:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXso3oor1A+0e7hDYx9LSFHrtc0b4cd5mNS2Xt256rTyfL908xsTRYVnoiZNS3XpV6gDsF67 X-Received: by 2002:a05:6402:528b:b0:499:b672:ee39 with SMTP id en11-20020a056402528b00b00499b672ee39mr16241938edb.11.1673601054972; Fri, 13 Jan 2023 01:10:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673601054; cv=none; d=google.com; s=arc-20160816; b=SUTZWE28XE53Khr442WAJHeUZ8ihBGkQTAGJ+PUWsfW1RQX1k9MfAI+2IK9QYR25Vc JTroT80J2dZwqcOvR7X0DxQU59RpT3hHgt3yAsJX7FEgpKl5418tmYd0t9vC10fkhuNl +a70JeUHjn+P1QVI5zRaFxYJji9CjWdimx3KD09yPmFvQQVOLlHaR+Tw7yhtQo8oIrpP vA/ZYnvxO2X7I0yOuAugDFxGOUsxxrP6AGvIYnLMjMKzyov1MgfRpwPW8+jaMHO1Y9IQ fEk4B5hrS4Q09xZOQswilp6X9p9c8lchIsYUFl3/i7GYt9//ay7GV+0LGx7SjhfXAa3R 9Mig== 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=8BZHqfo7sFuXP72fnPp/JPxWAqzMlK6d2jnnMQV3zqE=; b=HPj0hfRXUls9MtVip6Wu12EplDU3a/QSrltgTdfxs4dxWdFmyeHcgDo1RvJ8OkOAIv ynLDt764wohZbzTRohBP69Qjg7rUmn4OCK4eq3XAH0i/ozjV89GgqSLUusQI6VrU3Xdn EZtTcBMhJpxsnIOG72SEUWB/kDW+ocP9c5KK/Vh0i+9UHCeatmAszbkyFItEqqN8XCo7 9rXE9PQ569Bh0QaAVyVzPevzLIpmb3lS6RqgqHGHP16CkUiRFcWpWqTz5tTFmAAgGiHH 8D/uUfMCHrvlIPyP2qWw2YiJ39da75pTC6jphDvIdcH8eHoKMNs0c2OTrVPOFWNnaR8D zgcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@igel-co-jp.20210112.gappssmtp.com header.s=20210112 header.b=mCZTClX+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y9-20020a056402440900b00489644e8bd0si25780757eda.610.2023.01.13.01.10.41; Fri, 13 Jan 2023 01:10:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@igel-co-jp.20210112.gappssmtp.com header.s=20210112 header.b=mCZTClX+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240573AbjAMJFW (ORCPT + 53 others); Fri, 13 Jan 2023 04:05:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241034AbjAMJEU (ORCPT ); Fri, 13 Jan 2023 04:04:20 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85F9774640 for ; Fri, 13 Jan 2023 01:04:02 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id d15so22833282pls.6 for ; Fri, 13 Jan 2023 01:04:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=igel-co-jp.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8BZHqfo7sFuXP72fnPp/JPxWAqzMlK6d2jnnMQV3zqE=; b=mCZTClX+9zwz1K99NSs2vAa1hO/adLIB4vn6t+EUxTWLPFL5Ma3Ao5JOXnwQNCUetj FARd3QvLL4iLl5lWb8wlV6zbIX52LEacsNm51RT4Q7z/5RxQKIzp7elDl29oDTeUJHkl BN8zW4m6JUifl0QCt4mSrLlxyQd7xhyJqe7lfNzP5C9tGxpGXnSlLxDebAgPSplqFAPB TGF/RxQHr0AqWA2zm+5n3sKmE9yDOiHT/dsWmpKZHtdxrCmuFds1uDzTyEYSREUyLE7L yr7k4Y5W3kGu/8YMYXmgrqfGSLf9DyAQ1pPtcp10kyz9qQre5n8oLj5M9wKQrQ8I5vJg h26g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8BZHqfo7sFuXP72fnPp/JPxWAqzMlK6d2jnnMQV3zqE=; b=vKZyusijdSZvVjsJqVEt7o/AoXvRKrLr3PTcMGrjthopw9/nPYj/e48wDm6aDkp0r+ 2WuD6qw1Yujt/gDymWsRCzusclbTy1DSjFOmxUPWV+1k0jxnY58BUu7xVyE/dq2CX33Q a+s0WcCWg6+T4Sl0GleH9qTIOTXc+TZEL5tSry6Pake7w60DHu59ym70unJybPYXYhd4 5+5gaVncba0Hx2o1RmT+eqfW5o0vY+VwVYmQKshnnqzCSN0FYz1p2qWoe4hSQcDMhM6K HX8uEh2Wl0oxazcDX0toNVmCSkMxyFGFU+ma4xDn5A4gdj33aLelInjp0BmJ2vbLqE7f Co7Q== X-Gm-Message-State: AFqh2kqg60YCMBBd2Fsw9B8kbKFLjVuSRD0UQW6aOzSCCks3T9ricKKe egwe49dszF5oPYtFOw1/wIMKjA== X-Received: by 2002:a17:902:da83:b0:189:8425:730d with SMTP id j3-20020a170902da8300b001898425730dmr111444062plx.27.1673600641918; Fri, 13 Jan 2023 01:04:01 -0800 (PST) Received: from tyrell.hq.igel.co.jp (napt.igel.co.jp. [219.106.231.132]) by smtp.gmail.com with ESMTPSA id p15-20020a170902e74f00b00189bf5deda3sm13645510plf.133.2023.01.13.01.03.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 01:04:01 -0800 (PST) From: Shunsuke Mie To: Jingoo Han Cc: Gustavo Pimentel , Lorenzo Pieralisi , Rob Herring , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Bjorn Helgaas , Manivannan Sadhasivam , Kishon Vijay Abraham I , Shunsuke Mie , Kunihiko Hayashi , Hou Zhiqiang , Frank Li , Li Chen , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/3] PCI: endpoint: support an alignment aware map/unmaping Date: Fri, 13 Jan 2023 18:03:48 +0900 Message-Id: <20230113090350.1103494-2-mie@igel.co.jp> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230113090350.1103494-1-mie@igel.co.jp> References: <20230113090350.1103494-1-mie@igel.co.jp> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an align_mem operation to the EPC ops, which function is used to pci_epc_map/unmap_addr(). These change to enable mapping for any alignment restriction of EPC. The map function maps an aligned memory to include a requested memory region. Signed-off-by: Shunsuke Mie --- drivers/pci/endpoint/pci-epc-core.c | 57 ++++++++++++++++++++++++----- include/linux/pci-epc.h | 10 +++-- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c index 2542196e8c3d..60d586e05e7d 100644 --- a/drivers/pci/endpoint/pci-epc-core.c +++ b/drivers/pci/endpoint/pci-epc-core.c @@ -430,8 +430,12 @@ EXPORT_SYMBOL_GPL(pci_epc_set_msix); * Invoke to unmap the CPU address from PCI address. */ void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, - phys_addr_t phys_addr) + phys_addr_t phys_addr, void __iomem *virt_addr, size_t size) { + u64 aligned_phys; + void __iomem *aligned_virt; + size_t offset; + if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) return; @@ -441,9 +445,22 @@ void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, if (!epc->ops->unmap_addr) return; + if (epc->ops->align_mem) { + mutex_lock(&epc->lock); + aligned_phys = epc->ops->align_mem(epc, phys_addr, &size); + mutex_unlock(&epc->lock); + } else { + aligned_phys = phys_addr; + } + + offset = phys_addr - aligned_phys; + aligned_virt = virt_addr - offset; + mutex_lock(&epc->lock); - epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr); + epc->ops->unmap_addr(epc, func_no, vfunc_no, aligned_phys); mutex_unlock(&epc->lock); + + pci_epc_mem_free_addr(epc, aligned_phys, aligned_virt, size); } EXPORT_SYMBOL_GPL(pci_epc_unmap_addr); @@ -458,26 +475,46 @@ EXPORT_SYMBOL_GPL(pci_epc_unmap_addr); * * Invoke to map CPU address with PCI address. */ -int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, - phys_addr_t phys_addr, u64 pci_addr, size_t size) +void __iomem *pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, + u64 pci_addr, phys_addr_t *phys_addr, size_t size) { int ret; + u64 aligned_addr; + size_t offset; + void __iomem *virt_addr; if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) - return -EINVAL; + return ERR_PTR(-EINVAL); if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) - return -EINVAL; + return ERR_PTR(-EINVAL); if (!epc->ops->map_addr) - return 0; + return ERR_PTR(-ENOPTSUPP); + + if (epc->ops->align_mem) { + mutex_lock(&epc->lock); + aligned_addr = epc->ops->align_mem(epc, pci_addr, &size); + mutex_unlock(&epc->lock); + } else { + aligned_addr = pci_addr; + } + + offset = pci_addr - aligned_addr; + + virt_addr = pci_epc_mem_alloc_addr(epc, phys_addr, size); + if (!virt_addr) + return ERR_PTR(-ENOMEM); mutex_lock(&epc->lock); - ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr, - size); + ret = epc->ops->map_addr(epc, func_no, vfunc_no, *phys_addr, aligned_addr, size); mutex_unlock(&epc->lock); + if (ret) + return ERR_PTR(ret); - return ret; + *phys_addr += offset; + + return virt_addr + offset; } EXPORT_SYMBOL_GPL(pci_epc_map_addr); diff --git a/include/linux/pci-epc.h b/include/linux/pci-epc.h index a48778e1a4ee..8f29161bce80 100644 --- a/include/linux/pci-epc.h +++ b/include/linux/pci-epc.h @@ -84,6 +84,7 @@ struct pci_epc_ops { phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size, u32 *msi_data, u32 *msi_addr_offset); + u64 (*align_mem)(struct pci_epc *epc, u64 addr, size_t *size); int (*start)(struct pci_epc *epc); void (*stop)(struct pci_epc *epc); const struct pci_epc_features* (*get_features)(struct pci_epc *epc, @@ -218,11 +219,12 @@ int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, struct pci_epf_bar *epf_bar); void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, struct pci_epf_bar *epf_bar); -int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, - phys_addr_t phys_addr, - u64 pci_addr, size_t size); +void __iomem *pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, + u64 pci_addr, phys_addr_t *phys_addr, + size_t size); void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, - phys_addr_t phys_addr); + phys_addr_t phys_addr, void __iomem *virt_addr, + size_t size); int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts); int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no); -- 2.25.1