rproc-uclass.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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 <elf.h>
  10. #include <errno.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <virtio_ring.h>
  14. #include <remoteproc.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. #include <linux/compat.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. struct resource_table {
  23. u32 ver;
  24. u32 num;
  25. u32 reserved[2];
  26. u32 offset[0];
  27. } __packed;
  28. typedef int (*handle_resource_t) (struct udevice *, void *, int offset, int avail);
  29. static struct resource_table *rsc_table;
  30. /**
  31. * for_each_remoteproc_device() - iterate through the list of rproc devices
  32. * @fn: check function to call per match, if this function returns fail,
  33. * iteration is aborted with the resultant error value
  34. * @skip_dev: Device to skip calling the callback about.
  35. * @data: Data to pass to the callback function
  36. *
  37. * Return: 0 if none of the callback returned a non 0 result, else returns the
  38. * result from the callback function
  39. */
  40. static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
  41. struct dm_rproc_uclass_pdata *uc_pdata,
  42. const void *data),
  43. struct udevice *skip_dev,
  44. const void *data)
  45. {
  46. struct udevice *dev;
  47. struct dm_rproc_uclass_pdata *uc_pdata;
  48. int ret;
  49. for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
  50. ret = uclass_find_next_device(&dev)) {
  51. if (ret || dev == skip_dev)
  52. continue;
  53. uc_pdata = dev_get_uclass_plat(dev);
  54. ret = fn(dev, uc_pdata, data);
  55. if (ret)
  56. return ret;
  57. }
  58. return 0;
  59. }
  60. /**
  61. * _rproc_name_is_unique() - iteration helper to check if rproc name is unique
  62. * @dev: device that we are checking name for
  63. * @uc_pdata: uclass platform data
  64. * @data: compare data (this is the name we want to ensure is unique)
  65. *
  66. * Return: 0 is there is no match(is unique); if there is a match(we dont
  67. * have a unique name), return -EINVAL.
  68. */
  69. static int _rproc_name_is_unique(struct udevice *dev,
  70. struct dm_rproc_uclass_pdata *uc_pdata,
  71. const void *data)
  72. {
  73. const char *check_name = data;
  74. /* devices not yet populated with data - so skip them */
  75. if (!uc_pdata->name || !check_name)
  76. return 0;
  77. /* Return 0 to search further if we dont match */
  78. if (strlen(uc_pdata->name) != strlen(check_name))
  79. return 0;
  80. if (!strcmp(uc_pdata->name, check_name))
  81. return -EINVAL;
  82. return 0;
  83. }
  84. /**
  85. * rproc_name_is_unique() - Check if the rproc name is unique
  86. * @check_dev: Device we are attempting to ensure is unique
  87. * @check_name: Name we are trying to ensure is unique.
  88. *
  89. * Return: true if we have a unique name, false if name is not unique.
  90. */
  91. static bool rproc_name_is_unique(struct udevice *check_dev,
  92. const char *check_name)
  93. {
  94. int ret;
  95. ret = for_each_remoteproc_device(_rproc_name_is_unique,
  96. check_dev, check_name);
  97. return ret ? false : true;
  98. }
  99. /**
  100. * rproc_pre_probe() - Pre probe accessor for the uclass
  101. * @dev: device for which we are preprobing
  102. *
  103. * Parses and fills up the uclass pdata for use as needed by core and
  104. * remote proc drivers.
  105. *
  106. * Return: 0 if all wernt ok, else appropriate error value.
  107. */
  108. static int rproc_pre_probe(struct udevice *dev)
  109. {
  110. struct dm_rproc_uclass_pdata *uc_pdata;
  111. const struct dm_rproc_ops *ops;
  112. uc_pdata = dev_get_uclass_plat(dev);
  113. /* See if we need to populate via fdt */
  114. if (!dev_get_plat(dev)) {
  115. #if CONFIG_IS_ENABLED(OF_CONTROL)
  116. bool tmp;
  117. debug("'%s': using fdt\n", dev->name);
  118. uc_pdata->name = dev_read_string(dev, "remoteproc-name");
  119. /* Default is internal memory mapped */
  120. uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
  121. tmp = dev_read_bool(dev, "remoteproc-internal-memory-mapped");
  122. if (tmp)
  123. uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
  124. #else
  125. /* Nothing much we can do about this, can we? */
  126. return -EINVAL;
  127. #endif
  128. } else {
  129. struct dm_rproc_uclass_pdata *pdata = dev_get_plat(dev);
  130. debug("'%s': using legacy data\n", dev->name);
  131. if (pdata->name)
  132. uc_pdata->name = pdata->name;
  133. uc_pdata->mem_type = pdata->mem_type;
  134. uc_pdata->driver_plat_data = pdata->driver_plat_data;
  135. }
  136. /* Else try using device Name */
  137. if (!uc_pdata->name)
  138. uc_pdata->name = dev->name;
  139. if (!uc_pdata->name) {
  140. debug("Unnamed device!");
  141. return -EINVAL;
  142. }
  143. if (!rproc_name_is_unique(dev, uc_pdata->name)) {
  144. debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name);
  145. return -EINVAL;
  146. }
  147. ops = rproc_get_ops(dev);
  148. if (!ops) {
  149. debug("%s driver has no ops?\n", dev->name);
  150. return -EINVAL;
  151. }
  152. if (!ops->load || !ops->start) {
  153. debug("%s driver has missing mandatory ops?\n", dev->name);
  154. return -EINVAL;
  155. }
  156. return 0;
  157. }
  158. /**
  159. * rproc_post_probe() - post probe accessor for the uclass
  160. * @dev: deivce we finished probing
  161. *
  162. * initiate init function after the probe is completed. This allows
  163. * the remote processor drivers to split up the initializations between
  164. * probe and init as needed.
  165. *
  166. * Return: if the remote proc driver has a init routine, invokes it and
  167. * hands over the return value. overall, 0 if all went well, else appropriate
  168. * error value.
  169. */
  170. static int rproc_post_probe(struct udevice *dev)
  171. {
  172. const struct dm_rproc_ops *ops;
  173. ops = rproc_get_ops(dev);
  174. if (!ops) {
  175. debug("%s driver has no ops?\n", dev->name);
  176. return -EINVAL;
  177. }
  178. if (ops->init)
  179. return ops->init(dev);
  180. return 0;
  181. }
  182. /**
  183. * rproc_add_res() - After parsing the resource table add the mappings
  184. * @dev: device we finished probing
  185. * @mapping: rproc_mem_entry for the resource
  186. *
  187. * Return: if the remote proc driver has a add_res routine, invokes it and
  188. * hands over the return value. overall, 0 if all went well, else appropriate
  189. * error value.
  190. */
  191. static int rproc_add_res(struct udevice *dev, struct rproc_mem_entry *mapping)
  192. {
  193. const struct dm_rproc_ops *ops = rproc_get_ops(dev);
  194. if (!ops->add_res)
  195. return -ENOSYS;
  196. return ops->add_res(dev, mapping);
  197. }
  198. /**
  199. * rproc_alloc_mem() - After parsing the resource table allocat mem
  200. * @dev: device we finished probing
  201. * @len: rproc_mem_entry for the resource
  202. * @align: alignment for the resource
  203. *
  204. * Return: if the remote proc driver has a add_res routine, invokes it and
  205. * hands over the return value. overall, 0 if all went well, else appropriate
  206. * error value.
  207. */
  208. static void *rproc_alloc_mem(struct udevice *dev, unsigned long len,
  209. unsigned long align)
  210. {
  211. const struct dm_rproc_ops *ops;
  212. ops = rproc_get_ops(dev);
  213. if (!ops) {
  214. debug("%s driver has no ops?\n", dev->name);
  215. return NULL;
  216. }
  217. if (ops->alloc_mem)
  218. return ops->alloc_mem(dev, len, align);
  219. return NULL;
  220. }
  221. /**
  222. * rproc_config_pagetable() - Configure page table for remote processor
  223. * @dev: device we finished probing
  224. * @virt: Virtual address of the resource
  225. * @phys: Physical address the resource
  226. * @len: length the resource
  227. *
  228. * Return: if the remote proc driver has a add_res routine, invokes it and
  229. * hands over the return value. overall, 0 if all went well, else appropriate
  230. * error value.
  231. */
  232. static int rproc_config_pagetable(struct udevice *dev, unsigned int virt,
  233. unsigned int phys, unsigned int len)
  234. {
  235. const struct dm_rproc_ops *ops;
  236. ops = rproc_get_ops(dev);
  237. if (!ops) {
  238. debug("%s driver has no ops?\n", dev->name);
  239. return -EINVAL;
  240. }
  241. if (ops->config_pagetable)
  242. return ops->config_pagetable(dev, virt, phys, len);
  243. return 0;
  244. }
  245. UCLASS_DRIVER(rproc) = {
  246. .id = UCLASS_REMOTEPROC,
  247. .name = "remoteproc",
  248. .flags = DM_UC_FLAG_SEQ_ALIAS,
  249. .pre_probe = rproc_pre_probe,
  250. .post_probe = rproc_post_probe,
  251. .per_device_plat_auto = sizeof(struct dm_rproc_uclass_pdata),
  252. };
  253. /* Remoteproc subsystem access functions */
  254. /**
  255. * _rproc_probe_dev() - iteration helper to probe a rproc device
  256. * @dev: device to probe
  257. * @uc_pdata: uclass data allocated for the device
  258. * @data: unused
  259. *
  260. * Return: 0 if all ok, else appropriate error value.
  261. */
  262. static int _rproc_probe_dev(struct udevice *dev,
  263. struct dm_rproc_uclass_pdata *uc_pdata,
  264. const void *data)
  265. {
  266. int ret;
  267. ret = device_probe(dev);
  268. if (ret)
  269. debug("%s: Failed to initialize - %d\n", dev->name, ret);
  270. return ret;
  271. }
  272. /**
  273. * _rproc_dev_is_probed() - check if the device has been probed
  274. * @dev: device to check
  275. * @uc_pdata: unused
  276. * @data: unused
  277. *
  278. * Return: -EAGAIN if not probed else return 0
  279. */
  280. static int _rproc_dev_is_probed(struct udevice *dev,
  281. struct dm_rproc_uclass_pdata *uc_pdata,
  282. const void *data)
  283. {
  284. if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
  285. return 0;
  286. return -EAGAIN;
  287. }
  288. bool rproc_is_initialized(void)
  289. {
  290. int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL);
  291. return ret ? false : true;
  292. }
  293. int rproc_init(void)
  294. {
  295. int ret;
  296. if (rproc_is_initialized()) {
  297. debug("Already initialized\n");
  298. return -EINVAL;
  299. }
  300. ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL);
  301. return ret;
  302. }
  303. int rproc_dev_init(int id)
  304. {
  305. struct udevice *dev = NULL;
  306. int ret;
  307. ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
  308. if (ret) {
  309. debug("Unknown remote processor id '%d' requested(%d)\n",
  310. id, ret);
  311. return ret;
  312. }
  313. ret = device_probe(dev);
  314. if (ret)
  315. debug("%s: Failed to initialize - %d\n", dev->name, ret);
  316. return ret;
  317. }
  318. int rproc_load(int id, ulong addr, ulong size)
  319. {
  320. struct udevice *dev = NULL;
  321. struct dm_rproc_uclass_pdata *uc_pdata;
  322. const struct dm_rproc_ops *ops;
  323. int ret;
  324. ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
  325. if (ret) {
  326. debug("Unknown remote processor id '%d' requested(%d)\n",
  327. id, ret);
  328. return ret;
  329. }
  330. uc_pdata = dev_get_uclass_plat(dev);
  331. ops = rproc_get_ops(dev);
  332. if (!ops) {
  333. debug("%s driver has no ops?\n", dev->name);
  334. return -EINVAL;
  335. }
  336. debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n",
  337. uc_pdata->name, addr, size);
  338. if (ops->load)
  339. return ops->load(dev, addr, size);
  340. debug("%s: data corruption?? mandatory function is missing!\n",
  341. dev->name);
  342. return -EINVAL;
  343. };
  344. /*
  345. * Completely internal helper enums..
  346. * Keeping this isolated helps this code evolve independent of other
  347. * parts..
  348. */
  349. enum rproc_ops {
  350. RPROC_START,
  351. RPROC_STOP,
  352. RPROC_RESET,
  353. RPROC_PING,
  354. RPROC_RUNNING,
  355. };
  356. /**
  357. * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback
  358. * @id: id of the remote processor
  359. * @op: one of rproc_ops that indicate what operation to invoke
  360. *
  361. * Most of the checks and verification for remoteproc operations are more
  362. * or less same for almost all operations. This allows us to put a wrapper
  363. * and use the common checks to allow the driver to function appropriately.
  364. *
  365. * Return: 0 if all ok, else appropriate error value.
  366. */
  367. static int _rproc_ops_wrapper(int id, enum rproc_ops op)
  368. {
  369. struct udevice *dev = NULL;
  370. struct dm_rproc_uclass_pdata *uc_pdata;
  371. const struct dm_rproc_ops *ops;
  372. int (*fn)(struct udevice *dev);
  373. bool mandatory = false;
  374. char *op_str;
  375. int ret;
  376. ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
  377. if (ret) {
  378. debug("Unknown remote processor id '%d' requested(%d)\n",
  379. id, ret);
  380. return ret;
  381. }
  382. uc_pdata = dev_get_uclass_plat(dev);
  383. ops = rproc_get_ops(dev);
  384. if (!ops) {
  385. debug("%s driver has no ops?\n", dev->name);
  386. return -EINVAL;
  387. }
  388. switch (op) {
  389. case RPROC_START:
  390. fn = ops->start;
  391. mandatory = true;
  392. op_str = "Starting";
  393. break;
  394. case RPROC_STOP:
  395. fn = ops->stop;
  396. op_str = "Stopping";
  397. break;
  398. case RPROC_RESET:
  399. fn = ops->reset;
  400. op_str = "Resetting";
  401. break;
  402. case RPROC_RUNNING:
  403. fn = ops->is_running;
  404. op_str = "Checking if running:";
  405. break;
  406. case RPROC_PING:
  407. fn = ops->ping;
  408. op_str = "Pinging";
  409. break;
  410. default:
  411. debug("what is '%d' operation??\n", op);
  412. return -EINVAL;
  413. }
  414. debug("%s %s...\n", op_str, uc_pdata->name);
  415. if (fn)
  416. return fn(dev);
  417. if (mandatory)
  418. debug("%s: data corruption?? mandatory function is missing!\n",
  419. dev->name);
  420. return -ENOSYS;
  421. }
  422. int rproc_start(int id)
  423. {
  424. return _rproc_ops_wrapper(id, RPROC_START);
  425. };
  426. int rproc_stop(int id)
  427. {
  428. return _rproc_ops_wrapper(id, RPROC_STOP);
  429. };
  430. int rproc_reset(int id)
  431. {
  432. return _rproc_ops_wrapper(id, RPROC_RESET);
  433. };
  434. int rproc_ping(int id)
  435. {
  436. return _rproc_ops_wrapper(id, RPROC_PING);
  437. };
  438. int rproc_is_running(int id)
  439. {
  440. return _rproc_ops_wrapper(id, RPROC_RUNNING);
  441. };
  442. static int handle_trace(struct udevice *dev, struct fw_rsc_trace *rsc,
  443. int offset, int avail)
  444. {
  445. if (sizeof(*rsc) > avail) {
  446. debug("trace rsc is truncated\n");
  447. return -EINVAL;
  448. }
  449. /*
  450. * make sure reserved bytes are zeroes
  451. */
  452. if (rsc->reserved) {
  453. debug("trace rsc has non zero reserved bytes\n");
  454. return -EINVAL;
  455. }
  456. debug("trace rsc: da 0x%x, len 0x%x\n", rsc->da, rsc->len);
  457. return 0;
  458. }
  459. static int handle_devmem(struct udevice *dev, struct fw_rsc_devmem *rsc,
  460. int offset, int avail)
  461. {
  462. struct rproc_mem_entry *mapping;
  463. if (sizeof(*rsc) > avail) {
  464. debug("devmem rsc is truncated\n");
  465. return -EINVAL;
  466. }
  467. /*
  468. * make sure reserved bytes are zeroes
  469. */
  470. if (rsc->reserved) {
  471. debug("devmem rsc has non zero reserved bytes\n");
  472. return -EINVAL;
  473. }
  474. debug("devmem rsc: pa 0x%x, da 0x%x, len 0x%x\n",
  475. rsc->pa, rsc->da, rsc->len);
  476. rproc_config_pagetable(dev, rsc->da, rsc->pa, rsc->len);
  477. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  478. if (!mapping)
  479. return -ENOMEM;
  480. /*
  481. * We'll need this info later when we'll want to unmap everything
  482. * (e.g. on shutdown).
  483. *
  484. * We can't trust the remote processor not to change the resource
  485. * table, so we must maintain this info independently.
  486. */
  487. mapping->dma = rsc->pa;
  488. mapping->da = rsc->da;
  489. mapping->len = rsc->len;
  490. rproc_add_res(dev, mapping);
  491. debug("mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
  492. rsc->pa, rsc->da, rsc->len);
  493. return 0;
  494. }
  495. static int handle_carveout(struct udevice *dev, struct fw_rsc_carveout *rsc,
  496. int offset, int avail)
  497. {
  498. struct rproc_mem_entry *mapping;
  499. if (sizeof(*rsc) > avail) {
  500. debug("carveout rsc is truncated\n");
  501. return -EINVAL;
  502. }
  503. /*
  504. * make sure reserved bytes are zeroes
  505. */
  506. if (rsc->reserved) {
  507. debug("carveout rsc has non zero reserved bytes\n");
  508. return -EINVAL;
  509. }
  510. debug("carveout rsc: da %x, pa %x, len %x, flags %x\n",
  511. rsc->da, rsc->pa, rsc->len, rsc->flags);
  512. rsc->pa = (uintptr_t)rproc_alloc_mem(dev, rsc->len, 8);
  513. if (!rsc->pa) {
  514. debug
  515. ("failed to allocate carveout rsc: da %x, pa %x, len %x, flags %x\n",
  516. rsc->da, rsc->pa, rsc->len, rsc->flags);
  517. return -ENOMEM;
  518. }
  519. rproc_config_pagetable(dev, rsc->da, rsc->pa, rsc->len);
  520. /*
  521. * Ok, this is non-standard.
  522. *
  523. * Sometimes we can't rely on the generic iommu-based DMA API
  524. * to dynamically allocate the device address and then set the IOMMU
  525. * tables accordingly, because some remote processors might
  526. * _require_ us to use hard coded device addresses that their
  527. * firmware was compiled with.
  528. *
  529. * In this case, we must use the IOMMU API directly and map
  530. * the memory to the device address as expected by the remote
  531. * processor.
  532. *
  533. * Obviously such remote processor devices should not be configured
  534. * to use the iommu-based DMA API: we expect 'dma' to contain the
  535. * physical address in this case.
  536. */
  537. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  538. if (!mapping)
  539. return -ENOMEM;
  540. /*
  541. * We'll need this info later when we'll want to unmap
  542. * everything (e.g. on shutdown).
  543. *
  544. * We can't trust the remote processor not to change the
  545. * resource table, so we must maintain this info independently.
  546. */
  547. mapping->dma = rsc->pa;
  548. mapping->da = rsc->da;
  549. mapping->len = rsc->len;
  550. rproc_add_res(dev, mapping);
  551. debug("carveout mapped 0x%x to 0x%x\n", rsc->da, rsc->pa);
  552. return 0;
  553. }
  554. #define RPROC_PAGE_SHIFT 12
  555. #define RPROC_PAGE_SIZE BIT(RPROC_PAGE_SHIFT)
  556. #define RPROC_PAGE_ALIGN(x) (((x) + (RPROC_PAGE_SIZE - 1)) & ~(RPROC_PAGE_SIZE - 1))
  557. static int alloc_vring(struct udevice *dev, struct fw_rsc_vdev *rsc, int i)
  558. {
  559. struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
  560. int size;
  561. int order;
  562. void *pa;
  563. debug("vdev rsc: vring%d: da %x, qsz %d, align %d\n",
  564. i, vring->da, vring->num, vring->align);
  565. /*
  566. * verify queue size and vring alignment are sane
  567. */
  568. if (!vring->num || !vring->align) {
  569. debug("invalid qsz (%d) or alignment (%d)\n", vring->num,
  570. vring->align);
  571. return -EINVAL;
  572. }
  573. /*
  574. * actual size of vring (in bytes)
  575. */
  576. size = RPROC_PAGE_ALIGN(vring_size(vring->num, vring->align));
  577. order = vring->align >> RPROC_PAGE_SHIFT;
  578. pa = rproc_alloc_mem(dev, size, order);
  579. if (!pa) {
  580. debug("failed to allocate vring rsc\n");
  581. return -ENOMEM;
  582. }
  583. debug("alloc_mem(%#x, %d): %p\n", size, order, pa);
  584. vring->da = (uintptr_t)pa;
  585. return !pa;
  586. }
  587. static int handle_vdev(struct udevice *dev, struct fw_rsc_vdev *rsc,
  588. int offset, int avail)
  589. {
  590. int i, ret;
  591. void *pa;
  592. /*
  593. * make sure resource isn't truncated
  594. */
  595. if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
  596. + rsc->config_len > avail) {
  597. debug("vdev rsc is truncated\n");
  598. return -EINVAL;
  599. }
  600. /*
  601. * make sure reserved bytes are zeroes
  602. */
  603. if (rsc->reserved[0] || rsc->reserved[1]) {
  604. debug("vdev rsc has non zero reserved bytes\n");
  605. return -EINVAL;
  606. }
  607. debug("vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
  608. rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
  609. /*
  610. * we currently support only two vrings per rvdev
  611. */
  612. if (rsc->num_of_vrings > 2) {
  613. debug("too many vrings: %d\n", rsc->num_of_vrings);
  614. return -EINVAL;
  615. }
  616. /*
  617. * allocate the vrings
  618. */
  619. for (i = 0; i < rsc->num_of_vrings; i++) {
  620. ret = alloc_vring(dev, rsc, i);
  621. if (ret)
  622. goto alloc_error;
  623. }
  624. pa = rproc_alloc_mem(dev, RPMSG_TOTAL_BUF_SPACE, 6);
  625. if (!pa) {
  626. debug("failed to allocate vdev rsc\n");
  627. return -ENOMEM;
  628. }
  629. debug("vring buffer alloc_mem(%#x, 6): %p\n", RPMSG_TOTAL_BUF_SPACE,
  630. pa);
  631. return 0;
  632. alloc_error:
  633. return ret;
  634. }
  635. /*
  636. * A lookup table for resource handlers. The indices are defined in
  637. * enum fw_resource_type.
  638. */
  639. static handle_resource_t loading_handlers[RSC_LAST] = {
  640. [RSC_CARVEOUT] = (handle_resource_t)handle_carveout,
  641. [RSC_DEVMEM] = (handle_resource_t)handle_devmem,
  642. [RSC_TRACE] = (handle_resource_t)handle_trace,
  643. [RSC_VDEV] = (handle_resource_t)handle_vdev,
  644. };
  645. /*
  646. * handle firmware resource entries before booting the remote processor
  647. */
  648. static int handle_resources(struct udevice *dev, int len,
  649. handle_resource_t handlers[RSC_LAST])
  650. {
  651. handle_resource_t handler;
  652. int ret = 0, i;
  653. for (i = 0; i < rsc_table->num; i++) {
  654. int offset = rsc_table->offset[i];
  655. struct fw_rsc_hdr *hdr = (void *)rsc_table + offset;
  656. int avail = len - offset - sizeof(*hdr);
  657. void *rsc = (void *)hdr + sizeof(*hdr);
  658. /*
  659. * make sure table isn't truncated
  660. */
  661. if (avail < 0) {
  662. debug("rsc table is truncated\n");
  663. return -EINVAL;
  664. }
  665. debug("rsc: type %d\n", hdr->type);
  666. if (hdr->type >= RSC_LAST) {
  667. debug("unsupported resource %d\n", hdr->type);
  668. continue;
  669. }
  670. handler = handlers[hdr->type];
  671. if (!handler)
  672. continue;
  673. ret = handler(dev, rsc, offset + sizeof(*hdr), avail);
  674. if (ret)
  675. break;
  676. }
  677. return ret;
  678. }
  679. static int
  680. handle_intmem_to_l3_mapping(struct udevice *dev,
  681. struct rproc_intmem_to_l3_mapping *l3_mapping)
  682. {
  683. u32 i = 0;
  684. for (i = 0; i < l3_mapping->num_entries; i++) {
  685. struct l3_map *curr_map = &l3_mapping->mappings[i];
  686. struct rproc_mem_entry *mapping;
  687. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  688. if (!mapping)
  689. return -ENOMEM;
  690. mapping->dma = curr_map->l3_addr;
  691. mapping->da = curr_map->priv_addr;
  692. mapping->len = curr_map->len;
  693. rproc_add_res(dev, mapping);
  694. }
  695. return 0;
  696. }
  697. static Elf32_Shdr *rproc_find_table(unsigned int addr)
  698. {
  699. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  700. Elf32_Shdr *shdr; /* Section header structure pointer */
  701. Elf32_Shdr sectionheader;
  702. int i;
  703. u8 *elf_data;
  704. char *name_table;
  705. struct resource_table *ptable;
  706. ehdr = (Elf32_Ehdr *)(uintptr_t)addr;
  707. elf_data = (u8 *)ehdr;
  708. shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
  709. memcpy(&sectionheader, &shdr[ehdr->e_shstrndx], sizeof(sectionheader));
  710. name_table = (char *)(elf_data + sectionheader.sh_offset);
  711. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  712. memcpy(&sectionheader, shdr, sizeof(sectionheader));
  713. u32 size = sectionheader.sh_size;
  714. u32 offset = sectionheader.sh_offset;
  715. if (strcmp
  716. (name_table + sectionheader.sh_name, ".resource_table"))
  717. continue;
  718. ptable = (struct resource_table *)(elf_data + offset);
  719. /*
  720. * make sure table has at least the header
  721. */
  722. if (sizeof(struct resource_table) > size) {
  723. debug("header-less resource table\n");
  724. return NULL;
  725. }
  726. /*
  727. * we don't support any version beyond the first
  728. */
  729. if (ptable->ver != 1) {
  730. debug("unsupported fw ver: %d\n", ptable->ver);
  731. return NULL;
  732. }
  733. /*
  734. * make sure reserved bytes are zeroes
  735. */
  736. if (ptable->reserved[0] || ptable->reserved[1]) {
  737. debug("non zero reserved bytes\n");
  738. return NULL;
  739. }
  740. /*
  741. * make sure the offsets array isn't truncated
  742. */
  743. if (ptable->num * sizeof(ptable->offset[0]) +
  744. sizeof(struct resource_table) > size) {
  745. debug("resource table incomplete\n");
  746. return NULL;
  747. }
  748. return shdr;
  749. }
  750. return NULL;
  751. }
  752. struct resource_table *rproc_find_resource_table(struct udevice *dev,
  753. unsigned int addr,
  754. int *tablesz)
  755. {
  756. Elf32_Shdr *shdr;
  757. Elf32_Shdr sectionheader;
  758. struct resource_table *ptable;
  759. u8 *elf_data = (u8 *)(uintptr_t)addr;
  760. shdr = rproc_find_table(addr);
  761. if (!shdr) {
  762. debug("%s: failed to get resource section header\n", __func__);
  763. return NULL;
  764. }
  765. memcpy(&sectionheader, shdr, sizeof(sectionheader));
  766. ptable = (struct resource_table *)(elf_data + sectionheader.sh_offset);
  767. if (tablesz)
  768. *tablesz = sectionheader.sh_size;
  769. return ptable;
  770. }
  771. unsigned long rproc_parse_resource_table(struct udevice *dev, struct rproc *cfg)
  772. {
  773. struct resource_table *ptable = NULL;
  774. int tablesz;
  775. int ret;
  776. unsigned long addr;
  777. addr = cfg->load_addr;
  778. ptable = rproc_find_resource_table(dev, addr, &tablesz);
  779. if (!ptable) {
  780. debug("%s : failed to find resource table\n", __func__);
  781. return 0;
  782. }
  783. debug("%s : found resource table\n", __func__);
  784. rsc_table = kzalloc(tablesz, GFP_KERNEL);
  785. if (!rsc_table) {
  786. debug("resource table alloc failed!\n");
  787. return 0;
  788. }
  789. /*
  790. * Copy the resource table into a local buffer before handling the
  791. * resource table.
  792. */
  793. memcpy(rsc_table, ptable, tablesz);
  794. if (cfg->intmem_to_l3_mapping)
  795. handle_intmem_to_l3_mapping(dev, cfg->intmem_to_l3_mapping);
  796. ret = handle_resources(dev, tablesz, loading_handlers);
  797. if (ret) {
  798. debug("handle_resources failed: %d\n", ret);
  799. return 0;
  800. }
  801. /*
  802. * Instead of trying to mimic the kernel flow of copying the
  803. * processed resource table into its post ELF load location in DDR
  804. * copying it into its original location.
  805. */
  806. memcpy(ptable, rsc_table, tablesz);
  807. free(rsc_table);
  808. rsc_table = NULL;
  809. return 1;
  810. }