From nobody Mon Apr 29 10:55:02 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87863C76195 for ; Sat, 25 Mar 2023 00:27:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231978AbjCYA1v (ORCPT ); Fri, 24 Mar 2023 20:27:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231810AbjCYA1l (ORCPT ); Fri, 24 Mar 2023 20:27:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A01216317; Fri, 24 Mar 2023 17:27:30 -0700 (PDT) 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 679D062CF9; Sat, 25 Mar 2023 00:27:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BB3EC4339B; Sat, 25 Mar 2023 00:27:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679704049; bh=wyNzs9+T4TyTUveC2AG8JrzhOfbyVA0mVx0XwJQWyR8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WIn1LBinv3V3T/MNmMcNGUKVyJmZhS9Kyg+lR3NNk3uS7hn3DoyUGeYA0Xvv0xlrF ZI6//tDvhHQScbJU4s8mg8OUlEeRXcQ6sJ8imYmAJ3nU4eWacHJtcvUiLk3Nn97Zrg PdsbYmUHsb7T47EYb1EIYczomrIhuVBkCspVwlcSCWjmZIskQPs5mzjjEpU1Py6bUv 5e73+croBMf54a0VApkSTpPmR4dkf0aQCxZFHB34IFpVtlxdJwxNTFhDvoytLC1mjL VBVYbaDUKXZ+cX19AF7ZEzJKvYaLdMpINUAG8RFpX8tJBWm5eTws/i3VwpTeMitBBL U5cGa61RWOOig== From: Mark Brown Date: Sat, 25 Mar 2023 00:27:17 +0000 Subject: [PATCH 1/2] regmap: Add RAM backed register map MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230324-regmap-kunit-v1-1-62ef9cfa9b89@kernel.org> References: <20230324-regmap-kunit-v1-0-62ef9cfa9b89@kernel.org> In-Reply-To: <20230324-regmap-kunit-v1-0-62ef9cfa9b89@kernel.org> To: David Gow , Brendan Higgins Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-2eb1a X-Developer-Signature: v=1; a=openpgp-sha256; l=4600; i=broonie@kernel.org; h=from:subject:message-id; bh=wyNzs9+T4TyTUveC2AG8JrzhOfbyVA0mVx0XwJQWyR8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBkHj/rlabPE/xfbKcOr64yF5y9tx55d09fMVQozH8m 6S72S5yJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZB4/6wAKCRAk1otyXVSH0Jt2B/ 4/sh9njhXJ8E7Kp5vUupdHqBkj5vZCP3gIm6He5dSWxc2lvW6guhtfTTlg1DBf4rFFe3asaWmcxAyw 4BEaHEww9BXVMe7QYOZTpmO1X5Si3ljpD4QPrWPXWRv0kdyvcI4IF4vYyb6+yWZAKUHADkSOtrKNyg ZHcy+cz34nTg3OPq7cM1H6IyKoR1SFAHhH/10Hkt+lpvnVBvJcSBGBoJUptmlPrlcDLW5MmQZEprOU Cl5NvnWwPNNGiKuByVlpRPx/t5eoe7ovHGOlEzqDl9ymVUEW1iCkcuc2YYfxqgkNvqS72iTPSe4Ncn 6WQbmMSzaajdjPprq7n3NPoh105JhP X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a register map that is a simple array of memory, for use in KUnit testing of the framework. This is not exposed in regmap.h since I can't think of a non-test use case, it is purely for use internally. To facilitate testing we track if registers have been read or written to. Signed-off-by: Mark Brown --- drivers/base/regmap/Kconfig | 3 ++ drivers/base/regmap/Makefile | 1 + drivers/base/regmap/internal.h | 19 +++++++++ drivers/base/regmap/regmap-ram.c | 85 ++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 108 insertions(+) diff --git a/drivers/base/regmap/Kconfig b/drivers/base/regmap/Kconfig index cd4bb642b9de..65ce888d7c04 100644 --- a/drivers/base/regmap/Kconfig +++ b/drivers/base/regmap/Kconfig @@ -46,6 +46,9 @@ config REGMAP_MMIO config REGMAP_IRQ bool =20 +config REGMAP_RAM + tristate + config REGMAP_SOUNDWIRE tristate depends on SOUNDWIRE diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile index 6990de7ca9a9..5ef6f129497c 100644 --- a/drivers/base/regmap/Makefile +++ b/drivers/base/regmap/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_REGCACHE_COMPRESSED) +=3D regcache-lzo.o obj-$(CONFIG_DEBUG_FS) +=3D regmap-debugfs.o obj-$(CONFIG_REGMAP_AC97) +=3D regmap-ac97.o obj-$(CONFIG_REGMAP_I2C) +=3D regmap-i2c.o +obj-$(CONFIG_REGMAP_RAM) +=3D regmap-ram.o obj-$(CONFIG_REGMAP_SLIMBUS) +=3D regmap-slimbus.o obj-$(CONFIG_REGMAP_SPI) +=3D regmap-spi.o obj-$(CONFIG_REGMAP_SPMI) +=3D regmap-spmi.o diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h index da8996e7a1f1..aa98a5284fb3 100644 --- a/drivers/base/regmap/internal.h +++ b/drivers/base/regmap/internal.h @@ -307,4 +307,23 @@ static inline unsigned int regcache_get_index_by_order= (const struct regmap *map, return reg >> map->reg_stride_order; } =20 +struct regmap_ram_data { + unsigned int *vals; /* Allocatd by caller */ + bool *read; + bool *written; +}; + +/* + * Create a test register map with data stored in RAM, not intended + * for practical use. + */ +struct regmap *__regmap_init_ram(const struct regmap_config *config, + struct regmap_ram_data *data, + struct lock_class_key *lock_key, + const char *lock_name); + +#define regmap_init_ram(config, data) \ + __regmap_lockdep_wrapper(__regmap_init_ram, #config, config, data) + + #endif diff --git a/drivers/base/regmap/regmap-ram.c b/drivers/base/regmap/regmap-= ram.c new file mode 100644 index 000000000000..85f34a5dee04 --- /dev/null +++ b/drivers/base/regmap/regmap-ram.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Register map access API - Memory region +// +// This is intended for testing only +// +// Copyright (c) 2023, Arm Ltd + +#include +#include +#include +#include +#include +#include +#include + +#include "internal.h" + +static int regmap_ram_write(void *context, unsigned int reg, unsigned int = val) +{ + struct regmap_ram_data *data =3D context; + + data->vals[reg] =3D val; + data->written[reg] =3D true; + + return 0; +} + +static int regmap_ram_read(void *context, unsigned int reg, unsigned int *= val) +{ + struct regmap_ram_data *data =3D context; + + *val =3D data->vals[reg]; + data->read[reg] =3D true; + + return 0; +} + +static void regmap_ram_free_context(void *context) +{ + struct regmap_ram_data *data =3D context; + + kfree(data->vals); + kfree(data->read); + kfree(data->written); + kfree(data); +} + +static const struct regmap_bus regmap_ram =3D { + .fast_io =3D true, + .reg_write =3D regmap_ram_write, + .reg_read =3D regmap_ram_read, + .free_context =3D regmap_ram_free_context, +}; + +struct regmap *__regmap_init_ram(const struct regmap_config *config, + struct regmap_ram_data *data, + struct lock_class_key *lock_key, + const char *lock_name) +{ + struct regmap *map; + + if (!config->max_register) { + pr_crit("No max_register specified for RAM regmap\n"); + return ERR_PTR(-EINVAL); + } + + data->read =3D kcalloc(sizeof(bool), config->max_register + 1, + GFP_KERNEL); + if (!data->read) + return ERR_PTR(-ENOMEM); + + data->written =3D kcalloc(sizeof(bool), config->max_register + 1, + GFP_KERNEL); + if (!data->written) + return ERR_PTR(-ENOMEM); + + map =3D __regmap_init(NULL, ®map_ram, data, config, + lock_key, lock_name); + + return map; +} +EXPORT_SYMBOL_GPL(__regmap_init_ram); + +MODULE_LICENSE("GPL v2"); --=20 2.34.1 From nobody Mon Apr 29 10:55:02 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0048AC6FD1C for ; Sat, 25 Mar 2023 00:28:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231387AbjCYA17 (ORCPT ); Fri, 24 Mar 2023 20:27:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231913AbjCYA1n (ORCPT ); Fri, 24 Mar 2023 20:27:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B6EC1CF60; Fri, 24 Mar 2023 17:27:34 -0700 (PDT) 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 209C1B82666; Sat, 25 Mar 2023 00:27:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 458AFC433D2; Sat, 25 Mar 2023 00:27:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679704051; bh=T6cf5XyWoPcJpUilixagzzUODTnbuLbKC4Chfoxm99U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mLbmhBCtifY79ELWaFx1f3kYhvaJ99H+vvp1iXfZE3Fv3XTHW3GWba+VIsxbx1U81 rnSBE2Y2nI1B1HYOQe2TgeL9uwMcD/c2zr/F8psMwFkIIEbG2NvNdY5FW0AJYMOZee mVJBosKjTKGqwill49CXQ5xsW8d0LIuT+yuo3RiBYqPdJ/4Q26vgCfokELMu/E4iDU jrB+PsQAMbmdNqQZaZWjHvO/vk2k8R4HCbf6XM36SWkDHjCXuY3+cAtSepND2S7sQt 4ol9Z74D9S+iKPN5qEZlgzyvi3+fjeeEsD2LuG9KEm+/YYefwRYjQCqoV0s9odgf4Q hX0ajT1dWg/Gg== From: Mark Brown Date: Sat, 25 Mar 2023 00:27:18 +0000 Subject: [PATCH 2/2] regmap: Add some basic kunit tests MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230324-regmap-kunit-v1-2-62ef9cfa9b89@kernel.org> References: <20230324-regmap-kunit-v1-0-62ef9cfa9b89@kernel.org> In-Reply-To: <20230324-regmap-kunit-v1-0-62ef9cfa9b89@kernel.org> To: David Gow , Brendan Higgins Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-2eb1a X-Developer-Signature: v=1; a=openpgp-sha256; l=20808; i=broonie@kernel.org; h=from:subject:message-id; bh=T6cf5XyWoPcJpUilixagzzUODTnbuLbKC4Chfoxm99U=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBkHj/snB/0qL4t8ePoQDZx7k/nr2gO9noaEEE4M7T7 tAQo1kiJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZB4/7AAKCRAk1otyXVSH0OOQB/ kB3T2NSYTiQVinX/RL45Q8hp2Tc+U9zn3Q0Q55MhYkLvl+nZ4vhk911pjx5ticIOZCIQPBBya8pV7g MXgVvTcO0W5OsGQVHMhHH3qsQ5RlvgraWk6NqclBypgXHHGRW4GbLUN4nFT9pUvqB4bHRWV9ahN5Cp WOi6DE7EsPN8qmoljy6ZiAdRe/75sMEkiOyDdvAzRBLpcgTngBj3jDwBH+3C8IkkuVNWfk7Q8E+cbN w9WFIwPafPd/T+qVY2WE2ECbEZVWYMtS124zRQb5eoPPxvTAyhmf9t+UZ5bJak5vv5+Ov47J8zx9j5 mnUMJ0cmg9cbvmEtFmFp/N2KZpe3ac X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On the theory that it's better to make a start let's add some KUnit tests for regmap. Currently this is a bit of a mess but it passes and hopefully will at some point help catch problems. We provide very basic cover for most of the core functionality that operates at the register level, repeating each test for each cache type in order to exercise the caches. There is no coverage of anything to do with the bulk operations at the bus level or formatting for byte stream buses yet. Each test creates it's own regmap since the cache structures are built incrementally, meaning we gain coverage from the different access patterns, and some of the tests cover different init scenarios. Signed-off-by: Mark Brown --- drivers/base/regmap/Kconfig | 7 + drivers/base/regmap/Makefile | 1 + drivers/base/regmap/regmap-kunit.c | 631 +++++++++++++++++++++++++++++++++= ++++ 3 files changed, 639 insertions(+) diff --git a/drivers/base/regmap/Kconfig b/drivers/base/regmap/Kconfig index 65ce888d7c04..6e77bf96569c 100644 --- a/drivers/base/regmap/Kconfig +++ b/drivers/base/regmap/Kconfig @@ -14,6 +14,13 @@ config REGCACHE_COMPRESSED select LZO_DECOMPRESS bool =20 +config REGMAP_KUNIT + tristate "KUnit tests for regmap" + depends on KUNIT + default KUNIT_ALL_TESTS + select REGMAP + select REGMAP_RAM + config REGMAP_AC97 tristate =20 diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile index 5ef6f129497c..fe2775fc76c6 100644 --- a/drivers/base/regmap/Makefile +++ b/drivers/base/regmap/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_REGMAP) +=3D regmap.o regcache.o obj-$(CONFIG_REGMAP) +=3D regcache-rbtree.o regcache-flat.o obj-$(CONFIG_REGCACHE_COMPRESSED) +=3D regcache-lzo.o obj-$(CONFIG_DEBUG_FS) +=3D regmap-debugfs.o +obj-$(CONFIG_REGMAP_KUNIT) +=3D regmap-kunit.o obj-$(CONFIG_REGMAP_AC97) +=3D regmap-ac97.o obj-$(CONFIG_REGMAP_I2C) +=3D regmap-i2c.o obj-$(CONFIG_REGMAP_RAM) +=3D regmap-ram.o diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regma= p-kunit.c new file mode 100644 index 000000000000..137681f12bc3 --- /dev/null +++ b/drivers/base/regmap/regmap-kunit.c @@ -0,0 +1,631 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// regmap KUnit tests +// +// Copyright 2023 Arm Ltd + +#include +#include "internal.h" + +#define BLOCK_TEST_SIZE 12 + +static const struct regmap_config test_regmap_config =3D { + .max_register =3D BLOCK_TEST_SIZE, + .reg_stride =3D 1, + .val_bits =3D sizeof(unsigned int) * 8, +}; + +struct regcache_types { + enum regcache_type type; + const char *name; +}; + +static void case_to_desc(const struct regcache_types *t, char *desc) +{ + strcpy(desc, t->name); +} + +static const struct regcache_types regcache_types_list[] =3D { + { REGCACHE_NONE, "none" }, + { REGCACHE_FLAT, "flat" }, + { REGCACHE_RBTREE, "rbtree" }, +}; + +KUNIT_ARRAY_PARAM(regcache_types, regcache_types_list, case_to_desc); + +static const struct regcache_types real_cache_types_list[] =3D { + { REGCACHE_FLAT, "flat" }, + { REGCACHE_RBTREE, "rbtree" }, +}; + +KUNIT_ARRAY_PARAM(real_cache_types, real_cache_types_list, case_to_desc); + +static struct regmap *gen_regmap(struct regmap_config *config, + struct regmap_ram_data **data) +{ + unsigned int *buf; + struct regmap *ret; + size_t size =3D (config->max_register + 1) * sizeof(unsigned int); + int i; + struct reg_default *defaults; + + buf =3D kmalloc(size, GFP_KERNEL); + if (!buf) + return ERR_PTR(-ENOMEM); + + get_random_bytes(buf, size); + + *data =3D kzalloc(sizeof(**data), GFP_KERNEL); + if (!(*data)) + return ERR_PTR(-ENOMEM); + (*data)->vals =3D buf; + + if (config->num_reg_defaults) { + defaults =3D kcalloc(config->num_reg_defaults, + sizeof(struct reg_default), + GFP_KERNEL); + if (!defaults) + return ERR_PTR(-ENOMEM); + config->reg_defaults =3D defaults; + + for (i =3D 0; i < config->num_reg_defaults; i++) { + defaults[i].reg =3D i * config->reg_stride; + defaults[i].def =3D buf[i * config->reg_stride]; + } + } + + ret =3D regmap_init_ram(config, *data); + if (IS_ERR(ret)) { + kfree(buf); + kfree(*data); + } + + return ret; +} + +static void basic_read_write(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val, rval; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + get_random_bytes(&val, sizeof(val)); + + /* If we write a value to a register we can read it back */ + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 0, val)); + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 0, &rval)); + KUNIT_EXPECT_EQ(test, val, rval); + + /* If using a cache the cache satisfied the read */ + KUNIT_EXPECT_EQ(test, t->type =3D=3D REGCACHE_NONE, data->read[0]); + + regmap_exit(map); +} + +static void bulk_write(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val[BLOCK_TEST_SIZE], rval[BLOCK_TEST_SIZE]; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + get_random_bytes(&val, sizeof(val)); + + /* + * Data written via the bulk API can be read back with single + * reads. + */ + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_write(map, 0, val, + BLOCK_TEST_SIZE)); + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, i, &rval[i])); + + KUNIT_EXPECT_MEMEQ(test, val, rval, sizeof(val)); + + /* If using a cache the cache satisfied the read */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, t->type =3D=3D REGCACHE_NONE, data->read[i]); + + regmap_exit(map); +} + +static void bulk_read(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val[BLOCK_TEST_SIZE], rval[BLOCK_TEST_SIZE]; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + get_random_bytes(&val, sizeof(val)); + + /* Data written as single writes can be read via the bulk API */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, i, val[i])); + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_read(map, 0, rval, + BLOCK_TEST_SIZE)); + KUNIT_EXPECT_MEMEQ(test, val, rval, sizeof(val)); + + /* If using a cache the cache satisfied the read */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, t->type =3D=3D REGCACHE_NONE, data->read[i]); + + regmap_exit(map); +} + +static void reg_defaults(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int rval[BLOCK_TEST_SIZE]; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.num_reg_defaults =3D BLOCK_TEST_SIZE; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + /* Read back the expected default data */ + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_read(map, 0, rval, + BLOCK_TEST_SIZE)); + KUNIT_EXPECT_MEMEQ(test, data->vals, rval, sizeof(rval)); + + /* The data should have been read from cache if there was one */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, t->type =3D=3D REGCACHE_NONE, data->read[i]); +} + +static void reg_defaults_read_dev(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int rval[BLOCK_TEST_SIZE]; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.num_reg_defaults_raw =3D BLOCK_TEST_SIZE; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + /* We should have read the cache defaults back from the map */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) { + KUNIT_EXPECT_EQ(test, t->type !=3D REGCACHE_NONE, data->read[i]); + data->read[i] =3D false; + } + + /* Read back the expected default data */ + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_read(map, 0, rval, + BLOCK_TEST_SIZE)); + KUNIT_EXPECT_MEMEQ(test, data->vals, rval, sizeof(rval)); + + /* The data should have been read from cache if there was one */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, t->type =3D=3D REGCACHE_NONE, data->read[i]); +} + +static void register_patch(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + struct reg_sequence patch[2]; + unsigned int rval[BLOCK_TEST_SIZE]; + int i; + + /* We need defaults so readback works */ + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.num_reg_defaults =3D BLOCK_TEST_SIZE; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + /* Stash the original values */ + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_read(map, 0, rval, + BLOCK_TEST_SIZE)); + + /* Patch a couple of values */ + patch[0].reg =3D 2; + patch[0].def =3D rval[2] + 1; + patch[0].delay_us =3D 0; + patch[1].reg =3D 5; + patch[1].def =3D rval[5] + 1; + patch[1].delay_us =3D 0; + KUNIT_EXPECT_EQ(test, 0, regmap_register_patch(map, patch, + ARRAY_SIZE(patch))); + + /* Only the patched registers are written */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) { + switch (i) { + case 2: + case 5: + KUNIT_EXPECT_TRUE(test, data->written[i]); + KUNIT_EXPECT_EQ(test, data->vals[i], rval[i] + 1); + break; + default: + KUNIT_EXPECT_FALSE(test, data->written[i]); + KUNIT_EXPECT_EQ(test, data->vals[i], rval[i]); + break; + } + } + + regmap_exit(map); +} + +static void stride(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int rval; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.reg_stride =3D 2; + config.num_reg_defaults =3D BLOCK_TEST_SIZE / 2; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + /* Only even registers can be accessed, try both read and write */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) { + data->read[i] =3D false; + data->written[i] =3D false; + + if (i % 2) { + KUNIT_EXPECT_NE(test, 0, regmap_read(map, i, &rval)); + KUNIT_EXPECT_NE(test, 0, regmap_write(map, i, rval)); + KUNIT_EXPECT_FALSE(test, data->read[i]); + KUNIT_EXPECT_FALSE(test, data->written[i]); + } else { + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, i, &rval)); + KUNIT_EXPECT_EQ(test, data->vals[i], rval); + KUNIT_EXPECT_EQ(test, t->type =3D=3D REGCACHE_NONE, + data->read[i]); + + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, i, rval)); + KUNIT_EXPECT_TRUE(test, data->written[i]); + } + } + + regmap_exit(map); +} + +static struct regmap_range_cfg test_range =3D { + .selector_reg =3D 1, + .selector_mask =3D 0xff, + + .window_start =3D 4, + .window_len =3D 10, + + .range_min =3D 20, + .range_max =3D 40, +}; + +static bool test_range_volatile(struct device *dev, unsigned int reg) +{ + if (reg >=3D test_range.window_start && + reg <=3D test_range.selector_reg + test_range.window_len) + return true; + + if (reg >=3D test_range.range_min && reg <=3D test_range.range_max) + return true; + + return false; +} + +static void basic_ranges(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.volatile_reg =3D test_range_volatile; + config.ranges =3D &test_range; + config.num_ranges =3D 1; + config.max_register =3D test_range.range_max; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + for (i =3D test_range.range_min; i < test_range.range_max; i++) { + data->read[i] =3D false; + data->written[i] =3D false; + } + + /* Reset the page to a non-zero value to trigger a change */ + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, test_range.selector_reg, + test_range.range_max)); + + /* Check we set the page and use the window for writes */ + data->written[test_range.selector_reg] =3D false; + data->written[test_range.window_start] =3D false; + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, test_range.range_min, 0)); + KUNIT_EXPECT_TRUE(test, data->written[test_range.selector_reg]); + KUNIT_EXPECT_TRUE(test, data->written[test_range.window_start]); + + data->written[test_range.selector_reg] =3D false; + data->written[test_range.window_start] =3D false; + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, + test_range.range_min + + test_range.window_len, + 0)); + KUNIT_EXPECT_TRUE(test, data->written[test_range.selector_reg]); + KUNIT_EXPECT_TRUE(test, data->written[test_range.window_start]); + + /* Same for reads */ + data->written[test_range.selector_reg] =3D false; + data->read[test_range.window_start] =3D false; + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, test_range.range_min, &val)); + KUNIT_EXPECT_TRUE(test, data->written[test_range.selector_reg]); + KUNIT_EXPECT_TRUE(test, data->read[test_range.window_start]); + + data->written[test_range.selector_reg] =3D false; + data->read[test_range.window_start] =3D false; + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, + test_range.range_min + + test_range.window_len, + &val)); + KUNIT_EXPECT_TRUE(test, data->written[test_range.selector_reg]); + KUNIT_EXPECT_TRUE(test, data->read[test_range.window_start]); + + /* No physical access triggered in the virtual range */ + for (i =3D test_range.range_min; i < test_range.range_max; i++) { + KUNIT_EXPECT_FALSE(test, data->read[i]); + KUNIT_EXPECT_FALSE(test, data->written[i]); + } + + regmap_exit(map); +} + +static void cache_bypass(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val, rval; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + get_random_bytes(&val, sizeof(val)); + + /* Ensure the cache has a value in it */ + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 0, val)); + + /* Bypass then write a different value */ + regcache_cache_bypass(map, true); + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 0, val + 1)); + + /* Read the bypassed value */ + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 0, &rval)); + KUNIT_EXPECT_EQ(test, val + 1, rval); + KUNIT_EXPECT_EQ(test, data->vals[0], rval); + + /* Disable bypass, the cache should still return the original value */ + regcache_cache_bypass(map, false); + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, 0, &rval)); + KUNIT_EXPECT_EQ(test, val, rval); + + regmap_exit(map); +} + +static void cache_sync(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val[BLOCK_TEST_SIZE]; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + get_random_bytes(&val, sizeof(val)); + + /* Put some data into the cache */ + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_write(map, 0, val, + BLOCK_TEST_SIZE)); + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + data->written[i] =3D false; + + /* Trash the data on the device itself then resync */ + regcache_mark_dirty(map); + memset(data->vals, 0, sizeof(val)); + KUNIT_EXPECT_EQ(test, 0, regcache_sync(map)); + + /* Did we just write the correct data out? */ + KUNIT_EXPECT_MEMEQ(test, data->vals, val, sizeof(val)); + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, true, data->written[i]); + + regmap_exit(map); +} + +static void cache_sync_defaults(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + unsigned int val; + int i; + + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.num_reg_defaults =3D BLOCK_TEST_SIZE; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + get_random_bytes(&val, sizeof(val)); + + /* Change the value of one register */ + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 2, val)); + + /* Resync */ + regcache_mark_dirty(map); + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + data->written[i] =3D false; + KUNIT_EXPECT_EQ(test, 0, regcache_sync(map)); + + /* Did we just sync the one register we touched? */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + KUNIT_EXPECT_EQ(test, i =3D=3D 2, data->written[i]); + + regmap_exit(map); +} + +static void cache_sync_patch(struct kunit *test) +{ + struct regcache_types *t =3D (struct regcache_types *)test->param_value; + struct regmap *map; + struct regmap_config config; + struct regmap_ram_data *data; + struct reg_sequence patch[2]; + unsigned int rval[BLOCK_TEST_SIZE], val; + int i; + + /* We need defaults so readback works */ + config =3D test_regmap_config; + config.cache_type =3D t->type; + config.num_reg_defaults =3D BLOCK_TEST_SIZE; + + map =3D gen_regmap(&config, &data); + KUNIT_ASSERT_FALSE(test, IS_ERR(map)); + if (IS_ERR(map)) + return; + + /* Stash the original values */ + KUNIT_EXPECT_EQ(test, 0, regmap_bulk_read(map, 0, rval, + BLOCK_TEST_SIZE)); + + /* Patch a couple of values */ + patch[0].reg =3D 2; + patch[0].def =3D rval[2] + 1; + patch[0].delay_us =3D 0; + patch[1].reg =3D 5; + patch[1].def =3D rval[5] + 1; + patch[1].delay_us =3D 0; + KUNIT_EXPECT_EQ(test, 0, regmap_register_patch(map, patch, + ARRAY_SIZE(patch))); + + /* Sync the cache */ + regcache_mark_dirty(map); + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) + data->written[i] =3D false; + KUNIT_EXPECT_EQ(test, 0, regcache_sync(map)); + + /* The patch should be on the device but not in the cache */ + for (i =3D 0; i < BLOCK_TEST_SIZE; i++) { + KUNIT_EXPECT_EQ(test, 0, regmap_read(map, i, &val)); + KUNIT_EXPECT_EQ(test, val, rval[i]); + + switch (i) { + case 2: + case 5: + KUNIT_EXPECT_EQ(test, true, data->written[i]); + KUNIT_EXPECT_EQ(test, data->vals[i], rval[i] + 1); + break; + default: + KUNIT_EXPECT_EQ(test, false, data->written[i]); + KUNIT_EXPECT_EQ(test, data->vals[i], rval[i]); + break; + } + } + + regmap_exit(map); +} + +static struct kunit_case regmap_test_cases[] =3D { + KUNIT_CASE_PARAM(basic_read_write, regcache_types_gen_params), + KUNIT_CASE_PARAM(bulk_write, regcache_types_gen_params), + KUNIT_CASE_PARAM(bulk_read, regcache_types_gen_params), + KUNIT_CASE_PARAM(reg_defaults, regcache_types_gen_params), + KUNIT_CASE_PARAM(reg_defaults_read_dev, regcache_types_gen_params), + KUNIT_CASE_PARAM(register_patch, regcache_types_gen_params), + KUNIT_CASE_PARAM(stride, regcache_types_gen_params), + KUNIT_CASE_PARAM(basic_ranges, regcache_types_gen_params), + KUNIT_CASE_PARAM(cache_bypass, real_cache_types_gen_params), + KUNIT_CASE_PARAM(cache_sync, real_cache_types_gen_params), + KUNIT_CASE_PARAM(cache_sync_defaults, real_cache_types_gen_params), + KUNIT_CASE_PARAM(cache_sync_patch, real_cache_types_gen_params), + {} +}; + +static struct kunit_suite regmap_test_suite =3D { + .name =3D "regmap", + .test_cases =3D regmap_test_cases, +}; +kunit_test_suite(regmap_test_suite); + +MODULE_LICENSE("GPL v2"); --=20 2.34.1