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