Skip to content

Commit f63eb9a

Browse files
rfvirgilbroonie
authored andcommitted
regmap: kunit: Add test cases for regmap_read_bypassed()
This adds test cases to prove that regmap_read_bypassed() reads the hardware value while the regmap is in cache-only. Signed-off-by: Richard Fitzgerald <[email protected]> Link: https://msgid.link/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 468d277 commit f63eb9a

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

drivers/base/regmap/regmap-kunit.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,135 @@ static void bulk_read(struct kunit *test)
295295
KUNIT_EXPECT_EQ(test, config.cache_type == REGCACHE_NONE, data->read[i]);
296296
}
297297

298+
static void read_bypassed(struct kunit *test)
299+
{
300+
const struct regmap_test_param *param = test->param_value;
301+
struct regmap *map;
302+
struct regmap_config config;
303+
struct regmap_ram_data *data;
304+
unsigned int val[BLOCK_TEST_SIZE], rval;
305+
int i;
306+
307+
config = test_regmap_config;
308+
309+
map = gen_regmap(test, &config, &data);
310+
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
311+
if (IS_ERR(map))
312+
return;
313+
314+
KUNIT_EXPECT_FALSE(test, map->cache_bypass);
315+
316+
get_random_bytes(&val, sizeof(val));
317+
318+
/* Write some test values */
319+
KUNIT_EXPECT_EQ(test, 0, regmap_bulk_write(map, param->from_reg, val, ARRAY_SIZE(val)));
320+
321+
regcache_cache_only(map, true);
322+
323+
/*
324+
* While in cache-only regmap_read_bypassed() should return the register
325+
* value and leave the map in cache-only.
326+
*/
327+
for (i = 0; i < ARRAY_SIZE(val); i++) {
328+
/* Put inverted bits in rval to prove we really read the value */
329+
rval = ~val[i];
330+
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, param->from_reg + i, &rval));
331+
KUNIT_EXPECT_EQ(test, val[i], rval);
332+
333+
rval = ~val[i];
334+
KUNIT_EXPECT_EQ(test, 0, regmap_read_bypassed(map, param->from_reg + i, &rval));
335+
KUNIT_EXPECT_EQ(test, val[i], rval);
336+
KUNIT_EXPECT_TRUE(test, map->cache_only);
337+
KUNIT_EXPECT_FALSE(test, map->cache_bypass);
338+
}
339+
340+
/*
341+
* Change the underlying register values to prove it is returning
342+
* real values not cached values.
343+
*/
344+
for (i = 0; i < ARRAY_SIZE(val); i++) {
345+
val[i] = ~val[i];
346+
data->vals[param->from_reg + i] = val[i];
347+
}
348+
349+
for (i = 0; i < ARRAY_SIZE(val); i++) {
350+
rval = ~val[i];
351+
KUNIT_EXPECT_EQ(test, 0, regmap_read(map, param->from_reg + i, &rval));
352+
KUNIT_EXPECT_NE(test, val[i], rval);
353+
354+
rval = ~val[i];
355+
KUNIT_EXPECT_EQ(test, 0, regmap_read_bypassed(map, param->from_reg + i, &rval));
356+
KUNIT_EXPECT_EQ(test, val[i], rval);
357+
KUNIT_EXPECT_TRUE(test, map->cache_only);
358+
KUNIT_EXPECT_FALSE(test, map->cache_bypass);
359+
}
360+
}
361+
362+
static void read_bypassed_volatile(struct kunit *test)
363+
{
364+
const struct regmap_test_param *param = test->param_value;
365+
struct regmap *map;
366+
struct regmap_config config;
367+
struct regmap_ram_data *data;
368+
unsigned int val[BLOCK_TEST_SIZE], rval;
369+
int i;
370+
371+
config = test_regmap_config;
372+
/* All registers except #5 volatile */
373+
config.volatile_reg = reg_5_false;
374+
375+
map = gen_regmap(test, &config, &data);
376+
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
377+
if (IS_ERR(map))
378+
return;
379+
380+
KUNIT_EXPECT_FALSE(test, map->cache_bypass);
381+
382+
get_random_bytes(&val, sizeof(val));
383+
384+
/* Write some test values */
385+
KUNIT_EXPECT_EQ(test, 0, regmap_bulk_write(map, param->from_reg, val, ARRAY_SIZE(val)));
386+
387+
regcache_cache_only(map, true);
388+
389+
/*
390+
* While in cache-only regmap_read_bypassed() should return the register
391+
* value and leave the map in cache-only.
392+
*/
393+
for (i = 0; i < ARRAY_SIZE(val); i++) {
394+
/* Register #5 is non-volatile so should read from cache */
395+
KUNIT_EXPECT_EQ(test, (i == 5) ? 0 : -EBUSY,
396+
regmap_read(map, param->from_reg + i, &rval));
397+
398+
/* Put inverted bits in rval to prove we really read the value */
399+
rval = ~val[i];
400+
KUNIT_EXPECT_EQ(test, 0, regmap_read_bypassed(map, param->from_reg + i, &rval));
401+
KUNIT_EXPECT_EQ(test, val[i], rval);
402+
KUNIT_EXPECT_TRUE(test, map->cache_only);
403+
KUNIT_EXPECT_FALSE(test, map->cache_bypass);
404+
}
405+
406+
/*
407+
* Change the underlying register values to prove it is returning
408+
* real values not cached values.
409+
*/
410+
for (i = 0; i < ARRAY_SIZE(val); i++) {
411+
val[i] = ~val[i];
412+
data->vals[param->from_reg + i] = val[i];
413+
}
414+
415+
for (i = 0; i < ARRAY_SIZE(val); i++) {
416+
if (i == 5)
417+
continue;
418+
419+
rval = ~val[i];
420+
KUNIT_EXPECT_EQ(test, 0, regmap_read_bypassed(map, param->from_reg + i, &rval));
421+
KUNIT_EXPECT_EQ(test, val[i], rval);
422+
KUNIT_EXPECT_TRUE(test, map->cache_only);
423+
KUNIT_EXPECT_FALSE(test, map->cache_bypass);
424+
}
425+
}
426+
298427
static void write_readonly(struct kunit *test)
299428
{
300429
struct regmap *map;
@@ -1747,6 +1876,8 @@ static void raw_ranges(struct kunit *test)
17471876

17481877
static struct kunit_case regmap_test_cases[] = {
17491878
KUNIT_CASE_PARAM(basic_read_write, regcache_types_gen_params),
1879+
KUNIT_CASE_PARAM(read_bypassed, real_cache_types_gen_params),
1880+
KUNIT_CASE_PARAM(read_bypassed_volatile, real_cache_types_gen_params),
17501881
KUNIT_CASE_PARAM(bulk_write, regcache_types_gen_params),
17511882
KUNIT_CASE_PARAM(bulk_read, regcache_types_gen_params),
17521883
KUNIT_CASE_PARAM(write_readonly, regcache_types_gen_params),

0 commit comments

Comments
 (0)