pru_rproc.c 25 KB

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