ti_k3_r5_remoteproc.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. #include "ti_k3_common.h"
  28. /* This address can either be for ATCM or BTCM with the other at address 0x0 */
  29. #define K3_R5_TCM_DEV_ADDR 0x41010000
  30. /* R5 TI-SCI Processor Configuration Flags */
  31. #define PROC_BOOT_CFG_FLAG_R5_DBG_EN 0x00000001
  32. #define PROC_BOOT_CFG_FLAG_R5_DBG_NIDEN 0x00000002
  33. #define PROC_BOOT_CFG_FLAG_R5_LOCKSTEP 0x00000100
  34. #define PROC_BOOT_CFG_FLAG_R5_TEINIT 0x00000200
  35. #define PROC_BOOT_CFG_FLAG_R5_NMFI_EN 0x00000400
  36. #define PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE 0x00000800
  37. #define PROC_BOOT_CFG_FLAG_R5_BTCM_EN 0x00001000
  38. #define PROC_BOOT_CFG_FLAG_R5_ATCM_EN 0x00002000
  39. /* Available from J7200 SoCs onwards */
  40. #define PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS 0x00004000
  41. /* Applicable to only AM64x SoCs */
  42. #define PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE 0x00008000
  43. /* R5 TI-SCI Processor Control Flags */
  44. #define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001
  45. /* R5 TI-SCI Processor Status Flags */
  46. #define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001
  47. #define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002
  48. #define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004
  49. #define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100
  50. /* Applicable to only AM64x SoCs */
  51. #define PROC_BOOT_STATUS_FLAG_R5_SINGLECORE_ONLY 0x00000200
  52. /*
  53. * All cluster mode values are not applicable on all SoCs. The following
  54. * are the modes supported on various SoCs:
  55. * Split mode : AM65x, J721E, J7200 and AM64x SoCs
  56. * LockStep mode : AM65x, J721E and J7200 SoCs
  57. * Single-CPU mode : AM64x SoCs only
  58. * Single-Core mode : AM62x, AM62A SoCs
  59. */
  60. enum cluster_mode {
  61. CLUSTER_MODE_SPLIT = 0,
  62. CLUSTER_MODE_LOCKSTEP,
  63. CLUSTER_MODE_SINGLECPU,
  64. CLUSTER_MODE_SINGLECORE
  65. };
  66. /**
  67. * struct k3_r5_soc_data - match data to handle SoC variations
  68. * @tcm_is_double: flag to denote the larger unified TCMs in certain modes
  69. * @tcm_ecc_autoinit: flag to denote the auto-initialization of TCMs for ECC
  70. * @single_cpu_mode: flag to denote if SoC/IP supports Single-CPU mode
  71. * @is_single_core: flag to denote if SoC/IP has only single core R5
  72. * @core_data: pointer to R5-core-specific device data
  73. */
  74. struct k3_r5_soc_data {
  75. bool tcm_is_double;
  76. bool tcm_ecc_autoinit;
  77. bool single_cpu_mode;
  78. bool is_single_core;
  79. const struct k3_rproc_dev_data *core_data;
  80. };
  81. /**
  82. * struct k3_r5_cluster - K3 R5F Cluster structure
  83. * @dev: cached device pointer
  84. * @mode: Mode to configure the Cluster - Split or LockStep
  85. * @cores: list of R5 cores within the cluster
  86. * @core_transition: wait queue to sync core state changes
  87. * @soc_data: SoC-specific feature data for a R5FSS
  88. */
  89. struct k3_r5_cluster {
  90. struct device *dev;
  91. enum cluster_mode mode;
  92. struct list_head cores;
  93. wait_queue_head_t core_transition;
  94. const struct k3_r5_soc_data *soc_data;
  95. };
  96. /**
  97. * struct k3_r5_core - K3 R5 core structure
  98. * @elem: linked list item
  99. * @dev: cached device pointer
  100. * @kproc: K3 rproc handle representing this core
  101. * @cluster: cached pointer to parent cluster structure
  102. * @sram: on-chip SRAM memory regions data
  103. * @num_sram: number of on-chip SRAM memory regions
  104. * @atcm_enable: flag to control ATCM enablement
  105. * @btcm_enable: flag to control BTCM enablement
  106. * @loczrama: flag to dictate which TCM is at device address 0x0
  107. * @released_from_reset: flag to signal when core is out of reset
  108. */
  109. struct k3_r5_core {
  110. struct list_head elem;
  111. struct device *dev;
  112. struct k3_rproc *kproc;
  113. struct k3_r5_cluster *cluster;
  114. struct k3_rproc_mem *sram;
  115. int num_sram;
  116. u32 atcm_enable;
  117. u32 btcm_enable;
  118. u32 loczrama;
  119. bool released_from_reset;
  120. };
  121. static int k3_r5_split_reset(struct k3_rproc *kproc)
  122. {
  123. int ret;
  124. ret = reset_control_assert(kproc->reset);
  125. if (ret) {
  126. dev_err(kproc->dev, "local-reset assert failed, ret = %d\n",
  127. ret);
  128. return ret;
  129. }
  130. ret = kproc->ti_sci->ops.dev_ops.put_device(kproc->ti_sci,
  131. kproc->ti_sci_id);
  132. if (ret) {
  133. dev_err(kproc->dev, "module-reset assert failed, ret = %d\n",
  134. ret);
  135. if (reset_control_deassert(kproc->reset))
  136. dev_warn(kproc->dev, "local-reset deassert back failed\n");
  137. }
  138. return ret;
  139. }
  140. static int k3_r5_split_release(struct k3_rproc *kproc)
  141. {
  142. int ret;
  143. ret = kproc->ti_sci->ops.dev_ops.get_device(kproc->ti_sci,
  144. kproc->ti_sci_id);
  145. if (ret) {
  146. dev_err(kproc->dev, "module-reset deassert failed, ret = %d\n",
  147. ret);
  148. return ret;
  149. }
  150. ret = reset_control_deassert(kproc->reset);
  151. if (ret) {
  152. dev_err(kproc->dev, "local-reset deassert failed, ret = %d\n",
  153. ret);
  154. if (kproc->ti_sci->ops.dev_ops.put_device(kproc->ti_sci,
  155. kproc->ti_sci_id))
  156. dev_warn(kproc->dev, "module-reset assert back failed\n");
  157. }
  158. return ret;
  159. }
  160. static int k3_r5_lockstep_reset(struct k3_r5_cluster *cluster)
  161. {
  162. struct k3_r5_core *core;
  163. struct k3_rproc *kproc;
  164. int ret;
  165. /* assert local reset on all applicable cores */
  166. list_for_each_entry(core, &cluster->cores, elem) {
  167. ret = reset_control_assert(core->kproc->reset);
  168. if (ret) {
  169. dev_err(core->dev, "local-reset assert failed, ret = %d\n",
  170. ret);
  171. core = list_prev_entry(core, elem);
  172. goto unroll_local_reset;
  173. }
  174. }
  175. /* disable PSC modules on all applicable cores */
  176. list_for_each_entry(core, &cluster->cores, elem) {
  177. kproc = core->kproc;
  178. ret = kproc->ti_sci->ops.dev_ops.put_device(kproc->ti_sci,
  179. kproc->ti_sci_id);
  180. if (ret) {
  181. dev_err(core->dev, "module-reset assert failed, ret = %d\n",
  182. ret);
  183. goto unroll_module_reset;
  184. }
  185. }
  186. return 0;
  187. unroll_module_reset:
  188. list_for_each_entry_continue_reverse(core, &cluster->cores, elem) {
  189. kproc = core->kproc;
  190. if (kproc->ti_sci->ops.dev_ops.put_device(kproc->ti_sci,
  191. kproc->ti_sci_id))
  192. dev_warn(core->dev, "module-reset assert back failed\n");
  193. }
  194. core = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  195. unroll_local_reset:
  196. list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
  197. if (reset_control_deassert(core->kproc->reset))
  198. dev_warn(core->dev, "local-reset deassert back failed\n");
  199. }
  200. return ret;
  201. }
  202. static int k3_r5_lockstep_release(struct k3_r5_cluster *cluster)
  203. {
  204. struct k3_r5_core *core;
  205. struct k3_rproc *kproc;
  206. int ret;
  207. /* enable PSC modules on all applicable cores */
  208. list_for_each_entry_reverse(core, &cluster->cores, elem) {
  209. kproc = core->kproc;
  210. ret = kproc->ti_sci->ops.dev_ops.get_device(kproc->ti_sci,
  211. kproc->ti_sci_id);
  212. if (ret) {
  213. dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
  214. ret);
  215. core = list_next_entry(core, elem);
  216. goto unroll_module_reset;
  217. }
  218. }
  219. /* deassert local reset on all applicable cores */
  220. list_for_each_entry_reverse(core, &cluster->cores, elem) {
  221. ret = reset_control_deassert(core->kproc->reset);
  222. if (ret) {
  223. dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
  224. ret);
  225. goto unroll_local_reset;
  226. }
  227. }
  228. return 0;
  229. unroll_local_reset:
  230. list_for_each_entry_continue(core, &cluster->cores, elem) {
  231. if (reset_control_assert(core->kproc->reset))
  232. dev_warn(core->dev, "local-reset assert back failed\n");
  233. }
  234. core = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  235. unroll_module_reset:
  236. list_for_each_entry_from(core, &cluster->cores, elem) {
  237. kproc = core->kproc;
  238. if (kproc->ti_sci->ops.dev_ops.put_device(kproc->ti_sci,
  239. kproc->ti_sci_id))
  240. dev_warn(core->dev, "module-reset assert back failed\n");
  241. }
  242. return ret;
  243. }
  244. static inline int k3_r5_core_halt(struct k3_rproc *kproc)
  245. {
  246. return ti_sci_proc_set_control(kproc->tsp,
  247. PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
  248. }
  249. static inline int k3_r5_core_run(struct k3_rproc *kproc)
  250. {
  251. return ti_sci_proc_set_control(kproc->tsp,
  252. 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
  253. }
  254. /*
  255. * The R5F cores have controls for both a reset and a halt/run. The code
  256. * execution from DDR requires the initial boot-strapping code to be run
  257. * from the internal TCMs. This function is used to release the resets on
  258. * applicable cores to allow loading into the TCMs. The .prepare() ops is
  259. * invoked by remoteproc core before any firmware loading, and is followed
  260. * by the .start() ops after loading to actually let the R5 cores run.
  261. *
  262. * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to
  263. * execute code, but combines the TCMs from both cores. The resets for both
  264. * cores need to be released to make this possible, as the TCMs are in general
  265. * private to each core. Only Core0 needs to be unhalted for running the
  266. * cluster in this mode. The function uses the same reset logic as LockStep
  267. * mode for this (though the behavior is agnostic of the reset release order).
  268. * This callback is invoked only in remoteproc mode.
  269. */
  270. static int k3_r5_rproc_prepare(struct rproc *rproc)
  271. {
  272. struct k3_rproc *kproc = rproc->priv;
  273. struct k3_r5_core *core = kproc->priv, *core0, *core1;
  274. struct k3_r5_cluster *cluster = core->cluster;
  275. struct device *dev = kproc->dev;
  276. u32 ctrl = 0, cfg = 0, stat = 0;
  277. u64 boot_vec = 0;
  278. bool mem_init_dis;
  279. int ret;
  280. /*
  281. * R5 cores require to be powered on sequentially, core0 should be in
  282. * higher power state than core1 in a cluster. So, wait for core0 to
  283. * power up before proceeding to core1 and put timeout of 2sec. This
  284. * waiting mechanism is necessary because rproc_auto_boot_callback() for
  285. * core1 can be called before core0 due to thread execution order.
  286. *
  287. * By placing the wait mechanism here in .prepare() ops, this condition
  288. * is enforced for rproc boot requests from sysfs as well.
  289. */
  290. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  291. core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  292. if (cluster->mode == CLUSTER_MODE_SPLIT && core == core1 &&
  293. !core0->released_from_reset) {
  294. ret = wait_event_interruptible_timeout(cluster->core_transition,
  295. core0->released_from_reset,
  296. msecs_to_jiffies(2000));
  297. if (ret <= 0) {
  298. dev_err(dev, "can not power up core1 before core0");
  299. return -EPERM;
  300. }
  301. }
  302. ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
  303. if (ret < 0)
  304. return ret;
  305. mem_init_dis = !!(cfg & PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS);
  306. /* Re-use LockStep-mode reset logic for Single-CPU mode */
  307. ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  308. cluster->mode == CLUSTER_MODE_SINGLECPU) ?
  309. k3_r5_lockstep_release(cluster) : k3_r5_split_release(kproc);
  310. if (ret) {
  311. dev_err(dev, "unable to enable cores for TCM loading, ret = %d\n",
  312. ret);
  313. return ret;
  314. }
  315. /*
  316. * Notify all threads in the wait queue when core0 state has changed so
  317. * that threads waiting for this condition can be executed.
  318. */
  319. core->released_from_reset = true;
  320. if (core == core0)
  321. wake_up_interruptible(&cluster->core_transition);
  322. /*
  323. * Newer IP revisions like on J7200 SoCs support h/w auto-initialization
  324. * of TCMs, so there is no need to perform the s/w memzero. This bit is
  325. * configurable through System Firmware, the default value does perform
  326. * auto-init, but account for it in case it is disabled
  327. */
  328. if (cluster->soc_data->tcm_ecc_autoinit && !mem_init_dis) {
  329. dev_dbg(dev, "leveraging h/w init for TCM memories\n");
  330. return 0;
  331. }
  332. /*
  333. * Zero out both TCMs unconditionally (access from v8 Arm core is not
  334. * affected by ATCM & BTCM enable configuration values) so that ECC
  335. * can be effective on all TCM addresses.
  336. */
  337. dev_dbg(dev, "zeroing out ATCM memory\n");
  338. memset_io(kproc->mem[0].cpu_addr, 0x00, kproc->mem[0].size);
  339. dev_dbg(dev, "zeroing out BTCM memory\n");
  340. memset_io(kproc->mem[1].cpu_addr, 0x00, kproc->mem[1].size);
  341. return 0;
  342. }
  343. /*
  344. * This function implements the .unprepare() ops and performs the complimentary
  345. * operations to that of the .prepare() ops. The function is used to assert the
  346. * resets on all applicable cores for the rproc device (depending on LockStep
  347. * or Split mode). This completes the second portion of powering down the R5F
  348. * cores. The cores themselves are only halted in the .stop() ops, and the
  349. * .unprepare() ops is invoked by the remoteproc core after the remoteproc is
  350. * stopped.
  351. *
  352. * The Single-CPU mode on applicable SoCs (eg: AM64x) combines the TCMs from
  353. * both cores. The access is made possible only with releasing the resets for
  354. * both cores, but with only Core0 unhalted. This function re-uses the same
  355. * reset assert logic as LockStep mode for this mode (though the behavior is
  356. * agnostic of the reset assert order). This callback is invoked only in
  357. * remoteproc mode.
  358. */
  359. static int k3_r5_rproc_unprepare(struct rproc *rproc)
  360. {
  361. struct k3_rproc *kproc = rproc->priv;
  362. struct k3_r5_core *core = kproc->priv, *core0, *core1;
  363. struct k3_r5_cluster *cluster = core->cluster;
  364. struct device *dev = kproc->dev;
  365. int ret;
  366. /*
  367. * Ensure power-down of cores is sequential in split mode. Core1 must
  368. * power down before Core0 to maintain the expected state. By placing
  369. * the wait mechanism here in .unprepare() ops, this condition is
  370. * enforced for rproc stop or shutdown requests from sysfs and device
  371. * removal as well.
  372. */
  373. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  374. core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  375. if (cluster->mode == CLUSTER_MODE_SPLIT && core == core0 &&
  376. core1->released_from_reset) {
  377. ret = wait_event_interruptible_timeout(cluster->core_transition,
  378. !core1->released_from_reset,
  379. msecs_to_jiffies(2000));
  380. if (ret <= 0) {
  381. dev_err(dev, "can not power down core0 before core1");
  382. return -EPERM;
  383. }
  384. }
  385. /* Re-use LockStep-mode reset logic for Single-CPU mode */
  386. ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  387. cluster->mode == CLUSTER_MODE_SINGLECPU) ?
  388. k3_r5_lockstep_reset(cluster) : k3_r5_split_reset(kproc);
  389. if (ret)
  390. dev_err(dev, "unable to disable cores, ret = %d\n", ret);
  391. /*
  392. * Notify all threads in the wait queue when core1 state has changed so
  393. * that threads waiting for this condition can be executed.
  394. */
  395. core->released_from_reset = false;
  396. if (core == core1)
  397. wake_up_interruptible(&cluster->core_transition);
  398. return ret;
  399. }
  400. /*
  401. * The R5F start sequence includes two different operations
  402. * 1. Configure the boot vector for R5F core(s)
  403. * 2. Unhalt/Run the R5F core(s)
  404. *
  405. * The sequence is different between LockStep and Split modes. The LockStep
  406. * mode requires the boot vector to be configured only for Core0, and then
  407. * unhalt both the cores to start the execution - Core1 needs to be unhalted
  408. * first followed by Core0. The Split-mode requires that Core0 to be maintained
  409. * always in a higher power state that Core1 (implying Core1 needs to be started
  410. * always only after Core0 is started).
  411. *
  412. * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to execute
  413. * code, so only Core0 needs to be unhalted. The function uses the same logic
  414. * flow as Split-mode for this. This callback is invoked only in remoteproc
  415. * mode.
  416. */
  417. static int k3_r5_rproc_start(struct rproc *rproc)
  418. {
  419. struct k3_rproc *kproc = rproc->priv;
  420. struct k3_r5_core *core = kproc->priv;
  421. struct k3_r5_cluster *cluster = core->cluster;
  422. struct device *dev = kproc->dev;
  423. u32 boot_addr;
  424. int ret;
  425. boot_addr = rproc->bootaddr;
  426. /* TODO: add boot_addr sanity checking */
  427. dev_dbg(dev, "booting R5F core using boot addr = 0x%x\n", boot_addr);
  428. /* boot vector need not be programmed for Core1 in LockStep mode */
  429. ret = ti_sci_proc_set_config(kproc->tsp, boot_addr, 0, 0);
  430. if (ret)
  431. return ret;
  432. /* unhalt/run all applicable cores */
  433. if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  434. list_for_each_entry_reverse(core, &cluster->cores, elem) {
  435. ret = k3_r5_core_run(core->kproc);
  436. if (ret)
  437. goto unroll_core_run;
  438. }
  439. } else {
  440. ret = k3_r5_core_run(core->kproc);
  441. if (ret)
  442. return ret;
  443. }
  444. return 0;
  445. unroll_core_run:
  446. list_for_each_entry_continue(core, &cluster->cores, elem) {
  447. if (k3_r5_core_halt(core->kproc))
  448. dev_warn(core->dev, "core halt back failed\n");
  449. }
  450. return ret;
  451. }
  452. /*
  453. * The R5F stop function includes the following operations
  454. * 1. Halt R5F core(s)
  455. *
  456. * The sequence is different between LockStep and Split modes, and the order
  457. * of cores the operations are performed are also in general reverse to that
  458. * of the start function. The LockStep mode requires each operation to be
  459. * performed first on Core0 followed by Core1. The Split-mode requires that
  460. * Core0 to be maintained always in a higher power state that Core1 (implying
  461. * Core1 needs to be stopped first before Core0).
  462. *
  463. * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to execute
  464. * code, so only Core0 needs to be halted. The function uses the same logic
  465. * flow as Split-mode for this.
  466. *
  467. * Note that the R5F halt operation in general is not effective when the R5F
  468. * core is running, but is needed to make sure the core won't run after
  469. * deasserting the reset the subsequent time. The asserting of reset can
  470. * be done here, but is preferred to be done in the .unprepare() ops - this
  471. * maintains the symmetric behavior between the .start(), .stop(), .prepare()
  472. * and .unprepare() ops, and also balances them well between sysfs 'state'
  473. * flow and device bind/unbind or module removal. This callback is invoked
  474. * only in remoteproc mode.
  475. */
  476. static int k3_r5_rproc_stop(struct rproc *rproc)
  477. {
  478. struct k3_rproc *kproc = rproc->priv;
  479. struct k3_r5_core *core = kproc->priv;
  480. struct k3_r5_cluster *cluster = core->cluster;
  481. int ret;
  482. /* halt all applicable cores */
  483. if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  484. list_for_each_entry(core, &cluster->cores, elem) {
  485. ret = k3_r5_core_halt(core->kproc);
  486. if (ret) {
  487. core = list_prev_entry(core, elem);
  488. goto unroll_core_halt;
  489. }
  490. }
  491. } else {
  492. ret = k3_r5_core_halt(core->kproc);
  493. if (ret)
  494. goto out;
  495. }
  496. return 0;
  497. unroll_core_halt:
  498. list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
  499. if (k3_r5_core_run(core->kproc))
  500. dev_warn(core->dev, "core run back failed\n");
  501. }
  502. out:
  503. return ret;
  504. }
  505. /*
  506. * Internal Memory translation helper
  507. *
  508. * Custom function implementing the rproc .da_to_va ops to provide address
  509. * translation (device address to kernel virtual address) for internal RAMs
  510. * present in a DSP or IPU device). The translated addresses can be used
  511. * either by the remoteproc core for loading, or by any rpmsg bus drivers.
  512. */
  513. static void *k3_r5_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  514. {
  515. struct k3_rproc *kproc = rproc->priv;
  516. struct k3_r5_core *core = kproc->priv;
  517. void __iomem *va = NULL;
  518. u32 dev_addr, offset;
  519. size_t size;
  520. int i;
  521. if (len == 0)
  522. return NULL;
  523. /* handle any SRAM regions using SoC-view addresses */
  524. for (i = 0; i < core->num_sram; i++) {
  525. dev_addr = core->sram[i].dev_addr;
  526. size = core->sram[i].size;
  527. if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
  528. offset = da - dev_addr;
  529. va = core->sram[i].cpu_addr + offset;
  530. return (__force void *)va;
  531. }
  532. }
  533. /* handle both TCM and DDR memory regions */
  534. return k3_rproc_da_to_va(rproc, da, len, is_iomem);
  535. }
  536. static const struct rproc_ops k3_r5_rproc_ops = {
  537. .prepare = k3_r5_rproc_prepare,
  538. .unprepare = k3_r5_rproc_unprepare,
  539. .start = k3_r5_rproc_start,
  540. .stop = k3_r5_rproc_stop,
  541. .kick = k3_rproc_kick,
  542. .da_to_va = k3_r5_rproc_da_to_va,
  543. };
  544. /*
  545. * Internal R5F Core configuration
  546. *
  547. * Each R5FSS has a cluster-level setting for configuring the processor
  548. * subsystem either in a safety/fault-tolerant LockStep mode or a performance
  549. * oriented Split mode on most SoCs. A fewer SoCs support a non-safety mode
  550. * as an alternate for LockStep mode that exercises only a single R5F core
  551. * called Single-CPU mode. Each R5F core has a number of settings to either
  552. * enable/disable each of the TCMs, control which TCM appears at the R5F core's
  553. * address 0x0. These settings need to be configured before the resets for the
  554. * corresponding core are released. These settings are all protected and managed
  555. * by the System Processor.
  556. *
  557. * This function is used to pre-configure these settings for each R5F core, and
  558. * the configuration is all done through various ti_sci_proc functions that
  559. * communicate with the System Processor. The function also ensures that both
  560. * the cores are halted before the .prepare() step.
  561. *
  562. * The function is called from k3_r5_cluster_rproc_init() and is invoked either
  563. * once (in LockStep mode or Single-CPU modes) or twice (in Split mode). Support
  564. * for LockStep-mode is dictated by an eFUSE register bit, and the config
  565. * settings retrieved from DT are adjusted accordingly as per the permitted
  566. * cluster mode. Another eFUSE register bit dictates if the R5F cluster only
  567. * supports a Single-CPU mode. All cluster level settings like Cluster mode and
  568. * TEINIT (exception handling state dictating ARM or Thumb mode) can only be set
  569. * and retrieved using Core0.
  570. *
  571. * The function behavior is different based on the cluster mode. The R5F cores
  572. * are configured independently as per their individual settings in Split mode.
  573. * They are identically configured in LockStep mode using the primary Core0
  574. * settings. However, some individual settings cannot be set in LockStep mode.
  575. * This is overcome by switching to Split-mode initially and then programming
  576. * both the cores with the same settings, before reconfiguing again for
  577. * LockStep mode.
  578. */
  579. static int k3_r5_rproc_configure(struct k3_rproc *kproc)
  580. {
  581. struct k3_r5_core *temp, *core0, *core = kproc->priv;
  582. struct k3_r5_cluster *cluster = core->cluster;
  583. struct device *dev = kproc->dev;
  584. u32 ctrl = 0, cfg = 0, stat = 0;
  585. u32 set_cfg = 0, clr_cfg = 0;
  586. u64 boot_vec = 0;
  587. bool lockstep_en;
  588. bool single_cpu;
  589. int ret;
  590. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  591. if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  592. cluster->mode == CLUSTER_MODE_SINGLECPU ||
  593. cluster->mode == CLUSTER_MODE_SINGLECORE) {
  594. core = core0;
  595. } else {
  596. core = kproc->priv;
  597. }
  598. ret = ti_sci_proc_get_status(core->kproc->tsp, &boot_vec, &cfg, &ctrl,
  599. &stat);
  600. if (ret < 0)
  601. return ret;
  602. dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
  603. boot_vec, cfg, ctrl, stat);
  604. single_cpu = !!(stat & PROC_BOOT_STATUS_FLAG_R5_SINGLECORE_ONLY);
  605. lockstep_en = !!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED);
  606. /* Override to single CPU mode if set in status flag */
  607. if (single_cpu && cluster->mode == CLUSTER_MODE_SPLIT) {
  608. dev_err(cluster->dev, "split-mode not permitted, force configuring for single-cpu mode\n");
  609. cluster->mode = CLUSTER_MODE_SINGLECPU;
  610. }
  611. /* Override to split mode if lockstep enable bit is not set in status flag */
  612. if (!lockstep_en && cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  613. dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
  614. cluster->mode = CLUSTER_MODE_SPLIT;
  615. }
  616. /* always enable ARM mode and set boot vector to 0 */
  617. boot_vec = 0x0;
  618. if (core == core0) {
  619. clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
  620. /*
  621. * Single-CPU configuration bit can only be configured
  622. * on Core0 and system firmware will NACK any requests
  623. * with the bit configured, so program it only on
  624. * permitted cores
  625. */
  626. if (cluster->mode == CLUSTER_MODE_SINGLECPU ||
  627. cluster->mode == CLUSTER_MODE_SINGLECORE) {
  628. set_cfg = PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE;
  629. } else {
  630. /*
  631. * LockStep configuration bit is Read-only on Split-mode
  632. * _only_ devices and system firmware will NACK any
  633. * requests with the bit configured, so program it only
  634. * on permitted devices
  635. */
  636. if (lockstep_en)
  637. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
  638. }
  639. }
  640. if (core->atcm_enable)
  641. set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
  642. else
  643. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
  644. if (core->btcm_enable)
  645. set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
  646. else
  647. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
  648. if (core->loczrama)
  649. set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
  650. else
  651. clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
  652. if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
  653. /*
  654. * work around system firmware limitations to make sure both
  655. * cores are programmed symmetrically in LockStep. LockStep
  656. * and TEINIT config is only allowed with Core0.
  657. */
  658. list_for_each_entry(temp, &cluster->cores, elem) {
  659. ret = k3_r5_core_halt(temp->kproc);
  660. if (ret)
  661. goto out;
  662. if (temp != core) {
  663. clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
  664. clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
  665. }
  666. ret = ti_sci_proc_set_config(temp->kproc->tsp, boot_vec,
  667. set_cfg, clr_cfg);
  668. if (ret)
  669. goto out;
  670. }
  671. set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
  672. clr_cfg = 0;
  673. ret = ti_sci_proc_set_config(core->kproc->tsp, boot_vec,
  674. set_cfg, clr_cfg);
  675. } else {
  676. ret = k3_r5_core_halt(core->kproc);
  677. if (ret)
  678. goto out;
  679. ret = ti_sci_proc_set_config(core->kproc->tsp, boot_vec,
  680. set_cfg, clr_cfg);
  681. }
  682. out:
  683. return ret;
  684. }
  685. /*
  686. * Each R5F core within a typical R5FSS instance has a total of 64 KB of TCMs,
  687. * split equally into two 32 KB banks between ATCM and BTCM. The TCMs from both
  688. * cores are usable in Split-mode, but only the Core0 TCMs can be used in
  689. * LockStep-mode. The newer revisions of the R5FSS IP maximizes these TCMs by
  690. * leveraging the Core1 TCMs as well in certain modes where they would have
  691. * otherwise been unusable (Eg: LockStep-mode on J7200 SoCs, Single-CPU mode on
  692. * AM64x SoCs). This is done by making a Core1 TCM visible immediately after the
  693. * corresponding Core0 TCM. The SoC memory map uses the larger 64 KB sizes for
  694. * the Core0 TCMs, and the dts representation reflects this increased size on
  695. * supported SoCs. The Core0 TCM sizes therefore have to be adjusted to only
  696. * half the original size in Split mode.
  697. */
  698. static void k3_r5_adjust_tcm_sizes(struct k3_rproc *kproc)
  699. {
  700. struct k3_r5_core *core0, *core = kproc->priv;
  701. struct k3_r5_cluster *cluster = core->cluster;
  702. struct device *cdev = core->dev;
  703. if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  704. cluster->mode == CLUSTER_MODE_SINGLECPU ||
  705. cluster->mode == CLUSTER_MODE_SINGLECORE ||
  706. !cluster->soc_data->tcm_is_double)
  707. return;
  708. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  709. if (core == core0) {
  710. WARN_ON(kproc->mem[0].size != SZ_64K);
  711. WARN_ON(kproc->mem[1].size != SZ_64K);
  712. kproc->mem[0].size /= 2;
  713. kproc->mem[1].size /= 2;
  714. dev_dbg(cdev, "adjusted TCM sizes, ATCM = 0x%zx BTCM = 0x%zx\n",
  715. kproc->mem[0].size, kproc->mem[1].size);
  716. }
  717. }
  718. /*
  719. * This function checks and configures a R5F core for IPC-only or remoteproc
  720. * mode. The driver is configured to be in IPC-only mode for a R5F core when
  721. * the core has been loaded and started by a bootloader. The IPC-only mode is
  722. * detected by querying the System Firmware for reset, power on and halt status
  723. * and ensuring that the core is running. Any incomplete steps at bootloader
  724. * are validated and errored out.
  725. *
  726. * In IPC-only mode, the driver state flags for ATCM, BTCM and LOCZRAMA settings
  727. * and cluster mode parsed originally from kernel DT are updated to reflect the
  728. * actual values configured by bootloader. The driver internal device memory
  729. * addresses for TCMs are also updated.
  730. */
  731. static int k3_r5_rproc_configure_mode(struct k3_rproc *kproc)
  732. {
  733. struct k3_r5_core *core0, *core = kproc->priv;
  734. struct k3_r5_cluster *cluster = core->cluster;
  735. struct device *cdev = core->dev;
  736. bool r_state = false, c_state = false, lockstep_en = false, single_cpu = false;
  737. u32 ctrl = 0, cfg = 0, stat = 0, halted = 0;
  738. u64 boot_vec = 0;
  739. u32 atcm_enable, btcm_enable, loczrama;
  740. enum cluster_mode mode = cluster->mode;
  741. int reset_ctrl_status;
  742. int ret;
  743. core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
  744. ret = kproc->ti_sci->ops.dev_ops.is_on(kproc->ti_sci, kproc->ti_sci_id,
  745. &r_state, &c_state);
  746. if (ret) {
  747. dev_err(cdev, "failed to get initial state, mode cannot be determined, ret = %d\n",
  748. ret);
  749. return ret;
  750. }
  751. if (r_state != c_state) {
  752. dev_warn(cdev, "R5F core may have been powered on by a different host, programmed state (%d) != actual state (%d)\n",
  753. r_state, c_state);
  754. }
  755. reset_ctrl_status = reset_control_status(kproc->reset);
  756. if (reset_ctrl_status < 0) {
  757. dev_err(cdev, "failed to get initial local reset status, ret = %d\n",
  758. reset_ctrl_status);
  759. return reset_ctrl_status;
  760. }
  761. /*
  762. * Skip the waiting mechanism for sequential power-on of cores if the
  763. * core has already been booted by another entity.
  764. */
  765. core->released_from_reset = c_state;
  766. ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl,
  767. &stat);
  768. if (ret < 0) {
  769. dev_err(cdev, "failed to get initial processor status, ret = %d\n",
  770. ret);
  771. return ret;
  772. }
  773. atcm_enable = cfg & PROC_BOOT_CFG_FLAG_R5_ATCM_EN ? 1 : 0;
  774. btcm_enable = cfg & PROC_BOOT_CFG_FLAG_R5_BTCM_EN ? 1 : 0;
  775. loczrama = cfg & PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE ? 1 : 0;
  776. single_cpu = cfg & PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE ? 1 : 0;
  777. lockstep_en = cfg & PROC_BOOT_CFG_FLAG_R5_LOCKSTEP ? 1 : 0;
  778. if (single_cpu && mode != CLUSTER_MODE_SINGLECORE)
  779. mode = CLUSTER_MODE_SINGLECPU;
  780. if (lockstep_en)
  781. mode = CLUSTER_MODE_LOCKSTEP;
  782. halted = ctrl & PROC_BOOT_CTRL_FLAG_R5_CORE_HALT;
  783. /*
  784. * IPC-only mode detection requires both local and module resets to
  785. * be deasserted and R5F core to be unhalted. Local reset status is
  786. * irrelevant if module reset is asserted (POR value has local reset
  787. * deasserted), and is deemed as remoteproc mode
  788. */
  789. if (c_state && !reset_ctrl_status && !halted) {
  790. dev_info(cdev, "configured R5F for IPC-only mode\n");
  791. kproc->rproc->state = RPROC_DETACHED;
  792. ret = 1;
  793. /* override rproc ops with only required IPC-only mode ops */
  794. kproc->rproc->ops->prepare = NULL;
  795. kproc->rproc->ops->unprepare = NULL;
  796. kproc->rproc->ops->start = NULL;
  797. kproc->rproc->ops->stop = NULL;
  798. kproc->rproc->ops->attach = k3_rproc_attach;
  799. kproc->rproc->ops->detach = k3_rproc_detach;
  800. kproc->rproc->ops->get_loaded_rsc_table =
  801. k3_get_loaded_rsc_table;
  802. } else if (!c_state) {
  803. dev_info(cdev, "configured R5F for remoteproc mode\n");
  804. ret = 0;
  805. } else {
  806. dev_err(cdev, "mismatched mode: local_reset = %s, module_reset = %s, core_state = %s\n",
  807. !reset_ctrl_status ? "deasserted" : "asserted",
  808. c_state ? "deasserted" : "asserted",
  809. halted ? "halted" : "unhalted");
  810. ret = -EINVAL;
  811. }
  812. /* fixup TCMs, cluster & core flags to actual values in IPC-only mode */
  813. if (ret > 0) {
  814. if (core == core0)
  815. cluster->mode = mode;
  816. core->atcm_enable = atcm_enable;
  817. core->btcm_enable = btcm_enable;
  818. core->loczrama = loczrama;
  819. kproc->mem[0].dev_addr = loczrama ? 0 : K3_R5_TCM_DEV_ADDR;
  820. kproc->mem[1].dev_addr = loczrama ? K3_R5_TCM_DEV_ADDR : 0;
  821. }
  822. return ret;
  823. }
  824. static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
  825. struct k3_rproc *kproc)
  826. {
  827. const struct k3_rproc_dev_data *data = kproc->data;
  828. struct device *dev = &pdev->dev;
  829. struct k3_r5_core *core = kproc->priv;
  830. int num_mems;
  831. int i, ret;
  832. num_mems = data->num_mems;
  833. kproc->mem = devm_kcalloc(kproc->dev, num_mems, sizeof(*kproc->mem),
  834. GFP_KERNEL);
  835. if (!kproc->mem)
  836. return -ENOMEM;
  837. ret = k3_rproc_of_get_memories(pdev, kproc);
  838. if (ret)
  839. return ret;
  840. for (i = 0; i < num_mems; i++) {
  841. /*
  842. * TODO:
  843. * The R5F cores can place ATCM & BTCM anywhere in its address
  844. * based on the corresponding Region Registers in the System
  845. * Control coprocessor. For now, place ATCM and BTCM at
  846. * addresses 0 and 0x41010000 (same as the bus address on AM65x
  847. * SoCs) based on loczrama setting overriding default assignment
  848. * done by k3_rproc_of_get_memories().
  849. */
  850. if (!strcmp(data->mems[i].name, "atcm")) {
  851. kproc->mem[i].dev_addr = core->loczrama ?
  852. 0 : K3_R5_TCM_DEV_ADDR;
  853. } else {
  854. kproc->mem[i].dev_addr = core->loczrama ?
  855. K3_R5_TCM_DEV_ADDR : 0;
  856. }
  857. dev_dbg(dev, "Updating bus addr %pa of memory %5s\n",
  858. &kproc->mem[i].bus_addr, data->mems[i].name);
  859. }
  860. return 0;
  861. }
  862. static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
  863. struct k3_r5_core *core)
  864. {
  865. struct device_node *np = pdev->dev.of_node;
  866. struct device *dev = &pdev->dev;
  867. struct device_node *sram_np;
  868. struct resource res;
  869. int num_sram;
  870. int i, ret;
  871. num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
  872. if (num_sram <= 0) {
  873. dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
  874. num_sram);
  875. return 0;
  876. }
  877. core->sram = devm_kcalloc(dev, num_sram, sizeof(*core->sram), GFP_KERNEL);
  878. if (!core->sram)
  879. return -ENOMEM;
  880. for (i = 0; i < num_sram; i++) {
  881. sram_np = of_parse_phandle(np, "sram", i);
  882. if (!sram_np)
  883. return -EINVAL;
  884. if (!of_device_is_available(sram_np)) {
  885. of_node_put(sram_np);
  886. return -EINVAL;
  887. }
  888. ret = of_address_to_resource(sram_np, 0, &res);
  889. of_node_put(sram_np);
  890. if (ret)
  891. return -EINVAL;
  892. core->sram[i].bus_addr = res.start;
  893. core->sram[i].dev_addr = res.start;
  894. core->sram[i].size = resource_size(&res);
  895. core->sram[i].cpu_addr = devm_ioremap_wc(dev, res.start,
  896. resource_size(&res));
  897. if (!core->sram[i].cpu_addr) {
  898. dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
  899. i, &res.start);
  900. return -ENOMEM;
  901. }
  902. dev_dbg(dev, "memory sram%d: bus addr %pa size 0x%zx va %p da 0x%x\n",
  903. i, &core->sram[i].bus_addr,
  904. core->sram[i].size, core->sram[i].cpu_addr,
  905. core->sram[i].dev_addr);
  906. }
  907. core->num_sram = num_sram;
  908. return 0;
  909. }
  910. static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
  911. {
  912. struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
  913. struct device *dev = &pdev->dev;
  914. struct k3_rproc *kproc;
  915. struct k3_r5_core *core, *core1;
  916. struct device_node *np;
  917. struct device *cdev;
  918. const char *fw_name;
  919. struct rproc *rproc;
  920. int ret, ret1;
  921. core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  922. list_for_each_entry(core, &cluster->cores, elem) {
  923. cdev = core->dev;
  924. np = dev_of_node(cdev);
  925. ret = rproc_of_parse_firmware(cdev, 0, &fw_name);
  926. if (ret) {
  927. dev_err(dev, "failed to parse firmware-name property, ret = %d\n",
  928. ret);
  929. goto out;
  930. }
  931. rproc = devm_rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
  932. fw_name, sizeof(*kproc));
  933. if (!rproc) {
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. /* K3 R5s have a Region Address Translator (RAT) but no MMU */
  938. rproc->has_iommu = false;
  939. /* error recovery is not supported at present */
  940. rproc->recovery_disabled = true;
  941. kproc = rproc->priv;
  942. kproc->priv = core;
  943. kproc->dev = cdev;
  944. kproc->rproc = rproc;
  945. kproc->data = cluster->soc_data->core_data;
  946. core->kproc = kproc;
  947. kproc->ti_sci = devm_ti_sci_get_by_phandle(cdev, "ti,sci");
  948. if (IS_ERR(kproc->ti_sci)) {
  949. ret = dev_err_probe(cdev, PTR_ERR(kproc->ti_sci),
  950. "failed to get ti-sci handle\n");
  951. kproc->ti_sci = NULL;
  952. goto out;
  953. }
  954. ret = of_property_read_u32(np, "ti,sci-dev-id", &kproc->ti_sci_id);
  955. if (ret) {
  956. dev_err(cdev, "missing 'ti,sci-dev-id' property\n");
  957. goto out;
  958. }
  959. kproc->reset = devm_reset_control_get_exclusive(cdev, NULL);
  960. if (IS_ERR_OR_NULL(kproc->reset)) {
  961. ret = PTR_ERR_OR_ZERO(kproc->reset);
  962. if (!ret)
  963. ret = -ENODEV;
  964. dev_err_probe(cdev, ret, "failed to get reset handle\n");
  965. goto out;
  966. }
  967. kproc->tsp = ti_sci_proc_of_get_tsp(cdev, kproc->ti_sci);
  968. if (IS_ERR(kproc->tsp)) {
  969. ret = dev_err_probe(cdev, PTR_ERR(kproc->tsp),
  970. "failed to construct ti-sci proc control\n");
  971. goto out;
  972. }
  973. ret = k3_r5_core_of_get_internal_memories(to_platform_device(cdev), kproc);
  974. if (ret) {
  975. dev_err(cdev, "failed to get internal memories, ret = %d\n",
  976. ret);
  977. goto out;
  978. }
  979. ret = ti_sci_proc_request(kproc->tsp);
  980. if (ret < 0) {
  981. dev_err(cdev, "ti_sci_proc_request failed, ret = %d\n", ret);
  982. goto out;
  983. }
  984. ret = devm_add_action_or_reset(cdev, k3_release_tsp, kproc->tsp);
  985. if (ret)
  986. goto out;
  987. }
  988. list_for_each_entry(core, &cluster->cores, elem) {
  989. cdev = core->dev;
  990. kproc = core->kproc;
  991. rproc = kproc->rproc;
  992. ret = k3_rproc_request_mbox(rproc);
  993. if (ret)
  994. return ret;
  995. ret = k3_r5_rproc_configure_mode(kproc);
  996. if (ret < 0)
  997. goto out;
  998. if (ret)
  999. goto init_rmem;
  1000. ret = k3_r5_rproc_configure(kproc);
  1001. if (ret) {
  1002. dev_err(cdev, "initial configure failed, ret = %d\n",
  1003. ret);
  1004. goto out;
  1005. }
  1006. init_rmem:
  1007. k3_r5_adjust_tcm_sizes(kproc);
  1008. ret = k3_reserved_mem_init(kproc);
  1009. if (ret) {
  1010. dev_err(cdev, "reserved memory init failed, ret = %d\n",
  1011. ret);
  1012. goto out;
  1013. }
  1014. ret = devm_rproc_add(cdev, rproc);
  1015. if (ret) {
  1016. dev_err_probe(cdev, ret, "rproc_add failed\n");
  1017. goto out;
  1018. }
  1019. /* create only one rproc in lockstep, single-cpu or
  1020. * single core mode
  1021. */
  1022. if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  1023. cluster->mode == CLUSTER_MODE_SINGLECPU ||
  1024. cluster->mode == CLUSTER_MODE_SINGLECORE)
  1025. break;
  1026. }
  1027. return 0;
  1028. err_split:
  1029. if (rproc->state == RPROC_ATTACHED) {
  1030. ret1 = rproc_detach(rproc);
  1031. if (ret1) {
  1032. dev_err(kproc->dev, "failed to detach rproc, ret = %d\n",
  1033. ret1);
  1034. return ret1;
  1035. }
  1036. }
  1037. out:
  1038. /* undo core0 upon any failures on core1 in split-mode */
  1039. if (cluster->mode == CLUSTER_MODE_SPLIT && core == core1) {
  1040. core = list_prev_entry(core, elem);
  1041. kproc = core->kproc;
  1042. rproc = kproc->rproc;
  1043. goto err_split;
  1044. }
  1045. return ret;
  1046. }
  1047. static void k3_r5_cluster_rproc_exit(void *data)
  1048. {
  1049. struct k3_r5_cluster *cluster = platform_get_drvdata(data);
  1050. struct k3_rproc *kproc;
  1051. struct k3_r5_core *core;
  1052. struct rproc *rproc;
  1053. int ret;
  1054. /*
  1055. * lockstep mode and single-cpu modes have only one rproc associated
  1056. * with first core, whereas split-mode has two rprocs associated with
  1057. * each core, and requires that core1 be powered down first
  1058. */
  1059. core = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
  1060. cluster->mode == CLUSTER_MODE_SINGLECPU) ?
  1061. list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
  1062. list_last_entry(&cluster->cores, struct k3_r5_core, elem);
  1063. list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
  1064. kproc = core->kproc;
  1065. rproc = kproc->rproc;
  1066. if (rproc->state == RPROC_ATTACHED) {
  1067. ret = rproc_detach(rproc);
  1068. if (ret) {
  1069. dev_err(kproc->dev, "failed to detach rproc, ret = %d\n", ret);
  1070. return;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. static int k3_r5_core_of_init(struct platform_device *pdev)
  1076. {
  1077. struct device *dev = &pdev->dev;
  1078. struct device_node *np = dev_of_node(dev);
  1079. struct k3_r5_core *core;
  1080. int ret;
  1081. if (!devres_open_group(dev, k3_r5_core_of_init, GFP_KERNEL))
  1082. return -ENOMEM;
  1083. core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
  1084. if (!core) {
  1085. ret = -ENOMEM;
  1086. goto err;
  1087. }
  1088. core->dev = dev;
  1089. /*
  1090. * Use SoC Power-on-Reset values as default if no DT properties are
  1091. * used to dictate the TCM configurations
  1092. */
  1093. core->atcm_enable = 0;
  1094. core->btcm_enable = 1;
  1095. core->loczrama = 1;
  1096. ret = of_property_read_u32(np, "ti,atcm-enable", &core->atcm_enable);
  1097. if (ret < 0 && ret != -EINVAL) {
  1098. dev_err(dev, "invalid format for ti,atcm-enable, ret = %d\n",
  1099. ret);
  1100. goto err;
  1101. }
  1102. ret = of_property_read_u32(np, "ti,btcm-enable", &core->btcm_enable);
  1103. if (ret < 0 && ret != -EINVAL) {
  1104. dev_err(dev, "invalid format for ti,btcm-enable, ret = %d\n",
  1105. ret);
  1106. goto err;
  1107. }
  1108. ret = of_property_read_u32(np, "ti,loczrama", &core->loczrama);
  1109. if (ret < 0 && ret != -EINVAL) {
  1110. dev_err(dev, "invalid format for ti,loczrama, ret = %d\n", ret);
  1111. goto err;
  1112. }
  1113. ret = k3_r5_core_of_get_sram_memories(pdev, core);
  1114. if (ret) {
  1115. dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
  1116. goto err;
  1117. }
  1118. platform_set_drvdata(pdev, core);
  1119. devres_close_group(dev, k3_r5_core_of_init);
  1120. return 0;
  1121. err:
  1122. devres_release_group(dev, k3_r5_core_of_init);
  1123. return ret;
  1124. }
  1125. /*
  1126. * free the resources explicitly since driver model is not being used
  1127. * for the child R5F devices
  1128. */
  1129. static void k3_r5_core_of_exit(struct platform_device *pdev)
  1130. {
  1131. struct device *dev = &pdev->dev;
  1132. platform_set_drvdata(pdev, NULL);
  1133. devres_release_group(dev, k3_r5_core_of_init);
  1134. }
  1135. static void k3_r5_cluster_of_exit(void *data)
  1136. {
  1137. struct k3_r5_cluster *cluster = platform_get_drvdata(data);
  1138. struct platform_device *cpdev;
  1139. struct k3_r5_core *core, *temp;
  1140. list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
  1141. list_del(&core->elem);
  1142. cpdev = to_platform_device(core->dev);
  1143. k3_r5_core_of_exit(cpdev);
  1144. }
  1145. }
  1146. static int k3_r5_cluster_of_init(struct platform_device *pdev)
  1147. {
  1148. struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
  1149. struct device *dev = &pdev->dev;
  1150. struct device_node *np = dev_of_node(dev);
  1151. struct platform_device *cpdev;
  1152. struct k3_r5_core *core;
  1153. int ret;
  1154. for_each_available_child_of_node_scoped(np, child) {
  1155. cpdev = of_find_device_by_node(child);
  1156. if (!cpdev) {
  1157. ret = -ENODEV;
  1158. dev_err(dev, "could not get R5 core platform device\n");
  1159. goto fail;
  1160. }
  1161. ret = k3_r5_core_of_init(cpdev);
  1162. if (ret) {
  1163. dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
  1164. ret);
  1165. put_device(&cpdev->dev);
  1166. goto fail;
  1167. }
  1168. core = platform_get_drvdata(cpdev);
  1169. core->cluster = cluster;
  1170. put_device(&cpdev->dev);
  1171. list_add_tail(&core->elem, &cluster->cores);
  1172. }
  1173. return 0;
  1174. fail:
  1175. k3_r5_cluster_of_exit(pdev);
  1176. return ret;
  1177. }
  1178. static int k3_r5_probe(struct platform_device *pdev)
  1179. {
  1180. struct device *dev = &pdev->dev;
  1181. struct device_node *np = dev_of_node(dev);
  1182. struct k3_r5_cluster *cluster;
  1183. const struct k3_r5_soc_data *data;
  1184. int ret;
  1185. int num_cores;
  1186. data = of_device_get_match_data(&pdev->dev);
  1187. if (!data) {
  1188. dev_err(dev, "SoC-specific data is not defined\n");
  1189. return -ENODEV;
  1190. }
  1191. cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
  1192. if (!cluster)
  1193. return -ENOMEM;
  1194. cluster->dev = dev;
  1195. cluster->soc_data = data;
  1196. INIT_LIST_HEAD(&cluster->cores);
  1197. init_waitqueue_head(&cluster->core_transition);
  1198. ret = of_property_read_u32(np, "ti,cluster-mode", &cluster->mode);
  1199. if (ret < 0 && ret != -EINVAL)
  1200. return dev_err_probe(dev, ret, "invalid format for ti,cluster-mode\n");
  1201. if (ret == -EINVAL) {
  1202. /*
  1203. * default to most common efuse configurations - Split-mode on AM64x
  1204. * and LockStep-mode on all others
  1205. * default to most common efuse configurations -
  1206. * Split-mode on AM64x
  1207. * Single core on AM62x
  1208. * LockStep-mode on all others
  1209. */
  1210. if (!data->is_single_core)
  1211. cluster->mode = data->single_cpu_mode ?
  1212. CLUSTER_MODE_SPLIT : CLUSTER_MODE_LOCKSTEP;
  1213. else
  1214. cluster->mode = CLUSTER_MODE_SINGLECORE;
  1215. }
  1216. if ((cluster->mode == CLUSTER_MODE_SINGLECPU && !data->single_cpu_mode) ||
  1217. (cluster->mode == CLUSTER_MODE_SINGLECORE && !data->is_single_core))
  1218. return dev_err_probe(dev, -EINVAL,
  1219. "Cluster mode = %d is not supported on this SoC\n",
  1220. cluster->mode);
  1221. num_cores = of_get_available_child_count(np);
  1222. if (num_cores != 2 && !data->is_single_core)
  1223. return dev_err_probe(dev, -ENODEV,
  1224. "MCU cluster requires both R5F cores to be enabled but num_cores is set to = %d\n",
  1225. num_cores);
  1226. if (num_cores != 1 && data->is_single_core)
  1227. return dev_err_probe(dev, -ENODEV,
  1228. "SoC supports only single core R5 but num_cores is set to %d\n",
  1229. num_cores);
  1230. platform_set_drvdata(pdev, cluster);
  1231. ret = devm_of_platform_populate(dev);
  1232. if (ret)
  1233. return dev_err_probe(dev, ret, "devm_of_platform_populate failed\n");
  1234. ret = k3_r5_cluster_of_init(pdev);
  1235. if (ret)
  1236. return dev_err_probe(dev, ret, "k3_r5_cluster_of_init failed\n");
  1237. ret = devm_add_action_or_reset(dev, k3_r5_cluster_of_exit, pdev);
  1238. if (ret)
  1239. return ret;
  1240. ret = k3_r5_cluster_rproc_init(pdev);
  1241. if (ret)
  1242. return dev_err_probe(dev, ret, "k3_r5_cluster_rproc_init failed\n");
  1243. ret = devm_add_action_or_reset(dev, k3_r5_cluster_rproc_exit, pdev);
  1244. if (ret)
  1245. return ret;
  1246. return 0;
  1247. }
  1248. static const struct k3_rproc_mem_data r5_mems[] = {
  1249. { .name = "atcm", .dev_addr = 0x0 },
  1250. { .name = "btcm", .dev_addr = K3_R5_TCM_DEV_ADDR },
  1251. };
  1252. static const struct k3_rproc_dev_data r5_data = {
  1253. .mems = r5_mems,
  1254. .num_mems = ARRAY_SIZE(r5_mems),
  1255. .boot_align_addr = 0,
  1256. .uses_lreset = true,
  1257. };
  1258. static const struct k3_r5_soc_data am65_j721e_soc_data = {
  1259. .tcm_is_double = false,
  1260. .tcm_ecc_autoinit = false,
  1261. .single_cpu_mode = false,
  1262. .is_single_core = false,
  1263. .core_data = &r5_data,
  1264. };
  1265. static const struct k3_r5_soc_data j7200_j721s2_soc_data = {
  1266. .tcm_is_double = true,
  1267. .tcm_ecc_autoinit = true,
  1268. .single_cpu_mode = false,
  1269. .is_single_core = false,
  1270. .core_data = &r5_data,
  1271. };
  1272. static const struct k3_r5_soc_data am64_soc_data = {
  1273. .tcm_is_double = true,
  1274. .tcm_ecc_autoinit = true,
  1275. .single_cpu_mode = true,
  1276. .is_single_core = false,
  1277. .core_data = &r5_data,
  1278. };
  1279. static const struct k3_r5_soc_data am62_soc_data = {
  1280. .tcm_is_double = false,
  1281. .tcm_ecc_autoinit = true,
  1282. .single_cpu_mode = false,
  1283. .is_single_core = true,
  1284. .core_data = &r5_data,
  1285. };
  1286. static const struct of_device_id k3_r5_of_match[] = {
  1287. { .compatible = "ti,am654-r5fss", .data = &am65_j721e_soc_data, },
  1288. { .compatible = "ti,j721e-r5fss", .data = &am65_j721e_soc_data, },
  1289. { .compatible = "ti,j7200-r5fss", .data = &j7200_j721s2_soc_data, },
  1290. { .compatible = "ti,am64-r5fss", .data = &am64_soc_data, },
  1291. { .compatible = "ti,am62-r5fss", .data = &am62_soc_data, },
  1292. { .compatible = "ti,j721s2-r5fss", .data = &j7200_j721s2_soc_data, },
  1293. { /* sentinel */ },
  1294. };
  1295. MODULE_DEVICE_TABLE(of, k3_r5_of_match);
  1296. static struct platform_driver k3_r5_rproc_driver = {
  1297. .probe = k3_r5_probe,
  1298. .driver = {
  1299. .name = "k3_r5_rproc",
  1300. .of_match_table = k3_r5_of_match,
  1301. },
  1302. };
  1303. module_platform_driver(k3_r5_rproc_driver);
  1304. MODULE_LICENSE("GPL v2");
  1305. MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
  1306. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");