omap_remoteproc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OMAP Remote Processor driver
  4. *
  5. * Copyright (C) 2011-2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Copyright (C) 2011 Google, Inc.
  7. *
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. * Brian Swetland <swetland@google.com>
  10. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  11. * Mark Grosen <mgrosen@ti.com>
  12. * Suman Anna <s-anna@ti.com>
  13. * Hari Kanigeri <h-kanigeri2@ti.com>
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_reserved_mem.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/remoteproc.h>
  27. #include <linux/mailbox_client.h>
  28. #include <linux/omap-mailbox.h>
  29. #include <linux/omap-iommu.h>
  30. #include <linux/regmap.h>
  31. #include <linux/mfd/syscon.h>
  32. #include <clocksource/timer-ti-dm.h>
  33. #include <linux/platform_data/remoteproc-omap.h>
  34. #include <linux/platform_data/dmtimer-omap.h>
  35. #include "omap_remoteproc.h"
  36. #include "remoteproc_internal.h"
  37. #define OMAP_RPROC_DSP_LOCAL_MEM_OFFSET (0x00800000)
  38. #define OMAP_RPROC_IPU_L2RAM_DEV_ADDR (0x20000000)
  39. /* default auto-suspend delay (ms) */
  40. #define DEFAULT_AUTOSUSPEND_DELAY 10000
  41. /**
  42. * struct omap_rproc_boot_data - boot data structure for the DSP omap rprocs
  43. * @syscon: regmap handle for the system control configuration module
  44. * @boot_reg: boot register offset within the @syscon regmap
  45. * @boot_reg_shift: bit-field shift required for the boot address value in
  46. * @boot_reg
  47. */
  48. struct omap_rproc_boot_data {
  49. struct regmap *syscon;
  50. unsigned int boot_reg;
  51. unsigned int boot_reg_shift;
  52. };
  53. /**
  54. * struct omap_rproc_mem - internal memory structure
  55. * @cpu_addr: MPU virtual address of the memory region
  56. * @bus_addr: bus address used to access the memory region
  57. * @dev_addr: device address of the memory region from DSP view
  58. * @size: size of the memory region
  59. */
  60. struct omap_rproc_mem {
  61. void __iomem *cpu_addr;
  62. phys_addr_t bus_addr;
  63. u32 dev_addr;
  64. size_t size;
  65. };
  66. /**
  67. * struct omap_rproc_timer - data structure for a timer used by a omap rproc
  68. * @odt: timer pointer
  69. * @timer_ops: OMAP dmtimer ops for @odt timer
  70. * @irq: timer irq
  71. */
  72. struct omap_rproc_timer {
  73. struct omap_dm_timer *odt;
  74. const struct omap_dm_timer_ops *timer_ops;
  75. int irq;
  76. };
  77. /**
  78. * struct omap_rproc - omap remote processor state
  79. * @mbox: mailbox channel handle
  80. * @client: mailbox client to request the mailbox channel
  81. * @boot_data: boot data structure for setting processor boot address
  82. * @mem: internal memory regions data
  83. * @num_mems: number of internal memory regions
  84. * @num_timers: number of rproc timer(s)
  85. * @num_wd_timers: number of rproc watchdog timers
  86. * @timers: timer(s) info used by rproc
  87. * @autosuspend_delay: auto-suspend delay value to be used for runtime pm
  88. * @need_resume: if true a resume is needed in the system resume callback
  89. * @rproc: rproc handle
  90. * @pm_comp: completion primitive to sync for suspend response
  91. * @standby_addr: kernel address of the register having module standby status
  92. * @suspend_acked: state machine flag to store the suspend request ack
  93. */
  94. struct omap_rproc {
  95. struct mbox_chan *mbox;
  96. struct mbox_client client;
  97. struct omap_rproc_boot_data *boot_data;
  98. struct omap_rproc_mem *mem;
  99. int num_mems;
  100. int num_timers;
  101. int num_wd_timers;
  102. struct omap_rproc_timer *timers;
  103. int autosuspend_delay;
  104. bool need_resume;
  105. struct rproc *rproc;
  106. struct completion pm_comp;
  107. void __iomem *standby_addr;
  108. bool suspend_acked;
  109. };
  110. /**
  111. * struct omap_rproc_dev_data - device data for the omap remote processor
  112. * @device_name: device name of the remote processor
  113. * @fw_name: firmware name to use
  114. * @autosuspend_delay: custom auto-suspend delay value in milliseconds
  115. */
  116. struct omap_rproc_dev_data {
  117. const char *device_name;
  118. const char *fw_name;
  119. int autosuspend_delay;
  120. };
  121. /**
  122. * omap_rproc_request_timer - request a timer for a remoteproc
  123. * @np: device node pointer to the desired timer
  124. * @timer: handle to a struct omap_rproc_timer to return the timer handle
  125. *
  126. * This helper function is used primarily to request a timer associated with
  127. * a remoteproc. The returned handle is stored in the .odt field of the
  128. * @timer structure passed in, and is used to invoke other timer specific
  129. * ops (like starting a timer either during device initialization or during
  130. * a resume operation, or for stopping/freeing a timer).
  131. *
  132. * Returns 0 on success, otherwise an appropriate failure
  133. */
  134. static int omap_rproc_request_timer(struct device_node *np,
  135. struct omap_rproc_timer *timer)
  136. {
  137. int ret = 0;
  138. timer->odt = timer->timer_ops->request_by_node(np);
  139. if (!timer->odt) {
  140. pr_err("request for timer node %p failed\n", np);
  141. return -EBUSY;
  142. }
  143. ret = timer->timer_ops->set_source(timer->odt, OMAP_TIMER_SRC_SYS_CLK);
  144. if (ret) {
  145. pr_err("error setting OMAP_TIMER_SRC_SYS_CLK as source for timer node %p\n",
  146. np);
  147. timer->timer_ops->free(timer->odt);
  148. return ret;
  149. }
  150. /* clean counter, remoteproc code will set the value */
  151. timer->timer_ops->set_load(timer->odt, 0, 0);
  152. return ret;
  153. }
  154. /**
  155. * omap_rproc_start_timer - start a timer for a remoteproc
  156. * @timer: handle to a OMAP rproc timer
  157. *
  158. * This helper function is used to start a timer associated with a remoteproc,
  159. * obtained using the request_timer ops. The helper function needs to be
  160. * invoked by the driver to start the timer (during device initialization)
  161. * or to just resume the timer.
  162. *
  163. * Returns 0 on success, otherwise a failure as returned by DMTimer ops
  164. */
  165. static inline int omap_rproc_start_timer(struct omap_rproc_timer *timer)
  166. {
  167. return timer->timer_ops->start(timer->odt);
  168. }
  169. /**
  170. * omap_rproc_stop_timer - stop a timer for a remoteproc
  171. * @timer: handle to a OMAP rproc timer
  172. *
  173. * This helper function is used to disable a timer associated with a
  174. * remoteproc, and needs to be called either during a device shutdown
  175. * or suspend operation. The separate helper function allows the driver
  176. * to just stop a timer without having to release the timer during a
  177. * suspend operation.
  178. *
  179. * Returns 0 on success, otherwise a failure as returned by DMTimer ops
  180. */
  181. static inline int omap_rproc_stop_timer(struct omap_rproc_timer *timer)
  182. {
  183. return timer->timer_ops->stop(timer->odt);
  184. }
  185. /**
  186. * omap_rproc_release_timer - release a timer for a remoteproc
  187. * @timer: handle to a OMAP rproc timer
  188. *
  189. * This helper function is used primarily to release a timer associated
  190. * with a remoteproc. The dmtimer will be available for other clients to
  191. * use once released.
  192. *
  193. * Returns 0 on success, otherwise a failure as returned by DMTimer ops
  194. */
  195. static inline int omap_rproc_release_timer(struct omap_rproc_timer *timer)
  196. {
  197. return timer->timer_ops->free(timer->odt);
  198. }
  199. /**
  200. * omap_rproc_get_timer_irq - get the irq for a timer
  201. * @timer - handle to a OMAP rproc timer
  202. *
  203. * This function is used to get the irq associated with a watchdog timer. The
  204. * function is called by the OMAP remoteproc driver to register a interrupt
  205. * handler to handle watchdog events on the remote processor.
  206. *
  207. * Returns the irq id on success, otherwise a failure as returned by DMTimer ops
  208. */
  209. static inline int omap_rproc_get_timer_irq(struct omap_rproc_timer *timer)
  210. {
  211. return timer->timer_ops->get_irq(timer->odt);
  212. }
  213. /**
  214. * omap_rproc_ack_timer_irq - acknowledge a timer irq
  215. * @timer: handle to a OMAP rproc timer
  216. *
  217. * This function is used to clear the irq associated with a watchdog timer. The
  218. * The function is called by the OMAP remoteproc upon a watchdog event on the
  219. * remote processor to clear the interrupt status of the watchdog timer.
  220. *
  221. * Returns the irq id on success, otherwise a failure as returned by DMTimer ops
  222. */
  223. static inline void omap_rproc_ack_timer_irq(struct omap_rproc_timer *timer)
  224. {
  225. timer->timer_ops->write_status(timer->odt, OMAP_TIMER_INT_OVERFLOW);
  226. }
  227. /**
  228. * omap_rproc_watchdog_isr - Watchdog ISR handler for remoteproc device
  229. * @irq: IRQ number associated with a watchdog timer
  230. * @data: IRQ handler data
  231. *
  232. * This ISR routine executes the required necessary low-level code to
  233. * acknowledge a watchdog timer interrupt. There can be multiple watchdog
  234. * timers associated with a rproc (like IPUs which have 2 watchdog timers,
  235. * one per Cortex M3/M4 core), so a lookup has to be performed to identify
  236. * the timer to acknowledge its interrupt.
  237. *
  238. * The function also invokes rproc_report_crash to report the watchdog event
  239. * to the remoteproc driver core, to trigger a recovery.
  240. *
  241. * Return: IRQ_HANDLED or IRQ_NONE
  242. */
  243. static irqreturn_t omap_rproc_watchdog_isr(int irq, void *data)
  244. {
  245. struct rproc *rproc = data;
  246. struct omap_rproc *oproc = rproc->priv;
  247. struct device *dev = rproc->dev.parent;
  248. struct omap_rproc_timer *timers = oproc->timers;
  249. struct omap_rproc_timer *wd_timer = NULL;
  250. int num_timers = oproc->num_timers + oproc->num_wd_timers;
  251. int i;
  252. for (i = oproc->num_timers; i < num_timers; i++) {
  253. if (timers[i].irq > 0 && irq == timers[i].irq) {
  254. wd_timer = &timers[i];
  255. break;
  256. }
  257. }
  258. if (!wd_timer) {
  259. dev_err(dev, "invalid timer\n");
  260. return IRQ_NONE;
  261. }
  262. omap_rproc_ack_timer_irq(wd_timer);
  263. rproc_report_crash(rproc, RPROC_WATCHDOG);
  264. return IRQ_HANDLED;
  265. }
  266. /**
  267. * omap_rproc_enable_timers - enable the timers for a remoteproc
  268. * @rproc: handle of a remote processor
  269. * @configure: boolean flag used to acquire and configure the timer handle
  270. *
  271. * This function is used primarily to enable the timers associated with
  272. * a remoteproc. The configure flag is provided to allow the driver to
  273. * to either acquire and start a timer (during device initialization) or
  274. * to just start a timer (during a resume operation).
  275. */
  276. static int omap_rproc_enable_timers(struct rproc *rproc, bool configure)
  277. {
  278. int i;
  279. int ret = 0;
  280. struct platform_device *tpdev;
  281. struct dmtimer_platform_data *tpdata;
  282. const struct omap_dm_timer_ops *timer_ops;
  283. struct omap_rproc *oproc = rproc->priv;
  284. struct omap_rproc_timer *timers = oproc->timers;
  285. struct device *dev = rproc->dev.parent;
  286. struct device_node *np = NULL;
  287. int num_timers = oproc->num_timers + oproc->num_wd_timers;
  288. if (num_timers <= 0)
  289. return 0;
  290. if (!configure)
  291. goto start_timers;
  292. for (i = 0; i < num_timers; i++) {
  293. if (i < oproc->num_timers)
  294. np = of_parse_phandle(dev->of_node, "timers", i);
  295. else
  296. np = of_parse_phandle(dev->of_node, "watchdog-timers",
  297. (i - oproc->num_timers));
  298. if (!np) {
  299. ret = -ENXIO;
  300. dev_err(dev, "device node lookup for timer at index %d failed: %d\n",
  301. i < oproc->num_timers ? i :
  302. i - oproc->num_timers, ret);
  303. goto free_timers;
  304. }
  305. tpdev = of_find_device_by_node(np);
  306. if (!tpdev) {
  307. ret = -ENODEV;
  308. dev_err(dev, "could not get timer platform device\n");
  309. goto put_node;
  310. }
  311. tpdata = dev_get_platdata(&tpdev->dev);
  312. put_device(&tpdev->dev);
  313. if (!tpdata) {
  314. ret = -EINVAL;
  315. dev_err(dev, "dmtimer pdata structure NULL\n");
  316. goto put_node;
  317. }
  318. timer_ops = tpdata->timer_ops;
  319. if (!timer_ops || !timer_ops->request_by_node ||
  320. !timer_ops->set_source || !timer_ops->set_load ||
  321. !timer_ops->free || !timer_ops->start ||
  322. !timer_ops->stop || !timer_ops->get_irq ||
  323. !timer_ops->write_status) {
  324. ret = -EINVAL;
  325. dev_err(dev, "device does not have required timer ops\n");
  326. goto put_node;
  327. }
  328. timers[i].irq = -1;
  329. timers[i].timer_ops = timer_ops;
  330. ret = omap_rproc_request_timer(np, &timers[i]);
  331. if (ret) {
  332. dev_err(dev, "request for timer %p failed: %d\n", np,
  333. ret);
  334. goto put_node;
  335. }
  336. of_node_put(np);
  337. if (i >= oproc->num_timers) {
  338. timers[i].irq = omap_rproc_get_timer_irq(&timers[i]);
  339. if (timers[i].irq < 0) {
  340. dev_err(dev, "get_irq for timer %p failed: %d\n",
  341. np, timers[i].irq);
  342. ret = -EBUSY;
  343. goto free_timers;
  344. }
  345. ret = request_irq(timers[i].irq,
  346. omap_rproc_watchdog_isr, IRQF_SHARED,
  347. "rproc-wdt", rproc);
  348. if (ret) {
  349. dev_err(dev, "error requesting irq for timer %p\n",
  350. np);
  351. omap_rproc_release_timer(&timers[i]);
  352. timers[i].odt = NULL;
  353. timers[i].timer_ops = NULL;
  354. timers[i].irq = -1;
  355. goto free_timers;
  356. }
  357. }
  358. }
  359. start_timers:
  360. for (i = 0; i < num_timers; i++)
  361. omap_rproc_start_timer(&timers[i]);
  362. return 0;
  363. put_node:
  364. of_node_put(np);
  365. free_timers:
  366. while (i--) {
  367. if (i >= oproc->num_timers)
  368. free_irq(timers[i].irq, rproc);
  369. omap_rproc_release_timer(&timers[i]);
  370. timers[i].odt = NULL;
  371. timers[i].timer_ops = NULL;
  372. timers[i].irq = -1;
  373. }
  374. return ret;
  375. }
  376. /**
  377. * omap_rproc_disable_timers - disable the timers for a remoteproc
  378. * @rproc: handle of a remote processor
  379. * @configure: boolean flag used to release the timer handle
  380. *
  381. * This function is used primarily to disable the timers associated with
  382. * a remoteproc. The configure flag is provided to allow the driver to
  383. * to either stop and release a timer (during device shutdown) or to just
  384. * stop a timer (during a suspend operation).
  385. */
  386. static int omap_rproc_disable_timers(struct rproc *rproc, bool configure)
  387. {
  388. int i;
  389. struct omap_rproc *oproc = rproc->priv;
  390. struct omap_rproc_timer *timers = oproc->timers;
  391. int num_timers = oproc->num_timers + oproc->num_wd_timers;
  392. if (num_timers <= 0)
  393. return 0;
  394. for (i = 0; i < num_timers; i++) {
  395. omap_rproc_stop_timer(&timers[i]);
  396. if (configure) {
  397. if (i >= oproc->num_timers)
  398. free_irq(timers[i].irq, rproc);
  399. omap_rproc_release_timer(&timers[i]);
  400. timers[i].odt = NULL;
  401. timers[i].timer_ops = NULL;
  402. timers[i].irq = -1;
  403. }
  404. }
  405. return 0;
  406. }
  407. /**
  408. * omap_rproc_mbox_callback() - inbound mailbox message handler
  409. * @client: mailbox client pointer used for requesting the mailbox channel
  410. * @data: mailbox payload
  411. *
  412. * This handler is invoked by omap's mailbox driver whenever a mailbox
  413. * message is received. Usually, the mailbox payload simply contains
  414. * the index of the virtqueue that is kicked by the remote processor,
  415. * and we let remoteproc core handle it.
  416. *
  417. * In addition to virtqueue indices, we also have some out-of-band values
  418. * that indicates different events. Those values are deliberately very
  419. * big so they don't coincide with virtqueue indices.
  420. */
  421. static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
  422. {
  423. struct omap_rproc *oproc = container_of(client, struct omap_rproc,
  424. client);
  425. struct device *dev = oproc->rproc->dev.parent;
  426. const char *name = oproc->rproc->name;
  427. u32 msg = (u32)data;
  428. dev_dbg(dev, "mbox msg: 0x%x\n", msg);
  429. switch (msg) {
  430. case RP_MBOX_CRASH:
  431. /*
  432. * remoteproc detected an exception, notify the rproc core.
  433. * The remoteproc core will handle the recovery.
  434. */
  435. dev_err(dev, "omap rproc %s crashed\n", name);
  436. rproc_report_crash(oproc->rproc, RPROC_FATAL_ERROR);
  437. break;
  438. case RP_MBOX_ECHO_REPLY:
  439. dev_info(dev, "received echo reply from %s\n", name);
  440. break;
  441. case RP_MBOX_SUSPEND_ACK:
  442. case RP_MBOX_SUSPEND_CANCEL:
  443. oproc->suspend_acked = msg == RP_MBOX_SUSPEND_ACK;
  444. complete(&oproc->pm_comp);
  445. break;
  446. default:
  447. if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
  448. return;
  449. if (msg > oproc->rproc->max_notifyid) {
  450. dev_dbg(dev, "dropping unknown message 0x%x", msg);
  451. return;
  452. }
  453. /* msg contains the index of the triggered vring */
  454. if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
  455. dev_dbg(dev, "no message was found in vqid %d\n", msg);
  456. }
  457. }
  458. /* kick a virtqueue */
  459. static void omap_rproc_kick(struct rproc *rproc, int vqid)
  460. {
  461. struct omap_rproc *oproc = rproc->priv;
  462. struct device *dev = rproc->dev.parent;
  463. int ret;
  464. /* wake up the rproc before kicking it */
  465. ret = pm_runtime_get_sync(dev);
  466. if (WARN_ON(ret < 0)) {
  467. dev_err(dev, "pm_runtime_get_sync() failed during kick, ret = %d\n",
  468. ret);
  469. pm_runtime_put_noidle(dev);
  470. return;
  471. }
  472. /* send the index of the triggered virtqueue in the mailbox payload */
  473. ret = mbox_send_message(oproc->mbox, (void *)vqid);
  474. if (ret < 0)
  475. dev_err(dev, "failed to send mailbox message, status = %d\n",
  476. ret);
  477. pm_runtime_mark_last_busy(dev);
  478. pm_runtime_put_autosuspend(dev);
  479. }
  480. /**
  481. * omap_rproc_write_dsp_boot_addr - set boot address for a DSP remote processor
  482. * @rproc: handle of a remote processor
  483. *
  484. * Set boot address for a supported DSP remote processor.
  485. */
  486. static int omap_rproc_write_dsp_boot_addr(struct rproc *rproc)
  487. {
  488. struct device *dev = rproc->dev.parent;
  489. struct omap_rproc *oproc = rproc->priv;
  490. struct omap_rproc_boot_data *bdata = oproc->boot_data;
  491. u32 offset = bdata->boot_reg;
  492. unsigned int value = rproc->bootaddr;
  493. unsigned int mask = ~(SZ_1K - 1);
  494. if (value & (SZ_1K - 1)) {
  495. dev_err(dev, "invalid boot address 0x%x, must be aligned on a 1KB boundary\n",
  496. value);
  497. return -EINVAL;
  498. }
  499. value >>= bdata->boot_reg_shift;
  500. mask >>= bdata->boot_reg_shift;
  501. regmap_update_bits(bdata->syscon, offset, mask, value);
  502. return 0;
  503. }
  504. /*
  505. * Power up the remote processor.
  506. *
  507. * This function will be invoked only after the firmware for this rproc
  508. * was loaded, parsed successfully, and all of its resource requirements
  509. * were met.
  510. */
  511. static int omap_rproc_start(struct rproc *rproc)
  512. {
  513. struct omap_rproc *oproc = rproc->priv;
  514. struct device *dev = rproc->dev.parent;
  515. struct platform_device *pdev = to_platform_device(dev);
  516. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  517. int ret;
  518. struct mbox_client *client = &oproc->client;
  519. /*
  520. * We set boot address irrespective of the value of the late attach flag
  521. * as boot address takes effect only on a deassert of remoteproc reset.
  522. */
  523. if (oproc->boot_data) {
  524. ret = omap_rproc_write_dsp_boot_addr(rproc);
  525. if (ret)
  526. return ret;
  527. }
  528. client->dev = dev;
  529. client->tx_done = NULL;
  530. client->rx_callback = omap_rproc_mbox_callback;
  531. client->tx_block = false;
  532. client->knows_txdone = false;
  533. oproc->mbox = mbox_request_channel(client, 0);
  534. if (IS_ERR(oproc->mbox)) {
  535. ret = -EBUSY;
  536. dev_err(dev, "mbox_request_channel failed: %ld\n",
  537. PTR_ERR(oproc->mbox));
  538. return ret;
  539. }
  540. /*
  541. * Ping the remote processor. this is only for sanity-sake;
  542. * there is no functional effect whatsoever.
  543. *
  544. * Note that the reply will _not_ arrive immediately: this message
  545. * will wait in the mailbox fifo until the remote processor is booted.
  546. */
  547. ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
  548. if (ret < 0) {
  549. dev_err(dev, "mbox_send_message failed: %d\n", ret);
  550. goto put_mbox;
  551. }
  552. ret = omap_rproc_enable_timers(rproc, true);
  553. if (ret) {
  554. dev_err(dev, "omap_rproc_enable_timers failed: %d\n", ret);
  555. goto put_mbox;
  556. }
  557. if (!rproc->late_attach) {
  558. ret = pdata->device_enable(pdev);
  559. if (ret) {
  560. dev_err(dev, "omap_device_enable failed: %d\n", ret);
  561. goto reset_timers;
  562. }
  563. }
  564. /*
  565. * remote processor is up, so update the runtime pm status and
  566. * enable the auto-suspend. The device usage count is incremented
  567. * manually for balancing it for auto-suspend
  568. */
  569. pm_runtime_set_active(dev);
  570. pm_runtime_set_autosuspend_delay(dev, oproc->autosuspend_delay);
  571. pm_runtime_use_autosuspend(dev);
  572. pm_runtime_get_noresume(dev);
  573. pm_runtime_enable(dev);
  574. pm_runtime_mark_last_busy(dev);
  575. pm_runtime_put_autosuspend(dev);
  576. return 0;
  577. reset_timers:
  578. omap_rproc_disable_timers(rproc, true);
  579. put_mbox:
  580. mbox_free_channel(oproc->mbox);
  581. return ret;
  582. }
  583. /* power off the remote processor */
  584. static int omap_rproc_stop(struct rproc *rproc)
  585. {
  586. struct device *dev = rproc->dev.parent;
  587. struct platform_device *pdev = to_platform_device(dev);
  588. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  589. struct omap_rproc *oproc = rproc->priv;
  590. int ret;
  591. /*
  592. * cancel any possible scheduled runtime suspend by incrementing
  593. * the device usage count, and resuming the device. The remoteproc
  594. * also needs to be woken up if suspended, to avoid the remoteproc
  595. * OS to continue to remember any context that it has saved, and
  596. * avoid potential issues in misindentifying a subsequent device
  597. * reboot as a power restore boot
  598. */
  599. ret = pm_runtime_get_sync(dev);
  600. if (ret < 0) {
  601. pm_runtime_put_noidle(dev);
  602. return ret;
  603. }
  604. ret = pdata->device_shutdown(pdev);
  605. if (ret)
  606. goto out;
  607. ret = omap_rproc_disable_timers(rproc, true);
  608. if (ret)
  609. goto enable_device;
  610. /*
  611. * During late attach, we use non-zeroing dma ops to prevent the kernel
  612. * from overwriting already loaded code and data segments. When
  613. * shutting down the processor, we restore the normal zeroing dma ops.
  614. * This allows the kernel to clear memory when loading a new remoteproc
  615. * binary or during error recovery with the current remoteproc binary.
  616. */
  617. if (rproc->late_attach)
  618. set_dma_ops(dev, &arm_dma_ops);
  619. mbox_free_channel(oproc->mbox);
  620. /*
  621. * update the runtime pm states and status now that the remoteproc
  622. * has stopped
  623. */
  624. pm_runtime_disable(dev);
  625. pm_runtime_dont_use_autosuspend(dev);
  626. pm_runtime_put_noidle(dev);
  627. pm_runtime_set_suspended(dev);
  628. return 0;
  629. enable_device:
  630. pdata->device_enable(pdev);
  631. out:
  632. /* schedule the next auto-suspend */
  633. pm_runtime_mark_last_busy(dev);
  634. pm_runtime_put_autosuspend(dev);
  635. return ret;
  636. }
  637. /*
  638. * Internal Memory translation helper
  639. *
  640. * Custom function implementing the rproc .da_to_va ops to provide address
  641. * translation (device address to kernel virtual address) for internal RAMs
  642. * present in a DSP or IPU device). The translated addresses can be used
  643. * either by the remoteproc core for loading, or by any rpmsg bus drivers.
  644. */
  645. static void *omap_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
  646. u32 flags)
  647. {
  648. struct omap_rproc *oproc = rproc->priv;
  649. void *va = NULL;
  650. int i;
  651. u32 offset;
  652. if (len <= 0)
  653. return NULL;
  654. if (!oproc->num_mems)
  655. return NULL;
  656. for (i = 0; i < oproc->num_mems; i++) {
  657. if (da >= oproc->mem[i].dev_addr && da + len <=
  658. oproc->mem[i].dev_addr + oproc->mem[i].size) {
  659. offset = da - oproc->mem[i].dev_addr;
  660. /* __force to make sparse happy with type conversion */
  661. va = (__force void *)(oproc->mem[i].cpu_addr + offset);
  662. break;
  663. }
  664. }
  665. return va;
  666. }
  667. static const struct rproc_ops omap_rproc_ops = {
  668. .start = omap_rproc_start,
  669. .stop = omap_rproc_stop,
  670. .kick = omap_rproc_kick,
  671. .da_to_va = omap_rproc_da_to_va,
  672. };
  673. #ifdef CONFIG_PM
  674. static bool _is_rproc_in_standby(struct omap_rproc *oproc)
  675. {
  676. static int standby_mask = (1 << 18);
  677. return readl(oproc->standby_addr) & standby_mask;
  678. }
  679. /* 1 sec is long enough time to let the remoteproc side suspend the device */
  680. #define DEF_SUSPEND_TIMEOUT 1000
  681. static int _omap_rproc_suspend(struct rproc *rproc, bool auto_suspend)
  682. {
  683. struct device *dev = rproc->dev.parent;
  684. struct platform_device *pdev = to_platform_device(dev);
  685. struct omap_rproc_pdata *pdata = dev_get_platdata(dev);
  686. struct omap_rproc *oproc = rproc->priv;
  687. unsigned long to = msecs_to_jiffies(DEF_SUSPEND_TIMEOUT);
  688. unsigned long ta = jiffies + to;
  689. u32 suspend_msg = auto_suspend ?
  690. RP_MBOX_SUSPEND_AUTO : RP_MBOX_SUSPEND_SYSTEM;
  691. int ret;
  692. reinit_completion(&oproc->pm_comp);
  693. oproc->suspend_acked = false;
  694. ret = mbox_send_message(oproc->mbox, (void *)suspend_msg);
  695. if (ret < 0) {
  696. dev_err(dev, "PM mbox_send_message failed: %d\n", ret);
  697. return ret;
  698. }
  699. ret = wait_for_completion_timeout(&oproc->pm_comp, to);
  700. if (!oproc->suspend_acked)
  701. return -EBUSY;
  702. /*
  703. * The remoteproc side is returning the ACK message before saving the
  704. * context, because the context saving is performed within a SYS/BIOS
  705. * function, and it cannot have any inter-dependencies against the IPC
  706. * layer. Also, as the SYS/BIOS needs to preserve properly the processor
  707. * register set, sending this ACK or signalling the completion of the
  708. * context save through a shared memory variable can never be the
  709. * absolute last thing to be executed on the remoteproc side, and the
  710. * MPU cannot use the ACK message as a sync point to put the remoteproc
  711. * into reset. The only way to ensure that the remote processor has
  712. * completed saving the context is to check that the module has reached
  713. * STANDBY state (after saving the context, the SYS/BIOS executes the
  714. * appropriate target-specific WFI instruction causing the module to
  715. * enter STANDBY).
  716. */
  717. while (!_is_rproc_in_standby(oproc)) {
  718. if (time_after(jiffies, ta))
  719. return -ETIME;
  720. schedule();
  721. }
  722. ret = pdata->device_shutdown(pdev);
  723. if (ret)
  724. return ret;
  725. ret = omap_rproc_disable_timers(rproc, false);
  726. if (ret) {
  727. dev_err(dev, "disabling timers during suspend failed %d\n",
  728. ret);
  729. goto enable_device;
  730. }
  731. /*
  732. * IOMMUs would have to be disabled specifically for runtime suspend.
  733. * They are handled automatically through System PM callbacks for
  734. * regular system suspend
  735. */
  736. if (auto_suspend) {
  737. ret = omap_iommu_domain_deactivate(rproc->domain);
  738. if (ret) {
  739. dev_err(dev, "iommu domain deactivate failed %d\n",
  740. ret);
  741. goto enable_timers;
  742. }
  743. }
  744. return 0;
  745. enable_timers:
  746. /* ignore errors on re-enabling code */
  747. omap_rproc_enable_timers(rproc, false);
  748. enable_device:
  749. pdata->device_enable(pdev);
  750. return ret;
  751. }
  752. static int _omap_rproc_resume(struct rproc *rproc, bool auto_suspend)
  753. {
  754. struct device *dev = rproc->dev.parent;
  755. struct platform_device *pdev = to_platform_device(dev);
  756. struct omap_rproc_pdata *pdata = dev_get_platdata(dev);
  757. struct omap_rproc *oproc = rproc->priv;
  758. int ret;
  759. /*
  760. * IOMMUs would have to be enabled specifically for runtime resume.
  761. * They would have been already enabled automatically through System
  762. * PM callbacks for regular system resume
  763. */
  764. if (auto_suspend) {
  765. ret = omap_iommu_domain_activate(rproc->domain);
  766. if (ret) {
  767. dev_err(dev, "omap_iommu activate failed %d\n", ret);
  768. goto out;
  769. }
  770. }
  771. /* boot address could be lost after suspend, so restore it */
  772. if (oproc->boot_data) {
  773. ret = omap_rproc_write_dsp_boot_addr(rproc);
  774. if (ret) {
  775. dev_err(dev, "boot address restore failed %d\n", ret);
  776. goto suspend_iommu;
  777. }
  778. }
  779. ret = omap_rproc_enable_timers(rproc, false);
  780. if (ret) {
  781. dev_err(dev, "enabling timers during resume failed %d\n",
  782. ret);
  783. goto suspend_iommu;
  784. }
  785. ret = pdata->device_enable(pdev);
  786. if (ret)
  787. goto disable_timers;
  788. return 0;
  789. disable_timers:
  790. omap_rproc_disable_timers(rproc, false);
  791. suspend_iommu:
  792. if (auto_suspend)
  793. omap_iommu_domain_deactivate(rproc->domain);
  794. out:
  795. return ret;
  796. }
  797. static int __maybe_unused omap_rproc_suspend(struct device *dev)
  798. {
  799. struct platform_device *pdev = to_platform_device(dev);
  800. struct rproc *rproc = platform_get_drvdata(pdev);
  801. struct omap_rproc *oproc = rproc->priv;
  802. int ret = 0;
  803. mutex_lock(&rproc->lock);
  804. if (rproc->state == RPROC_OFFLINE)
  805. goto out;
  806. if (rproc->state == RPROC_SUSPENDED)
  807. goto out;
  808. if (rproc->state != RPROC_RUNNING) {
  809. ret = -EBUSY;
  810. goto out;
  811. }
  812. ret = _omap_rproc_suspend(rproc, false);
  813. if (ret) {
  814. dev_err(dev, "suspend failed %d\n", ret);
  815. goto out;
  816. }
  817. /*
  818. * remoteproc is running at the time of system suspend, so remember
  819. * it so as to wake it up during system resume
  820. */
  821. oproc->need_resume = 1;
  822. rproc->state = RPROC_SUSPENDED;
  823. /*
  824. * update the runtime pm status to be suspended, without decrementing
  825. * the device usage count
  826. */
  827. pm_runtime_disable(dev);
  828. pm_runtime_set_suspended(dev);
  829. out:
  830. mutex_unlock(&rproc->lock);
  831. return ret;
  832. }
  833. static int __maybe_unused omap_rproc_resume(struct device *dev)
  834. {
  835. struct platform_device *pdev = to_platform_device(dev);
  836. struct rproc *rproc = platform_get_drvdata(pdev);
  837. struct omap_rproc *oproc = rproc->priv;
  838. int ret = 0;
  839. mutex_lock(&rproc->lock);
  840. if (rproc->state == RPROC_OFFLINE)
  841. goto out;
  842. if (rproc->state != RPROC_SUSPENDED) {
  843. ret = -EBUSY;
  844. goto out;
  845. }
  846. /*
  847. * remoteproc was auto-suspended at the time of system suspend,
  848. * so no need to wake-up the processor (leave it in suspended
  849. * state, will be woken up during a subsequent runtime_resume)
  850. */
  851. if (!oproc->need_resume)
  852. goto out;
  853. ret = _omap_rproc_resume(rproc, false);
  854. if (ret) {
  855. dev_err(dev, "resume failed %d\n", ret);
  856. goto out;
  857. }
  858. oproc->need_resume = false;
  859. rproc->state = RPROC_RUNNING;
  860. /*
  861. * update the runtime pm status to be active, without incrementing
  862. * the device usage count
  863. */
  864. pm_runtime_set_active(dev);
  865. pm_runtime_enable(dev);
  866. pm_runtime_mark_last_busy(dev);
  867. out:
  868. mutex_unlock(&rproc->lock);
  869. return ret;
  870. }
  871. static int omap_rproc_runtime_suspend(struct device *dev)
  872. {
  873. struct rproc *rproc = dev_get_drvdata(dev);
  874. struct omap_rproc *oproc = rproc->priv;
  875. int ret;
  876. if (rproc->state == RPROC_CRASHED) {
  877. dev_dbg(dev, "rproc cannot be runtime suspended when crashed!\n");
  878. return -EBUSY;
  879. }
  880. if (WARN_ON(rproc->state != RPROC_RUNNING)) {
  881. dev_err(dev, "rproc cannot be runtime suspended when not running!\n");
  882. return -EBUSY;
  883. }
  884. /*
  885. * do not even attempt suspend if the remote processor is not
  886. * idled for runtime auto-suspend
  887. */
  888. if (!_is_rproc_in_standby(oproc)) {
  889. ret = -EBUSY;
  890. goto abort;
  891. }
  892. ret = _omap_rproc_suspend(rproc, true);
  893. if (ret)
  894. goto abort;
  895. rproc->state = RPROC_SUSPENDED;
  896. return 0;
  897. abort:
  898. pm_runtime_mark_last_busy(dev);
  899. return ret;
  900. }
  901. static int omap_rproc_runtime_resume(struct device *dev)
  902. {
  903. struct rproc *rproc = dev_get_drvdata(dev);
  904. int ret;
  905. if (WARN_ON(rproc->state != RPROC_SUSPENDED)) {
  906. dev_err(dev, "rproc cannot be runtime resumed if not suspended!\n");
  907. return -EBUSY;
  908. }
  909. ret = _omap_rproc_resume(rproc, true);
  910. if (ret) {
  911. dev_err(dev, "runtime resume failed %d\n", ret);
  912. return ret;
  913. }
  914. rproc->state = RPROC_RUNNING;
  915. return 0;
  916. }
  917. #endif /* CONFIG_PM */
  918. static const struct omap_rproc_dev_data omap4_dsp_dev_data = {
  919. .device_name = "dsp",
  920. .fw_name = "omap4-dsp-fw.xe64T",
  921. };
  922. static const struct omap_rproc_dev_data omap4_ipu_dev_data = {
  923. .device_name = "ipu",
  924. .fw_name = "omap4-ipu-fw.xem3",
  925. };
  926. static const struct omap_rproc_dev_data omap5_dsp_dev_data = {
  927. .device_name = "dsp",
  928. .fw_name = "omap5-dsp-fw.xe64T",
  929. };
  930. static const struct omap_rproc_dev_data omap5_ipu_dev_data = {
  931. .device_name = "ipu",
  932. .fw_name = "omap5-ipu-fw.xem4",
  933. };
  934. static const struct omap_rproc_dev_data dra7_rproc_dev_data[] = {
  935. {
  936. .device_name = "40800000.dsp",
  937. .fw_name = "dra7-dsp1-fw.xe66",
  938. },
  939. {
  940. .device_name = "41000000.dsp",
  941. .fw_name = "dra7-dsp2-fw.xe66",
  942. },
  943. {
  944. .device_name = "55020000.ipu",
  945. .fw_name = "dra7-ipu2-fw.xem4",
  946. },
  947. {
  948. .device_name = "58820000.ipu",
  949. .fw_name = "dra7-ipu1-fw.xem4",
  950. },
  951. {
  952. /* sentinel */
  953. },
  954. };
  955. static const struct of_device_id omap_rproc_of_match[] = {
  956. {
  957. .compatible = "ti,omap4-dsp",
  958. .data = &omap4_dsp_dev_data,
  959. },
  960. {
  961. .compatible = "ti,omap4-ipu",
  962. .data = &omap4_ipu_dev_data,
  963. },
  964. {
  965. .compatible = "ti,omap5-dsp",
  966. .data = &omap5_dsp_dev_data,
  967. },
  968. {
  969. .compatible = "ti,omap5-ipu",
  970. .data = &omap5_ipu_dev_data,
  971. },
  972. {
  973. .compatible = "ti,dra7-dsp",
  974. .data = dra7_rproc_dev_data,
  975. },
  976. {
  977. .compatible = "ti,dra7-ipu",
  978. .data = dra7_rproc_dev_data,
  979. },
  980. {
  981. /* end */
  982. },
  983. };
  984. MODULE_DEVICE_TABLE(of, omap_rproc_of_match);
  985. static int omap_rproc_get_autosuspend_delay(struct platform_device *pdev)
  986. {
  987. struct device_node *np = pdev->dev.of_node;
  988. const struct omap_rproc_dev_data *data;
  989. int delay = -EINVAL;
  990. data = of_device_get_match_data(&pdev->dev);
  991. if (!data)
  992. return -ENODEV;
  993. if (!of_device_is_compatible(np, "ti,dra7-dsp") &&
  994. !of_device_is_compatible(np, "ti,dra7-ipu")) {
  995. delay = data->autosuspend_delay;
  996. goto out;
  997. }
  998. for (; data && data->device_name; data++) {
  999. if (!strcmp(dev_name(&pdev->dev), data->device_name)) {
  1000. delay = data->autosuspend_delay;
  1001. break;
  1002. }
  1003. }
  1004. out:
  1005. return (delay > 0) ? delay : DEFAULT_AUTOSUSPEND_DELAY;
  1006. }
  1007. static const char *omap_rproc_get_firmware(struct platform_device *pdev)
  1008. {
  1009. struct device_node *np = pdev->dev.of_node;
  1010. const struct omap_rproc_dev_data *data;
  1011. data = of_device_get_match_data(&pdev->dev);
  1012. if (!data)
  1013. return ERR_PTR(-ENODEV);
  1014. if (!of_device_is_compatible(np, "ti,dra7-dsp") &&
  1015. !of_device_is_compatible(np, "ti,dra7-ipu"))
  1016. return data->fw_name;
  1017. for (; data && data->device_name; data++) {
  1018. if (!strcmp(dev_name(&pdev->dev), data->device_name))
  1019. return data->fw_name;
  1020. }
  1021. return ERR_PTR(-ENOENT);
  1022. }
  1023. static int omap_rproc_get_boot_data(struct platform_device *pdev,
  1024. struct rproc *rproc)
  1025. {
  1026. struct device_node *np = pdev->dev.of_node;
  1027. struct omap_rproc *oproc = rproc->priv;
  1028. int ret;
  1029. if (!of_device_is_compatible(np, "ti,omap4-dsp") &&
  1030. !of_device_is_compatible(np, "ti,omap5-dsp") &&
  1031. !of_device_is_compatible(np, "ti,dra7-dsp"))
  1032. return 0;
  1033. oproc->boot_data = devm_kzalloc(&pdev->dev, sizeof(*oproc->boot_data),
  1034. GFP_KERNEL);
  1035. if (!oproc->boot_data)
  1036. return -ENOMEM;
  1037. if (!of_property_read_bool(np, "syscon-bootreg")) {
  1038. dev_err(&pdev->dev, "syscon-bootreg property is missing\n");
  1039. return -EINVAL;
  1040. }
  1041. oproc->boot_data->syscon =
  1042. syscon_regmap_lookup_by_phandle(np, "syscon-bootreg");
  1043. if (IS_ERR(oproc->boot_data->syscon)) {
  1044. ret = PTR_ERR(oproc->boot_data->syscon);
  1045. return ret;
  1046. }
  1047. if (of_property_read_u32_index(np, "syscon-bootreg", 1,
  1048. &oproc->boot_data->boot_reg)) {
  1049. dev_err(&pdev->dev, "couldn't get the boot register\n");
  1050. return -EINVAL;
  1051. }
  1052. if (of_device_is_compatible(np, "ti,dra7-dsp"))
  1053. oproc->boot_data->boot_reg_shift = 10;
  1054. return 0;
  1055. }
  1056. static int omap_rproc_of_get_internal_memories(struct platform_device *pdev,
  1057. struct rproc *rproc)
  1058. {
  1059. static const char * const ipu_mem_names[] = {"l2ram"};
  1060. static const char * const dra7_dsp_mem_names[] = {"l2ram", "l1pram",
  1061. "l1dram"};
  1062. struct device_node *np = pdev->dev.of_node;
  1063. struct omap_rproc *oproc = rproc->priv;
  1064. struct device *dev = &pdev->dev;
  1065. const char * const *mem_names;
  1066. struct resource *res;
  1067. int num_mems;
  1068. const __be32 *addrp;
  1069. u32 l4_offset = 0;
  1070. u64 size;
  1071. int i;
  1072. /* OMAP4 and OMAP5 DSPs do not have support for flat SRAM */
  1073. if (of_device_is_compatible(np, "ti,omap4-dsp") ||
  1074. of_device_is_compatible(np, "ti,omap5-dsp"))
  1075. return 0;
  1076. /* DRA7 DSPs have two additional SRAMs at L1 level */
  1077. if (of_device_is_compatible(np, "ti,dra7-dsp")) {
  1078. mem_names = dra7_dsp_mem_names;
  1079. num_mems = ARRAY_SIZE(dra7_dsp_mem_names);
  1080. } else {
  1081. mem_names = ipu_mem_names;
  1082. num_mems = ARRAY_SIZE(ipu_mem_names);
  1083. }
  1084. oproc->mem = devm_kcalloc(dev, num_mems, sizeof(*oproc->mem),
  1085. GFP_KERNEL);
  1086. if (!oproc->mem)
  1087. return -ENOMEM;
  1088. for (i = 0; i < num_mems; i++) {
  1089. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1090. mem_names[i]);
  1091. oproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  1092. if (IS_ERR(oproc->mem[i].cpu_addr)) {
  1093. dev_err(dev, "failed to parse and map %s memory\n",
  1094. mem_names[i]);
  1095. return PTR_ERR(oproc->mem[i].cpu_addr);
  1096. }
  1097. oproc->mem[i].bus_addr = res->start;
  1098. /*
  1099. * The DSPs have the internal memories starting at a fixed
  1100. * offset of 0x800000 from address 0, and this corresponds to
  1101. * L2RAM. The L3 address view has the L2RAM bus address as the
  1102. * starting address for the IP, so the L2RAM memory region needs
  1103. * to be processed first, and the device addresses for each
  1104. * memory region can be computed using the relative offset
  1105. * from this base address.
  1106. */
  1107. if (of_device_is_compatible(np, "ti,dra7-dsp") &&
  1108. !strcmp(mem_names[i], "l2ram")) {
  1109. addrp = of_get_address(dev->of_node, i, &size, NULL);
  1110. l4_offset = be32_to_cpu(*addrp);
  1111. }
  1112. oproc->mem[i].dev_addr =
  1113. of_device_is_compatible(np, "ti,dra7-dsp") ?
  1114. res->start - l4_offset +
  1115. OMAP_RPROC_DSP_LOCAL_MEM_OFFSET :
  1116. OMAP_RPROC_IPU_L2RAM_DEV_ADDR;
  1117. oproc->mem[i].size = resource_size(res);
  1118. dev_dbg(dev, "memory %8s: bus addr %pa size 0x%x va %p da 0x%x\n",
  1119. mem_names[i], &oproc->mem[i].bus_addr,
  1120. oproc->mem[i].size, oproc->mem[i].cpu_addr,
  1121. oproc->mem[i].dev_addr);
  1122. }
  1123. oproc->num_mems = num_mems;
  1124. return 0;
  1125. }
  1126. static int omap_rproc_probe(struct platform_device *pdev)
  1127. {
  1128. struct omap_rproc_pdata *pdata = dev_get_platdata(&pdev->dev);
  1129. struct device_node *np = pdev->dev.of_node;
  1130. struct omap_rproc *oproc;
  1131. struct rproc *rproc;
  1132. const char *firmware;
  1133. u32 standby_addr = 0;
  1134. int num_timers;
  1135. int ret;
  1136. if (!np) {
  1137. dev_err(&pdev->dev, "only DT-based devices are supported\n");
  1138. return -ENODEV;
  1139. }
  1140. /*
  1141. * self-manage the ordering dependencies between omap_device_enable/idle
  1142. * and omap_device_assert/deassert_hardreset API during runtime suspend
  1143. * and resume, rather than relying on the order in omap_device layer.
  1144. */
  1145. if (pdev->dev.pm_domain) {
  1146. dev_dbg(&pdev->dev, "device pm_domain is being reset for this remoteproc device\n");
  1147. pdev->dev.pm_domain = NULL;
  1148. }
  1149. if (!pdata || !pdata->device_enable || !pdata->device_shutdown) {
  1150. dev_err(&pdev->dev, "platform data is either missing or incomplete\n");
  1151. return -ENODEV;
  1152. }
  1153. firmware = omap_rproc_get_firmware(pdev);
  1154. if (IS_ERR(firmware))
  1155. return PTR_ERR(firmware);
  1156. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  1157. if (ret) {
  1158. dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
  1159. return ret;
  1160. }
  1161. rproc = rproc_alloc(&pdev->dev, dev_name(&pdev->dev), &omap_rproc_ops,
  1162. firmware, sizeof(*oproc));
  1163. if (!rproc)
  1164. return -ENOMEM;
  1165. if (pdata->device_is_enabled && pdata->device_is_enabled(pdev)) {
  1166. rproc->late_attach = 1;
  1167. set_dma_ops(&pdev->dev, &arm_dma_m_ops);
  1168. }
  1169. oproc = rproc->priv;
  1170. oproc->rproc = rproc;
  1171. /* All existing OMAP IPU and DSP processors have an MMU */
  1172. rproc->has_iommu = true;
  1173. ret = omap_rproc_of_get_internal_memories(pdev, rproc);
  1174. if (ret)
  1175. goto free_rproc;
  1176. ret = omap_rproc_get_boot_data(pdev, rproc);
  1177. if (ret)
  1178. goto free_rproc;
  1179. /*
  1180. * Timer nodes are directly used in client nodes as phandles, so
  1181. * retrieve the count using appropriate size
  1182. */
  1183. oproc->num_timers = of_property_count_elems_of_size(np, "timers",
  1184. sizeof(phandle));
  1185. if (oproc->num_timers <= 0) {
  1186. dev_dbg(&pdev->dev, "device does not have timers, status = %d\n",
  1187. oproc->num_timers);
  1188. oproc->num_timers = 0;
  1189. }
  1190. #ifdef CONFIG_OMAP_REMOTEPROC_WATCHDOG
  1191. oproc->num_wd_timers = of_count_phandle_with_args(np, "watchdog-timers",
  1192. NULL);
  1193. if (oproc->num_wd_timers <= 0) {
  1194. dev_dbg(&pdev->dev, "device does not have watchdog timers, status = %d\n",
  1195. oproc->num_wd_timers);
  1196. oproc->num_wd_timers = 0;
  1197. }
  1198. #endif
  1199. if (oproc->num_timers || oproc->num_wd_timers) {
  1200. num_timers = oproc->num_timers + oproc->num_wd_timers;
  1201. oproc->timers = devm_kzalloc(&pdev->dev, sizeof(*oproc->timers)
  1202. * num_timers, GFP_KERNEL);
  1203. if (!oproc->timers) {
  1204. ret = -ENOMEM;
  1205. goto free_rproc;
  1206. }
  1207. dev_dbg(&pdev->dev, "device has %d tick timers and %d watchdog timers\n",
  1208. oproc->num_timers, oproc->num_wd_timers);
  1209. }
  1210. init_completion(&oproc->pm_comp);
  1211. oproc->autosuspend_delay = omap_rproc_get_autosuspend_delay(pdev);
  1212. if (oproc->autosuspend_delay < 0) {
  1213. ret = oproc->autosuspend_delay;
  1214. goto free_rproc;
  1215. }
  1216. ret = of_property_read_u32(np, "ti,rproc-standby-info", &standby_addr);
  1217. if (ret || !standby_addr) {
  1218. ret = !standby_addr ? -EINVAL : ret;
  1219. goto free_rproc;
  1220. }
  1221. oproc->standby_addr = devm_ioremap(&pdev->dev, standby_addr,
  1222. sizeof(u32));
  1223. if (!oproc->standby_addr) {
  1224. ret = -ENOMEM;
  1225. goto free_rproc;
  1226. }
  1227. ret = of_reserved_mem_device_init(&pdev->dev);
  1228. if (ret) {
  1229. dev_err(&pdev->dev, "device does not have specific CMA pool\n");
  1230. goto free_rproc;
  1231. }
  1232. platform_set_drvdata(pdev, rproc);
  1233. ret = rproc_add(rproc);
  1234. if (ret)
  1235. goto release_mem;
  1236. if (rproc_get_id(rproc) < 0)
  1237. dev_warn(&pdev->dev, "device does not have an alias id\n");
  1238. return 0;
  1239. release_mem:
  1240. of_reserved_mem_device_release(&pdev->dev);
  1241. free_rproc:
  1242. if (rproc->late_attach)
  1243. set_dma_ops(&pdev->dev, &arm_dma_ops);
  1244. rproc_free(rproc);
  1245. return ret;
  1246. }
  1247. static int omap_rproc_remove(struct platform_device *pdev)
  1248. {
  1249. struct rproc *rproc = platform_get_drvdata(pdev);
  1250. rproc_del(rproc);
  1251. rproc_free(rproc);
  1252. of_reserved_mem_device_release(&pdev->dev);
  1253. return 0;
  1254. }
  1255. static const struct dev_pm_ops omap_rproc_pm_ops = {
  1256. SET_SYSTEM_SLEEP_PM_OPS(omap_rproc_suspend, omap_rproc_resume)
  1257. SET_RUNTIME_PM_OPS(omap_rproc_runtime_suspend,
  1258. omap_rproc_runtime_resume, NULL)
  1259. };
  1260. static struct platform_driver omap_rproc_driver = {
  1261. .probe = omap_rproc_probe,
  1262. .remove = omap_rproc_remove,
  1263. .driver = {
  1264. .name = "omap-rproc",
  1265. .pm = &omap_rproc_pm_ops,
  1266. .of_match_table = omap_rproc_of_match,
  1267. },
  1268. };
  1269. module_platform_driver(omap_rproc_driver);
  1270. MODULE_LICENSE("GPL v2");
  1271. MODULE_DESCRIPTION("OMAP Remote Processor control driver");