Debian Codegraph @

drivers/remoteproc/xlnx_r5_remoteproc.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ZynqMP R5 Remote Processor driver
4 *
5 */
6
7 #include <dt-bindings/power/xlnx-zynqmp-power.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/firmware/xlnx-zynqmp.h>
10 #include <linux/kernel.h>
11 #include <linux/mailbox_client.h>
12 #include <linux/mailbox/zynqmp-ipi-message.h>
13 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/of_platform.h>
16 #include <linux/of_reserved_mem.h>
17 #include <linux/platform_device.h>
18 #include <linux/remoteproc.h>
19
20 #include "remoteproc_internal.h"
21
22 /* IPI buffer MAX length */
23 #define IPI_BUF_LEN_MAX 32U
24
25 /* RX mailbox client buffer max length */
26 #define MBOX_CLIENT_BUF_MAX (IPI_BUF_LEN_MAX + \
27 sizeof(struct zynqmp_ipi_message))
28
29 #define RSC_TBL_XLNX_MAGIC ((uint32_t)'x' << 24 | (uint32_t)'a' << 16 | \
30 (uint32_t)'m' << 8 | (uint32_t)'p')
31
32 /*
33 * settings for RPU cluster mode which
34 * reflects possible values of xlnx,cluster-mode dt-property
35 */
36 enum zynqmp_r5_cluster_mode {
37 SPLIT_MODE = 0, /* When cores run as separate processor */
38 LOCKSTEP_MODE = 1, /* cores execute same code in lockstep,clk-for-clk */
39 SINGLE_CPU_MODE = 2, /* core0 is held in reset and only core1 runs */
40 };
41
42 /**
43 * struct mem_bank_data - Memory Bank description
44 *
45 * @addr: Start address of memory bank
46 * @da: device address
47 * @size: Size of Memory bank
48 * @pm_domain_id: Power-domains id of memory bank for firmware to turn on/off
49 * @bank_name: name of the bank for remoteproc framework
50 */
51 struct mem_bank_data {
52 phys_addr_t addr;
53 u32 da;
54 size_t size;
55 u32 pm_domain_id;
56 char *bank_name;
57 };
58
59 /**
60 * struct zynqmp_sram_bank - sram bank description
61 *
62 * @sram_res: sram address region information
63 * @da: device address of sram
64 */
65 struct zynqmp_sram_bank {
66 struct resource sram_res;
67 u32 da;
68 };
69
70 /**
71 * struct mbox_info - mailbox channel data
72 *
73 * @rx_mc_buf: to copy data from mailbox rx channel
74 * @tx_mc_buf: to copy data to mailbox tx channel
75 * @r5_core: this mailbox's corresponding r5_core pointer
76 * @mbox_work: schedule work after receiving data from mailbox
77 * @mbox_cl: mailbox client
78 * @tx_chan: mailbox tx channel
79 * @rx_chan: mailbox rx channel
80 */
81 struct mbox_info {
82 unsigned char rx_mc_buf[MBOX_CLIENT_BUF_MAX];
83 unsigned char tx_mc_buf[MBOX_CLIENT_BUF_MAX];
84 struct zynqmp_r5_core *r5_core;
85 struct work_struct mbox_work;
86 struct mbox_client mbox_cl;
87 struct mbox_chan *tx_chan;
88 struct mbox_chan *rx_chan;
89 };
90
91 /**
92 * struct rsc_tbl_data - resource table metadata
93 *
94 * Platform specific data structure used to sync resource table address.
95 * It's important to maintain order and size of each field on remote side.
96 *
97 * @version: version of data structure
98 * @magic_num: 32-bit magic number.
99 * @comp_magic_num: complement of above magic number
100 * @rsc_tbl_size: resource table size
101 * @rsc_tbl: resource table address
102 */
103 struct rsc_tbl_data {
104 const int version;
105 const u32 magic_num;
106 const u32 comp_magic_num;
107 const u32 rsc_tbl_size;
108 const uintptr_t rsc_tbl;
109 } __packed;
110
111 /*
112 * Hardcoded TCM bank values. This will stay in driver to maintain backward
113 * compatibility with device-tree that does not have TCM information.
114 */
115 static const struct mem_bank_data zynqmp_tcm_banks_split[] = {
116 {0xffe00000UL, 0x0, 0x10000UL, PD_R5_0_ATCM, "atcm0"}, /* TCM 64KB each */
117 {0xffe20000UL, 0x20000, 0x10000UL, PD_R5_0_BTCM, "btcm0"},
118 {0xffe90000UL, 0x0, 0x10000UL, PD_R5_1_ATCM, "atcm1"},
119 {0xffeb0000UL, 0x20000, 0x10000UL, PD_R5_1_BTCM, "btcm1"},
120 };
121
122 /* In lockstep mode cluster uses each 64KB TCM from second core as well */
123 static const struct mem_bank_data zynqmp_tcm_banks_lockstep[] = {
124 {0xffe00000UL, 0x0, 0x10000UL, PD_R5_0_ATCM, "atcm0"}, /* TCM 64KB each */
125 {0xffe20000UL, 0x20000, 0x10000UL, PD_R5_0_BTCM, "btcm0"},
126 {0xffe10000UL, 0x10000, 0x10000UL, PD_R5_1_ATCM, "atcm1"},
127 {0xffe30000UL, 0x30000, 0x10000UL, PD_R5_1_BTCM, "btcm1"},
128 };
129
130 /**
131 * struct zynqmp_r5_core - remoteproc core's internal data
132 *
133 * @rsc_tbl_va: resource table virtual address
134 * @sram: Array of sram memories assigned to this core
135 * @num_sram: number of sram for this core
136 * @dev: device of RPU instance
137 * @np: device node of RPU instance
138 * @tcm_bank_count: number TCM banks accessible to this RPU
139 * @tcm_banks: array of each TCM bank data
140 * @rproc: rproc handle
141 * @rsc_tbl_size: resource table size retrieved from remote
142 * @pm_domain_id: RPU CPU power domain id
143 * @ipi: pointer to mailbox information
144 */
145 struct zynqmp_r5_core {
146 void __iomem *rsc_tbl_va;
147 struct zynqmp_sram_bank *sram;
148 int num_sram;
149 struct device *dev;
150 struct device_node *np;
151 int tcm_bank_count;
152 struct mem_bank_data **tcm_banks;
153 struct rproc *rproc;
154 u32 rsc_tbl_size;
155 u32 pm_domain_id;
156 struct mbox_info *ipi;
157 };
158
159 /**
160 * struct zynqmp_r5_cluster - remoteproc cluster's internal data
161 *
162 * @dev: r5f subsystem cluster device node
163 * @mode: cluster mode of type zynqmp_r5_cluster_mode
164 * @core_count: number of r5 cores used for this cluster mode
165 * @r5_cores: Array of pointers pointing to r5 core
166 */
167 struct zynqmp_r5_cluster {
168 struct device *dev;
169 enum zynqmp_r5_cluster_mode mode;
170 int core_count;
171 struct zynqmp_r5_core **r5_cores;
172 };
173
174 /**
175 * event_notified_idr_cb() - callback for vq_interrupt per notifyid
176 * @id: rproc->notify id
177 * @ptr: pointer to idr private data
178 * @data: data passed to idr_for_each callback
179 *
180 * Pass notification to remoteproc virtio
181 *
182 * Return: 0. having return is to satisfy the idr_for_each() function
183 * pointer input argument requirement.
184 **/
185 static int event_notified_idr_cb(int id, void *ptr, void *data)
186 {
187 struct rproc *rproc = data;
188
189 if (rproc_vq_interrupt(rproc, id) == IRQ_NONE)
190 dev_dbg(&rproc->dev, "data not found for vqid=%d\n", id);
191
192 return 0;
193 }
194
195 /**
196 * handle_event_notified() - remoteproc notification work function
197 * @work: pointer to the work structure
198 *
199 * It checks each registered remoteproc notify IDs.
200 */
201 static void handle_event_notified(struct work_struct *work)
202 {
203 struct mbox_info *ipi;
204 struct rproc *rproc;
205
206 ipi = container_of(work, struct mbox_info, mbox_work);
207 rproc = ipi->r5_core->rproc;
208
209 /*
210 * We only use IPI for interrupt. The RPU firmware side may or may
211 * not write the notifyid when it trigger IPI.
212 * And thus, we scan through all the registered notifyids and
213 * find which one is valid to get the message.
214 * Even if message from firmware is NULL, we attempt to get vqid
215 */
216 idr_for_each(&rproc->notifyids, event_notified_idr_cb, rproc);
217 }
218
219 /**
220 * zynqmp_r5_mb_rx_cb() - receive channel mailbox callback
221 * @cl: mailbox client
222 * @msg: message pointer
223 *
224 * Receive data from ipi buffer, ack interrupt and then
225 * it will schedule the R5 notification work.
226 */
227 static void zynqmp_r5_mb_rx_cb(struct mbox_client *cl, void *msg)
228 {
229 struct zynqmp_ipi_message *ipi_msg, *buf_msg;
230 struct mbox_info *ipi;
231 size_t len;
232
233 ipi = container_of(cl, struct mbox_info, mbox_cl);
234
235 /* copy data from ipi buffer to r5_core if IPI is buffered. */
236 ipi_msg = (struct zynqmp_ipi_message *)msg;
237 if (ipi_msg) {
238 buf_msg = (struct zynqmp_ipi_message *)ipi->rx_mc_buf;
239 len = ipi_msg->len;
240 if (len > IPI_BUF_LEN_MAX) {
241 dev_warn(cl->dev, "msg size exceeded than %d\n",
242 IPI_BUF_LEN_MAX);
243 len = IPI_BUF_LEN_MAX;
244 }
245 buf_msg->len = len;
246 memcpy(buf_msg->data, ipi_msg->data, len);
247 }
248
249 /* received and processed interrupt ack */
250 if (mbox_send_message(ipi->rx_chan, NULL) < 0)
251 dev_err(cl->dev, "ack failed to mbox rx_chan\n");
252
253 schedule_work(&ipi->mbox_work);
254 }
255
256 /**
257 * zynqmp_r5_setup_mbox() - Setup mailboxes related properties
258 * this is used for each individual R5 core
259 *
260 * @cdev: child node device
261 *
262 * Function to setup mailboxes related properties
263 * return : NULL if failed else pointer to mbox_info
264 */
265 static struct mbox_info *zynqmp_r5_setup_mbox(struct device *cdev)
266 {
267 struct mbox_client *mbox_cl;
268 struct mbox_info *ipi;
269
270 ipi = kzalloc_obj(*ipi);
271 if (!ipi)
272 return NULL;
273
274 mbox_cl = &ipi->mbox_cl;
275 mbox_cl->rx_callback = zynqmp_r5_mb_rx_cb;
276 mbox_cl->tx_block = false;
277 mbox_cl->knows_txdone = false;
278 mbox_cl->tx_done = NULL;
279 mbox_cl->dev = cdev;
280
281 /* Request TX and RX channels */
282 ipi->tx_chan = mbox_request_channel_byname(mbox_cl, "tx");
283 if (IS_ERR(ipi->tx_chan)) {
284 ipi->tx_chan = NULL;
285 kfree(ipi);
286 dev_warn(cdev, "mbox tx channel request failed\n");
287 return NULL;
288 }
289
290 ipi->rx_chan = mbox_request_channel_byname(mbox_cl, "rx");
291 if (IS_ERR(ipi->rx_chan)) {
292 mbox_free_channel(ipi->tx_chan);
293 ipi->rx_chan = NULL;
294 ipi->tx_chan = NULL;
295 kfree(ipi);
296 dev_warn(cdev, "mbox rx channel request failed\n");
297 return NULL;
298 }
299
300 INIT_WORK(&ipi->mbox_work, handle_event_notified);
301
302 return ipi;
303 }
304
305 static void zynqmp_r5_free_mbox(struct mbox_info *ipi)
306 {
307 if (!ipi)
308 return;
309
310 if (ipi->tx_chan) {
311 mbox_free_channel(ipi->tx_chan);
312 ipi->tx_chan = NULL;
313 }
314
315 if (ipi->rx_chan) {
316 mbox_free_channel(ipi->rx_chan);
317 ipi->rx_chan = NULL;
318 }
319
320 kfree(ipi);
321 }
322
323 /*
324 * zynqmp_r5_core_kick() - kick a firmware if mbox is provided
325 * @rproc: r5 core's corresponding rproc structure
326 * @vqid: virtqueue ID
327 */
328 static void zynqmp_r5_rproc_kick(struct rproc *rproc, int vqid)
329 {
330 struct zynqmp_r5_core *r5_core = rproc->priv;
331 struct device *dev = r5_core->dev;
332 struct zynqmp_ipi_message *mb_msg;
333 struct mbox_info *ipi;
334 int ret;
335
336 ipi = r5_core->ipi;
337 if (!ipi)
338 return;
339
340 mb_msg = (struct zynqmp_ipi_message *)ipi->tx_mc_buf;
341 memcpy(mb_msg->data, &vqid, sizeof(vqid));
342 mb_msg->len = sizeof(vqid);
343 ret = mbox_send_message(ipi->tx_chan, mb_msg);
344 if (ret < 0)
345 dev_warn(dev, "failed to send message\n");
346 }
347
348 /*
349 * zynqmp_r5_rproc_start()
350 * @rproc: single R5 core's corresponding rproc instance
351 *
352 * Start R5 Core from designated boot address.
353 *
354 * return 0 on success, otherwise non-zero value on failure
355 */
356 static int zynqmp_r5_rproc_start(struct rproc *rproc)
357 {
358 struct zynqmp_r5_core *r5_core = rproc->priv;
359 enum rpu_boot_mem bootmem;
360 int ret;
361
362 /*
363 * The exception vector pointers (EVP) refer to the base-address of
364 * exception vectors (for reset, IRQ, FIQ, etc). The reset-vector
365 * starts at the base-address and subsequent vectors are on 4-byte
366 * boundaries.
367 *
368 * Exception vectors can start either from 0x0000_0000 (LOVEC) or
369 * from 0xFFFF_0000 (HIVEC) which is mapped in the OCM (On-Chip Memory)
370 *
371 * Usually firmware will put Exception vectors at LOVEC.
372 *
373 * It is not recommend that you change the exception vector.
374 * Changing the EVP to HIVEC will result in increased interrupt latency
375 * and jitter. Also, if the OCM is secured and the Cortex-R5F processor
376 * is non-secured, then the Cortex-R5F processor cannot access the
377 * HIVEC exception vectors in the OCM.
378 */
379 bootmem = (rproc->bootaddr >= 0xFFFC0000) ?
380 PM_RPU_BOOTMEM_HIVEC : PM_RPU_BOOTMEM_LOVEC;
381
382 dev_dbg(r5_core->dev, "RPU boot addr 0x%llx from %s.", rproc->bootaddr,
383 bootmem == PM_RPU_BOOTMEM_HIVEC ? "OCM" : "TCM");
384
385 /* Request node before starting RPU core if new version of API is supported */
386 if (zynqmp_pm_feature(PM_REQUEST_NODE) > 1) {
387 ret = zynqmp_pm_request_node(r5_core->pm_domain_id,
388 ZYNQMP_PM_CAPABILITY_ACCESS, 0,
389 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
390 if (ret < 0) {
391 dev_err(r5_core->dev, "failed to request 0x%x",
392 r5_core->pm_domain_id);
393 return ret;
394 }
395 }
396
397 ret = zynqmp_pm_request_wake(r5_core->pm_domain_id, 1,
398 bootmem, ZYNQMP_PM_REQUEST_ACK_NO);
399 if (ret)
400 dev_err(r5_core->dev,
401 "failed to start RPU = 0x%x\n", r5_core->pm_domain_id);
402 return ret;
403 }
404
405 /*
406 * zynqmp_r5_rproc_stop()
407 * @rproc: single R5 core's corresponding rproc instance
408 *
409 * Power down R5 Core.
410 *
411 * return 0 on success, otherwise non-zero value on failure
412 */
413 static int zynqmp_r5_rproc_stop(struct rproc *rproc)
414 {
415 struct zynqmp_r5_core *r5_core = rproc->priv;
416 int ret;
417
418 /* Use release node API to stop core if new version of API is supported */
419 if (zynqmp_pm_feature(PM_RELEASE_NODE) > 1) {
420 ret = zynqmp_pm_release_node(r5_core->pm_domain_id);
421 if (ret)
422 dev_err(r5_core->dev, "failed to stop remoteproc RPU %d\n", ret);
423 return ret;
424 }
425
426 /*
427 * Check expected version of EEMI call before calling it. This avoids
428 * any error or warning prints from firmware as it is expected that fw
429 * doesn't support it.
430 */
431 if (zynqmp_pm_feature(PM_FORCE_POWERDOWN) != 1) {
432 dev_dbg(r5_core->dev, "EEMI interface %d ver 1 not supported\n",
433 PM_FORCE_POWERDOWN);
434 return -EOPNOTSUPP;
435 }
436
437 /* maintain force pwr down for backward compatibility */
438 ret = zynqmp_pm_force_pwrdwn(r5_core->pm_domain_id,
439 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
440 if (ret)
441 dev_err(r5_core->dev, "core force power down failed\n");
442
443 return ret;
444 }
445
446 /*
447 * zynqmp_r5_mem_region_map()
448 * @rproc: single R5 core's corresponding rproc instance
449 * @mem: mem descriptor to map reserved memory-regions
450 *
451 * Callback to map va for memory-region's carveout.
452 *
453 * return 0 on success, otherwise non-zero value on failure
454 */
455 static int zynqmp_r5_mem_region_map(struct rproc *rproc,
456 struct rproc_mem_entry *mem)
457 {
458 void __iomem *va;
459
460 va = ioremap_wc(mem->dma, mem->len);
461 if (IS_ERR_OR_NULL(va))
462 return -ENOMEM;
463
464 mem->va = (void *)va;
465
466 return 0;
467 }
468
469 /*
470 * zynqmp_r5_rproc_mem_unmap
471 * @rproc: single R5 core's corresponding rproc instance
472 * @mem: mem entry to unmap
473 *
474 * Unmap memory-region carveout
475 *
476 * return: always returns 0
477 */
478 static int zynqmp_r5_mem_region_unmap(struct rproc *rproc,
479 struct rproc_mem_entry *mem)
480 {
481 iounmap((void __iomem *)mem->va);
482 return 0;
483 }
484
485 /*
486 * add_mem_regions_carveout()
487 * @rproc: single R5 core's corresponding rproc instance
488 *
489 * Construct rproc mem carveouts from memory-region property nodes
490 *
491 * return 0 on success, otherwise non-zero value on failure
492 */
493 static int add_mem_regions_carveout(struct rproc *rproc)
494 {
495 struct rproc_mem_entry *rproc_mem;
496 struct zynqmp_r5_core *r5_core;
497 int i = 0;
498
499 r5_core = rproc->priv;
500
501 /* Register associated reserved memory regions */
502 while (1) {
503 int err;
504 struct resource res;
505
506 err = of_reserved_mem_region_to_resource(r5_core->np, i, &res);
507 if (err)
508 return 0;
509
510 if (strstarts(res.name, "vdev0buffer")) {
511 /* Init reserved memory for vdev buffer */
512 rproc_mem = rproc_of_resm_mem_entry_init(&rproc->dev, i,
513 resource_size(&res),
514 res.start,
515 "vdev0buffer");
516 } else {
517 /* Register associated reserved memory regions */
518 rproc_mem = rproc_mem_entry_init(&rproc->dev, NULL,
519 (dma_addr_t)res.start,
520 resource_size(&res), res.start,
521 zynqmp_r5_mem_region_map,
522 zynqmp_r5_mem_region_unmap,
523 "%.*s",
524 strchrnul(res.name, '@') - res.name,
525 res.name);
526 }
527
528 if (!rproc_mem)
529 return -ENOMEM;
530
531 rproc_add_carveout(rproc, rproc_mem);
532 rproc_coredump_add_segment(rproc, res.start, resource_size(&res));
533
534 dev_dbg(&rproc->dev, "reserved mem carveout %pR\n", &res);
535 i++;
536 }
537 }
538
539 static int add_sram_carveouts(struct rproc *rproc)
540 {
541 struct zynqmp_r5_core *r5_core = rproc->priv;
542 struct rproc_mem_entry *rproc_mem;
543 struct zynqmp_sram_bank *sram;
544 dma_addr_t dma_addr;
545 size_t len;
546 int da, i;
547
548 for (i = 0; i < r5_core->num_sram; i++) {
549 sram = &r5_core->sram[i];
550
551 dma_addr = (dma_addr_t)sram->sram_res.start;
552
553 len = resource_size(&sram->sram_res);
554 da = sram->da;
555
556 rproc_mem = rproc_mem_entry_init(&rproc->dev, NULL,
557 dma_addr,
558 len, da,
559 zynqmp_r5_mem_region_map,
560 zynqmp_r5_mem_region_unmap,
561 sram->sram_res.name);
562 if (!rproc_mem) {
563 dev_err(&rproc->dev, "failed to add sram %s da=0x%x, size=0x%lx",
564 sram->sram_res.name, da, len);
565 return -ENOMEM;
566 }
567
568 rproc_add_carveout(rproc, rproc_mem);
569 rproc_coredump_add_segment(rproc, da, len);
570
571 dev_dbg(&rproc->dev, "sram carveout %s addr=%llx, da=0x%x, size=0x%lx",
572 sram->sram_res.name, dma_addr, da, len);
573 }
574
575 return 0;
576 }
577
578 /*
579 * tcm_mem_unmap()
580 * @rproc: single R5 core's corresponding rproc instance
581 * @mem: tcm mem entry to unmap
582 *
583 * Unmap TCM banks when powering down R5 core.
584 *
585 * return always 0
586 */
587 static int tcm_mem_unmap(struct rproc *rproc, struct rproc_mem_entry *mem)
588 {
589 iounmap((void __iomem *)mem->va);
590
591 return 0;
592 }
593
594 /*
595 * tcm_mem_map()
596 * @rproc: single R5 core's corresponding rproc instance
597 * @mem: tcm memory entry descriptor
598 *
599 * Given TCM bank entry, this func setup virtual address for TCM bank
600 * remoteproc carveout. It also takes care of va to da address translation
601 *
602 * return 0 on success, otherwise non-zero value on failure
603 */
604 static int tcm_mem_map(struct rproc *rproc,
605 struct rproc_mem_entry *mem)
606 {
607 void __iomem *va;
608
609 va = ioremap_wc(mem->dma, mem->len);
610 if (IS_ERR_OR_NULL(va))
611 return -ENOMEM;
612
613 /* Update memory entry va */
614 mem->va = (void *)va;
615
616 /* clear TCMs */
617 memset_io(va, 0, mem->len);
618
619 return 0;
620 }
621
622 /*
623 * add_tcm_banks()
624 * @rproc: single R5 core's corresponding rproc instance
625 *
626 * allocate and add remoteproc carveout for TCM memory
627 *
628 * return 0 on success, otherwise non-zero value on failure
629 */
630 static int add_tcm_banks(struct rproc *rproc)
631 {
632 struct rproc_mem_entry *rproc_mem;
633 struct zynqmp_r5_core *r5_core;
634 int i, num_banks, ret;
635 phys_addr_t bank_addr;
636 struct device *dev;
637 u32 pm_domain_id;
638 size_t bank_size;
639 char *bank_name;
640 u32 da;
641
642 r5_core = rproc->priv;
643 dev = r5_core->dev;
644 num_banks = r5_core->tcm_bank_count;
645
646 /*
647 * Power-on Each 64KB TCM,
648 * register its address space, map and unmap functions
649 * and add carveouts accordingly
650 */
651 for (i = 0; i < num_banks; i++) {
652 bank_addr = r5_core->tcm_banks[i]->addr;
653 da = r5_core->tcm_banks[i]->da;
654 bank_name = r5_core->tcm_banks[i]->bank_name;
655 bank_size = r5_core->tcm_banks[i]->size;
656 pm_domain_id = r5_core->tcm_banks[i]->pm_domain_id;
657
658 ret = zynqmp_pm_request_node(pm_domain_id,
659 ZYNQMP_PM_CAPABILITY_ACCESS, 0,
660 ZYNQMP_PM_REQUEST_ACK_BLOCKING);
661 if (ret < 0) {
662 dev_err(dev, "failed to turn on TCM 0x%x", pm_domain_id);
663 goto release_tcm;
664 }
665
666 dev_dbg(dev, "TCM carveout %s addr=%llx, da=0x%x, size=0x%lx",
667 bank_name, bank_addr, da, bank_size);
668
669 /*
670 * In DETACHED state firmware is already running so no need to
671 * request add TCM registers. However, request TCM PD node to let
672 * platform management firmware know that TCM is in use.
673 */
674 if (rproc->state == RPROC_DETACHED)
675 continue;
676
677 rproc_mem = rproc_mem_entry_init(dev, NULL, bank_addr,
678 bank_size, da,
679 tcm_mem_map, tcm_mem_unmap,
680 bank_name);
681 if (!rproc_mem) {
682 ret = -ENOMEM;
683 zynqmp_pm_release_node(pm_domain_id);
684 goto release_tcm;
685 }
686
687 rproc_add_carveout(rproc, rproc_mem);
688 rproc_coredump_add_segment(rproc, da, bank_size);
689 }
690
691 return 0;
692
693 release_tcm:
694 /* If failed, Turn off all TCM banks turned on before */
695 for (i--; i >= 0; i--) {
696 pm_domain_id = r5_core->tcm_banks[i]->pm_domain_id;
697 zynqmp_pm_release_node(pm_domain_id);
698 }
699 return ret;
700 }
701
702 /*
703 * zynqmp_r5_parse_fw()
704 * @rproc: single R5 core's corresponding rproc instance
705 * @fw: ptr to firmware to be loaded onto r5 core
706 *
707 * get resource table if available
708 *
709 * return 0 on success, otherwise non-zero value on failure
710 */
711 static int zynqmp_r5_parse_fw(struct rproc *rproc, const struct firmware *fw)
712 {
713 int ret;
714
715 ret = rproc_elf_load_rsc_table(rproc, fw);
716 if (ret == -EINVAL) {
717 /*
718 * resource table only required for IPC.
719 * if not present, this is not necessarily an error;
720 * for example, loading r5 hello world application
721 * so simply inform user and keep going.
722 */
723 dev_info(&rproc->dev, "no resource table found.\n");
724 ret = 0;
725 }
726 return ret;
727 }
728
729 /**
730 * zynqmp_r5_rproc_prepare() - prepare core to boot/attach
731 * adds carveouts for TCM bank and reserved memory regions
732 *
733 * @rproc: Device node of each rproc
734 *
735 * Return: 0 for success else < 0 error code
736 */
737 static int zynqmp_r5_rproc_prepare(struct rproc *rproc)
738 {
739 int ret;
740
741 ret = add_tcm_banks(rproc);
742 if (ret) {
743 dev_err(&rproc->dev, "failed to get TCM banks, err %d\n", ret);
744 return ret;
745 }
746
747 ret = add_mem_regions_carveout(rproc);
748 if (ret) {
749 dev_err(&rproc->dev, "failed to get reserve mem regions %d\n", ret);
750 return ret;
751 }
752
753 ret = add_sram_carveouts(rproc);
754 if (ret) {
755 dev_err(&rproc->dev, "failed to get sram carveout %d\n", ret);
756 return ret;
757 }
758
759 return 0;
760 }
761
762 /**
763 * zynqmp_r5_rproc_unprepare() - programming sequence after stop/detach.
764 * Turns off TCM banks using power-domain id
765 *
766 * @rproc: Device node of each rproc
767 *
768 * Return: always 0
769 */
770 static int zynqmp_r5_rproc_unprepare(struct rproc *rproc)
771 {
772 struct zynqmp_r5_core *r5_core;
773 u32 pm_domain_id;
774 int i;
775
776 r5_core = rproc->priv;
777
778 for (i = 0; i < r5_core->tcm_bank_count; i++) {
779 pm_domain_id = r5_core->tcm_banks[i]->pm_domain_id;
780 if (zynqmp_pm_release_node(pm_domain_id))
781 dev_warn(r5_core->dev,
782 "can't turn off TCM bank 0x%x", pm_domain_id);
783 }
784
785 return 0;
786 }
787
788 static struct resource_table *zynqmp_r5_get_loaded_rsc_table(struct rproc *rproc,
789 size_t *size)
790 {
791 struct zynqmp_r5_core *r5_core;
792
793 r5_core = rproc->priv;
794
795 *size = r5_core->rsc_tbl_size;
796
797 return (struct resource_table *)r5_core->rsc_tbl_va;
798 }
799
800 static int zynqmp_r5_get_rsc_table_va(struct zynqmp_r5_core *r5_core)
801 {
802 struct resource_table *rsc_tbl_addr;
803 struct device *dev = r5_core->dev;
804 struct rsc_tbl_data *rsc_data_va;
805 struct resource res_mem;
806 int ret;
807
808 /*
809 * It is expected from remote processor firmware to provide resource
810 * table address via struct rsc_tbl_data data structure.
811 * Start address of first entry under "memory-region" property list
812 * contains that data structure which holds resource table address, size
813 * and some magic number to validate correct resource table entry.
814 */
815 ret = of_reserved_mem_region_to_resource(r5_core->np, 0, &res_mem);
816 if (ret) {
817 dev_err(dev, "failed to get memory-region resource addr\n");
818 return -EINVAL;
819 }
820
821 rsc_data_va = (struct rsc_tbl_data *)ioremap_wc(res_mem.start,
822 sizeof(struct rsc_tbl_data));
823 if (!rsc_data_va) {
824 dev_err(dev, "failed to map resource table data address\n");
825 return -EIO;
826 }
827
828 /*
829 * If RSC_TBL_XLNX_MAGIC number and its complement isn't found then
830 * do not consider resource table address valid and don't attach
831 */
832 if (rsc_data_va->magic_num != RSC_TBL_XLNX_MAGIC ||
833 rsc_data_va->comp_magic_num != ~RSC_TBL_XLNX_MAGIC) {
834 dev_dbg(dev, "invalid magic number, won't attach\n");
835 return -EINVAL;
836 }
837
838 r5_core->rsc_tbl_va = ioremap_wc(rsc_data_va->rsc_tbl,
839 rsc_data_va->rsc_tbl_size);
840 if (!r5_core->rsc_tbl_va) {
841 dev_err(dev, "failed to get resource table va\n");
842 return -EINVAL;
843 }
844
845 rsc_tbl_addr = (struct resource_table *)r5_core->rsc_tbl_va;
846
847 /*
848 * As of now resource table version 1 is expected. Don't fail to attach
849 * but warn users about it.
850 */
851 if (rsc_tbl_addr->ver != 1)
852 dev_warn(dev, "unexpected resource table version %d\n",
853 rsc_tbl_addr->ver);
854
855 r5_core->rsc_tbl_size = rsc_data_va->rsc_tbl_size;
856
857 iounmap((void __iomem *)rsc_data_va);
858
859 return 0;
860 }
861
862 static int zynqmp_r5_attach(struct rproc *rproc)
863 {
864 dev_dbg(&rproc->dev, "rproc %d attached\n", rproc->index);
865
866 return 0;
867 }
868
869 static int zynqmp_r5_detach(struct rproc *rproc)
870 {
871 /*
872 * Generate last notification to remote after clearing virtio flag.
873 * Remote can avoid polling on virtio reset flag if kick is generated
874 * during detach by host and check virtio reset flag on kick interrupt.
875 */
876 zynqmp_r5_rproc_kick(rproc, 0);
877
878 return 0;
879 }
880
881 static const struct rproc_ops zynqmp_r5_rproc_ops = {
882 .prepare = zynqmp_r5_rproc_prepare,
883 .unprepare = zynqmp_r5_rproc_unprepare,
884 .start = zynqmp_r5_rproc_start,
885 .stop = zynqmp_r5_rproc_stop,
886 .load = rproc_elf_load_segments,
887 .parse_fw = zynqmp_r5_parse_fw,
888 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
889 .sanity_check = rproc_elf_sanity_check,
890 .get_boot_addr = rproc_elf_get_boot_addr,
891 .kick = zynqmp_r5_rproc_kick,
892 .get_loaded_rsc_table = zynqmp_r5_get_loaded_rsc_table,
893 .attach = zynqmp_r5_attach,
894 .detach = zynqmp_r5_detach,
895 };
896
897 /**
898 * zynqmp_r5_add_rproc_core() - Add core data to framework.
899 * Allocate and add struct rproc object for each r5f core
900 * This is called for each individual r5f core
901 *
902 * @cdev: Device node of each r5 core
903 *
904 * Return: zynqmp_r5_core object for success else error code pointer
905 */
906 static struct zynqmp_r5_core *zynqmp_r5_add_rproc_core(struct device *cdev)
907 {
908 struct zynqmp_r5_core *r5_core;
909 struct rproc *r5_rproc;
910 int ret;
911
912 /* Set up DMA mask */
913 ret = dma_set_coherent_mask(cdev, DMA_BIT_MASK(32));
914 if (ret)
915 return ERR_PTR(ret);
916
917 /* Allocate remoteproc instance */
918 r5_rproc = rproc_alloc(cdev, dev_name(cdev),
919 &zynqmp_r5_rproc_ops,
920 NULL, sizeof(struct zynqmp_r5_core));
921 if (!r5_rproc) {
922 dev_err(cdev, "failed to allocate memory for rproc instance\n");
923 return ERR_PTR(-ENOMEM);
924 }
925
926 rproc_coredump_set_elf_info(r5_rproc, ELFCLASS32, EM_ARM);
927
928 r5_rproc->recovery_disabled = true;
929 r5_rproc->has_iommu = false;
930 r5_rproc->auto_boot = false;
931 r5_core = r5_rproc->priv;
932 r5_core->dev = cdev;
933 r5_core->np = dev_of_node(cdev);
934 if (!r5_core->np) {
935 dev_err(cdev, "can't get device node for r5 core\n");
936 ret = -EINVAL;
937 goto free_rproc;
938 }
939
940 /* Add R5 remoteproc core */
941 ret = rproc_add(r5_rproc);
942 if (ret) {
943 dev_err(cdev, "failed to add r5 remoteproc\n");
944 goto free_rproc;
945 }
946
947 /*
948 * If firmware is already available in the memory then move rproc state
949 * to DETACHED. Firmware can be preloaded via debugger or by any other
950 * agent (processors) in the system.
951 * If firmware isn't available in the memory and resource table isn't
952 * found, then rproc state remains OFFLINE.
953 */
954 if (!zynqmp_r5_get_rsc_table_va(r5_core))
955 r5_rproc->state = RPROC_DETACHED;
956
957 r5_core->rproc = r5_rproc;
958 return r5_core;
959
960 free_rproc:
961 rproc_free(r5_rproc);
962 return ERR_PTR(ret);
963 }
964
965 static int zynqmp_r5_get_sram_banks(struct zynqmp_r5_core *r5_core)
966 {
967 struct device_node *np = r5_core->np;
968 struct device *dev = r5_core->dev;
969 struct zynqmp_sram_bank *sram;
970 struct device_node *sram_np;
971 int num_sram, i, ret;
972 u64 abs_addr, size;
973
974 /* "sram" is optional property. Do not fail, if unavailable. */
975 if (!of_property_present(r5_core->np, "sram"))
976 return 0;
977
978 num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
979 if (num_sram <= 0) {
980 dev_err(dev, "Invalid sram property, ret = %d\n",
981 num_sram);
982 return -EINVAL;
983 }
984
985 sram = devm_kcalloc(dev, num_sram,
986 sizeof(struct zynqmp_sram_bank), GFP_KERNEL);
987 if (!sram)
988 return -ENOMEM;
989
990 for (i = 0; i < num_sram; i++) {
991 sram_np = of_parse_phandle(np, "sram", i);
992 if (!sram_np) {
993 dev_err(dev, "failed to get sram %d phandle\n", i);
994 return -EINVAL;
995 }
996
997 if (!of_device_is_available(sram_np)) {
998 dev_err(dev, "sram device not available\n");
999 ret = -EINVAL;
1000 goto fail_sram_get;
1001 }
1002
1003 ret = of_address_to_resource(sram_np, 0, &sram[i].sram_res);
1004 if (ret) {
1005 dev_err(dev, "addr to res failed\n");
1006 goto fail_sram_get;
1007 }
1008
1009 /* Get SRAM device address */
1010 ret = of_property_read_reg(sram_np, 0, &abs_addr, &size);
1011 if (ret) {
1012 dev_err(dev, "failed to get reg property\n");
1013 goto fail_sram_get;
1014 }
1015
1016 sram[i].da = (u32)abs_addr;
1017
1018 of_node_put(sram_np);
1019
1020 dev_dbg(dev, "sram %d: name=%s, addr=0x%llx, da=0x%x, size=0x%llx\n",
1021 i, sram[i].sram_res.name, sram[i].sram_res.start,
1022 sram[i].da, resource_size(&sram[i].sram_res));
1023 }
1024
1025 r5_core->sram = sram;
1026 r5_core->num_sram = num_sram;
1027
1028 return 0;
1029
1030 fail_sram_get:
1031 of_node_put(sram_np);
1032
1033 return ret;
1034 }
1035
1036 static int zynqmp_r5_get_tcm_node_from_dt(struct zynqmp_r5_cluster *cluster)
1037 {
1038 int i, j, tcm_bank_count, ret, tcm_pd_idx, pd_count;
1039 struct of_phandle_args out_args;
1040 struct zynqmp_r5_core *r5_core;
1041 struct platform_device *cpdev;
1042 struct mem_bank_data *tcm;
1043 struct device_node *np;
1044 struct resource *res;
1045 u64 abs_addr, size;
1046 struct device *dev;
1047
1048 for (i = 0; i < cluster->core_count; i++) {
1049 r5_core = cluster->r5_cores[i];
1050 dev = r5_core->dev;
1051 np = r5_core->np;
1052
1053 pd_count = of_count_phandle_with_args(np, "power-domains",
1054 "#power-domain-cells");
1055
1056 if (pd_count <= 0) {
1057 dev_err(dev, "invalid power-domains property, %d\n", pd_count);
1058 return -EINVAL;
1059 }
1060
1061 /* First entry in power-domains list is for r5 core, rest for TCM. */
1062 tcm_bank_count = pd_count - 1;
1063
1064 if (tcm_bank_count <= 0) {
1065 dev_err(dev, "invalid TCM count %d\n", tcm_bank_count);
1066 return -EINVAL;
1067 }
1068
1069 r5_core->tcm_banks = devm_kcalloc(dev, tcm_bank_count,
1070 sizeof(struct mem_bank_data *),
1071 GFP_KERNEL);
1072 if (!r5_core->tcm_banks)
1073 return -ENOMEM;
1074
1075 r5_core->tcm_bank_count = tcm_bank_count;
1076 for (j = 0, tcm_pd_idx = 1; j < tcm_bank_count; j++, tcm_pd_idx++) {
1077 tcm = devm_kzalloc(dev, sizeof(struct mem_bank_data),
1078 GFP_KERNEL);
1079 if (!tcm)
1080 return -ENOMEM;
1081
1082 r5_core->tcm_banks[j] = tcm;
1083
1084 /* Get power-domains id of TCM. */
1085 ret = of_parse_phandle_with_args(np, "power-domains",
1086 "#power-domain-cells",
1087 tcm_pd_idx, &out_args);
1088 if (ret) {
1089 dev_err(r5_core->dev,
1090 "failed to get tcm %d pm domain, ret %d\n",
1091 tcm_pd_idx, ret);
1092 return ret;
1093 }
1094 tcm->pm_domain_id = out_args.args[0];
1095 of_node_put(out_args.np);
1096
1097 /* Get TCM address without translation. */
1098 ret = of_property_read_reg(np, j, &abs_addr, &size);
1099 if (ret) {
1100 dev_err(dev, "failed to get reg property\n");
1101 return ret;
1102 }
1103
1104 /*
1105 * Remote processor can address only 32 bits
1106 * so convert 64-bits into 32-bits. This will discard
1107 * any unwanted upper 32-bits.
1108 */
1109 tcm->da = (u32)abs_addr;
1110 tcm->size = (u32)size;
1111
1112 cpdev = to_platform_device(dev);
1113 res = platform_get_resource(cpdev, IORESOURCE_MEM, j);
1114 if (!res) {
1115 dev_err(dev, "failed to get tcm resource\n");
1116 return -EINVAL;
1117 }
1118
1119 tcm->addr = (u32)res->start;
1120 tcm->bank_name = (char *)res->name;
1121 res = devm_request_mem_region(dev, tcm->addr, tcm->size,
1122 tcm->bank_name);
1123 if (!res) {
1124 dev_err(dev, "failed to request tcm resource\n");
1125 return -EINVAL;
1126 }
1127 }
1128 }
1129
1130 return 0;
1131 }
1132
1133 /**
1134 * zynqmp_r5_get_tcm_node() - Get TCM info
1135 * Ideally this function should parse tcm node and store information
1136 * in r5_core instance. For now, Hardcoded TCM information is used.
1137 * This approach is used as TCM bindings for system-dt is being developed
1138 *
1139 * @cluster: pointer to zynqmp_r5_cluster type object
1140 *
1141 * Return: 0 for success and < 0 error code for failure.
1142 */
1143 static int zynqmp_r5_get_tcm_node(struct zynqmp_r5_cluster *cluster)
1144 {
1145 const struct mem_bank_data *zynqmp_tcm_banks;
1146 struct device *dev = cluster->dev;
1147 struct zynqmp_r5_core *r5_core;
1148 int tcm_bank_count, tcm_node;
1149 int i, j;
1150
1151 if (cluster->mode == SPLIT_MODE) {
1152 zynqmp_tcm_banks = zynqmp_tcm_banks_split;
1153 tcm_bank_count = ARRAY_SIZE(zynqmp_tcm_banks_split);
1154 } else {
1155 zynqmp_tcm_banks = zynqmp_tcm_banks_lockstep;
1156 tcm_bank_count = ARRAY_SIZE(zynqmp_tcm_banks_lockstep);
1157 }
1158
1159 /* count per core tcm banks */
1160 tcm_bank_count = tcm_bank_count / cluster->core_count;
1161
1162 /*
1163 * r5 core 0 will use all of TCM banks in lockstep mode.
1164 * In split mode, r5 core0 will use 128k and r5 core1 will use another
1165 * 128k. Assign TCM banks to each core accordingly
1166 */
1167 tcm_node = 0;
1168 for (i = 0; i < cluster->core_count; i++) {
1169 r5_core = cluster->r5_cores[i];
1170 r5_core->tcm_banks = devm_kcalloc(dev, tcm_bank_count,
1171 sizeof(struct mem_bank_data *),
1172 GFP_KERNEL);
1173 if (!r5_core->tcm_banks)
1174 return -ENOMEM;
1175
1176 for (j = 0; j < tcm_bank_count; j++) {
1177 /*
1178 * Use pre-defined TCM reg values.
1179 * Eventually this should be replaced by values
1180 * parsed from dts.
1181 */
1182 r5_core->tcm_banks[j] =
1183 (struct mem_bank_data *)&zynqmp_tcm_banks[tcm_node];
1184 tcm_node++;
1185 }
1186
1187 r5_core->tcm_bank_count = tcm_bank_count;
1188 }
1189
1190 return 0;
1191 }
1192
1193 /*
1194 * zynqmp_r5_core_init()
1195 * Create and initialize zynqmp_r5_core type object
1196 *
1197 * @cluster: pointer to zynqmp_r5_cluster type object
1198 * @fw_reg_val: value expected by firmware to configure RPU cluster mode
1199 * @tcm_mode: value expected by fw to configure TCM mode (lockstep or split)
1200 *
1201 * Return: 0 for success and error code for failure.
1202 */
1203 static int zynqmp_r5_core_init(struct zynqmp_r5_cluster *cluster,
1204 enum rpu_oper_mode fw_reg_val,
1205 enum rpu_tcm_comb tcm_mode)
1206 {
1207 struct device *dev = cluster->dev;
1208 struct zynqmp_r5_core *r5_core;
1209 int ret = -EINVAL, i;
1210
1211 r5_core = cluster->r5_cores[0];
1212
1213 /* Maintain backward compatibility for zynqmp by using hardcode TCM address. */
1214 if (of_property_present(r5_core->np, "reg"))
1215 ret = zynqmp_r5_get_tcm_node_from_dt(cluster);
1216 else if (device_is_compatible(dev, "xlnx,zynqmp-r5fss"))
1217 ret = zynqmp_r5_get_tcm_node(cluster);
1218
1219 if (ret) {
1220 dev_err(dev, "can't get tcm, err %d\n", ret);
1221 return ret;
1222 }
1223
1224 for (i = 0; i < cluster->core_count; i++) {
1225 r5_core = cluster->r5_cores[i];
1226
1227 /* Initialize r5 cores with power-domains parsed from dts */
1228 ret = of_property_read_u32_index(r5_core->np, "power-domains",
1229 1, &r5_core->pm_domain_id);
1230 if (ret) {
1231 dev_err(dev, "failed to get power-domains property\n");
1232 return ret;
1233 }
1234
1235 ret = zynqmp_pm_set_rpu_mode(r5_core->pm_domain_id, fw_reg_val);
1236 if (ret < 0) {
1237 dev_err(r5_core->dev, "failed to set RPU mode\n");
1238 return ret;
1239 }
1240
1241 if (of_property_present(dev_of_node(dev), "xlnx,tcm-mode") ||
1242 device_is_compatible(dev, "xlnx,zynqmp-r5fss")) {
1243 ret = zynqmp_pm_set_tcm_config(r5_core->pm_domain_id,
1244 tcm_mode);
1245 if (ret < 0) {
1246 dev_err(r5_core->dev, "failed to configure TCM\n");
1247 return ret;
1248 }
1249 }
1250
1251 ret = zynqmp_r5_get_sram_banks(r5_core);
1252 if (ret)
1253 return ret;
1254 }
1255
1256 return 0;
1257 }
1258
1259 /*
1260 * zynqmp_r5_cluster_init()
1261 * Create and initialize zynqmp_r5_cluster type object
1262 *
1263 * @cluster: pointer to zynqmp_r5_cluster type object
1264 *
1265 * Return: 0 for success and error code for failure.
1266 */
1267 static int zynqmp_r5_cluster_init(struct zynqmp_r5_cluster *cluster)
1268 {
1269 enum zynqmp_r5_cluster_mode cluster_mode = LOCKSTEP_MODE;
1270 struct device *dev = cluster->dev;
1271 struct device_node *dev_node = dev_of_node(dev);
1272 struct platform_device *child_pdev;
1273 struct zynqmp_r5_core **r5_cores;
1274 enum rpu_oper_mode fw_reg_val;
1275 struct device **child_devs;
1276 enum rpu_tcm_comb tcm_mode;
1277 int core_count, ret, i;
1278 struct mbox_info *ipi;
1279
1280 ret = of_property_read_u32(dev_node, "xlnx,cluster-mode", &cluster_mode);
1281
1282 /*
1283 * on success returns 0, if not defined then returns -EINVAL,
1284 * In that case, default is LOCKSTEP mode. Other than that
1285 * returns relative error code < 0.
1286 */
1287 if (ret != -EINVAL && ret != 0) {
1288 dev_err(dev, "Invalid xlnx,cluster-mode property\n");
1289 return ret;
1290 }
1291
1292 /*
1293 * For now driver only supports split mode and lockstep mode.
1294 * fail driver probe if either of that is not set in dts.
1295 */
1296 if (cluster_mode == LOCKSTEP_MODE) {
1297 fw_reg_val = PM_RPU_MODE_LOCKSTEP;
1298 } else if (cluster_mode == SPLIT_MODE) {
1299 fw_reg_val = PM_RPU_MODE_SPLIT;
1300 } else {
1301 dev_err(dev, "driver does not support cluster mode %d\n", cluster_mode);
1302 return -EINVAL;
1303 }
1304
1305 if (of_property_present(dev_node, "xlnx,tcm-mode")) {
1306 ret = of_property_read_u32(dev_node, "xlnx,tcm-mode", (u32 *)&tcm_mode);
1307 if (ret)
1308 return ret;
1309 } else if (device_is_compatible(dev, "xlnx,zynqmp-r5fss")) {
1310 if (cluster_mode == LOCKSTEP_MODE)
1311 tcm_mode = PM_RPU_TCM_COMB;
1312 else
1313 tcm_mode = PM_RPU_TCM_SPLIT;
1314 } else {
1315 tcm_mode = PM_RPU_TCM_COMB;
1316 }
1317
1318 /*
1319 * Number of cores is decided by number of child nodes of
1320 * r5f subsystem node in dts.
1321 * In split mode maximum two child nodes are expected.
1322 * However, only single core can be enabled too.
1323 * Driver can handle following configuration in split mode:
1324 * 1) core0 enabled, core1 disabled
1325 * 2) core0 disabled, core1 enabled
1326 * 3) core0 and core1 both are enabled.
1327 * For now, no more than two cores are expected per cluster
1328 * in split mode.
1329 * In lockstep mode if two child nodes are available,
1330 * only use first child node and consider it as core0
1331 * and ignore core1 dt node.
1332 */
1333 core_count = of_get_available_child_count(dev_node);
1334 if (core_count == 0 || core_count > 2) {
1335 dev_err(dev, "Invalid number of r5 cores %d", core_count);
1336 return -EINVAL;
1337 } else if (cluster_mode == LOCKSTEP_MODE && core_count == 2) {
1338 dev_warn(dev, "Only r5 core0 will be used\n");
1339 core_count = 1;
1340 }
1341
1342 child_devs = kzalloc_objs(struct device *, core_count);
1343 if (!child_devs)
1344 return -ENOMEM;
1345
1346 r5_cores = kzalloc_objs(struct zynqmp_r5_core *, core_count);
1347 if (!r5_cores) {
1348 kfree(child_devs);
1349 return -ENOMEM;
1350 }
1351
1352 i = 0;
1353 for_each_available_child_of_node_scoped(dev_node, child) {
1354 child_pdev = of_find_device_by_node(child);
1355 if (!child_pdev) {
1356 ret = -ENODEV;
1357 goto release_r5_cores;
1358 }
1359
1360 child_devs[i] = &child_pdev->dev;
1361
1362 /* create and add remoteproc instance of type struct rproc */
1363 r5_cores[i] = zynqmp_r5_add_rproc_core(&child_pdev->dev);
1364 if (IS_ERR(r5_cores[i])) {
1365 ret = PTR_ERR(r5_cores[i]);
1366 r5_cores[i] = NULL;
1367 goto release_r5_cores;
1368 }
1369
1370 /*
1371 * If mailbox nodes are disabled using "status" property then
1372 * setting up mailbox channels will fail.
1373 */
1374 ipi = zynqmp_r5_setup_mbox(&child_pdev->dev);
1375 if (ipi) {
1376 r5_cores[i]->ipi = ipi;
1377 ipi->r5_core = r5_cores[i];
1378 }
1379
1380 /*
1381 * If two child nodes are available in dts in lockstep mode,
1382 * then ignore second child node.
1383 */
1384 if (cluster_mode == LOCKSTEP_MODE)
1385 break;
1386
1387 i++;
1388 }
1389
1390 cluster->mode = cluster_mode;
1391 cluster->core_count = core_count;
1392 cluster->r5_cores = r5_cores;
1393
1394 ret = zynqmp_r5_core_init(cluster, fw_reg_val, tcm_mode);
1395 if (ret < 0) {
1396 dev_err(dev, "failed to init r5 core err %d\n", ret);
1397 cluster->core_count = 0;
1398 cluster->r5_cores = NULL;
1399
1400 /*
1401 * at this point rproc resources for each core are allocated.
1402 * adjust index to free resources in reverse order
1403 */
1404 i = core_count - 1;
1405 goto release_r5_cores;
1406 }
1407
1408 kfree(child_devs);
1409 return 0;
1410
1411 release_r5_cores:
1412 while (i >= 0) {
1413 put_device(child_devs[i]);
1414 if (r5_cores[i]) {
1415 zynqmp_r5_free_mbox(r5_cores[i]->ipi);
1416 of_reserved_mem_device_release(r5_cores[i]->dev);
1417 rproc_del(r5_cores[i]->rproc);
1418 rproc_free(r5_cores[i]->rproc);
1419 }
1420 i--;
1421 }
1422 kfree(r5_cores);
1423 kfree(child_devs);
1424 return ret;
1425 }
1426
1427 static void zynqmp_r5_cluster_exit(void *data)
1428 {
1429 struct platform_device *pdev = data;
1430 struct zynqmp_r5_cluster *cluster;
1431 struct zynqmp_r5_core *r5_core;
1432 int i;
1433
1434 cluster = platform_get_drvdata(pdev);
1435 if (!cluster)
1436 return;
1437
1438 for (i = 0; i < cluster->core_count; i++) {
1439 r5_core = cluster->r5_cores[i];
1440 zynqmp_r5_free_mbox(r5_core->ipi);
1441 iounmap(r5_core->rsc_tbl_va);
1442 of_reserved_mem_device_release(r5_core->dev);
1443 put_device(r5_core->dev);
1444 rproc_del(r5_core->rproc);
1445 rproc_free(r5_core->rproc);
1446 }
1447
1448 kfree(cluster->r5_cores);
1449 kfree(cluster);
1450 platform_set_drvdata(pdev, NULL);
1451 }
1452
1453 /*
1454 * zynqmp_r5_remoteproc_shutdown()
1455 * Follow shutdown sequence in case of kexec call.
1456 *
1457 * @pdev: domain platform device for cluster
1458 *
1459 * Return: None.
1460 */
1461 static void zynqmp_r5_remoteproc_shutdown(struct platform_device *pdev)
1462 {
1463 const char *rproc_state_str = NULL;
1464 struct zynqmp_r5_cluster *cluster;
1465 struct zynqmp_r5_core *r5_core;
1466 struct rproc *rproc;
1467 int i, ret = 0;
1468
1469 cluster = platform_get_drvdata(pdev);
1470
1471 for (i = 0; i < cluster->core_count; i++) {
1472 r5_core = cluster->r5_cores[i];
1473 rproc = r5_core->rproc;
1474
1475 if (rproc->state == RPROC_RUNNING) {
1476 ret = rproc_shutdown(rproc);
1477 rproc_state_str = "shutdown";
1478 } else if (rproc->state == RPROC_ATTACHED) {
1479 ret = rproc_detach(rproc);
1480 rproc_state_str = "detach";
1481 } else {
1482 ret = 0;
1483 }
1484
1485 if (ret) {
1486 dev_err(cluster->dev, "failed to %s rproc %d\n",
1487 rproc_state_str, rproc->index);
1488 }
1489 }
1490 }
1491
1492 /*
1493 * zynqmp_r5_remoteproc_probe()
1494 * parse device-tree, initialize hardware and allocate required resources
1495 * and remoteproc ops
1496 *
1497 * @pdev: domain platform device for R5 cluster
1498 *
1499 * Return: 0 for success and < 0 for failure.
1500 */
1501 static int zynqmp_r5_remoteproc_probe(struct platform_device *pdev)
1502 {
1503 struct zynqmp_r5_cluster *cluster;
1504 struct device *dev = &pdev->dev;
1505 int ret;
1506
1507 cluster = kzalloc_obj(*cluster);
1508 if (!cluster)
1509 return -ENOMEM;
1510
1511 cluster->dev = dev;
1512
1513 ret = devm_of_platform_populate(dev);
1514 if (ret) {
1515 dev_err_probe(dev, ret, "failed to populate platform dev\n");
1516 kfree(cluster);
1517 return ret;
1518 }
1519
1520 /* wire in so each core can be cleaned up at driver remove */
1521 platform_set_drvdata(pdev, cluster);
1522
1523 ret = zynqmp_r5_cluster_init(cluster);
1524 if (ret) {
1525 kfree(cluster);
1526 platform_set_drvdata(pdev, NULL);
1527 dev_err_probe(dev, ret, "Invalid r5f subsystem device tree\n");
1528 return ret;
1529 }
1530
1531 ret = devm_add_action_or_reset(dev, zynqmp_r5_cluster_exit, pdev);
1532 if (ret)
1533 return ret;
1534
1535 return 0;
1536 }
1537
1538 /* Match table for OF platform binding */
1539 static const struct of_device_id zynqmp_r5_remoteproc_match[] = {
1540 { .compatible = "xlnx,versal-net-r52fss", },
1541 { .compatible = "xlnx,versal-r5fss", },
1542 { .compatible = "xlnx,zynqmp-r5fss", },
1543 { /* end of list */ },
1544 };
1545 MODULE_DEVICE_TABLE(of, zynqmp_r5_remoteproc_match);
1546
1547 static struct platform_driver zynqmp_r5_remoteproc_driver = {
1548 .probe = zynqmp_r5_remoteproc_probe,
1549 .driver = {
1550 .name = "zynqmp_r5_remoteproc",
1551 .of_match_table = zynqmp_r5_remoteproc_match,
1552 },
1553 .shutdown = zynqmp_r5_remoteproc_shutdown,
1554 };
1555 module_platform_driver(zynqmp_r5_remoteproc_driver);
1556
1557 MODULE_DESCRIPTION("Xilinx R5F remote processor driver");
1558 MODULE_AUTHOR("Xilinx Inc.");
1559 MODULE_LICENSE("GPL");