From patchwork Thu Mar 2 01:38:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63158 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3979614wrd; Wed, 1 Mar 2023 17:39:42 -0800 (PST) X-Google-Smtp-Source: AK7set/hLFK5ZBs1ot/zolEET6GL245ztO0c9xRndDj05Eey5G/mqZm5zyBcqLP/xxx1oKPAfgDz X-Received: by 2002:aa7:df0b:0:b0:4bd:11d3:e86f with SMTP id c11-20020aa7df0b000000b004bd11d3e86fmr3473522edy.6.1677721182331; Wed, 01 Mar 2023 17:39:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721182; cv=none; d=google.com; s=arc-20160816; b=vZqoW+RIZEb1IAUxlvNIhWLaMLpm6OAM7SXgGSuYMES2dN8q9Zup3qb2dG8ylgh8kK Wr1BbvfB3A73O2qGziPb5H3zAp7xWj7VhY2wWEPmYnPvK4S+MqsxTIB0IUpG6DySwgva EQV1wAb57336r67MwDc9uXz6/1yYEsCUuXO9IiHzlLxtbYP6VUcv7N4u9SIry3ngStAY hhIxTmESoSbs/8Ss3l3rHUKtVuQjiWCFiEGSvZpKmDg+wp4DaVpS7uO985nrORs2pCoj F2u9gMNSq65QfmbFBDhBcO8GuolsF+qMbLq+zErCO7KvQEAFG+eRPLM+OZU+6xS3ONOg 50JQ== 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=dkfreM0CCqZHeYZIGhMEOFv6eqgphqEHOeVnc5bktqY=; b=LMAi5uaryU+kYkFR2/z0igdEQkW+xWuz4D/GZM2cQp70QuQUca1aA/2/Ry5nyeY4oa kxSSd1xi49aK7LzKWbAaEQ5jY6J3eIvV69v0WtYashNDIE/HsI1wWMyROjg+dq+wlsOo pt21QevxdcVQkvXPrChFbXsxQDVQKa1wfcoc6e6tPrklpNaPN0Mp8rsUJ9PhYxQ8A2E3 Be+pniJ2lzjZNTM43uWLIrZ2zyPGu3S65pxtFSNkSO9glALsNcZui05KL1KcF49f7qtg zu2q4z+TsVZ6AtWjM2ptl3d9W7/zpCv1jB9mH99SFnsUx2MddvV9LcsykbwHbhCc0MvH lWmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QX5VMEDW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mc17-20020a170906eb5100b008b17a1f0979si16461271ejb.319.2023.03.01.17.39.17; Wed, 01 Mar 2023 17:39:42 -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=@kernel.org header.s=k20201202 header.b=QX5VMEDW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229714AbjCBBid (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229699AbjCBBia (ORCPT ); Wed, 1 Mar 2023 20:38:30 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAF8929E14; Wed, 1 Mar 2023 17:38:25 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1E12C61542; Thu, 2 Mar 2023 01:38:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE626C43442; Thu, 2 Mar 2023 01:38:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721104; bh=xlmggg4SwPHgsUHI5EKsdomiaUIjY1b7gCCFhpuQ1f0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QX5VMEDWKzXOvBrh3s5n8VNsk9WCy+BA9LU0FftVmXQFxnx9N4tmGdqqJbt4HBNC/ /Qlpzm4VHNwG1upxQs/3iAv3kEvXMUcNPICy2unmITpgwI27wshbBkaYelU99Og3/X p5YKr67RY+XlVu88alwK46oCYJX2sz1t55xB+CborNapRC+fVCStkxZLrs4vbUVQp8 VkGBUpBi7nyiEcrDtkNgaz+3/+D2JOiC74nqF8N0t5IIQy6bc7yL9zxQMfRoYpZZcU f28hGP1XZakglVR91gvvQt0gH03SKVUs98uG1rm/CYB0ImCiuK9S8Hxa9KyjMIIN1F k/5ExXHAciHDA== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 1/8] dt-bindings: Add linux,kunit binding Date: Wed, 1 Mar 2023 17:38:14 -0800 Message-Id: <20230302013822.1808711-2-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218166636995320?= X-GMAIL-MSGID: =?utf-8?q?1759218166636995320?= Document the linux,kunit board compatible string. This board is loaded into the Linux kernel when KUnit is testing devicetree dependent code. Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- .../bindings/kunit/linux,kunit.yaml | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Documentation/devicetree/bindings/kunit/linux,kunit.yaml diff --git a/Documentation/devicetree/bindings/kunit/linux,kunit.yaml b/Documentation/devicetree/bindings/kunit/linux,kunit.yaml new file mode 100644 index 000000000000..dfe6da4796e8 --- /dev/null +++ b/Documentation/devicetree/bindings/kunit/linux,kunit.yaml @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/kunit/linux,kunit.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: KUnit + +maintainers: + - Brendan Higgins + - David Gow + +description: + KUnit board used to unit test the Linux kernel in User Mode Linux (UML). + +properties: + $nodename: + const: "/" + compatible: + const: linux,kunit + +additionalProperties: true + +... From patchwork Thu Mar 2 01:38:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63162 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3980276wrd; Wed, 1 Mar 2023 17:41:27 -0800 (PST) X-Google-Smtp-Source: AK7set/IiaLoEvfKfuba8mhPDUacAG8AC2zyd4kj9gUNsGIIn8PZ+Qy3pTRjSaVr3XLD8gvmi6pu X-Received: by 2002:a17:906:49ca:b0:87b:dac1:bbe6 with SMTP id w10-20020a17090649ca00b0087bdac1bbe6mr8409531ejv.36.1677721287344; Wed, 01 Mar 2023 17:41:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721287; cv=none; d=google.com; s=arc-20160816; b=jj2X1zskVDR2asc6ygEDNwBuegTIE5J/I5J35L6lOg9I8msvJsCdXGYuPFW/jiHIkp X+8u2fhMczClCIR6VZ1ILiDirLjPkN074v7ctEWPRa0k45B/jguMcDUnHGRE9CY9sDTT m+aA6l9fLO/Ho15HtSgqiUBllMG+hqk4E3UgcCS+xNvT6IILswb0lymGX6oDN1dLZxnb j15DT7daWYCU3tO15uZsB+IjANWlgaOLoMO7oKmh4lTJcA5wjQqgf1HKHNEAdtK/MM/n YpySAt0CQs+apG5KotGzKipJOmuoCXYA6EjKs4Sr+00kiuk3cOVMQaNQ6XziLBd1W1qn M5RA== 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=1ipjaVbDDigjeR23nLUf51iny5ZPp68yJ5WbVUVhyYM=; b=SndJGw40OKVWGLmdmGnQWnAMwIiYCAdGMiXh+LG6k7P9uEnBDS6ckdtH1NUVpXTj6a rDhXzP9Y6OhXULyOFkttBXoZcJDI6GOpmJHdEVE9t1ELE1O2NODRJXN/2HALUtMIPzok OzOkOz7V0Mlo5QsVh23SxL2oNwfqlqzTAz/M+8bld6EsuCA5lPL2+6r1rxkH+sqFFD4q TSkbybbm5blzlAuZrUQtqvKvlWYj+ECHZOvi66rMu+8iBvoY3Vq9Gr1kDGMqG37QOG6K n95NvfVUq+bVnuFD5lwVqrm2+wGAdFfeaHIDz/bWQXRz2JDlcK0PkR1iNsC5nILSjqL1 RgQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hvFYRxNc; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u15-20020aa7d0cf000000b004aef3d356eesi16316146edo.621.2023.03.01.17.41.04; Wed, 01 Mar 2023 17:41:27 -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=@kernel.org header.s=k20201202 header.b=hvFYRxNc; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229723AbjCBBie (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229703AbjCBBia (ORCPT ); Wed, 1 Mar 2023 20:38:30 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BF9B2313F; Wed, 1 Mar 2023 17:38:26 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EE89A61544; Thu, 2 Mar 2023 01:38:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2CCFC4339E; Thu, 2 Mar 2023 01:38:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721105; bh=kuvw9W5FZhpfWvxyQ40xAiL1cU8qQButV6bl0j/U8CY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hvFYRxNcF/h2bnEXB525WG0BYTbbsmT9V2JIlTtAcKJCeL3H6/rVztATelYl8ioT3 0ofMBzrOcsfG3jfXxGqc7LCrD2YNMMqIo5SpU+i/A79tZVpSEFRDLLKMZQ5jfrZUev dprzZA6j8c+VRmLyzIqz+Aj5eJ6Hf5T0TMd66jeOhvQd1SCzBrHTNYpVxjHrHdwFI/ Ckz1aUqKMUxLyWqWvE9UqkclchocVqKZEl54ZtQ07ngMVTH2WbRcyQvM1XmvXVDW5S rY/yDUvP8okVyFHcE6u2F4m1kE7bY6HMkrOETWHIC0QqFzla4Ox7fUfV6VJvu82m6r Pz3tBMtJCmjGg== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 2/8] of: Enable DTB loading on UML for KUnit tests Date: Wed, 1 Mar 2023 17:38:15 -0800 Message-Id: <20230302013822.1808711-3-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218276736534313?= X-GMAIL-MSGID: =?utf-8?q?1759218276736534313?= To fully exercise common clk framework code in KUnit we need to associate 'struct device' pointers with 'struct device_node' pointers so that things like clk_get() can parse DT nodes for 'clocks' and so that clk providers can use DT to provide clks; the most common mode of operation for clk providers. Adding support to KUnit so that it loads a DTB is fairly simple after commit b31297f04e86 ("um: Add devicetree support"). We can simply pass a pre-compiled deviectree blob (DTB) on the kunit.py commandline and UML will load it. The problem is that tests won't know that the commandline has been modified, nor that a DTB has been loaded. Take a different approach so that tests can skip if a DTB hasn't been loaded. Reuse the Makefile logic from the OF unittests to build a DTB into the kernel. This DTB will be for the mythical machine "linux,kunit", i.e. the devicetree for the KUnit "board". In practice, it is a dtsi file that will gather includes for kunit tests that rely in part on a devicetree being loaded. The devicetree should only be loaded if CONFIG_OF_KUNIT=y. Make that a choice config parallel to the existing CONFIG_OF_UNITTEST so that only one devicetree can be loaded in the system at a time. Similarly, the kernel commandline option to load a DTB is ignored if CONFIG_OF_KUNIT is enabled so that only one DTB is loaded at a time. Add a simple unit test to confirm that the DTB loading worked. Future tests will add to the kunit.dtsi file to include their specific test nodes. Cc: Richard Weinberger Cc: Anton Ivanov Cc: Johannes Berg Cc: Vincent Whitchurch Cc: Rob Herring Cc: Frank Rowand Signed-off-by: Stephen Boyd --- arch/um/kernel/dtb.c | 29 +++++++++++++++-- drivers/of/Kconfig | 26 ++++++++++++++++ drivers/of/Makefile | 1 + drivers/of/kunit/.kunitconfig | 4 +++ drivers/of/kunit/Makefile | 4 +++ drivers/of/kunit/kunit.dtsi | 8 +++++ drivers/of/kunit/kunit.dtso | 4 +++ drivers/of/kunit/uml_dtb_test.c | 55 +++++++++++++++++++++++++++++++++ 8 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 drivers/of/kunit/.kunitconfig create mode 100644 drivers/of/kunit/Makefile create mode 100644 drivers/of/kunit/kunit.dtsi create mode 100644 drivers/of/kunit/kunit.dtso create mode 100644 drivers/of/kunit/uml_dtb_test.c diff --git a/arch/um/kernel/dtb.c b/arch/um/kernel/dtb.c index 484141b06938..ee63951b12df 100644 --- a/arch/um/kernel/dtb.c +++ b/arch/um/kernel/dtb.c @@ -15,9 +15,32 @@ void uml_dtb_init(void) long long size; void *area; - area = uml_load_file(dtb, &size); - if (!area) - return; + if (IS_ENABLED(CONFIG_OF_KUNIT)) { + /* + * __dtbo_kunit_begin[] and __dtbo_kunit_end[] are magically + * created by cmd_dt_S_dtbo in scripts/Makefile.lib from the + * drivers/of/kunit/kunit.dtsi file. + */ + extern uint8_t __dtbo_kunit_begin[]; + extern uint8_t __dtbo_kunit_end[]; + + size = __dtbo_kunit_end - __dtbo_kunit_begin; + if (!size) { + pr_warn("%s: kunit testcases is empty\n", __func__); + return; + } + + /* creating copy */ + area = memblock_alloc(size, 8); + if (!area) + return; + + memcpy(area, __dtbo_kunit_begin, size); + } else { + area = uml_load_file(dtb, &size); + if (!area) + return; + } if (!early_init_dt_scan(area)) { pr_err("invalid DTB %s\n", dtb); diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index 80b5fd44ab1c..1f968b6a3dde 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig @@ -12,6 +12,20 @@ menuconfig OF if OF +choice + prompt "Devicetree Runtime Tests" + default OF_UNITTEST + +config OF_KUNIT + bool "Devicetree KUnit support" if KUNIT + depends on UML + select IRQ_DOMAIN + select OF_EARLY_FLATTREE + help + This option builds in KUnit test cases that rely on device tree infrastructure. + A fake Device Tree Blob (DTB) is loaded on the UML kernel running KUnit so that + KUnit tests can test device tree dependent code. + config OF_UNITTEST bool "Device Tree runtime unit tests" depends on !SPARC @@ -25,6 +39,18 @@ config OF_UNITTEST If unsure, say N here, but this option is safe to enable. +endchoice + +config OF_DTB_KUNIT_TEST + tristate "Devicetree KUnit DTB Test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option builds unit tests for the "linux,kunit" DTB built into + the UML kernel image. + + If unsure, say N here, but this option is safe to enable. + config OF_ALL_DTBS bool "Build all Device Tree Blobs" depends on COMPILE_TEST diff --git a/drivers/of/Makefile b/drivers/of/Makefile index e0360a44306e..16eef3fdf60a 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile @@ -19,4 +19,5 @@ obj-y += kexec.o endif endif +obj-y += kunit/ obj-$(CONFIG_OF_UNITTEST) += unittest-data/ diff --git a/drivers/of/kunit/.kunitconfig b/drivers/of/kunit/.kunitconfig new file mode 100644 index 000000000000..1def0ad30d29 --- /dev/null +++ b/drivers/of/kunit/.kunitconfig @@ -0,0 +1,4 @@ +CONFIG_KUNIT=y +CONFIG_OF=y +CONFIG_OF_KUNIT=y +CONFIG_OF_DTB_KUNIT_TEST=y diff --git a/drivers/of/kunit/Makefile b/drivers/of/kunit/Makefile new file mode 100644 index 000000000000..ffe0447e1ac7 --- /dev/null +++ b/drivers/of/kunit/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_OF_KUNIT) += kunit.dtbo.o + +obj-$(CONFIG_OF_DTB_KUNIT_TEST) += uml_dtb_test.o diff --git a/drivers/of/kunit/kunit.dtsi b/drivers/of/kunit/kunit.dtsi new file mode 100644 index 000000000000..82f6c3e2b8d5 --- /dev/null +++ b/drivers/of/kunit/kunit.dtsi @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +/ { + model = "KUnit UML"; + compatible = "linux,kunit"; +}; + +/* Include testcase dtsi files below */ diff --git a/drivers/of/kunit/kunit.dtso b/drivers/of/kunit/kunit.dtso new file mode 100644 index 000000000000..50187e8d1422 --- /dev/null +++ b/drivers/of/kunit/kunit.dtso @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; + +#include "kunit.dtsi" diff --git a/drivers/of/kunit/uml_dtb_test.c b/drivers/of/kunit/uml_dtb_test.c new file mode 100644 index 000000000000..8966c9ebf51f --- /dev/null +++ b/drivers/of/kunit/uml_dtb_test.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for DTB loading on UML + */ +#include +#include +#include + +#include + +/* + * Test that of_machine_is_compatible() returns positive int when loaded DTB + * matches. + */ +static void uml_dtb_of_machine_compatible_test(struct kunit *test) +{ + KUNIT_EXPECT_GT(test, of_machine_is_compatible("linux,kunit"), 0); +} + +/* + * Test that of_flat_dt_get_machine_name() returns the expected 'model' from the + * loaded DTB. + */ +static void uml_dtb_of_flat_dt_get_machine_name_test(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, of_flat_dt_get_machine_name(), "KUnit UML"); +} + +static struct kunit_case uml_dtb_test_cases[] = { + KUNIT_CASE(uml_dtb_of_machine_compatible_test), + KUNIT_CASE(uml_dtb_of_flat_dt_get_machine_name_test), + {} +}; + +static int uml_dtb_test_init(struct kunit *test) +{ + if (!IS_ENABLED(CONFIG_OF_KUNIT)) + kunit_skip(test, "requires CONFIG_OF_KUNIT"); + + return 0; +} + +/* + * Test suite to confirm DTB is loaded on UML. + */ +static struct kunit_suite uml_dtb_suite = { + .name = "uml_dtb", + .init = uml_dtb_test_init, + .test_cases = uml_dtb_test_cases, +}; + +kunit_test_suites( + ¨_dtb_suite, +); +MODULE_LICENSE("GPL"); From patchwork Thu Mar 2 01:38:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63160 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3979859wrd; Wed, 1 Mar 2023 17:40:23 -0800 (PST) X-Google-Smtp-Source: AK7set9BM3Y8eCm2ZMvSkGgKcCdqzhlkBmXjePNfnoiSi41ZkOqaIL9LDv8tFRIPpCIxoVCN8uFc X-Received: by 2002:a17:906:2c11:b0:8b1:ce91:a40c with SMTP id e17-20020a1709062c1100b008b1ce91a40cmr7904270ejh.53.1677721223737; Wed, 01 Mar 2023 17:40:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721223; cv=none; d=google.com; s=arc-20160816; b=YDWOgY7bhQN7cSwxaSrnWzWjM5ZOFoaSaCO6Yutlif5+eV8GbVITOYk3EbOdUbSxy0 HK5Po6DL7SXeLJ/VhCV54dcuHHegBNnsDWB9pdiKjxtu5DQjx1kqj/7AVzqj4l1L7WDb cN1cQRDFm1pVXA3oTwI6GqbRXVYZwmfx5HslQSye19t+8TZCwe2aGzJOd3id2CmthMl1 EziSE1ZOFQIVtZcwoySta0PN0BJmzyVSJRJMzkS6ReQqJGrrcSJX5P8kKtWm5ygCJ4WY u8wVBeX7Woh2GY4YWFZ1f8C0LCs94vNa1gwzoz9W6xsiBJOPDrEDGB3SX4aXo4O576WE qJAg== 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=AlSi4X8AaP1DvqCzP+yDv+pG0Xvc2Q/76r6vcYLERSc=; b=TGTpwNA6v7Q9SZPK/mZ61dw0CqW5CZlTUUU/PYA8U+mS17gbch9K0iwIEGdhwp8vYB qVO8LbQYkDhlqgJiJak7oSJ4H4SnEEl/MyjzQUzDlwBqqYULPB7+92UHbR9jFM2Q/pyt tjVvFhhWJgP31YYzN1w+WNg6mA/UsTul2LNmDjCUhjh9iuP5U9/ehDsk8T80RjRMyG0O uBe9sQt5MxbHxaghPc8NtuucTak9PN/1Lollhj/o+A6PU+ruiRnaHi4f8ug5M/DWmmdC 2GGrFuOWLf9PTsVQa9LopIHyCKjyIh271wGopJLaDsOmNNLJbXaixjSFwE2fVOBMDze8 oAlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=U1tDZTB8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y20-20020a170906915400b008d0378ec19dsi15341218ejw.487.2023.03.01.17.40.00; Wed, 01 Mar 2023 17:40:23 -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=@kernel.org header.s=k20201202 header.b=U1tDZTB8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229790AbjCBBit (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229701AbjCBBin (ORCPT ); Wed, 1 Mar 2023 20:38:43 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49C74366AD; Wed, 1 Mar 2023 17:38:29 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 91BC2B811E9; Thu, 2 Mar 2023 01:38:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87A2AC433A4; Thu, 2 Mar 2023 01:38:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721106; bh=3k+YvMdWMsnOAG6Xbljbz1eZOpgJZtilmS9lPO8bApY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U1tDZTB8oHaK4uGxcnbievgIEFFJPTmIrMhXBoFMFjvGUjrIl2802ciDrN52ep62A AVE9HbAZGEFxf0hNkdfxmi+B/8Z34RfMeP2bCn8SNnGQPyMGnJTvsCKqXC3SffE0kf mIGMLE+9tsSXw0OjOu5K2+NQwZw4tTL7y1+VRyFbrlzIfBUWAt4i7i63Thxc3L4imN jMkVsQvjRn/tsxOs/e4dmcYHMzbxRay9MCSSp9oj+kPuudtdFQJzTVPOpP2Pf9S9HL lTXsCpRx4boL3rL4bAp59OIe9HFqUAPV1kiB1nJb9NLeVE6wklKducBO79kdyLbtGk aXitEVYhkrAuw== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 3/8] kunit: Add test managed platform_device/driver APIs Date: Wed, 1 Mar 2023 17:38:16 -0800 Message-Id: <20230302013822.1808711-4-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218209886035056?= X-GMAIL-MSGID: =?utf-8?q?1759218209886035056?= Introduce KUnit resource wrappers around platform_driver_register(), platform_device_alloc(), and platform_device_add() so that test authors can register platform drivers/devices from their tests and have the drivers/devices automatically be unregistered when the test is done. This makes test setup code simpler when a platform driver or platform device is needed. Add a few test cases at the same time to make sure the APIs work as intended. Cc: Brendan Higgins Cc: David Gow Cc: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" Signed-off-by: Stephen Boyd --- Should this be moved to drivers/base/ and called platform_kunit.c? The include/kunit/platform_driver.h could also be kunit/platform_device.h to match linux/platform_device.h if that is more familiar. And I'm not super certain about allocating a driver structure and embedding it in a wrapper struct. Maybe the code should just use kunit_get_current_test() instead? include/kunit/platform_driver.h | 15 +++ lib/kunit/Makefile | 6 + lib/kunit/platform_driver-test.c | 107 ++++++++++++++++ lib/kunit/platform_driver.c | 207 +++++++++++++++++++++++++++++++ 4 files changed, 335 insertions(+) create mode 100644 include/kunit/platform_driver.h create mode 100644 lib/kunit/platform_driver-test.c create mode 100644 lib/kunit/platform_driver.c diff --git a/include/kunit/platform_driver.h b/include/kunit/platform_driver.h new file mode 100644 index 000000000000..dc211ff8f893 --- /dev/null +++ b/include/kunit/platform_driver.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _KUNIT_PLATFORM_DRIVER_H +#define _KUNIT_PLATFORM_DRIVER_H + +struct kunit; +struct platform_device; +struct platform_driver; + +struct platform_device * +kunit_platform_device_alloc(struct kunit *test, const char *name, int id); +int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev); + +int kunit_platform_driver_register(struct kunit *test, struct platform_driver *drv); + +#endif diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 29aff6562b42..5964d8231ff5 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -1,5 +1,6 @@ obj-$(CONFIG_KUNIT) += kunit.o +# Core KUnit code kunit-objs += test.o \ resource.o \ string-stream.o \ @@ -11,7 +12,12 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif +# KUnit helpers +kunit-objs += platform_driver.o + +# KUnit tests obj-$(CONFIG_KUNIT_TEST) += kunit-test.o +obj-$(CONFIG_KUNIT_TEST) += platform_driver-test.o # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) diff --git a/lib/kunit/platform_driver-test.c b/lib/kunit/platform_driver-test.c new file mode 100644 index 000000000000..c926fe01b40a --- /dev/null +++ b/lib/kunit/platform_driver-test.c @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for platform driver infrastructure. + */ + +#include + +#include +#include + +/* + * Test that kunit_platform_device_alloc() creates a platform device. + */ +static void kunit_platform_device_alloc_test(struct kunit *test) +{ + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, + kunit_platform_device_alloc(test, "kunit-platform", 1)); +} + +/* + * Test that kunit_platform_device_add() registers a platform device on the + * platform bus with the proper name and id. + */ +static void kunit_platform_device_add_test(struct kunit *test) +{ + struct platform_device *pdev; + const char *name = "kunit-platform"; + const int id = -1; + + pdev = kunit_platform_device_alloc(test, name, id); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + + KUNIT_EXPECT_EQ(test, 0, kunit_platform_device_add(test, pdev)); + KUNIT_EXPECT_TRUE(test, dev_is_platform(&pdev->dev)); + KUNIT_EXPECT_STREQ(test, pdev->name, name); + KUNIT_EXPECT_EQ(test, pdev->id, id); +} + +static struct kunit_case kunit_platform_device_test_cases[] = { + KUNIT_CASE(kunit_platform_device_alloc_test), + KUNIT_CASE(kunit_platform_device_add_test), + {} +}; + +static struct kunit_suite kunit_platform_device_suite = { + .name = "kunit_platform_device", + .test_cases = kunit_platform_device_test_cases, +}; + +struct kunit_platform_driver_test_context { + struct platform_driver pdrv; + const char *data; +}; + +static inline struct kunit_platform_driver_test_context * +to_test_context(struct platform_device *pdev) +{ + return container_of(to_platform_driver(pdev->dev.driver), + struct kunit_platform_driver_test_context, + pdrv); +} + +static int kunit_platform_driver_probe(struct platform_device *pdev) +{ + struct kunit_platform_driver_test_context *ctx; + + ctx = to_test_context(pdev); + ctx->data = "test data"; + + return 0; +} + +/* Test that kunit_platform_driver_register() registers a driver that probes. */ +static void kunit_platform_driver_register_test(struct kunit *test) +{ + struct platform_device *pdev; + struct kunit_platform_driver_test_context *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + pdev = kunit_platform_device_alloc(test, "kunit-platform", -1); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev)); + + ctx->pdrv.probe = kunit_platform_driver_probe; + ctx->pdrv.driver.name = "kunit-platform"; + ctx->pdrv.driver.owner = THIS_MODULE; + + KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv)); + KUNIT_EXPECT_STREQ(test, ctx->data, "test data"); +} + +static struct kunit_case kunit_platform_driver_test_cases[] = { + KUNIT_CASE(kunit_platform_driver_register_test), + {} +}; + +static struct kunit_suite kunit_platform_driver_suite = { + .name = "kunit_platform_driver", + .test_cases = kunit_platform_driver_test_cases, +}; + +kunit_test_suites(&kunit_platform_device_suite, + &kunit_platform_driver_suite); + +MODULE_LICENSE("GPL"); diff --git a/lib/kunit/platform_driver.c b/lib/kunit/platform_driver.c new file mode 100644 index 000000000000..11d155114936 --- /dev/null +++ b/lib/kunit/platform_driver.c @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test managed platform driver + */ + +#include +#include + +#include + +struct kunit_platform_device_alloc_params { + const char *name; + int id; +}; + +static int kunit_platform_device_alloc_init(struct kunit_resource *res, void *context) +{ + struct kunit_platform_device_alloc_params *params = context; + struct platform_device *pdev; + + pdev = platform_device_alloc(params->name, params->id); + if (!pdev) + return -ENOMEM; + + res->data = pdev; + + return 0; +} + +static void kunit_platform_device_alloc_exit(struct kunit_resource *res) +{ + struct platform_device *pdev = res->data; + + platform_device_put(pdev); +} + +/** + * kunit_platform_device_alloc() - Allocate a KUnit test managed platform device + * @test: test context + * @dev: platform device to alloc + * + * Register a test managed platform device. The device is put when the test completes. + * + * Returns: 0 on success, negative errno on failure. + */ +struct platform_device * +kunit_platform_device_alloc(struct kunit *test, const char *name, int id) +{ + struct platform_device *pdev; + struct kunit_platform_device_alloc_params params = { + .name = name, + .id = id, + }; + + pdev = kunit_alloc_resource(test, + kunit_platform_device_alloc_init, + kunit_platform_device_alloc_exit, + GFP_KERNEL, ¶ms); + if (!pdev) + return ERR_PTR(-ENOMEM); + + return pdev; +} +EXPORT_SYMBOL_GPL(kunit_platform_device_alloc); + +static int kunit_platform_device_add_init(struct kunit_resource *res, void *context) +{ + struct platform_device *pdev = context; + int ret; + + ret = platform_device_add(pdev); + if (ret) { + platform_device_put(pdev); + return ret; + } + res->data = pdev; + + return 0; +} + +static void kunit_platform_device_add_exit(struct kunit_resource *res) +{ + struct platform_device *pdev = res->data; + + platform_device_unregister(pdev); +} + +/** + * kunit_platform_device_add() - Register a KUnit test managed platform device + * @test: test context + * @dev: platform device to add + * + * Register a test managed platform device. The device is unregistered when the + * test completes. + * + * Returns: 0 on success, negative errno on failure. + */ +int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev) +{ + struct platform_device *res; + + res = kunit_alloc_resource(test, + kunit_platform_device_add_init, + kunit_platform_device_add_exit, + GFP_KERNEL, pdev); + if (!res) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_GPL(kunit_platform_device_add); + +static int kunit_platform_driver_register_init(struct kunit_resource *res, void *context) +{ + struct platform_driver *drv = context; + int ret; + + ret = platform_driver_register(drv); + if (ret) + return ret; + res->data = drv; + + /* + * Wait for the driver to probe (or at least flush out of the deferred + * workqueue) + */ + wait_for_device_probe(); + + return 0; +} + +static void kunit_platform_driver_register_exit(struct kunit_resource *res) +{ + struct platform_driver *drv = res->data; + + platform_driver_unregister(drv); +} + +/** + * kunit_platform_driver_register() - Register a KUnit test managed platform driver + * @test: test context + * @drv: platform driver to register + * + * Register a test managed platform driver. This allows callers to embed the + * @drv in a container structure and use container_of() in the probe function + * to pass information to kunit tests. It can be assumed that the driver has + * probed when this function returns. + * + * Example: + * + * .. code-block:: c + * + * struct kunit_test_context { + * struct platform_driver pdrv; + * const char *data; + * }; + * + * static inline struct kunit_test_context * + * to_test_context(struct platform_device *pdev) + * { + * return container_of(to_platform_driver(pdev->dev.driver), + * struct kunit_test_context, + * pdrv); + * } + * + * static int kunit_platform_driver_probe(struct platform_device *pdev) + * { + * struct kunit_test_context *ctx; + * + * ctx = to_test_context(pdev); + * ctx->data = "test data"; + * + * return 0; + * } + * + * static void kunit_platform_driver_test(struct kunit *test) + * { + * struct kunit_test_context *ctx; + * + * ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + * + * ctx->pdrv.probe = kunit_platform_driver_probe; + * ctx->pdrv.driver.name = "kunit-platform"; + * ctx->pdrv.driver.owner = THIS_MODULE; + * + * KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv)); + * KUNIT_EXPECT_STREQ(test, ctx->data, "test data"); + * } + * + * Returns: 0 on success, negative errno on failure. + */ +int kunit_platform_driver_register(struct kunit *test, + struct platform_driver *drv) +{ + struct platform_driver *res; + + res = kunit_alloc_resource(test, + kunit_platform_driver_register_init, + kunit_platform_driver_register_exit, + GFP_KERNEL, drv); + if (!res) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_GPL(kunit_platform_driver_register); From patchwork Thu Mar 2 01:38:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3980020wrd; Wed, 1 Mar 2023 17:40:48 -0800 (PST) X-Google-Smtp-Source: AK7set+EW+ObEsKRNIxVUgL7GOpTRLmTW0+/uT/aP7UV/tTEWoRS+k9eH814cfrt0lNugUU+nvpk X-Received: by 2002:a17:906:a16:b0:8b1:78b6:4b3c with SMTP id w22-20020a1709060a1600b008b178b64b3cmr8031677ejf.73.1677721248548; Wed, 01 Mar 2023 17:40:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721248; cv=none; d=google.com; s=arc-20160816; b=d6vAqf0zbH6b8Kvzx9Ez2UcrfFKM0WQzi6O4nqhljBwv6/4G4yKu9nF4UQnUKM78IL GcUXmZE8sVeeYKyFHr2ohPEIo79zkcXmLfMrUxBIGiX09rKVN/Ic0+hh/HDo5Ah2/HO1 i5Y5rLqZTHJnuXL47LnoxehBlHZ8JUlQ8aboK2ESebqsVIZqhvSM4M168P3Xn1bGBGiJ 3GXt2G/IOWB+xEnHuDtAFtwnSAkw3peESgP9RmZerUyU3LoKeLFBKBSJvNc8qIs8vH0c 3ZsVit8RY1v4309gszLHtA3oMg0MMgXq3Qt6By35Uge88/QXpEVA9Mi/NECKOC5mNP9w +dKA== 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=ByzIiPa3/Ty5BxCvKqjUoj9ECBkQyvoI00oFOK4vfhM=; b=XBpa9gcEErwd9r9gyApI7c0RRIlkW0FFeTXXuvZNLRRQdOLqvOjJbsTc15Vuau3kim lv0U9u8wYxndiFxPZQTcbs7kGDeASQcG9GCBomdOTmqSYoTscAeFZM0sPfjD7hA5KCY2 Lu1oQS/XrIxqDVshitjpspnkzMWXnU67u0tXtDxK8bfoYjG5dqf4ev02KwvfuQW9eqtZ oJhxzl89Bgk7+IAP+Vvv3ywFsZi+yBS/R1llYWOeo1spkSeBk+r2Z19lNNU0bNhh9TN0 gUIm/lWvuYu4a08EFi52Ia948fm9e+E7/vCc/PvacIMnteTUC464yvfuraTyDX/9qFR/ T/Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=i8zopeNC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id le26-20020a170907171a00b008db13af5641si5391053ejc.261.2023.03.01.17.40.25; Wed, 01 Mar 2023 17:40:48 -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=@kernel.org header.s=k20201202 header.b=i8zopeNC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbjCBBio (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229471AbjCBBif (ORCPT ); Wed, 1 Mar 2023 20:38:35 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4506512BD7; Wed, 1 Mar 2023 17:38:28 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9D6876153E; Thu, 2 Mar 2023 01:38:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 625B0C433D2; Thu, 2 Mar 2023 01:38:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721107; bh=fvPrCAyBkgnxgijN1lTuag/4in6rKiEGnEPO+gbwEs4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i8zopeNC1in3w5XGUJKY/LXKlzK5BRSdMPry4rWNbnjNE84GWR1xF+RINEeejLejd gKJpd8LdNs0TePE9fGcx70fY9XJ9a1rnTqoXELutE5T7dWUUu0TvXclZsGcJKLA9Ta TnaxwHXeO/1BPtBl6s09cfKsPnLL8/Qxj6LP+ZweOmzZPczLC4+iFKN4d6MB6t2ebM XlyovDX82U6lPSCyKUlkFXAD6OHJcEi7K4N64x81/QZvn8Jn0WIlq4kosMjwoNMe3M OV9ydVXq2h7mGhu/mlZ4GkHAYKEQSp5u/n9pe41izH+Ewj/iiX9TOcCMlYBIWrIxyC X6lYxry87mTrw== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 4/8] clk: Add test managed clk provider/consumer APIs Date: Wed, 1 Mar 2023 17:38:17 -0800 Message-Id: <20230302013822.1808711-5-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218235843744168?= X-GMAIL-MSGID: =?utf-8?q?1759218235843744168?= Unit tests are more ergonomic and simpler to understand if they don't have to hoist a bunch of code into the test harness init and exit functions. Add some test managed wrappers for the clk APIs so that clk unit tests can write more code in the actual test and less code in the harness. Only add APIs that are used for now. More wrappers can be added in the future as necessary. Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- drivers/clk/Makefile | 5 + drivers/clk/clk-kunit.c | 204 ++++++++++++++++++++++++++++++++++++++++ drivers/clk/clk-kunit.h | 28 ++++++ 3 files changed, 237 insertions(+) create mode 100644 drivers/clk/clk-kunit.c create mode 100644 drivers/clk/clk-kunit.h diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index e3ca0d058a25..7efce649b0d3 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -17,6 +17,11 @@ ifeq ($(CONFIG_OF), y) obj-$(CONFIG_COMMON_CLK) += clk-conf.o endif +# KUnit specific helpers +ifeq ($(CONFIG_COMMON_CLK), y) +obj-$(CONFIG_KUNIT) += clk-kunit.o +endif + # hardware specific clock types # please keep this section sorted lexicographically by file path name obj-$(CONFIG_COMMON_CLK_APPLE_NCO) += clk-apple-nco.o diff --git a/drivers/clk/clk-kunit.c b/drivers/clk/clk-kunit.c new file mode 100644 index 000000000000..78d85b3a7a4a --- /dev/null +++ b/drivers/clk/clk-kunit.c @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit helpers for clk tests + */ +#include +#include +#include +#include +#include + +#include + +#include "clk-kunit.h" + +static void kunit_clk_disable_unprepare(struct kunit_resource *res) +{ + struct clk *clk = res->data; + + clk_disable_unprepare(clk); +} + +/** + * kunit_clk_prepare_enable() - Test managed clk_prepare_enable() + * @test: The test context + * @clk: clk to prepare and enable + * + * Returns: 0 on success, or negative errno on failure. + */ +int kunit_clk_prepare_enable(struct kunit *test, struct clk *clk) +{ + if (!kunit_alloc_resource(test, NULL, kunit_clk_disable_unprepare, + GFP_KERNEL, clk)) + return -EINVAL; + + return clk_prepare_enable(clk); +} +EXPORT_SYMBOL_GPL(kunit_clk_prepare_enable); + +static void kunit_clk_put(struct kunit_resource *res) +{ + struct clk *clk = res->data; + + clk_put(clk); +} + +/** + * kunit_clk_get() - Test managed clk_get() + * @test: The test context + * @dev: device for clock "consumer" + * @id: clock consumer ID + * + * Returns: new clk consumer or ERR_PTR on failure + */ +struct clk * +kunit_clk_get(struct kunit *test, struct device *dev, const char *con_id) +{ + struct clk *clk; + + clk = clk_get(dev, con_id); + if (IS_ERR(clk)) + return clk; + + if (!kunit_alloc_resource(test, NULL, kunit_clk_put, GFP_KERNEL, clk)) { + clk_put(clk); + return ERR_PTR(-EINVAL); + } + + return clk; +} +EXPORT_SYMBOL_GPL(kunit_clk_get); + +/** + * kunit_of_clk_get() - Test managed of_clk_get() + * @test: The test context + * @np: device_node for clock "consumer" + * @index: index in 'clocks' property of @np + * + * Returns: new clk consumer or ERR_PTR on failure + */ +struct clk * +kunit_of_clk_get(struct kunit *test, struct device_node *np, int index) +{ + struct clk *clk; + + clk = of_clk_get(np, index); + if (IS_ERR(clk)) + return clk; + + if (!kunit_alloc_resource(test, NULL, kunit_clk_put, GFP_KERNEL, clk)) { + clk_put(clk); + return ERR_PTR(-EINVAL); + } + + return clk; +} +EXPORT_SYMBOL_GPL(kunit_of_clk_get); + +/** + * kunit_clk_hw_get_clk() - Test managed clk_hw_get_clk() + * @test: The test context + * @hw: clk_hw associated with the clk being consumed + * @con_id: connection ID string on device + * + * Returns: new clk consumer or ERR_PTR on failure + */ +struct clk * +kunit_clk_hw_get_clk(struct kunit *test, struct clk_hw *hw, const char *con_id) +{ + struct clk *clk; + + clk = clk_hw_get_clk(hw, con_id); + if (IS_ERR(clk)) + return clk; + + if (!kunit_alloc_resource(test, NULL, kunit_clk_put, GFP_KERNEL, clk)) { + clk_put(clk); + return ERR_PTR(-EINVAL); + } + + return clk; +} +EXPORT_SYMBOL_GPL(kunit_clk_hw_get_clk); + +/** + * kunit_clk_hw_get_clk_prepared_enabled() - Test managed clk_hw_get_clk() + clk_prepare_enable() + * @test: The test context + * @hw: clk_hw associated with the clk being consumed + * @con_id: connection ID string on device + * + * Returns: new clk consumer that is prepared and enabled or ERR_PTR on failure + */ +struct clk * +kunit_clk_hw_get_clk_prepared_enabled(struct kunit *test, struct clk_hw *hw, + const char *con_id) +{ + int ret; + struct clk *clk; + + clk = kunit_clk_hw_get_clk(test, hw, con_id); + if (IS_ERR(clk)) + return clk; + + ret = kunit_clk_prepare_enable(test, clk); + if (ret) + return ERR_PTR(ret); + + return clk; +} +EXPORT_SYMBOL_GPL(kunit_clk_hw_get_clk_prepared_enabled); + +static void kunit_clk_hw_unregister(struct kunit_resource *res) +{ + struct clk_hw *hw = res->data; + + clk_hw_unregister(hw); +} + +/** + * kunit_clk_hw_register() - Test managed clk_hw_register() + * @test: The test context + * @dev: device that is registering this clock + * @hw: link to hardware-specific clock data + * + * Returns: 0 on success or a negative errno value on failure + */ +int kunit_clk_hw_register(struct kunit *test, struct device *dev, struct clk_hw *hw) +{ + int ret; + + ret = clk_hw_register(dev, hw); + if (ret) + return ret; + + if (!kunit_alloc_resource(test, NULL, kunit_clk_hw_unregister, GFP_KERNEL, hw)) { + clk_hw_unregister(hw); + return -EINVAL; + } + + return 0; +} + +/** + * kunit_of_clk_hw_register() - Test managed of_clk_hw_register() + * @test: The test context + * @node: device_node of device that is registering this clock + * @hw: link to hardware-specific clock data + * + * Returns: 0 on success or a negative errno value on failure + */ +int kunit_of_clk_hw_register(struct kunit *test, struct device_node *node, struct clk_hw *hw) +{ + int ret; + + ret = of_clk_hw_register(node, hw); + if (ret) + return ret; + + if (!kunit_alloc_resource(test, NULL, kunit_clk_hw_unregister, GFP_KERNEL, hw)) { + clk_hw_unregister(hw); + return -EINVAL; + } + + return 0; +} diff --git a/drivers/clk/clk-kunit.h b/drivers/clk/clk-kunit.h new file mode 100644 index 000000000000..153597d69269 --- /dev/null +++ b/drivers/clk/clk-kunit.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _CLK_KUNIT_H +#define _CLK_KUNIT_H + +struct clk; +struct clk_hw; +struct device; +struct device_node; +struct kunit; + +struct clk * +kunit_clk_get(struct kunit *test, struct device *dev, const char *con_id); +struct clk * +kunit_of_clk_get(struct kunit *test, struct device_node *np, int index); + +struct clk * +kunit_clk_hw_get_clk(struct kunit *test, struct clk_hw *hw, const char *con_id); +struct clk * +kunit_clk_hw_get_clk_prepared_enabled(struct kunit *test, struct clk_hw *hw, + const char *con_id); + +int kunit_clk_prepare_enable(struct kunit *test, struct clk *clk); + +int kunit_clk_hw_register(struct kunit *test, struct device *dev, struct clk_hw *hw); +int kunit_of_clk_hw_register(struct kunit *test, struct device_node *node, + struct clk_hw *hw); + +#endif From patchwork Thu Mar 2 01:38:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63159 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3979822wrd; Wed, 1 Mar 2023 17:40:19 -0800 (PST) X-Google-Smtp-Source: AK7set8XgX68voMDbekDodEjfdD88Lux1kcDktxRS/0G/rTIy4vObX+GC96nZMvPBGDTIXBEUSzU X-Received: by 2002:a17:906:4ec9:b0:881:a3ec:2b43 with SMTP id i9-20020a1709064ec900b00881a3ec2b43mr8394650ejv.56.1677721219351; Wed, 01 Mar 2023 17:40:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721219; cv=none; d=google.com; s=arc-20160816; b=GyfmTC2aKGcycBhA0UnCxBoO1meHmdOvVJ0L95M4K8LAzVwDrE4JhlmfkAlpNaSO64 5QjRAjQtGtSh83EJ1JplEx8agMeYlFfpAnslwb0FWIwkWjt/2d65GrYhhnuXZWcvrFVG UL3Wb+dX5gy5zxmF26eLrEggS3+B6+/ctkPaDEdr07fl+BxXSlHiKRSJ2xyE7Goz+OLG YYf3PMfj1/1aqPUhiyoNO/Xac5bm0AHQ5BIj/v/rRmuh+S7OAeX0WZfLxwYr6MGu6DLX vQVNSiQAMunFNxmfwfk0eh0n/+U7JMT2l9dN0ZryDCD8uAwVpUyRDE4NBgjk/V2PACGj pA/Q== 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=Urs49HBHmT0UEM1g+YYvmfVeAwOcjBK6gw8JqkLFSXM=; b=tlDMeicwliIm8VgR9vWhcmOmObOrV3ZIxB5j94dk4sTipSENQAKqWp6Mup3XX9Vq8x xdgsBuh4nC7M5H6MH60kDREs3I5H8UmqlIMuM8urOF0w8k8S0zE0uEjCNKhTsTB6o8pD RW34kMWOw/wjBQQg4g3hyO99hd0fQZ7DpOgokSNf1NRQ6PV54gs46Rx08m1YH2KV4Bgk fGAvFd2vXsr5R8hY1VWqTFcCx8cRvau9Khc2qX/DHM0xYwhs9/8bRLgfRFaEXiXyioky zED9IXsE5TXdXFwpYM9rYkFA2P8+mWTuqEtv8EuC+0xRCOeZWn4IMLfSZVyTlKqOhSr8 VV6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ClHeefyh; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qk42-20020a1709077faa00b008de42a0a7a1si19798834ejc.825.2023.03.01.17.39.56; Wed, 01 Mar 2023 17:40:19 -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=@kernel.org header.s=k20201202 header.b=ClHeefyh; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229771AbjCBBiq (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229740AbjCBBin (ORCPT ); Wed, 1 Mar 2023 20:38:43 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 062E138032; Wed, 1 Mar 2023 17:38:28 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6E59561547; Thu, 2 Mar 2023 01:38:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35F3AC4339E; Thu, 2 Mar 2023 01:38:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721107; bh=82/H9NmKlkCam/MX1SwZss+jj5L2cQaZPxt44VOdqIg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ClHeefyhLi2pHzIqItEz1Cg9pvG7jsw+TRCffg92mo+4as42h7NjVcqQ7aGpMUsMJ H4i3LOWqFKT3uUz0G8xcvn34yv/fG8Q8X4Jm4hthRYmdwASJfkhEcMcF2+8jzwmW+T j2XDFHyDgnakpgulnJIayrJWFvwZJCVrGjPlqgmvpmoJsb1GKMUEX7LDE+Ny6H5nst gskRJ11qaHSG5Z3sizLM8C3Kwb4VR8BkrZH6tCHZnEeXL5rSAFKHvfIpmrKL7b5oQj 97PatdddLBVHL7TbM8MwEh1CtRC4G9PEbcdywmhmgpd8lk4MOBpZp6MPhXHZr0CeKy X2IjycdnIH8yQ== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 5/8] dt-bindings: kunit: Add fixed rate clk consumer test Date: Wed, 1 Mar 2023 17:38:18 -0800 Message-Id: <20230302013822.1808711-6-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218205519347742?= X-GMAIL-MSGID: =?utf-8?q?1759218205519347742?= Describe a binding for a device that consumes a fixed rate clk in DT so that a KUnit test can get the clk registered by of_fixed_clk_setup() and test that it is setup properly. Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- .../kunit/linux,clk-kunit-fixed-rate.yaml | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Documentation/devicetree/bindings/kunit/linux,clk-kunit-fixed-rate.yaml diff --git a/Documentation/devicetree/bindings/kunit/linux,clk-kunit-fixed-rate.yaml b/Documentation/devicetree/bindings/kunit/linux,clk-kunit-fixed-rate.yaml new file mode 100644 index 000000000000..2d46eb7a6273 --- /dev/null +++ b/Documentation/devicetree/bindings/kunit/linux,clk-kunit-fixed-rate.yaml @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/kunit/linux,clk-kunit-fixed-rate.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: KUnit clk fixed rate test clk consumer + +maintainers: + - Stephen Boyd + +description: | + A clk consumer of a fixed rate clk used to test the fixed rate clk + implementation in the Linux kernel. + +properties: + compatible: + const: linux,clk-kunit-fixed-rate + + clocks: + maxItems: 1 + +required: + - compatible + - clocks + +additionalProperties: false + +examples: + - | + clock-consumer { + compatible = "linux,clk-kunit-fixed-rate"; + clocks = <&fixed_clk>; + }; +... From patchwork Thu Mar 2 01:38:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3980702wrd; Wed, 1 Mar 2023 17:42:35 -0800 (PST) X-Google-Smtp-Source: AK7set/I8xIg9SGEAvx3+fqYYKjwHA97tSI4daImRtrS5gUdldfVRtgz3BHxj4AOnVWWFbGnt2HS X-Received: by 2002:a17:906:31c9:b0:8c7:f906:7fa8 with SMTP id f9-20020a17090631c900b008c7f9067fa8mr9505441ejf.38.1677721355829; Wed, 01 Mar 2023 17:42:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721355; cv=none; d=google.com; s=arc-20160816; b=N6O1TNuSHQpfw2sXSGE0JYcJWRY8iBEiTUqL7lXyJeaTK0h0VSw4SUMS2jYX33QDxM 5RhzN3q8F9RCdLC4QRVePyvtOjoZklh6k03/I1ojDYI21OAG6C6NiwohVj12uXZBj038 8BneNP0DZOAt2A0fcWL3BhVz8XRJe5X2U3SUF+5jmm5v8X4rIoK9t6X4C8p2u89snhhx uoA0mcCfjHWZA9iT4NSoPdXN2HWdYmLoloVhw1Igx3McAixSQa6kkjzNMkz+1uDdGLkn EUYLTRV/1nsHl/AvEAsTykT6SFmaFpiQAZUhXV1GlkKPuf1ynwge3wdhCpPIjEJG9Xm6 XqaQ== 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=VR5VbMJEU5KQSp1yMys/a5jB9l5JUgiuICesXNAzXbY=; b=aNqLlM7U4x/6q6e2wqpX27Mg7odbGLuEe5369pYNNCoqMyyperIwFoJug1PdyibL4J cKV8nJffqmIVVdPQFZVSsz2SPddB3qKvjxJQvvInCmAz8in00s5SMzKC2Z/WApL1p154 ++Ol1TrjFrtsuUmlzuTluwhbCrhv1JRIPthUDhEQMJoRMinurUc0malyxgCqm8Lw4IRO PAY0em0NaDZzMgnTbPF5ErNsVCfaz7Ui2H5Y7jppo0ekeexDCLlRl+PVmbN1Y+7RswsT 3TIOix3wmqFCAiCTk/dXjfjWjlrA6PdCvlkJsKuIyxudSEm0xwSpUPWvcwAjaM0cUXCM mrfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vGJUS7xp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w5-20020aa7dcc5000000b004af60cc2a8csi936959edu.486.2023.03.01.17.42.13; Wed, 01 Mar 2023 17:42:35 -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=@kernel.org header.s=k20201202 header.b=vGJUS7xp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229553AbjCBBiz (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229758AbjCBBio (ORCPT ); Wed, 1 Mar 2023 20:38:44 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54B3D23DB3; Wed, 1 Mar 2023 17:38:31 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id DE7F5B811E7; Thu, 2 Mar 2023 01:38:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09F07C433A7; Thu, 2 Mar 2023 01:38:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721108; bh=YUrgmkmhNxjIFII2DN6KJTUk7Xh2e6Kirr02L2QOPVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vGJUS7xpzaWHki88lJN+lhc/1chy0l1LrjFyvlEv6IU30LKGxCFsBQw7hHQvSkvPq vEJrFH98oL7OsZ8o1OKjQxvjJr7siTFZHHROH5r73qZw4nuuAX2MwJWcA9cke7MT2h mLxZWWi7xybOGcpcgzFe1ZYUenI/E9eZ4TuU/T8UaDZA19+3hKkmF87DlI0KUR8o0Y wvIiApxrC7FXDJwSof6h2Rgt+clUK1Ef5mMamrNq30v2kIuzRFzKuK1Ov4RedPU7jI CaUbf+zoLT/9XaVl6mQZBwxDhRHcEeKeh5ozPK6Eqdzcl1rHpEbMl0V01REdM+TnxE bLXmgEaUhF8ww== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 6/8] clk: Add KUnit tests for clk fixed rate basic type Date: Wed, 1 Mar 2023 17:38:19 -0800 Message-Id: <20230302013822.1808711-7-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218348699667582?= X-GMAIL-MSGID: =?utf-8?q?1759218348699667582?= Test that the fixed rate basic type clk works as intended. Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- This should be extended somewhat to test various combinations of the registration functions. drivers/clk/.kunitconfig | 3 + drivers/clk/Kconfig | 7 + drivers/clk/Makefile | 1 + drivers/clk/clk-fixed-rate_test.c | 296 ++++++++++++++++++++++++++++++ drivers/of/kunit/clk.dtsi | 15 ++ drivers/of/kunit/kunit.dtsi | 1 + 6 files changed, 323 insertions(+) create mode 100644 drivers/clk/clk-fixed-rate_test.c create mode 100644 drivers/of/kunit/clk.dtsi diff --git a/drivers/clk/.kunitconfig b/drivers/clk/.kunitconfig index 2fbeb71316f8..3616cebd22f5 100644 --- a/drivers/clk/.kunitconfig +++ b/drivers/clk/.kunitconfig @@ -1,5 +1,8 @@ CONFIG_KUNIT=y +CONFIG_OF=y +CONFIG_OF_KUNIT=y CONFIG_COMMON_CLK=y CONFIG_CLK_KUNIT_TEST=y +CONFIG_CLK_FIXED_RATE_KUNIT_TEST=y CONFIG_CLK_GATE_KUNIT_TEST=y CONFIG_UML_PCI_OVER_VIRTIO=n diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index d79905f3e174..4849046b821f 100644 --- a/drivers/clk/Kconfig +++ b/drivers/clk/Kconfig @@ -479,6 +479,13 @@ config CLK_KUNIT_TEST help Kunit tests for the common clock framework. +config CLK_FIXED_RATE_KUNIT_TEST + tristate "Basic fixed rate clk type KUnit test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + KUnit tests for the basic fixed rate clk type. + config CLK_GATE_KUNIT_TEST tristate "Basic gate type Kunit test" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index 7efce649b0d3..e0689e9f73a4 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_CLK_KUNIT_TEST) += clk_test.o obj-$(CONFIG_COMMON_CLK) += clk-divider.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-rate.o +obj-$(CONFIG_CLK_FIXED_RATE_KUNIT_TEST) += clk-fixed-rate_test.o obj-$(CONFIG_COMMON_CLK) += clk-gate.o obj-$(CONFIG_CLK_GATE_KUNIT_TEST) += clk-gate_test.o obj-$(CONFIG_COMMON_CLK) += clk-multiplier.o diff --git a/drivers/clk/clk-fixed-rate_test.c b/drivers/clk/clk-fixed-rate_test.c new file mode 100644 index 000000000000..82e9e59a327c --- /dev/null +++ b/drivers/clk/clk-fixed-rate_test.c @@ -0,0 +1,296 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for clk fixed rate basic type + */ +#include +#include +#include +#include + +#include +#include +#include + +#include "clk-kunit.h" + +/** + * struct kunit_clk_hw_fixed_rate_params - Parameters to pass to __clk_hw_register_fixed_rate() + * @dev: device registering clk + * @np: device_node of device registering clk + * @name: name of clk + * @parent_name: parent name of clk + * @parent_hw: clk_hw pointer to parent of clk + * @parent_data: parent_data describing parent of clk + * @flags: clk framework flags + * @fixed_rate: frequency of clk + * @fixed_accuracy: accuracy of clk + * @clk_fixed_flags: fixed rate specific clk flags + */ +struct kunit_clk_hw_fixed_rate_params { + struct device *dev; + struct device_node *np; + const char *name; + const char *parent_name; + const struct clk_hw *parent_hw; + const struct clk_parent_data *parent_data; + unsigned long flags; + unsigned long fixed_rate; + unsigned long fixed_accuracy; + unsigned long clk_fixed_flags; +}; + +static int +kunit_clk_hw_register_fixed_rate_init(struct kunit_resource *res, void *context) +{ + struct kunit_clk_hw_fixed_rate_params *params = context; + struct clk_hw *hw; + + hw = __clk_hw_register_fixed_rate(params->dev, params->np, + params->name, + params->parent_name, + params->parent_hw, + params->parent_data, + params->flags, + params->fixed_rate, + params->fixed_accuracy, + params->clk_fixed_flags, + false); + if (IS_ERR(hw)) + return PTR_ERR(hw); + + res->data = hw; + + return 0; +} + +static void kunit_clk_hw_register_fixed_rate_free(struct kunit_resource *res) +{ + struct clk_hw *hw = res->data; + + clk_hw_unregister_fixed_rate(hw); +} + +/** + * kunit_clk_hw_register_fixed_rate() - Test managed __clk_hw_register_fixed_rate() + * @test: The test context + * @params: Arguments to __clk_hw_register_fixed_rate() + * + * Returns: registered fixed rate clk_hw or ERR_PTR on failure. + */ +static struct clk_hw * +kunit_clk_hw_register_fixed_rate(struct kunit *test, struct kunit_clk_hw_fixed_rate_params *params) +{ + struct clk_hw *hw; + + hw = kunit_alloc_resource(test, + kunit_clk_hw_register_fixed_rate_init, + kunit_clk_hw_register_fixed_rate_free, + GFP_KERNEL, params); + if (!hw) + return ERR_PTR(-EINVAL); + + return hw; +} + +/** + * kunit_clk_hw_unregister_fixed_rate() - Test managed clk_hw_unregister_fixed_rate() + * @test: The test context + * @hw: fixed rate clk to unregister upon test completion + * + * Automatically unregister @hw when @test is complete via + * clk_hw_unregister_fixed_rate(). + * + * Returns: 0 on success or negative errno on failure + */ +static int kunit_clk_hw_unregister_fixed_rate(struct kunit *test, struct clk_hw *hw) +{ + if (!kunit_alloc_resource(test, NULL, + kunit_clk_hw_register_fixed_rate_free, + GFP_KERNEL, hw)) + return -ENOMEM; + + return 0; +} + +/* + * Test that clk_get_rate() on a fixed rate clk registered with + * clk_hw_register_fixed_rate() gets the proper frequency. + */ +static void clk_fixed_rate_rate_test(struct kunit *test) +{ + struct clk_hw *hw; + struct clk *clk; + const unsigned long fixed_rate = 230000; + + hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", NULL, 0, fixed_rate); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, kunit_clk_hw_unregister_fixed_rate(test, hw)); + + clk = kunit_clk_hw_get_clk_prepared_enabled(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, fixed_rate, clk_get_rate(clk)); +} + +/* + * Test that clk_get_accuracy() on a fixed rate clk registered via + * clk_hw_register_fixed_rate_with_accuracy() gets the proper accuracy. + */ +static void clk_fixed_rate_accuracy_test(struct kunit *test) +{ + struct clk_hw *hw; + struct clk *clk; + const unsigned long fixed_accuracy = 5000; + + hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate", + NULL, 0, 0, + fixed_accuracy); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, kunit_clk_hw_unregister_fixed_rate(test, hw)); + + clk = kunit_clk_hw_get_clk(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, fixed_accuracy, clk_get_accuracy(clk)); +} + +/* + * Test that clk_get_parent() on a fixed rate clk gets the proper parent. + */ +static void clk_fixed_rate_parent_test(struct kunit *test) +{ + struct clk_hw *hw, *parent_hw; + struct clk *expected_parent, *actual_parent; + struct clk *clk; + const char *parent_name = "test-fixed-rate-parent"; + struct kunit_clk_hw_fixed_rate_params parent_params = { + .name = parent_name, + }; + + parent_hw = kunit_clk_hw_register_fixed_rate(test, &parent_params); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw)); + + expected_parent = kunit_clk_hw_get_clk(test, parent_hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent); + + hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw); + KUNIT_ASSERT_EQ(test, 0, kunit_clk_hw_unregister_fixed_rate(test, hw)); + + clk = kunit_clk_hw_get_clk(test, hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + actual_parent = clk_get_parent(clk); + KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent)); +} + +static struct kunit_case clk_fixed_rate_test_cases[] = { + KUNIT_CASE(clk_fixed_rate_rate_test), + KUNIT_CASE(clk_fixed_rate_accuracy_test), + KUNIT_CASE(clk_fixed_rate_parent_test), + {} +}; + +static struct kunit_suite clk_fixed_rate_suite = { + .name = "clk_fixed_rate", + .test_cases = clk_fixed_rate_test_cases, +}; + +struct clk_fixed_rate_of_test_context { + struct device *dev; + struct platform_driver pdrv; +}; + +static inline struct clk_fixed_rate_of_test_context * +pdev_to_clk_fixed_rate_of_test_context(struct platform_device *pdev) +{ + return container_of(to_platform_driver(pdev->dev.driver), + struct clk_fixed_rate_of_test_context, + pdrv); +} + +/* + * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper + * rate. + */ +static void clk_fixed_rate_of_probe_test(struct kunit *test) +{ + struct clk_fixed_rate_of_test_context *ctx = test->priv; + struct device *dev = ctx->dev; + struct clk *clk; + + clk = kunit_clk_get(test, dev, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_ASSERT_EQ(test, 0, kunit_clk_prepare_enable(test, clk)); + KUNIT_EXPECT_EQ(test, 50000000, clk_get_rate(clk)); +} + +/* + * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper + * accuracy. + */ +static void clk_fixed_rate_of_accuracy_test(struct kunit *test) +{ + struct clk_fixed_rate_of_test_context *ctx = test->priv; + struct device *dev = ctx->dev; + struct clk *clk; + + clk = kunit_clk_get(test, dev, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk); + + KUNIT_EXPECT_EQ(test, 300, clk_get_accuracy(clk)); +} + +static struct kunit_case clk_fixed_rate_of_cases[] = { + KUNIT_CASE(clk_fixed_rate_of_probe_test), + KUNIT_CASE(clk_fixed_rate_of_accuracy_test), + {} +}; + +static int clk_fixed_rate_of_test_probe(struct platform_device *pdev) +{ + struct clk_fixed_rate_of_test_context *ctx; + + ctx = pdev_to_clk_fixed_rate_of_test_context(pdev); + ctx->dev = &pdev->dev; + + return 0; +} + +static int clk_fixed_rate_of_init(struct kunit *test) +{ + struct clk_fixed_rate_of_test_context *ctx; + static const struct of_device_id match_table[] = { + { .compatible = "linux,clk-kunit-fixed-rate" }, + { } + }; + + if (!IS_ENABLED(CONFIG_OF_KUNIT)) + kunit_skip(test, "requires CONFIG_OF_KUNIT"); + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + ctx->pdrv.probe = clk_fixed_rate_of_test_probe; + ctx->pdrv.driver.of_match_table = match_table; + ctx->pdrv.driver.name = __func__; + ctx->pdrv.driver.owner = THIS_MODULE; + + return kunit_platform_driver_register(test, &ctx->pdrv); +} + +static struct kunit_suite clk_fixed_rate_of_suite = { + .name = "clk_fixed_rate_of", + .init = clk_fixed_rate_of_init, + .test_cases = clk_fixed_rate_of_cases, +}; + +kunit_test_suites( + &clk_fixed_rate_suite, + &clk_fixed_rate_of_suite, +); +MODULE_LICENSE("GPL"); diff --git a/drivers/of/kunit/clk.dtsi b/drivers/of/kunit/clk.dtsi new file mode 100644 index 000000000000..e3466bcfeb4b --- /dev/null +++ b/drivers/of/kunit/clk.dtsi @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +/ { + fixed_50MHz: clock-50MHz { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = <50000000>; + clock-accuracy = <300>; + }; + + clock-consumer-fixed-50 { + compatible = "linux,clk-kunit-fixed-rate"; + clocks = <&fixed_50MHz>; + }; +}; diff --git a/drivers/of/kunit/kunit.dtsi b/drivers/of/kunit/kunit.dtsi index 82f6c3e2b8d5..bce5bd8b9505 100644 --- a/drivers/of/kunit/kunit.dtsi +++ b/drivers/of/kunit/kunit.dtsi @@ -6,3 +6,4 @@ / { }; /* Include testcase dtsi files below */ +#include "clk.dtsi" From patchwork Thu Mar 2 01:38:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63163 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3980692wrd; Wed, 1 Mar 2023 17:42:34 -0800 (PST) X-Google-Smtp-Source: AK7set/qQBo7R1Pe8TsOmFo/D5dcFn4PM10PBdD82h7SQM1KwvUlhsUtYYAKVDyxwwdVlqK8NLwP X-Received: by 2002:a17:906:a18c:b0:8f8:35c2:1357 with SMTP id s12-20020a170906a18c00b008f835c21357mr8094641ejy.23.1677721354790; Wed, 01 Mar 2023 17:42:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721354; cv=none; d=google.com; s=arc-20160816; b=e1KK6iRC5wNuUGf4ZNRB+dMLxlcfibwxtTAnIOP4UgMv2QaWyhzg8f77WwuyULlcL0 LeJPY1aLfZRJzl4zxBQZfpXstDnvaW4S82e5wLsuYRcbX3eFcYj7Y9BN5Rv2DPa7YcXp OHDFiEvN9ILWydA+MCCRx8JA3OoJ2HE6k0lv5sq2ZbbMO/GfOiuUT83Z5jZVGLHyYs8b ILTFxD3vvJxoWdFevr3vV8nK9KvEzYA7E2gjoDYYo1nDVjuNfmpDF8hWFSktU48FGfTu SreNOgN7NJ94lkPcoqKCkevaK3UYe3BUw7IzswB33qH1QZ3P9OJXXNjrjmqqQNWwKqzM CmuA== 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=vfI58wuMbuwhclbxOV/zwZIQLHu8rbPMEip3UAvIgkI=; b=Ik+xNROCKfTAaylf6djOKRX5Y+m6WOdk6tWhy/V0OWHBZ+rWLt5LYj3fa5CnzkB1A9 ltgeSIdB7euTFutQOuoS0a/xQr2LPqYUw9V06Pebpj8YaY8H835K8tqYnrXeFthAYWZd MadJlpO59vgQ3FAEs37l+lVTuqDyR5ODavUBHEzgofNQlcl35gsB7WqnjGeZD/2xKo6c Z72AgW85CiR6HeIB//e99xHHcGkizR+AYymSDun5UOgTkpZzYq27gyRCZE/w0JT/Z9TD OwgL3nlmCzI+P1/3YyI+ml/I5OIn4y0Cw9+CIwrHtHrjxSu2Xd2xkxHU0vjmlZc3bk3Q YWNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m6Rk22s1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sd27-20020a170906ce3b00b008b1ce6f5811si2982702ejb.345.2023.03.01.17.42.12; Wed, 01 Mar 2023 17:42:34 -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=@kernel.org header.s=k20201202 header.b=m6Rk22s1; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229754AbjCBBiv (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229734AbjCBBin (ORCPT ); Wed, 1 Mar 2023 20:38:43 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80B9C39CD7; Wed, 1 Mar 2023 17:38:30 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1500A6153F; Thu, 2 Mar 2023 01:38:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D25C5C433D2; Thu, 2 Mar 2023 01:38:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721109; bh=/l+SZdmlEj6k/2KkGkyZGLbEgxbQ4jmIkEvZrUe5VZs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m6Rk22s1o6bU0lm2Ynjuyb6MIDTN+qyyMJiFSr1BXExoXWkZqzXLDKkf3hBns0OQD Fu4gxDGugxNn39ZJXiGN7GkSXKt1MXixXjLBeHmxivEWKLYO0ah4cVvjRUfCjitHEW SoDckBVLBvh9sQ/m/4Ge6IM8Q2lmBIP05Fnmx8dPmB/FPFYgaY6Ms85AcXzLcaoRHm 9GcPolkHoK8M0Nva04vWUO28cKR4sK6L2xGZpGvvWmouuf965qHjBXYXeVn2xFFHLo 67whL7JZkCIevfHzUd7nHEJqYyhLjmMB2N5Nuw4ZzC0tgLGjFqEYRVAYNK+kesBJje QOI7UAvT1e9LA== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 7/8] dt-bindings: clk: Add KUnit clk_parent_data test Date: Wed, 1 Mar 2023 17:38:20 -0800 Message-Id: <20230302013822.1808711-8-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218347806873919?= X-GMAIL-MSGID: =?utf-8?q?1759218347806873919?= Describe a binding for a device that provides and consumes clks in DT so that a KUnit test can register clks based on the device node and test clk_hw_register() with clk_parent_data. Cc: Rob Herring Cc: Krzysztof Kozlowski Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- .../clock/linux,clk-kunit-parent-data.yaml | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Documentation/devicetree/bindings/clock/linux,clk-kunit-parent-data.yaml diff --git a/Documentation/devicetree/bindings/clock/linux,clk-kunit-parent-data.yaml b/Documentation/devicetree/bindings/clock/linux,clk-kunit-parent-data.yaml new file mode 100644 index 000000000000..29609e07c115 --- /dev/null +++ b/Documentation/devicetree/bindings/clock/linux,clk-kunit-parent-data.yaml @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/clock/linux,clk-kunit-parent-data.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: KUnit clk provider for clk_parent_data unit tests + +maintainers: + - Stephen Boyd + +description: | + A clk provider to test the struct clk_parent_data implementation in the Linux + kernel. + +properties: + compatible: + const: linux,clk-kunit-parent-data + + clocks: + items: + - description: Fixed parent + - description: 50 MHz fixed parent + + clock-names: + items: + - const: parent_fwname + - const: "50" + + "#clock-cells": + const: 1 + +required: + - compatible + - "#clock-cells" + +additionalProperties: false + +examples: + - | + clock-controller { + compatible = "linux,clk-kunit-parent-data"; + #clock-cells = <1>; + clocks = <&fixed_parent>, <&fixed_50MHz>; + clock-names = "parent_fwname", "50"; + }; +... From patchwork Thu Mar 2 01:38:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Boyd X-Patchwork-Id: 63165 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3980811wrd; Wed, 1 Mar 2023 17:42:52 -0800 (PST) X-Google-Smtp-Source: AK7set/LS6YdcCmia97DoM1Ea31KlfkDAS9pUJY6inPZBlqVngpUpq2bQCJBWYIW38bpy0MfxxzN X-Received: by 2002:a17:906:edd6:b0:906:4739:d73e with SMTP id sb22-20020a170906edd600b009064739d73emr507792ejb.2.1677721371841; Wed, 01 Mar 2023 17:42:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677721371; cv=none; d=google.com; s=arc-20160816; b=vmz7P4mGy9RMiM+zZUK3JdjgQzCAiipBJB5nJBvQeA4guP4QdSBpCMg1rCdBihHVeC ie3FXTKKmHQHJhrZ0lFbJN0kIKhIpvNTBbexgarzez9pH8BrzZ3OuSVji6WnE7amyIw6 sif1fvmooivQz34QZ/CAP3dKOKU+yGgL8BnWqXK32SSmLlK7rko5rL0LHOb54zVS2wQE Jm+mv6CVeY6svLTEbfovvdVTsZE2hNiXfeTXI4L8hyUYDkogJOvE4Hf3ybWlq0lYQUht kWWa/tMJQGslfsByst0T0c/ts+sC+j4UByBjt9EC2llgK1gtan+nVxBdE3SudheoFw3D QnOg== 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=0cU9kamuf2oJauYme+7ZH0EVopGQbz2+fFqnMFt0OfE=; b=tPQj8btHUGQ2wWfrSonnlEI9UB7QYeSIHoG1/gHAdL/eQDKPntXBWWxPcBJyytUxhh TDmvZ8VAMp+Y3W3+qr6ny64Iz4H4ay6TaGOiVz7xYUtnVh6aP7FLAwv6wdnXUlupKvLt ch9b6Rnc2KyK1Y/ht5EJ6f6VWKo5mlncD2JTRASqf8aRLS8UOxYtEUFqzaHIKtGvBZyT QnGSirJSRqCQ6rrVcT2A/HMvqXz1ND49OwkGbY8ij5QLaIlJtJeXge7ks0kl36eNs616 5axr0eaR5eCtKaq83Lfp2TDHHh975RXRJt67SxdlrND4pEjJHdW2JjRZKuvBaEVaHkB3 Yl6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oQSOuVS5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n7-20020aa7c447000000b004be734e2de8si1806164edr.68.2023.03.01.17.42.29; Wed, 01 Mar 2023 17:42:51 -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=@kernel.org header.s=k20201202 header.b=oQSOuVS5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229523AbjCBBi6 (ORCPT + 99 others); Wed, 1 Mar 2023 20:38:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbjCBBio (ORCPT ); Wed, 1 Mar 2023 20:38:44 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62C9137B61; Wed, 1 Mar 2023 17:38:31 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E6AA761543; Thu, 2 Mar 2023 01:38:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A53C4C4339B; Thu, 2 Mar 2023 01:38:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677721110; bh=8V/QzCKxoD3493UXD/fF79fcE0FqXapWjtJR7YpxH1U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oQSOuVS5Mo1uHlylczcUBWIFIj3dtpl6ppG3ilqpFnPZgekhqFNCX0Dkl/P5GQUSO ZYOkUN+WfAijW/KzyfW+fOcyYhvWoTxWMd6wcHLj48Jw5LUdNDmYrpCOXAJcqhzONu j3t5S85JnvwSQQE+Ua3OALKQci40SB7Vi68jgkTrYsV+Yz/PMjHGM3WyboWZvnhZPx HpB9hTg8ht5Z/U9IEAP7gVSc3O1Y5bCJ8vtkpAucL+MzizProarnnhDXlNBPqaDaEw Luwlu47wgwLL2fuS0C2JcYJ1R2/zf+Xy+tc9DAT8lv65DDKsCCpeqQs7HRRMimkGh2 aPeeUN/lMwOrQ== From: Stephen Boyd To: Michael Turquette , Stephen Boyd Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, Brendan Higgins , David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Richard Weinberger , Anton Ivanov , Johannes Berg , Vincent Whitchurch , Rob Herring , Frank Rowand , Christian Marangi , Krzysztof Kozlowski , devicetree@vger.kernel.org, linux-um@lists.infradead.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH 8/8] clk: Add KUnit tests for clks registered with struct clk_parent_data Date: Wed, 1 Mar 2023 17:38:21 -0800 Message-Id: <20230302013822.1808711-9-sboyd@kernel.org> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302013822.1808711-1-sboyd@kernel.org> References: <20230302013822.1808711-1-sboyd@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759218365385123680?= X-GMAIL-MSGID: =?utf-8?q?1759218365385123680?= Test that clks registered with 'struct clk_parent_data' work as intended and can find their parents. Cc: Christian Marangi Cc: Brendan Higgins Cc: David Gow Signed-off-by: Stephen Boyd --- drivers/clk/clk_test.c | 456 +++++++++++++++++++++++++++++++++++++- drivers/of/kunit/clk.dtsi | 15 ++ 2 files changed, 470 insertions(+), 1 deletion(-) diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c index f9a5c2964c65..ad63958b809d 100644 --- a/drivers/clk/clk_test.c +++ b/drivers/clk/clk_test.c @@ -4,10 +4,14 @@ */ #include #include +#include +#include /* Needed for clk_hw_get_clk() */ #include "clk.h" +#include "clk-kunit.h" +#include #include #define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000) @@ -2394,6 +2398,454 @@ static struct kunit_suite clk_mux_notifier_test_suite = { .test_cases = clk_mux_notifier_test_cases, }; +struct clk_register_clk_parent_data_test_case { + const char *desc; + struct clk_parent_data pdata; +}; + +static void +clk_register_clk_parent_data_test_case_to_desc( + const struct clk_register_clk_parent_data_test_case *t, char *desc) +{ + strcpy(desc, t->desc); +} + +static const struct clk_register_clk_parent_data_test_case +clk_register_clk_parent_data_of_cases[] = { + { + /* + * Test that a clk registered with a struct device_node can + * find a parent based on struct clk_parent_data::index. + */ + .desc = "clk_parent_data_of_index_test", + .pdata.index = 0, + }, + { + /* + * Test that a clk registered with a struct device_node can + * find a parent based on struct clk_parent_data::fwname. + */ + .desc = "clk_parent_data_of_fwname_test", + .pdata.fw_name = "parent_fwname", + }, + { + /* + * Test that a clk registered with a struct device_node can + * find a parent based on struct clk_parent_data::name. + */ + .desc = "clk_parent_data_of_name_test", + /* The index must be negative to indicate firmware not used */ + .pdata.index = -1, + .pdata.name = "1mhz_fixed_legacy", + }, + { + /* + * Test that a clk registered with a struct device_node can + * find a parent based on struct + * clk_parent_data::{fw_name,name}. + */ + .desc = "clk_parent_data_of_fwname_name_test", + .pdata.fw_name = "parent_fwname", + .pdata.name = "not_matching", + }, + { + /* + * Test that a clk registered with a struct device_node can + * find a parent based on struct clk_parent_data::{index,name}. + * Index takes priority. + */ + .desc = "clk_parent_data_of_index_name_priority_test", + .pdata.index = 0, + .pdata.name = "not_matching", + }, + { + /* + * Test that a clk registered with a struct device_node can + * find a parent based on struct + * clk_parent_data::{index,fwname,name}. The fw_name takes + * priority over index and name. + */ + .desc = "clk_parent_data_of_index_fwname_name_priority_test", + .pdata.index = 1, + .pdata.fw_name = "parent_fwname", + .pdata.name = "not_matching", + }, +}; + +KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_of_test, clk_register_clk_parent_data_of_cases, + clk_register_clk_parent_data_test_case_to_desc) + +/** + * struct clk_register_clk_parent_data_of_ctx - Context for clk_parent_data OF tests + * @np: device node of clk under test + * @hw: clk_hw for clk under test + */ +struct clk_register_clk_parent_data_of_ctx { + struct device_node *np; + struct clk_hw hw; +}; + +static int clk_register_clk_parent_data_of_test_init(struct kunit *test) +{ + struct clk_register_clk_parent_data_of_ctx *ctx; + + if (!IS_ENABLED(CONFIG_OF_KUNIT)) + kunit_skip(test, "requires CONFIG_OF_KUNIT"); + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + ctx->np = of_find_compatible_node(NULL, NULL, "linux,clk-kunit-parent-data"); + if (!ctx->np) + return -ENODEV; + + return 0; +} + +static void clk_register_clk_parent_data_of_test_exit(struct kunit *test) +{ + struct clk_register_clk_parent_data_of_ctx *ctx = test->priv; + + of_node_put(ctx->np); +} + +/* + * Test that a clk registered with a struct device_node can find a parent based on + * struct clk_parent_data when the hw member isn't set. + */ +static void clk_register_clk_parent_data_of_test(struct kunit *test) +{ + struct clk_register_clk_parent_data_of_ctx *ctx = test->priv; + struct clk_hw *parent_hw; + const struct clk_register_clk_parent_data_test_case *test_param; + struct clk_init_data init = { }; + struct clk *expected_parent, *actual_parent; + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->np); + + expected_parent = kunit_of_clk_get(test, ctx->np, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent); + + test_param = test->param_value; + init.parent_data = &test_param->pdata; + init.num_parents = 1; + init.name = "parent_data_of_test_clk"; + init.ops = &clk_dummy_single_parent_ops; + ctx->hw.init = &init; + KUNIT_ASSERT_EQ(test, 0, kunit_of_clk_hw_register(test, ctx->np, &ctx->hw)); + + parent_hw = clk_hw_get_parent(&ctx->hw); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + + actual_parent = kunit_clk_hw_get_clk(test, parent_hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent); + + KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent)); +} + +static struct kunit_case clk_register_clk_parent_data_of_test_cases[] = { + KUNIT_CASE_PARAM(clk_register_clk_parent_data_of_test, + clk_register_clk_parent_data_of_test_gen_params), + {} +}; + +/* + * Test suite for registering clks with struct clk_parent_data and a struct + * device_node. + */ +static struct kunit_suite clk_register_clk_parent_data_of_suite = { + .name = "clk_register_clk_parent_data_of", + .init = clk_register_clk_parent_data_of_test_init, + .exit = clk_register_clk_parent_data_of_test_exit, + .test_cases = clk_register_clk_parent_data_of_test_cases, +}; + +/** + * struct clk_register_clk_parent_data_device_ctx - Context for clk_parent_data device tests + * @dev: device of clk under test + * @hw: clk_hw for clk under test + * @pdrv: driver to attach to find @dev + */ +struct clk_register_clk_parent_data_device_ctx { + struct device *dev; + struct clk_hw hw; + struct platform_driver pdrv; +}; + +static inline struct clk_register_clk_parent_data_device_ctx * +clk_register_clk_parent_data_driver_to_test_context(struct platform_device *pdev) +{ + return container_of(to_platform_driver(pdev->dev.driver), + struct clk_register_clk_parent_data_device_ctx, pdrv); +} + +static int clk_register_clk_parent_data_device_probe(struct platform_device *pdev) +{ + struct clk_register_clk_parent_data_device_ctx *ctx; + + ctx = clk_register_clk_parent_data_driver_to_test_context(pdev); + ctx->dev = &pdev->dev; + + return 0; +} + +static void clk_register_clk_parent_data_device_driver(struct kunit *test) +{ + struct clk_register_clk_parent_data_device_ctx *ctx = test->priv; + static const struct of_device_id match_table[] = { + { .compatible = "linux,clk-kunit-parent-data" }, + { } + }; + + ctx->pdrv.probe = clk_register_clk_parent_data_device_probe; + ctx->pdrv.driver.of_match_table = match_table; + ctx->pdrv.driver.name = __func__; + ctx->pdrv.driver.owner = THIS_MODULE; + + KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv)); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dev); +} + +static const struct clk_register_clk_parent_data_test_case +clk_register_clk_parent_data_device_cases[] = { + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::index. + */ + .desc = "clk_parent_data_device_index_test", + .pdata.index = 1, + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::fwname. + */ + .desc = "clk_parent_data_device_fwname_test", + .pdata.fw_name = "50", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::name. + */ + .desc = "clk_parent_data_device_name_test", + /* The index must be negative to indicate firmware not used */ + .pdata.index = -1, + .pdata.name = "50_clk", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::{fw_name,name}. + */ + .desc = "clk_parent_data_device_fwname_name_test", + .pdata.fw_name = "50", + .pdata.name = "not_matching", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::{index,name}. Index + * takes priority. + */ + .desc = "clk_parent_data_device_index_name_priority_test", + .pdata.index = 1, + .pdata.name = "not_matching", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::{index,fwname,name}. + * The fw_name takes priority over index and name. + */ + .desc = "clk_parent_data_device_index_fwname_name_priority_test", + .pdata.index = 0, + .pdata.fw_name = "50", + .pdata.name = "not_matching", + }, +}; + +KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_test, + clk_register_clk_parent_data_device_cases, + clk_register_clk_parent_data_test_case_to_desc) + +/* + * Test that a clk registered with a struct device can find a parent based on + * struct clk_parent_data when the hw member isn't set. + */ +static void clk_register_clk_parent_data_device_test(struct kunit *test) +{ + struct clk_register_clk_parent_data_device_ctx *ctx; + const struct clk_register_clk_parent_data_test_case *test_param; + struct clk_hw *parent_hw; + struct clk_init_data init = { }; + struct clk *expected_parent, *actual_parent; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + test->priv = ctx; + + clk_register_clk_parent_data_device_driver(test); + + expected_parent = kunit_clk_get(test, ctx->dev, "50"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent); + + test_param = test->param_value; + init.parent_data = &test_param->pdata; + init.num_parents = 1; + init.name = "parent_data_device_test_clk"; + init.ops = &clk_dummy_single_parent_ops; + ctx->hw.init = &init; + KUNIT_ASSERT_EQ(test, 0, kunit_clk_hw_register(test, ctx->dev, &ctx->hw)); + + parent_hw = clk_hw_get_parent(&ctx->hw); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + + actual_parent = kunit_clk_hw_get_clk(test, parent_hw, __func__); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, actual_parent); + + KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent)); +} + +static const struct clk_register_clk_parent_data_test_case +clk_register_clk_parent_data_device_hw_cases[] = { + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw. + */ + .desc = "clk_parent_data_device_hw_index_test", + /* The index must be negative to indicate firmware not used */ + .pdata.index = -1, + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw when + * struct clk_parent_data::fw_name is set. + */ + .desc = "clk_parent_data_device_hw_fwname_test", + .pdata.fw_name = "50", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw when struct + * clk_parent_data::name is set. + */ + .desc = "clk_parent_data_device_hw_name_test", + /* The index must be negative to indicate firmware not used */ + .pdata.index = -1, + .pdata.name = "50_clk", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw when struct + * clk_parent_data::{fw_name,name} are set. + */ + .desc = "clk_parent_data_device_hw_fwname_name_test", + .pdata.fw_name = "50", + .pdata.name = "not_matching", + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw when struct + * clk_parent_data::index is set. The hw pointer takes + * priority. + */ + .desc = "clk_parent_data_device_hw_index_priority_test", + .pdata.index = 0, + }, + { + /* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw when + * struct clk_parent_data::{index,fwname,name} are set. + * The hw pointer takes priority over everything else. + */ + .desc = "clk_parent_data_device_hw_index_fwname_name_priority_test", + .pdata.index = 0, + .pdata.fw_name = "50", + .pdata.name = "not_matching", + }, +}; + +KUNIT_ARRAY_PARAM(clk_register_clk_parent_data_device_hw_test, + clk_register_clk_parent_data_device_hw_cases, + clk_register_clk_parent_data_test_case_to_desc) + +/* + * Test that a clk registered with a struct device can find a + * parent based on struct clk_parent_data::hw. + */ +static void clk_register_clk_parent_data_device_hw_test(struct kunit *test) +{ + struct clk_register_clk_parent_data_device_ctx *ctx; + const struct clk_register_clk_parent_data_test_case *test_param; + struct clk_dummy_context *parent; + struct clk_hw *parent_hw; + struct clk_parent_data pdata = { }; + struct clk_init_data init = { }; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + test->priv = ctx; + + clk_register_clk_parent_data_device_driver(test); + + parent = kunit_kzalloc(test, sizeof(*parent), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + + parent_hw = &parent->hw; + parent_hw->init = CLK_HW_INIT_NO_PARENT("parent-clk", + &clk_dummy_rate_ops, 0); + + KUNIT_ASSERT_EQ(test, 0, kunit_clk_hw_register(test, ctx->dev, parent_hw)); + + test_param = test->param_value; + memcpy(&pdata, &test_param->pdata, sizeof(pdata)); + pdata.hw = parent_hw; + init.parent_data = &pdata; + init.num_parents = 1; + init.ops = &clk_dummy_single_parent_ops; + init.name = "parent_data_device_hw_test_clk"; + ctx->hw.init = &init; + KUNIT_ASSERT_EQ(test, 0, kunit_clk_hw_register(test, ctx->dev, &ctx->hw)); + + KUNIT_EXPECT_PTR_EQ(test, parent_hw, clk_hw_get_parent(&ctx->hw)); +} + +static struct kunit_case clk_register_clk_parent_data_device_test_cases[] = { + KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_test, + clk_register_clk_parent_data_device_test_gen_params), + KUNIT_CASE_PARAM(clk_register_clk_parent_data_device_hw_test, + clk_register_clk_parent_data_device_hw_test_gen_params), + {} +}; + +static int clk_register_clk_parent_data_device_init(struct kunit *test) +{ + if (!IS_ENABLED(CONFIG_OF_KUNIT)) + kunit_skip(test, "requires CONFIG_OF_KUNIT"); + + return 0; +} + +/* + * Test suite for registering clks with struct clk_parent_data and a struct + * device. + */ +static struct kunit_suite clk_register_clk_parent_data_device_suite = { + .name = "clk_register_clk_parent_data_device", + .init = clk_register_clk_parent_data_device_init, + .test_cases = clk_register_clk_parent_data_device_test_cases, +}; + kunit_test_suites( &clk_leaf_mux_set_rate_parent_test_suite, &clk_test_suite, @@ -2405,7 +2857,9 @@ kunit_test_suites( &clk_range_test_suite, &clk_range_maximize_test_suite, &clk_range_minimize_test_suite, + &clk_register_clk_parent_data_of_suite, + &clk_register_clk_parent_data_device_suite, &clk_single_parent_mux_test_suite, - &clk_uncached_test_suite + &clk_uncached_test_suite, ); MODULE_LICENSE("GPL v2"); diff --git a/drivers/of/kunit/clk.dtsi b/drivers/of/kunit/clk.dtsi index e3466bcfeb4b..4ac14dd78063 100644 --- a/drivers/of/kunit/clk.dtsi +++ b/drivers/of/kunit/clk.dtsi @@ -6,10 +6,25 @@ fixed_50MHz: clock-50MHz { #clock-cells = <0>; clock-frequency = <50000000>; clock-accuracy = <300>; + clock-output-names = "50_clk"; }; clock-consumer-fixed-50 { compatible = "linux,clk-kunit-fixed-rate"; clocks = <&fixed_50MHz>; }; + + fixed_parent: clock-1MHz { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = <1000000>; + clock-output-names = "1mhz_fixed_legacy"; + }; + + clock-provider-of { + compatible = "linux,clk-kunit-parent-data"; + clocks = <&fixed_parent>, <&fixed_50MHz>; + clock-names = "parent_fwname", "50"; + #clock-cells = <1>; + }; };