xref: /openbmc/u-boot/include/dm/test.h (revision 4a34e4b8)
1 /*
2  * Copyright (c) 2013 Google, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #ifndef __DM_TEST_H
8 #define __DM_TEST_H
9 
10 #include <dm.h>
11 #include <malloc.h>
12 
13 /**
14  * struct dm_test_cdata - configuration data for test instance
15  *
16  * @ping_add: Amonut to add each time we get a ping
17  * @base: Base address of this device
18  */
19 struct dm_test_pdata {
20 	int ping_add;
21 	uint32_t base;
22 };
23 
24 /**
25  * struct test_ops - Operations supported by the test device
26  *
27  * @ping: Ping operation
28  *	@dev: Device to operate on
29  *	@pingval: Value to ping the device with
30  *	@pingret: Returns resulting value from driver
31  *	@return 0 if OK, -ve on error
32  */
33 struct test_ops {
34 	int (*ping)(struct udevice *dev, int pingval, int *pingret);
35 };
36 
37 /* Operations that our test driver supports */
38 enum {
39 	DM_TEST_OP_BIND = 0,
40 	DM_TEST_OP_UNBIND,
41 	DM_TEST_OP_PROBE,
42 	DM_TEST_OP_REMOVE,
43 
44 	/* For uclass */
45 	DM_TEST_OP_POST_BIND,
46 	DM_TEST_OP_PRE_UNBIND,
47 	DM_TEST_OP_PRE_PROBE,
48 	DM_TEST_OP_POST_PROBE,
49 	DM_TEST_OP_PRE_REMOVE,
50 	DM_TEST_OP_INIT,
51 	DM_TEST_OP_DESTROY,
52 
53 	DM_TEST_OP_COUNT,
54 };
55 
56 /* Test driver types */
57 enum {
58 	DM_TEST_TYPE_FIRST = 0,
59 	DM_TEST_TYPE_SECOND,
60 };
61 
62 /* The number added to the ping total on each probe */
63 #define DM_TEST_START_TOTAL	5
64 
65 /**
66  * struct dm_test_priv - private data for the test devices
67  */
68 struct dm_test_priv {
69 	int ping_total;
70 	int op_count[DM_TEST_OP_COUNT];
71 	int uclass_flag;
72 	int uclass_total;
73 };
74 
75 /**
76  * struct dm_test_perdev_class_priv - private per-device data for test uclass
77  */
78 struct dm_test_uclass_perdev_priv {
79 	int base_add;
80 };
81 
82 /**
83  * struct dm_test_uclass_priv - private data for test uclass
84  */
85 struct dm_test_uclass_priv {
86 	int total_add;
87 };
88 
89 /**
90  * struct dm_test_parent_data - parent's information on each child
91  *
92  * @sum: Test value used to check parent data works correctly
93  * @flag: Used to track calling of parent operations
94  * @uclass_flag: Used to track calling of parent operations by uclass
95  */
96 struct dm_test_parent_data {
97 	int sum;
98 	int flag;
99 };
100 
101 /* Test values for test device's uclass platform data */
102 enum {
103 	TEST_UC_PDATA_INTVAL1 = 2,
104 	TEST_UC_PDATA_INTVAL2 = 334,
105 	TEST_UC_PDATA_INTVAL3 = 789452,
106 };
107 
108 /**
109  * struct dm_test_uclass_platda - uclass's information on each device
110  *
111  * @intval1: set to TEST_UC_PDATA_INTVAL1 in .post_bind method of test uclass
112  * @intval2: set to TEST_UC_PDATA_INTVAL2 in .post_bind method of test uclass
113  * @intval3: set to TEST_UC_PDATA_INTVAL3 in .post_bind method of test uclass
114  */
115 struct dm_test_perdev_uc_pdata {
116 	int intval1;
117 	int intval2;
118 	int intval3;
119 };
120 
121 /*
122  * Operation counts for the test driver, used to check that each method is
123  * called correctly
124  */
125 extern int dm_testdrv_op_count[DM_TEST_OP_COUNT];
126 
127 extern struct dm_test_state global_test_state;
128 
129 /*
130  * struct dm_test_state - Entire state of dm test system
131  *
132  * This is often abreviated to dms.
133  *
134  * @root: Root device
135  * @testdev: Test device
136  * @fail_count: Number of tests that failed
137  * @force_fail_alloc: Force all memory allocs to fail
138  * @skip_post_probe: Skip uclass post-probe processing
139  * @removed: Used to keep track of a device that was removed
140  */
141 struct dm_test_state {
142 	struct udevice *root;
143 	struct udevice *testdev;
144 	int fail_count;
145 	int force_fail_alloc;
146 	int skip_post_probe;
147 	struct udevice *removed;
148 	struct mallinfo start;
149 };
150 
151 /* Test flags for each test */
152 enum {
153 	DM_TESTF_SCAN_PDATA	= 1 << 0,	/* test needs platform data */
154 	DM_TESTF_PROBE_TEST	= 1 << 1,	/* probe test uclass */
155 	DM_TESTF_SCAN_FDT	= 1 << 2,	/* scan device tree */
156 };
157 
158 /**
159  * struct dm_test - Information about a driver model test
160  *
161  * @name: Name of test
162  * @func: Function to call to perform test
163  * @flags: Flags indicated pre-conditions for test
164  */
165 struct dm_test {
166 	const char *name;
167 	int (*func)(struct dm_test_state *dms);
168 	int flags;
169 };
170 
171 /* Declare a new driver model test */
172 #define DM_TEST(_name, _flags)						\
173 	ll_entry_declare(struct dm_test, _name, dm_test) = {		\
174 		.name = #_name,						\
175 		.flags = _flags,					\
176 		.func = _name,						\
177 	}
178 
179 /* Declare ping methods for the drivers */
180 int test_ping(struct udevice *dev, int pingval, int *pingret);
181 int testfdt_ping(struct udevice *dev, int pingval, int *pingret);
182 
183 /**
184  * dm_check_operations() - Check that we can perform ping operations
185  *
186  * This checks that the ping operations work as expected for a device
187  *
188  * @dms: Overall test state
189  * @dev: Device to test
190  * @base: Base address, used to check ping return value
191  * @priv: Pointer to private test information
192  * @return 0 if OK, -ve on error
193  */
194 int dm_check_operations(struct dm_test_state *dms, struct udevice *dev,
195 			uint32_t base, struct dm_test_priv *priv);
196 
197 /**
198  * dm_check_devices() - check the devices respond to operations correctly
199  *
200  * @dms: Overall test state
201  * @num_devices: Number of test devices to check
202  * @return 0 if OK, -ve on error
203  */
204 int dm_check_devices(struct dm_test_state *dms, int num_devices);
205 
206 /**
207  * dm_leak_check_start() - Prepare to check for a memory leak
208  *
209  * Call this before allocating memory to record the amount of memory being
210  * used.
211  *
212  * @dms: Overall test state
213  */
214 void dm_leak_check_start(struct dm_test_state *dms);
215 
216 /**
217  * dm_leak_check_end() - Check that no memory has leaked
218  *
219  * Call this after dm_leak_check_start() and after you have hopefuilly freed
220  * all the memory that was allocated. This function will print an error if
221  * it sees a different amount of total memory allocated than before.
222  *
223  * @dms: Overall test state
224  */int dm_leak_check_end(struct dm_test_state *dms);
225 
226 
227 /**
228  * dm_test_main() - Run all or one of the tests
229  *
230  * This runs all available driver model tests, or a selected one
231  *
232  * @test_name:	Name of test to run, or NULL for all
233  * @return 0 if OK, -ve on error
234  */
235 int dm_test_main(const char *test_name);
236 
237 #endif
238