firmware: arm_scmi: Segregate tx channel handling and prepare to add rx
[linux-block.git] / drivers / firmware / arm_scmi / driver.c
index b5bc4c7a8fab2957bb0842b95e32d152f11837aa..08787aed551995302b6f7ed05dce434082f26b30 100644 (file)
@@ -86,7 +86,7 @@ struct scmi_desc {
 };
 
 /**
- * struct scmi_chan_info - Structure representing a SCMI channel informfation
+ * struct scmi_chan_info - Structure representing a SCMI channel information
  *
  * @cl: Mailbox Client
  * @chan: Transmit/Receive mailbox channel
@@ -112,7 +112,7 @@ struct scmi_chan_info {
  * @version: SCMI revision information containing protocol version,
  *     implementation version and (sub-)vendor identification.
  * @minfo: Message info
- * @tx_idr: IDR object to map protocol id to channel info pointer
+ * @tx_idr: IDR object to map protocol id to Tx channel info pointer
  * @protocols_imp: List of protocols implemented, currently maximum of
  *     MAX_PROTOCOLS_IMP elements allocated by the base protocol
  * @node: List head
@@ -182,7 +182,7 @@ static inline int scmi_to_linux_errno(int errno)
 static inline void scmi_dump_header_dbg(struct device *dev,
                                        struct scmi_msg_hdr *hdr)
 {
-       dev_dbg(dev, "Command ID: %x Sequence ID: %x Protocol: %x\n",
+       dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
                hdr->id, hdr->seq, hdr->protocol_id);
 }
 
@@ -190,64 +190,20 @@ static void scmi_fetch_response(struct scmi_xfer *xfer,
                                struct scmi_shared_mem __iomem *mem)
 {
        xfer->hdr.status = ioread32(mem->msg_payload);
-       /* Skip the length of header and statues in payload area i.e 8 bytes*/
+       /* Skip the length of header and status in payload area i.e 8 bytes */
        xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8);
 
        /* Take a copy to the rx buffer.. */
        memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len);
 }
 
-/**
- * scmi_rx_callback() - mailbox client callback for receive messages
- *
- * @cl: client pointer
- * @m: mailbox message
- *
- * Processes one received message to appropriate transfer information and
- * signals completion of the transfer.
- *
- * NOTE: This function will be invoked in IRQ context, hence should be
- * as optimal as possible.
- */
-static void scmi_rx_callback(struct mbox_client *cl, void *m)
-{
-       u16 xfer_id;
-       struct scmi_xfer *xfer;
-       struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
-       struct device *dev = cinfo->dev;
-       struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
-       struct scmi_xfers_info *minfo = &info->minfo;
-       struct scmi_shared_mem __iomem *mem = cinfo->payload;
-
-       xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
-
-       /* Are we even expecting this? */
-       if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
-               dev_err(dev, "message for %d is not expected!\n", xfer_id);
-               return;
-       }
-
-       xfer = &minfo->xfer_block[xfer_id];
-
-       scmi_dump_header_dbg(dev, &xfer->hdr);
-       /* Is the message of valid length? */
-       if (xfer->rx.len > info->desc->max_msg_size) {
-               dev_err(dev, "unable to handle %zu xfer(max %d)\n",
-                       xfer->rx.len, info->desc->max_msg_size);
-               return;
-       }
-
-       scmi_fetch_response(xfer, mem);
-       complete(&xfer->done);
-}
-
 /**
  * pack_scmi_header() - packs and returns 32-bit header
  *
  * @hdr: pointer to header containing all the information on message id,
  *     protocol id and sequence id.
  *
- * Return: 32-bit packed command header to be sent to the platform.
+ * Return: 32-bit packed message header to be sent to the platform.
  */
 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
 {
@@ -271,6 +227,14 @@ static void scmi_tx_prepare(struct mbox_client *cl, void *m)
        struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
        struct scmi_shared_mem __iomem *mem = cinfo->payload;
 
+       /*
+        * Ideally channel must be free by now unless OS timeout last
+        * request and platform continued to process the same, wait
+        * until it releases the shared memory, otherwise we may endup
+        * overwriting its response with new message payload or vice-versa
+        */
+       spin_until_cond(ioread32(&mem->channel_status) &
+                       SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
        /* Mark channel busy + clear error */
        iowrite32(0x0, &mem->channel_status);
        iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
@@ -286,7 +250,7 @@ static void scmi_tx_prepare(struct mbox_client *cl, void *m)
  *
  * @handle: Pointer to SCMI entity handle
  *
- * Helper function which is used by various command functions that are
+ * Helper function which is used by various message functions that are
  * exposed to clients of this driver for allocating a message traffic event.
  *
  * This function can sleep depending on pending requests already in the system
@@ -347,6 +311,44 @@ void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
        spin_unlock_irqrestore(&minfo->xfer_lock, flags);
 }
 
+/**
+ * scmi_rx_callback() - mailbox client callback for receive messages
+ *
+ * @cl: client pointer
+ * @m: mailbox message
+ *
+ * Processes one received message to appropriate transfer information and
+ * signals completion of the transfer.
+ *
+ * NOTE: This function will be invoked in IRQ context, hence should be
+ * as optimal as possible.
+ */
+static void scmi_rx_callback(struct mbox_client *cl, void *m)
+{
+       u16 xfer_id;
+       struct scmi_xfer *xfer;
+       struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
+       struct device *dev = cinfo->dev;
+       struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
+       struct scmi_xfers_info *minfo = &info->minfo;
+       struct scmi_shared_mem __iomem *mem = cinfo->payload;
+
+       xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
+
+       /* Are we even expecting this? */
+       if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
+               dev_err(dev, "message for %d is not expected!\n", xfer_id);
+               return;
+       }
+
+       xfer = &minfo->xfer_block[xfer_id];
+
+       scmi_dump_header_dbg(dev, &xfer->hdr);
+
+       scmi_fetch_response(xfer, mem);
+       complete(&xfer->done);
+}
+
 static bool
 scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
 {
@@ -597,20 +599,6 @@ int scmi_handle_put(const struct scmi_handle *handle)
        return 0;
 }
 
