^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * System Control and Management Interface (SCMI) Message SMC/HVC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Transport driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2020 NXP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/arm-smccc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * struct scmi_smc - Structure representing a SCMI smc transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @cinfo: SCMI channel info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @shmem: Transmit/Receive shared memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @shmem_lock: Lock to protect access to Tx/Rx shared memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @func_id: smc/hvc call function id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * @irq: Optional; employed when platforms indicates msg completion by intr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @tx_complete: Optional, employed only when irq is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct scmi_smc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct scmi_chan_info *cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct scmi_shared_mem __iomem *shmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct mutex shmem_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u32 func_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct completion tx_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static irqreturn_t smc_msg_done_isr(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct scmi_smc *scmi_info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) complete(&scmi_info->tx_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static bool smc_chan_available(struct device *dev, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct device_node *np = of_parse_phandle(dev->of_node, "shmem", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int smc_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) bool tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct device *cdev = cinfo->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct scmi_smc *scmi_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) resource_size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u32 func_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int ret, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) scmi_info = devm_kzalloc(dev, sizeof(*scmi_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!scmi_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) np = of_parse_phandle(cdev->of_node, "shmem", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ret = of_address_to_resource(np, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dev_err(cdev, "failed to get SCMI Tx shared memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) size = resource_size(&res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) scmi_info->shmem = devm_ioremap(dev, res.start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!scmi_info->shmem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev_err(dev, "failed to ioremap SCMI Tx shared memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = of_property_read_u32(dev->of_node, "arm,smc-id", &func_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * If there is an interrupt named "a2p", then the service and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * completion of a message is signaled by an interrupt rather than by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * the return of the SMC call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) irq = of_irq_get_byname(cdev->of_node, "a2p");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = devm_request_irq(dev, irq, smc_msg_done_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) IRQF_NO_SUSPEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dev_name(dev), scmi_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_err(dev, "failed to setup SCMI smc irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) init_completion(&scmi_info->tx_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) scmi_info->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) scmi_info->func_id = func_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) scmi_info->cinfo = cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) mutex_init(&scmi_info->shmem_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) cinfo->transport_info = scmi_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int smc_chan_free(int id, void *p, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct scmi_chan_info *cinfo = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct scmi_smc *scmi_info = cinfo->transport_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) cinfo->transport_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) scmi_info->cinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) scmi_free_channel(cinfo, data, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int smc_send_message(struct scmi_chan_info *cinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct scmi_xfer *xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct scmi_smc *scmi_info = cinfo->transport_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct arm_smccc_res res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) mutex_lock(&scmi_info->shmem_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) shmem_tx_prepare(scmi_info->shmem, xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (scmi_info->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) reinit_completion(&scmi_info->tx_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) arm_smccc_1_1_invoke(scmi_info->func_id, 0, 0, 0, 0, 0, 0, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (scmi_info->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) wait_for_completion(&scmi_info->tx_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) scmi_rx_callback(scmi_info->cinfo, shmem_read_header(scmi_info->shmem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) mutex_unlock(&scmi_info->shmem_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* Only SMCCC_RET_NOT_SUPPORTED is valid error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (res.a0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void smc_fetch_response(struct scmi_chan_info *cinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct scmi_xfer *xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct scmi_smc *scmi_info = cinfo->transport_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) shmem_fetch_response(scmi_info->shmem, xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) smc_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct scmi_smc *scmi_info = cinfo->transport_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return shmem_poll_done(scmi_info->shmem, xfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static const struct scmi_transport_ops scmi_smc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .chan_available = smc_chan_available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .chan_setup = smc_chan_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .chan_free = smc_chan_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .send_message = smc_send_message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .fetch_response = smc_fetch_response,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .poll_done = smc_poll_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) const struct scmi_desc scmi_smc_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .ops = &scmi_smc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .max_rx_timeout_ms = 30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .max_msg = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .max_msg_size = 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) };