pru_rproc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PRU-ICSS remoteproc driver for various TI SoCs
  4. *
  5. * Copyright (C) 2014-2022 Texas Instruments Incorporated - https://www.ti.com/
  6. *
  7. * Author(s):
  8. * Suman Anna <s-anna@ti.com>
  9. * Andrew F. Davis <afd@ti.com>
  10. * Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org> for Texas Instruments
  11. * Puranjay Mohan <p-mohan@ti.com>
  12. * Md Danish Anwar <danishanwar@ti.com>
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/remoteproc/pruss.h>
  22. #include <linux/pruss_driver.h>
  23. #include <linux/remoteproc.h>
  24. #include "remoteproc_internal.h"
  25. #include "remoteproc_elf_helpers.h"
  26. #include "pru_rproc.h"
  27. /* PRU_ICSS_PRU_CTRL registers */
  28. #define PRU_CTRL_CTRL 0x0000
  29. #define PRU_CTRL_STS 0x0004
  30. #define PRU_CTRL_WAKEUP_EN 0x0008
  31. #define PRU_CTRL_CYCLE 0x000C
  32. #define PRU_CTRL_STALL 0x0010
  33. #define PRU_CTRL_CTBIR0 0x0020
  34. #define PRU_CTRL_CTBIR1 0x0024
  35. #define PRU_CTRL_CTPPR0 0x0028
  36. #define PRU_CTRL_CTPPR1 0x002C
  37. /* CTRL register bit-fields */
  38. #define CTRL_CTRL_SOFT_RST_N BIT(0)
  39. #define CTRL_CTRL_EN BIT(1)
  40. #define CTRL_CTRL_SLEEPING BIT(2)
  41. #define CTRL_CTRL_CTR_EN BIT(3)
  42. #define CTRL_CTRL_SINGLE_STEP BIT(8)
  43. #define CTRL_CTRL_RUNSTATE BIT(15)
  44. /* PRU_ICSS_PRU_DEBUG registers */
  45. #define PRU_DEBUG_GPREG(x) (0x0000 + (x) * 4)
  46. #define PRU_DEBUG_CT_REG(x) (0x0080 + (x) * 4)
  47. /* PRU/RTU/Tx_PRU Core IRAM address masks */
  48. #define PRU_IRAM_ADDR_MASK 0x3ffff
  49. #define PRU0_IRAM_ADDR_MASK 0x34000
  50. #define PRU1_IRAM_ADDR_MASK 0x38000
  51. #define RTU0_IRAM_ADDR_MASK 0x4000
  52. #define RTU1_IRAM_ADDR_MASK 0x6000
  53. #define TX_PRU0_IRAM_ADDR_MASK 0xa000
  54. #define TX_PRU1_IRAM_ADDR_MASK 0xc000
  55. /* PRU device addresses for various type of PRU RAMs */
  56. #define PRU_IRAM_DA 0 /* Instruction RAM */
  57. #define PRU_PDRAM_DA 0 /* Primary Data RAM */
  58. #define PRU_SDRAM_DA 0x2000 /* Secondary Data RAM */
  59. #define PRU_SHRDRAM_DA 0x10000 /* Shared Data RAM */
  60. #define MAX_PRU_SYS_EVENTS 160
  61. /**
  62. * enum pru_iomem - PRU core memory/register range identifiers
  63. *
  64. * @PRU_IOMEM_IRAM: PRU Instruction RAM range
  65. * @PRU_IOMEM_CTRL: PRU Control register range
  66. * @PRU_IOMEM_DEBUG: PRU Debug register range
  67. * @PRU_IOMEM_MAX: just keep this one at the end
  68. */
  69. enum pru_iomem {
  70. PRU_IOMEM_IRAM = 0,
  71. PRU_IOMEM_CTRL,
  72. PRU_IOMEM_DEBUG,
  73. PRU_IOMEM_MAX,
  74. };
  75. /**
  76. * struct pru_private_data - device data for a PRU core
  77. * @type: type of the PRU core (PRU, RTU, Tx_PRU)
  78. * @is_k3: flag used to identify the need for special load handling
  79. */
  80. struct pru_private_data {
  81. enum pru_type type;
  82. unsigned int is_k3 : 1;
  83. };
  84. /**
  85. * struct pru_rproc - PRU remoteproc structure
  86. * @id: id of the PRU core within the PRUSS
  87. * @dev: PRU core device pointer
  88. * @pruss: back-reference to parent PRUSS structure
  89. * @rproc: remoteproc pointer for this PRU core
  90. * @data: PRU core specific data
  91. * @mem_regions: data for each of the PRU memory regions
  92. * @client_np: client device node
  93. * @lock: mutex to protect client usage
  94. * @fw_name: name of firmware image used during loading
  95. * @mapped_irq: virtual interrupt numbers of created fw specific mapping
  96. * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
  97. * @pru_interrupt_map_sz: pru_interrupt_map size
  98. * @rmw_lock: lock for read, modify, write operations on registers
  99. * @dbg_single_step: debug state variable to set PRU into single step mode
  100. * @dbg_continuous: debug state variable to restore PRU execution mode
  101. * @evt_count: number of mapped events
  102. * @gpmux_save: saved value for gpmux config
  103. */
  104. struct pru_rproc {
  105. int id;
  106. struct device *dev;
  107. struct pruss *pruss;
  108. struct rproc *rproc;
  109. const struct pru_private_data *data;
  110. struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
  111. struct device_node *client_np;
  112. struct mutex lock;
  113. const char *fw_name;
  114. unsigned int *mapped_irq;
  115. struct pru_irq_rsc *pru_interrupt_map;
  116. size_t pru_interrupt_map_sz;
  117. spinlock_t rmw_lock;
  118. u32 dbg_single_step;
  119. u32 dbg_continuous;
  120. u8 evt_count;
  121. u8 gpmux_save;
  122. };
  123. static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
  124. {
  125. return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
  126. }
  127. static inline
  128. void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
  129. {
  130. writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
  131. }
  132. static inline
  133. void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
  134. u32 mask, u32 set)
  135. {
  136. u32 val;
  137. unsigned long flags;
  138. spin_lock_irqsave(&pru->rmw_lock, flags);
  139. val = pru_control_read_reg(pru, reg);
  140. val &= ~mask;
  141. val |= (set & mask);
  142. pru_control_write_reg(pru, reg, val);
  143. spin_unlock_irqrestore(&pru->rmw_lock, flags);
  144. }
  145. /**
  146. * pru_rproc_set_firmware() - set firmware for a PRU core
  147. * @rproc: the rproc instance of the PRU
  148. * @fw_name: the new firmware name, or NULL if default is desired
  149. *
  150. * Return: 0 on success, or errno in error case.
  151. */
  152. static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
  153. {
  154. struct pru_rproc *pru = rproc->priv;
  155. if (!fw_name)
  156. fw_name = pru->fw_name;
  157. return rproc_set_firmware(rproc, fw_name);
  158. }
  159. static struct rproc *__pru_rproc_get(struct device_node *np, int index)
  160. {
  161. struct rproc *rproc;
  162. phandle rproc_phandle;
  163. int ret;
  164. ret = of_property_read_u32_index(np, "ti,prus", index, &rproc_phandle);
  165. if (ret)
  166. return ERR_PTR(ret);
  167. rproc = rproc_get_by_phandle(rproc_phandle);
  168. if (!rproc) {
  169. ret = -EPROBE_DEFER;
  170. return ERR_PTR(ret);
  171. }
  172. /* make sure it is PRU rproc */
  173. if (!is_pru_rproc(rproc->dev.parent)) {
  174. rproc_put(rproc);
  175. return ERR_PTR(-ENODEV);
  176. }
  177. return rproc;
  178. }
  179. /**
  180. * pru_rproc_get() - get the PRU rproc instance from a device node
  181. * @np: the user/client device node
  182. * @index: index to use for the ti,prus property
  183. * @pru_id: optional pointer to return the PRU remoteproc processor id
  184. *
  185. * This function looks through a client device node's "ti,prus" property at
  186. * index @index and returns the rproc handle for a valid PRU remote processor if
  187. * found. The function allows only one user to own the PRU rproc resource at a
  188. * time. Caller must call pru_rproc_put() when done with using the rproc, not
  189. * required if the function returns a failure.
  190. *
  191. * When optional @pru_id pointer is passed the PRU remoteproc processor id is
  192. * returned.
  193. *
  194. * Return: rproc handle on success, and an ERR_PTR on failure using one
  195. * of the following error values
  196. * -ENODEV if device is not found
  197. * -EBUSY if PRU is already acquired by anyone
  198. * -EPROBE_DEFER is PRU device is not probed yet
  199. */
  200. struct rproc *pru_rproc_get(struct device_node *np, int index,
  201. enum pruss_pru_id *pru_id)
  202. {
  203. struct rproc *rproc;
  204. struct pru_rproc *pru;
  205. struct device *dev;
  206. const char *fw_name;
  207. int ret;
  208. u32 mux;
  209. rproc = __pru_rproc_get(np, index);
  210. if (IS_ERR(rproc))
  211. return rproc;
  212. pru = rproc->priv;
  213. dev = &rproc->dev;
  214. mutex_lock(&pru->lock);
  215. if (pru->client_np) {
  216. mutex_unlock(&pru->lock);
  217. ret = -EBUSY;
  218. goto err_no_rproc_handle;
  219. }
  220. pru->client_np = np;
  221. rproc->sysfs_read_only = true;
  222. mutex_unlock(&pru->lock);
  223. if (pru_id)
  224. *pru_id = pru->id;
  225. ret = pruss_cfg_get_gpmux(pru->pruss, pru->id, &pru->gpmux_save);
  226. if (ret) {
  227. dev_err(dev, "failed to get cfg gpmux: %d\n", ret);
  228. goto err;
  229. }
  230. /* An error here is acceptable for backward compatibility */
  231. ret = of_property_read_u32_index(np, "ti,pruss-gp-mux-sel", index,
  232. &mux);
  233. if (!ret) {
  234. ret = pruss_cfg_set_gpmux(pru->pruss, pru->id, mux);
  235. if (ret) {
  236. dev_err(dev, "failed to set cfg gpmux: %d\n", ret);
  237. goto err;
  238. }
  239. }
  240. ret = of_property_read_string_index(np, "firmware-name", index,
  241. &fw_name);
  242. if (!ret) {
  243. ret = pru_rproc_set_firmware(rproc, fw_name);
  244. if (ret) {
  245. dev_err(dev, "failed to set firmware: %d\n", ret);
  246. goto err;
  247. }
  248. }
  249. return rproc;
  250. err_no_rproc_handle:
  251. rproc_put(rproc);
  252. return ERR_PTR(ret);
  253. err:
  254. pru_rproc_put(rproc);
  255. return ERR_PTR(ret);
  256. }
  257. EXPORT_SYMBOL_GPL(pru_rproc_get);
  258. /**
  259. * pru_rproc_put() - release the PRU rproc resource
  260. * @rproc: the rproc resource to release
  261. *
  262. * Releases the PRU rproc resource and makes it available to other
  263. * users.
  264. */
  265. void pru_rproc_put(struct rproc *rproc)
  266. {
  267. struct pru_rproc *pru;
  268. if (IS_ERR_OR_NULL(rproc) || !is_pru_rproc(rproc->dev.parent))
  269. return;
  270. pru = rproc->priv;
  271. pruss_cfg_set_gpmux(pru->pruss, pru->id, pru->gpmux_save);
  272. pru_rproc_set_firmware(rproc, NULL);
  273. mutex_lock(&pru->lock);
  274. if (!pru->client_np) {
  275. mutex_unlock(&pru->lock);
  276. return;
  277. }
  278. pru->client_np = NULL;
  279. rproc->sysfs_read_only = false;
  280. mutex_unlock(&pru->lock);
  281. rproc_put(rproc);
  282. }
  283. EXPORT_SYMBOL_GPL(pru_rproc_put);
  284. /**
  285. * pru_rproc_set_ctable() - set the constant table index for the PRU
  286. * @rproc: the rproc instance of the PRU
  287. * @c: constant table index to set
  288. * @addr: physical address to set it to
  289. *
  290. * Return: 0 on success, or errno in error case.
  291. */
  292. int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
  293. {
  294. struct pru_rproc *pru;
  295. unsigned int reg;
  296. u32 mask, set;
  297. u16 idx;
  298. u16 idx_mask;
  299. if (IS_ERR_OR_NULL(rproc))
  300. return -EINVAL;
  301. if (!rproc->dev.parent || !is_pru_rproc(rproc->dev.parent))
  302. return -ENODEV;
  303. pru = rproc->priv;
  304. /* pointer is 16 bit and index is 8-bit so mask out the rest */
  305. idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
  306. /* ctable uses bit 8 and upwards only */
  307. idx = (addr >> 8) & idx_mask;
  308. /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
  309. reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
  310. mask = idx_mask << (16 * (c & 1));
  311. set = idx << (16 * (c & 1));
  312. pru_control_set_reg(pru, reg, mask, set);
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
  316. static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
  317. {
  318. return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
  319. }
  320. static int regs_show(struct seq_file *s, void *data)
  321. {
  322. struct rproc *rproc = s->private;
  323. struct pru_rproc *pru = rproc->priv;
  324. int i, nregs = 32;
  325. u32 pru_sts;
  326. int pru_is_running;
  327. seq_puts(s, "============== Control Registers ==============\n");
  328. seq_printf(s, "CTRL := 0x%08x\n",
  329. pru_control_read_reg(pru, PRU_CTRL_CTRL));
  330. pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
  331. seq_printf(s, "STS (PC) := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
  332. seq_printf(s, "WAKEUP_EN := 0x%08x\n",
  333. pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
  334. seq_printf(s, "CYCLE := 0x%08x\n",
  335. pru_control_read_reg(pru, PRU_CTRL_CYCLE));
  336. seq_printf(s, "STALL := 0x%08x\n",
  337. pru_control_read_reg(pru, PRU_CTRL_STALL));
  338. seq_printf(s, "CTBIR0 := 0x%08x\n",
  339. pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
  340. seq_printf(s, "CTBIR1 := 0x%08x\n",
  341. pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
  342. seq_printf(s, "CTPPR0 := 0x%08x\n",
  343. pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
  344. seq_printf(s, "CTPPR1 := 0x%08x\n",
  345. pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
  346. seq_puts(s, "=============== Debug Registers ===============\n");
  347. pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
  348. CTRL_CTRL_RUNSTATE;
  349. if (pru_is_running) {
  350. seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
  351. return 0;
  352. }
  353. for (i = 0; i < nregs; i++) {
  354. seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
  355. i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
  356. i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
  357. }
  358. return 0;
  359. }
  360. DEFINE_SHOW_ATTRIBUTE(regs);
  361. /*
  362. * Control PRU single-step mode
  363. *
  364. * This is a debug helper function used for controlling the single-step
  365. * mode of the PRU. The PRU Debug registers are not accessible when the
  366. * PRU is in RUNNING state.
  367. *
  368. * Writing a non-zero value sets the PRU into single-step mode irrespective
  369. * of its previous state. The PRU mode is saved only on the first set into
  370. * a single-step mode. Writing a zero value will restore the PRU into its
  371. * original mode.
  372. */
  373. static int pru_rproc_debug_ss_set(void *data, u64 val)
  374. {
  375. struct rproc *rproc = data;
  376. struct pru_rproc *pru = rproc->priv;
  377. u32 reg_val;
  378. val = val ? 1 : 0;
  379. if (!val && !pru->dbg_single_step)
  380. return 0;
  381. reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
  382. if (val && !pru->dbg_single_step)
  383. pru->dbg_continuous = reg_val;
  384. if (val)
  385. reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
  386. else
  387. reg_val = pru->dbg_continuous;
  388. pru->dbg_single_step = val;
  389. pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
  390. return 0;
  391. }
  392. static int pru_rproc_debug_ss_get(void *data, u64 *val)
  393. {
  394. struct rproc *rproc = data;
  395. struct pru_rproc *pru = rproc->priv;
  396. *val = pru->dbg_single_step;
  397. return 0;
  398. }
  399. DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
  400. pru_rproc_debug_ss_set, "%llu\n");
  401. /*
  402. * Create PRU-specific debugfs entries
  403. *
  404. * The entries are created only if the parent remoteproc debugfs directory
  405. * exists, and will be cleaned up by the remoteproc core.
  406. */
  407. static void pru_rproc_create_debug_entries(struct rproc *rproc)
  408. {
  409. if (!rproc->dbg_dir)
  410. return;
  411. debugfs_create_file("regs", 0400, rproc->dbg_dir,
  412. rproc, &regs_fops);
  413. debugfs_create_file("single_step", 0600, rproc->dbg_dir,
  414. rproc, &pru_rproc_debug_ss_fops);
  415. }
  416. static void pru_dispose_irq_mapping(struct pru_rproc *pru)
  417. {
  418. if (!pru->mapped_irq)
  419. return;
  420. while (pru->evt_count) {
  421. pru->evt_count--;
  422. if (pru->mapped_irq[pru->evt_count] > 0)
  423. irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
  424. }
  425. kfree(pru->mapped_irq);
  426. pru->mapped_irq = NULL;
  427. }
  428. /*
  429. * Parse the custom PRU interrupt map resource and configure the INTC
  430. * appropriately.
  431. */
  432. static int pru_handle_intrmap(struct rproc *rproc)
  433. {
  434. struct device *dev = rproc->dev.parent;
  435. struct pru_rproc *pru = rproc->priv;
  436. struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
  437. struct irq_fwspec fwspec;
  438. struct device_node *parent, *irq_parent;
  439. int i, ret = 0;
  440. /* not having pru_interrupt_map is not an error */
  441. if (!rsc)
  442. return 0;
  443. /* currently supporting only type 0 */
  444. if (rsc->type != 0) {
  445. dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
  446. return -EINVAL;
  447. }
  448. if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
  449. return -EINVAL;
  450. if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
  451. pru->pru_interrupt_map_sz)
  452. return -EINVAL;
  453. pru->evt_count = rsc->num_evts;
  454. pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
  455. GFP_KERNEL);
  456. if (!pru->mapped_irq) {
  457. pru->evt_count = 0;
  458. return -ENOMEM;
  459. }
  460. /*
  461. * parse and fill in system event to interrupt channel and
  462. * channel-to-host mapping. The interrupt controller to be used
  463. * for these mappings for a given PRU remoteproc is always its
  464. * corresponding sibling PRUSS INTC node.
  465. */
  466. parent = of_get_parent(dev_of_node(pru->dev));
  467. if (!parent) {
  468. kfree(pru->mapped_irq);
  469. pru->mapped_irq = NULL;
  470. pru->evt_count = 0;
  471. return -ENODEV;
  472. }
  473. irq_parent = of_get_child_by_name(parent, "interrupt-controller");
  474. of_node_put(parent);
  475. if (!irq_parent) {
  476. kfree(pru->mapped_irq);
  477. pru->mapped_irq = NULL;
  478. pru->evt_count = 0;
  479. return -ENODEV;
  480. }
  481. fwspec.fwnode = of_fwnode_handle(irq_parent);
  482. fwspec.param_count = 3;
  483. for (i = 0; i < pru->evt_count; i++) {
  484. fwspec.param[0] = rsc->pru_intc_map[i].event;
  485. fwspec.param[1] = rsc->pru_intc_map[i].chnl;
  486. fwspec.param[2] = rsc->pru_intc_map[i].host;
  487. dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
  488. i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
  489. pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
  490. if (!pru->mapped_irq[i]) {
  491. dev_err(dev, "failed to get virq for fw mapping %d: event %d chnl %d host %d\n",
  492. i, fwspec.param[0], fwspec.param[1],
  493. fwspec.param[2]);
  494. ret = -EINVAL;
  495. goto map_fail;
  496. }
  497. }
  498. of_node_put(irq_parent);
  499. return ret;
  500. map_fail:
  501. pru_dispose_irq_mapping(pru);
  502. of_node_put(irq_parent);
  503. return ret;
  504. }
  505. static int pru_rproc_start(struct rproc *rproc)
  506. {
  507. struct device *dev = &rproc->dev;
  508. struct pru_rproc *pru = rproc->priv;
  509. const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
  510. u32 val;
  511. int ret;
  512. dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
  513. names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
  514. ret = pru_handle_intrmap(rproc);
  515. /*
  516. * reset references to pru interrupt map - they will stop being valid
  517. * after rproc_start returns
  518. */
  519. pru->pru_interrupt_map = NULL;
  520. pru->pru_interrupt_map_sz = 0;
  521. if (ret)
  522. return ret;
  523. val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
  524. pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
  525. return 0;
  526. }
  527. static int pru_rproc_stop(struct rproc *rproc)
  528. {
  529. struct device *dev = &rproc->dev;
  530. struct pru_rproc *pru = rproc->priv;
  531. const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
  532. u32 val;
  533. dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
  534. val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
  535. val &= ~CTRL_CTRL_EN;
  536. pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
  537. /* dispose irq mapping - new firmware can provide new mapping */
  538. pru_dispose_irq_mapping(pru);
  539. return 0;
  540. }
  541. /*
  542. * Convert PRU device address (data spaces only) to kernel virtual address.
  543. *
  544. * Each PRU has access to all data memories within the PRUSS, accessible at
  545. * different ranges. So, look through both its primary and secondary Data
  546. * RAMs as well as any shared Data RAM to convert a PRU device address to
  547. * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
  548. * RAM1 is primary Data RAM for PRU1.
  549. */
  550. static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
  551. {
  552. struct pruss_mem_region dram0, dram1, shrd_ram;
  553. struct pruss *pruss = pru->pruss;
  554. u32 offset;
  555. void *va = NULL;
  556. if (len == 0)
  557. return NULL;
  558. dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
  559. dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
  560. /* PRU1 has its local RAM addresses reversed */
  561. if (pru->id == PRUSS_PRU1)
  562. swap(dram0, dram1);
  563. shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
  564. if (da + len <= PRU_PDRAM_DA + dram0.size) {
  565. offset = da - PRU_PDRAM_DA;
  566. va = (__force void *)(dram0.va + offset);
  567. } else if (da >= PRU_SDRAM_DA &&
  568. da + len <= PRU_SDRAM_DA + dram1.size) {
  569. offset = da - PRU_SDRAM_DA;
  570. va = (__force void *)(dram1.va + offset);
  571. } else if (da >= PRU_SHRDRAM_DA &&
  572. da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
  573. offset = da - PRU_SHRDRAM_DA;
  574. va = (__force void *)(shrd_ram.va + offset);
  575. }
  576. return va;
  577. }
  578. /*
  579. * Convert PRU device address (instruction space) to kernel virtual address.
  580. *
  581. * A PRU does not have an unified address space. Each PRU has its very own
  582. * private Instruction RAM, and its device address is identical to that of
  583. * its primary Data RAM device address.
  584. */
  585. static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
  586. {
  587. u32 offset;
  588. void *va = NULL;
  589. if (len == 0)
  590. return NULL;
  591. /*
  592. * GNU binutils do not support multiple address spaces. The GNU
  593. * linker's default linker script places IRAM at an arbitrary high
  594. * offset, in order to differentiate it from DRAM. Hence we need to
  595. * strip the artificial offset in the IRAM addresses coming from the
  596. * ELF file.
  597. *
  598. * The TI proprietary linker would never set those higher IRAM address
  599. * bits anyway. PRU architecture limits the program counter to 16-bit
  600. * word-address range. This in turn corresponds to 18-bit IRAM
  601. * byte-address range for ELF.
  602. *
  603. * Two more bits are added just in case to make the final 20-bit mask.
  604. * Idea is to have a safeguard in case TI decides to add banking
  605. * in future SoCs.
  606. */
  607. da &= 0xfffff;
  608. if (da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
  609. offset = da - PRU_IRAM_DA;
  610. va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
  611. offset);
  612. }
  613. return va;
  614. }
  615. /*
  616. * Provide address translations for only PRU Data RAMs through the remoteproc
  617. * core for any PRU client drivers. The PRU Instruction RAM access is restricted
  618. * only to the PRU loader code.
  619. */
  620. static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  621. {
  622. struct pru_rproc *pru = rproc->priv;
  623. return pru_d_da_to_va(pru, da, len);
  624. }
  625. /* PRU-specific address translator used by PRU loader. */
  626. static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
  627. {
  628. struct pru_rproc *pru = rproc->priv;
  629. void *va;
  630. if (is_iram)
  631. va = pru_i_da_to_va(pru, da, len);
  632. else
  633. va = pru_d_da_to_va(pru, da, len);
  634. return va;
  635. }
  636. static struct rproc_ops pru_rproc_ops = {
  637. .start = pru_rproc_start,
  638. .stop = pru_rproc_stop,
  639. .da_to_va = pru_rproc_da_to_va,
  640. };
  641. /*
  642. * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
  643. *
  644. * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM
  645. * memories, that is not seen on previous generation SoCs. The data is reflected
  646. * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned
  647. * copies result in all the other pre-existing bytes zeroed out within that
  648. * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
  649. * IRAM memory port interface does not allow any 8-byte copies (as commonly used
  650. * by ARM64 memcpy implementation) and throws an exception. The DRAM memory
  651. * ports do not show this behavior.
  652. */
  653. static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
  654. {
  655. const u32 *s = src;
  656. u32 *d = dest;
  657. size_t size = count / 4;
  658. u32 *tmp_src = NULL;
  659. /*
  660. * TODO: relax limitation of 4-byte aligned dest addresses and copy
  661. * sizes
  662. */
  663. if ((long)dest % 4 || count % 4)
  664. return -EINVAL;
  665. /* src offsets in ELF firmware image can be non-aligned */
  666. if ((long)src % 4) {
  667. tmp_src = kmemdup(src, count, GFP_KERNEL);
  668. if (!tmp_src)
  669. return -ENOMEM;
  670. s = tmp_src;
  671. }
  672. while (size--)
  673. *d++ = *s++;
  674. kfree(tmp_src);
  675. return 0;
  676. }
  677. static int
  678. pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
  679. {
  680. struct pru_rproc *pru = rproc->priv;
  681. struct device *dev = &rproc->dev;
  682. struct elf32_hdr *ehdr;
  683. struct elf32_phdr *phdr;
  684. int i, ret = 0;
  685. const u8 *elf_data = fw->data;
  686. ehdr = (struct elf32_hdr *)elf_data;
  687. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  688. /* go through the available ELF segments */
  689. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  690. u32 da = phdr->p_paddr;
  691. u32 memsz = phdr->p_memsz;
  692. u32 filesz = phdr->p_filesz;
  693. u32 offset = phdr->p_offset;
  694. bool is_iram;
  695. void *ptr;
  696. if (phdr->p_type != PT_LOAD || !filesz)
  697. continue;
  698. dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
  699. phdr->p_type, da, memsz, filesz);
  700. if (filesz > memsz) {
  701. dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
  702. filesz, memsz);
  703. ret = -EINVAL;
  704. break;
  705. }
  706. if (offset + filesz > fw->size) {
  707. dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
  708. offset + filesz, fw->size);
  709. ret = -EINVAL;
  710. break;
  711. }
  712. /* grab the kernel address for this device address */
  713. is_iram = phdr->p_flags & PF_X;
  714. ptr = pru_da_to_va(rproc, da, memsz, is_iram);
  715. if (!ptr) {
  716. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  717. ret = -EINVAL;
  718. break;
  719. }
  720. if (pru->data->is_k3) {
  721. ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
  722. filesz);
  723. if (ret) {
  724. dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
  725. da, memsz);
  726. break;
  727. }
  728. } else {
  729. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  730. }
  731. /* skip the memzero logic performed by remoteproc ELF loader */
  732. }
  733. return ret;
  734. }
  735. static const void *
  736. pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
  737. {
  738. struct elf32_shdr *shdr, *name_table_shdr;
  739. const char *name_table;
  740. const u8 *elf_data = fw->data;
  741. struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
  742. u16 shnum = ehdr->e_shnum;
  743. u16 shstrndx = ehdr->e_shstrndx;
  744. int i;
  745. /* first, get the section header */
  746. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  747. /* compute name table section header entry in shdr array */
  748. name_table_shdr = shdr + shstrndx;
  749. /* finally, compute the name table section address in elf */
  750. name_table = elf_data + name_table_shdr->sh_offset;
  751. for (i = 0; i < shnum; i++, shdr++) {
  752. u32 size = shdr->sh_size;
  753. u32 offset = shdr->sh_offset;
  754. u32 name = shdr->sh_name;
  755. if (strcmp(name_table + name, ".pru_irq_map"))
  756. continue;
  757. /* make sure we have the entire irq map */
  758. if (offset + size > fw->size || offset + size < size) {
  759. dev_err(dev, ".pru_irq_map section truncated\n");
  760. return ERR_PTR(-EINVAL);
  761. }
  762. /* make sure irq map has at least the header */
  763. if (sizeof(struct pru_irq_rsc) > size) {
  764. dev_err(dev, "header-less .pru_irq_map section\n");
  765. return ERR_PTR(-EINVAL);
  766. }
  767. return shdr;
  768. }
  769. dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
  770. return NULL;
  771. }
  772. /*
  773. * Use a custom parse_fw callback function for dealing with PRU firmware
  774. * specific sections.
  775. *
  776. * The firmware blob can contain optional ELF sections: .resource_table section
  777. * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
  778. * description, which needs to be setup before powering on the PRU core. To
  779. * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
  780. * firmware linker) and therefore is not loaded to PRU memory.
  781. */
  782. static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
  783. {
  784. struct device *dev = &rproc->dev;
  785. struct pru_rproc *pru = rproc->priv;
  786. const u8 *elf_data = fw->data;
  787. const void *shdr;
  788. u8 class = fw_elf_get_class(fw);
  789. u64 sh_offset;
  790. int ret;
  791. /* load optional rsc table */
  792. ret = rproc_elf_load_rsc_table(rproc, fw);
  793. if (ret == -EINVAL)
  794. dev_dbg(&rproc->dev, "no resource table found for this fw\n");
  795. else if (ret)
  796. return ret;
  797. /* find .pru_interrupt_map section, not having it is not an error */
  798. shdr = pru_rproc_find_interrupt_map(dev, fw);
  799. if (IS_ERR(shdr))
  800. return PTR_ERR(shdr);
  801. if (!shdr)
  802. return 0;
  803. /* preserve pointer to PRU interrupt map together with it size */
  804. sh_offset = elf_shdr_get_sh_offset(class, shdr);
  805. pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
  806. pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
  807. return 0;
  808. }
  809. /*
  810. * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
  811. * always at a particular offset within the PRUSS address space.
  812. */
  813. static int pru_rproc_set_id(struct pru_rproc *pru)
  814. {
  815. int ret = 0;
  816. switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
  817. case TX_PRU0_IRAM_ADDR_MASK:
  818. fallthrough;
  819. case RTU0_IRAM_ADDR_MASK:
  820. fallthrough;
  821. case PRU0_IRAM_ADDR_MASK:
  822. pru->id = PRUSS_PRU0;
  823. break;
  824. case TX_PRU1_IRAM_ADDR_MASK:
  825. fallthrough;
  826. case RTU1_IRAM_ADDR_MASK:
  827. fallthrough;
  828. case PRU1_IRAM_ADDR_MASK:
  829. pru->id = PRUSS_PRU1;
  830. break;
  831. default:
  832. ret = -EINVAL;
  833. }
  834. return ret;
  835. }
  836. static int pru_rproc_probe(struct platform_device *pdev)
  837. {
  838. struct device *dev = &pdev->dev;
  839. struct device_node *np = dev->of_node;
  840. struct platform_device *ppdev = to_platform_device(dev->parent);
  841. struct pru_rproc *pru;
  842. const char *fw_name;
  843. struct rproc *rproc = NULL;
  844. struct resource *res;
  845. int i, ret;
  846. const struct pru_private_data *data;
  847. const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
  848. data = of_device_get_match_data(&pdev->dev);
  849. if (!data)
  850. return -ENODEV;
  851. ret = of_property_read_string(np, "firmware-name", &fw_name);
  852. if (ret) {
  853. dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
  854. return ret;
  855. }
  856. rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
  857. sizeof(*pru));
  858. if (!rproc) {
  859. dev_err(dev, "rproc_alloc failed\n");
  860. return -ENOMEM;
  861. }
  862. /* use a custom load function to deal with PRU-specific quirks */
  863. rproc->ops->load = pru_rproc_load_elf_segments;
  864. /* use a custom parse function to deal with PRU-specific resources */
  865. rproc->ops->parse_fw = pru_rproc_parse_fw;
  866. /* error recovery is not supported for PRUs */
  867. rproc->recovery_disabled = true;
  868. /*
  869. * rproc_add will auto-boot the processor normally, but this is not
  870. * desired with PRU client driven boot-flow methodology. A PRU
  871. * application/client driver will boot the corresponding PRU
  872. * remote-processor as part of its state machine either through the
  873. * remoteproc sysfs interface or through the equivalent kernel API.
  874. */
  875. rproc->auto_boot = false;
  876. pru = rproc->priv;
  877. pru->dev = dev;
  878. pru->data = data;
  879. pru->pruss = platform_get_drvdata(ppdev);
  880. pru->rproc = rproc;
  881. pru->fw_name = fw_name;
  882. pru->client_np = NULL;
  883. spin_lock_init(&pru->rmw_lock);
  884. mutex_init(&pru->lock);
  885. for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
  886. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  887. mem_names[i]);
  888. pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
  889. if (IS_ERR(pru->mem_regions[i].va)) {
  890. dev_err(dev, "failed to parse and map memory resource %d %s\n",
  891. i, mem_names[i]);
  892. ret = PTR_ERR(pru->mem_regions[i].va);
  893. return ret;
  894. }
  895. pru->mem_regions[i].pa = res->start;
  896. pru->mem_regions[i].size = resource_size(res);
  897. dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %p\n",
  898. mem_names[i], &pru->mem_regions[i].pa,
  899. pru->mem_regions[i].size, pru->mem_regions[i].va);
  900. }
  901. ret = pru_rproc_set_id(pru);
  902. if (ret < 0)
  903. return ret;
  904. platform_set_drvdata(pdev, rproc);
  905. ret = devm_rproc_add(dev, pru->rproc);
  906. if (ret) {
  907. dev_err(dev, "rproc_add failed: %d\n", ret);
  908. return ret;
  909. }
  910. pru_rproc_create_debug_entries(rproc);
  911. dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
  912. return 0;
  913. }
  914. static void pru_rproc_remove(struct platform_device *pdev)
  915. {
  916. struct device *dev = &pdev->dev;
  917. struct rproc *rproc = platform_get_drvdata(pdev);
  918. dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
  919. }
  920. static const struct pru_private_data pru_data = {
  921. .type = PRU_TYPE_PRU,
  922. };
  923. static const struct pru_private_data k3_pru_data = {
  924. .type = PRU_TYPE_PRU,
  925. .is_k3 = 1,
  926. };
  927. static const struct pru_private_data k3_rtu_data = {
  928. .type = PRU_TYPE_RTU,
  929. .is_k3 = 1,
  930. };
  931. static const struct pru_private_data k3_tx_pru_data = {
  932. .type = PRU_TYPE_TX_PRU,
  933. .is_k3 = 1,
  934. };
  935. static const struct of_device_id pru_rproc_match[] = {
  936. { .compatible = "ti,am3356-pru", .data = &pru_data },
  937. { .compatible = "ti,am4376-pru", .data = &pru_data },
  938. { .compatible = "ti,am5728-pru", .data = &pru_data },
  939. { .compatible = "ti,am642-pru", .data = &k3_pru_data },
  940. { .compatible = "ti,am642-rtu", .data = &k3_rtu_data },
  941. { .compatible = "ti,am642-tx-pru", .data = &k3_tx_pru_data },
  942. { .compatible = "ti,k2g-pru", .data = &pru_data },
  943. { .compatible = "ti,am654-pru", .data = &k3_pru_data },
  944. { .compatible = "ti,am654-rtu", .data = &k3_rtu_data },
  945. { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data },
  946. { .compatible = "ti,j721e-pru", .data = &k3_pru_data },
  947. { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data },
  948. { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data },
  949. { .compatible = "ti,am625-pru", .data = &k3_pru_data },
  950. {},
  951. };
  952. MODULE_DEVICE_TABLE(of, pru_rproc_match);
  953. static struct platform_driver pru_rproc_driver = {
  954. .driver = {
  955. .name = PRU_RPROC_DRVNAME,
  956. .of_match_table = pru_rproc_match,
  957. .suppress_bind_attrs = true,
  958. },
  959. .probe = pru_rproc_probe,
  960. .remove = pru_rproc_remove,
  961. };
  962. module_platform_driver(pru_rproc_driver);
  963. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
  964. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  965. MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
  966. MODULE_AUTHOR("Puranjay Mohan <p-mohan@ti.com>");
  967. MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>");
  968. MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
  969. MODULE_LICENSE("GPL v2");