-static const struct scmi_desc scmi_generic_desc = {
-       .max_rx_timeout_ms = 30,        /* We may increase this if required */
-       .max_msg = 20,          /* Limited by MBOX_TX_QUEUE_LEN */
-       .max_msg_size = 128,
-};
-
-/* Each compatible listed below must have descriptor associated with it */
-static const struct of_device_id scmi_of_match[] = {
-       { .compatible = "arm,scmi", .data = &scmi_generic_desc },
-       { /* Sentinel */ },
-};
-
-MODULE_DEVICE_TABLE(of, scmi_of_match);
-
 static int scmi_xfer_info_init(struct scmi_info *sinfo)
 {
        int i;
@@ -652,60 +640,27 @@ static int scmi_xfer_info_init(struct scmi_info *sinfo)
        return 0;
 }
 
-static int scmi_mailbox_check(struct device_node *np)
+static int scmi_mailbox_check(struct device_node *np, int idx)
 {
-       return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells", 0, NULL);
+       return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells",
+                                         idx, NULL);
 }
 
-static int scmi_mbox_free_channel(int id, void *p, void *data)
-{
-       struct scmi_chan_info *cinfo = p;
-       struct idr *idr = data;
-
-       if (!IS_ERR_OR_NULL(cinfo->chan)) {
-               mbox_free_channel(cinfo->chan);
-               cinfo->chan = NULL;
-       }
-
-       idr_remove(idr, id);
-
-       return 0;
-}
-
-static int scmi_remove(struct platform_device *pdev)
+static int scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev,
+                               int prot_id, bool tx)
 {
-       int ret = 0;
-       struct scmi_info *info = platform_get_drvdata(pdev);
-       struct idr *idr = &info->tx_idr;
-
-       mutex_lock(&scmi_list_mutex);
-       if (info->users)
-               ret = -EBUSY;
-       else
-               list_del(&info->node);
-       mutex_unlock(&scmi_list_mutex);
-
-       if (ret)
-               return ret;
-
-       /* Safe to free channels since no more users */
-       ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
-       idr_destroy(&info->tx_idr);
-
-       return ret;
-}
-
-static inline int
-scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev, int prot_id)
-{
-       int ret;
+       int ret, idx;
        struct resource res;
        resource_size_t size;
        struct device_node *shmem, *np = dev->of_node;
        struct scmi_chan_info *cinfo;
        struct mbox_client *cl;
+       const char *desc = tx ? "Tx" : "Rx";
 
-       if (scmi_mailbox_check(np)) {
+       /* Transmit channel is first entry i.e. index 0 */
+       idx = tx ? 0 : 1;
+
+       if (scmi_mailbox_check(np, idx)) {
                cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
                goto idr_alloc;
        }
@@ -719,31 +674,31 @@ scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev, int prot_id)
        cl = &cinfo->cl;
        cl->dev = dev;
        cl->rx_callback = scmi_rx_callback;
-       cl->tx_prepare = scmi_tx_prepare;
+       cl->tx_prepare = tx ? scmi_tx_prepare : NULL;
        cl->tx_block = false;
-       cl->knows_txdone = true;
+       cl->knows_txdone = tx;
 
-       shmem = of_parse_phandle(np, "shmem", 0);
+       shmem = of_parse_phandle(np, "shmem", idx);
        ret = of_address_to_resource(shmem, 0, &res);
        of_node_put(shmem);
        if (ret) {
-               dev_err(dev, "failed to get SCMI Tx payload mem resource\n");
+               dev_err(dev, "failed to get SCMI %s payload memory\n", desc);
                return ret;
        }
 
        size = resource_size(&res);
        cinfo->payload = devm_ioremap(info->dev, res.start, size);
        if (!cinfo->payload) {
-               dev_err(dev, "failed to ioremap SCMI Tx payload\n");
+               dev_err(dev, "failed to ioremap SCMI %s payload\n", desc);
                return -EADDRNOTAVAIL;
        }
 
-       /* Transmit channel is first entry i.e. index 0 */
-       cinfo->chan = mbox_request_channel(cl, 0);
+       cinfo->chan = mbox_request_channel(cl, idx);
        if (IS_ERR(cinfo->chan)) {
                ret = PTR_ERR(cinfo->chan);
                if (ret != -EPROBE_DEFER)
-                       dev_err(dev, "failed to request SCMI Tx mailbox\n");
+                       dev_err(dev, "failed to request SCMI %s mailbox\n",
+                               desc);
                return ret;
        }
 
@@ -771,7 +726,7 @@ scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
                return;
        }
 
