ti_k3_r5_remoteproc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI K3 R5F (MCU) Remote Processor driver
  4. *
  5. * Copyright (C) 2017-2022 Texas Instruments Incorporated - https://www.ti.com/
  6. * Suman Anna <s-anna@ti.com>
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/err.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mailbox_client.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_reserved_mem.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/omap-mailbox.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/remoteproc.h>
  22. #include <linux/reset.h>
  23. #include <linux/slab.h>
  24. #include "omap_remoteproc.h"
  25. #include "remoteproc_internal.h"
  26. #include "ti_sci_proc.h"
  27. /* This address can either be for ATCM or BTCM with the other at address 0x0 */
  28. #define K3_R5_TCM_DEV_ADDR 0x41010000
  29. /* R5 TI-SCI Processor Configuration Flags */
  30. #define PROC_BOOT_CFG_FLAG_R5_DBG_EN 0x00000001
  31. #define PROC_BOOT_CFG_FLAG_R5_DBG_NIDEN 0x00000002
  32. #define PROC_BOOT_CFG_FLAG_R5_LOCKSTEP 0x00000100
  33. #define PROC_BOOT_CFG_FLAG_R5_TEINIT 0x00000200
  34. #define PROC_BOOT_CFG_FLAG_R5_NMFI_EN 0x00000400
  35. #define PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE 0x00000800
  36. #define PROC_BOOT_CFG_FLAG_R5_BTCM_EN 0x00001000
  37. #define PROC_BOOT_CFG_FLAG_R5_ATCM_EN 0x00002000
  38. /* Available from J7200 SoCs onwards */
  39. #define PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS 0x00004000
  40. /* Applicable to only AM64x SoCs */
  41. #define PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE 0x00008000
  42. /* R5 TI-SCI Processor Control Flags */
  43. #define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001
  44. /* R5 TI-SCI Processor Status Flags */
  45. #define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001
  46. #define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002
  47. #define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004
  48. #define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100
  49. /* Applicable to only AM64x SoCs */
  50. #define PROC_BOOT_STATUS_FLAG_R5_SINGLECORE_ONLY 0x00000200
  51. /**
  52. * struct k3_r5_mem - internal memory structure
  53. * @cpu_addr: MPU virtual address of the memory region
  54. * @bus_addr: Bus address used to access the memory region
  55. * @dev_addr: Device address from remoteproc view
  56. * @size: Size of the memory region
  57. */
  58. struct k3_r5_mem {
  59. void __iomem *cpu_addr;
  60. phys_addr_t bus_addr;
  61. u32 dev_addr;
  62. size_t size;
  63. };
  64. /*
  65. * All cluster mode values are not applicable on all SoCs. The following
  66. * are the modes supported on various SoCs:
  67. * Split mode : AM65x, J721E, J7200 and AM64x SoCs
  68. * LockStep mode : AM65x, J721E and J7200 SoCs
  69. * Single-CPU mode : AM64x SoCs only
  70. * Single-Core mode : AM62x, AM62A SoCs
  71. */
  72. enum cluster_mode {
  73. CLUSTER_MODE_SPLIT = 0,
  74. CLUSTER_MODE_LOCKSTEP,
  75. CLUSTER_MODE_SINGLECPU,
  76. CLUSTER_MODE_SINGLECORE
  77. };
  78. /**
  79. * struct k3_r5_soc_data - match data to handle SoC variations
  80. * @tcm_is_double: flag to denote the larger unified TCMs in certain modes
  81. * @tcm_ecc_autoinit: flag to denote the auto-initialization of TCMs for ECC
  82. * @single_cpu_mode: flag to denote if SoC/IP supports Single-CPU mode
  83. * @is_single_core: flag to denote if SoC/IP has only single core R5
  84. */
  85. struct k3_r5_soc_data {
  86. bool tcm_is_double;
  87. bool tcm_ecc_autoinit;
  88. bool single_cpu_mode;
  89. bool is_single_core;
  90. };
  91. /**
  92. * struct k3_r5_cluster - K3 R5F Cluster structure
  93. * @dev: cached device pointer
  94. * @mode: Mode to configure the Cluster - Split or LockStep
  95. * @cores: list of R5 cores within the cluster
  96. * @core_transition: wait queue to sync core state changes
  97. * @soc_data: SoC-specific feature data for a R5FSS
  98. */
  99. struct k3_r5_cluster {
  100. struct device *dev;
  101. enum cluster_mode mode;
  102. struct list_head cores;
  103. wait_queue_head_t core_transition;
  104. const struct k3_r5_soc_data *soc_data;
  105. };
  106. /**
  107. * struct k3_r5_core - K3 R5 core structure
  108. * @elem: linked list item
  109. * @dev: cached device pointer
  110. * @rproc: rproc handle representing this core
  111. * @mem: internal memory regions data
  112. * @sram: on-chip SRAM memory regions data
  113. * @num_mems: number of internal memory regions
  114. * @num_sram: number of on-chip SRAM memory regions
  115. * @reset: reset control handle
  116. * @tsp: TI-SCI processor control handle
  117. * @ti_sci: TI-SCI handle
  118. * @ti_sci_id: TI-SCI device identifier
  119. * @atcm_enable: flag to control ATCM enablement
  120. * @btcm_enable: flag to control BTCM enablement
  121. * @loczrama: flag to dictate which TCM is at device address 0x0
  122. * @released_from_reset: flag to signal when core is out of reset
  123. */
  124. struct k3_r5_core {
  125. struct list_head elem;
  126. struct device *dev;
  127. struct rproc *rproc;
  128. struct k3_r5_mem *mem;
  129. struct k3_r5_mem *sram;
  130. int num_mems;
  131. int num_sram;
  132. struct reset_control *reset;
  133. struct ti_sci_proc *tsp;
  134. const struct ti_sci_handle *ti_sci;
  135. u32 ti_sci_id;
  136. u32 atcm_enable;
  137. u32 btcm_enable;
  138. u32 loczrama;
  139. bool released_from_reset;
  140. };
  141. /**
  142. * struct k3_r5_rproc - K3 remote processor state
  143. * @dev: cached device pointer
  144. * @cluster: cached pointer to parent cluster structure
  145. * @mbox: mailbox channel handle
  146. * @client: mailbox client to request the mailbox channel
  147. * @rproc: rproc handle
  148. * @core: cached pointer to r5 core structure being used
  149. * @rmem: reserved memory regions data
  150. * @num_rmems: number of reserved memory regions
  151. */
  152. struct k3_r5_rproc {
  153. struct device *dev;
  154. struct k3_r5_cluster *cluster;
  155. struct mbox_chan *mbox;
  156. struct mbox_client client;
  157. struct rproc *rproc;
  158. struct k3_r5_core *core;
  159. struct k3_r5_mem *rmem;
  160. int num_rmems;
  161. };
  162. /**
  163. * k3_r5_rproc_mbox_callback() - inbound mailbox message handler
  164. * @client: mailbox client pointer used for requesting the mailbox channel
  165. * @data: mailbox payload
  166. *
  167. * This handler is invoked by the OMAP mailbox driver whenever a mailbox
  168. * message is received. Usually, the mailbox payload simply contains
  169. * the index of the virtqueue that is kicked by the remote processor,
  170. * and we let remoteproc core handle it.
  171. *
  172. * In addition to virtqueue indices, we also have some out-of-band values
  173. * that indicate different events. Those values are deliberately very
  174. * large so they don't coincide with virtqueue indices.
  175. */
  176. static void k3_r5_rproc_mbox_callback(struct mbox_client *client, void *data)
  177. {
  178. struct k3_r5_rproc *kproc = container_of(client, struct k3_r5_rproc,
  179. client);
  180. struct device *dev = kproc->rproc->dev.parent;
  181. const char *name = kproc->rproc->name;
  182. u32 msg = omap_mbox_message(data);
  183. dev_dbg(dev, "mbox msg: 0x%x\n", msg);
  184. switch (msg) {
  185. case RP_MBOX_CRASH:
  186. /*
  187. * remoteproc detected an exception, but error recovery is not
  188. * supported. So, just log this for now
  189. */
  190. dev_err(dev, "K3 R5F rproc %s crashed\n", name);
  191. break;
  192. case RP_MBOX_ECHO_REPLY:
  193. dev_info(dev, "received echo reply from %s\n", name);
  194. break;
  195. default:
  196. /* silently handle all other valid messages */
  197. if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
  198. return;
  199. if (msg > kproc->rproc->max_notifyid) {
  200. dev_dbg(dev, "dropping unknown message 0x%x", msg);
  201. return;
  202. }
  203. /* msg contains the index of the triggered vring */
  204. if (rproc_vq_interrupt(kproc->rproc, msg) == IRQ_NONE)
  205. dev_dbg(dev, "no message was found in vqid %d\n", msg);
  206. }
  207. }
  208. /* kick a virtqueue */
  209. static void k3_r5_rproc_kick(struct rproc *rproc, int vqid)
  210. {
  211. struct k3_r5_rproc *kproc = rproc->priv;
  212. struct device *dev = rproc->dev.parent;
  213. mbox_msg_t msg = (mbox_msg_t)vqid;
  214. int ret;
  215. /* send the index of the triggered virtqueue in the mailbox payload */
  216. ret = mbox_send_message(kproc->mbox, (void *)msg);
  217. if (ret < 0)
  218. dev_err(dev, "failed to send mailbox message, status = %d\n",
  219. ret);
  220. }
  221. static int k3_r5_split_reset(struct k3_r5_core *core)
  222. {
  223. int ret;
  224. ret = reset_control_assert(core->reset);
  225. if (ret) {
  226. dev_err(core->dev, "local-reset assert failed, ret = %d\n",
  227. ret);
  228. return ret;
  229. }
  230. ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
  231. core->ti_sci_id);
  232. if (ret) {
  233. dev_err(core->dev, "module-reset assert failed, ret = %d\n",
  234. ret);
  235. if (reset_control_deassert(core->reset))
  236. dev_warn(core->dev, "local-reset deassert back failed\n");
  237. }
  238. return ret;
  239. }
  240. static int k3_r5_split_release(struct k3_r5_core *core)
  241. {
  242. int ret;
  243. ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
  244. core->ti_sci_id);
  245. if (ret) {
  246. dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
  247. ret);
  248. return ret;
  249. }
  250. ret = reset_control_deassert(core->reset);
  251. if (ret) {
  252. dev_err(core->dev, "local-reset deassert failed, ret = %d\n",
  253. ret);
  254. if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
  255. core->ti_sci_id))
  256. dev_warn(core->dev, "module-reset assert back failed\n");
  257. }
  258. return ret;
  259. }
  260. static int k3_r5_lockstep_reset(struct k3_r5_cluster *cluster)
  261. {
  262. struct k3_r5_core *core;
  263. int ret;
  264. /* assert local reset on all applicable cores */
  265. list_for_each_entry(core, &cluster->cores, elem) {
  266. ret = reset_control_assert(core->reset);
  267. if (ret) {
  268. dev_err(core->dev, "local-reset assert failed, ret = %d\n",
  269. ret);
  270. core = list_prev_entry(core, elem);
  271. goto unroll_local_reset;
  272. }
  273. }
  274. /* disable PSC modules on all applicable cores */
  275. list_for_each_entry(core, &cluster->cores, elem) {
  276. ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
  277. core->ti_sci_id);
  278. if (ret) {
  279. dev_err(core->dev, "module-reset assert failed, ret = %d\n",
  280. ret);
  281. goto unroll_module_reset;
  282. }
  283. }
  284. return 0;
  285. unroll_module_reset:
  286. list_for_each_entry_continue_reverse(core, &cluster->cores, elem) {
  287. if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
  288. core->ti_sci_id))
  289. dev_warn(core->dev, "module-reset assert back failed\n");
  290. }
  291. core = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  292. unroll_local_reset:
  293. list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
  294. if (reset_control_deassert(core->reset))
  295. dev_warn(core->dev, "local-reset deassert back failed\n");
  296. }
  297. return ret;
  298. }
  299. static int k3_r5_lockstep_release(struct k3_r5_cluster *cluster)
  300. {
  301. struct k3_r5_core *core;
  302. int ret;
  303. /* enable PSC modules on all applicable cores */
  304. list_for_each_entry_reverse(core, &cluster->cores, elem) {
  305. ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
  306. core->ti_sci_id);
  307. if (ret) {
  308. dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
  309. ret);
  310. core = list_next_entry(core, elem);
  311. goto unroll_module_reset;
  312. }
  313. }
  314. /* deassert local reset on all applicable cores */
  315. list_for_each_entry_reverse(core, &cluster->cores, elem) {
  316. ret = reset_control_deassert(core->reset);
  317. if (ret) {
  318. dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
  319. ret);
  320. goto unroll_local_reset;
  321. }
  322. }
  323. return 0;
  324. unroll_local_reset:
  325. list_for_each_entry_continue(core, &cluster->cores, elem) {
  326. if (reset_control_assert(core->reset))
  327. dev_warn(core->dev, "local-reset assert back failed\n");
  328. }
  329. core = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  330. unroll_module_reset:
  331. list_for_each_entry_from(core, &cluster->cores, elem) {
  332. if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
  333. core->ti_sci_id))
  334. dev_warn(core->dev, "module-reset assert back failed\n");
  335. }
  336. return ret;
  337. }
  338. static inline int k3_r5_core_halt(struct k3_r5_core *core)
  339. {
  340. return ti_sci_proc_set_control(core->tsp,
  341. PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
  342. }
  343. static inline int k3_r5_core_run(struct k3_r5_core *core)
  344. {
  345. return ti_sci_proc_set_control(core->tsp,
  346. 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
  347. }
  348. static int k3_r5_rproc_request_mbox(struct rproc *rproc)
  349. {
  350. struct k3_r5_rproc *kproc = rproc->priv;
  351. struct mbox_client *client = &kproc->client;
  352. struct device *dev = kproc->dev;
  353. int ret;
  354. client->dev = dev;
  355. client->tx_done = NULL;
  356. client->rx_callback = k3_r5_rproc_mbox_callback;
  357. client->tx_block = false;
  358. client->knows_txdone = false;
  359. kproc->mbox = mbox_request_channel(client, 0);
  360. if (IS_ERR(kproc->mbox))
  361. return dev_err_probe(dev, PTR_ERR(kproc->mbox),
  362. "mbox_request_channel failed\n");
  363. /*
  364. * Ping the remote processor, this is only for sanity-sake for now;
  365. * there is no functional effect whatsoever.
  366. *
  367. * Note that the reply will _not_ arrive immediately: this message
  368. * will wait in the mailbox fifo until the remote processor is booted.
  369. */
  370. ret = mbox_send_message(kproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
  371. if (ret < 0) {
  372. dev_err(dev, "mbox_send_message failed: %d\n", ret);
  373. mbox_free_channel(kproc->mbox);
  374. return ret;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * The R5F cores have controls for both a reset and a halt/run. The code
  380. * execution from DDR requires the initial boot-strapping code to be run
  381. * from the internal TCMs. This function is used to release the resets on
  382. * applicable cores to allow loading into the TCMs. The .prepare() ops is
  383. * invoked by remoteproc core before any firmware loading, and is followed
  384. * by the .start() ops after loading to actually let the R5 cores run.
  385. *
  386. * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to
  387. * execute code, but combines the TCMs from both cores. The resets for both
  388. * cores need to be released to make this possible, as the TCMs are in general
  389. * private to each core. Only Core0 needs to be unhalted for running the
  390. * cluster in this mode. The function uses the same reset logic as LockStep
  391. * mode for this (though the behavior is agnostic of the reset release order).
  392. * This callback is invoked only in remoteproc mode.
  393. */
  394. static int k3_r5_rproc_prepare(struct rproc *rproc)
  395. {
  396. struct k3_r5_rproc *kproc = rproc->priv;
  397. struct k3_r5_cluster *cluster = kproc->cluster;
  398. struct k3_r5_core *core = kproc->core, *core0, *core1;
  399. struct device *dev = kproc->dev;
  400. u32 ctrl = 0, cfg = 0, stat = 0;
  401. u64 boot_vec = 0;
  402. bool mem_init_dis;
  403. int ret;
  404. /*
  405. * R5 cores require to be powered on sequentially, core0 should be in
  406. * higher power state than core1 in a cluster. So, wait for core0 to
  407. * power up before proceeding to core1 and put timeout of 2sec. This
  408. * waiting mechanism is necessary because rproc_auto_boot_callback() for
  409. * core1 can be called before core0 due to thread execution order.
  410. *
  411. * By placing the wait mechanism here in .prepare() ops, this condition
  412. * is enforced for rproc boot requests from sysfs as well.
  413. */
  414. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  415. core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  416. if (cluster->mode == CLUSTER_MODE_SPLIT && core == core1 &&
  417. !core0->released_from_reset) {
  418. ret = wait_event_interruptible_timeout(cluster->core_transition,
  419. core0->released_from_reset,
  420. msecs_to_jiffies(2000));
  421. if (ret <= 0) {
  422. dev_err(dev, "can not power up core1 before core0");
  423. return -EPERM;
  424. }
  425. }
  426. ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl, &stat);
  427. if (ret < 0)
  428. return ret;
  429. mem_init_dis = !!(cfg & PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS);
  430. /* Re-use LockStep-mode reset logic for Single-CPU mode */
  431. ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  432. cluster->mode == CLUSTER_MODE_SINGLECPU) ?
  433. k3_r5_lockstep_release(cluster) : k3_r5_split_release(core);
  434. if (ret) {
  435. dev_err(dev, "unable to enable cores for TCM loading, ret = %d\n",
  436. ret);
  437. return ret;
  438. }
  439. /*
  440. * Notify all threads in the wait queue when core0 state has changed so
  441. * that threads waiting for this condition can be executed.
  442. */
  443. core->released_from_reset = true;
  444. if (core == core0)
  445. wake_up_interruptible(&cluster->core_transition);
  446. /*
  447. * Newer IP revisions like on J7200 SoCs support h/w auto-initialization
  448. * of TCMs, so there is no need to perform the s/w memzero. This bit is
  449. * configurable through System Firmware, the default value does perform
  450. * auto-init, but account for it in case it is disabled
  451. */
  452. if (cluster->soc_data->tcm_ecc_autoinit && !mem_init_dis) {
  453. dev_dbg(dev, "leveraging h/w init for TCM memories\n");
  454. return 0;
  455. }
  456. /*
  457. * Zero out both TCMs unconditionally (access from v8 Arm core is not
  458. * affected by ATCM & BTCM enable configuration values) so that ECC
  459. * can be effective on all TCM addresses.
  460. */
  461. dev_dbg(dev, "zeroing out ATCM memory\n");
  462. memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size);
  463. dev_dbg(dev, "zeroing out BTCM memory\n");
  464. memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size);
  465. return 0;
  466. }
  467. /*
  468. * This function implements the .unprepare() ops and performs the complimentary
  469. * operations to that of the .prepare() ops. The function is used to assert the
  470. * resets on all applicable cores for the rproc device (depending on LockStep
  471. * or Split mode). This completes the second portion of powering down the R5F
  472. * cores. The cores themselves are only halted in the .stop() ops, and the
  473. * .unprepare() ops is invoked by the remoteproc core after the remoteproc is
  474. * stopped.
  475. *
  476. * The Single-CPU mode on applicable SoCs (eg: AM64x) combines the TCMs from
  477. * both cores. The access is made possible only with releasing the resets for
  478. * both cores, but with only Core0 unhalted. This function re-uses the same
  479. * reset assert logic as LockStep mode for this mode (though the behavior is
  480. * agnostic of the reset assert order). This callback is invoked only in
  481. * remoteproc mode.
  482. */
  483. static int k3_r5_rproc_unprepare(struct rproc *rproc)
  484. {
  485. struct k3_r5_rproc *kproc = rproc->priv;
  486. struct k3_r5_cluster *cluster = kproc->cluster;
  487. struct k3_r5_core *core = kproc->core, *core0, *core1;
  488. struct device *dev = kproc->dev;
  489. int ret;
  490. /*
  491. * Ensure power-down of cores is sequential in split mode. Core1 must
  492. * power down before Core0 to maintain the expected state. By placing
  493. * the wait mechanism here in .unprepare() ops, this condition is
  494. * enforced for rproc stop or shutdown requests from sysfs and device
  495. * removal as well.
  496. */
  497. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  498. core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  499. if (cluster->mode == CLUSTER_MODE_SPLIT && core == core0 &&
  500. core1->released_from_reset) {
  501. ret = wait_event_interruptible_timeout(cluster->core_transition,
  502. !core1->released_from_reset,
  503. msecs_to_jiffies(2000));
  504. if (ret <= 0) {
  505. dev_err(dev, "can not power down core0 before core1");
  506. return -EPERM;
  507. }
  508. }
  509. /* Re-use LockStep-mode reset logic for Single-CPU mode */
  510. ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  511. cluster->mode == CLUSTER_MODE_SINGLECPU) ?
  512. k3_r5_lockstep_reset(cluster) : k3_r5_split_reset(core);
  513. if (ret)
  514. dev_err(dev, "unable to disable cores, ret = %d\n", ret);
  515. /*
  516. * Notify all threads in the wait queue when core1 state has changed so
  517. * that threads waiting for this condition can be executed.
  518. */
  519. core->released_from_reset = false;
  520. if (core == core1)
  521. wake_up_interruptible(&cluster->core_transition);
  522. return ret;
  523. }
  524. /*
  525. * The R5F start sequence includes two different operations
  526. * 1. Configure the boot vector for R5F core(s)
  527. * 2. Unhalt/Run the R5F core(s)
  528. *
  529. * The sequence is different between LockStep and Split modes. The LockStep
  530. * mode requires the boot vector to be configured only for Core0, and then
  531. * unhalt both the cores to start the execution - Core1 needs to be unhalted
  532. * first followed by Core0. The Split-mode requires that Core0 to be maintained
  533. * always in a higher power state that Core1 (implying Core1 needs to be started
  534. * always only after Core0 is started).
  535. *
  536. * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to execute
  537. * code, so only Core0 needs to be unhalted. The function uses the same logic
  538. * flow as Split-mode for this. This callback is invoked only in remoteproc
  539. * mode.
  540. */
  541. static int k3_r5_rproc_start(struct rproc *rproc)
  542. {
  543. struct k3_r5_rproc *kproc = rproc->priv;
  544. struct k3_r5_cluster *cluster = kproc->cluster;
  545. struct device *dev = kproc->dev;
  546. struct k3_r5_core *core;
  547. u32 boot_addr;
  548. int ret;
  549. boot_addr = rproc->bootaddr;
  550. /* TODO: add boot_addr sanity checking */
  551. dev_dbg(dev, "booting R5F core using boot addr = 0x%x\n", boot_addr);
  552. /* boot vector need not be programmed for Core1 in LockStep mode */
  553. core = kproc->core;
  554. ret = ti_sci_proc_set_config(core->tsp, boot_addr, 0, 0);
  555. if (ret)
  556. return ret;
  557. /* unhalt/run all applicable cores */
  558. if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  559. list_for_each_entry_reverse(core, &cluster->cores, elem) {
  560. ret = k3_r5_core_run(core);
  561. if (ret)
  562. goto unroll_core_run;
  563. }
  564. } else {
  565. ret = k3_r5_core_run(core);
  566. if (ret)
  567. return ret;
  568. }
  569. return 0;
  570. unroll_core_run:
  571. list_for_each_entry_continue(core, &cluster->cores, elem) {
  572. if (k3_r5_core_halt(core))
  573. dev_warn(core->dev, "core halt back failed\n");
  574. }
  575. return ret;
  576. }
  577. /*
  578. * The R5F stop function includes the following operations
  579. * 1. Halt R5F core(s)
  580. *
  581. * The sequence is different between LockStep and Split modes, and the order
  582. * of cores the operations are performed are also in general reverse to that
  583. * of the start function. The LockStep mode requires each operation to be
  584. * performed first on Core0 followed by Core1. The Split-mode requires that
  585. * Core0 to be maintained always in a higher power state that Core1 (implying
  586. * Core1 needs to be stopped first before Core0).
  587. *
  588. * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to execute
  589. * code, so only Core0 needs to be halted. The function uses the same logic
  590. * flow as Split-mode for this.
  591. *
  592. * Note that the R5F halt operation in general is not effective when the R5F
  593. * core is running, but is needed to make sure the core won't run after
  594. * deasserting the reset the subsequent time. The asserting of reset can
  595. * be done here, but is preferred to be done in the .unprepare() ops - this
  596. * maintains the symmetric behavior between the .start(), .stop(), .prepare()
  597. * and .unprepare() ops, and also balances them well between sysfs 'state'
  598. * flow and device bind/unbind or module removal. This callback is invoked
  599. * only in remoteproc mode.
  600. */
  601. static int k3_r5_rproc_stop(struct rproc *rproc)
  602. {
  603. struct k3_r5_rproc *kproc = rproc->priv;
  604. struct k3_r5_cluster *cluster = kproc->cluster;
  605. struct k3_r5_core *core = kproc->core;
  606. int ret;
  607. /* halt all applicable cores */
  608. if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  609. list_for_each_entry(core, &cluster->cores, elem) {
  610. ret = k3_r5_core_halt(core);
  611. if (ret) {
  612. core = list_prev_entry(core, elem);
  613. goto unroll_core_halt;
  614. }
  615. }
  616. } else {
  617. ret = k3_r5_core_halt(core);
  618. if (ret)
  619. goto out;
  620. }
  621. return 0;
  622. unroll_core_halt:
  623. list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
  624. if (k3_r5_core_run(core))
  625. dev_warn(core->dev, "core run back failed\n");
  626. }
  627. out:
  628. return ret;
  629. }
  630. /*
  631. * Attach to a running R5F remote processor (IPC-only mode)
  632. *
  633. * The R5F attach callback is a NOP. The remote processor is already booted, and
  634. * all required resources have been acquired during probe routine, so there is
  635. * no need to issue any TI-SCI commands to boot the R5F cores in IPC-only mode.
  636. * This callback is invoked only in IPC-only mode and exists because
  637. * rproc_validate() checks for its existence.
  638. */
  639. static int k3_r5_rproc_attach(struct rproc *rproc) { return 0; }
  640. /*
  641. * Detach from a running R5F remote processor (IPC-only mode)
  642. *
  643. * The R5F detach callback is a NOP. The R5F cores are not stopped and will be
  644. * left in booted state in IPC-only mode. This callback is invoked only in
  645. * IPC-only mode and exists for sanity sake.
  646. */
  647. static int k3_r5_rproc_detach(struct rproc *rproc) { return 0; }
  648. /*
  649. * This function implements the .get_loaded_rsc_table() callback and is used
  650. * to provide the resource table for the booted R5F in IPC-only mode. The K3 R5F
  651. * firmwares follow a design-by-contract approach and are expected to have the
  652. * resource table at the base of the DDR region reserved for firmware usage.
  653. * This provides flexibility for the remote processor to be booted by different
  654. * bootloaders that may or may not have the ability to publish the resource table
  655. * address and size through a DT property. This callback is invoked only in
  656. * IPC-only mode.
  657. */
  658. static struct resource_table *k3_r5_get_loaded_rsc_table(struct rproc *rproc,
  659. size_t *rsc_table_sz)
  660. {
  661. struct k3_r5_rproc *kproc = rproc->priv;
  662. struct device *dev = kproc->dev;
  663. if (!kproc->rmem[0].cpu_addr) {
  664. dev_err(dev, "memory-region #1 does not exist, loaded rsc table can't be found");
  665. return ERR_PTR(-ENOMEM);
  666. }
  667. /*
  668. * NOTE: The resource table size is currently hard-coded to a maximum
  669. * of 256 bytes. The most common resource table usage for K3 firmwares
  670. * is to only have the vdev resource entry and an optional trace entry.
  671. * The exact size could be computed based on resource table address, but
  672. * the hard-coded value suffices to support the IPC-only mode.
  673. */
  674. *rsc_table_sz = 256;
  675. return (struct resource_table *)kproc->rmem[0].cpu_addr;
  676. }
  677. /*
  678. * Internal Memory translation helper
  679. *
  680. * Custom function implementing the rproc .da_to_va ops to provide address
  681. * translation (device address to kernel virtual address) for internal RAMs
  682. * present in a DSP or IPU device). The translated addresses can be used
  683. * either by the remoteproc core for loading, or by any rpmsg bus drivers.
  684. */
  685. static void *k3_r5_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  686. {
  687. struct k3_r5_rproc *kproc = rproc->priv;
  688. struct k3_r5_core *core = kproc->core;
  689. void __iomem *va = NULL;
  690. phys_addr_t bus_addr;
  691. u32 dev_addr, offset;
  692. size_t size;
  693. int i;
  694. if (len == 0)
  695. return NULL;
  696. /* handle both R5 and SoC views of ATCM and BTCM */
  697. for (i = 0; i < core->num_mems; i++) {
  698. bus_addr = core->mem[i].bus_addr;
  699. dev_addr = core->mem[i].dev_addr;
  700. size = core->mem[i].size;
  701. /* handle R5-view addresses of TCMs */
  702. if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
  703. offset = da - dev_addr;
  704. va = core->mem[i].cpu_addr + offset;
  705. return (__force void *)va;
  706. }
  707. /* handle SoC-view addresses of TCMs */
  708. if (da >= bus_addr && ((da + len) <= (bus_addr + size))) {
  709. offset = da - bus_addr;
  710. va = core->mem[i].cpu_addr + offset;
  711. return (__force void *)va;
  712. }
  713. }
  714. /* handle any SRAM regions using SoC-view addresses */
  715. for (i = 0; i < core->num_sram; i++) {
  716. dev_addr = core->sram[i].dev_addr;
  717. size = core->sram[i].size;
  718. if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
  719. offset = da - dev_addr;
  720. va = core->sram[i].cpu_addr + offset;
  721. return (__force void *)va;
  722. }
  723. }
  724. /* handle static DDR reserved memory regions */
  725. for (i = 0; i < kproc->num_rmems; i++) {
  726. dev_addr = kproc->rmem[i].dev_addr;
  727. size = kproc->rmem[i].size;
  728. if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
  729. offset = da - dev_addr;
  730. va = kproc->rmem[i].cpu_addr + offset;
  731. return (__force void *)va;
  732. }
  733. }
  734. return NULL;
  735. }
  736. static const struct rproc_ops k3_r5_rproc_ops = {
  737. .prepare = k3_r5_rproc_prepare,
  738. .unprepare = k3_r5_rproc_unprepare,
  739. .start = k3_r5_rproc_start,
  740. .stop = k3_r5_rproc_stop,
  741. .kick = k3_r5_rproc_kick,
  742. .da_to_va = k3_r5_rproc_da_to_va,
  743. };
  744. /*
  745. * Internal R5F Core configuration
  746. *
  747. * Each R5FSS has a cluster-level setting for configuring the processor
  748. * subsystem either in a safety/fault-tolerant LockStep mode or a performance
  749. * oriented Split mode on most SoCs. A fewer SoCs support a non-safety mode
  750. * as an alternate for LockStep mode that exercises only a single R5F core
  751. * called Single-CPU mode. Each R5F core has a number of settings to either
  752. * enable/disable each of the TCMs, control which TCM appears at the R5F core's
  753. * address 0x0. These settings need to be configured before the resets for the
  754. * corresponding core are released. These settings are all protected and managed
  755. * by the System Processor.
  756. *
  757. * This function is used to pre-configure these settings for each R5F core, and
  758. * the configuration is all done through various ti_sci_proc functions that
  759. * communicate with the System Processor. The function also ensures that both
  760. * the cores are halted before the .prepare() step.
  761. *
  762. * The function is called from k3_r5_cluster_rproc_init() and is invoked either
  763. * once (in LockStep mode or Single-CPU modes) or twice (in Split mode). Support
  764. * for LockStep-mode is dictated by an eFUSE register bit, and the config
  765. * settings retrieved from DT are adjusted accordingly as per the permitted
  766. * cluster mode. Another eFUSE register bit dictates if the R5F cluster only
  767. * supports a Single-CPU mode. All cluster level settings like Cluster mode and
  768. * TEINIT (exception handling state dictating ARM or Thumb mode) can only be set
  769. * and retrieved using Core0.
  770. *
  771. * The function behavior is different based on the cluster mode. The R5F cores
  772. * are configured independently as per their individual settings in Split mode.
  773. * They are identically configured in LockStep mode using the primary Core0
  774. * settings. However, some individual settings cannot be set in LockStep mode.
  775. * This is overcome by switching to Split-mode initially and then programming
  776. * both the cores with the same settings, before reconfiguing again for
  777. * LockStep mode.
  778. */
  779. static int k3_r5_rproc_configure(struct k3_r5_rproc *kproc)
  780. {
  781. struct k3_r5_cluster *cluster = kproc->cluster;
  782. struct device *dev = kproc->dev;
  783. struct k3_r5_core *core0, *core, *temp;
  784. u32 ctrl = 0, cfg = 0, stat = 0;
  785. u32 set_cfg = 0, clr_cfg = 0;
  786. u64 boot_vec = 0;
  787. bool lockstep_en;
  788. bool single_cpu;
  789. int ret;
  790. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  791. if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  792. cluster->mode == CLUSTER_MODE_SINGLECPU ||
  793. cluster->mode == CLUSTER_MODE_SINGLECORE) {
  794. core = core0;
  795. } else {
  796. core = kproc->core;
  797. }
  798. ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
  799. &stat);
  800. if (ret < 0)
  801. return ret;
  802. dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
  803. boot_vec, cfg, ctrl, stat);
  804. single_cpu = !!(stat & PROC_BOOT_STATUS_FLAG_R5_SINGLECORE_ONLY);
  805. lockstep_en = !!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED);
  806. /* Override to single CPU mode if set in status flag */
  807. if (single_cpu && cluster->mode == CLUSTER_MODE_SPLIT) {
  808. dev_err(cluster->dev, "split-mode not permitted, force configuring for single-cpu mode\n");
  809. cluster->mode = CLUSTER_MODE_SINGLECPU;
  810. }
  811. /* Override to split mode if lockstep enable bit is not set in status flag */
  812. if (!lockstep_en && cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  813. dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
  814. cluster->mode = CLUSTER_MODE_SPLIT;
  815. }
  816. /* always enable ARM mode and set boot vector to 0 */
  817. boot_vec = 0x0;
  818. if (core == core0) {
  819. clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
  820. /*
  821. * Single-CPU configuration bit can only be configured
  822. * on Core0 and system firmware will NACK any requests
  823. * with the bit configured, so program it only on
  824. * permitted cores
  825. */
  826. if (cluster->mode == CLUSTER_MODE_SINGLECPU ||
  827. cluster->mode == CLUSTER_MODE_SINGLECORE) {
  828. set_cfg = PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE;
  829. } else {
  830. /*
  831. * LockStep configuration bit is Read-only on Split-mode
  832. * _only_ devices and system firmware will NACK any
  833. * requests with the bit configured, so program it only
  834. * on permitted devices
  835. */
  836. if (lockstep_en)
  837. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
  838. }
  839. }
  840. if (core->atcm_enable)
  841. set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
  842. else
  843. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
  844. if (core->btcm_enable)
  845. set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
  846. else
  847. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
  848. if (core->loczrama)
  849. set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
  850. else
  851. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
  852. if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  853. /*
  854. * work around system firmware limitations to make sure both
  855. * cores are programmed symmetrically in LockStep. LockStep
  856. * and TEINIT config is only allowed with Core0.
  857. */
  858. list_for_each_entry(temp, &cluster->cores, elem) {
  859. ret = k3_r5_core_halt(temp);
  860. if (ret)
  861. goto out;
  862. if (temp != core) {
  863. clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
  864. clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
  865. }
  866. ret = ti_sci_proc_set_config(temp->tsp, boot_vec,
  867. set_cfg, clr_cfg);
  868. if (ret)
  869. goto out;
  870. }
  871. set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
  872. clr_cfg = 0;
  873. ret = ti_sci_proc_set_config(core->tsp, boot_vec,
  874. set_cfg, clr_cfg);
  875. } else {
  876. ret = k3_r5_core_halt(core);
  877. if (ret)
  878. goto out;
  879. ret = ti_sci_proc_set_config(core->tsp, boot_vec,
  880. set_cfg, clr_cfg);
  881. }
  882. out:
  883. return ret;
  884. }
  885. static void k3_r5_mem_release(void *data)
  886. {
  887. struct device *dev = data;
  888. of_reserved_mem_device_release(dev);
  889. }
  890. static int k3_r5_reserved_mem_init(struct k3_r5_rproc *kproc)
  891. {
  892. struct device *dev = kproc->dev;
  893. struct device_node *np = dev_of_node(dev);
  894. struct device_node *rmem_np;
  895. struct reserved_mem *rmem;
  896. int num_rmems;
  897. int ret, i;
  898. num_rmems = of_property_count_elems_of_size(np, "memory-region",
  899. sizeof(phandle));
  900. if (num_rmems <= 0) {
  901. dev_err(dev, "device does not have reserved memory regions, ret = %d\n",
  902. num_rmems);
  903. return -EINVAL;
  904. }
  905. if (num_rmems < 2) {
  906. dev_err(dev, "device needs at least two memory regions to be defined, num = %d\n",
  907. num_rmems);
  908. return -EINVAL;
  909. }
  910. /* use reserved memory region 0 for vring DMA allocations */
  911. ret = of_reserved_mem_device_init_by_idx(dev, np, 0);
  912. if (ret) {
  913. dev_err(dev, "device cannot initialize DMA pool, ret = %d\n",
  914. ret);
  915. return ret;
  916. }
  917. ret = devm_add_action_or_reset(dev, k3_r5_mem_release, dev);
  918. if (ret)
  919. return ret;
  920. num_rmems--;
  921. kproc->rmem = devm_kcalloc(dev, num_rmems, sizeof(*kproc->rmem), GFP_KERNEL);
  922. if (!kproc->rmem)
  923. return -ENOMEM;
  924. /* use remaining reserved memory regions for static carveouts */
  925. for (i = 0; i < num_rmems; i++) {
  926. rmem_np = of_parse_phandle(np, "memory-region", i + 1);
  927. if (!rmem_np)
  928. return -EINVAL;
  929. rmem = of_reserved_mem_lookup(rmem_np);
  930. of_node_put(rmem_np);
  931. if (!rmem)
  932. return -EINVAL;
  933. kproc->rmem[i].bus_addr = rmem->base;
  934. /*
  935. * R5Fs do not have an MMU, but have a Region Address Translator
  936. * (RAT) module that provides a fixed entry translation between
  937. * the 32-bit processor addresses to 64-bit bus addresses. The
  938. * RAT is programmable only by the R5F cores. Support for RAT
  939. * is currently not supported, so 64-bit address regions are not
  940. * supported. The absence of MMUs implies that the R5F device
  941. * addresses/supported memory regions are restricted to 32-bit
  942. * bus addresses, and are identical
  943. */
  944. kproc->rmem[i].dev_addr = (u32)rmem->base;
  945. kproc->rmem[i].size = rmem->size;
  946. kproc->rmem[i].cpu_addr = devm_ioremap_wc(dev, rmem->base, rmem->size);
  947. if (!kproc->rmem[i].cpu_addr) {
  948. dev_err(dev, "failed to map reserved memory#%d at %pa of size %pa\n",
  949. i + 1, &rmem->base, &rmem->size);
  950. return -ENOMEM;
  951. }
  952. dev_dbg(dev, "reserved memory%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
  953. i + 1, &kproc->rmem[i].bus_addr,
  954. kproc->rmem[i].size, kproc->rmem[i].cpu_addr,
  955. kproc->rmem[i].dev_addr);
  956. }
  957. kproc->num_rmems = num_rmems;
  958. return 0;
  959. }
  960. /*
  961. * Each R5F core within a typical R5FSS instance has a total of 64 KB of TCMs,
  962. * split equally into two 32 KB banks between ATCM and BTCM. The TCMs from both
  963. * cores are usable in Split-mode, but only the Core0 TCMs can be used in
  964. * LockStep-mode. The newer revisions of the R5FSS IP maximizes these TCMs by
  965. * leveraging the Core1 TCMs as well in certain modes where they would have
  966. * otherwise been unusable (Eg: LockStep-mode on J7200 SoCs, Single-CPU mode on
  967. * AM64x SoCs). This is done by making a Core1 TCM visible immediately after the
  968. * corresponding Core0 TCM. The SoC memory map uses the larger 64 KB sizes for
  969. * the Core0 TCMs, and the dts representation reflects this increased size on
  970. * supported SoCs. The Core0 TCM sizes therefore have to be adjusted to only
  971. * half the original size in Split mode.
  972. */
  973. static void k3_r5_adjust_tcm_sizes(struct k3_r5_rproc *kproc)
  974. {
  975. struct k3_r5_cluster *cluster = kproc->cluster;
  976. struct k3_r5_core *core = kproc->core;
  977. struct device *cdev = core->dev;
  978. struct k3_r5_core *core0;
  979. if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  980. cluster->mode == CLUSTER_MODE_SINGLECPU ||
  981. cluster->mode == CLUSTER_MODE_SINGLECORE ||
  982. !cluster->soc_data->tcm_is_double)
  983. return;
  984. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  985. if (core == core0) {
  986. WARN_ON(core->mem[0].size != SZ_64K);
  987. WARN_ON(core->mem[1].size != SZ_64K);
  988. core->mem[0].size /= 2;
  989. core->mem[1].size /= 2;
  990. dev_dbg(cdev, "adjusted TCM sizes, ATCM = 0x%zx BTCM = 0x%zx\n",
  991. core->mem[0].size, core->mem[1].size);
  992. }
  993. }
  994. /*
  995. * This function checks and configures a R5F core for IPC-only or remoteproc
  996. * mode. The driver is configured to be in IPC-only mode for a R5F core when
  997. * the core has been loaded and started by a bootloader. The IPC-only mode is
  998. * detected by querying the System Firmware for reset, power on and halt status
  999. * and ensuring that the core is running. Any incomplete steps at bootloader
  1000. * are validated and errored out.
  1001. *
  1002. * In IPC-only mode, the driver state flags for ATCM, BTCM and LOCZRAMA settings
  1003. * and cluster mode parsed originally from kernel DT are updated to reflect the
  1004. * actual values configured by bootloader. The driver internal device memory
  1005. * addresses for TCMs are also updated.
  1006. */
  1007. static int k3_r5_rproc_configure_mode(struct k3_r5_rproc *kproc)
  1008. {
  1009. struct k3_r5_cluster *cluster = kproc->cluster;
  1010. struct k3_r5_core *core = kproc->core;
  1011. struct device *cdev = core->dev;
  1012. bool r_state = false, c_state = false, lockstep_en = false, single_cpu = false;
  1013. u32 ctrl = 0, cfg = 0, stat = 0, halted = 0;
  1014. u64 boot_vec = 0;
  1015. u32 atcm_enable, btcm_enable, loczrama;
  1016. struct k3_r5_core *core0;
  1017. enum cluster_mode mode = cluster->mode;
  1018. int reset_ctrl_status;
  1019. int ret;
  1020. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  1021. ret = core->ti_sci->ops.dev_ops.is_on(core->ti_sci, core->ti_sci_id,
  1022. &r_state, &c_state);
  1023. if (ret) {
  1024. dev_err(cdev, "failed to get initial state, mode cannot be determined, ret = %d\n",
  1025. ret);
  1026. return ret;
  1027. }
  1028. if (r_state != c_state) {
  1029. dev_warn(cdev, "R5F core may have been powered on by a different host, programmed state (%d) != actual state (%d)\n",
  1030. r_state, c_state);
  1031. }
  1032. reset_ctrl_status = reset_control_status(core->reset);
  1033. if (reset_ctrl_status < 0) {
  1034. dev_err(cdev, "failed to get initial local reset status, ret = %d\n",
  1035. reset_ctrl_status);
  1036. return reset_ctrl_status;
  1037. }
  1038. /*
  1039. * Skip the waiting mechanism for sequential power-on of cores if the
  1040. * core has already been booted by another entity.
  1041. */
  1042. core->released_from_reset = c_state;
  1043. ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
  1044. &stat);
  1045. if (ret < 0) {
  1046. dev_err(cdev, "failed to get initial processor status, ret = %d\n",
  1047. ret);
  1048. return ret;
  1049. }
  1050. atcm_enable = cfg & PROC_BOOT_CFG_FLAG_R5_ATCM_EN ? 1 : 0;
  1051. btcm_enable = cfg & PROC_BOOT_CFG_FLAG_R5_BTCM_EN ? 1 : 0;
  1052. loczrama = cfg & PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE ? 1 : 0;
  1053. single_cpu = cfg & PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE ? 1 : 0;
  1054. lockstep_en = cfg & PROC_BOOT_CFG_FLAG_R5_LOCKSTEP ? 1 : 0;
  1055. if (single_cpu && mode != CLUSTER_MODE_SINGLECORE)
  1056. mode = CLUSTER_MODE_SINGLECPU;
  1057. if (lockstep_en)
  1058. mode = CLUSTER_MODE_LOCKSTEP;
  1059. halted = ctrl & PROC_BOOT_CTRL_FLAG_R5_CORE_HALT;
  1060. /*
  1061. * IPC-only mode detection requires both local and module resets to
  1062. * be deasserted and R5F core to be unhalted. Local reset status is
  1063. * irrelevant if module reset is asserted (POR value has local reset
  1064. * deasserted), and is deemed as remoteproc mode
  1065. */
  1066. if (c_state && !reset_ctrl_status && !halted) {
  1067. dev_info(cdev, "configured R5F for IPC-only mode\n");
  1068. kproc->rproc->state = RPROC_DETACHED;
  1069. ret = 1;
  1070. /* override rproc ops with only required IPC-only mode ops */
  1071. kproc->rproc->ops->prepare = NULL;
  1072. kproc->rproc->ops->unprepare = NULL;
  1073. kproc->rproc->ops->start = NULL;
  1074. kproc->rproc->ops->stop = NULL;
  1075. kproc->rproc->ops->attach = k3_r5_rproc_attach;
  1076. kproc->rproc->ops->detach = k3_r5_rproc_detach;
  1077. kproc->rproc->ops->get_loaded_rsc_table =
  1078. k3_r5_get_loaded_rsc_table;
  1079. } else if (!c_state) {
  1080. dev_info(cdev, "configured R5F for remoteproc mode\n");
  1081. ret = 0;
  1082. } else {
  1083. dev_err(cdev, "mismatched mode: local_reset = %s, module_reset = %s, core_state = %s\n",
  1084. !reset_ctrl_status ? "deasserted" : "asserted",
  1085. c_state ? "deasserted" : "asserted",
  1086. halted ? "halted" : "unhalted");
  1087. ret = -EINVAL;
  1088. }
  1089. /* fixup TCMs, cluster & core flags to actual values in IPC-only mode */
  1090. if (ret > 0) {
  1091. if (core == core0)
  1092. cluster->mode = mode;
  1093. core->atcm_enable = atcm_enable;
  1094. core->btcm_enable = btcm_enable;
  1095. core->loczrama = loczrama;
  1096. core->mem[0].dev_addr = loczrama ? 0 : K3_R5_TCM_DEV_ADDR;
  1097. core->mem[1].dev_addr = loczrama ? K3_R5_TCM_DEV_ADDR : 0;
  1098. }
  1099. return ret;
  1100. }
  1101. static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
  1102. {
  1103. struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
  1104. struct device *dev = &pdev->dev;
  1105. struct k3_r5_rproc *kproc;
  1106. struct k3_r5_core *core, *core1;
  1107. struct device *cdev;
  1108. const char *fw_name;
  1109. struct rproc *rproc;
  1110. int ret, ret1;
  1111. core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  1112. list_for_each_entry(core, &cluster->cores, elem) {
  1113. cdev = core->dev;
  1114. ret = rproc_of_parse_firmware(cdev, 0, &fw_name);
  1115. if (ret) {
  1116. dev_err(dev, "failed to parse firmware-name property, ret = %d\n",
  1117. ret);
  1118. goto out;
  1119. }
  1120. rproc = devm_rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
  1121. fw_name, sizeof(*kproc));
  1122. if (!rproc) {
  1123. ret = -ENOMEM;
  1124. goto out;
  1125. }
  1126. /* K3 R5s have a Region Address Translator (RAT) but no MMU */
  1127. rproc->has_iommu = false;
  1128. /* error recovery is not supported at present */
  1129. rproc->recovery_disabled = true;
  1130. kproc = rproc->priv;
  1131. kproc->cluster = cluster;
  1132. kproc->core = core;
  1133. kproc->dev = cdev;
  1134. kproc->rproc = rproc;
  1135. core->rproc = rproc;
  1136. ret = k3_r5_rproc_request_mbox(rproc);
  1137. if (ret)
  1138. return ret;
  1139. ret = k3_r5_rproc_configure_mode(kproc);
  1140. if (ret < 0)
  1141. goto out;
  1142. if (ret)
  1143. goto init_rmem;
  1144. ret = k3_r5_rproc_configure(kproc);
  1145. if (ret) {
  1146. dev_err(dev, "initial configure failed, ret = %d\n",
  1147. ret);
  1148. goto out;
  1149. }
  1150. init_rmem:
  1151. k3_r5_adjust_tcm_sizes(kproc);
  1152. ret = k3_r5_reserved_mem_init(kproc);
  1153. if (ret) {
  1154. dev_err(dev, "reserved memory init failed, ret = %d\n",
  1155. ret);
  1156. goto out;
  1157. }
  1158. ret = devm_rproc_add(dev, rproc);
  1159. if (ret) {
  1160. dev_err_probe(dev, ret, "rproc_add failed\n");
  1161. goto out;
  1162. }
  1163. /* create only one rproc in lockstep, single-cpu or
  1164. * single core mode
  1165. */
  1166. if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  1167. cluster->mode == CLUSTER_MODE_SINGLECPU ||
  1168. cluster->mode == CLUSTER_MODE_SINGLECORE)
  1169. break;
  1170. }
  1171. return 0;
  1172. err_split:
  1173. if (rproc->state == RPROC_ATTACHED) {
  1174. ret1 = rproc_detach(rproc);
  1175. if (ret1) {
  1176. dev_err(kproc->dev, "failed to detach rproc, ret = %d\n",
  1177. ret1);
  1178. return ret1;
  1179. }
  1180. }
  1181. out:
  1182. /* undo core0 upon any failures on core1 in split-mode */
  1183. if (cluster->mode == CLUSTER_MODE_SPLIT && core == core1) {
  1184. core = list_prev_entry(core, elem);
  1185. rproc = core->rproc;
  1186. kproc = rproc->priv;
  1187. goto err_split;
  1188. }
  1189. return ret;
  1190. }
  1191. static void k3_r5_cluster_rproc_exit(void *data)
  1192. {
  1193. struct k3_r5_cluster *cluster = platform_get_drvdata(data);
  1194. struct k3_r5_rproc *kproc;
  1195. struct k3_r5_core *core;
  1196. struct rproc *rproc;
  1197. int ret;
  1198. /*
  1199. * lockstep mode and single-cpu modes have only one rproc associated
  1200. * with first core, whereas split-mode has two rprocs associated with
  1201. * each core, and requires that core1 be powered down first
  1202. */
  1203. core = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  1204. cluster->mode == CLUSTER_MODE_SINGLECPU) ?
  1205. list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
  1206. list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  1207. list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
  1208. rproc = core->rproc;
  1209. kproc = rproc->priv;
  1210. if (rproc->state == RPROC_ATTACHED) {
  1211. ret = rproc_detach(rproc);
  1212. if (ret) {
  1213. dev_err(kproc->dev, "failed to detach rproc, ret = %d\n", ret);
  1214. return;
  1215. }
  1216. }
  1217. mbox_free_channel(kproc->mbox);
  1218. }
  1219. }
  1220. static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
  1221. struct k3_r5_core *core)
  1222. {
  1223. static const char * const mem_names[] = {"atcm", "btcm"};
  1224. struct device *dev = &pdev->dev;
  1225. struct resource *res;
  1226. int num_mems;
  1227. int i;
  1228. num_mems = ARRAY_SIZE(mem_names);
  1229. core->mem = devm_kcalloc(dev, num_mems, sizeof(*core->mem), GFP_KERNEL);
  1230. if (!core->mem)
  1231. return -ENOMEM;
  1232. for (i = 0; i < num_mems; i++) {
  1233. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1234. mem_names[i]);
  1235. if (!res) {
  1236. dev_err(dev, "found no memory resource for %s\n",
  1237. mem_names[i]);
  1238. return -EINVAL;
  1239. }
  1240. if (!devm_request_mem_region(dev, res->start,
  1241. resource_size(res),
  1242. dev_name(dev))) {
  1243. dev_err(dev, "could not request %s region for resource\n",
  1244. mem_names[i]);
  1245. return -EBUSY;
  1246. }
  1247. /*
  1248. * TCMs are designed in general to support RAM-like backing
  1249. * memories. So, map these as Normal Non-Cached memories. This
  1250. * also avoids/fixes any potential alignment faults due to
  1251. * unaligned data accesses when using memcpy() or memset()
  1252. * functions (normally seen with device type memory).
  1253. */
  1254. core->mem[i].cpu_addr = devm_ioremap_wc(dev, res->start,
  1255. resource_size(res));
  1256. if (!core->mem[i].cpu_addr) {
  1257. dev_err(dev, "failed to map %s memory\n", mem_names[i]);
  1258. return -ENOMEM;
  1259. }
  1260. core->mem[i].bus_addr = res->start;
  1261. /*
  1262. * TODO:
  1263. * The R5F cores can place ATCM & BTCM anywhere in its address
  1264. * based on the corresponding Region Registers in the System
  1265. * Control coprocessor. For now, place ATCM and BTCM at
  1266. * addresses 0 and 0x41010000 (same as the bus address on AM65x
  1267. * SoCs) based on loczrama setting
  1268. */
  1269. if (!strcmp(mem_names[i], "atcm")) {
  1270. core->mem[i].dev_addr = core->loczrama ?
  1271. 0 : K3_R5_TCM_DEV_ADDR;
  1272. } else {
  1273. core->mem[i].dev_addr = core->loczrama ?
  1274. K3_R5_TCM_DEV_ADDR : 0;
  1275. }
  1276. core->mem[i].size = resource_size(res);
  1277. dev_dbg(dev, "memory %5s: bus addr %pa size 0x%zx va %pK da 0x%x\n",
  1278. mem_names[i], &core->mem[i].bus_addr,
  1279. core->mem[i].size, core->mem[i].cpu_addr,
  1280. core->mem[i].dev_addr);
  1281. }
  1282. core->num_mems = num_mems;
  1283. return 0;
  1284. }
  1285. static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
  1286. struct k3_r5_core *core)
  1287. {
  1288. struct device_node *np = pdev->dev.of_node;
  1289. struct device *dev = &pdev->dev;
  1290. struct device_node *sram_np;
  1291. struct resource res;
  1292. int num_sram;
  1293. int i, ret;
  1294. num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
  1295. if (num_sram <= 0) {
  1296. dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
  1297. num_sram);
  1298. return 0;
  1299. }
  1300. core->sram = devm_kcalloc(dev, num_sram, sizeof(*core->sram), GFP_KERNEL);
  1301. if (!core->sram)
  1302. return -ENOMEM;
  1303. for (i = 0; i < num_sram; i++) {
  1304. sram_np = of_parse_phandle(np, "sram", i);
  1305. if (!sram_np)
  1306. return -EINVAL;
  1307. if (!of_device_is_available(sram_np)) {
  1308. of_node_put(sram_np);
  1309. return -EINVAL;
  1310. }
  1311. ret = of_address_to_resource(sram_np, 0, &res);
  1312. of_node_put(sram_np);
  1313. if (ret)
  1314. return -EINVAL;
  1315. core->sram[i].bus_addr = res.start;
  1316. core->sram[i].dev_addr = res.start;
  1317. core->sram[i].size = resource_size(&res);
  1318. core->sram[i].cpu_addr = devm_ioremap_wc(dev, res.start,
  1319. resource_size(&res));
  1320. if (!core->sram[i].cpu_addr) {
  1321. dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
  1322. i, &res.start);
  1323. return -ENOMEM;
  1324. }
  1325. dev_dbg(dev, "memory sram%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
  1326. i, &core->sram[i].bus_addr,
  1327. core->sram[i].size, core->sram[i].cpu_addr,
  1328. core->sram[i].dev_addr);
  1329. }
  1330. core->num_sram = num_sram;
  1331. return 0;
  1332. }
  1333. static int k3_r5_core_of_init(struct platform_device *pdev)
  1334. {
  1335. struct device *dev = &pdev->dev;
  1336. struct device_node *np = dev_of_node(dev);
  1337. struct k3_r5_core *core;
  1338. int ret;
  1339. if (!devres_open_group(dev, k3_r5_core_of_init, GFP_KERNEL))
  1340. return -ENOMEM;
  1341. core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
  1342. if (!core) {
  1343. ret = -ENOMEM;
  1344. goto err;
  1345. }
  1346. core->dev = dev;
  1347. /*
  1348. * Use SoC Power-on-Reset values as default if no DT properties are
  1349. * used to dictate the TCM configurations
  1350. */
  1351. core->atcm_enable = 0;
  1352. core->btcm_enable = 1;
  1353. core->loczrama = 1;
  1354. ret = of_property_read_u32(np, "ti,atcm-enable", &core->atcm_enable);
  1355. if (ret < 0 && ret != -EINVAL) {
  1356. dev_err(dev, "invalid format for ti,atcm-enable, ret = %d\n",
  1357. ret);
  1358. goto err;
  1359. }
  1360. ret = of_property_read_u32(np, "ti,btcm-enable", &core->btcm_enable);
  1361. if (ret < 0 && ret != -EINVAL) {
  1362. dev_err(dev, "invalid format for ti,btcm-enable, ret = %d\n",
  1363. ret);
  1364. goto err;
  1365. }
  1366. ret = of_property_read_u32(np, "ti,loczrama", &core->loczrama);
  1367. if (ret < 0 && ret != -EINVAL) {
  1368. dev_err(dev, "invalid format for ti,loczrama, ret = %d\n", ret);
  1369. goto err;
  1370. }
  1371. core->ti_sci = devm_ti_sci_get_by_phandle(dev, "ti,sci");
  1372. if (IS_ERR(core->ti_sci)) {
  1373. ret = PTR_ERR(core->ti_sci);
  1374. if (ret != -EPROBE_DEFER) {
  1375. dev_err(dev, "failed to get ti-sci handle, ret = %d\n",
  1376. ret);
  1377. }
  1378. core->ti_sci = NULL;
  1379. goto err;
  1380. }
  1381. ret = of_property_read_u32(np, "ti,sci-dev-id", &core->ti_sci_id);
  1382. if (ret) {
  1383. dev_err(dev, "missing 'ti,sci-dev-id' property\n");
  1384. goto err;
  1385. }
  1386. core->reset = devm_reset_control_get_exclusive(dev, NULL);
  1387. if (IS_ERR_OR_NULL(core->reset)) {
  1388. ret = PTR_ERR_OR_ZERO(core->reset);
  1389. if (!ret)
  1390. ret = -ENODEV;
  1391. if (ret != -EPROBE_DEFER) {
  1392. dev_err(dev, "failed to get reset handle, ret = %d\n",
  1393. ret);
  1394. }
  1395. goto err;
  1396. }
  1397. core->tsp = ti_sci_proc_of_get_tsp(dev, core->ti_sci);
  1398. if (IS_ERR(core->tsp)) {
  1399. ret = PTR_ERR(core->tsp);
  1400. dev_err(dev, "failed to construct ti-sci proc control, ret = %d\n",
  1401. ret);
  1402. goto err;
  1403. }
  1404. ret = k3_r5_core_of_get_internal_memories(pdev, core);
  1405. if (ret) {
  1406. dev_err(dev, "failed to get internal memories, ret = %d\n",
  1407. ret);
  1408. goto err;
  1409. }
  1410. ret = k3_r5_core_of_get_sram_memories(pdev, core);
  1411. if (ret) {
  1412. dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
  1413. goto err;
  1414. }
  1415. ret = ti_sci_proc_request(core->tsp);
  1416. if (ret < 0) {
  1417. dev_err(dev, "ti_sci_proc_request failed, ret = %d\n", ret);
  1418. goto err;
  1419. }
  1420. platform_set_drvdata(pdev, core);
  1421. devres_close_group(dev, k3_r5_core_of_init);
  1422. return 0;
  1423. err:
  1424. devres_release_group(dev, k3_r5_core_of_init);
  1425. return ret;
  1426. }
  1427. /*
  1428. * free the resources explicitly since driver model is not being used
  1429. * for the child R5F devices
  1430. */
  1431. static void k3_r5_core_of_exit(struct platform_device *pdev)
  1432. {
  1433. struct k3_r5_core *core = platform_get_drvdata(pdev);
  1434. struct device *dev = &pdev->dev;
  1435. int ret;
  1436. ret = ti_sci_proc_release(core->tsp);
  1437. if (ret)
  1438. dev_err(dev, "failed to release proc, ret = %d\n", ret);
  1439. platform_set_drvdata(pdev, NULL);
  1440. devres_release_group(dev, k3_r5_core_of_init);
  1441. }
  1442. static void k3_r5_cluster_of_exit(void *data)
  1443. {
  1444. struct k3_r5_cluster *cluster = platform_get_drvdata(data);
  1445. struct platform_device *cpdev;
  1446. struct k3_r5_core *core, *temp;
  1447. list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
  1448. list_del(&core->elem);
  1449. cpdev = to_platform_device(core->dev);
  1450. k3_r5_core_of_exit(cpdev);
  1451. }
  1452. }
  1453. static int k3_r5_cluster_of_init(struct platform_device *pdev)
  1454. {
  1455. struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
  1456. struct device *dev = &pdev->dev;
  1457. struct device_node *np = dev_of_node(dev);
  1458. struct platform_device *cpdev;
  1459. struct device_node *child;
  1460. struct k3_r5_core *core;
  1461. int ret;
  1462. for_each_available_child_of_node(np, child) {
  1463. cpdev = of_find_device_by_node(child);
  1464. if (!cpdev) {
  1465. ret = -ENODEV;
  1466. dev_err(dev, "could not get R5 core platform device\n");
  1467. of_node_put(child);
  1468. goto fail;
  1469. }
  1470. ret = k3_r5_core_of_init(cpdev);
  1471. if (ret) {
  1472. dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
  1473. ret);
  1474. put_device(&cpdev->dev);
  1475. of_node_put(child);
  1476. goto fail;
  1477. }
  1478. core = platform_get_drvdata(cpdev);
  1479. put_device(&cpdev->dev);
  1480. list_add_tail(&core->elem, &cluster->cores);
  1481. }
  1482. return 0;
  1483. fail:
  1484. k3_r5_cluster_of_exit(pdev);
  1485. return ret;
  1486. }
  1487. static int k3_r5_probe(struct platform_device *pdev)
  1488. {
  1489. struct device *dev = &pdev->dev;
  1490. struct device_node *np = dev_of_node(dev);
  1491. struct k3_r5_cluster *cluster;
  1492. const struct k3_r5_soc_data *data;
  1493. int ret;
  1494. int num_cores;
  1495. data = of_device_get_match_data(&pdev->dev);
  1496. if (!data) {
  1497. dev_err(dev, "SoC-specific data is not defined\n");
  1498. return -ENODEV;
  1499. }
  1500. cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
  1501. if (!cluster)
  1502. return -ENOMEM;
  1503. cluster->dev = dev;
  1504. cluster->soc_data = data;
  1505. INIT_LIST_HEAD(&cluster->cores);
  1506. init_waitqueue_head(&cluster->core_transition);
  1507. ret = of_property_read_u32(np, "ti,cluster-mode", &cluster->mode);
  1508. if (ret < 0 && ret != -EINVAL) {
  1509. dev_err(dev, "invalid format for ti,cluster-mode, ret = %d\n",
  1510. ret);
  1511. return ret;
  1512. }
  1513. if (ret == -EINVAL) {
  1514. /*
  1515. * default to most common efuse configurations - Split-mode on AM64x
  1516. * and LockStep-mode on all others
  1517. * default to most common efuse configurations -
  1518. * Split-mode on AM64x
  1519. * Single core on AM62x
  1520. * LockStep-mode on all others
  1521. */
  1522. if (!data->is_single_core)
  1523. cluster->mode = data->single_cpu_mode ?
  1524. CLUSTER_MODE_SPLIT : CLUSTER_MODE_LOCKSTEP;
  1525. else
  1526. cluster->mode = CLUSTER_MODE_SINGLECORE;
  1527. }
  1528. if ((cluster->mode == CLUSTER_MODE_SINGLECPU && !data->single_cpu_mode) ||
  1529. (cluster->mode == CLUSTER_MODE_SINGLECORE && !data->is_single_core)) {
  1530. dev_err(dev, "Cluster mode = %d is not supported on this SoC\n", cluster->mode);
  1531. return -EINVAL;
  1532. }
  1533. num_cores = of_get_available_child_count(np);
  1534. if (num_cores != 2 && !data->is_single_core) {
  1535. dev_err(dev, "MCU cluster requires both R5F cores to be enabled but num_cores is set to = %d\n",
  1536. num_cores);
  1537. return -ENODEV;
  1538. }
  1539. if (num_cores != 1 && data->is_single_core) {
  1540. dev_err(dev, "SoC supports only single core R5 but num_cores is set to %d\n",
  1541. num_cores);
  1542. return -ENODEV;
  1543. }
  1544. platform_set_drvdata(pdev, cluster);
  1545. ret = devm_of_platform_populate(dev);
  1546. if (ret) {
  1547. dev_err(dev, "devm_of_platform_populate failed, ret = %d\n",
  1548. ret);
  1549. return ret;
  1550. }
  1551. ret = k3_r5_cluster_of_init(pdev);
  1552. if (ret) {
  1553. dev_err(dev, "k3_r5_cluster_of_init failed, ret = %d\n", ret);
  1554. return ret;
  1555. }
  1556. ret = devm_add_action_or_reset(dev, k3_r5_cluster_of_exit, pdev);
  1557. if (ret)
  1558. return ret;
  1559. ret = k3_r5_cluster_rproc_init(pdev);
  1560. if (ret) {
  1561. dev_err(dev, "k3_r5_cluster_rproc_init failed, ret = %d\n",
  1562. ret);
  1563. return ret;
  1564. }
  1565. ret = devm_add_action_or_reset(dev, k3_r5_cluster_rproc_exit, pdev);
  1566. if (ret)
  1567. return ret;
  1568. return 0;
  1569. }
  1570. static const struct k3_r5_soc_data am65_j721e_soc_data = {
  1571. .tcm_is_double = false,
  1572. .tcm_ecc_autoinit = false,
  1573. .single_cpu_mode = false,
  1574. .is_single_core = false,
  1575. };
  1576. static const struct k3_r5_soc_data j7200_j721s2_soc_data = {
  1577. .tcm_is_double = true,
  1578. .tcm_ecc_autoinit = true,
  1579. .single_cpu_mode = false,
  1580. .is_single_core = false,
  1581. };
  1582. static const struct k3_r5_soc_data am64_soc_data = {
  1583. .tcm_is_double = true,
  1584. .tcm_ecc_autoinit = true,
  1585. .single_cpu_mode = true,
  1586. .is_single_core = false,
  1587. };
  1588. static const struct k3_r5_soc_data am62_soc_data = {
  1589. .tcm_is_double = false,
  1590. .tcm_ecc_autoinit = true,
  1591. .single_cpu_mode = false,
  1592. .is_single_core = true,
  1593. };
  1594. static const struct of_device_id k3_r5_of_match[] = {
  1595. { .compatible = "ti,am654-r5fss", .data = &am65_j721e_soc_data, },
  1596. { .compatible = "ti,j721e-r5fss", .data = &am65_j721e_soc_data, },
  1597. { .compatible = "ti,j7200-r5fss", .data = &j7200_j721s2_soc_data, },
  1598. { .compatible = "ti,am64-r5fss", .data = &am64_soc_data, },
  1599. { .compatible = "ti,am62-r5fss", .data = &am62_soc_data, },
  1600. { .compatible = "ti,j721s2-r5fss", .data = &j7200_j721s2_soc_data, },
  1601. { /* sentinel */ },
  1602. };
  1603. MODULE_DEVICE_TABLE(of, k3_r5_of_match);
  1604. static struct platform_driver k3_r5_rproc_driver = {
  1605. .probe = k3_r5_probe,
  1606. .driver = {
  1607. .name = "k3_r5_rproc",
  1608. .of_match_table = k3_r5_of_match,
  1609. },
  1610. };
  1611. module_platform_driver(k3_r5_rproc_driver);
  1612. MODULE_LICENSE("GPL v2");
  1613. MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
  1614. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");