Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * System Control and Management Interface (SCMI) Message Mailbox Transport
0004  * driver.
0005  *
0006  * Copyright (C) 2019 ARM Ltd.
0007  */
0008 
0009 #include <linux/err.h>
0010 #include <linux/device.h>
0011 #include <linux/mailbox_client.h>
0012 #include <linux/of.h>
0013 #include <linux/of_address.h>
0014 #include <linux/slab.h>
0015 
0016 #include "common.h"
0017 
0018 /**
0019  * struct scmi_mailbox - Structure representing a SCMI mailbox transport
0020  *
0021  * @cl: Mailbox Client
0022  * @chan: Transmit/Receive mailbox channel
0023  * @cinfo: SCMI channel info
0024  * @shmem: Transmit/Receive shared memory area
0025  */
0026 struct scmi_mailbox {
0027     struct mbox_client cl;
0028     struct mbox_chan *chan;
0029     struct scmi_chan_info *cinfo;
0030     struct scmi_shared_mem __iomem *shmem;
0031 };
0032 
0033 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl)
0034 
0035 static void tx_prepare(struct mbox_client *cl, void *m)
0036 {
0037     struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
0038 
0039     shmem_tx_prepare(smbox->shmem, m);
0040 }
0041 
0042 static void rx_callback(struct mbox_client *cl, void *m)
0043 {
0044     struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
0045 
0046     scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL);
0047 }
0048 
0049 static bool mailbox_chan_available(struct device *dev, int idx)
0050 {
0051     return !of_parse_phandle_with_args(dev->of_node, "mboxes",
0052                        "#mbox-cells", idx, NULL);
0053 }
0054 
0055 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
0056                   bool tx)
0057 {
0058     const char *desc = tx ? "Tx" : "Rx";
0059     struct device *cdev = cinfo->dev;
0060     struct scmi_mailbox *smbox;
0061     struct device_node *shmem;
0062     int ret, idx = tx ? 0 : 1;
0063     struct mbox_client *cl;
0064     resource_size_t size;
0065     struct resource res;
0066 
0067     smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
0068     if (!smbox)
0069         return -ENOMEM;
0070 
0071     shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
0072     if (!of_device_is_compatible(shmem, "arm,scmi-shmem"))
0073         return -ENXIO;
0074 
0075     ret = of_address_to_resource(shmem, 0, &res);
0076     of_node_put(shmem);
0077     if (ret) {
0078         dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
0079         return ret;
0080     }
0081 
0082     size = resource_size(&res);
0083     smbox->shmem = devm_ioremap(dev, res.start, size);
0084     if (!smbox->shmem) {
0085         dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
0086         return -EADDRNOTAVAIL;
0087     }
0088 
0089     cl = &smbox->cl;
0090     cl->dev = cdev;
0091     cl->tx_prepare = tx ? tx_prepare : NULL;
0092     cl->rx_callback = rx_callback;
0093     cl->tx_block = false;
0094     cl->knows_txdone = tx;
0095 
0096     smbox->chan = mbox_request_channel(cl, tx ? 0 : 1);
0097     if (IS_ERR(smbox->chan)) {
0098         ret = PTR_ERR(smbox->chan);
0099         if (ret != -EPROBE_DEFER)
0100             dev_err(cdev, "failed to request SCMI %s mailbox\n",
0101                 tx ? "Tx" : "Rx");
0102         return ret;
0103     }
0104 
0105     cinfo->transport_info = smbox;
0106     smbox->cinfo = cinfo;
0107 
0108     return 0;
0109 }
0110 
0111 static int mailbox_chan_free(int id, void *p, void *data)
0112 {
0113     struct scmi_chan_info *cinfo = p;
0114     struct scmi_mailbox *smbox = cinfo->transport_info;
0115 
0116     if (smbox && !IS_ERR(smbox->chan)) {
0117         mbox_free_channel(smbox->chan);
0118         cinfo->transport_info = NULL;
0119         smbox->chan = NULL;
0120         smbox->cinfo = NULL;
0121     }
0122 
0123     scmi_free_channel(cinfo, data, id);
0124 
0125     return 0;
0126 }
0127 
0128 static int mailbox_send_message(struct scmi_chan_info *cinfo,
0129                 struct scmi_xfer *xfer)
0130 {
0131     struct scmi_mailbox *smbox = cinfo->transport_info;
0132     int ret;
0133 
0134     ret = mbox_send_message(smbox->chan, xfer);
0135 
0136     /* mbox_send_message returns non-negative value on success, so reset */
0137     if (ret > 0)
0138         ret = 0;
0139 
0140     return ret;
0141 }
0142 
0143 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
0144                 struct scmi_xfer *__unused)
0145 {
0146     struct scmi_mailbox *smbox = cinfo->transport_info;
0147 
0148     /*
0149      * NOTE: we might prefer not to need the mailbox ticker to manage the
0150      * transfer queueing since the protocol layer queues things by itself.
0151      * Unfortunately, we have to kick the mailbox framework after we have
0152      * received our message.
0153      */
0154     mbox_client_txdone(smbox->chan, ret);
0155 }
0156 
0157 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
0158                    struct scmi_xfer *xfer)
0159 {
0160     struct scmi_mailbox *smbox = cinfo->transport_info;
0161 
0162     shmem_fetch_response(smbox->shmem, xfer);
0163 }
0164 
0165 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
0166                        size_t max_len, struct scmi_xfer *xfer)
0167 {
0168     struct scmi_mailbox *smbox = cinfo->transport_info;
0169 
0170     shmem_fetch_notification(smbox->shmem, max_len, xfer);
0171 }
0172 
0173 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
0174 {
0175     struct scmi_mailbox *smbox = cinfo->transport_info;
0176 
0177     shmem_clear_channel(smbox->shmem);
0178 }
0179 
0180 static bool
0181 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
0182 {
0183     struct scmi_mailbox *smbox = cinfo->transport_info;
0184 
0185     return shmem_poll_done(smbox->shmem, xfer);
0186 }
0187 
0188 static const struct scmi_transport_ops scmi_mailbox_ops = {
0189     .chan_available = mailbox_chan_available,
0190     .chan_setup = mailbox_chan_setup,
0191     .chan_free = mailbox_chan_free,
0192     .send_message = mailbox_send_message,
0193     .mark_txdone = mailbox_mark_txdone,
0194     .fetch_response = mailbox_fetch_response,
0195     .fetch_notification = mailbox_fetch_notification,
0196     .clear_channel = mailbox_clear_channel,
0197     .poll_done = mailbox_poll_done,
0198 };
0199 
0200 const struct scmi_desc scmi_mailbox_desc = {
0201     .ops = &scmi_mailbox_ops,
0202     .max_rx_timeout_ms = 30, /* We may increase this if required */
0203     .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
0204     .max_msg_size = 128,
0205 };