From patchwork Wed Oct 12 18:13:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lizhi Hou X-Patchwork-Id: 1689263 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=DFD2G4Xq; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by legolas.ozlabs.org (Postfix) with ESMTP id 4Mngm36r7Jz23jX for ; Thu, 13 Oct 2022 05:14:43 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229701AbiJLSOk (ORCPT ); Wed, 12 Oct 2022 14:14:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbiJLSOh (ORCPT ); Wed, 12 Oct 2022 14:14:37 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2080.outbound.protection.outlook.com [40.107.93.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 025F5FE750; Wed, 12 Oct 2022 11:14:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NxclKlW2hA182K/bRU5tjKfNdXUIQvqy3dBIyOQKRUPoa7ry7TyFQsmhbAmtU/HRnlh3k/k3v7q8NpslA7ra4oLmVbRA0XCes60WQXhlZOO5HkTqWGgf+F+Q+zL6qvsNbBDcUFp3F8GxUxlMuIATNTwAA/JYxYWxsb67eod/C60tvyjGYfaD//Dnd3RQ/bvu1kt35dW6e0FVMol+OMD1snL96/CU2kNjxVGoXTVye0Gk/rBgWbDdl3W/BEfI53t+g9qSkgYAk2Dm4mslI7gRIXkwKZp2czJypCv+lY1X5K2xYsCdM3nFp8C4aI9s/7s1MSddnghMKpQkuxGKYUMOCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=S6ckbe4rVitzLrY6iyJ2lmey+Bxaz72/a+CjZ7YJfzk=; b=Q2nNMxT3to1AWHzdVvMUFjcemKp1hA18bFdoa4uYJzumEByD82/oR/0B81HPaXe+vR0BfbwCDXGf1mx/hTmy+AQOCXfMhLzoIq/sfNqG3W1tSG9hba0/y9G5MTeRRrV+e/bPTqzzTpOOv5l0QAlZ5cmtViAFBrWy5nhVUnidKi7BwKzVPp7+yuxnyrxEmD0GZC5FNV9fBBuSfsPMAD2n8nUHXrMedN/ByHQZ0t/pur6gnDReANlZ6eZ7sv1NC20wFB4NG+regZHlgw6K682smBtsgN71hcr8NbqNsBS8Hl3HpVnvsFdnAEVmuKEkh3iGe/B903wRgQveZfz1I5s1rw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=S6ckbe4rVitzLrY6iyJ2lmey+Bxaz72/a+CjZ7YJfzk=; b=DFD2G4XqVgYYXiS+P3KdqcRc8uRtbpbr75VeCX7DAa+Xd5oFUNPIKmzxOsONelKo0hWwBdVg/R2IrVmD4jp4v95sYhTqBzVGvrTqLMPx3r1+pIp67tuYA5Mw+cRlKdq8RkxG++YAubj+2c/FyqZU5+k0u8GKYnPoqjCbiSG/PFM= Received: from DM6PR21CA0019.namprd21.prod.outlook.com (2603:10b6:5:174::29) by SJ0PR12MB6830.namprd12.prod.outlook.com (2603:10b6:a03:47c::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5709.15; Wed, 12 Oct 2022 18:14:31 +0000 Received: from DM6NAM11FT026.eop-nam11.prod.protection.outlook.com (2603:10b6:5:174:cafe::91) by DM6PR21CA0019.outlook.office365.com (2603:10b6:5:174::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.1 via Frontend Transport; Wed, 12 Oct 2022 18:14:31 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT026.mail.protection.outlook.com (10.13.172.161) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5723.20 via Frontend Transport; Wed, 12 Oct 2022 18:14:31 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Wed, 12 Oct 2022 13:14:30 -0500 Received: from xsjlizhih40.xilinx.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2375.28 via Frontend Transport; Wed, 12 Oct 2022 13:14:29 -0500 From: Lizhi Hou To: , , , , , CC: Lizhi Hou , , , , , , , Subject: [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node() Date: Wed, 12 Oct 2022 11:13:58 -0700 Message-ID: <1665598440-47410-2-git-send-email-lizhi.hou@amd.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com> References: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT026:EE_|SJ0PR12MB6830:EE_ X-MS-Office365-Filtering-Correlation-Id: c3082ea0-b394-43e6-bbec-08daac7d9bf5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +Z1eegtTTxN2IVnqeZQvvIf+IT/pSM+Crg4Oq2CrFeJBd8C75posUIdoO+CIufOn+aqlHxnfh5Lvta0txE2K1XgjdZhz65kDURyeTgBx40RijsJ3G4HfOJN0TA1I4D3PR51Yildc6B/gKTLoi2rwZGwA+6Ie3xYMALkho7nF/HzyKx6awhjIa3rr4li1QPpnqIIYzJJs9kvfEL+3y8Vzrd0qLWVqj8Q4krwOmyIqwNmF56QscGhhImtRsLl0SVro6+I0ujOvFkD5CQPlk6vkvoL+pVkdyNSJZvR1S9XQqEmM/etratkXBRcMIazDFuuQadUykGjHdkTn6uMjqrwJx8SxAQrYjeMBy1tYsk+YrRC70rYpJFQrGETpw8jqZHIV5A5wCbqhYQeriYYTyJo3VYwkNKxCmsrkWixe5TkRSG1BiSUmT2vQ2EK/Nu5DcwK/8UHpIdbgA4aIjc0k1f2cyRA2zwgIMjUJO22ff+1dl8EiFg1hEaKKPWVIpBDFHWztcthaGxS66DQdAcuk//V6D9sJW/LBSp9lNbydW/WwEQFVT/BYDrOg9f0CH9pUYY/Ac/rGRasV25YJWRMuTJvNDN0Q117/Q0M4i6gzzttMvpP9d8DChlH7uttcP/21DfU9MxUEP0mWXLtz3pSIZmw0aS6qRcyWn8Fyo8bosKcxnareV9/0rgUA5ZdQf6PyjN0UX2Sutx3TyVT2yvmBllJEfXcQeUym7X9dhoa/FdWjoQMBt8OpWHdlJ+fSn6RGsgP2kUgc7gsQMf3P1/5vowq8ULq8eC9Ln4nCgPQRoNPcBZaxAldWcVX04jasNmhZdiUr X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(376002)(396003)(39860400002)(346002)(451199015)(40470700004)(46966006)(36840700001)(81166007)(36860700001)(4326008)(86362001)(40460700003)(5660300002)(82740400003)(36756003)(40480700001)(356005)(316002)(478600001)(110136005)(8676002)(8936002)(2906002)(82310400005)(41300700001)(70206006)(336012)(2616005)(426003)(47076005)(44832011)(26005)(186003)(70586007)(6666004)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Oct 2022 18:14:31.3027 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c3082ea0-b394-43e6-bbec-08daac7d9bf5 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT026.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6830 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,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-pci@vger.kernel.org of_create_node() creates device node and apply to base tree dynamically. The parent device node and full name are required for creating the node. And the caller can also provide a property array for the node. Inside this function, it creates a changeset. Then the new device node and properties are added to the changeset and applied to base tree. The pointer of this changeset is saved in device node private data. of_destroy_node() removes the node created by of_create_node() from the base tree and free it. It gets the changeset pointer from device node private data and call of_changeset_destroy() to free everything. Signed-off-by: Lizhi Hou Signed-off-by: Sonal Santan Signed-off-by: Max Zhen Signed-off-by: Brian Xu --- drivers/of/dynamic.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 4 +++ 2 files changed, 84 insertions(+) diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c index cd3821a6444f..eca28b723706 100644 --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -934,3 +934,83 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action, return 0; } EXPORT_SYMBOL_GPL(of_changeset_action); + +/** + * of_create_node - Dynamically create a device node and apply it to base tree + * + * @parent: Pointer to parent device node + * @full_name: Full name of device node + * @props: Pointer to property array + * + * Return: Pointer to the created device node or NULL in case of an error. + */ +struct device_node *of_create_node(struct device_node *parent, + const char *full_name, + struct property *props) +{ + struct of_changeset *cset; + struct property *new_pp; + struct device_node *np; + int ret, i; + + cset = kzalloc(sizeof(*cset), GFP_KERNEL); + if (!cset) + return NULL; + + of_changeset_init(cset); + + np = __of_node_dup(NULL, full_name); + if (!np) + goto failed; + np->parent = parent; + + ret = of_changeset_attach_node(cset, np); + if (ret) + goto failed; + + if (props) { + for (i = 0; props[i].name; i++) { + new_pp = __of_prop_dup(&props[i], GFP_KERNEL); + if (!new_pp) + goto failed; + ret = of_changeset_add_property(cset, np, new_pp); + if (ret) { + kfree(new_pp->name); + kfree(new_pp->value); + kfree(new_pp); + goto failed; + } + } + } + + ret = of_changeset_apply(cset); + if (ret) + goto failed; + + np->data = cset; + + return np; + +failed: + of_changeset_destroy(cset); + if (np) + of_node_put(np); + + return NULL; +} + +/** + * of_destroy_node - Destroy a dynamically created device node + * + * @np: Pointer to dynamically created device node + * + */ +void of_destroy_node(struct device_node *np) +{ + struct of_changeset *cset; + + cset = (struct of_changeset *)np->data; + of_changeset_destroy(cset); + of_node_put(np); + kfree(cset); +} diff --git a/include/linux/of.h b/include/linux/of.h index 766d002bddb9..493ef957c1a8 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -1475,6 +1475,10 @@ extern int of_changeset_revert(struct of_changeset *ocs); extern int of_changeset_action(struct of_changeset *ocs, unsigned long action, struct device_node *np, struct property *prop); +struct device_node *of_create_node(struct device_node *parent, + const char *full_name, + struct property *props); +void of_destroy_node(struct device_node *np); static inline int of_changeset_attach_node(struct of_changeset *ocs, struct device_node *np) From patchwork Wed Oct 12 18:13:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lizhi Hou X-Patchwork-Id: 1689267 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=fsNI4SVD; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by legolas.ozlabs.org (Postfix) with ESMTP id 4Mngm71Dkxz23jX for ; Thu, 13 Oct 2022 05:14:47 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229733AbiJLSOo (ORCPT ); Wed, 12 Oct 2022 14:14:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiJLSOh (ORCPT ); Wed, 12 Oct 2022 14:14:37 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2062.outbound.protection.outlook.com [40.107.96.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14DD2FE75A; Wed, 12 Oct 2022 11:14:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Gp7CpRyCnsXlFtEOtRi+kcPk5/aSNWtQ7YXXkjeJF9pHAAJ0Fi5qX5DheqBhogD719pPs7TKVykE2SE9AJLCgDmKfI+ZKCn1TqeLT+7DleUlpY9WNDLhFfGG0x/cUbuc103OjtxAWx3YyRnjwI9EcIy0uNYM7pbSWFHAHhhQZQFsOJTyFIC8e16sZtRHIGjYEnL2jC/UnrMqI7EMzVv0REdx85Fm6QaOLR9JgKv7S7yvTOEOvQW0K8GbCOkpm1eIdUTDVqJCSMwLh2j5AE9MIV0CKDiHacHJkEarY/NlbtbaaYJZGg15DbPXYJz7F23je+GUixp0wwi7RBTNy895WA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GyFfnoyng2I6wWzTJJ3eMqzGxm8D/1xhnn35e0+/7Tk=; b=FEJ4DmlBnoHgWKQhAx3KL+L+cSbjbx+l1WKu8kymqgQm81w+WDNY1zBacuWt21Mscbhmk1HwjvGe0HA8Rb7Ez0AB0eqCapgYooNlCrAe3auFLAXA2uuLQezMQen0eerE1QvHjtFEed8b2NClNFtfWucFibhE66hzRd13wURQ2Rq7TRWbV2WehXCV5FSweygKfcZcnL/auc5EoRqBx5iJFDH4oESE1tjNvR46YH6bkHGv1wLbdNaEQGkTGfqZOOpUrThxUcfrHlvwUCv+8cyc37Xk8cK6WPEdy05WmgMnAoUCvub4oKFqqxJ8Ttsi/8sSjG9edcjEBjJx019Ifwa7Mg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GyFfnoyng2I6wWzTJJ3eMqzGxm8D/1xhnn35e0+/7Tk=; b=fsNI4SVDcuLi6WsXCVKnawOH4m8AvxItajYsJY9wYiPpeMWKMVqNzobANnH0MyG9UGye6Ne2cQX+UUhVQp1mPy3+PQaDjH1J/UUTMJjbXarWKBVCTzNyshXapx163EWXvS2syPp72fPa/mpRI0Qauug+8R/yULzJ6nLmjuv05QY= Received: from DM6PR18CA0033.namprd18.prod.outlook.com (2603:10b6:5:15b::46) by BN9PR12MB5337.namprd12.prod.outlook.com (2603:10b6:408:102::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5709.21; Wed, 12 Oct 2022 18:14:32 +0000 Received: from DM6NAM11FT099.eop-nam11.prod.protection.outlook.com (2603:10b6:5:15b:cafe::11) by DM6PR18CA0033.outlook.office365.com (2603:10b6:5:15b::46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5709.19 via Frontend Transport; Wed, 12 Oct 2022 18:14:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT099.mail.protection.outlook.com (10.13.172.241) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5723.20 via Frontend Transport; Wed, 12 Oct 2022 18:14:32 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Wed, 12 Oct 2022 13:14:32 -0500 Received: from xsjlizhih40.xilinx.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2375.28 via Frontend Transport; Wed, 12 Oct 2022 13:14:31 -0500 From: Lizhi Hou To: , , , , , CC: Lizhi Hou , , , , , , , Subject: [PATCH RFC V2 2/3] PCI: Create device tree node for selected devices Date: Wed, 12 Oct 2022 11:13:59 -0700 Message-ID: <1665598440-47410-3-git-send-email-lizhi.hou@amd.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com> References: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT099:EE_|BN9PR12MB5337:EE_ X-MS-Office365-Filtering-Correlation-Id: 2169858a-9368-476f-ebbf-08daac7d9cc0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CVZOqaIWVf6wcKoeVnq/NEigEHwo6YO/pvJfJ+Afv96qXd8G+rBbyB+n3WOh4K1DA/jNoo5ZqrAJUwV/uqG0SHuFWY24ewFzDrRuPuTORjRegmoJBU5CgkG/UL0w6MyokS9R+E61Q1CK/mnakZQjaxqUw58lmyMXuo6gOnWcl3ihdA8wyEHETTyq5bt8VhzKMoJWI/rN4Jv1eXG24ZG4fw21gKBAWBtjKq+XwT7VNsD4IvP7XrGnacUnoUakdGJxUm/yRXU5v3Gw2Fywkc3YYrFuWFT+5iGYN9ZYy1McTnPJ9r8vsMJ/Gv4aSPy/lhLgRHFBktRd2wEFXr4/TACH5HeKv5GTownaBKZEYeVtN53vRG023MG27V7G85kQa9FseqO+btp14mvzO8UJ5qUSIsrFK37ZV/UL+IkqaEooQn3w/KDNq4v1cSiWzYyg6FNOxuQL/oNkyUgR6MU5nxCTl/LOZJo9QlAa4cm0Fl64vdnw0IYYZQAoru63i+QdfeFL1h/tLtB4pMlgpCAOSLgOan9XJYjeydXjR41PptDyXekjUbnxqED1lcigmC4yTxA/QioO8/NhjG6KbXP9dbdS1M33lNNjfGfn9XGt63pmtCK5jeah/m39b/PzTRLzTLqsbBgyD0hpmfmTaS2hL2SoVsdTRfOHnWx1jH0+5f7T+vWJL0kxI8pc6NdD8lev/ngrpDY3rJ8AQi1TRItG+GqeDSiw9rj99xen7/b7Y1aO2oQJ4pjx+XyT8KzqnBEZt1XjkJyBOiNIVe2IVjqtTizLE3K4Y+fml/TuCGK/SEz8EQI= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(346002)(376002)(396003)(136003)(451199015)(36840700001)(40470700004)(46966006)(36860700001)(426003)(54906003)(316002)(83380400001)(356005)(2906002)(47076005)(82740400003)(82310400005)(81166007)(44832011)(86362001)(70586007)(8676002)(70206006)(4326008)(8936002)(5660300002)(41300700001)(2616005)(40460700003)(336012)(186003)(26005)(40480700001)(110136005)(478600001)(6666004)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Oct 2022 18:14:32.6343 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2169858a-9368-476f-ebbf-08daac7d9cc0 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT099.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5337 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,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-pci@vger.kernel.org The PCIe endpoint device such as Xilinx Alveo PCIe card maps the register spaces from multiple hardware peripherals to its PCIe BAR. Normally, the PCI core discovers devices and BARs using the PCI enumeration process. There is no infrastructure to discover the hardware peripherals that are present in a PCI device, and which can be accessed through the PCI BARs. For Alveo PCIe card, the card firmware provides a flattened device tree to describe the hardware peripherals on its BARs. The Alveo card driver can load this flattened device tree and leverage device tree framework to generate platform devices for the hardware peripherals eventually. Apparently, the device tree framework requires a device tree node for the PCIe device. Thus, it can generate the device tree nodes for hardware peripherals underneath. Because PCIe is self discoverable bus, there might not be a device tree node created for PCIe devices. This patch is to add support to generate device tree node for PCIe devices. It introduces a kernel option. When the option is turned on, the kernel will generate device tree nodes for PCI bridges unconditionally. It will also generate a device tree node for Xilinx Alveo U50 by using PCI quirks. The generated device tree nodes do not have any property. The future patches will add necessary properties. Signed-off-by: Lizhi Hou Signed-off-by: Sonal Santan Signed-off-by: Max Zhen Signed-off-by: Brian Xu --- drivers/pci/Kconfig | 12 ++++++++ drivers/pci/bus.c | 2 ++ drivers/pci/msi/irqdomain.c | 6 ++-- drivers/pci/of.c | 61 +++++++++++++++++++++++++++++++++++++ drivers/pci/pci-driver.c | 3 +- drivers/pci/pci.h | 16 ++++++++++ drivers/pci/quirks.c | 11 +++++++ drivers/pci/remove.c | 1 + 8 files changed, 109 insertions(+), 3 deletions(-) diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig index 55c028af4bd9..8243a92fd97d 100644 --- a/drivers/pci/Kconfig +++ b/drivers/pci/Kconfig @@ -198,6 +198,18 @@ config PCI_HYPERV The PCI device frontend driver allows the kernel to import arbitrary PCI devices from a PCI backend to support PCI driver domains. +config PCI_DYNAMIC_OF_NODES + bool "Device tree node for PCI devices" + depends on OF + select OF_DYNAMIC + help + This option enables support for generating device tree nodes for some + PCI devices. Thus, the driver of this kind can load and overlay + flattened device tree for its downstream devices. + + Once this option is selected, the device tree nodes will be generated + for all PCI/PCIE bridges. + choice prompt "PCI Express hierarchy optimization setting" default PCIE_BUS_DEFAULT diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c index 3cef835b375f..8507cc32b61d 100644 --- a/drivers/pci/bus.c +++ b/drivers/pci/bus.c @@ -316,6 +316,8 @@ void pci_bus_add_device(struct pci_dev *dev) */ pcibios_bus_add_device(dev); pci_fixup_device(pci_fixup_final, dev); + if (pci_is_bridge(dev)) + of_pci_make_dev_node(dev); pci_create_sysfs_dev_files(dev); pci_proc_attach_device(dev); pci_bridge_d3_update(dev); diff --git a/drivers/pci/msi/irqdomain.c b/drivers/pci/msi/irqdomain.c index e9cf318e6670..eeaf44169bfd 100644 --- a/drivers/pci/msi/irqdomain.c +++ b/drivers/pci/msi/irqdomain.c @@ -230,8 +230,10 @@ u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev) pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); of_node = irq_domain_get_of_node(domain); - rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) : - iort_msi_map_id(&pdev->dev, rid); + if (of_node && !of_node_check_flag(of_node, OF_DYNAMIC)) + rid = of_msi_map_id(&pdev->dev, of_node, rid); + else + rid = iort_msi_map_id(&pdev->dev, rid); return rid; } diff --git a/drivers/pci/of.c b/drivers/pci/of.c index 196834ed44fe..83e042f495a6 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -469,6 +469,8 @@ static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args * } else { /* We found a P2P bridge, check if it has a node */ ppnode = pci_device_to_OF_node(ppdev); + if (of_node_check_flag(ppnode, OF_DYNAMIC)) + ppnode = NULL; } /* @@ -599,6 +601,65 @@ int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge) return pci_parse_request_of_pci_ranges(dev, bridge); } +#if IS_ENABLED(CONFIG_PCI_DYNAMIC_OF_NODES) + +void of_pci_remove_node(struct pci_dev *pdev) +{ + struct device_node *dt_node; + + dt_node = pci_device_to_OF_node(pdev); + if (!dt_node || !of_node_check_flag(dt_node, OF_DYNAMIC)) + return; + pdev->dev.of_node = NULL; + + of_destroy_node(dt_node); +} + +void of_pci_make_dev_node(struct pci_dev *pdev) +{ + struct device_node *parent, *dt_node; + const char *pci_type = "dev"; + char *full_name; + + /* + * if there is already a device tree node linked to this device, + * return immediately. + */ + if (pci_device_to_OF_node(pdev)) + return; + + /* check if there is device tree node for parent device */ + if (!pdev->bus->self) + parent = pdev->bus->dev.of_node; + else + parent = pdev->bus->self->dev.of_node; + if (!parent) + return; + + if (pci_is_bridge(pdev)) + pci_type = "pci"; + + full_name = kasprintf(GFP_KERNEL, "%s@%x,%x", pci_type, + PCI_SLOT(pdev->devfn), + PCI_FUNC(pdev->devfn)); + if (!full_name) + goto failed; + + dt_node = of_create_node(parent, full_name, NULL); + if (!dt_node) + goto failed; + + kfree(full_name); + + pdev->dev.of_node = dt_node; + + return; + +failed: + kfree(full_name); +} +#endif + #endif /* CONFIG_PCI */ /** diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c index 49238ddd39ee..1540c4c9a770 100644 --- a/drivers/pci/pci-driver.c +++ b/drivers/pci/pci-driver.c @@ -1628,7 +1628,8 @@ static int pci_dma_configure(struct device *dev) bridge = pci_get_host_bridge_device(to_pci_dev(dev)); if (IS_ENABLED(CONFIG_OF) && bridge->parent && - bridge->parent->of_node) { + bridge->parent->of_node && + !of_node_check_flag(bridge->parent->of_node, OF_DYNAMIC)) { ret = of_dma_configure(dev, bridge->parent->of_node, true); } else if (has_acpi_companion(bridge)) { struct acpi_device *adev = to_acpi_device_node(bridge->fwnode); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 785f31086313..e0a11497b1ad 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -678,6 +678,22 @@ static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_br #endif /* CONFIG_OF */ +#ifdef CONFIG_PCI_DYNAMIC_OF_NODES +void of_pci_make_dev_node(struct pci_dev *pdev); +void of_pci_remove_node(struct pci_dev *pdev); + +#else +static inline void +of_pci_make_dev_node(struct pci_dev *pdev) +{ +} + +static inline void +of_pci_remove_node(struct pci_dev *pdev) +{ +} +#endif /* CONFIG_OF_DYNAMIC */ + #ifdef CONFIG_PCIEAER void pci_no_aer(void); void pci_aer_init(struct pci_dev *dev); diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index 4944798e75b5..5d76932f59ec 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -5956,3 +5956,14 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56b1, aspm_l1_acceptable_latency DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56c0, aspm_l1_acceptable_latency); DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x56c1, aspm_l1_acceptable_latency); #endif + +/* + * For PCI device which have multiple downstream devices, its driver may use + * a flattened device tree to describe the downstream devices. + * To overlay the flattened device tree, the PCI device and all its ancestor + * devices need to have device tree nodes on system base device tree. Thus, + * before driver probing, it might need to add a device tree node as the final + * fixup. + */ +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_XILINX, 0x5020, of_pci_make_dev_node); +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_XILINX, 0x5021, of_pci_make_dev_node); diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c index 4c54c75050dc..0eaa9d9a3609 100644 --- a/drivers/pci/remove.c +++ b/drivers/pci/remove.c @@ -23,6 +23,7 @@ static void pci_stop_dev(struct pci_dev *dev) device_release_driver(&dev->dev); pci_proc_detach_device(dev); pci_remove_sysfs_dev_files(dev); + of_pci_remove_node(dev); pci_dev_assign_added(dev, false); } From patchwork Wed Oct 12 18:14:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lizhi Hou X-Patchwork-Id: 1689268 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-pci-owner@vger.kernel.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=YT8tfhLY; dkim-atps=neutral Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by legolas.ozlabs.org (Postfix) with ESMTP id 4Mngm942jnz23jX for ; Thu, 13 Oct 2022 05:14:49 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229747AbiJLSOr (ORCPT ); Wed, 12 Oct 2022 14:14:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229714AbiJLSOk (ORCPT ); Wed, 12 Oct 2022 14:14:40 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam07on2068.outbound.protection.outlook.com [40.107.212.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C88D91103; Wed, 12 Oct 2022 11:14:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dpdE42NPETtHZJYusIrDUMNbYX+7w66X1aXf/bp15Wc2xOMtfG2C2OxZB2I4jWBsNVvlluaVfkASre6/liFIN3ZuDEseuIn+sgJtko2UD2+85CVa7TZbcp8FYDeFWZJfXOlr8Dew9yH9dDonmoTmMSdyLAr5mvEYtf1Ncl4h4SpDaUCOnTeCHsUNOwiiQlRH/70gcGjutIoO3e+bllY4hoGJGPdp/qJeQG1N9/jOYBAQmJKDoSXpoirnKrjnxnfdSSaDrJmizYRZTdO92qTwrGl7i4ZyXgi6Q5RiS8CFTFUxK2ecST76U98FRI9Yx9w2cwKwCuSdcu4zjVcb6Pfi0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NP45TGotFcPnMTqvsEe8IqQciJFPIzynwhiPiSk/1Is=; b=Fr5VfiToN3LVrvix76APq09iX49VAqjzGZQugBf55sNCunhYr2RVKBHV+VeA8a3TwfOSUa//iOmS+WM21bcEWeSgXNEwKfD1C87X1psugx2V3UUaI4GhPsAovBOhfZxH1+KlqQE6jsHtRtcHTSnVMyiSa/destwi0rxpffunYB/fzqjrxAo66zemdorW5URFkTcYqFrYWwZQcyLV1ZgCRMv6W95Z2+klm6QNO0r14GL0UjFecfL+uEvSkPzX3XBTvvRq1lptIL51uVK4pfwgQqeo+5k5EL8vvAZB31Y8QXJCMz9rLeEz1KHs9KhvKo+3qhRTSQ9utH+60zcfXeFJ2Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=NP45TGotFcPnMTqvsEe8IqQciJFPIzynwhiPiSk/1Is=; b=YT8tfhLYj/4yuD/4JamELJQJJTRJZ0KtWhFeXnf+u70TyhnpU0ep8ememf4h4Lji2zLJxqG7/LncnJaU5bq/sXnQcpLCCmHPKpvJp2IObboIA2F7XO8xm97Jz5mYMyGqYJ1fWWHpgnP6LnfoZjl0h0DX9zjSnNHrDecXyoTCleg= Received: from DS7PR03CA0275.namprd03.prod.outlook.com (2603:10b6:5:3ad::10) by CY5PR12MB6108.namprd12.prod.outlook.com (2603:10b6:930:27::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.36; Wed, 12 Oct 2022 18:14:35 +0000 Received: from DM6NAM11FT106.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3ad:cafe::70) by DS7PR03CA0275.outlook.office365.com (2603:10b6:5:3ad::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5709.21 via Frontend Transport; Wed, 12 Oct 2022 18:14:35 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DM6NAM11FT106.mail.protection.outlook.com (10.13.172.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5723.20 via Frontend Transport; Wed, 12 Oct 2022 18:14:35 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Wed, 12 Oct 2022 13:14:33 -0500 Received: from xsjlizhih40.xilinx.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2375.28 via Frontend Transport; Wed, 12 Oct 2022 13:14:32 -0500 From: Lizhi Hou To: , , , , , CC: Lizhi Hou , , , , , , , Subject: [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node Date: Wed, 12 Oct 2022 11:14:00 -0700 Message-ID: <1665598440-47410-4-git-send-email-lizhi.hou@amd.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com> References: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT106:EE_|CY5PR12MB6108:EE_ X-MS-Office365-Filtering-Correlation-Id: 72e0741f-7fc5-4766-6d47-08daac7d9e6f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1SfcVDTGjGjdQI8wI8SPPDwi6nV7vL6fBDqr9bkUTmb7j3wuTaKg8aXE0BkK9BXzHuPYS6arUdh+uJ3rf0qK4wA7OUW88215IMavxjnPmpKocZ5RP23y32fcBGanbLa5wZlRBU5N/ccBLZaJbaElyYwG5avOxgctUG+7OdhdQhLZtnZu834P3WVD51NrFG9guAblF/nth7hPvtWXtp8Vu+gxgxOxEHpOUnySsJjRLR9c250d6XOivO78oqo2QutbAZFQcnyeVbAzZhMtpw8GTTzwuShsWkEIAoAEqWW8U/h7i39hkntVQQh/jj259KVV6jzyJ6xVO2aD7cALjgMkOmYnK0lf4H73ZPM5Bc/I/BAIOvjKUjV8Vu4ly3F+D4uc/SrX9y1Qh6IQCuWgqW0icwRI6M2d1YCuxt4gc2wHqeVV580Etzg4ba13/EAkq1F7KZF5tpsFQ9KX41s1L7As9JDXrPjGwG98f2iYNKH2rdgHujawNa0dkfTunkOOOHiYYhQBRvL7NCEio6IX0x2BEPwsdQ8eyTZGSfvHHgyAB9uv0s7GDxNuPctzCfsdDf7+qfZxgSTKxGEpWe3ZSV/jGsMakFNdpabh5zeO60OdE33En5DSVVyyrE+VzYoLwSl8Ypeaepm3AViK0z+/ezEjfe3kAG/ztTqX27r1QsclmM5cp/Wk0AVeYwmv3Q8X1XtC5BzuGJNituJyjrubUDKS7w9pLg3HUM/sMUmYT9xIBnrpqycvY+jmTZQcePTWiJSLdpqVceN8E3rcW+hXDtnScfFSUNBm/bbANki8CICmLRY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(396003)(346002)(39860400002)(376002)(451199015)(40470700004)(46966006)(36840700001)(81166007)(36756003)(86362001)(186003)(44832011)(83380400001)(2616005)(70206006)(8676002)(82740400003)(47076005)(36860700001)(426003)(41300700001)(2906002)(82310400005)(316002)(4326008)(6666004)(26005)(54906003)(70586007)(40460700003)(356005)(478600001)(5660300002)(336012)(40480700001)(8936002)(110136005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Oct 2022 18:14:35.4557 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 72e0741f-7fc5-4766-6d47-08daac7d9e6f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT106.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6108 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,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-pci@vger.kernel.org This patch addes 'reg', 'compatible' and 'device_typ' properties for dynamically generated PCI device tree node Signed-off-by: Lizhi Hou Signed-off-by: Sonal Santan Signed-off-by: Max Zhen Signed-off-by: Brian Xu --- drivers/pci/Makefile | 1 + drivers/pci/of.c | 10 ++- drivers/pci/of_property.c | 177 ++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 3 +- 4 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 drivers/pci/of_property.c diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile index 2680e4c92f0a..cc8b4e01e29d 100644 --- a/drivers/pci/Makefile +++ b/drivers/pci/Makefile @@ -32,6 +32,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o obj-$(CONFIG_VGA_ARB) += vgaarb.o obj-$(CONFIG_PCI_DOE) += doe.o +obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o # Endpoint library must be initialized before its users obj-$(CONFIG_PCI_ENDPOINT) += endpoint/ diff --git a/drivers/pci/of.c b/drivers/pci/of.c index 83e042f495a6..00d716589660 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -619,6 +619,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev) { struct device_node *parent, *dt_node; const char *pci_type = "dev"; + struct property *props; char *full_name; /* @@ -645,10 +646,15 @@ void of_pci_make_dev_node(struct pci_dev *pdev) if (!full_name) goto failed; - dt_node = of_create_node(parent, full_name, NULL); + props = of_pci_props_create(pdev); + if (!props) + goto failed; + + dt_node = of_create_node(parent, full_name, props); if (!dt_node) goto failed; + of_pci_props_destroy(props); kfree(full_name); pdev->dev.of_node = dt_node; @@ -656,6 +662,8 @@ void of_pci_make_dev_node(struct pci_dev *pdev) return; failed: + if (props) + of_pci_props_destroy(props); kfree(full_name); } #endif diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c new file mode 100644 index 000000000000..693a08323aa4 --- /dev/null +++ b/drivers/pci/of_property.c @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2022, Advanced Micro Devices, Inc. + */ + +#include +#include +#include +#include + +struct of_pci_prop { + char *name; + int (*prop_val)(struct pci_dev *pdev, void **val, u32 *len); +}; + +struct of_pci_addr_pair { + __be32 phys_hi; + __be32 phys_mid; + __be32 phys_lo; + __be32 size_hi; + __be32 size_lo; +}; + +#define OF_PCI_ADDR_SPACE_CONFIG 0x0 +#define OF_PCI_ADDR_SPACE_IO 0x1 +#define OF_PCI_ADDR_SPACE_MEM32 0x2 +#define OF_PCI_ADDR_SPACE_MEM64 0x3 + +#define OF_PCI_ADDR_FIELD_SS GENMASK(25, 24) +#define OF_PCI_ADDR_FIELD_PREFETCH BIT(30) +#define OF_PCI_ADDR_FIELD_BUS GENMASK(23, 16) +#define OF_PCI_ADDR_FIELD_DEV GENMASK(15, 11) +#define OF_PCI_ADDR_FIELD_FUNC GENMASK(10, 8) +#define OF_PCI_ADDR_FIELD_REG GENMASK(7, 0) + +#define OF_PCI_SIZE_HI GENMASK_ULL(63, 32) +#define OF_PCI_SIZE_LO GENMASK_ULL(31, 0) + +#define OF_PCI_PROP_COMPAT_LEN_MAX 256 +static int of_pci_prop_device_type(struct pci_dev *pdev, void **val, u32 *len) +{ + if (!pci_is_bridge(pdev)) + return 0; + + *val = kasprintf(GFP_KERNEL, "pci"); + if (!*val) + return -ENOMEM; + + *len = strlen(*val) + 1; + + return 0; +} + +static int of_pci_prop_reg(struct pci_dev *pdev, void **val, u32 *len) +{ + struct of_pci_addr_pair *reg; + u32 reg_val, base_addr, ss; + resource_size_t sz; + int i = 1, resno; + + reg = kzalloc(sizeof(*reg) * (PCI_STD_NUM_BARS + 1), GFP_KERNEL); + if (!reg) + return -ENOMEM; + + reg_val = FIELD_PREP(OF_PCI_ADDR_FIELD_SS, OF_PCI_ADDR_SPACE_CONFIG) | + FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) | + FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) | + FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn)); + reg[0].phys_hi = cpu_to_be32(reg_val); + + base_addr = PCI_BASE_ADDRESS_0; + for (resno = PCI_STD_RESOURCES; resno <= PCI_STD_RESOURCE_END; + resno++, base_addr += 4) { + sz = pci_resource_len(pdev, resno); + if (!sz) + continue; + + if (pci_resource_flags(pdev, resno) & IORESOURCE_IO) + ss = OF_PCI_ADDR_SPACE_IO; + else if (pci_resource_flags(pdev, resno) & IORESOURCE_MEM_64) + ss = OF_PCI_ADDR_SPACE_MEM64; + else + ss = OF_PCI_ADDR_SPACE_MEM32; + + reg_val &= ~(OF_PCI_ADDR_FIELD_SS | OF_PCI_ADDR_FIELD_PREFETCH | + OF_PCI_ADDR_FIELD_REG); + reg_val |= FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss) | + FIELD_PREP(OF_PCI_ADDR_FIELD_REG, base_addr); + if (pci_resource_flags(pdev, resno) & IORESOURCE_PREFETCH) + reg_val |= OF_PCI_ADDR_FIELD_PREFETCH; + reg[i].phys_hi = cpu_to_be32(reg_val); + reg[i].size_hi = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_HI, sz)); + reg[i].size_lo = cpu_to_be32(FIELD_GET(OF_PCI_SIZE_LO, sz)); + i++; + } + + *val = reg; + *len = i * sizeof(*reg); + + return 0; +} + +static int of_pci_prop_compatible(struct pci_dev *pdev, void **val, u32 *len) +{ + char *compat; + + compat = kzalloc(OF_PCI_PROP_COMPAT_LEN_MAX, GFP_KERNEL); + if (!compat) + return -ENOMEM; + + *val = compat; + if (pdev->subsystem_vendor) { + compat += sprintf(compat, "pci%x,%x.%x.%x.%x", + pdev->vendor, pdev->device, + pdev->subsystem_vendor, + pdev->subsystem_device, + pdev->revision) + 1; + compat += sprintf(compat, "pci%x,%x.%x.%x", + pdev->vendor, pdev->device, + pdev->subsystem_vendor, + pdev->subsystem_device) + 1; + compat += sprintf(compat, "pci%x,%x", + pdev->subsystem_vendor, + pdev->subsystem_device) + 1; + } + compat += sprintf(compat, "pci%x,%x.%x", + pdev->vendor, pdev->device, pdev->revision) + 1; + compat += sprintf(compat, "pci%x,%x", pdev->vendor, pdev->device) + 1; + compat += sprintf(compat, "pciclass,%06x", pdev->class) + 1; + compat += sprintf(compat, "pciclass,%04x", pdev->class >> 8) + 1; + + *len = (u32)(compat - (char *)*val); + + return 0; +} + +struct of_pci_prop of_pci_props[] = { + { .name = "device_type", .prop_val = of_pci_prop_device_type }, + { .name = "reg", .prop_val = of_pci_prop_reg }, + { .name = "compatible", .prop_val = of_pci_prop_compatible }, + {}, +}; + +struct property *of_pci_props_create(struct pci_dev *pdev) +{ + struct property *props, *pp; + void *val; + u32 len; + int i; + + props = kcalloc(ARRAY_SIZE(of_pci_props), sizeof(*props), GFP_KERNEL); + if (!props) + return NULL; + + pp = props; + for (i = 0; of_pci_props[i].name; i++) { + len = 0; + of_pci_props[i].prop_val(pdev, &val, &len); + if (!len) + continue; + props->name = of_pci_props[i].name; + props->value = val; + props->length = len; + props++; + } + + return pp; +} + +void of_pci_props_destroy(struct property *props) +{ + int i; + + for (i = 0; props[i].name; i++) + kfree(props[i].value); + kfree(props); +} diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index e0a11497b1ad..37841241ceea 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -681,7 +681,8 @@ static inline int devm_of_pci_bridge_init(struct device *dev, struct pci_host_br #ifdef CONFIG_PCI_DYNAMIC_OF_NODES void of_pci_make_dev_node(struct pci_dev *pdev); void of_pci_remove_node(struct pci_dev *pdev); - +struct property *of_pci_props_create(struct pci_dev *pdev); +void of_pci_props_destroy(struct property *props); #else static inline void of_pci_make_dev_node(struct pci_dev *pdev)