pru_rproc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PRU-ICSS remoteproc driver for various TI SoCs
  4. *
  5. * Copyright (C) 2014-2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Suman Anna <s-anna@ti.com>
  7. * Andrew F. Davis <afd@ti.com>
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mailbox_client.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/omap-mailbox.h>
  16. #include <linux/pruss.h>
  17. #include <linux/pruss_driver.h>
  18. #include <linux/remoteproc.h>
  19. #include "remoteproc_internal.h"
  20. #include "pru_rproc.h"
  21. /* PRU_ICSS_PRU_CTRL registers */
  22. #define PRU_CTRL_CTRL 0x0000
  23. #define PRU_CTRL_STS 0x0004
  24. #define PRU_CTRL_WAKEUP_EN 0x0008
  25. #define PRU_CTRL_CYCLE 0x000C
  26. #define PRU_CTRL_STALL 0x0010
  27. #define PRU_CTRL_CTBIR0 0x0020
  28. #define PRU_CTRL_CTBIR1 0x0024
  29. #define PRU_CTRL_CTPPR0 0x0028
  30. #define PRU_CTRL_CTPPR1 0x002C
  31. /* CTRL register bit-fields */
  32. #define CTRL_CTRL_SOFT_RST_N BIT(0)
  33. #define CTRL_CTRL_EN BIT(1)
  34. #define CTRL_CTRL_SLEEPING BIT(2)
  35. #define CTRL_CTRL_CTR_EN BIT(3)
  36. #define CTRL_CTRL_SINGLE_STEP BIT(8)
  37. #define CTRL_CTRL_RUNSTATE BIT(15)
  38. /* PRU_ICSS_PRU_DEBUG registers */
  39. #define PRU_DEBUG_GPREG(x) (0x0000 + (x) * 4)
  40. #define PRU_DEBUG_CT_REG(x) (0x0080 + (x) * 4)
  41. /* PRU/RTU Core IRAM address masks */
  42. #define PRU0_IRAM_ADDR_MASK 0x34000
  43. #define PRU1_IRAM_ADDR_MASK 0x38000
  44. #define RTU0_IRAM_ADDR_MASK 0x4000
  45. #define RTU1_IRAM_ADDR_MASK 0x6000
  46. #define TX_PRU0_IRAM_ADDR_MASK 0xa000
  47. #define TX_PRU1_IRAM_ADDR_MASK 0xc000
  48. /**
  49. * enum pru_iomem - PRU core memory/register range identifiers
  50. */
  51. enum pru_iomem {
  52. PRU_IOMEM_IRAM = 0,
  53. PRU_IOMEM_CTRL,
  54. PRU_IOMEM_DEBUG,
  55. PRU_IOMEM_MAX,
  56. };
  57. /**
  58. * enum pru_type - PRU core type identifier
  59. */
  60. enum pru_type {
  61. PRU_TYPE_PRU = 0,
  62. PRU_TYPE_RTU,
  63. PRU_TYPE_TX_PRU,
  64. PRU_TYPE_MAX,
  65. };
  66. /**
  67. * struct pru_rproc - PRU remoteproc structure
  68. * @id: id of the PRU core within the PRUSS
  69. * @pruss: back-reference to parent PRUSS structure
  70. * @rproc: remoteproc pointer for this PRU core
  71. * @client_np: client device node
  72. * @mbox: mailbox channel handle used for vring signalling with MPU
  73. * @client: mailbox client to request the mailbox channel
  74. * @type: type of the PRU core (PRU, RTU, Tx_PRU)
  75. * @irq_ring: IRQ number to use for processing vring buffers
  76. * @irq_kick: IRQ number to use to perform virtio kick
  77. * @mem_regions: data for each of the PRU memory regions
  78. * @intc_config: PRU INTC configuration data
  79. * @rmw_lock: lock for read, modify, write operations on registers
  80. * @iram_da: device address of Instruction RAM for this PRU
  81. * @pdram_da: device address of primary Data RAM for this PRU
  82. * @sdram_da: device address of secondary Data RAM for this PRU
  83. * @shrdram_da: device address of shared Data RAM
  84. * @fw_name: name of firmware image used during loading
  85. * @dt_irqs: number of irqs configured from DT
  86. * @gpmux_save: saved value for gpmux config
  87. * @lock: mutex to protect client usage
  88. * @dbg_single_step: debug state variable to set PRU into single step mode
  89. * @dbg_continuous: debug state variable to restore PRU execution mode
  90. * @fw_has_intc_rsc: boolean flag to indicate INTC config through firmware
  91. * @is_k3: boolean flag used to indicate the core has increased number of events
  92. */
  93. struct pru_rproc {
  94. int id;
  95. struct pruss *pruss;
  96. struct rproc *rproc;
  97. struct device_node *client_np;
  98. struct mbox_chan *mbox;
  99. struct mbox_client client;
  100. enum pru_type type;
  101. int irq_vring;
  102. int irq_kick;
  103. struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
  104. struct pruss_intc_config intc_config;
  105. spinlock_t rmw_lock; /* register access lock */
  106. u32 iram_da;
  107. u32 pdram_da;
  108. u32 sdram_da;
  109. u32 shrdram_da;
  110. const char *fw_name;
  111. int dt_irqs;
  112. u8 gpmux_save;
  113. struct mutex lock; /* client access lock */
  114. u32 dbg_single_step;
  115. u32 dbg_continuous;
  116. unsigned int fw_has_intc_rsc : 1;
  117. unsigned int is_k3 : 1;
  118. };
  119. static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, int len);
  120. static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
  121. {
  122. return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
  123. }
  124. static inline
  125. void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
  126. {
  127. writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
  128. }
  129. static inline
  130. void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
  131. u32 mask, u32 set)
  132. {
  133. u32 val;
  134. unsigned long flags;
  135. spin_lock_irqsave(&pru->rmw_lock, flags);
  136. val = pru_control_read_reg(pru, reg);
  137. val &= ~mask;
  138. val |= (set & mask);
  139. pru_control_write_reg(pru, reg, val);
  140. spin_unlock_irqrestore(&pru->rmw_lock, flags);
  141. }
  142. /**
  143. * pru_rproc_set_firmware() - set firmware for a pru core
  144. * @rproc: the rproc instance of the PRU
  145. * @fw_name: the new firmware name, or NULL if default is desired
  146. */
  147. static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
  148. {
  149. struct pru_rproc *pru = rproc->priv;
  150. if (!fw_name)
  151. fw_name = pru->fw_name;
  152. return rproc_set_firmware(rproc, fw_name);
  153. }
  154. static int pru_rproc_intc_dt_config(struct pru_rproc *pru, int index)
  155. {
  156. struct device *dev = &pru->rproc->dev;
  157. struct device_node *np = pru->client_np;
  158. struct property *prop;
  159. const char *prop_name = "ti,pru-interrupt-map";
  160. u8 max_system_events, max_pru_channels, max_pru_host_ints;
  161. int ret = 0, i;
  162. int dt_irqs;
  163. u32 *arr;
  164. bool has_irqs = false;
  165. prop = of_find_property(np, prop_name, NULL);
  166. if (!prop)
  167. return 0;
  168. dt_irqs = of_property_count_u32_elems(np, prop_name);
  169. if (dt_irqs <= 0 || dt_irqs % 4) {
  170. dev_err(dev, "bad interrupt map data %d, expected multiple of 4\n",
  171. dt_irqs);
  172. return -EINVAL;
  173. }
  174. arr = kmalloc_array(dt_irqs, sizeof(u32), GFP_KERNEL);
  175. if (!arr)
  176. return -ENOMEM;
  177. ret = of_property_read_u32_array(np, prop_name, arr, dt_irqs);
  178. if (ret) {
  179. dev_err(dev, "failed to read pru irq map: %d\n", ret);
  180. goto out;
  181. }
  182. max_system_events = pru->is_k3 ?
  183. MAX_PRU_SYS_EVENTS_K3 : MAX_PRU_SYS_EVENTS;
  184. max_pru_channels = pru->is_k3 ? MAX_PRU_CHANNELS_K3 : MAX_PRU_CHANNELS;
  185. max_pru_host_ints = pru->is_k3 ? MAX_PRU_HOST_INT_K3 : MAX_PRU_HOST_INT;
  186. for (i = 0; i < ARRAY_SIZE(pru->intc_config.sysev_to_ch); i++)
  187. pru->intc_config.sysev_to_ch[i] = -1;
  188. for (i = 0; i < ARRAY_SIZE(pru->intc_config.ch_to_host); i++)
  189. pru->intc_config.ch_to_host[i] = -1;
  190. for (i = 0; i < dt_irqs; i += 4) {
  191. if (arr[i] != index)
  192. continue;
  193. if (arr[i + 1] < 0 ||
  194. arr[i + 1] >= max_system_events) {
  195. dev_err(dev, "bad sys event %d\n", arr[i + 1]);
  196. ret = -EINVAL;
  197. goto out;
  198. }
  199. if (arr[i + 2] < 0 ||
  200. arr[i + 2] >= max_pru_channels) {
  201. dev_err(dev, "bad channel %d\n", arr[i + 2]);
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. if (arr[i + 3] < 0 ||
  206. arr[i + 3] >= max_pru_host_ints) {
  207. dev_err(dev, "bad irq %d\n", arr[i + 3]);
  208. ret = -EINVAL;
  209. goto out;
  210. }
  211. pru->intc_config.sysev_to_ch[arr[i + 1]] = arr[i + 2];
  212. dev_dbg(dev, "sysevt-to-ch[%d] -> %d\n", arr[i + 1],
  213. arr[i + 2]);
  214. pru->intc_config.ch_to_host[arr[i + 2]] = arr[i + 3];
  215. dev_dbg(dev, "chnl-to-host[%d] -> %d\n", arr[i + 2],
  216. arr[i + 3]);
  217. has_irqs = true;
  218. }
  219. /*
  220. * The property "ti,pru-interrupt-map" is used in a consumer node, but
  221. * need not necessarily have data for all referenced PRUs. Provide a
  222. * fallback to get the interrupt data from firmware for PRUs ith no
  223. * interrupt data.
  224. */
  225. if (!has_irqs) {
  226. dev_dbg(dev, "no DT irqs, falling back to firmware intc rsc mode\n");
  227. goto out;
  228. }
  229. pru->dt_irqs = dt_irqs;
  230. ret = pruss_intc_configure(pru->pruss, &pru->intc_config);
  231. if (ret) {
  232. dev_err(dev, "failed to configure intc %d\n", ret);
  233. pru->dt_irqs = 0;
  234. }
  235. out:
  236. kfree(arr);
  237. return ret;
  238. }
  239. static struct rproc *__pru_rproc_get(struct device_node *np, int index)
  240. {
  241. struct device_node *rproc_np = NULL;
  242. struct platform_device *pdev;
  243. struct rproc *rproc;
  244. rproc_np = of_parse_phandle(np, "prus", index);
  245. if (!rproc_np || !of_device_is_available(rproc_np))
  246. return ERR_PTR(-ENODEV);
  247. pdev = of_find_device_by_node(rproc_np);
  248. of_node_put(rproc_np);
  249. if (!pdev)
  250. /* probably PRU not yet probed */
  251. return ERR_PTR(-EPROBE_DEFER);
  252. /* TODO: replace the crude string based check to make sure it is PRU */
  253. if (!strstr(dev_name(&pdev->dev), "pru") &&
  254. !strstr(dev_name(&pdev->dev), "rtu")) {
  255. put_device(&pdev->dev);
  256. return ERR_PTR(-ENODEV);
  257. }
  258. rproc = platform_get_drvdata(pdev);
  259. put_device(&pdev->dev);
  260. if (!rproc)
  261. return ERR_PTR(-EPROBE_DEFER);
  262. get_device(&rproc->dev);
  263. return rproc;
  264. }
  265. /**
  266. * pru_rproc_get() - get the PRU rproc instance from a device node
  267. * @np: the user/client device node
  268. * @index: index to use for the prus property
  269. *
  270. * This function looks through a client device node's "prus" property at index
  271. * @index and returns the rproc handle for a valid PRU remote processor if
  272. * found. The function allows only one user to own the PRU rproc resource at
  273. * a time. Caller must call pru_rproc_put() when done with using the rproc,
  274. * not required if the function returns a failure.
  275. *
  276. * Returns the rproc handle on success, and an ERR_PTR on failure using one
  277. * of the following error values
  278. * -ENODEV if device is not found
  279. * -EBUSY if PRU is already acquired by anyone
  280. * -EPROBE_DEFER is PRU device is not probed yet
  281. */
  282. struct rproc *pru_rproc_get(struct device_node *np, int index)
  283. {
  284. struct rproc *rproc;
  285. struct pru_rproc *pru;
  286. const char *fw_name;
  287. struct device *dev;
  288. int ret;
  289. u32 mux;
  290. rproc = __pru_rproc_get(np, index);
  291. if (IS_ERR(rproc))
  292. return rproc;
  293. pru = rproc->priv;
  294. dev = &rproc->dev;
  295. mutex_lock(&pru->lock);
  296. if (pru->client_np) {
  297. mutex_unlock(&pru->lock);
  298. put_device(&rproc->dev);
  299. return ERR_PTR(-EBUSY);
  300. }
  301. pru->client_np = np;
  302. rproc->deny_sysfs_ops = 1;
  303. mutex_unlock(&pru->lock);
  304. ret = pruss_cfg_get_gpmux(pru->pruss, pru->id, &pru->gpmux_save);
  305. if (ret) {
  306. dev_err(dev, "failed to get cfg gpmux: %d\n", ret);
  307. goto err;
  308. }
  309. ret = of_property_read_u32_index(np, "ti,pruss-gp-mux-sel", index,
  310. &mux);
  311. if (!ret) {
  312. ret = pruss_cfg_set_gpmux(pru->pruss, pru->id, mux);
  313. if (ret) {
  314. dev_err(dev, "failed to set cfg gpmux: %d\n", ret);
  315. goto err;
  316. }
  317. }
  318. ret = of_property_read_string_index(np, "firmware-name", index,
  319. &fw_name);
  320. if (!ret) {
  321. ret = pru_rproc_set_firmware(rproc, fw_name);
  322. if (ret) {
  323. dev_err(dev, "failed to set firmware: %d\n", ret);
  324. goto err;
  325. }
  326. }
  327. ret = pru_rproc_intc_dt_config(pru, index);
  328. if (ret)
  329. goto err;
  330. return rproc;
  331. err:
  332. pru_rproc_put(rproc);
  333. return ERR_PTR(ret);
  334. }
  335. EXPORT_SYMBOL_GPL(pru_rproc_get);
  336. /**
  337. * pru_rproc_put() - release the PRU rproc resource
  338. * @rproc: the rproc resource to release
  339. *
  340. * Releases the PRU rproc resource and makes it available to other
  341. * users.
  342. */
  343. void pru_rproc_put(struct rproc *rproc)
  344. {
  345. struct pru_rproc *pru;
  346. if (IS_ERR_OR_NULL(rproc))
  347. return;
  348. /* TODO: replace the crude string based check to make sure it is PRU */
  349. if (!strstr(dev_name(rproc->dev.parent), "pru") &&
  350. !strstr(dev_name(rproc->dev.parent), "rtu"))
  351. return;
  352. pru = rproc->priv;
  353. if (!pru->client_np)
  354. return;
  355. pruss_cfg_set_gpmux(pru->pruss, pru->id, pru->gpmux_save);
  356. if (pru->dt_irqs)
  357. pruss_intc_unconfigure(pru->pruss, &pru->intc_config);
  358. pru_rproc_set_firmware(rproc, NULL);
  359. mutex_lock(&pru->lock);
  360. pru->client_np = NULL;
  361. rproc->deny_sysfs_ops = 0;
  362. mutex_unlock(&pru->lock);
  363. put_device(&rproc->dev);
  364. }
  365. EXPORT_SYMBOL_GPL(pru_rproc_put);
  366. /**
  367. * pru_rproc_get_id() - get PRU id from a previously acquired PRU remoteproc
  368. * @rproc: the rproc instance of the PRU
  369. *
  370. * Returns the PRU id of the PRU remote processor that has been acquired through
  371. * a pru_rproc_get(), or a negative value on error
  372. */
  373. enum pruss_pru_id pru_rproc_get_id(struct rproc *rproc)
  374. {
  375. struct pru_rproc *pru;
  376. if (IS_ERR_OR_NULL(rproc) || !rproc->dev.parent)
  377. return -EINVAL;
  378. /* TODO: replace the crude string based check to make sure it is PRU */
  379. if (!strstr(dev_name(rproc->dev.parent), "pru") &&
  380. !strstr(dev_name(rproc->dev.parent), "rtu"))
  381. return -EINVAL;
  382. pru = rproc->priv;
  383. return pru->id;
  384. }
  385. EXPORT_SYMBOL_GPL(pru_rproc_get_id);
  386. /**
  387. * pru_rproc_set_ctable() - set the constant table index for the PRU
  388. * @rproc: the rproc instance of the PRU
  389. * @c: constant table index to set
  390. * @addr: physical address to set it to
  391. */
  392. int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
  393. {
  394. struct pru_rproc *pru = rproc->priv;
  395. unsigned int reg;
  396. u32 mask, set;
  397. u16 idx;
  398. u16 idx_mask;
  399. /* pointer is 16 bit and index is 8-bit so mask out the rest */
  400. idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
  401. /* ctable uses bit 8 and upwards only */
  402. idx = (addr >> 8) & idx_mask;
  403. /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
  404. reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
  405. mask = idx_mask << (16 * (c & 1));
  406. set = idx << (16 * (c & 1));
  407. pru_control_set_reg(pru, reg, mask, set);
  408. return 0;
  409. }
  410. EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
  411. static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
  412. {
  413. return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
  414. }
  415. static inline
  416. void pru_debug_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
  417. {
  418. writel_relaxed(val, pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
  419. }
  420. static int regs_show(struct seq_file *s, void *data)
  421. {
  422. struct rproc *rproc = s->private;
  423. struct pru_rproc *pru = rproc->priv;
  424. int i, nregs = 32;
  425. u32 pru_sts;
  426. int pru_is_running;
  427. seq_puts(s, "============== Control Registers ==============\n");
  428. seq_printf(s, "CTRL := 0x%08x\n",
  429. pru_control_read_reg(pru, PRU_CTRL_CTRL));
  430. pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
  431. seq_printf(s, "STS (PC) := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
  432. seq_printf(s, "WAKEUP_EN := 0x%08x\n",
  433. pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
  434. seq_printf(s, "CYCLE := 0x%08x\n",
  435. pru_control_read_reg(pru, PRU_CTRL_CYCLE));
  436. seq_printf(s, "STALL := 0x%08x\n",
  437. pru_control_read_reg(pru, PRU_CTRL_STALL));
  438. seq_printf(s, "CTBIR0 := 0x%08x\n",
  439. pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
  440. seq_printf(s, "CTBIR1 := 0x%08x\n",
  441. pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
  442. seq_printf(s, "CTPPR0 := 0x%08x\n",
  443. pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
  444. seq_printf(s, "CTPPR1 := 0x%08x\n",
  445. pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
  446. seq_puts(s, "=============== Debug Registers ===============\n");
  447. pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
  448. CTRL_CTRL_RUNSTATE;
  449. if (pru_is_running) {
  450. seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
  451. return 0;
  452. }
  453. for (i = 0; i < nregs; i++) {
  454. seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
  455. i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
  456. i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
  457. }
  458. return 0;
  459. }
  460. DEFINE_SHOW_ATTRIBUTE(regs);
  461. /*
  462. * Control PRU single-step mode
  463. *
  464. * This is a debug helper function used for controlling the single-step
  465. * mode of the PRU. The PRU Debug registers are not accessible when the
  466. * PRU is in RUNNING state.
  467. *
  468. * Writing a non-zero value sets the PRU into single-step mode irrespective
  469. * of its previous state. The PRU mode is saved only on the first set into
  470. * a single-step mode. Writing a zero value will restore the PRU into its
  471. * original mode.
  472. */
  473. static int pru_rproc_debug_ss_set(void *data, u64 val)
  474. {
  475. struct rproc *rproc = data;
  476. struct pru_rproc *pru = rproc->priv;
  477. u32 reg_val;
  478. val = val ? 1 : 0;
  479. if (!val && !pru->dbg_single_step)
  480. return 0;
  481. reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
  482. if (val && !pru->dbg_single_step)
  483. pru->dbg_continuous = reg_val;
  484. if (val)
  485. reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
  486. else
  487. reg_val = pru->dbg_continuous;
  488. pru->dbg_single_step = val;
  489. pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
  490. return 0;
  491. }
  492. static int pru_rproc_debug_ss_get(void *data, u64 *val)
  493. {
  494. struct rproc *rproc = data;
  495. struct pru_rproc *pru = rproc->priv;
  496. *val = pru->dbg_single_step;
  497. return 0;
  498. }
  499. DEFINE_SIMPLE_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
  500. pru_rproc_debug_ss_set, "%llu\n");
  501. /*
  502. * Create PRU-specific debugfs entries
  503. *
  504. * The entries are created only if the parent remoteproc debugfs directory
  505. * exists, and will be cleaned up by the remoteproc core.
  506. */
  507. static void pru_rproc_create_debug_entries(struct rproc *rproc)
  508. {
  509. if (!rproc->dbg_dir)
  510. return;
  511. debugfs_create_file("regs", 0400, rproc->dbg_dir,
  512. rproc, &regs_fops);
  513. debugfs_create_file("single_step", 0600, rproc->dbg_dir,
  514. rproc, &pru_rproc_debug_ss_fops);
  515. }
  516. /**
  517. * pru_rproc_mbox_callback() - inbound mailbox message handler
  518. * @client: mailbox client pointer used for requesting the mailbox channel
  519. * @data: mailbox payload
  520. *
  521. * This handler is invoked by omap's mailbox driver whenever a mailbox
  522. * message is received. Usually, the mailbox payload simply contains
  523. * the index of the virtqueue that is kicked by the PRU remote processor,
  524. * and we let remoteproc core handle it.
  525. *
  526. * In addition to virtqueue indices, we might also have some out-of-band
  527. * values that indicates different events. Those values are deliberately
  528. * very big so they don't coincide with virtqueue indices.
  529. */
  530. static void pru_rproc_mbox_callback(struct mbox_client *client, void *data)
  531. {
  532. struct pru_rproc *pru = container_of(client, struct pru_rproc, client);
  533. struct device *dev = &pru->rproc->dev;
  534. u32 msg = to_omap_mbox_msg(data);
  535. dev_dbg(dev, "mbox msg: 0x%x\n", msg);
  536. /* msg contains the index of the triggered vring */
  537. if (rproc_vq_interrupt(pru->rproc, msg) == IRQ_NONE)
  538. dev_dbg(dev, "no message was found in vqid %d\n", msg);
  539. }
  540. /**
  541. * pru_rproc_vring_interrupt() - interrupt handler for processing vrings
  542. * @irq: irq number associated with the PRU event MPU is listening on
  543. * @data: interrupt handler data, will be a PRU rproc structure
  544. *
  545. * This handler is used by the PRU remoteproc driver when using PRU system
  546. * events for processing the virtqueues. Unlike the mailbox IP, there is
  547. * no payload associated with an interrupt, so either a unique event is
  548. * used for each virtqueue kick, or a both virtqueues are processed on
  549. * a single event. The latter is chosen to conserve the usable PRU system
  550. * events.
  551. */
  552. static irqreturn_t pru_rproc_vring_interrupt(int irq, void *data)
  553. {
  554. struct pru_rproc *pru = data;
  555. dev_dbg(&pru->rproc->dev, "got vring irq\n");
  556. /* process incoming buffers on both the Rx and Tx vrings */
  557. rproc_vq_interrupt(pru->rproc, 0);
  558. rproc_vq_interrupt(pru->rproc, 1);
  559. return IRQ_HANDLED;
  560. }
  561. /* kick a virtqueue */
  562. static void pru_rproc_kick(struct rproc *rproc, int vq_id)
  563. {
  564. struct device *dev = &rproc->dev;
  565. struct pru_rproc *pru = rproc->priv;
  566. int ret;
  567. mbox_msg_t msg = (mbox_msg_t)vq_id;
  568. const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
  569. dev_dbg(dev, "kicking vqid %d on %s%d\n", vq_id,
  570. names[pru->type], pru->id);
  571. if (pru->irq_kick > 0) {
  572. ret = pruss_intc_trigger(pru->irq_kick);
  573. if (ret < 0)
  574. dev_err(dev, "pruss_intc_trigger failed: %d\n", ret);
  575. } else if (pru->mbox) {
  576. /*
  577. * send the index of the triggered virtqueue in the mailbox
  578. * payload
  579. */
  580. ret = mbox_send_message(pru->mbox, (void *)msg);
  581. if (ret < 0)
  582. dev_err(dev, "mbox_send_message failed: %d\n", ret);
  583. }
  584. }
  585. /* start a PRU core */
  586. static int pru_rproc_start(struct rproc *rproc)
  587. {
  588. struct device *dev = &rproc->dev;
  589. struct pru_rproc *pru = rproc->priv;
  590. const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
  591. u32 val;
  592. int ret;
  593. dev_dbg(dev, "starting %s%d: entry-point = 0x%x\n",
  594. names[pru->type], pru->id, (rproc->bootaddr >> 2));
  595. if (!list_empty(&pru->rproc->rvdevs)) {
  596. if (!pru->mbox && (pru->irq_vring <= 0 || pru->irq_kick <= 0)) {
  597. dev_err(dev, "virtio vring interrupt mechanisms are not provided\n");
  598. ret = -EINVAL;
  599. goto fail;
  600. }
  601. if (!pru->mbox && pru->irq_vring > 0) {
  602. ret = request_threaded_irq(pru->irq_vring, NULL,
  603. pru_rproc_vring_interrupt,
  604. IRQF_ONESHOT, dev_name(dev),
  605. pru);
  606. if (ret) {
  607. dev_err(dev, "failed to enable vring interrupt, ret = %d\n",
  608. ret);
  609. goto fail;
  610. }
  611. }
  612. }
  613. val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
  614. pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
  615. return 0;
  616. fail:
  617. if (!pru->dt_irqs && pru->fw_has_intc_rsc)
  618. pruss_intc_unconfigure(pru->pruss, &pru->intc_config);
  619. return ret;
  620. }
  621. /* stop/disable a PRU core */
  622. static int pru_rproc_stop(struct rproc *rproc)
  623. {
  624. struct device *dev = &rproc->dev;
  625. struct pru_rproc *pru = rproc->priv;
  626. const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
  627. u32 val;
  628. dev_dbg(dev, "stopping %s%d\n", names[pru->type], pru->id);
  629. val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
  630. val &= ~CTRL_CTRL_EN;
  631. pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
  632. if (!list_empty(&pru->rproc->rvdevs) &&
  633. !pru->mbox && pru->irq_vring > 0)
  634. free_irq(pru->irq_vring, pru);
  635. /* undo INTC config */
  636. if (!pru->dt_irqs && pru->fw_has_intc_rsc)
  637. pruss_intc_unconfigure(pru->pruss, &pru->intc_config);
  638. return 0;
  639. }
  640. /*
  641. * parse the custom PRU interrupt map resource and configure the INTC
  642. * appropriately
  643. */
  644. static int pru_handle_vendor_intrmap(struct rproc *rproc,
  645. struct fw_rsc_vendor *rsc)
  646. {
  647. struct device *dev = rproc->dev.parent;
  648. struct pru_rproc *pru = rproc->priv;
  649. struct pruss *pruss = pru->pruss;
  650. struct pruss_event_chnl *event_chnl_map;
  651. struct fw_rsc_pruss_intrmap *intr_rsc0;
  652. struct fw_rsc_pruss_intrmap_k3 *intr_rsc1;
  653. int i, ret;
  654. u32 event_chnl_map_da, event_chnl_map_size;
  655. s8 sys_evt, chnl, intr_no;
  656. s8 *chnl_host_intr_map;
  657. u8 max_system_events, max_pru_channels, max_pru_host_ints;
  658. if (rsc->u.st.st_ver != 0 && rsc->u.st.st_ver != 1) {
  659. dev_err(dev, "only PRU interrupt resource versions 0 and 1 are supported\n");
  660. return -EINVAL;
  661. }
  662. if (!rsc->u.st.st_ver) {
  663. intr_rsc0 = (struct fw_rsc_pruss_intrmap *)rsc->data;
  664. event_chnl_map_da = intr_rsc0->event_chnl_map_addr;
  665. event_chnl_map_size = intr_rsc0->event_chnl_map_size;
  666. chnl_host_intr_map = intr_rsc0->chnl_host_intr_map;
  667. max_system_events = MAX_PRU_SYS_EVENTS;
  668. max_pru_channels = MAX_PRU_CHANNELS;
  669. max_pru_host_ints = MAX_PRU_HOST_INT;
  670. dev_dbg(dev, "version %d event_chnl_map_size %d event_chnl_map_da 0x%x\n",
  671. rsc->u.st.st_ver, intr_rsc0->event_chnl_map_size,
  672. event_chnl_map_da);
  673. } else {
  674. intr_rsc1 = (struct fw_rsc_pruss_intrmap_k3 *)rsc->data;
  675. event_chnl_map_da = intr_rsc1->event_chnl_map_addr;
  676. event_chnl_map_size = intr_rsc1->event_chnl_map_size;
  677. chnl_host_intr_map = intr_rsc1->chnl_host_intr_map;
  678. max_system_events = MAX_PRU_SYS_EVENTS_K3;
  679. max_pru_channels = MAX_PRU_CHANNELS_K3;
  680. max_pru_host_ints = MAX_PRU_HOST_INT_K3;
  681. dev_dbg(dev, "version %d event_chnl_map_size %d event_chnl_map_da 0x%x\n",
  682. rsc->u.st.st_ver, intr_rsc1->event_chnl_map_size,
  683. event_chnl_map_da);
  684. }
  685. if (event_chnl_map_size < 0 ||
  686. event_chnl_map_size >= max_system_events) {
  687. dev_err(dev, "PRU interrupt resource has more events than present on hardware\n");
  688. return -EINVAL;
  689. }
  690. /*
  691. * XXX: The event_chnl_map_addr mapping is currently a pointer in device
  692. * memory, evaluate if this needs to be directly in firmware file.
  693. */
  694. event_chnl_map = pru_d_da_to_va(pru, event_chnl_map_da,
  695. event_chnl_map_size *
  696. sizeof(*event_chnl_map));
  697. if (!event_chnl_map) {
  698. dev_err(dev, "PRU interrupt resource has inadequate event_chnl_map configuration\n");
  699. return -EINVAL;
  700. }
  701. /* init intc_config to defaults */
  702. for (i = 0; i < ARRAY_SIZE(pru->intc_config.sysev_to_ch); i++)
  703. pru->intc_config.sysev_to_ch[i] = -1;
  704. for (i = 0; i < ARRAY_SIZE(pru->intc_config.ch_to_host); i++)
  705. pru->intc_config.ch_to_host[i] = -1;
  706. /* parse and fill in system event to interrupt channel mapping */
  707. for (i = 0; i < event_chnl_map_size; i++) {
  708. sys_evt = event_chnl_map[i].event;
  709. chnl = event_chnl_map[i].chnl;
  710. if (sys_evt < 0 || sys_evt >= max_system_events) {
  711. dev_err(dev, "[%d] bad sys event %d\n", i, sys_evt);
  712. return -EINVAL;
  713. }
  714. if (chnl < 0 || chnl >= max_pru_channels) {
  715. dev_err(dev, "[%d] bad channel value %d\n", i, chnl);
  716. return -EINVAL;
  717. }
  718. pru->intc_config.sysev_to_ch[sys_evt] = chnl;
  719. dev_dbg(dev, "sysevt-to-ch[%d] -> %d\n", sys_evt, chnl);
  720. }
  721. /* parse and handle interrupt channel-to-host interrupt mapping */
  722. for (i = 0; i < max_pru_channels; i++) {
  723. intr_no = chnl_host_intr_map[i];
  724. if (intr_no < 0) {
  725. dev_dbg(dev, "skip intr mapping for chnl %d\n", i);
  726. continue;
  727. }
  728. if (intr_no >= max_pru_host_ints) {
  729. dev_err(dev, "bad intr mapping for chnl %d, intr_no %d\n",
  730. i, intr_no);
  731. return -EINVAL;
  732. }
  733. pru->intc_config.ch_to_host[i] = intr_no;
  734. dev_dbg(dev, "chnl-to-host[%d] -> %d\n", i, intr_no);
  735. }
  736. pru->fw_has_intc_rsc = 1;
  737. ret = pruss_intc_configure(pruss, &pru->intc_config);
  738. if (ret)
  739. dev_err(dev, "failed to configure pruss intc %d\n", ret);
  740. return ret;
  741. }
  742. /* PRU-specific vendor resource handler */
  743. static int pru_rproc_handle_vendor_rsc(struct rproc *rproc,
  744. struct fw_rsc_vendor *rsc)
  745. {
  746. struct device *dev = rproc->dev.parent;
  747. struct pru_rproc *pru = rproc->priv;
  748. int ret = 0;
  749. switch (rsc->u.st.st_type) {
  750. case PRUSS_RSC_INTRS:
  751. if (!pru->dt_irqs)
  752. ret = pru_handle_vendor_intrmap(rproc, rsc);
  753. break;
  754. default:
  755. dev_err(dev, "%s: cannot handle unknown type %d\n", __func__,
  756. rsc->u.st.st_type);
  757. ret = -EINVAL;
  758. }
  759. return ret;
  760. }
  761. /*
  762. * Convert PRU device address (data spaces only) to kernel virtual address
  763. *
  764. * Each PRU has access to all data memories within the PRUSS, accessible at
  765. * different ranges. So, look through both its primary and secondary Data
  766. * RAMs as well as any shared Data RAM to convert a PRU device address to
  767. * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
  768. * RAM1 is primary Data RAM for PRU1.
  769. */
  770. static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, int len)
  771. {
  772. struct pruss_mem_region dram0, dram1, shrd_ram;
  773. struct pruss *pruss = pru->pruss;
  774. u32 offset;
  775. void *va = NULL;
  776. if (len <= 0)
  777. return NULL;
  778. dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
  779. dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
  780. /* PRU1 has its local RAM addresses reversed */
  781. if (pru->id == 1)
  782. swap(dram0, dram1);
  783. shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
  784. if (da >= pru->pdram_da && da + len <= pru->pdram_da + dram0.size) {
  785. offset = da - pru->pdram_da;
  786. va = (__force void *)(dram0.va + offset);
  787. } else if (da >= pru->sdram_da &&
  788. da + len <= pru->sdram_da + dram1.size) {
  789. offset = da - pru->sdram_da;
  790. va = (__force void *)(dram1.va + offset);
  791. } else if (da >= pru->shrdram_da &&
  792. da + len <= pru->shrdram_da + shrd_ram.size) {
  793. offset = da - pru->shrdram_da;
  794. va = (__force void *)(shrd_ram.va + offset);
  795. }
  796. return va;
  797. }
  798. /*
  799. * Convert PRU device address (instruction space) to kernel virtual address
  800. *
  801. * A PRU does not have an unified address space. Each PRU has its very own
  802. * private Instruction RAM, and its device address is identical to that of
  803. * its primary Data RAM device address.
  804. */
  805. static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, int len)
  806. {
  807. u32 offset;
  808. void *va = NULL;
  809. if (len <= 0)
  810. return NULL;
  811. if (da >= pru->iram_da &&
  812. da + len <= pru->iram_da + pru->mem_regions[PRU_IOMEM_IRAM].size) {
  813. offset = da - pru->iram_da;
  814. va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
  815. offset);
  816. }
  817. return va;
  818. }
  819. /* PRU-specific address translator */
  820. static void *pru_da_to_va(struct rproc *rproc, u64 da, int len, u32 flags)
  821. {
  822. struct pru_rproc *pru = rproc->priv;
  823. void *va;
  824. u32 exec_flag;
  825. exec_flag = ((flags & RPROC_FLAGS_ELF_SHDR) ? flags & SHF_EXECINSTR :
  826. ((flags & RPROC_FLAGS_ELF_PHDR) ? flags & PF_X : 0));
  827. if (exec_flag)
  828. va = pru_i_da_to_va(pru, da, len);
  829. else
  830. va = pru_d_da_to_va(pru, da, len);
  831. return va;
  832. }
  833. static struct rproc_ops pru_rproc_ops = {
  834. .start = pru_rproc_start,
  835. .stop = pru_rproc_stop,
  836. .kick = pru_rproc_kick,
  837. .handle_vendor_rsc = pru_rproc_handle_vendor_rsc,
  838. .da_to_va = pru_da_to_va,
  839. };
  840. /*
  841. * Custom memory copy implementation for ICSSG PRU/RTU Cores
  842. *
  843. * The ICSSG PRU/RTU cores have a memory copying issue with IRAM memories, that
  844. * is not seen on previous generation SoCs. The data is reflected properly in
  845. * the IRAM memories only for integer (4-byte) copies. Any unaligned copies
  846. * result in all the other pre-existing bytes zeroed out within that 4-byte
  847. * boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
  848. * IRAM memory port interface does not allow any 8-byte copies (as commonly
  849. * used by ARM64 memcpy implementation) and throws an exception. The DRAM
  850. * memory ports do not show this behavior. Use this custom copying function
  851. * to properly load the PRU/RTU firmware images on all memories for simplicity.
  852. *
  853. * TODO: Improve the function to deal with additional corner cases like
  854. * unaligned copy sizes or sub-integer trailing bytes when the need arises.
  855. */
  856. static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
  857. {
  858. const int *s = src;
  859. int *d = dest;
  860. int size = count / 4;
  861. int *tmp_src = NULL;
  862. /* limited to 4-byte aligned addresses and copy sizes */
  863. if ((long)dest % 4 || count % 4)
  864. return -EINVAL;
  865. /* src offsets in ELF firmware image can be non-aligned */
  866. if ((long)src % 4) {
  867. tmp_src = kmemdup(src, count, GFP_KERNEL);
  868. if (!tmp_src)
  869. return -ENOMEM;
  870. s = tmp_src;
  871. }
  872. while (size--)
  873. *d++ = *s++;
  874. kfree(tmp_src);
  875. return 0;
  876. }
  877. static int
  878. pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
  879. {
  880. struct device *dev = &rproc->dev;
  881. struct elf32_hdr *ehdr;
  882. struct elf32_phdr *phdr;
  883. int i, ret = 0;
  884. const u8 *elf_data = fw->data;
  885. ehdr = (struct elf32_hdr *)elf_data;
  886. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  887. /* go through the available ELF segments */
  888. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  889. u32 da = phdr->p_paddr;
  890. u32 memsz = phdr->p_memsz;
  891. u32 filesz = phdr->p_filesz;
  892. u32 offset = phdr->p_offset;
  893. void *ptr;
  894. if (phdr->p_type != PT_LOAD)
  895. continue;
  896. dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
  897. phdr->p_type, da, memsz, filesz);
  898. if (filesz > memsz) {
  899. dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
  900. filesz, memsz);
  901. ret = -EINVAL;
  902. break;
  903. }
  904. if (offset + filesz > fw->size) {
  905. dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
  906. offset + filesz, fw->size);
  907. ret = -EINVAL;
  908. break;
  909. }
  910. /* grab the kernel address for this device address */
  911. ptr = rproc_da_to_va(rproc, da, memsz,
  912. RPROC_FLAGS_ELF_PHDR | phdr->p_flags);
  913. if (!ptr) {
  914. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  915. ret = -EINVAL;
  916. break;
  917. }
  918. /* skip the memzero logic performed by remoteproc ELF loader */
  919. if (!phdr->p_filesz)
  920. continue;
  921. ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset, filesz);
  922. if (ret) {
  923. dev_err(dev, "PRU custom memory copy failed for da 0x%x memsz 0x%x\n",
  924. da, memsz);
  925. break;
  926. }
  927. }
  928. return ret;
  929. }
  930. /*
  931. * compute PRU id based on the IRAM addresses. The PRU IRAMs are
  932. * always at a particular offset within the PRUSS address space.
  933. * The other alternative is to use static data for each core (not a
  934. * hardware property to define it in DT), and the id can always be
  935. * computated using this inherent address logic.
  936. */
  937. static int pru_rproc_set_id(struct device_node *np, struct pru_rproc *pru)
  938. {
  939. int ret = 0;
  940. u32 mask1 = PRU0_IRAM_ADDR_MASK;
  941. u32 mask2 = PRU1_IRAM_ADDR_MASK;
  942. if (of_device_is_compatible(np, "ti,am654-rtu") ||
  943. of_device_is_compatible(np, "ti,j721e-rtu")) {
  944. mask1 = RTU0_IRAM_ADDR_MASK;
  945. mask2 = RTU1_IRAM_ADDR_MASK;
  946. pru->type = PRU_TYPE_RTU;
  947. }
  948. if (of_device_is_compatible(np, "ti,j721e-tx-pru")) {
  949. mask1 = TX_PRU0_IRAM_ADDR_MASK;
  950. mask2 = TX_PRU1_IRAM_ADDR_MASK;
  951. pru->type = PRU_TYPE_TX_PRU;
  952. }
  953. if ((pru->mem_regions[PRU_IOMEM_IRAM].pa & mask2) == mask2)
  954. pru->id = PRUSS_PRU1;
  955. else if ((pru->mem_regions[PRU_IOMEM_IRAM].pa & mask1) == mask1)
  956. pru->id = PRUSS_PRU0;
  957. else
  958. ret = -EINVAL;
  959. return ret;
  960. }
  961. static int pru_rproc_probe(struct platform_device *pdev)
  962. {
  963. struct device *dev = &pdev->dev;
  964. struct device_node *np = dev->of_node;
  965. struct platform_device *ppdev = to_platform_device(dev->parent);
  966. struct pru_rproc *pru;
  967. const char *fw_name;
  968. struct rproc *rproc = NULL;
  969. struct mbox_client *client;
  970. struct resource *res;
  971. int i, ret;
  972. const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
  973. if (!np) {
  974. dev_err(dev, "Non-DT platform device not supported\n");
  975. return -ENODEV;
  976. }
  977. ret = of_property_read_string(np, "firmware-name", &fw_name);
  978. if (ret) {
  979. dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
  980. return ret;
  981. }
  982. rproc = rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
  983. sizeof(*pru));
  984. if (!rproc) {
  985. dev_err(dev, "rproc_alloc failed\n");
  986. return -ENOMEM;
  987. }
  988. /* error recovery is not supported for PRUs */
  989. rproc->recovery_disabled = true;
  990. /*
  991. * rproc_add will auto-boot the processor normally, but this is
  992. * not desired with PRU client driven boot-flow methodology. A PRU
  993. * application/client driver will boot the corresponding PRU
  994. * remote-processor as part of its state machine either through
  995. * the remoteproc sysfs interface or through the equivalent kernel API
  996. */
  997. rproc->auto_boot = false;
  998. pru = rproc->priv;
  999. pru->pruss = platform_get_drvdata(ppdev);
  1000. pru->rproc = rproc;
  1001. pru->fw_name = fw_name;
  1002. spin_lock_init(&pru->rmw_lock);
  1003. mutex_init(&pru->lock);
  1004. if (of_device_is_compatible(np, "ti,am654-pru") ||
  1005. of_device_is_compatible(np, "ti,am654-rtu") ||
  1006. of_device_is_compatible(np, "ti,j721e-pru") ||
  1007. of_device_is_compatible(np, "ti,j721e-rtu") ||
  1008. of_device_is_compatible(np, "ti,j721e-tx-pru")) {
  1009. /* use generic elf ops for undefined platform driver ops */
  1010. rproc->ops->load = pru_rproc_load_elf_segments;
  1011. pru->is_k3 = 1;
  1012. }
  1013. /* XXX: get this from match data if different in the future */
  1014. pru->iram_da = 0;
  1015. pru->pdram_da = 0;
  1016. pru->sdram_da = 0x2000;
  1017. pru->shrdram_da = 0x10000;
  1018. for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
  1019. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1020. mem_names[i]);
  1021. pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
  1022. if (IS_ERR(pru->mem_regions[i].va)) {
  1023. dev_err(dev, "failed to parse and map memory resource %d %s\n",
  1024. i, mem_names[i]);
  1025. ret = PTR_ERR(pru->mem_regions[i].va);
  1026. goto free_rproc;
  1027. }
  1028. pru->mem_regions[i].pa = res->start;
  1029. pru->mem_regions[i].size = resource_size(res);
  1030. dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %p\n",
  1031. mem_names[i], &pru->mem_regions[i].pa,
  1032. pru->mem_regions[i].size, pru->mem_regions[i].va);
  1033. }
  1034. ret = pru_rproc_set_id(np, pru);
  1035. if (ret < 0)
  1036. goto free_rproc;
  1037. platform_set_drvdata(pdev, rproc);
  1038. /* get optional vring and kick interrupts for supporting virtio rpmsg */
  1039. pru->irq_vring = platform_get_irq_byname(pdev, "vring");
  1040. if (pru->irq_vring <= 0) {
  1041. ret = pru->irq_vring;
  1042. if (ret == -EPROBE_DEFER)
  1043. goto free_rproc;
  1044. dev_dbg(dev, "unable to get vring interrupt, status = %d\n",
  1045. ret);
  1046. }
  1047. pru->irq_kick = platform_get_irq_byname(pdev, "kick");
  1048. if (pru->irq_kick <= 0) {
  1049. ret = pru->irq_kick;
  1050. if (ret == -EPROBE_DEFER)
  1051. goto free_rproc;
  1052. dev_dbg(dev, "unable to get kick interrupt, status = %d\n",
  1053. ret);
  1054. }
  1055. /*
  1056. * get optional mailbox for virtio rpmsg signalling if vring and kick
  1057. * interrupts are not specified for OMAP architecture based SoCs
  1058. */
  1059. if (pru->irq_vring <= 0 && pru->irq_kick <= 0 &&
  1060. !of_device_is_compatible(np, "ti,k2g-pru")) {
  1061. client = &pru->client;
  1062. client->dev = dev;
  1063. client->tx_done = NULL;
  1064. client->rx_callback = pru_rproc_mbox_callback;
  1065. client->tx_block = false;
  1066. client->knows_txdone = false;
  1067. pru->mbox = mbox_request_channel(client, 0);
  1068. if (IS_ERR(pru->mbox)) {
  1069. ret = PTR_ERR(pru->mbox);
  1070. pru->mbox = NULL;
  1071. dev_dbg(dev, "unable to get mailbox channel, status = %d\n",
  1072. ret);
  1073. }
  1074. }
  1075. ret = rproc_add(pru->rproc);
  1076. if (ret) {
  1077. dev_err(dev, "rproc_add failed: %d\n", ret);
  1078. goto put_mbox;
  1079. }
  1080. pru_rproc_create_debug_entries(rproc);
  1081. dev_info(dev, "PRU rproc node %s probed successfully\n", np->full_name);
  1082. return 0;
  1083. put_mbox:
  1084. mbox_free_channel(pru->mbox);
  1085. free_rproc:
  1086. rproc_free(rproc);
  1087. return ret;
  1088. }
  1089. static int pru_rproc_remove(struct platform_device *pdev)
  1090. {
  1091. struct device *dev = &pdev->dev;
  1092. struct rproc *rproc = platform_get_drvdata(pdev);
  1093. struct pru_rproc *pru = rproc->priv;
  1094. dev_info(dev, "%s: removing rproc %s\n", __func__, rproc->name);
  1095. mbox_free_channel(pru->mbox);
  1096. rproc_del(rproc);
  1097. rproc_free(rproc);
  1098. return 0;
  1099. }
  1100. static const struct of_device_id pru_rproc_match[] = {
  1101. { .compatible = "ti,am3356-pru", },
  1102. { .compatible = "ti,am4376-pru", },
  1103. { .compatible = "ti,am5728-pru", },
  1104. { .compatible = "ti,k2g-pru", },
  1105. { .compatible = "ti,am654-pru", },
  1106. { .compatible = "ti,am654-rtu", },
  1107. { .compatible = "ti,j721e-pru", },
  1108. { .compatible = "ti,j721e-rtu", },
  1109. { .compatible = "ti,j721e-tx-pru", },
  1110. {},
  1111. };
  1112. MODULE_DEVICE_TABLE(of, pru_rproc_match);
  1113. static struct platform_driver pru_rproc_driver = {
  1114. .driver = {
  1115. .name = "pru-rproc",
  1116. .of_match_table = pru_rproc_match,
  1117. .suppress_bind_attrs = true,
  1118. },
  1119. .probe = pru_rproc_probe,
  1120. .remove = pru_rproc_remove,
  1121. };
  1122. module_platform_driver(pru_rproc_driver);
  1123. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
  1124. MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
  1125. MODULE_LICENSE("GPL v2");