rproc-uclass.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015
  4. * Texas Instruments Incorporated - http://www.ti.com/
  5. */
  6. #define pr_fmt(fmt) "%s: " fmt, __func__
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <malloc.h>
  11. #include <remoteproc.h>
  12. #include <asm/io.h>
  13. #include <dm/device-internal.h>
  14. #include <dm.h>
  15. #include <dm/uclass.h>
  16. #include <dm/uclass-internal.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /**
  19. * for_each_remoteproc_device() - iterate through the list of rproc devices
  20. * @fn: check function to call per match, if this function returns fail,
  21. * iteration is aborted with the resultant error value
  22. * @skip_dev: Device to skip calling the callback about.
  23. * @data: Data to pass to the callback function
  24. *
  25. * Return: 0 if none of the callback returned a non 0 result, else returns the
  26. * result from the callback function
  27. */
  28. static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
  29. struct dm_rproc_uclass_pdata *uc_pdata,
  30. const void *data),
  31. struct udevice *skip_dev,
  32. const void *data)
  33. {
  34. struct udevice *dev;
  35. struct dm_rproc_uclass_pdata *uc_pdata;
  36. int ret;
  37. for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
  38. ret = uclass_find_next_device(&dev)) {
  39. if (ret || dev == skip_dev)
  40. continue;
  41. uc_pdata = dev_get_uclass_platdata(dev);
  42. ret = fn(dev, uc_pdata, data);
  43. if (ret)
  44. return ret;
  45. }
  46. return 0;
  47. }
  48. /**
  49. * _rproc_name_is_unique() - iteration helper to check if rproc name is unique
  50. * @dev: device that we are checking name for
  51. * @uc_pdata: uclass platform data
  52. * @data: compare data (this is the name we want to ensure is unique)
  53. *
  54. * Return: 0 is there is no match(is unique); if there is a match(we dont
  55. * have a unique name), return -EINVAL.
  56. */
  57. static int _rproc_name_is_unique(struct udevice *dev,
  58. struct dm_rproc_uclass_pdata *uc_pdata,
  59. const void *data)
  60. {
  61. const char *check_name = data;
  62. /* devices not yet populated with data - so skip them */
  63. if (!uc_pdata->name || !check_name)
  64. return 0;
  65. /* Return 0 to search further if we dont match */
  66. if (strlen(uc_pdata->name) != strlen(check_name))
  67. return 0;
  68. if (!strcmp(uc_pdata->name, check_name))
  69. return -EINVAL;
  70. return 0;
  71. }
  72. /**
  73. * rproc_name_is_unique() - Check if the rproc name is unique
  74. * @check_dev: Device we are attempting to ensure is unique
  75. * @check_name: Name we are trying to ensure is unique.
  76. *
  77. * Return: true if we have a unique name, false if name is not unique.
  78. */
  79. static bool rproc_name_is_unique(struct udevice *check_dev,
  80. const char *check_name)
  81. {
  82. int ret;
  83. ret = for_each_remoteproc_device(_rproc_name_is_unique,
  84. check_dev, check_name);
  85. return ret ? false : true;
  86. }
  87. /**
  88. * rproc_pre_probe() - Pre probe accessor for the uclass
  89. * @dev: device for which we are preprobing
  90. *
  91. * Parses and fills up the uclass pdata for use as needed by core and
  92. * remote proc drivers.
  93. *
  94. * Return: 0 if all wernt ok, else appropriate error value.
  95. */
  96. static int rproc_pre_probe(struct udevice *dev)
  97. {
  98. struct dm_rproc_uclass_pdata *uc_pdata;
  99. const struct dm_rproc_ops *ops;
  100. uc_pdata = dev_get_uclass_platdata(dev);
  101. /* See if we need to populate via fdt */
  102. if (!dev->platdata) {
  103. #if CONFIG_IS_ENABLED(OF_CONTROL)
  104. int node = dev_of_offset(dev);
  105. const void *blob = gd->fdt_blob;
  106. bool tmp;
  107. if (!blob) {
  108. debug("'%s' no dt?\n", dev->name);
  109. return -EINVAL;
  110. }
  111. debug("'%s': using fdt\n", dev->name);
  112. uc_pdata->name = fdt_getprop(blob, node,
  113. "remoteproc-name", NULL);
  114. /* Default is internal memory mapped */
  115. uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
  116. tmp = fdtdec_get_bool(blob, node,
  117. "remoteproc-internal-memory-mapped");
  118. if (tmp)
  119. uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
  120. #else
  121. /* Nothing much we can do about this, can we? */
  122. return -EINVAL;
  123. #endif
  124. } else {
  125. struct dm_rproc_uclass_pdata *pdata = dev->platdata;
  126. debug("'%s': using legacy data\n", dev->name);
  127. if (pdata->name)
  128. uc_pdata->name = pdata->name;
  129. uc_pdata->mem_type = pdata->mem_type;
  130. uc_pdata->driver_plat_data = pdata->driver_plat_data;
  131. }
  132. /* Else try using device Name */
  133. if (!uc_pdata->name)
  134. uc_pdata->name = dev->name;
  135. if (!uc_pdata->name) {
  136. debug("Unnamed device!");
  137. return -EINVAL;
  138. }
  139. if (!rproc_name_is_unique(dev, uc_pdata->name)) {
  140. debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name);
  141. return -EINVAL;
  142. }
  143. ops = rproc_get_ops(dev);
  144. if (!ops) {
  145. debug("%s driver has no ops?\n", dev->name);
  146. return -EINVAL;
  147. }
  148. if (!ops->load || !ops->start) {
  149. debug("%s driver has missing mandatory ops?\n", dev->name);
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. /**
  155. * rproc_post_probe() - post probe accessor for the uclass
  156. * @dev: deivce we finished probing
  157. *
  158. * initiate init function after the probe is completed. This allows
  159. * the remote processor drivers to split up the initializations between
  160. * probe and init as needed.
  161. *
  162. * Return: if the remote proc driver has a init routine, invokes it and
  163. * hands over the return value. overall, 0 if all went well, else appropriate
  164. * error value.
  165. */
  166. static int rproc_post_probe(struct udevice *dev)
  167. {
  168. const struct dm_rproc_ops *ops;
  169. ops = rproc_get_ops(dev);
  170. if (!ops) {
  171. debug("%s driver has no ops?\n", dev->name);
  172. return -EINVAL;
  173. }
  174. if (ops->init)
  175. return ops->init(dev);
  176. return 0;
  177. }
  178. UCLASS_DRIVER(rproc) = {
  179. .id = UCLASS_REMOTEPROC,
  180. .name = "remoteproc",
  181. .flags = DM_UC_FLAG_SEQ_ALIAS,
  182. .pre_probe = rproc_pre_probe,
  183. .post_probe = rproc_post_probe,
  184. .per_device_platdata_auto_alloc_size =
  185. sizeof(struct dm_rproc_uclass_pdata),
  186. };
  187. /* Remoteproc subsystem access functions */
  188. /**
  189. * _rproc_probe_dev() - iteration helper to probe a rproc device
  190. * @dev: device to probe
  191. * @uc_pdata: uclass data allocated for the device
  192. * @data: unused
  193. *
  194. * Return: 0 if all ok, else appropriate error value.
  195. */
  196. static int _rproc_probe_dev(struct udevice *dev,
  197. struct dm_rproc_uclass_pdata *uc_pdata,
  198. const void *data)
  199. {
  200. int ret;
  201. ret = device_probe(dev);
  202. if (ret)
  203. debug("%s: Failed to initialize - %d\n", dev->name, ret);
  204. return ret;
  205. }
  206. /**
  207. * _rproc_dev_is_probed() - check if the device has been probed
  208. * @dev: device to check
  209. * @uc_pdata: unused
  210. * @data: unused
  211. *
  212. * Return: -EAGAIN if not probed else return 0
  213. */
  214. static int _rproc_dev_is_probed(struct udevice *dev,
  215. struct dm_rproc_uclass_pdata *uc_pdata,
  216. const void *data)
  217. {
  218. if (dev->flags & DM_FLAG_ACTIVATED)
  219. return 0;
  220. return -EAGAIN;
  221. }
  222. bool rproc_is_initialized(void)
  223. {
  224. int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL);
  225. return ret ? false : true;
  226. }
  227. int rproc_init(void)
  228. {
  229. int ret;
  230. if (rproc_is_initialized()) {
  231. debug("Already initialized\n");
  232. return -EINVAL;
  233. }
  234. ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL);
  235. return ret;
  236. }
  237. int rproc_dev_init(int id)
  238. {
  239. struct udevice *dev = NULL;
  240. int ret;
  241. ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
  242. if (ret) {
  243. debug("Unknown remote processor id '%d' requested(%d)\n",
  244. id, ret);
  245. return ret;
  246. }
  247. ret = device_probe(dev);
  248. if (ret)
  249. debug("%s: Failed to initialize - %d\n", dev->name, ret);
  250. return ret;
  251. }
  252. int rproc_load(int id, ulong addr, ulong size)
  253. {
  254. struct udevice *dev = NULL;
  255. struct dm_rproc_uclass_pdata *uc_pdata;
  256. const struct dm_rproc_ops *ops;
  257. int ret;
  258. ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
  259. if (ret) {
  260. debug("Unknown remote processor id '%d' requested(%d)\n",
  261. id, ret);
  262. return ret;
  263. }
  264. uc_pdata = dev_get_uclass_platdata(dev);
  265. ops = rproc_get_ops(dev);
  266. if (!ops) {
  267. debug("%s driver has no ops?\n", dev->name);
  268. return -EINVAL;
  269. }
  270. debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n",
  271. uc_pdata->name, addr, size);
  272. if (ops->load)
  273. return ops->load(dev, addr, size);
  274. debug("%s: data corruption?? mandatory function is missing!\n",
  275. dev->name);
  276. return -EINVAL;
  277. };
  278. /*
  279. * Completely internal helper enums..
  280. * Keeping this isolated helps this code evolve independent of other
  281. * parts..
  282. */
  283. enum rproc_ops {
  284. RPROC_START,
  285. RPROC_STOP,
  286. RPROC_RESET,
  287. RPROC_PING,
  288. RPROC_RUNNING,
  289. };
  290. /**
  291. * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback
  292. * @id: id of the remote processor
  293. * @op: one of rproc_ops that indicate what operation to invoke
  294. *
  295. * Most of the checks and verification for remoteproc operations are more
  296. * or less same for almost all operations. This allows us to put a wrapper
  297. * and use the common checks to allow the driver to function appropriately.
  298. *
  299. * Return: 0 if all ok, else appropriate error value.
  300. */
  301. static int _rproc_ops_wrapper(int id, enum rproc_ops op)
  302. {
  303. struct udevice *dev = NULL;
  304. struct dm_rproc_uclass_pdata *uc_pdata;
  305. const struct dm_rproc_ops *ops;
  306. int (*fn)(struct udevice *dev);
  307. bool mandatory = false;
  308. char *op_str;
  309. int ret;
  310. ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
  311. if (ret) {
  312. debug("Unknown remote processor id '%d' requested(%d)\n",
  313. id, ret);
  314. return ret;
  315. }
  316. uc_pdata = dev_get_uclass_platdata(dev);
  317. ops = rproc_get_ops(dev);
  318. if (!ops) {
  319. debug("%s driver has no ops?\n", dev->name);
  320. return -EINVAL;
  321. }
  322. switch (op) {
  323. case RPROC_START:
  324. fn = ops->start;
  325. mandatory = true;
  326. op_str = "Starting";
  327. break;
  328. case RPROC_STOP:
  329. fn = ops->stop;
  330. op_str = "Stopping";
  331. break;
  332. case RPROC_RESET:
  333. fn = ops->reset;
  334. op_str = "Resetting";
  335. break;
  336. case RPROC_RUNNING:
  337. fn = ops->is_running;
  338. op_str = "Checking if running:";
  339. break;
  340. case RPROC_PING:
  341. fn = ops->ping;
  342. op_str = "Pinging";
  343. break;
  344. default:
  345. debug("what is '%d' operation??\n", op);
  346. return -EINVAL;
  347. }
  348. debug("%s %s...\n", op_str, uc_pdata->name);
  349. if (fn)
  350. return fn(dev);
  351. if (mandatory)
  352. debug("%s: data corruption?? mandatory function is missing!\n",
  353. dev->name);
  354. return -ENOSYS;
  355. }
  356. int rproc_start(int id)
  357. {
  358. return _rproc_ops_wrapper(id, RPROC_START);
  359. };
  360. int rproc_stop(int id)
  361. {
  362. return _rproc_ops_wrapper(id, RPROC_STOP);
  363. };
  364. int rproc_reset(int id)
  365. {
  366. return _rproc_ops_wrapper(id, RPROC_RESET);
  367. };
  368. int rproc_ping(int id)
  369. {
  370. return _rproc_ops_wrapper(id, RPROC_PING);
  371. };
  372. int rproc_is_running(int id)
  373. {
  374. return _rproc_ops_wrapper(id, RPROC_RUNNING);
  375. };