rproc-uclass.c 10 KB

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