blob: 4ffaf6089eeaf48c7de77e5c2d99e9e0a4786b8c [file] [log] [blame]
Kyle Swenson7d38e032023-07-10 11:16:56 -06001/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2/*
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 */
5
6#ifndef _ZINC_SELFTEST_RUN_H
7#define _ZINC_SELFTEST_RUN_H
8
9#include <linux/kernel.h>
10#include <linux/printk.h>
11#include <linux/bug.h>
12
13static inline bool selftest_run(const char *name, bool (*selftest)(void),
14 bool *const nobs[], unsigned int nobs_len)
15{
16 unsigned long set = 0, subset = 0, largest_subset = 0;
17 unsigned int i;
18
19 BUILD_BUG_ON(!__builtin_constant_p(nobs_len) ||
20 nobs_len >= BITS_PER_LONG);
21
22 if (!IS_ENABLED(CONFIG_ZINC_SELFTEST))
23 return true;
24
25 for (i = 0; i < nobs_len; ++i)
26 set |= ((unsigned long)*nobs[i]) << i;
27
28 do {
29 for (i = 0; i < nobs_len; ++i)
30 *nobs[i] = BIT(i) & subset;
31 if (selftest())
32 largest_subset = max(subset, largest_subset);
33 else
34 pr_err("%s self-test combination 0x%lx: FAIL\n", name,
35 subset);
36 subset = (subset - set) & set;
37 } while (subset);
38
39 for (i = 0; i < nobs_len; ++i)
40 *nobs[i] = BIT(i) & largest_subset;
41
42 if (largest_subset == set)
43 pr_info("%s self-tests: pass\n", name);
44
45 return !WARN_ON(largest_subset != set);
46}
47
48#endif