xref: /linux/drivers/firmware/arm_scpi.c (revision 32471d84a487c7fd74532bc96be56f8028cf4a3f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * System Control and Power Interface (SCPI) Message Protocol driver
4  *
5  * SCPI Message Protocol is used between the System Control Processor(SCP)
6  * and the Application Processors(AP). The Message Handling Unit(MHU)
7  * provides a mechanism for inter-processor communication between SCP's
8  * Cortex M3 and AP.
9  *
10  * SCP offers control and management of the core/cluster power states,
11  * various power domain DVFS including the core/cluster, certain system
12  * clocks configuration, thermal sensors and many others.
13  *
14  * Copyright (C) 2015 ARM Ltd.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/bitmap.h>
20 #include <linux/bitfield.h>
21 #include <linux/cleanup.h>
22 #include <linux/device.h>
23 #include <linux/err.h>
24 #include <linux/export.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/mailbox_client.h>
29 #include <linux/module.h>
30 #include <linux/of.h>
31 #include <linux/of_address.h>
32 #include <linux/of_platform.h>
33 #include <linux/platform_device.h>
34 #include <linux/printk.h>
35 #include <linux/property.h>
36 #include <linux/pm_opp.h>
37 #include <linux/scpi_protocol.h>
38 #include <linux/slab.h>
39 #include <linux/sort.h>
40 #include <linux/spinlock.h>
41 
42 #define CMD_ID_MASK		GENMASK(6, 0)
43 #define CMD_TOKEN_ID_MASK	GENMASK(15, 8)
44 #define CMD_DATA_SIZE_MASK	GENMASK(24, 16)
45 #define CMD_LEGACY_DATA_SIZE_MASK	GENMASK(28, 20)
46 #define PACK_SCPI_CMD(cmd_id, tx_sz)		\
47 	(FIELD_PREP(CMD_ID_MASK, cmd_id) |	\
48 	FIELD_PREP(CMD_DATA_SIZE_MASK, tx_sz))
49 #define PACK_LEGACY_SCPI_CMD(cmd_id, tx_sz)	\
50 	(FIELD_PREP(CMD_ID_MASK, cmd_id) |	\
51 	FIELD_PREP(CMD_LEGACY_DATA_SIZE_MASK, tx_sz))
52 
53 #define CMD_SIZE(cmd)	FIELD_GET(CMD_DATA_SIZE_MASK, cmd)
54 #define CMD_UNIQ_MASK	(CMD_TOKEN_ID_MASK | CMD_ID_MASK)
55 #define CMD_XTRACT_UNIQ(cmd)	((cmd) & CMD_UNIQ_MASK)
56 
57 #define SCPI_SLOT		0
58 
59 #define MAX_DVFS_DOMAINS	8
60 #define MAX_DVFS_OPPS		16
61 
62 #define PROTO_REV_MAJOR_MASK	GENMASK(31, 16)
63 #define PROTO_REV_MINOR_MASK	GENMASK(15, 0)
64 
65 #define FW_REV_MAJOR_MASK	GENMASK(31, 24)
66 #define FW_REV_MINOR_MASK	GENMASK(23, 16)
67 #define FW_REV_PATCH_MASK	GENMASK(15, 0)
68 
69 #define MAX_RX_TIMEOUT		(msecs_to_jiffies(30))
70 
71 enum scpi_error_codes {
72 	SCPI_SUCCESS = 0, /* Success */
73 	SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */
74 	SCPI_ERR_ALIGN = 2, /* Invalid alignment */
75 	SCPI_ERR_SIZE = 3, /* Invalid size */
76 	SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */
77 	SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */
78 	SCPI_ERR_RANGE = 6, /* Value out of range */
79 	SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */
80 	SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */
81 	SCPI_ERR_PWRSTATE = 9, /* Invalid power state */
82 	SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */
83 	SCPI_ERR_DEVICE = 11, /* Device error */
84 	SCPI_ERR_BUSY = 12, /* Device busy */
85 	SCPI_ERR_MAX
86 };
87 
88 /* SCPI Standard commands */
89 enum scpi_std_cmd {
90 	SCPI_CMD_INVALID		= 0x00,
91 	SCPI_CMD_SCPI_READY		= 0x01,
92 	SCPI_CMD_SCPI_CAPABILITIES	= 0x02,
93 	SCPI_CMD_SET_CSS_PWR_STATE	= 0x03,
94 	SCPI_CMD_GET_CSS_PWR_STATE	= 0x04,
95 	SCPI_CMD_SET_SYS_PWR_STATE	= 0x05,
96 	SCPI_CMD_SET_CPU_TIMER		= 0x06,
97 	SCPI_CMD_CANCEL_CPU_TIMER	= 0x07,
98 	SCPI_CMD_DVFS_CAPABILITIES	= 0x08,
99 	SCPI_CMD_GET_DVFS_INFO		= 0x09,
100 	SCPI_CMD_SET_DVFS		= 0x0a,
101 	SCPI_CMD_GET_DVFS		= 0x0b,
102 	SCPI_CMD_GET_DVFS_STAT		= 0x0c,
103 	SCPI_CMD_CLOCK_CAPABILITIES	= 0x0d,
104 	SCPI_CMD_GET_CLOCK_INFO		= 0x0e,
105 	SCPI_CMD_SET_CLOCK_VALUE	= 0x0f,
106 	SCPI_CMD_GET_CLOCK_VALUE	= 0x10,
107 	SCPI_CMD_PSU_CAPABILITIES	= 0x11,
108 	SCPI_CMD_GET_PSU_INFO		= 0x12,
109 	SCPI_CMD_SET_PSU		= 0x13,
110 	SCPI_CMD_GET_PSU		= 0x14,
111 	SCPI_CMD_SENSOR_CAPABILITIES	= 0x15,
112 	SCPI_CMD_SENSOR_INFO		= 0x16,
113 	SCPI_CMD_SENSOR_VALUE		= 0x17,
114 	SCPI_CMD_SENSOR_CFG_PERIODIC	= 0x18,
115 	SCPI_CMD_SENSOR_CFG_BOUNDS	= 0x19,
116 	SCPI_CMD_SENSOR_ASYNC_VALUE	= 0x1a,
117 	SCPI_CMD_SET_DEVICE_PWR_STATE	= 0x1b,
118 	SCPI_CMD_GET_DEVICE_PWR_STATE	= 0x1c,
119 	SCPI_CMD_COUNT
120 };
121 
122 /* SCPI Legacy Commands */
123 enum legacy_scpi_std_cmd {
124 	LEGACY_SCPI_CMD_INVALID			= 0x00,
125 	LEGACY_SCPI_CMD_SCPI_READY		= 0x01,
126 	LEGACY_SCPI_CMD_SCPI_CAPABILITIES	= 0x02,
127 	LEGACY_SCPI_CMD_EVENT			= 0x03,
128 	LEGACY_SCPI_CMD_SET_CSS_PWR_STATE	= 0x04,
129 	LEGACY_SCPI_CMD_GET_CSS_PWR_STATE	= 0x05,
130 	LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT	= 0x06,
131 	LEGACY_SCPI_CMD_GET_PWR_STATE_STAT	= 0x07,
132 	LEGACY_SCPI_CMD_SYS_PWR_STATE		= 0x08,
133 	LEGACY_SCPI_CMD_L2_READY		= 0x09,
134 	LEGACY_SCPI_CMD_SET_AP_TIMER		= 0x0a,
135 	LEGACY_SCPI_CMD_CANCEL_AP_TIME		= 0x0b,
136 	LEGACY_SCPI_CMD_DVFS_CAPABILITIES	= 0x0c,
137 	LEGACY_SCPI_CMD_GET_DVFS_INFO		= 0x0d,
138 	LEGACY_SCPI_CMD_SET_DVFS		= 0x0e,
139 	LEGACY_SCPI_CMD_GET_DVFS		= 0x0f,
140 	LEGACY_SCPI_CMD_GET_DVFS_STAT		= 0x10,
141 	LEGACY_SCPI_CMD_SET_RTC			= 0x11,
142 	LEGACY_SCPI_CMD_GET_RTC			= 0x12,
143 	LEGACY_SCPI_CMD_CLOCK_CAPABILITIES	= 0x13,
144 	LEGACY_SCPI_CMD_SET_CLOCK_INDEX		= 0x14,
145 	LEGACY_SCPI_CMD_SET_CLOCK_VALUE		= 0x15,
146 	LEGACY_SCPI_CMD_GET_CLOCK_VALUE		= 0x16,
147 	LEGACY_SCPI_CMD_PSU_CAPABILITIES	= 0x17,
148 	LEGACY_SCPI_CMD_SET_PSU			= 0x18,
149 	LEGACY_SCPI_CMD_GET_PSU			= 0x19,
150 	LEGACY_SCPI_CMD_SENSOR_CAPABILITIES	= 0x1a,
151 	LEGACY_SCPI_CMD_SENSOR_INFO		= 0x1b,
152 	LEGACY_SCPI_CMD_SENSOR_VALUE		= 0x1c,
153 	LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC	= 0x1d,
154 	LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS	= 0x1e,
155 	LEGACY_SCPI_CMD_SENSOR_ASYNC_VALUE	= 0x1f,
156 	LEGACY_SCPI_CMD_COUNT
157 };
158 
159 /* List all commands that are required to go through the high priority link */
160 static int legacy_hpriority_cmds[] = {
161 	LEGACY_SCPI_CMD_GET_CSS_PWR_STATE,
162 	LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT,
163 	LEGACY_SCPI_CMD_GET_PWR_STATE_STAT,
164 	LEGACY_SCPI_CMD_SET_DVFS,
165 	LEGACY_SCPI_CMD_GET_DVFS,
166 	LEGACY_SCPI_CMD_SET_RTC,
167 	LEGACY_SCPI_CMD_GET_RTC,
168 	LEGACY_SCPI_CMD_SET_CLOCK_INDEX,
169 	LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
170 	LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
171 	LEGACY_SCPI_CMD_SET_PSU,
172 	LEGACY_SCPI_CMD_GET_PSU,
173 	LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC,
174 	LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS,
175 };
176 
177 /* List all commands used by this driver, used as indexes */
178 enum scpi_drv_cmds {
179 	CMD_SCPI_CAPABILITIES = 0,
180 	CMD_GET_CLOCK_INFO,
181 	CMD_GET_CLOCK_VALUE,
182 	CMD_SET_CLOCK_VALUE,
183 	CMD_GET_DVFS,
184 	CMD_SET_DVFS,
185 	CMD_GET_DVFS_INFO,
186 	CMD_SENSOR_CAPABILITIES,
187 	CMD_SENSOR_INFO,
188 	CMD_SENSOR_VALUE,
189 	CMD_SET_DEVICE_PWR_STATE,
190 	CMD_GET_DEVICE_PWR_STATE,
191 	CMD_MAX_COUNT,
192 };
193 
194 static int scpi_std_commands[CMD_MAX_COUNT] = {
195 	SCPI_CMD_SCPI_CAPABILITIES,
196 	SCPI_CMD_GET_CLOCK_INFO,
197 	SCPI_CMD_GET_CLOCK_VALUE,
198 	SCPI_CMD_SET_CLOCK_VALUE,
199 	SCPI_CMD_GET_DVFS,
200 	SCPI_CMD_SET_DVFS,
201 	SCPI_CMD_GET_DVFS_INFO,
202 	SCPI_CMD_SENSOR_CAPABILITIES,
203 	SCPI_CMD_SENSOR_INFO,
204 	SCPI_CMD_SENSOR_VALUE,
205 	SCPI_CMD_SET_DEVICE_PWR_STATE,
206 	SCPI_CMD_GET_DEVICE_PWR_STATE,
207 };
208 
209 static int scpi_legacy_commands[CMD_MAX_COUNT] = {
210 	LEGACY_SCPI_CMD_SCPI_CAPABILITIES,
211 	-1, /* GET_CLOCK_INFO */
212 	LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
213 	LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
214 	LEGACY_SCPI_CMD_GET_DVFS,
215 	LEGACY_SCPI_CMD_SET_DVFS,
216 	LEGACY_SCPI_CMD_GET_DVFS_INFO,
217 	LEGACY_SCPI_CMD_SENSOR_CAPABILITIES,
218 	LEGACY_SCPI_CMD_SENSOR_INFO,
219 	LEGACY_SCPI_CMD_SENSOR_VALUE,
220 	-1, /* SET_DEVICE_PWR_STATE */
221 	-1, /* GET_DEVICE_PWR_STATE */
222 };
223 
224 struct scpi_xfer {
225 	u32 slot; /* has to be first element */
226 	u32 cmd;
227 	u32 status;
228 	const void *tx_buf;
229 	void *rx_buf;
230 	unsigned int tx_len;
231 	unsigned int rx_len;
232 	struct list_head node;
233 	struct completion done;
234 };
235 
236 struct scpi_chan {
237 	struct mbox_client cl;
238 	struct mbox_chan *chan;
239 	void __iomem *tx_payload;
240 	void __iomem *rx_payload;
241 	struct list_head rx_pending;
242 	struct list_head xfers_list;
243 	struct scpi_xfer *xfers;
244 	spinlock_t rx_lock; /* locking for the rx pending list */
245 	struct mutex xfers_lock;
246 	u8 token;
247 };
248 
249 struct scpi_drvinfo {
250 	u32 protocol_version;
251 	u32 firmware_version;
252 	bool is_legacy;
253 	int num_chans;
254 	int *commands;
255 	DECLARE_BITMAP(cmd_priority, LEGACY_SCPI_CMD_COUNT);
256 	atomic_t next_chan;
257 	struct scpi_ops *scpi_ops;
258 	struct scpi_chan *channels;
259 	struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS];
260 };
261 
262 /*
263  * The SCP firmware only executes in little-endian mode, so any buffers
264  * shared through SCPI should have their contents converted to little-endian
265  */
266 struct scpi_shared_mem {
267 	__le32 command;
268 	__le32 status;
269 	u8 payload[];
270 } __packed;
271 
272 struct legacy_scpi_shared_mem {
273 	__le32 status;
274 	u8 payload[];
275 } __packed;
276 
277 struct scp_capabilities {
278 	__le32 protocol_version;
279 	__le32 event_version;
280 	__le32 platform_version;
281 	__le32 commands[4];
282 } __packed;
283 
284 struct clk_get_info {
285 	__le16 id;
286 	__le16 flags;
287 	__le32 min_rate;
288 	__le32 max_rate;
289 	u8 name[20];
290 } __packed;
291 
292 struct clk_set_value {
293 	__le16 id;
294 	__le16 reserved;
295 	__le32 rate;
296 } __packed;
297 
298 struct legacy_clk_set_value {
299 	__le32 rate;
300 	__le16 id;
301 	__le16 reserved;
302 } __packed;
303 
304 struct dvfs_info {
305 	u8 domain;
306 	u8 opp_count;
307 	__le16 latency;
308 	struct {
309 		__le32 freq;
310 		__le32 m_volt;
311 	} opps[MAX_DVFS_OPPS];
312 } __packed;
313 
314 struct dvfs_set {
315 	u8 domain;
316 	u8 index;
317 } __packed;
318 
319 struct _scpi_sensor_info {
320 	__le16 sensor_id;
321 	u8 class;
322 	u8 trigger_type;
323 	char name[20];
324 };
325 
326 struct dev_pstate_set {
327 	__le16 dev_id;
328 	u8 pstate;
329 } __packed;
330 
331 static struct scpi_drvinfo *scpi_info;
332 
333 static int scpi_linux_errmap[SCPI_ERR_MAX] = {
334 	/* better than switch case as long as return value is continuous */
335 	0, /* SCPI_SUCCESS */
336 	-EINVAL, /* SCPI_ERR_PARAM */
337 	-ENOEXEC, /* SCPI_ERR_ALIGN */
338 	-EMSGSIZE, /* SCPI_ERR_SIZE */
339 	-EINVAL, /* SCPI_ERR_HANDLER */
340 	-EACCES, /* SCPI_ERR_ACCESS */
341 	-ERANGE, /* SCPI_ERR_RANGE */
342 	-ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
343 	-ENOMEM, /* SCPI_ERR_NOMEM */
344 	-EINVAL, /* SCPI_ERR_PWRSTATE */
345 	-EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
346 	-EIO, /* SCPI_ERR_DEVICE */
347 	-EBUSY, /* SCPI_ERR_BUSY */
348 };
349 
350 static inline int scpi_to_linux_errno(int errno)
351 {
352 	if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
353 		return scpi_linux_errmap[errno];
354 	return -EIO;
355 }
356 
357 static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
358 {
359 	unsigned long flags;
360 	struct scpi_xfer *t, *match = NULL;
361 
362 	spin_lock_irqsave(&ch->rx_lock, flags);
363 	if (list_empty(&ch->rx_pending)) {
364 		spin_unlock_irqrestore(&ch->rx_lock, flags);
365 		return;
366 	}
367 
368 	/* Command type is not replied by the SCP Firmware in legacy Mode
369 	 * We should consider that command is the head of pending RX commands
370 	 * if the list is not empty. In TX only mode, the list would be empty.
371 	 */
372 	if (scpi_info->is_legacy) {
373 		match = list_first_entry(&ch->rx_pending, struct scpi_xfer,
374 					 node);
375 		list_del(&match->node);
376 	} else {
377 		list_for_each_entry(t, &ch->rx_pending, node)
378 			if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
379 				list_del(&t->node);
380 				match = t;
381 				break;
382 			}
383 	}
384 	/* check if wait_for_completion is in progress or timed-out */
385 	if (match && !completion_done(&match->done)) {
386 		unsigned int len;
387 
388 		if (scpi_info->is_legacy) {
389 			struct legacy_scpi_shared_mem __iomem *mem =
390 							ch->rx_payload;
391 
392 			/* RX Length is not replied by the legacy Firmware */
393 			len = match->rx_len;
394 
395 			match->status = ioread32(&mem->status);
396 			memcpy_fromio(match->rx_buf, mem->payload, len);
397 		} else {
398 			struct scpi_shared_mem __iomem *mem = ch->rx_payload;
399 
400 			len = min_t(unsigned int, match->rx_len, CMD_SIZE(cmd));
401 
402 			match->status = ioread32(&mem->status);
403 			memcpy_fromio(match->rx_buf, mem->payload, len);
404 		}
405 
406 		if (match->rx_len > len)
407 			memset(match->rx_buf + len, 0, match->rx_len - len);
408 		complete(&match->done);
409 	}
410 	spin_unlock_irqrestore(&ch->rx_lock, flags);
411 }
412 
413 static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
414 {
415 	struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
416 	struct scpi_shared_mem __iomem *mem = ch->rx_payload;
417 	u32 cmd = 0;
418 
419 	if (!scpi_info->is_legacy)
420 		cmd = ioread32(&mem->command);
421 
422 	scpi_process_cmd(ch, cmd);
423 }
424 
425 static void scpi_tx_prepare(struct mbox_client *c, void *msg)
426 {
427 	unsigned long flags;
428 	struct scpi_xfer *t = msg;
429 	struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
430 	struct scpi_shared_mem __iomem *mem = ch->tx_payload;
431 
432 	if (t->tx_buf) {
433 		if (scpi_info->is_legacy)
434 			memcpy_toio(ch->tx_payload, t->tx_buf, t->tx_len);
435 		else
436 			memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
437 	}
438 
439 	if (t->rx_buf) {
440 		if (!(++ch->token))
441 			++ch->token;
442 		t->cmd |= FIELD_PREP(CMD_TOKEN_ID_MASK, ch->token);
443 		spin_lock_irqsave(&ch->rx_lock, flags);
444 		list_add_tail(&t->node, &ch->rx_pending);
445 		spin_unlock_irqrestore(&ch->rx_lock, flags);
446 	}
447 
448 	if (!scpi_info->is_legacy)
449 		iowrite32(t->cmd, &mem->command);
450 }
451 
452 static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
453 {
454 	struct scpi_xfer *t;
455 
456 	mutex_lock(&ch->xfers_lock);
457 	if (list_empty(&ch->xfers_list)) {
458 		mutex_unlock(&ch->xfers_lock);
459 		return NULL;
460 	}
461 	t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
462 	list_del(&t->node);
463 	mutex_unlock(&ch->xfers_lock);
464 	return t;
465 }
466 
467 static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
468 {
469 	mutex_lock(&ch->xfers_lock);
470 	list_add_tail(&t->node, &ch->xfers_list);
471 	mutex_unlock(&ch->xfers_lock);
472 }
473 
474 static int scpi_send_message(u8 idx, void *tx_buf, unsigned int tx_len,
475 			     void *rx_buf, unsigned int rx_len)
476 {
477 	int ret;
478 	u8 chan;
479 	u8 cmd;
480 	struct scpi_xfer *msg;
481 	struct scpi_chan *scpi_chan;
482 
483 	if (scpi_info->commands[idx] < 0)
484 		return -EOPNOTSUPP;
485 
486 	cmd = scpi_info->commands[idx];
487 
488 	if (scpi_info->is_legacy)
489 		chan = test_bit(cmd, scpi_info->cmd_priority) ? 1 : 0;
490 	else
491 		chan = atomic_inc_return(&scpi_info->next_chan) %
492 			scpi_info->num_chans;
493 	scpi_chan = scpi_info->channels + chan;
494 
495 	msg = get_scpi_xfer(scpi_chan);
496 	if (!msg)
497 		return -ENOMEM;
498 
499 	if (scpi_info->is_legacy) {
500 		msg->cmd = PACK_LEGACY_SCPI_CMD(cmd, tx_len);
501 		msg->slot = msg->cmd;
502 	} else {
503 		msg->slot = BIT(SCPI_SLOT);
504 		msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
505 	}
506 	msg->tx_buf = tx_buf;
507 	msg->tx_len = tx_len;
508 	msg->rx_buf = rx_buf;
509 	msg->rx_len = rx_len;
510 	reinit_completion(&msg->done);
511 
512 	ret = mbox_send_message(scpi_chan->chan, msg);
513 	if (ret < 0 || !rx_buf)
514 		goto out;
515 
516 	if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
517 		ret = -ETIMEDOUT;
518 	else
519 		/* first status word */
520 		ret = msg->status;
521 out:
522 	if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
523 		scpi_process_cmd(scpi_chan, msg->cmd);
524 
525 	put_scpi_xfer(msg, scpi_chan);
526 	/* SCPI error codes > 0, translate them to Linux scale*/
527 	return ret > 0 ? scpi_to_linux_errno(ret) : ret;
528 }
529 
530 static u32 scpi_get_version(void)
531 {
532 	return scpi_info->protocol_version;
533 }
534 
535 static int
536 scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
537 {
538 	int ret;
539 	struct clk_get_info clk;
540 	__le16 le_clk_id = cpu_to_le16(clk_id);
541 
542 	ret = scpi_send_message(CMD_GET_CLOCK_INFO, &le_clk_id,
543 				sizeof(le_clk_id), &clk, sizeof(clk));
544 	if (!ret) {
545 		*min = le32_to_cpu(clk.min_rate);
546 		*max = le32_to_cpu(clk.max_rate);
547 	}
548 	return ret;
549 }
550 
551 static unsigned long scpi_clk_get_val(u16 clk_id)
552 {
553 	int ret;
554 	__le32 rate;
555 	__le16 le_clk_id = cpu_to_le16(clk_id);
556 
557 	ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
558 				sizeof(le_clk_id), &rate, sizeof(rate));
559 	if (ret)
560 		return 0;
561 
562 	return le32_to_cpu(rate);
563 }
564 
565 static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
566 {
567 	int stat;
568 	struct clk_set_value clk = {
569 		.id = cpu_to_le16(clk_id),
570 		.rate = cpu_to_le32(rate)
571 	};
572 
573 	return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
574 				 &stat, sizeof(stat));
575 }
576 
577 static int legacy_scpi_clk_set_val(u16 clk_id, unsigned long rate)
578 {
579 	int stat;
580 	struct legacy_clk_set_value clk = {
581 		.id = cpu_to_le16(clk_id),
582 		.rate = cpu_to_le32(rate)
583 	};
584 
585 	return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
586 				 &stat, sizeof(stat));
587 }
588 
589 static int scpi_dvfs_get_idx(u8 domain)
590 {
591 	int ret;
592 	u8 dvfs_idx;
593 
594 	ret = scpi_send_message(CMD_GET_DVFS, &domain, sizeof(domain),
595 				&dvfs_idx, sizeof(dvfs_idx));
596 
597 	return ret ? ret : dvfs_idx;
598 }
599 
600 static int scpi_dvfs_set_idx(u8 domain, u8 index)
601 {
602 	int stat;
603 	struct dvfs_set dvfs = {domain, index};
604 
605 	return scpi_send_message(CMD_SET_DVFS, &dvfs, sizeof(dvfs),
606 				 &stat, sizeof(stat));
607 }
608 
609 static int opp_cmp_func(const void *opp1, const void *opp2)
610 {
611 	const struct scpi_opp *t1 = opp1, *t2 = opp2;
612 
613 	return t1->freq - t2->freq;
614 }
615 
616 static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
617 {
618 	struct scpi_dvfs_info *info;
619 	struct scpi_opp *opp;
620 	struct dvfs_info buf;
621 	int ret, i;
622 
623 	if (domain >= MAX_DVFS_DOMAINS)
624 		return ERR_PTR(-EINVAL);
625 
626 	if (scpi_info->dvfs[domain])	/* data already populated */
627 		return scpi_info->dvfs[domain];
628 
629 	ret = scpi_send_message(CMD_GET_DVFS_INFO, &domain, sizeof(domain),
630 				&buf, sizeof(buf));
631 	if (ret)
632 		return ERR_PTR(ret);
633 
634 	if (!buf.opp_count || buf.opp_count > MAX_DVFS_OPPS)
635 		return ERR_PTR(-EINVAL);
636 
637 	info = kmalloc_obj(*info);
638 	if (!info)
639 		return ERR_PTR(-ENOMEM);
640 
641 	info->count = buf.opp_count;
642 	info->latency = le16_to_cpu(buf.latency) * 1000; /* uS to nS */
643 
644 	info->opps = kzalloc_objs(*opp, info->count);
645 	if (!info->opps) {
646 		kfree(info);
647 		return ERR_PTR(-ENOMEM);
648 	}
649 
650 	for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
651 		opp->freq = le32_to_cpu(buf.opps[i].freq);
652 		opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
653 	}
654 
655 	sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
656 
657 	scpi_info->dvfs[domain] = info;
658 	return info;
659 }
660 
661 static int scpi_dev_domain_id(struct device *dev)
662 {
663 	struct of_phandle_args clkspec;
664 	int domain;
665 
666 	if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
667 				       0, &clkspec))
668 		return -EINVAL;
669 
670 	domain = clkspec.args[0];
671 	of_node_put(clkspec.np);
672 	return domain;
673 }
674 
675 static struct scpi_dvfs_info *scpi_dvfs_info(struct device *dev)
676 {
677 	int domain = scpi_dev_domain_id(dev);
678 
679 	if (domain < 0)
680 		return ERR_PTR(domain);
681 
682 	return scpi_dvfs_get_info(domain);
683 }
684 
685 static int scpi_dvfs_get_transition_latency(struct device *dev)
686 {
687 	struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
688 
689 	if (IS_ERR(info))
690 		return PTR_ERR(info);
691 
692 	return info->latency;
693 }
694 
695 static int scpi_dvfs_add_opps_to_device(struct device *dev)
696 {
697 	int idx, ret;
698 	struct scpi_opp *opp;
699 	struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
700 
701 	if (IS_ERR(info))
702 		return PTR_ERR(info);
703 
704 	if (!info->opps)
705 		return -EIO;
706 
707 	for (opp = info->opps, idx = 0; idx < info->count; idx++, opp++) {
708 		ret = dev_pm_opp_add(dev, opp->freq, opp->m_volt * 1000);
709 		if (ret) {
710 			dev_warn(dev, "failed to add opp %uHz %umV\n",
711 				 opp->freq, opp->m_volt);
712 			while (idx-- > 0)
713 				dev_pm_opp_remove(dev, (--opp)->freq);
714 			return ret;
715 		}
716 	}
717 	return 0;
718 }
719 
720 static int scpi_sensor_get_capability(u16 *sensors)
721 {
722 	__le16 cap;
723 	int ret;
724 
725 	ret = scpi_send_message(CMD_SENSOR_CAPABILITIES, NULL, 0, &cap,
726 				sizeof(cap));
727 	if (!ret)
728 		*sensors = le16_to_cpu(cap);
729 
730 	return ret;
731 }
732 
733 static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
734 {
735 	__le16 id = cpu_to_le16(sensor_id);
736 	struct _scpi_sensor_info _info;
737 	int ret;
738 
739 	ret = scpi_send_message(CMD_SENSOR_INFO, &id, sizeof(id),
740 				&_info, sizeof(_info));
741 	if (!ret) {
742 		memcpy(info, &_info, sizeof(*info));
743 		info->sensor_id = le16_to_cpu(_info.sensor_id);
744 	}
745 
746 	return ret;
747 }
748 
749 static int scpi_sensor_get_value(u16 sensor, u64 *val)
750 {
751 	__le16 id = cpu_to_le16(sensor);
752 	__le64 value;
753 	int ret;
754 
755 	ret = scpi_send_message(CMD_SENSOR_VALUE, &id, sizeof(id),
756 				&value, sizeof(value));
757 	if (ret)
758 		return ret;
759 
760 	if (scpi_info->is_legacy)
761 		/* only 32-bits supported, upper 32 bits can be junk */
762 		*val = le32_to_cpup((__le32 *)&value);
763 	else
764 		*val = le64_to_cpu(value);
765 
766 	return 0;
767 }
768 
769 static int scpi_device_get_power_state(u16 dev_id)
770 {
771 	int ret;
772 	u8 pstate;
773 	__le16 id = cpu_to_le16(dev_id);
774 
775 	ret = scpi_send_message(CMD_GET_DEVICE_PWR_STATE, &id,
776 				sizeof(id), &pstate, sizeof(pstate));
777 	return ret ? ret : pstate;
778 }
779 
780 static int scpi_device_set_power_state(u16 dev_id, u8 pstate)
781 {
782 	int stat;
783 	struct dev_pstate_set dev_set = {
784 		.dev_id = cpu_to_le16(dev_id),
785 		.pstate = pstate,
786 	};
787 
788 	return scpi_send_message(CMD_SET_DEVICE_PWR_STATE, &dev_set,
789 				 sizeof(dev_set), &stat, sizeof(stat));
790 }
791 
792 static struct scpi_ops scpi_ops = {
793 	.get_version = scpi_get_version,
794 	.clk_get_range = scpi_clk_get_range,
795 	.clk_get_val = scpi_clk_get_val,
796 	.clk_set_val = scpi_clk_set_val,
797 	.dvfs_get_idx = scpi_dvfs_get_idx,
798 	.dvfs_set_idx = scpi_dvfs_set_idx,
799 	.dvfs_get_info = scpi_dvfs_get_info,
800 	.device_domain_id = scpi_dev_domain_id,
801 	.get_transition_latency = scpi_dvfs_get_transition_latency,
802 	.add_opps_to_device = scpi_dvfs_add_opps_to_device,
803 	.sensor_get_capability = scpi_sensor_get_capability,
804 	.sensor_get_info = scpi_sensor_get_info,
805 	.sensor_get_value = scpi_sensor_get_value,
806 	.device_get_power_state = scpi_device_get_power_state,
807 	.device_set_power_state = scpi_device_set_power_state,
808 };
809 
810 struct scpi_ops *get_scpi_ops(void)
811 {
812 	return scpi_info ? scpi_info->scpi_ops : NULL;
813 }
814 EXPORT_SYMBOL_GPL(get_scpi_ops);
815 
816 static int scpi_init_versions(struct scpi_drvinfo *info)
817 {
818 	int ret;
819 	struct scp_capabilities caps;
820 
821 	ret = scpi_send_message(CMD_SCPI_CAPABILITIES, NULL, 0,
822 				&caps, sizeof(caps));
823 	if (!ret) {
824 		info->protocol_version = le32_to_cpu(caps.protocol_version);
825 		info->firmware_version = le32_to_cpu(caps.platform_version);
826 	}
827 	/* Ignore error if not implemented */
828 	if (info->is_legacy && ret == -EOPNOTSUPP)
829 		return 0;
830 
831 	return ret;
832 }
833 
834 static ssize_t protocol_version_show(struct device *dev,
835 				     struct device_attribute *attr, char *buf)
836 {
837 	struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
838 
839 	return sprintf(buf, "%lu.%lu\n",
840 		FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version),
841 		FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version));
842 }
843 static DEVICE_ATTR_RO(protocol_version);
844 
845 static ssize_t firmware_version_show(struct device *dev,
846 				     struct device_attribute *attr, char *buf)
847 {
848 	struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
849 
850 	return sprintf(buf, "%lu.%lu.%lu\n",
851 		FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version),
852 		FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version),
853 		FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version));
854 }
855 static DEVICE_ATTR_RO(firmware_version);
856 
857 static struct attribute *versions_attrs[] = {
858 	&dev_attr_firmware_version.attr,
859 	&dev_attr_protocol_version.attr,
860 	NULL,
861 };
862 ATTRIBUTE_GROUPS(versions);
863 
864 static void scpi_free_channels(void *data)
865 {
866 	struct scpi_drvinfo *info = data;
867 	int i;
868 
869 	for (i = 0; i < info->num_chans; i++)
870 		mbox_free_channel(info->channels[i].chan);
871 }
872 
873 static void scpi_remove(struct platform_device *pdev)
874 {
875 	int i;
876 	struct scpi_drvinfo *info = platform_get_drvdata(pdev);
877 
878 	scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
879 
880 	for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
881 		kfree(info->dvfs[i]->opps);
882 		kfree(info->dvfs[i]);
883 	}
884 }
885 
886 #define MAX_SCPI_XFERS		10
887 static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
888 {
889 	int i;
890 	struct scpi_xfer *xfers;
891 
892 	xfers = devm_kcalloc(dev, MAX_SCPI_XFERS, sizeof(*xfers), GFP_KERNEL);
893 	if (!xfers)
894 		return -ENOMEM;
895 
896 	ch->xfers = xfers;
897 	for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++) {
898 		init_completion(&xfers->done);
899 		list_add_tail(&xfers->node, &ch->xfers_list);
900 	}
901 
902 	return 0;
903 }
904 
905 static const struct of_device_id shmem_of_match[] __maybe_unused = {
906 	{ .compatible = "amlogic,meson-gxbb-scp-shmem", },
907 	{ .compatible = "amlogic,meson-axg-scp-shmem", },
908 	{ .compatible = "arm,juno-scp-shmem", },
909 	{ .compatible = "arm,scp-shmem", },
910 	{ }
911 };
912 
913 static int scpi_probe(struct platform_device *pdev)
914 {
915 	int count, idx, ret;
916 	struct resource res;
917 	struct device *dev = &pdev->dev;
918 	struct device_node *np = dev->of_node;
919 	struct scpi_drvinfo *scpi_drvinfo;
920 
921 	scpi_drvinfo = devm_kzalloc(dev, sizeof(*scpi_drvinfo), GFP_KERNEL);
922 	if (!scpi_drvinfo)
923 		return -ENOMEM;
924 
925 	scpi_drvinfo->is_legacy = !!device_get_match_data(dev);
926 
927 	count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
928 	if (count < 0) {
929 		dev_err(dev, "no mboxes property in '%pOF'\n", np);
930 		return -ENODEV;
931 	}
932 
933 	scpi_drvinfo->channels =
934 		devm_kcalloc(dev, count, sizeof(struct scpi_chan), GFP_KERNEL);
935 	if (!scpi_drvinfo->channels)
936 		return -ENOMEM;
937 
938 	ret = devm_add_action(dev, scpi_free_channels, scpi_drvinfo);
939 	if (ret)
940 		return ret;
941 
942 	for (; scpi_drvinfo->num_chans < count; scpi_drvinfo->num_chans++) {
943 		resource_size_t size;
944 		int idx = scpi_drvinfo->num_chans;
945 		struct scpi_chan *pchan = scpi_drvinfo->channels + idx;
946 		struct mbox_client *cl = &pchan->cl;
947 		struct device_node *shmem __free(device_node) =
948 			of_parse_phandle(np, "shmem", idx);
949 
950 		if (!of_match_node(shmem_of_match, shmem))
951 			return -ENXIO;
952 
953 		ret = of_address_to_resource(shmem, 0, &res);
954 		if (ret) {
955 			dev_err(dev, "failed to get SCPI payload mem resource\n");
956 			return ret;
957 		}
958 
959 		size = resource_size(&res);
960 		pchan->rx_payload = devm_ioremap(dev, res.start, size);
961 		if (!pchan->rx_payload) {
962 			dev_err(dev, "failed to ioremap SCPI payload\n");
963 			return -EADDRNOTAVAIL;
964 		}
965 		pchan->tx_payload = pchan->rx_payload + (size >> 1);
966 
967 		cl->dev = dev;
968 		cl->rx_callback = scpi_handle_remote_msg;
969 		cl->tx_prepare = scpi_tx_prepare;
970 		cl->tx_block = true;
971 		cl->tx_tout = 20;
972 		cl->knows_txdone = false; /* controller can't ack */
973 
974 		INIT_LIST_HEAD(&pchan->rx_pending);
975 		INIT_LIST_HEAD(&pchan->xfers_list);
976 		spin_lock_init(&pchan->rx_lock);
977 		mutex_init(&pchan->xfers_lock);
978 
979 		ret = scpi_alloc_xfer_list(dev, pchan);
980 		if (!ret) {
981 			pchan->chan = mbox_request_channel(cl, idx);
982 			if (!IS_ERR(pchan->chan))
983 				continue;
984 			ret = PTR_ERR(pchan->chan);
985 			if (ret != -EPROBE_DEFER)
986 				dev_err(dev, "failed to get channel%d err %d\n",
987 					idx, ret);
988 		}
989 		return ret;
990 	}
991 
992 	scpi_drvinfo->commands = scpi_std_commands;
993 
994 	platform_set_drvdata(pdev, scpi_drvinfo);
995 
996 	if (scpi_drvinfo->is_legacy) {
997 		/* Replace with legacy variants */
998 		scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
999 		scpi_drvinfo->commands = scpi_legacy_commands;
1000 
1001 		/* Fill priority bitmap */
1002 		for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
1003 			set_bit(legacy_hpriority_cmds[idx],
1004 				scpi_drvinfo->cmd_priority);
1005 	}
1006 
1007 	scpi_info = scpi_drvinfo;
1008 
1009 	ret = scpi_init_versions(scpi_drvinfo);
1010 	if (ret) {
1011 		dev_err(dev, "incorrect or no SCP firmware found\n");
1012 		scpi_info = NULL;
1013 		return ret;
1014 	}
1015 
1016 	if (scpi_drvinfo->is_legacy && !scpi_drvinfo->protocol_version &&
1017 	    !scpi_drvinfo->firmware_version)
1018 		dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n");
1019 	else
1020 		dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
1021 			 FIELD_GET(PROTO_REV_MAJOR_MASK,
1022 				   scpi_drvinfo->protocol_version),
1023 			 FIELD_GET(PROTO_REV_MINOR_MASK,
1024 				   scpi_drvinfo->protocol_version),
1025 			 FIELD_GET(FW_REV_MAJOR_MASK,
1026 				   scpi_drvinfo->firmware_version),
1027 			 FIELD_GET(FW_REV_MINOR_MASK,
1028 				   scpi_drvinfo->firmware_version),
1029 			 FIELD_GET(FW_REV_PATCH_MASK,
1030 				   scpi_drvinfo->firmware_version));
1031 
1032 	scpi_drvinfo->scpi_ops = &scpi_ops;
1033 
1034 	ret = devm_of_platform_populate(dev);
1035 	if (ret)
1036 		scpi_info = NULL;
1037 
1038 	return ret;
1039 }
1040 
1041 static const struct of_device_id scpi_of_match[] = {
1042 	{.compatible = "arm,scpi"},
1043 	{.compatible = "arm,scpi-pre-1.0", .data = (void *)1UL },
1044 	{},
1045 };
1046 
1047 MODULE_DEVICE_TABLE(of, scpi_of_match);
1048 
1049 static struct platform_driver scpi_driver = {
1050 	.driver = {
1051 		.name = "scpi_protocol",
1052 		.of_match_table = scpi_of_match,
1053 		.dev_groups = versions_groups,
1054 	},
1055 	.probe = scpi_probe,
1056 	.remove = scpi_remove,
1057 };
1058 module_platform_driver(scpi_driver);
1059 
1060 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1061 MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
1062 MODULE_LICENSE("GPL v2");
1063