linux/drivers/remoteproc/zynqmp_r5_remoteproc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Zynq R5 Remote Processor driver
   4 *
   5 * Based on origin OMAP and Zynq Remote Processor driver
   6 *
   7 */
   8
   9#include <linux/firmware/xlnx-zynqmp.h>
  10#include <linux/interrupt.h>
  11#include <linux/kernel.h>
  12#include <linux/list.h>
  13#include <linux/mailbox_client.h>
  14#include <linux/mailbox/zynqmp-ipi-message.h>
  15#include <linux/module.h>
  16#include <linux/of_address.h>
  17#include <linux/of_platform.h>
  18#include <linux/of_reserved_mem.h>
  19#include <linux/platform_device.h>
  20#include <linux/remoteproc.h>
  21#include <linux/skbuff.h>
  22#include <linux/sysfs.h>
  23
  24#include "remoteproc_internal.h"
  25
  26#define MAX_RPROCS      2 /* Support up to 2 RPU */
  27#define BANK_LIST_PROP  "sram"
  28#define DDR_LIST_PROP   "memory-region"
  29
  30/* IPI buffer MAX length */
  31#define IPI_BUF_LEN_MAX 32U
  32/* RX mailbox client buffer max length */
  33#define RX_MBOX_CLIENT_BUF_MAX  (IPI_BUF_LEN_MAX + \
  34                                 sizeof(struct zynqmp_ipi_message))
  35
  36/*
  37 * Map each Xilinx on-chip SRAM  Bank address to their own respective
  38 * pm_node_id.
  39 */
  40struct sram_addr_data {
  41        phys_addr_t addr;
  42        enum pm_node_id id;
  43};
  44
  45#define NUM_SRAMS 8U
  46static const struct sram_addr_data zynqmp_banks[NUM_SRAMS] = {
  47        {0xfffc0000UL, NODE_OCM_BANK_0},
  48        {0xfffd0000UL, NODE_OCM_BANK_1},
  49        {0xfffe0000UL, NODE_OCM_BANK_2},
  50        {0xffff0000UL, NODE_OCM_BANK_3},
  51        {0xffe00000UL, NODE_TCM_0_A},
  52        {0xffe20000UL, NODE_TCM_0_B},
  53        {0xffe90000UL, NODE_TCM_1_A},
  54        {0xffeb0000UL, NODE_TCM_1_B},
  55};
  56
  57#define VERSAL_TCM(ID)  ((ID) + 0x18317FFCU)
  58#define VERSAL_OCM(ID)  ((ID) + 0x18313FFCU)
  59#define VERSAL_RPU_0    (NODE_RPU_0 + 0x1810FFFEU)
  60#define VERSAL_RPU_1    (VERSAL_RPU_0 + 1U)
  61
  62/**
  63 * struct zynqmp_r5_rproc - ZynqMP R5 core structure
  64 *
  65 * @rx_mc_buf: rx mailbox client buffer to save the rx message
  66 * @tx_mc: tx mailbox client
  67 * @rx_mc: rx mailbox client
  68 * @mbox_work: mbox_work for the RPU remoteproc
  69 * @tx_mc_skbs: socket buffers for tx mailbox client
  70 * @dev: device of RPU instance
  71 * @rproc: rproc handle
  72 * @tx_chan: tx mailbox channel
  73 * @rx_chan: rx mailbox channel
  74 * @pnode_id: RPU CPU power domain id
  75 * @elem: linked list item
  76 * @versal: flag that if on, denotes this driver is for Versal SoC.
  77 */
  78struct zynqmp_r5_rproc {
  79        unsigned char rx_mc_buf[RX_MBOX_CLIENT_BUF_MAX];
  80        struct mbox_client tx_mc;
  81        struct mbox_client rx_mc;
  82        struct work_struct mbox_work;
  83        struct sk_buff_head tx_mc_skbs;
  84        struct device *dev;
  85        struct rproc *rproc;
  86        struct mbox_chan *tx_chan;
  87        struct mbox_chan *rx_chan;
  88        u32 pnode_id;
  89        struct list_head elem;
  90        bool versal;
  91};
  92
  93/*
  94 * r5_set_mode - set RPU operation mode
  95 * @z_rproc: Remote processor private data
  96 * @rpu_mode: mode specified by device tree to configure the RPU to
  97 *
  98 * set RPU operation mode
  99 *
 100 * Return: 0 for success, negative value for failure
 101 */
 102static int r5_set_mode(struct zynqmp_r5_rproc *z_rproc,
 103                       enum rpu_oper_mode rpu_mode)
 104{
 105        enum rpu_tcm_comb tcm_mode;
 106        enum rpu_oper_mode cur_rpu_mode;
 107        int ret;
 108
 109        ret = zynqmp_pm_get_rpu_mode(z_rproc->pnode_id, &cur_rpu_mode);
 110        if (ret < 0)
 111                return ret;
 112
 113        if (rpu_mode != cur_rpu_mode) {
 114                ret = zynqmp_pm_set_rpu_mode(z_rproc->pnode_id,
 115                                             rpu_mode);
 116                if (ret < 0)
 117                        return ret;
 118        }
 119
 120        tcm_mode = (rpu_mode == PM_RPU_MODE_LOCKSTEP) ?
 121                    PM_RPU_TCM_COMB : PM_RPU_TCM_SPLIT;
 122        return zynqmp_pm_set_tcm_config(z_rproc->pnode_id, tcm_mode);
 123}
 124
 125/*
 126 * zynqmp_r5_rproc_mem_release
 127 * @rproc: single R5 core's corresponding rproc instance
 128 * @mem: mem entry to unmap
 129 *
 130 * Unmap SRAM banks when powering down R5 core.
 131 *
 132 * return 0 on success, otherwise non-zero value on failure
 133 */
 134static int sram_mem_release(struct rproc *rproc, struct rproc_mem_entry *mem)
 135{
 136        u32 pnode_id = (u64)mem->priv;
 137
 138        iounmap(mem->va);
 139        return zynqmp_pm_release_node(pnode_id);
 140}
 141
 142/*
 143 * zynqmp_r5_rproc_start
 144 * @rproc: single R5 core's corresponding rproc instance
 145 *
 146 * Start R5 Core from designated boot address.
 147 *
 148 * return 0 on success, otherwise non-zero value on failure
 149 */
 150static int zynqmp_r5_rproc_start(struct rproc *rproc)
 151{
 152        struct zynqmp_r5_rproc *z_rproc = rproc->priv;
 153        enum rpu_boot_mem bootmem;
 154
 155        bootmem = (rproc->bootaddr & 0xF0000000) == 0xF0000000 ?
 156                  PM_RPU_BOOTMEM_HIVEC : PM_RPU_BOOTMEM_LOVEC;
 157
 158        dev_dbg(rproc->dev.parent, "RPU boot from %s.",
 159                bootmem == PM_RPU_BOOTMEM_HIVEC ? "OCM" : "TCM");
 160
 161        return zynqmp_pm_request_wake(z_rproc->pnode_id, 1,
 162                                     bootmem, ZYNQMP_PM_REQUEST_ACK_NO);
 163}
 164
 165/*
 166 * zynqmp_r5_rproc_stop
 167 * @rproc: single R5 core's corresponding rproc instance
 168 *
 169 * Power down  R5 Core.
 170 *
 171 * return 0 on success, otherwise non-zero value on failure
 172 */
 173static int zynqmp_r5_rproc_stop(struct rproc *rproc)
 174{
 175        struct zynqmp_r5_rproc *z_rproc = rproc->priv;
 176
 177        return zynqmp_pm_force_pwrdwn(z_rproc->pnode_id,
 178                                     ZYNQMP_PM_REQUEST_ACK_BLOCKING);
 179}
 180
 181/*
 182 * zynqmp_r5_rproc_mem_alloc
 183 * @rproc: single R5 core's corresponding rproc instance
 184 * @mem: mem entry to map
 185 *
 186 * Callback to map va for memory-region's carveout.
 187 *
 188 * return 0 on success, otherwise non-zero value on failure
 189 */
 190static int zynqmp_r5_rproc_mem_alloc(struct rproc *rproc,
 191                                     struct rproc_mem_entry *mem)
 192{
 193        void *va;
 194
 195        va = ioremap_wc(mem->dma, mem->len);
 196        if (IS_ERR_OR_NULL(va))
 197                return -ENOMEM;
 198
 199        mem->va = va;
 200
 201        return 0;
 202}
 203
 204/*
 205 * zynqmp_r5_rproc_mem_release
 206 * @rproc: single R5 core's corresponding rproc instance
 207 * @mem: mem entry to unmap
 208 *
 209 * Unmap memory-region carveout
 210 *
 211 * return 0 on success, otherwise non-zero value on failure
 212 */
 213static int zynqmp_r5_rproc_mem_release(struct rproc *rproc,
 214                                       struct rproc_mem_entry *mem)
 215{
 216        iounmap(mem->va);
 217        return 0;
 218}
 219
 220/*
 221 * parse_mem_regions
 222 * @rproc: single R5 core's corresponding rproc instance
 223 *
 224 * Construct rproc mem carveouts from carveout provided in
 225 * memory-region property
 226 *
 227 * return 0 on success, otherwise non-zero value on failure
 228 */
 229static int parse_mem_regions(struct rproc *rproc)
 230{
 231        int num_mems, i;
 232        struct zynqmp_r5_rproc *z_rproc = rproc->priv;
 233        struct device *dev = &rproc->dev;
 234        struct device_node *np = z_rproc->dev->of_node;
 235        struct rproc_mem_entry *mem;
 236
 237        num_mems = of_count_phandle_with_args(np, DDR_LIST_PROP, NULL);
 238        if (num_mems <= 0)
 239                return 0;
 240
 241        for (i = 0; i < num_mems; i++) {
 242                struct device_node *node;
 243                struct reserved_mem *rmem;
 244
 245                node = of_parse_phandle(np, DDR_LIST_PROP, i);
 246                if (!node)
 247                        return -EINVAL;
 248
 249                rmem = of_reserved_mem_lookup(node);
 250                if (!rmem)
 251                        return -EINVAL;
 252
 253                if (strstr(node->name, "vdev0vring")) {
 254                        int vring_id;
 255                        char name[16];
 256
 257                        /*
 258                         * expecting form of "rpuXvdev0vringX as documented
 259                         * in xilinx remoteproc device tree binding
 260                         */
 261                        if (strlen(node->name) < 15) {
 262                                dev_err(dev, "%pOF is less than 14 chars",
 263                                        node);
 264                                return -EINVAL;
 265                        }
 266
 267                        /*
 268                         * can be 1 of multiple vring IDs per IPC channel
 269                         * e.g. 'vdev0vring0' and 'vdev0vring1'
 270                         */
 271                        vring_id = node->name[14] - '0';
 272                        snprintf(name, sizeof(name), "vdev0vring%d", vring_id);
 273                        /* Register vring */
 274                        mem = rproc_mem_entry_init(dev, NULL,
 275                                                   (dma_addr_t)rmem->base,
 276                                                   rmem->size, rmem->base,
 277                                                   zynqmp_r5_rproc_mem_alloc,
 278                                                   zynqmp_r5_rproc_mem_release,
 279                                                   name);
 280                } else {
 281                        /* Register DMA region */
 282                        int (*alloc)(struct rproc *r,
 283                                     struct rproc_mem_entry *rme);
 284                        int (*release)(struct rproc *r,
 285                                       struct rproc_mem_entry *rme);
 286                        char name[20];
 287
 288                        if (strstr(node->name, "vdev0buffer")) {
 289                                alloc = NULL;
 290                                release = NULL;
 291                                strcpy(name, "vdev0buffer");
 292                        } else {
 293                                alloc = zynqmp_r5_rproc_mem_alloc;
 294                                release = zynqmp_r5_rproc_mem_release;
 295                                strcpy(name, node->name);
 296                        }
 297
 298                        mem = rproc_mem_entry_init(dev, NULL,
 299                                                   (dma_addr_t)rmem->base,
 300                                                   rmem->size, rmem->base,
 301                                                   alloc, release, name);
 302                }
 303                if (!mem)
 304                        return -ENOMEM;
 305
 306                rproc_add_carveout(rproc, mem);
 307        }
 308
 309        return 0;
 310}
 311
 312/*
 313 * zynqmp_r5_pm_request_tcm
 314 * @addr: base address of mem provided in R5 core's sram property.
 315 * @versal: denote whether to use Versal or ZU+ platform IDs
 316 * @pnode_id: store platform ID here for later use
 317 *
 318 * Given sram base address, determine its corresponding Xilinx
 319 * Platform Management ID and then request access to this node
 320 * so that it can be power up.
 321 *
 322 * return 0 on success, otherwise non-zero value on failure
 323 */
 324static int zynqmp_r5_pm_request_sram(phys_addr_t addr, bool versal,
 325                                     u32 *pnode_id)
 326{
 327        unsigned int i;
 328
 329        for (i = 0; i < NUM_SRAMS; i++) {
 330                if (zynqmp_banks[i].addr == addr) {
 331                        *pnode_id = zynqmp_banks[i].id;
 332
 333                        if (versal) {
 334                                switch (addr) {
 335                                case 0xffe00000UL:
 336                                case 0xffe20000UL:
 337                                case 0xffe90000UL:
 338                                case 0xffeb0000UL:
 339                                        *pnode_id = VERSAL_TCM(zynqmp_banks[i].id);
 340                                        break;
 341                                case 0xfffc0000UL:
 342                                case 0xfffd0000UL:
 343                                case 0xfffe0000UL:
 344                                case 0xffff0000UL:
 345                                        *pnode_id = VERSAL_OCM(zynqmp_banks[i].id);
 346                                        break;
 347                                default:
 348                                        return -EINVAL;
 349                                }
 350                        }
 351
 352                        return zynqmp_pm_request_node(*pnode_id,
 353                                                      ZYNQMP_PM_CAPABILITY_ACCESS,
 354                                                      0,
 355                                                      ZYNQMP_PM_REQUEST_ACK_BLOCKING);
 356                }
 357        }
 358
 359        return -EINVAL;
 360}
 361
 362/*
 363 * sram_mem_alloc
 364 * @rproc: single R5 core's corresponding rproc instance
 365 * @mem: mem entry to initialize the va and da fields of
 366 *
 367 * Given SRAM bank entry,
 368 * this callback will set device address for R5 running on TCM
 369 * and also setup virtual address for TCM bank remoteproc carveout
 370 *
 371 * return 0 on success, otherwise non-zero value on failure
 372 */
 373static int sram_mem_alloc(struct rproc *rproc, struct rproc_mem_entry *mem)
 374{
 375        void *va;
 376        struct device *dev = rproc->dev.parent;
 377
 378        va = ioremap_wc(mem->dma, mem->len);
 379        if (IS_ERR_OR_NULL(va))
 380                return -ENOMEM;
 381
 382        /* Update memory entry va */
 383        mem->va = va;
 384
 385        va = devm_ioremap_wc(dev, mem->da, mem->len);
 386        if (!va)
 387                return -ENOMEM;
 388        /* Handle TCM translation for R5-relative addresses */
 389        if (mem->da >= 0xffe00000UL && mem->da <= 0xffeb0000UL) {
 390                /* As R5 is 32 bit, wipe out extra high bits */
 391                mem->da &= 0x000fffff;
 392                /*
 393                 * The R5s expect their TCM banks to be at address 0x0 and 0x2000,
 394                 * while on the Linux side they are at 0xffexxxxx. Zero out the high
 395                 * 12 bits of the address.
 396                 */
 397
 398                /*
 399                 * TCM Banks 1A and 1B (0xffe90000 and 0xffeb0000) still
 400                 * need to be translated to 0x0 and 0x20000
 401                 */
 402                if (mem->da == 0x90000 || mem->da == 0xB0000)
 403                        mem->da -= 0x90000;
 404
 405                /* if translated TCM bank address is not valid report error */
 406                if (mem->da != 0x0 && mem->da != 0x20000) {
 407                        dev_err(dev, "invalid TCM bank address: %x\n", mem->da);
 408                        return -EINVAL;
 409                }
 410        }
 411
 412        return 0;
 413}
 414
 415/*
 416 * parse_tcm_banks()
 417 * @rproc: single R5 core's corresponding rproc instance
 418 *
 419 * Given R5 node in remoteproc instance
 420 * allocate remoteproc carveout for TCM memory
 421 * needed for firmware to be loaded
 422 *
 423 * return 0 on success, otherwise non-zero value on failure
 424 */
 425static int parse_tcm_banks(struct rproc *rproc)
 426{
 427        int i, num_banks;
 428        struct zynqmp_r5_rproc *z_rproc = rproc->priv;
 429        struct device *dev = &rproc->dev;
 430        struct device_node *r5_node = z_rproc->dev->of_node;
 431
 432        /* go through TCM banks for r5 node */
 433        num_banks = of_count_phandle_with_args(r5_node, BANK_LIST_PROP, NULL);
 434        if (num_banks <= 0) {
 435                dev_err(dev, "need to specify TCM banks\n");
 436                return -EINVAL;
 437        }
 438        for (i = 0; i < num_banks; i++) {
 439                struct resource rsc;
 440                resource_size_t size;
 441                struct device_node *dt_node;
 442                struct rproc_mem_entry *mem;
 443                int ret;
 444                u32 pnode_id; /* zynqmp_pm* fn's expect u32 */
 445
 446                dt_node = of_parse_phandle(r5_node, BANK_LIST_PROP, i);
 447                if (!dt_node)
 448                        return -EINVAL;
 449
 450                if (of_device_is_available(dt_node)) {
 451                        ret = of_address_to_resource(dt_node, 0, &rsc);
 452                        if (ret < 0)
 453                                return ret;
 454                        ret = zynqmp_r5_pm_request_sram(rsc.start,
 455                                                        z_rproc->versal,
 456                                                        &pnode_id);
 457                        if (ret < 0)
 458                                return ret;
 459
 460                        /* add carveout */
 461                        size = resource_size(&rsc);
 462                        mem = rproc_mem_entry_init(dev, NULL, rsc.start,
 463                                                   (int)size, rsc.start,
 464                                                   sram_mem_alloc,
 465                                                   sram_mem_release,
 466                                                   rsc.name);
 467                        if (!mem)
 468                                return -ENOMEM;
 469
 470                        mem->priv = (void *)(u64)pnode_id;
 471                        rproc_add_carveout(rproc, mem);
 472                }
 473        }
 474
 475        return 0;
 476}
 477
 478/*
 479 * zynqmp_r5_parse_fw()
 480 * @rproc: single R5 core's corresponding rproc instance
 481 * @fw: ptr to firmware to be loaded onto r5 core
 482 *
 483 * When loading firmware, ensure the necessary carveouts are in remoteproc
 484 *
 485 * return 0 on success, otherwise non-zero value on failure
 486 */
 487static int zynqmp_r5_parse_fw(struct rproc *rproc, const struct firmware *fw)
 488{
 489        int ret;
 490
 491        ret = parse_tcm_banks(rproc);
 492        if (ret)
 493                return ret;
 494
 495        ret = parse_mem_regions(rproc);
 496        if (ret)
 497                return ret;
 498
 499        ret = rproc_elf_load_rsc_table(rproc, fw);
 500        if (ret == -EINVAL) {
 501                /*
 502                 * resource table only required for IPC.
 503                 * if not present, this is not necessarily an error;
 504                 * for example, loading r5 hello world application
 505                 * so simply inform user and keep going.
 506                 */
 507                dev_info(&rproc->dev, "no resource table found.\n");
 508                ret = 0;
 509        }
 510        return ret;
 511}
 512
 513/*
 514 * zynqmp_r5_rproc_kick() - kick a firmware if mbox is provided
 515 * @rproc: r5 core's corresponding rproc structure
 516 * @vqid: virtqueue ID
 517 */
 518static void zynqmp_r5_rproc_kick(struct rproc *rproc, int vqid)
 519{
 520        struct sk_buff *skb = NULL;
 521        unsigned int skb_len = 0;
 522        struct zynqmp_ipi_message *mb_msg = NULL;
 523        int ret = 0;
 524
 525        struct device *dev = rproc->dev.parent;
 526        struct zynqmp_r5_rproc *z_rproc = rproc->priv;
 527
 528        if (of_property_read_bool(dev->of_node, "mboxes")) {
 529                skb_len = (unsigned int)(sizeof(vqid) + sizeof(mb_msg));
 530                skb = alloc_skb(skb_len, GFP_ATOMIC);
 531                if (!skb)
 532                        return;
 533
 534                mb_msg = (struct zynqmp_ipi_message *)skb_put(skb, skb_len);
 535                mb_msg->len = sizeof(vqid);
 536                memcpy(mb_msg->data, &vqid, sizeof(vqid));
 537
 538                skb_queue_tail(&z_rproc->tx_mc_skbs, skb);
 539                ret = mbox_send_message(z_rproc->tx_chan, mb_msg);
 540                if (ret < 0) {
 541                        dev_warn(dev, "Failed to kick remote.\n");
 542                        skb_dequeue_tail(&z_rproc->tx_mc_skbs);
 543                        kfree_skb(skb);
 544                }
 545        } else {
 546                (void)skb;
 547                (void)skb_len;
 548                (void)mb_msg;
 549                (void)ret;
 550                (void)vqid;
 551        }
 552}
 553
 554static struct rproc_ops zynqmp_r5_rproc_ops = {
 555        .start          = zynqmp_r5_rproc_start,
 556        .stop           = zynqmp_r5_rproc_stop,
 557        .load           = rproc_elf_load_segments,
 558        .parse_fw       = zynqmp_r5_parse_fw,
 559        .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
 560        .sanity_check   = rproc_elf_sanity_check,
 561        .get_boot_addr  = rproc_elf_get_boot_addr,
 562        .kick           = zynqmp_r5_rproc_kick,
 563};
 564
 565/**
 566 * event_notified_idr_cb() - event notified idr callback
 567 * @id: idr id
 568 * @ptr: pointer to idr private data
 569 * @data: data passed to idr_for_each callback
 570 *
 571 * Pass notification to remoteproc virtio
 572 *
 573 * Return: 0. having return is to satisfy the idr_for_each() function
 574 *          pointer input argument requirement.
 575 **/
 576static int event_notified_idr_cb(int id, void *ptr, void *data)
 577{
 578        struct rproc *rproc = data;
 579
 580        (void)rproc_vq_interrupt(rproc, id);
 581        return 0;
 582}
 583
 584/**
 585 * handle_event_notified() - remoteproc notification work function
 586 * @work: pointer to the work structure
 587 *
 588 * It checks each registered remoteproc notify IDs.
 589 */
 590static void handle_event_notified(struct work_struct *work)
 591{
 592        struct rproc *rproc;
 593        struct zynqmp_r5_rproc *z_rproc;
 594
 595        z_rproc = container_of(work, struct zynqmp_r5_rproc, mbox_work);
 596
 597        (void)mbox_send_message(z_rproc->rx_chan, NULL);
 598        rproc = z_rproc->rproc;
 599        /*
 600         * We only use IPI for interrupt. The firmware side may or may
 601         * not write the notifyid when it trigger IPI.
 602         * And thus, we scan through all the registered notifyids.
 603         */
 604        idr_for_each(&rproc->notifyids, event_notified_idr_cb, rproc);
 605}
 606
 607/**
 608 * zynqmp_r5_mb_rx_cb() - Receive channel mailbox callback
 609 * @cl: mailbox client
 610 * @msg: message pointer
 611 *
 612 * It will schedule the R5 notification work.
 613 */
 614static void zynqmp_r5_mb_rx_cb(struct mbox_client *cl, void *msg)
 615{
 616        struct zynqmp_r5_rproc *z_rproc;
 617
 618        z_rproc = container_of(cl, struct zynqmp_r5_rproc, rx_mc);
 619        if (msg) {
 620                struct zynqmp_ipi_message *ipi_msg, *buf_msg;
 621                size_t len;
 622
 623                ipi_msg = (struct zynqmp_ipi_message *)msg;
 624                buf_msg = (struct zynqmp_ipi_message *)z_rproc->rx_mc_buf;
 625                len = (ipi_msg->len >= IPI_BUF_LEN_MAX) ?
 626                      IPI_BUF_LEN_MAX : ipi_msg->len;
 627                buf_msg->len = len;
 628                memcpy(buf_msg->data, ipi_msg->data, len);
 629        }
 630        schedule_work(&z_rproc->mbox_work);
 631}
 632
 633/**
 634 * zynqmp_r5_mb_tx_done() - Request has been sent to the remote
 635 * @cl: mailbox client
 636 * @msg: pointer to the message which has been sent
 637 * @r: status of last TX - OK or error
 638 *
 639 * It will be called by the mailbox framework when the last TX has done.
 640 */
 641static void zynqmp_r5_mb_tx_done(struct mbox_client *cl, void *msg, int r)
 642{
 643        struct zynqmp_r5_rproc *z_rproc;
 644        struct sk_buff *skb;
 645
 646        if (!msg)
 647                return;
 648        z_rproc = container_of(cl, struct zynqmp_r5_rproc, tx_mc);
 649        skb = skb_dequeue(&z_rproc->tx_mc_skbs);
 650        kfree_skb(skb);
 651}
 652
 653/**
 654 * zynqmp_r5_setup_mbox() - Setup mailboxes
 655 *                          this is used for each individual R5 core
 656 *
 657 * @z_rproc: pointer to the ZynqMP R5 processor platform data
 658 * @node: pointer of the device node
 659 *
 660 * Function to setup mailboxes to talk to RPU.
 661 *
 662 * Return: 0 for success, negative value for failure.
 663 */
 664static int zynqmp_r5_setup_mbox(struct zynqmp_r5_rproc *z_rproc,
 665                                struct device_node *node)
 666{
 667        struct mbox_client *mclient;
 668
 669        /* Setup TX mailbox channel client */
 670        mclient = &z_rproc->tx_mc;
 671        mclient->rx_callback = NULL;
 672        mclient->tx_block = false;
 673        mclient->knows_txdone = false;
 674        mclient->tx_done = zynqmp_r5_mb_tx_done;
 675        mclient->dev = z_rproc->dev;
 676
 677        /* Setup TX mailbox channel client */
 678        mclient = &z_rproc->rx_mc;
 679        mclient->dev = z_rproc->dev;
 680        mclient->rx_callback = zynqmp_r5_mb_rx_cb;
 681        mclient->tx_block = false;
 682        mclient->knows_txdone = false;
 683
 684        INIT_WORK(&z_rproc->mbox_work, handle_event_notified);
 685
 686        /* Request TX and RX channels */
 687        z_rproc->tx_chan = mbox_request_channel_byname(&z_rproc->tx_mc, "tx");
 688        if (IS_ERR(z_rproc->tx_chan)) {
 689                dev_err(z_rproc->dev, "failed to request mbox tx channel.\n");
 690                z_rproc->tx_chan = NULL;
 691                return -EINVAL;
 692        }
 693
 694        z_rproc->rx_chan = mbox_request_channel_byname(&z_rproc->rx_mc, "rx");
 695        if (IS_ERR(z_rproc->rx_chan)) {
 696                dev_err(z_rproc->dev, "failed to request mbox rx channel.\n");
 697                z_rproc->rx_chan = NULL;
 698                return -EINVAL;
 699        }
 700        skb_queue_head_init(&z_rproc->tx_mc_skbs);
 701
 702        return 0;
 703}
 704
 705/**
 706 * zynqmp_r5_probe() - Probes ZynqMP R5 processor device node
 707 *                     this is called for each individual R5 core to
 708 *                     set up mailbox, Xilinx platform manager unique ID,
 709 *                     add to rproc core
 710 *
 711 * @pdev: domain platform device for current R5 core
 712 * @node: pointer of the device node for current R5 core
 713 * @rpu_mode: mode to configure RPU, split or lockstep
 714 * @z_rproc: Xilinx specific remoteproc structure used later to link
 715 *           in to cluster of cores
 716 *
 717 * Return: 0 for success, negative value for failure.
 718 */
 719static int zynqmp_r5_probe(struct platform_device *pdev,
 720                           struct device_node *node,
 721                           enum rpu_oper_mode rpu_mode,
 722                           struct zynqmp_r5_rproc **z_rproc)
 723{
 724        int ret;
 725        struct device *dev = &pdev->dev;
 726        struct rproc *rproc_ptr;
 727
 728        /* Allocate remoteproc instance */
 729        rproc_ptr = devm_rproc_alloc(dev, dev_name(dev), &zynqmp_r5_rproc_ops,
 730                                     NULL, sizeof(struct zynqmp_r5_rproc));
 731        if (!rproc_ptr) {
 732                ret = -ENOMEM;
 733                goto error;
 734        }
 735
 736        rproc_ptr->auto_boot = false;
 737        *z_rproc = rproc_ptr->priv;
 738        (*z_rproc)->rproc = rproc_ptr;
 739        (*z_rproc)->dev = dev;
 740        /* Set up DMA mask */
 741        ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
 742        if (ret)
 743                goto error;
 744
 745        /* Get R5 power domain node */
 746        ret = of_property_read_u32(node, "power-domain", &(*z_rproc)->pnode_id);
 747        if (ret)
 748                goto error;
 749
 750        if ((VERSAL_RPU_0 == (*z_rproc)->pnode_id) ||
 751            (VERSAL_RPU_1 == (*z_rproc)->pnode_id))
 752                (*z_rproc)->versal = true;
 753
 754        ret = r5_set_mode(*z_rproc, rpu_mode);
 755        if (ret)
 756                goto error;
 757
 758        if (of_property_read_bool(node, "mboxes")) {
 759                ret = zynqmp_r5_setup_mbox(*z_rproc, node);
 760                if (ret)
 761                        goto error;
 762        }
 763
 764        /* Add R5 remoteproc */
 765        ret = devm_rproc_add(dev, rproc_ptr);
 766        if (ret)
 767                goto error;
 768
 769        /*
 770         * In Versal SoC, the Xilinx platform management firmware will power
 771         * off the R5 cores if they are not requested. In this case, this call
 772         * notifies Xilinx platform management firmware that the R5 core will
 773         * be used and should be powered on.
 774         *
 775         * On ZynqMP platform this is not needed as the R5 cores are not
 776         * powered off by default.
 777         */
 778        if ((*z_rproc)->versal) {
 779                ret = zynqmp_pm_request_node((*z_rproc)->pnode_id,
 780                                             ZYNQMP_PM_CAPABILITY_ACCESS, 0,
 781                                             ZYNQMP_PM_REQUEST_ACK_BLOCKING);
 782                if (ret < 0)
 783                        goto error;
 784        }
 785
 786        return 0;
 787error:
 788        *z_rproc = NULL;
 789        return ret;
 790}
 791
 792/*
 793 * zynqmp_r5_remoteproc_probe()
 794 *
 795 * @pdev: domain platform device for R5 cluster
 796 *
 797 * called when driver is probed, for each R5 core specified in DT,
 798 * setup as needed to do remoteproc-related operations
 799 *
 800 * Return: 0 for success, negative value for failure.
 801 */
 802static int zynqmp_r5_remoteproc_probe(struct platform_device *pdev)
 803{
 804        int ret, core_count;
 805        struct device *dev = &pdev->dev;
 806        struct device_node *nc;
 807        enum rpu_oper_mode rpu_mode = PM_RPU_MODE_LOCKSTEP;
 808        struct list_head *cluster; /* list to track each core's rproc */
 809        struct zynqmp_r5_rproc *z_rproc = NULL;
 810        struct platform_device *child_pdev;
 811        struct list_head *pos;
 812
 813        ret = of_property_read_u32(dev->of_node, "xlnx,cluster-mode", &rpu_mode);
 814        if (ret < 0 || (rpu_mode != PM_RPU_MODE_LOCKSTEP &&
 815                        rpu_mode != PM_RPU_MODE_SPLIT)) {
 816                dev_err(dev, "invalid format cluster mode: ret %d mode %x\n",
 817                        ret, rpu_mode);
 818                return ret;
 819        }
 820
 821        dev_dbg(dev, "RPU configuration: %s\n",
 822                rpu_mode == PM_RPU_MODE_LOCKSTEP ? "lockstep" : "split");
 823
 824        /*
 825         * if 2 RPUs provided but one is lockstep, then we have an
 826         * invalid configuration.
 827         */
 828
 829        core_count = of_get_available_child_count(dev->of_node);
 830        if ((rpu_mode == PM_RPU_MODE_LOCKSTEP && core_count != 1) ||
 831            core_count > MAX_RPROCS)
 832                return -EINVAL;
 833
 834        cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
 835        if (!cluster)
 836                return -ENOMEM;
 837        INIT_LIST_HEAD(cluster);
 838
 839        ret = devm_of_platform_populate(dev);
 840        if (ret) {
 841                dev_err(dev, "devm_of_platform_populate failed, ret = %d\n",
 842                        ret);
 843                return ret;
 844        }
 845
 846        /* probe each individual r5 core's remoteproc-related info */
 847        for_each_available_child_of_node(dev->of_node, nc) {
 848                child_pdev = of_find_device_by_node(nc);
 849                if (!child_pdev) {
 850                        dev_err(dev, "could not get R5 core platform device\n");
 851                        ret = -ENODEV;
 852                        goto out;
 853                }
 854
 855                ret = zynqmp_r5_probe(child_pdev, nc, rpu_mode, &z_rproc);
 856                dev_dbg(dev, "%s to probe rpu %pOF\n",
 857                        ret ? "Failed" : "Able",
 858                        nc);
 859                if (!z_rproc)
 860                        ret = -EINVAL;
 861                if (ret)
 862                        goto out;
 863                list_add_tail(&z_rproc->elem, cluster);
 864        }
 865        /* wire in so each core can be cleaned up at driver remove */
 866        platform_set_drvdata(pdev, cluster);
 867        return 0;
 868out:
 869        /*
 870         * undo core0 upon any failures on core1 in split-mode
 871         *
 872         * in zynqmp_r5_probe z_rproc is set to null
 873         * and ret to non-zero value if error
 874         */
 875        if (ret && !z_rproc && rpu_mode == PM_RPU_MODE_SPLIT &&
 876            !list_empty(cluster)) {
 877                list_for_each(pos, cluster) {
 878                        z_rproc = list_entry(pos, struct zynqmp_r5_rproc, elem);
 879                        if (of_property_read_bool(z_rproc->dev->of_node, "mboxes")) {
 880                                mbox_free_channel(z_rproc->tx_chan);
 881                                mbox_free_channel(z_rproc->rx_chan);
 882                        }
 883                }
 884        }
 885        return ret;
 886}
 887
 888/*
 889 * zynqmp_r5_remoteproc_remove()
 890 *
 891 * @pdev: domain platform device for R5 cluster
 892 *
 893 * When the driver is unloaded, clean up the mailboxes for each
 894 * remoteproc that was initially probed.
 895 */
 896static int zynqmp_r5_remoteproc_remove(struct platform_device *pdev)
 897{
 898        struct list_head *pos, *temp, *cluster = (struct list_head *)
 899                                                 platform_get_drvdata(pdev);
 900        struct zynqmp_r5_rproc *z_rproc = NULL;
 901
 902        list_for_each_safe(pos, temp, cluster) {
 903                z_rproc = list_entry(pos, struct zynqmp_r5_rproc, elem);
 904
 905                /*
 906                 * For Versal platform, the Xilinx platform management
 907                 * firmware needs to have a release call to match the
 908                 * corresponding reque in order to power down the core.
 909                 */
 910                if (z_rproc->versal)
 911                        zynqmp_pm_release_node(z_rproc->pnode_id);
 912
 913                if (of_property_read_bool(z_rproc->dev->of_node, "mboxes")) {
 914                        mbox_free_channel(z_rproc->tx_chan);
 915                        mbox_free_channel(z_rproc->rx_chan);
 916                }
 917                list_del(pos);
 918        }
 919        return 0;
 920}
 921
 922/* Match table for OF platform binding */
 923static const struct of_device_id zynqmp_r5_remoteproc_match[] = {
 924        { .compatible = "xlnx,zynqmp-r5-remoteproc", },
 925        { /* end of list */ },
 926};
 927MODULE_DEVICE_TABLE(of, zynqmp_r5_remoteproc_match);
 928
 929static struct platform_driver zynqmp_r5_remoteproc_driver = {
 930        .probe = zynqmp_r5_remoteproc_probe,
 931        .remove = zynqmp_r5_remoteproc_remove,
 932        .driver = {
 933                .name = "zynqmp_r5_remoteproc",
 934                .of_match_table = zynqmp_r5_remoteproc_match,
 935        },
 936};
 937module_platform_driver(zynqmp_r5_remoteproc_driver);
 938
 939MODULE_AUTHOR("Ben Levinsky <ben.levinsky@xilinx.com>");
 940MODULE_LICENSE("GPL v2");
 941