Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * System Control and Power Interface (SCPI) Message Protocol driver
0004  *
0005  * SCPI Message Protocol is used between the System Control Processor(SCP)
0006  * and the Application Processors(AP). The Message Handling Unit(MHU)
0007  * provides a mechanism for inter-processor communication between SCP's
0008  * Cortex M3 and AP.
0009  *
0010  * SCP offers control and management of the core/cluster power states,
0011  * various power domain DVFS including the core/cluster, certain system
0012  * clocks configuration, thermal sensors and many others.
0013  *
0014  * Copyright (C) 2015 ARM Ltd.
0015  */
0016 
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018 
0019 #include <linux/bitmap.h>
0020 #include <linux/bitfield.h>
0021 #include <linux/device.h>
0022 #include <linux/err.h>
0023 #include <linux/export.h>
0024 #include <linux/io.h>
0025 #include <linux/kernel.h>
0026 #include <linux/list.h>
0027 #include <linux/mailbox_client.h>
0028 #include <linux/module.h>
0029 #include <linux/of_address.h>
0030 #include <linux/of_platform.h>
0031 #include <linux/printk.h>
0032 #include <linux/pm_opp.h>
0033 #include <linux/scpi_protocol.h>
0034 #include <linux/slab.h>
0035 #include <linux/sort.h>
0036 #include <linux/spinlock.h>
0037 
0038 #define CMD_ID_MASK     GENMASK(6, 0)
0039 #define CMD_TOKEN_ID_MASK   GENMASK(15, 8)
0040 #define CMD_DATA_SIZE_MASK  GENMASK(24, 16)
0041 #define CMD_LEGACY_DATA_SIZE_MASK   GENMASK(28, 20)
0042 #define PACK_SCPI_CMD(cmd_id, tx_sz)        \
0043     (FIELD_PREP(CMD_ID_MASK, cmd_id) |  \
0044     FIELD_PREP(CMD_DATA_SIZE_MASK, tx_sz))
0045 #define PACK_LEGACY_SCPI_CMD(cmd_id, tx_sz) \
0046     (FIELD_PREP(CMD_ID_MASK, cmd_id) |  \
0047     FIELD_PREP(CMD_LEGACY_DATA_SIZE_MASK, tx_sz))
0048 
0049 #define CMD_SIZE(cmd)   FIELD_GET(CMD_DATA_SIZE_MASK, cmd)
0050 #define CMD_UNIQ_MASK   (CMD_TOKEN_ID_MASK | CMD_ID_MASK)
0051 #define CMD_XTRACT_UNIQ(cmd)    ((cmd) & CMD_UNIQ_MASK)
0052 
0053 #define SCPI_SLOT       0
0054 
0055 #define MAX_DVFS_DOMAINS    8
0056 #define MAX_DVFS_OPPS       16
0057 
0058 #define PROTO_REV_MAJOR_MASK    GENMASK(31, 16)
0059 #define PROTO_REV_MINOR_MASK    GENMASK(15, 0)
0060 
0061 #define FW_REV_MAJOR_MASK   GENMASK(31, 24)
0062 #define FW_REV_MINOR_MASK   GENMASK(23, 16)
0063 #define FW_REV_PATCH_MASK   GENMASK(15, 0)
0064 
0065 #define MAX_RX_TIMEOUT      (msecs_to_jiffies(30))
0066 
0067 enum scpi_error_codes {
0068     SCPI_SUCCESS = 0, /* Success */
0069     SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */
0070     SCPI_ERR_ALIGN = 2, /* Invalid alignment */
0071     SCPI_ERR_SIZE = 3, /* Invalid size */
0072     SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */
0073     SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */
0074     SCPI_ERR_RANGE = 6, /* Value out of range */
0075     SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */
0076     SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */
0077     SCPI_ERR_PWRSTATE = 9, /* Invalid power state */
0078     SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */
0079     SCPI_ERR_DEVICE = 11, /* Device error */
0080     SCPI_ERR_BUSY = 12, /* Device busy */
0081     SCPI_ERR_MAX
0082 };
0083 
0084 /* SCPI Standard commands */
0085 enum scpi_std_cmd {
0086     SCPI_CMD_INVALID        = 0x00,
0087     SCPI_CMD_SCPI_READY     = 0x01,
0088     SCPI_CMD_SCPI_CAPABILITIES  = 0x02,
0089     SCPI_CMD_SET_CSS_PWR_STATE  = 0x03,
0090     SCPI_CMD_GET_CSS_PWR_STATE  = 0x04,
0091     SCPI_CMD_SET_SYS_PWR_STATE  = 0x05,
0092     SCPI_CMD_SET_CPU_TIMER      = 0x06,
0093     SCPI_CMD_CANCEL_CPU_TIMER   = 0x07,
0094     SCPI_CMD_DVFS_CAPABILITIES  = 0x08,
0095     SCPI_CMD_GET_DVFS_INFO      = 0x09,
0096     SCPI_CMD_SET_DVFS       = 0x0a,
0097     SCPI_CMD_GET_DVFS       = 0x0b,
0098     SCPI_CMD_GET_DVFS_STAT      = 0x0c,
0099     SCPI_CMD_CLOCK_CAPABILITIES = 0x0d,
0100     SCPI_CMD_GET_CLOCK_INFO     = 0x0e,
0101     SCPI_CMD_SET_CLOCK_VALUE    = 0x0f,
0102     SCPI_CMD_GET_CLOCK_VALUE    = 0x10,
0103     SCPI_CMD_PSU_CAPABILITIES   = 0x11,
0104     SCPI_CMD_GET_PSU_INFO       = 0x12,
0105     SCPI_CMD_SET_PSU        = 0x13,
0106     SCPI_CMD_GET_PSU        = 0x14,
0107     SCPI_CMD_SENSOR_CAPABILITIES    = 0x15,
0108     SCPI_CMD_SENSOR_INFO        = 0x16,
0109     SCPI_CMD_SENSOR_VALUE       = 0x17,
0110     SCPI_CMD_SENSOR_CFG_PERIODIC    = 0x18,
0111     SCPI_CMD_SENSOR_CFG_BOUNDS  = 0x19,
0112     SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1a,
0113     SCPI_CMD_SET_DEVICE_PWR_STATE   = 0x1b,
0114     SCPI_CMD_GET_DEVICE_PWR_STATE   = 0x1c,
0115     SCPI_CMD_COUNT
0116 };
0117 
0118 /* SCPI Legacy Commands */
0119 enum legacy_scpi_std_cmd {
0120     LEGACY_SCPI_CMD_INVALID         = 0x00,
0121     LEGACY_SCPI_CMD_SCPI_READY      = 0x01,
0122     LEGACY_SCPI_CMD_SCPI_CAPABILITIES   = 0x02,
0123     LEGACY_SCPI_CMD_EVENT           = 0x03,
0124     LEGACY_SCPI_CMD_SET_CSS_PWR_STATE   = 0x04,
0125     LEGACY_SCPI_CMD_GET_CSS_PWR_STATE   = 0x05,
0126     LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT  = 0x06,
0127     LEGACY_SCPI_CMD_GET_PWR_STATE_STAT  = 0x07,
0128     LEGACY_SCPI_CMD_SYS_PWR_STATE       = 0x08,
0129     LEGACY_SCPI_CMD_L2_READY        = 0x09,
0130     LEGACY_SCPI_CMD_SET_AP_TIMER        = 0x0a,
0131     LEGACY_SCPI_CMD_CANCEL_AP_TIME      = 0x0b,
0132     LEGACY_SCPI_CMD_DVFS_CAPABILITIES   = 0x0c,
0133     LEGACY_SCPI_CMD_GET_DVFS_INFO       = 0x0d,
0134     LEGACY_SCPI_CMD_SET_DVFS        = 0x0e,
0135     LEGACY_SCPI_CMD_GET_DVFS        = 0x0f,
0136     LEGACY_SCPI_CMD_GET_DVFS_STAT       = 0x10,
0137     LEGACY_SCPI_CMD_SET_RTC         = 0x11,
0138     LEGACY_SCPI_CMD_GET_RTC         = 0x12,
0139     LEGACY_SCPI_CMD_CLOCK_CAPABILITIES  = 0x13,
0140     LEGACY_SCPI_CMD_SET_CLOCK_INDEX     = 0x14,
0141     LEGACY_SCPI_CMD_SET_CLOCK_VALUE     = 0x15,
0142     LEGACY_SCPI_CMD_GET_CLOCK_VALUE     = 0x16,
0143     LEGACY_SCPI_CMD_PSU_CAPABILITIES    = 0x17,
0144     LEGACY_SCPI_CMD_SET_PSU         = 0x18,
0145     LEGACY_SCPI_CMD_GET_PSU         = 0x19,
0146     LEGACY_SCPI_CMD_SENSOR_CAPABILITIES = 0x1a,
0147     LEGACY_SCPI_CMD_SENSOR_INFO     = 0x1b,
0148     LEGACY_SCPI_CMD_SENSOR_VALUE        = 0x1c,
0149     LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC = 0x1d,
0150     LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS   = 0x1e,
0151     LEGACY_SCPI_CMD_SENSOR_ASYNC_VALUE  = 0x1f,
0152     LEGACY_SCPI_CMD_COUNT
0153 };
0154 
0155 /* List all commands that are required to go through the high priority link */
0156 static int legacy_hpriority_cmds[] = {
0157     LEGACY_SCPI_CMD_GET_CSS_PWR_STATE,
0158     LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT,
0159     LEGACY_SCPI_CMD_GET_PWR_STATE_STAT,
0160     LEGACY_SCPI_CMD_SET_DVFS,
0161     LEGACY_SCPI_CMD_GET_DVFS,
0162     LEGACY_SCPI_CMD_SET_RTC,
0163     LEGACY_SCPI_CMD_GET_RTC,
0164     LEGACY_SCPI_CMD_SET_CLOCK_INDEX,
0165     LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
0166     LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
0167     LEGACY_SCPI_CMD_SET_PSU,
0168     LEGACY_SCPI_CMD_GET_PSU,
0169     LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC,
0170     LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS,
0171 };
0172 
0173 /* List all commands used by this driver, used as indexes */
0174 enum scpi_drv_cmds {
0175     CMD_SCPI_CAPABILITIES = 0,
0176     CMD_GET_CLOCK_INFO,
0177     CMD_GET_CLOCK_VALUE,
0178     CMD_SET_CLOCK_VALUE,
0179     CMD_GET_DVFS,
0180     CMD_SET_DVFS,
0181     CMD_GET_DVFS_INFO,
0182     CMD_SENSOR_CAPABILITIES,
0183     CMD_SENSOR_INFO,
0184     CMD_SENSOR_VALUE,
0185     CMD_SET_DEVICE_PWR_STATE,
0186     CMD_GET_DEVICE_PWR_STATE,
0187     CMD_MAX_COUNT,
0188 };
0189 
0190 static int scpi_std_commands[CMD_MAX_COUNT] = {
0191     SCPI_CMD_SCPI_CAPABILITIES,
0192     SCPI_CMD_GET_CLOCK_INFO,
0193     SCPI_CMD_GET_CLOCK_VALUE,
0194     SCPI_CMD_SET_CLOCK_VALUE,
0195     SCPI_CMD_GET_DVFS,
0196     SCPI_CMD_SET_DVFS,
0197     SCPI_CMD_GET_DVFS_INFO,
0198     SCPI_CMD_SENSOR_CAPABILITIES,
0199     SCPI_CMD_SENSOR_INFO,
0200     SCPI_CMD_SENSOR_VALUE,
0201     SCPI_CMD_SET_DEVICE_PWR_STATE,
0202     SCPI_CMD_GET_DEVICE_PWR_STATE,
0203 };
0204 
0205 static int scpi_legacy_commands[CMD_MAX_COUNT] = {
0206     LEGACY_SCPI_CMD_SCPI_CAPABILITIES,
0207     -1, /* GET_CLOCK_INFO */
0208     LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
0209     LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
0210     LEGACY_SCPI_CMD_GET_DVFS,
0211     LEGACY_SCPI_CMD_SET_DVFS,
0212     LEGACY_SCPI_CMD_GET_DVFS_INFO,
0213     LEGACY_SCPI_CMD_SENSOR_CAPABILITIES,
0214     LEGACY_SCPI_CMD_SENSOR_INFO,
0215     LEGACY_SCPI_CMD_SENSOR_VALUE,
0216     -1, /* SET_DEVICE_PWR_STATE */
0217     -1, /* GET_DEVICE_PWR_STATE */
0218 };
0219 
0220 struct scpi_xfer {
0221     u32 slot; /* has to be first element */
0222     u32 cmd;
0223     u32 status;
0224     const void *tx_buf;
0225     void *rx_buf;
0226     unsigned int tx_len;
0227     unsigned int rx_len;
0228     struct list_head node;
0229     struct completion done;
0230 };
0231 
0232 struct scpi_chan {
0233     struct mbox_client cl;
0234     struct mbox_chan *chan;
0235     void __iomem *tx_payload;
0236     void __iomem *rx_payload;
0237     struct list_head rx_pending;
0238     struct list_head xfers_list;
0239     struct scpi_xfer *xfers;
0240     spinlock_t rx_lock; /* locking for the rx pending list */
0241     struct mutex xfers_lock;
0242     u8 token;
0243 };
0244 
0245 struct scpi_drvinfo {
0246     u32 protocol_version;
0247     u32 firmware_version;
0248     bool is_legacy;
0249     int num_chans;
0250     int *commands;
0251     DECLARE_BITMAP(cmd_priority, LEGACY_SCPI_CMD_COUNT);
0252     atomic_t next_chan;
0253     struct scpi_ops *scpi_ops;
0254     struct scpi_chan *channels;
0255     struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS];
0256 };
0257 
0258 /*
0259  * The SCP firmware only executes in little-endian mode, so any buffers
0260  * shared through SCPI should have their contents converted to little-endian
0261  */
0262 struct scpi_shared_mem {
0263     __le32 command;
0264     __le32 status;
0265     u8 payload[];
0266 } __packed;
0267 
0268 struct legacy_scpi_shared_mem {
0269     __le32 status;
0270     u8 payload[];
0271 } __packed;
0272 
0273 struct scp_capabilities {
0274     __le32 protocol_version;
0275     __le32 event_version;
0276     __le32 platform_version;
0277     __le32 commands[4];
0278 } __packed;
0279 
0280 struct clk_get_info {
0281     __le16 id;
0282     __le16 flags;
0283     __le32 min_rate;
0284     __le32 max_rate;
0285     u8 name[20];
0286 } __packed;
0287 
0288 struct clk_set_value {
0289     __le16 id;
0290     __le16 reserved;
0291     __le32 rate;
0292 } __packed;
0293 
0294 struct legacy_clk_set_value {
0295     __le32 rate;
0296     __le16 id;
0297     __le16 reserved;
0298 } __packed;
0299 
0300 struct dvfs_info {
0301     u8 domain;
0302     u8 opp_count;
0303     __le16 latency;
0304     struct {
0305         __le32 freq;
0306         __le32 m_volt;
0307     } opps[MAX_DVFS_OPPS];
0308 } __packed;
0309 
0310 struct dvfs_set {
0311     u8 domain;
0312     u8 index;
0313 } __packed;
0314 
0315 struct _scpi_sensor_info {
0316     __le16 sensor_id;
0317     u8 class;
0318     u8 trigger_type;
0319     char name[20];
0320 };
0321 
0322 struct dev_pstate_set {
0323     __le16 dev_id;
0324     u8 pstate;
0325 } __packed;
0326 
0327 static struct scpi_drvinfo *scpi_info;
0328 
0329 static int scpi_linux_errmap[SCPI_ERR_MAX] = {
0330     /* better than switch case as long as return value is continuous */
0331     0, /* SCPI_SUCCESS */
0332     -EINVAL, /* SCPI_ERR_PARAM */
0333     -ENOEXEC, /* SCPI_ERR_ALIGN */
0334     -EMSGSIZE, /* SCPI_ERR_SIZE */
0335     -EINVAL, /* SCPI_ERR_HANDLER */
0336     -EACCES, /* SCPI_ERR_ACCESS */
0337     -ERANGE, /* SCPI_ERR_RANGE */
0338     -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
0339     -ENOMEM, /* SCPI_ERR_NOMEM */
0340     -EINVAL, /* SCPI_ERR_PWRSTATE */
0341     -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
0342     -EIO, /* SCPI_ERR_DEVICE */
0343     -EBUSY, /* SCPI_ERR_BUSY */
0344 };
0345 
0346 static inline int scpi_to_linux_errno(int errno)
0347 {
0348     if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
0349         return scpi_linux_errmap[errno];
0350     return -EIO;
0351 }
0352 
0353 static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
0354 {
0355     unsigned long flags;
0356     struct scpi_xfer *t, *match = NULL;
0357 
0358     spin_lock_irqsave(&ch->rx_lock, flags);
0359     if (list_empty(&ch->rx_pending)) {
0360         spin_unlock_irqrestore(&ch->rx_lock, flags);
0361         return;
0362     }
0363 
0364     /* Command type is not replied by the SCP Firmware in legacy Mode
0365      * We should consider that command is the head of pending RX commands
0366      * if the list is not empty. In TX only mode, the list would be empty.
0367      */
0368     if (scpi_info->is_legacy) {
0369         match = list_first_entry(&ch->rx_pending, struct scpi_xfer,
0370                      node);
0371         list_del(&match->node);
0372     } else {
0373         list_for_each_entry(t, &ch->rx_pending, node)
0374             if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
0375                 list_del(&t->node);
0376                 match = t;
0377                 break;
0378             }
0379     }
0380     /* check if wait_for_completion is in progress or timed-out */
0381     if (match && !completion_done(&match->done)) {
0382         unsigned int len;
0383 
0384         if (scpi_info->is_legacy) {
0385             struct legacy_scpi_shared_mem __iomem *mem =
0386                             ch->rx_payload;
0387 
0388             /* RX Length is not replied by the legacy Firmware */
0389             len = match->rx_len;
0390 
0391             match->status = ioread32(&mem->status);
0392             memcpy_fromio(match->rx_buf, mem->payload, len);
0393         } else {
0394             struct scpi_shared_mem __iomem *mem = ch->rx_payload;
0395 
0396             len = min_t(unsigned int, match->rx_len, CMD_SIZE(cmd));
0397 
0398             match->status = ioread32(&mem->status);
0399             memcpy_fromio(match->rx_buf, mem->payload, len);
0400         }
0401 
0402         if (match->rx_len > len)
0403             memset(match->rx_buf + len, 0, match->rx_len - len);
0404         complete(&match->done);
0405     }
0406     spin_unlock_irqrestore(&ch->rx_lock, flags);
0407 }
0408 
0409 static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
0410 {
0411     struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
0412     struct scpi_shared_mem __iomem *mem = ch->rx_payload;
0413     u32 cmd = 0;
0414 
0415     if (!scpi_info->is_legacy)
0416         cmd = ioread32(&mem->command);
0417 
0418     scpi_process_cmd(ch, cmd);
0419 }
0420 
0421 static void scpi_tx_prepare(struct mbox_client *c, void *msg)
0422 {
0423     unsigned long flags;
0424     struct scpi_xfer *t = msg;
0425     struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
0426     struct scpi_shared_mem __iomem *mem = ch->tx_payload;
0427 
0428     if (t->tx_buf) {
0429         if (scpi_info->is_legacy)
0430             memcpy_toio(ch->tx_payload, t->tx_buf, t->tx_len);
0431         else
0432             memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
0433     }
0434 
0435     if (t->rx_buf) {
0436         if (!(++ch->token))
0437             ++ch->token;
0438         t->cmd |= FIELD_PREP(CMD_TOKEN_ID_MASK, ch->token);
0439         spin_lock_irqsave(&ch->rx_lock, flags);
0440         list_add_tail(&t->node, &ch->rx_pending);
0441         spin_unlock_irqrestore(&ch->rx_lock, flags);
0442     }
0443 
0444     if (!scpi_info->is_legacy)
0445         iowrite32(t->cmd, &mem->command);
0446 }
0447 
0448 static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
0449 {
0450     struct scpi_xfer *t;
0451 
0452     mutex_lock(&ch->xfers_lock);
0453     if (list_empty(&ch->xfers_list)) {
0454         mutex_unlock(&ch->xfers_lock);
0455         return NULL;
0456     }
0457     t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
0458     list_del(&t->node);
0459     mutex_unlock(&ch->xfers_lock);
0460     return t;
0461 }
0462 
0463 static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
0464 {
0465     mutex_lock(&ch->xfers_lock);
0466     list_add_tail(&t->node, &ch->xfers_list);
0467     mutex_unlock(&ch->xfers_lock);
0468 }
0469 
0470 static int scpi_send_message(u8 idx, void *tx_buf, unsigned int tx_len,
0471                  void *rx_buf, unsigned int rx_len)
0472 {
0473     int ret;
0474     u8 chan;
0475     u8 cmd;
0476     struct scpi_xfer *msg;
0477     struct scpi_chan *scpi_chan;
0478 
0479     if (scpi_info->commands[idx] < 0)
0480         return -EOPNOTSUPP;
0481 
0482     cmd = scpi_info->commands[idx];
0483 
0484     if (scpi_info->is_legacy)
0485         chan = test_bit(cmd, scpi_info->cmd_priority) ? 1 : 0;
0486     else
0487         chan = atomic_inc_return(&scpi_info->next_chan) %
0488             scpi_info->num_chans;
0489     scpi_chan = scpi_info->channels + chan;
0490 
0491     msg = get_scpi_xfer(scpi_chan);
0492     if (!msg)
0493         return -ENOMEM;
0494 
0495     if (scpi_info->is_legacy) {
0496         msg->cmd = PACK_LEGACY_SCPI_CMD(cmd, tx_len);
0497         msg->slot = msg->cmd;
0498     } else {
0499         msg->slot = BIT(SCPI_SLOT);
0500         msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
0501     }
0502     msg->tx_buf = tx_buf;
0503     msg->tx_len = tx_len;
0504     msg->rx_buf = rx_buf;
0505     msg->rx_len = rx_len;
0506     reinit_completion(&msg->done);
0507 
0508     ret = mbox_send_message(scpi_chan->chan, msg);
0509     if (ret < 0 || !rx_buf)
0510         goto out;
0511 
0512     if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
0513         ret = -ETIMEDOUT;
0514     else
0515         /* first status word */
0516         ret = msg->status;
0517 out:
0518     if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
0519         scpi_process_cmd(scpi_chan, msg->cmd);
0520 
0521     put_scpi_xfer(msg, scpi_chan);
0522     /* SCPI error codes > 0, translate them to Linux scale*/
0523     return ret > 0 ? scpi_to_linux_errno(ret) : ret;
0524 }
0525 
0526 static u32 scpi_get_version(void)
0527 {
0528     return scpi_info->protocol_version;
0529 }
0530 
0531 static int
0532 scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
0533 {
0534     int ret;
0535     struct clk_get_info clk;
0536     __le16 le_clk_id = cpu_to_le16(clk_id);
0537 
0538     ret = scpi_send_message(CMD_GET_CLOCK_INFO, &le_clk_id,
0539                 sizeof(le_clk_id), &clk, sizeof(clk));
0540     if (!ret) {
0541         *min = le32_to_cpu(clk.min_rate);
0542         *max = le32_to_cpu(clk.max_rate);
0543     }
0544     return ret;
0545 }
0546 
0547 static unsigned long scpi_clk_get_val(u16 clk_id)
0548 {
0549     int ret;
0550     __le32 rate;
0551     __le16 le_clk_id = cpu_to_le16(clk_id);
0552 
0553     ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
0554                 sizeof(le_clk_id), &rate, sizeof(rate));
0555     if (ret)
0556         return 0;
0557 
0558     return le32_to_cpu(rate);
0559 }
0560 
0561 static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
0562 {
0563     int stat;
0564     struct clk_set_value clk = {
0565         .id = cpu_to_le16(clk_id),
0566         .rate = cpu_to_le32(rate)
0567     };
0568 
0569     return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
0570                  &stat, sizeof(stat));
0571 }
0572 
0573 static int legacy_scpi_clk_set_val(u16 clk_id, unsigned long rate)
0574 {
0575     int stat;
0576     struct legacy_clk_set_value clk = {
0577         .id = cpu_to_le16(clk_id),
0578         .rate = cpu_to_le32(rate)
0579     };
0580 
0581     return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
0582                  &stat, sizeof(stat));
0583 }
0584 
0585 static int scpi_dvfs_get_idx(u8 domain)
0586 {
0587     int ret;
0588     u8 dvfs_idx;
0589 
0590     ret = scpi_send_message(CMD_GET_DVFS, &domain, sizeof(domain),
0591                 &dvfs_idx, sizeof(dvfs_idx));
0592 
0593     return ret ? ret : dvfs_idx;
0594 }
0595 
0596 static int scpi_dvfs_set_idx(u8 domain, u8 index)
0597 {
0598     int stat;
0599     struct dvfs_set dvfs = {domain, index};
0600 
0601     return scpi_send_message(CMD_SET_DVFS, &dvfs, sizeof(dvfs),
0602                  &stat, sizeof(stat));
0603 }
0604 
0605 static int opp_cmp_func(const void *opp1, const void *opp2)
0606 {
0607     const struct scpi_opp *t1 = opp1, *t2 = opp2;
0608 
0609     return t1->freq - t2->freq;
0610 }
0611 
0612 static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
0613 {
0614     struct scpi_dvfs_info *info;
0615     struct scpi_opp *opp;
0616     struct dvfs_info buf;
0617     int ret, i;
0618 
0619     if (domain >= MAX_DVFS_DOMAINS)
0620         return ERR_PTR(-EINVAL);
0621 
0622     if (scpi_info->dvfs[domain])    /* data already populated */
0623         return scpi_info->dvfs[domain];
0624 
0625     ret = scpi_send_message(CMD_GET_DVFS_INFO, &domain, sizeof(domain),
0626                 &buf, sizeof(buf));
0627     if (ret)
0628         return ERR_PTR(ret);
0629 
0630     info = kmalloc(sizeof(*info), GFP_KERNEL);
0631     if (!info)
0632         return ERR_PTR(-ENOMEM);
0633 
0634     info->count = buf.opp_count;
0635     info->latency = le16_to_cpu(buf.latency) * 1000; /* uS to nS */
0636 
0637     info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL);
0638     if (!info->opps) {
0639         kfree(info);
0640         return ERR_PTR(-ENOMEM);
0641     }
0642 
0643     for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
0644         opp->freq = le32_to_cpu(buf.opps[i].freq);
0645         opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
0646     }
0647 
0648     sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
0649 
0650     scpi_info->dvfs[domain] = info;
0651     return info;
0652 }
0653 
0654 static int scpi_dev_domain_id(struct device *dev)
0655 {
0656     struct of_phandle_args clkspec;
0657 
0658     if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
0659                        0, &clkspec))
0660         return -EINVAL;
0661 
0662     return clkspec.args[0];
0663 }
0664 
0665 static struct scpi_dvfs_info *scpi_dvfs_info(struct device *dev)
0666 {
0667     int domain = scpi_dev_domain_id(dev);
0668 
0669     if (domain < 0)
0670         return ERR_PTR(domain);
0671 
0672     return scpi_dvfs_get_info(domain);
0673 }
0674 
0675 static int scpi_dvfs_get_transition_latency(struct device *dev)
0676 {
0677     struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
0678 
0679     if (IS_ERR(info))
0680         return PTR_ERR(info);
0681 
0682     return info->latency;
0683 }
0684 
0685 static int scpi_dvfs_add_opps_to_device(struct device *dev)
0686 {
0687     int idx, ret;
0688     struct scpi_opp *opp;
0689     struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
0690 
0691     if (IS_ERR(info))
0692         return PTR_ERR(info);
0693 
0694     if (!info->opps)
0695         return -EIO;
0696 
0697     for (opp = info->opps, idx = 0; idx < info->count; idx++, opp++) {
0698         ret = dev_pm_opp_add(dev, opp->freq, opp->m_volt * 1000);
0699         if (ret) {
0700             dev_warn(dev, "failed to add opp %uHz %umV\n",
0701                  opp->freq, opp->m_volt);
0702             while (idx-- > 0)
0703                 dev_pm_opp_remove(dev, (--opp)->freq);
0704             return ret;
0705         }
0706     }
0707     return 0;
0708 }
0709 
0710 static int scpi_sensor_get_capability(u16 *sensors)
0711 {
0712     __le16 cap;
0713     int ret;
0714 
0715     ret = scpi_send_message(CMD_SENSOR_CAPABILITIES, NULL, 0, &cap,
0716                 sizeof(cap));
0717     if (!ret)
0718         *sensors = le16_to_cpu(cap);
0719 
0720     return ret;
0721 }
0722 
0723 static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
0724 {
0725     __le16 id = cpu_to_le16(sensor_id);
0726     struct _scpi_sensor_info _info;
0727     int ret;
0728 
0729     ret = scpi_send_message(CMD_SENSOR_INFO, &id, sizeof(id),
0730                 &_info, sizeof(_info));
0731     if (!ret) {
0732         memcpy(info, &_info, sizeof(*info));
0733         info->sensor_id = le16_to_cpu(_info.sensor_id);
0734     }
0735 
0736     return ret;
0737 }
0738 
0739 static int scpi_sensor_get_value(u16 sensor, u64 *val)
0740 {
0741     __le16 id = cpu_to_le16(sensor);
0742     __le64 value;
0743     int ret;
0744 
0745     ret = scpi_send_message(CMD_SENSOR_VALUE, &id, sizeof(id),
0746                 &value, sizeof(value));
0747     if (ret)
0748         return ret;
0749 
0750     if (scpi_info->is_legacy)
0751         /* only 32-bits supported, upper 32 bits can be junk */
0752         *val = le32_to_cpup((__le32 *)&value);
0753     else
0754         *val = le64_to_cpu(value);
0755 
0756     return 0;
0757 }
0758 
0759 static int scpi_device_get_power_state(u16 dev_id)
0760 {
0761     int ret;
0762     u8 pstate;
0763     __le16 id = cpu_to_le16(dev_id);
0764 
0765     ret = scpi_send_message(CMD_GET_DEVICE_PWR_STATE, &id,
0766                 sizeof(id), &pstate, sizeof(pstate));
0767     return ret ? ret : pstate;
0768 }
0769 
0770 static int scpi_device_set_power_state(u16 dev_id, u8 pstate)
0771 {
0772     int stat;
0773     struct dev_pstate_set dev_set = {
0774         .dev_id = cpu_to_le16(dev_id),
0775         .pstate = pstate,
0776     };
0777 
0778     return scpi_send_message(CMD_SET_DEVICE_PWR_STATE, &dev_set,
0779                  sizeof(dev_set), &stat, sizeof(stat));
0780 }
0781 
0782 static struct scpi_ops scpi_ops = {
0783     .get_version = scpi_get_version,
0784     .clk_get_range = scpi_clk_get_range,
0785     .clk_get_val = scpi_clk_get_val,
0786     .clk_set_val = scpi_clk_set_val,
0787     .dvfs_get_idx = scpi_dvfs_get_idx,
0788     .dvfs_set_idx = scpi_dvfs_set_idx,
0789     .dvfs_get_info = scpi_dvfs_get_info,
0790     .device_domain_id = scpi_dev_domain_id,
0791     .get_transition_latency = scpi_dvfs_get_transition_latency,
0792     .add_opps_to_device = scpi_dvfs_add_opps_to_device,
0793     .sensor_get_capability = scpi_sensor_get_capability,
0794     .sensor_get_info = scpi_sensor_get_info,
0795     .sensor_get_value = scpi_sensor_get_value,
0796     .device_get_power_state = scpi_device_get_power_state,
0797     .device_set_power_state = scpi_device_set_power_state,
0798 };
0799 
0800 struct scpi_ops *get_scpi_ops(void)
0801 {
0802     return scpi_info ? scpi_info->scpi_ops : NULL;
0803 }
0804 EXPORT_SYMBOL_GPL(get_scpi_ops);
0805 
0806 static int scpi_init_versions(struct scpi_drvinfo *info)
0807 {
0808     int ret;
0809     struct scp_capabilities caps;
0810 
0811     ret = scpi_send_message(CMD_SCPI_CAPABILITIES, NULL, 0,
0812                 &caps, sizeof(caps));
0813     if (!ret) {
0814         info->protocol_version = le32_to_cpu(caps.protocol_version);
0815         info->firmware_version = le32_to_cpu(caps.platform_version);
0816     }
0817     /* Ignore error if not implemented */
0818     if (info->is_legacy && ret == -EOPNOTSUPP)
0819         return 0;
0820 
0821     return ret;
0822 }
0823 
0824 static ssize_t protocol_version_show(struct device *dev,
0825                      struct device_attribute *attr, char *buf)
0826 {
0827     struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
0828 
0829     return sprintf(buf, "%lu.%lu\n",
0830         FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version),
0831         FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version));
0832 }
0833 static DEVICE_ATTR_RO(protocol_version);
0834 
0835 static ssize_t firmware_version_show(struct device *dev,
0836                      struct device_attribute *attr, char *buf)
0837 {
0838     struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
0839 
0840     return sprintf(buf, "%lu.%lu.%lu\n",
0841         FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version),
0842         FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version),
0843         FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version));
0844 }
0845 static DEVICE_ATTR_RO(firmware_version);
0846 
0847 static struct attribute *versions_attrs[] = {
0848     &dev_attr_firmware_version.attr,
0849     &dev_attr_protocol_version.attr,
0850     NULL,
0851 };
0852 ATTRIBUTE_GROUPS(versions);
0853 
0854 static void scpi_free_channels(void *data)
0855 {
0856     struct scpi_drvinfo *info = data;
0857     int i;
0858 
0859     for (i = 0; i < info->num_chans; i++)
0860         mbox_free_channel(info->channels[i].chan);
0861 }
0862 
0863 static int scpi_remove(struct platform_device *pdev)
0864 {
0865     int i;
0866     struct scpi_drvinfo *info = platform_get_drvdata(pdev);
0867 
0868     scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
0869 
0870     for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
0871         kfree(info->dvfs[i]->opps);
0872         kfree(info->dvfs[i]);
0873     }
0874 
0875     return 0;
0876 }
0877 
0878 #define MAX_SCPI_XFERS      10
0879 static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
0880 {
0881     int i;
0882     struct scpi_xfer *xfers;
0883 
0884     xfers = devm_kcalloc(dev, MAX_SCPI_XFERS, sizeof(*xfers), GFP_KERNEL);
0885     if (!xfers)
0886         return -ENOMEM;
0887 
0888     ch->xfers = xfers;
0889     for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++) {
0890         init_completion(&xfers->done);
0891         list_add_tail(&xfers->node, &ch->xfers_list);
0892     }
0893 
0894     return 0;
0895 }
0896 
0897 static const struct of_device_id legacy_scpi_of_match[] = {
0898     {.compatible = "arm,scpi-pre-1.0"},
0899     {},
0900 };
0901 
0902 static const struct of_device_id shmem_of_match[] __maybe_unused = {
0903     { .compatible = "amlogic,meson-gxbb-scp-shmem", },
0904     { .compatible = "amlogic,meson-axg-scp-shmem", },
0905     { .compatible = "arm,juno-scp-shmem", },
0906     { .compatible = "arm,scp-shmem", },
0907     { }
0908 };
0909 
0910 static int scpi_probe(struct platform_device *pdev)
0911 {
0912     int count, idx, ret;
0913     struct resource res;
0914     struct device *dev = &pdev->dev;
0915     struct device_node *np = dev->of_node;
0916     struct scpi_drvinfo *scpi_drvinfo;
0917 
0918     scpi_drvinfo = devm_kzalloc(dev, sizeof(*scpi_drvinfo), GFP_KERNEL);
0919     if (!scpi_drvinfo)
0920         return -ENOMEM;
0921 
0922     if (of_match_device(legacy_scpi_of_match, &pdev->dev))
0923         scpi_drvinfo->is_legacy = true;
0924 
0925     count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
0926     if (count < 0) {
0927         dev_err(dev, "no mboxes property in '%pOF'\n", np);
0928         return -ENODEV;
0929     }
0930 
0931     scpi_drvinfo->channels =
0932         devm_kcalloc(dev, count, sizeof(struct scpi_chan), GFP_KERNEL);
0933     if (!scpi_drvinfo->channels)
0934         return -ENOMEM;
0935 
0936     ret = devm_add_action(dev, scpi_free_channels, scpi_drvinfo);
0937     if (ret)
0938         return ret;
0939 
0940     for (; scpi_drvinfo->num_chans < count; scpi_drvinfo->num_chans++) {
0941         resource_size_t size;
0942         int idx = scpi_drvinfo->num_chans;
0943         struct scpi_chan *pchan = scpi_drvinfo->channels + idx;
0944         struct mbox_client *cl = &pchan->cl;
0945         struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
0946 
0947         if (!of_match_node(shmem_of_match, shmem))
0948             return -ENXIO;
0949 
0950         ret = of_address_to_resource(shmem, 0, &res);
0951         of_node_put(shmem);
0952         if (ret) {
0953             dev_err(dev, "failed to get SCPI payload mem resource\n");
0954             return ret;
0955         }
0956 
0957         size = resource_size(&res);
0958         pchan->rx_payload = devm_ioremap(dev, res.start, size);
0959         if (!pchan->rx_payload) {
0960             dev_err(dev, "failed to ioremap SCPI payload\n");
0961             return -EADDRNOTAVAIL;
0962         }
0963         pchan->tx_payload = pchan->rx_payload + (size >> 1);
0964 
0965         cl->dev = dev;
0966         cl->rx_callback = scpi_handle_remote_msg;
0967         cl->tx_prepare = scpi_tx_prepare;
0968         cl->tx_block = true;
0969         cl->tx_tout = 20;
0970         cl->knows_txdone = false; /* controller can't ack */
0971 
0972         INIT_LIST_HEAD(&pchan->rx_pending);
0973         INIT_LIST_HEAD(&pchan->xfers_list);
0974         spin_lock_init(&pchan->rx_lock);
0975         mutex_init(&pchan->xfers_lock);
0976 
0977         ret = scpi_alloc_xfer_list(dev, pchan);
0978         if (!ret) {
0979             pchan->chan = mbox_request_channel(cl, idx);
0980             if (!IS_ERR(pchan->chan))
0981                 continue;
0982             ret = PTR_ERR(pchan->chan);
0983             if (ret != -EPROBE_DEFER)
0984                 dev_err(dev, "failed to get channel%d err %d\n",
0985                     idx, ret);
0986         }
0987         return ret;
0988     }
0989 
0990     scpi_drvinfo->commands = scpi_std_commands;
0991 
0992     platform_set_drvdata(pdev, scpi_drvinfo);
0993 
0994     if (scpi_drvinfo->is_legacy) {
0995         /* Replace with legacy variants */
0996         scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
0997         scpi_drvinfo->commands = scpi_legacy_commands;
0998 
0999         /* Fill priority bitmap */
1000         for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
1001             set_bit(legacy_hpriority_cmds[idx],
1002                 scpi_drvinfo->cmd_priority);
1003     }
1004 
1005     scpi_info = scpi_drvinfo;
1006 
1007     ret = scpi_init_versions(scpi_drvinfo);
1008     if (ret) {
1009         dev_err(dev, "incorrect or no SCP firmware found\n");
1010         scpi_info = NULL;
1011         return ret;
1012     }
1013 
1014     if (scpi_drvinfo->is_legacy && !scpi_drvinfo->protocol_version &&
1015         !scpi_drvinfo->firmware_version)
1016         dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n");
1017     else
1018         dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
1019              FIELD_GET(PROTO_REV_MAJOR_MASK,
1020                    scpi_drvinfo->protocol_version),
1021              FIELD_GET(PROTO_REV_MINOR_MASK,
1022                    scpi_drvinfo->protocol_version),
1023              FIELD_GET(FW_REV_MAJOR_MASK,
1024                    scpi_drvinfo->firmware_version),
1025              FIELD_GET(FW_REV_MINOR_MASK,
1026                    scpi_drvinfo->firmware_version),
1027              FIELD_GET(FW_REV_PATCH_MASK,
1028                    scpi_drvinfo->firmware_version));
1029 
1030     scpi_drvinfo->scpi_ops = &scpi_ops;
1031 
1032     ret = devm_of_platform_populate(dev);
1033     if (ret)
1034         scpi_info = NULL;
1035 
1036     return ret;
1037 }
1038 
1039 static const struct of_device_id scpi_of_match[] = {
1040     {.compatible = "arm,scpi"},
1041     {.compatible = "arm,scpi-pre-1.0"},
1042     {},
1043 };
1044 
1045 MODULE_DEVICE_TABLE(of, scpi_of_match);
1046 
1047 static struct platform_driver scpi_driver = {
1048     .driver = {
1049         .name = "scpi_protocol",
1050         .of_match_table = scpi_of_match,
1051         .dev_groups = versions_groups,
1052     },
1053     .probe = scpi_probe,
1054     .remove = scpi_remove,
1055 };
1056 module_platform_driver(scpi_driver);
1057 
1058 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1059 MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
1060 MODULE_LICENSE("GPL v2");