test.h 5.0 KB

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