ti_k3_r5_remoteproc.c 52 KB

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