-       if (scmi_mbox_chan_setup(info, &sdev->dev, prot_id)) {
+       if (scmi_mbox_chan_setup(info, &sdev->dev, prot_id, true)) {
                dev_err(&sdev->dev, "failed to setup transport\n");
                scmi_device_destroy(sdev);
                return;
@@ -791,7 +746,7 @@ static int scmi_probe(struct platform_device *pdev)
        struct device_node *child, *np = dev->of_node;
 
        /* Only mailbox method supported, check for the presence of one */
-       if (scmi_mailbox_check(np)) {
+       if (scmi_mailbox_check(np, 0)) {
                dev_err(dev, "no mailbox found in %pOF\n", np);
                return -EINVAL;
        }
@@ -819,7 +774,7 @@ static int scmi_probe(struct platform_device *pdev)
        handle->dev = info->dev;
        handle->version = &info->version;
 
-       ret = scmi_mbox_chan_setup(info, dev, SCMI_PROTOCOL_BASE);
+       ret = scmi_mbox_chan_setup(info, dev, SCMI_PROTOCOL_BASE, true);
        if (ret)
                return ret;
 
@@ -854,6 +809,58 @@ static int scmi_probe(struct platform_device *pdev)
        return 0;
 }
 
+static int scmi_mbox_free_channel(int id, void *p, void *data)
+{
+       struct scmi_chan_info *cinfo = p;
+       struct idr *idr = data;
+
+       if (!IS_ERR_OR_NULL(cinfo->chan)) {
+               mbox_free_channel(cinfo->chan);
+               cinfo->chan = NULL;
+       }
+
+       idr_remove(idr, id);
+
+       return 0;
+}
+
+static int scmi_remove(struct platform_device *pdev)
+{
+       int ret = 0;
+       struct scmi_info *info = platform_get_drvdata(pdev);
+       struct idr *idr = &info->tx_idr;
+
+       mutex_lock(&scmi_list_mutex);
+       if (info->users)
+               ret = -EBUSY;
+       else
+               list_del(&info->node);
+       mutex_unlock(&scmi_list_mutex);
+
+       if (ret)
+               return ret;
+
+       /* Safe to free channels since no more users */
+       ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
+       idr_destroy(&info->tx_idr);
+
+       return ret;
+}
+
+static const struct scmi_desc scmi_generic_desc = {
+       .max_rx_timeout_ms = 30,        /* We may increase this if required */
+       .max_msg = 20,          /* Limited by MBOX_TX_QUEUE_LEN */
+       .max_msg_size = 128,
+};
+
+/* Each compatible listed below must have descriptor associated with it */
+static const struct of_device_id scmi_of_match[] = {
+       { .compatible = "arm,scmi", .data = &scmi_generic_desc },
+       { /* Sentinel */ },
+};
+
+MODULE_DEVICE_TABLE(of, scmi_of_match);
+
 static struct platform_driver scmi_driver = {
        .driver = {
                   .name = "arm-scmi",