rproc-uclass.c 10 KB

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