• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Message Protocol driver
4  *
5  * SCMI 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) 2018 ARM Ltd.
15  */
16 
17 #include <linux/bitmap.h>
18 #include <linux/export.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/ktime.h>
22 #include <linux/mailbox_client.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/of_device.h>
26 #include <linux/processor.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
29 
30 #include "common.h"
31 
32 #define MSG_ID_MASK		GENMASK(7, 0)
33 #define MSG_TYPE_MASK		GENMASK(9, 8)
34 #define MSG_PROTOCOL_ID_MASK	GENMASK(17, 10)
35 #define MSG_TOKEN_ID_MASK	GENMASK(27, 18)
36 #define MSG_XTRACT_TOKEN(hdr)	FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
37 #define MSG_TOKEN_MAX		(MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
38 
39 enum scmi_error_codes {
40 	SCMI_SUCCESS = 0,	/* Success */
41 	SCMI_ERR_SUPPORT = -1,	/* Not supported */
42 	SCMI_ERR_PARAMS = -2,	/* Invalid Parameters */
43 	SCMI_ERR_ACCESS = -3,	/* Invalid access/permission denied */
44 	SCMI_ERR_ENTRY = -4,	/* Not found */
45 	SCMI_ERR_RANGE = -5,	/* Value out of range */
46 	SCMI_ERR_BUSY = -6,	/* Device busy */
47 	SCMI_ERR_COMMS = -7,	/* Communication Error */
48 	SCMI_ERR_GENERIC = -8,	/* Generic Error */
49 	SCMI_ERR_HARDWARE = -9,	/* Hardware Error */
50 	SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
51 	SCMI_ERR_MAX
52 };
53 
54 /* List of all SCMI devices active in system */
55 static LIST_HEAD(scmi_list);
56 /* Protection for the entire list */
57 static DEFINE_MUTEX(scmi_list_mutex);
58 
59 /**
60  * struct scmi_xfers_info - Structure to manage transfer information
61  *
62  * @xfer_block: Preallocated Message array
63  * @xfer_alloc_table: Bitmap table for allocated messages.
64  *	Index of this bitmap table is also used for message
65  *	sequence identifier.
66  * @xfer_lock: Protection for message allocation
67  */
68 struct scmi_xfers_info {
69 	struct scmi_xfer *xfer_block;
70 	unsigned long *xfer_alloc_table;
71 	spinlock_t xfer_lock;
72 };
73 
74 /**
75  * struct scmi_desc - Description of SoC integration
76  *
77  * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
78  * @max_msg: Maximum number of messages that can be pending
79  *	simultaneously in the system
80  * @max_msg_size: Maximum size of data per message that can be handled.
81  */
82 struct scmi_desc {
83 	int max_rx_timeout_ms;
84 	int max_msg;
85 	int max_msg_size;
86 };
87 
88 /**
89  * struct scmi_chan_info - Structure representing a SCMI channel informfation
90  *
91  * @cl: Mailbox Client
92  * @chan: Transmit/Receive mailbox channel
93  * @payload: Transmit/Receive mailbox channel payload area
94  * @dev: Reference to device in the SCMI hierarchy corresponding to this
95  *	 channel
96  * @handle: Pointer to SCMI entity handle
97  */
98 struct scmi_chan_info {
99 	struct mbox_client cl;
100 	struct mbox_chan *chan;
101 	void __iomem *payload;
102 	struct device *dev;
103 	struct scmi_handle *handle;
104 };
105 
106 /**
107  * struct scmi_info - Structure representing a SCMI instance
108  *
109  * @dev: Device pointer
110  * @desc: SoC description for this instance
111  * @handle: Instance of SCMI handle to send to clients
112  * @version: SCMI revision information containing protocol version,
113  *	implementation version and (sub-)vendor identification.
114  * @minfo: Message info
115  * @tx_idr: IDR object to map protocol id to channel info pointer
116  * @protocols_imp: List of protocols implemented, currently maximum of
117  *	MAX_PROTOCOLS_IMP elements allocated by the base protocol
118  * @node: List head
119  * @users: Number of users of this instance
120  */
121 struct scmi_info {
122 	struct device *dev;
123 	const struct scmi_desc *desc;
124 	struct scmi_revision_info version;
125 	struct scmi_handle handle;
126 	struct scmi_xfers_info minfo;
127 	struct idr tx_idr;
128 	u8 *protocols_imp;
129 	struct list_head node;
130 	int users;
131 };
132 
133 #define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl)
134 #define handle_to_scmi_info(h)	container_of(h, struct scmi_info, handle)
135 
136 /*
137  * SCMI specification requires all parameters, message headers, return
138  * arguments or any protocol data to be expressed in little endian
139  * format only.
140  */
141 struct scmi_shared_mem {
142 	__le32 reserved;
143 	__le32 channel_status;
144 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR	BIT(1)
145 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE	BIT(0)
146 	__le32 reserved1[2];
147 	__le32 flags;
148 #define SCMI_SHMEM_FLAG_INTR_ENABLED	BIT(0)
149 	__le32 length;
150 	__le32 msg_header;
151 	u8 msg_payload[0];
152 };
153 
154 static const int scmi_linux_errmap[] = {
155 	/* better than switch case as long as return value is continuous */
156 	0,			/* SCMI_SUCCESS */
157 	-EOPNOTSUPP,		/* SCMI_ERR_SUPPORT */
158 	-EINVAL,		/* SCMI_ERR_PARAM */
159 	-EACCES,		/* SCMI_ERR_ACCESS */
160 	-ENOENT,		/* SCMI_ERR_ENTRY */
161 	-ERANGE,		/* SCMI_ERR_RANGE */
162 	-EBUSY,			/* SCMI_ERR_BUSY */
163 	-ECOMM,			/* SCMI_ERR_COMMS */
164 	-EIO,			/* SCMI_ERR_GENERIC */
165 	-EREMOTEIO,		/* SCMI_ERR_HARDWARE */
166 	-EPROTO,		/* SCMI_ERR_PROTOCOL */
167 };
168 
scmi_to_linux_errno(int errno)169 static inline int scmi_to_linux_errno(int errno)
170 {
171 	if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
172 		return scmi_linux_errmap[-errno];
173 	return -EIO;
174 }
175 
176 /**
177  * scmi_dump_header_dbg() - Helper to dump a message header.
178  *
179  * @dev: Device pointer corresponding to the SCMI entity
180  * @hdr: pointer to header.
181  */
scmi_dump_header_dbg(struct device * dev,struct scmi_msg_hdr * hdr)182 static inline void scmi_dump_header_dbg(struct device *dev,
183 					struct scmi_msg_hdr *hdr)
184 {
185 	dev_dbg(dev, "Command ID: %x Sequence ID: %x Protocol: %x\n",
186 		hdr->id, hdr->seq, hdr->protocol_id);
187 }
188 
scmi_fetch_response(struct scmi_xfer * xfer,struct scmi_shared_mem __iomem * mem)189 static void scmi_fetch_response(struct scmi_xfer *xfer,
190 				struct scmi_shared_mem __iomem *mem)
191 {
192 	xfer->hdr.status = ioread32(mem->msg_payload);
193 	/* Skip the length of header and statues in payload area i.e 8 bytes*/
194 	xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8);
195 
196 	/* Take a copy to the rx buffer.. */
197 	memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len);
198 }
199 
200 /**
201  * scmi_rx_callback() - mailbox client callback for receive messages
202  *
203  * @cl: client pointer
204  * @m: mailbox message
205  *
206  * Processes one received message to appropriate transfer information and
207  * signals completion of the transfer.
208  *
209  * NOTE: This function will be invoked in IRQ context, hence should be
210  * as optimal as possible.
211  */
scmi_rx_callback(struct mbox_client * cl,void * m)212 static void scmi_rx_callback(struct mbox_client *cl, void *m)
213 {
214 	u16 xfer_id;
215 	struct scmi_xfer *xfer;
216 	struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
217 	struct device *dev = cinfo->dev;
218 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
219 	struct scmi_xfers_info *minfo = &info->minfo;
220 	struct scmi_shared_mem __iomem *mem = cinfo->payload;
221 
222 	xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
223 
224 	/* Are we even expecting this? */
225 	if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
226 		dev_err(dev, "message for %d is not expected!\n", xfer_id);
227 		return;
228 	}
229 
230 	xfer = &minfo->xfer_block[xfer_id];
231 
232 	scmi_dump_header_dbg(dev, &xfer->hdr);
233 	/* Is the message of valid length? */
234 	if (xfer->rx.len > info->desc->max_msg_size) {
235 		dev_err(dev, "unable to handle %zu xfer(max %d)\n",
236 			xfer->rx.len, info->desc->max_msg_size);
237 		return;
238 	}
239 
240 	scmi_fetch_response(xfer, mem);
241 	complete(&xfer->done);
242 }
243 
244 /**
245  * pack_scmi_header() - packs and returns 32-bit header
246  *
247  * @hdr: pointer to header containing all the information on message id,
248  *	protocol id and sequence id.
249  *
250  * Return: 32-bit packed command header to be sent to the platform.
251  */
pack_scmi_header(struct scmi_msg_hdr * hdr)252 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
253 {
254 	return FIELD_PREP(MSG_ID_MASK, hdr->id) |
255 		FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
256 		FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
257 }
258 
259 /**
260  * scmi_tx_prepare() - mailbox client callback to prepare for the transfer
261  *
262  * @cl: client pointer
263  * @m: mailbox message
264  *
265  * This function prepares the shared memory which contains the header and the
266  * payload.
267  */
scmi_tx_prepare(struct mbox_client * cl,void * m)268 static void scmi_tx_prepare(struct mbox_client *cl, void *m)
269 {
270 	struct scmi_xfer *t = m;
271 	struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
272 	struct scmi_shared_mem __iomem *mem = cinfo->payload;
273 
274 	/*
275 	 * Ideally channel must be free by now unless OS timeout last
276 	 * request and platform continued to process the same, wait
277 	 * until it releases the shared memory, otherwise we may endup
278 	 * overwriting its response with new message payload or vice-versa
279 	 */
280 	spin_until_cond(ioread32(&mem->channel_status) &
281 			SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
282 	/* Mark channel busy + clear error */
283 	iowrite32(0x0, &mem->channel_status);
284 	iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
285 		  &mem->flags);
286 	iowrite32(sizeof(mem->msg_header) + t->tx.len, &mem->length);
287 	iowrite32(pack_scmi_header(&t->hdr), &mem->msg_header);
288 	if (t->tx.buf)
289 		memcpy_toio(mem->msg_payload, t->tx.buf, t->tx.len);
290 }
291 
292 /**
293  * scmi_xfer_get() - Allocate one message
294  *
295  * @handle: Pointer to SCMI entity handle
296  *
297  * Helper function which is used by various command functions that are
298  * exposed to clients of this driver for allocating a message traffic event.
299  *
300  * This function can sleep depending on pending requests already in the system
301  * for the SCMI entity. Further, this also holds a spinlock to maintain
302  * integrity of internal data structures.
303  *
304  * Return: 0 if all went fine, else corresponding error.
305  */
scmi_xfer_get(const struct scmi_handle * handle)306 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle)
307 {
308 	u16 xfer_id;
309 	struct scmi_xfer *xfer;
310 	unsigned long flags, bit_pos;
311 	struct scmi_info *info = handle_to_scmi_info(handle);
312 	struct scmi_xfers_info *minfo = &info->minfo;
313 
314 	/* Keep the locked section as small as possible */
315 	spin_lock_irqsave(&minfo->xfer_lock, flags);
316 	bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
317 				      info->desc->max_msg);
318 	if (bit_pos == info->desc->max_msg) {
319 		spin_unlock_irqrestore(&minfo->xfer_lock, flags);
320 		return ERR_PTR(-ENOMEM);
321 	}
322 	set_bit(bit_pos, minfo->xfer_alloc_table);
323 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
324 
325 	xfer_id = bit_pos;
326 
327 	xfer = &minfo->xfer_block[xfer_id];
328 	xfer->hdr.seq = xfer_id;
329 	reinit_completion(&xfer->done);
330 
331 	return xfer;
332 }
333 
334 /**
335  * scmi_xfer_put() - Release a message
336  *
337  * @handle: Pointer to SCMI entity handle
338  * @xfer: message that was reserved by scmi_xfer_get
339  *
340  * This holds a spinlock to maintain integrity of internal data structures.
341  */
scmi_xfer_put(const struct scmi_handle * handle,struct scmi_xfer * xfer)342 void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
343 {
344 	unsigned long flags;
345 	struct scmi_info *info = handle_to_scmi_info(handle);
346 	struct scmi_xfers_info *minfo = &info->minfo;
347 
348 	/*
349 	 * Keep the locked section as small as possible
350 	 * NOTE: we might escape with smp_mb and no lock here..
351 	 * but just be conservative and symmetric.
352 	 */
353 	spin_lock_irqsave(&minfo->xfer_lock, flags);
354 	clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
355 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
356 }
357 
358 static bool
scmi_xfer_poll_done(const struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)359 scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
360 {
361 	struct scmi_shared_mem __iomem *mem = cinfo->payload;
362 	u16 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
363 
364 	if (xfer->hdr.seq != xfer_id)
365 		return false;
366 
367 	return ioread32(&mem->channel_status) &
368 		(SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
369 		SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
370 }
371 
372 #define SCMI_MAX_POLL_TO_NS	(100 * NSEC_PER_USEC)
373 
scmi_xfer_done_no_timeout(const struct scmi_chan_info * cinfo,struct scmi_xfer * xfer,ktime_t stop)374 static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info *cinfo,
375 				      struct scmi_xfer *xfer, ktime_t stop)
376 {
377 	ktime_t __cur = ktime_get();
378 
379 	return scmi_xfer_poll_done(cinfo, xfer) || ktime_after(__cur, stop);
380 }
381 
382 /**
383  * scmi_do_xfer() - Do one transfer
384  *
385  * @handle: Pointer to SCMI entity handle
386  * @xfer: Transfer to initiate and wait for response
387  *
388  * Return: -ETIMEDOUT in case of no response, if transmit error,
389  *	return corresponding error, else if all goes well,
390  *	return 0.
391  */
scmi_do_xfer(const struct scmi_handle * handle,struct scmi_xfer * xfer)392 int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer)
393 {
394 	int ret;
395 	int timeout;
396 	struct scmi_info *info = handle_to_scmi_info(handle);
397 	struct device *dev = info->dev;
398 	struct scmi_chan_info *cinfo;
399 
400 	cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
401 	if (unlikely(!cinfo))
402 		return -EINVAL;
403 
404 	ret = mbox_send_message(cinfo->chan, xfer);
405 	if (ret < 0) {
406 		dev_dbg(dev, "mbox send fail %d\n", ret);
407 		return ret;
408 	}
409 
410 	/* mbox_send_message returns non-negative value on success, so reset */
411 	ret = 0;
412 
413 	if (xfer->hdr.poll_completion) {
414 		ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
415 
416 		spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
417 
418 		if (ktime_before(ktime_get(), stop))
419 			scmi_fetch_response(xfer, cinfo->payload);
420 		else
421 			ret = -ETIMEDOUT;
422 	} else {
423 		/* And we wait for the response. */
424 		timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
425 		if (!wait_for_completion_timeout(&xfer->done, timeout)) {
426 			dev_err(dev, "mbox timed out in resp(caller: %pS)\n",
427 				(void *)_RET_IP_);
428 			ret = -ETIMEDOUT;
429 		}
430 	}
431 
432 	if (!ret && xfer->hdr.status)
433 		ret = scmi_to_linux_errno(xfer->hdr.status);
434 
435 	/*
436 	 * NOTE: we might prefer not to need the mailbox ticker to manage the
437 	 * transfer queueing since the protocol layer queues things by itself.
438 	 * Unfortunately, we have to kick the mailbox framework after we have
439 	 * received our message.
440 	 */
441 	mbox_client_txdone(cinfo->chan, ret);
442 
443 	return ret;
444 }
445 
446 /**
447  * scmi_xfer_get_init() - Allocate and initialise one message
448  *
449  * @handle: Pointer to SCMI entity handle
450  * @msg_id: Message identifier
451  * @prot_id: Protocol identifier for the message
452  * @tx_size: transmit message size
453  * @rx_size: receive message size
454  * @p: pointer to the allocated and initialised message
455  *
456  * This function allocates the message using @scmi_xfer_get and
457  * initialise the header.
458  *
459  * Return: 0 if all went fine with @p pointing to message, else
460  *	corresponding error.
461  */
scmi_xfer_get_init(const struct scmi_handle * handle,u8 msg_id,u8 prot_id,size_t tx_size,size_t rx_size,struct scmi_xfer ** p)462 int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id,
463 		       size_t tx_size, size_t rx_size, struct scmi_xfer **p)
464 {
465 	int ret;
466 	struct scmi_xfer *xfer;
467 	struct scmi_info *info = handle_to_scmi_info(handle);
468 	struct device *dev = info->dev;
469 
470 	/* Ensure we have sane transfer sizes */
471 	if (rx_size > info->desc->max_msg_size ||
472 	    tx_size > info->desc->max_msg_size)
473 		return -ERANGE;
474 
475 	xfer = scmi_xfer_get(handle);
476 	if (IS_ERR(xfer)) {
477 		ret = PTR_ERR(xfer);
478 		dev_err(dev, "failed to get free message slot(%d)\n", ret);
479 		return ret;
480 	}
481 
482 	xfer->tx.len = tx_size;
483 	xfer->rx.len = rx_size ? : info->desc->max_msg_size;
484 	xfer->hdr.id = msg_id;
485 	xfer->hdr.protocol_id = prot_id;
486 	xfer->hdr.poll_completion = false;
487 
488 	*p = xfer;
489 
490 	return 0;
491 }
492 
493 /**
494  * scmi_version_get() - command to get the revision of the SCMI entity
495  *
496  * @handle: Pointer to SCMI entity handle
497  * @protocol: Protocol identifier for the message
498  * @version: Holds returned version of protocol.
499  *
500  * Updates the SCMI information in the internal data structure.
501  *
502  * Return: 0 if all went fine, else return appropriate error.
503  */
scmi_version_get(const struct scmi_handle * handle,u8 protocol,u32 * version)504 int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
505 		     u32 *version)
506 {
507 	int ret;
508 	__le32 *rev_info;
509 	struct scmi_xfer *t;
510 
511 	ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0,
512 				 sizeof(*version), &t);
513 	if (ret)
514 		return ret;
515 
516 	ret = scmi_do_xfer(handle, t);
517 	if (!ret) {
518 		rev_info = t->rx.buf;
519 		*version = le32_to_cpu(*rev_info);
520 	}
521 
522 	scmi_xfer_put(handle, t);
523 	return ret;
524 }
525 
scmi_setup_protocol_implemented(const struct scmi_handle * handle,u8 * prot_imp)526 void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
527 				     u8 *prot_imp)
528 {
529 	struct scmi_info *info = handle_to_scmi_info(handle);
530 
531 	info->protocols_imp = prot_imp;
532 }
533 
534 static bool
scmi_is_protocol_implemented(const struct scmi_handle * handle,u8 prot_id)535 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
536 {
537 	int i;
538 	struct scmi_info *info = handle_to_scmi_info(handle);
539 
540 	if (!info->protocols_imp)
541 		return false;
542 
543 	for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
544 		if (info->protocols_imp[i] == prot_id)
545 			return true;
546 	return false;
547 }
548 
549 /**
550  * scmi_handle_get() - Get the SCMI handle for a device
551  *
552  * @dev: pointer to device for which we want SCMI handle
553  *
554  * NOTE: The function does not track individual clients of the framework
555  * and is expected to be maintained by caller of SCMI protocol library.
556  * scmi_handle_put must be balanced with successful scmi_handle_get
557  *
558  * Return: pointer to handle if successful, NULL on error
559  */
scmi_handle_get(struct device * dev)560 struct scmi_handle *scmi_handle_get(struct device *dev)
561 {
562 	struct list_head *p;
563 	struct scmi_info *info;
564 	struct scmi_handle *handle = NULL;
565 
566 	mutex_lock(&scmi_list_mutex);
567 	list_for_each(p, &scmi_list) {
568 		info = list_entry(p, struct scmi_info, node);
569 		if (dev->parent == info->dev) {
570 			handle = &info->handle;
571 			info->users++;
572 			break;
573 		}
574 	}
575 	mutex_unlock(&scmi_list_mutex);
576 
577 	return handle;
578 }
579 
580 /**
581  * scmi_handle_put() - Release the handle acquired by scmi_handle_get
582  *
583  * @handle: handle acquired by scmi_handle_get
584  *
585  * NOTE: The function does not track individual clients of the framework
586  * and is expected to be maintained by caller of SCMI protocol library.
587  * scmi_handle_put must be balanced with successful scmi_handle_get
588  *
589  * Return: 0 is successfully released
590  *	if null was passed, it returns -EINVAL;
591  */
scmi_handle_put(const struct scmi_handle * handle)592 int scmi_handle_put(const struct scmi_handle *handle)
593 {
594 	struct scmi_info *info;
595 
596 	if (!handle)
597 		return -EINVAL;
598 
599 	info = handle_to_scmi_info(handle);
600 	mutex_lock(&scmi_list_mutex);
601 	if (!WARN_ON(!info->users))
602 		info->users--;
603 	mutex_unlock(&scmi_list_mutex);
604 
605 	return 0;
606 }
607 
608 static const struct scmi_desc scmi_generic_desc = {
609 	.max_rx_timeout_ms = 30,	/* We may increase this if required */
610 	.max_msg = 20,		/* Limited by MBOX_TX_QUEUE_LEN */
611 	.max_msg_size = 128,
612 };
613 
614 /* Each compatible listed below must have descriptor associated with it */
615 static const struct of_device_id scmi_of_match[] = {
616 	{ .compatible = "arm,scmi", .data = &scmi_generic_desc },
617 	{ /* Sentinel */ },
618 };
619 
620 MODULE_DEVICE_TABLE(of, scmi_of_match);
621 
scmi_xfer_info_init(struct scmi_info * sinfo)622 static int scmi_xfer_info_init(struct scmi_info *sinfo)
623 {
624 	int i;
625 	struct scmi_xfer *xfer;
626 	struct device *dev = sinfo->dev;
627 	const struct scmi_desc *desc = sinfo->desc;
628 	struct scmi_xfers_info *info = &sinfo->minfo;
629 
630 	/* Pre-allocated messages, no more than what hdr.seq can support */
631 	if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
632 		dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
633 			desc->max_msg, MSG_TOKEN_MAX);
634 		return -EINVAL;
635 	}
636 
637 	info->xfer_block = devm_kcalloc(dev, desc->max_msg,
638 					sizeof(*info->xfer_block), GFP_KERNEL);
639 	if (!info->xfer_block)
640 		return -ENOMEM;
641 
642 	info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
643 					      sizeof(long), GFP_KERNEL);
644 	if (!info->xfer_alloc_table)
645 		return -ENOMEM;
646 
647 	/* Pre-initialize the buffer pointer to pre-allocated buffers */
648 	for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
649 		xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
650 					    GFP_KERNEL);
651 		if (!xfer->rx.buf)
652 			return -ENOMEM;
653 
654 		xfer->tx.buf = xfer->rx.buf;
655 		init_completion(&xfer->done);
656 	}
657 
658 	spin_lock_init(&info->xfer_lock);
659 
660 	return 0;
661 }
662 
scmi_mailbox_check(struct device_node * np)663 static int scmi_mailbox_check(struct device_node *np)
664 {
665 	return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells", 0, NULL);
666 }
667 
scmi_mbox_free_channel(int id,void * p,void * data)668 static int scmi_mbox_free_channel(int id, void *p, void *data)
669 {
670 	struct scmi_chan_info *cinfo = p;
671 	struct idr *idr = data;
672 
673 	if (!IS_ERR_OR_NULL(cinfo->chan)) {
674 		mbox_free_channel(cinfo->chan);
675 		cinfo->chan = NULL;
676 	}
677 
678 	idr_remove(idr, id);
679 
680 	return 0;
681 }
682 
scmi_remove(struct platform_device * pdev)683 static int scmi_remove(struct platform_device *pdev)
684 {
685 	int ret = 0;
686 	struct scmi_info *info = platform_get_drvdata(pdev);
687 	struct idr *idr = &info->tx_idr;
688 
689 	mutex_lock(&scmi_list_mutex);
690 	if (info->users)
691 		ret = -EBUSY;
692 	else
693 		list_del(&info->node);
694 	mutex_unlock(&scmi_list_mutex);
695 
696 	if (ret)
697 		return ret;
698 
699 	/* Safe to free channels since no more users */
700 	ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
701 	idr_destroy(&info->tx_idr);
702 
703 	return ret;
704 }
705 
706 static inline int
scmi_mbox_chan_setup(struct scmi_info * info,struct device * dev,int prot_id)707 scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev, int prot_id)
708 {
709 	int ret;
710 	struct resource res;
711 	resource_size_t size;
712 	struct device_node *shmem, *np = dev->of_node;
713 	struct scmi_chan_info *cinfo;
714 	struct mbox_client *cl;
715 
716 	if (scmi_mailbox_check(np)) {
717 		cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
718 		goto idr_alloc;
719 	}
720 
721 	cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
722 	if (!cinfo)
723 		return -ENOMEM;
724 
725 	cinfo->dev = dev;
726 
727 	cl = &cinfo->cl;
728 	cl->dev = dev;
729 	cl->rx_callback = scmi_rx_callback;
730 	cl->tx_prepare = scmi_tx_prepare;
731 	cl->tx_block = false;
732 	cl->knows_txdone = true;
733 
734 	shmem = of_parse_phandle(np, "shmem", 0);
735 	ret = of_address_to_resource(shmem, 0, &res);
736 	of_node_put(shmem);
737 	if (ret) {
738 		dev_err(dev, "failed to get SCMI Tx payload mem resource\n");
739 		return ret;
740 	}
741 
742 	size = resource_size(&res);
743 	cinfo->payload = devm_ioremap(info->dev, res.start, size);
744 	if (!cinfo->payload) {
745 		dev_err(dev, "failed to ioremap SCMI Tx payload\n");
746 		return -EADDRNOTAVAIL;
747 	}
748 
749 	/* Transmit channel is first entry i.e. index 0 */
750 	cinfo->chan = mbox_request_channel(cl, 0);
751 	if (IS_ERR(cinfo->chan)) {
752 		ret = PTR_ERR(cinfo->chan);
753 		if (ret != -EPROBE_DEFER)
754 			dev_err(dev, "failed to request SCMI Tx mailbox\n");
755 		return ret;
756 	}
757 
758 idr_alloc:
759 	ret = idr_alloc(&info->tx_idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
760 	if (ret != prot_id) {
761 		dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
762 		return ret;
763 	}
764 
765 	cinfo->handle = &info->handle;
766 	return 0;
767 }
768 
769 static inline void
scmi_create_protocol_device(struct device_node * np,struct scmi_info * info,int prot_id)770 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
771 			    int prot_id)
772 {
773 	struct scmi_device *sdev;
774 
775 	sdev = scmi_device_create(np, info->dev, prot_id);
776 	if (!sdev) {
777 		dev_err(info->dev, "failed to create %d protocol device\n",
778 			prot_id);
779 		return;
780 	}
781 
782 	if (scmi_mbox_chan_setup(info, &sdev->dev, prot_id)) {
783 		dev_err(&sdev->dev, "failed to setup transport\n");
784 		scmi_device_destroy(sdev);
785 		return;
786 	}
787 
788 	/* setup handle now as the transport is ready */
789 	scmi_set_handle(sdev);
790 }
791 
scmi_probe(struct platform_device * pdev)792 static int scmi_probe(struct platform_device *pdev)
793 {
794 	int ret;
795 	struct scmi_handle *handle;
796 	const struct scmi_desc *desc;
797 	struct scmi_info *info;
798 	struct device *dev = &pdev->dev;
799 	struct device_node *child, *np = dev->of_node;
800 
801 	/* Only mailbox method supported, check for the presence of one */
802 	if (scmi_mailbox_check(np)) {
803 		dev_err(dev, "no mailbox found in %pOF\n", np);
804 		return -EINVAL;
805 	}
806 
807 	desc = of_match_device(scmi_of_match, dev)->data;
808 
809 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
810 	if (!info)
811 		return -ENOMEM;
812 
813 	info->dev = dev;
814 	info->desc = desc;
815 	INIT_LIST_HEAD(&info->node);
816 
817 	ret = scmi_xfer_info_init(info);
818 	if (ret)
819 		return ret;
820 
821 	platform_set_drvdata(pdev, info);
822 	idr_init(&info->tx_idr);
823 
824 	handle = &info->handle;
825 	handle->dev = info->dev;
826 	handle->version = &info->version;
827 
828 	ret = scmi_mbox_chan_setup(info, dev, SCMI_PROTOCOL_BASE);
829 	if (ret)
830 		return ret;
831 
832 	ret = scmi_base_protocol_init(handle);
833 	if (ret) {
834 		dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
835 		return ret;
836 	}
837 
838 	mutex_lock(&scmi_list_mutex);
839 	list_add_tail(&info->node, &scmi_list);
840 	mutex_unlock(&scmi_list_mutex);
841 
842 	for_each_available_child_of_node(np, child) {
843 		u32 prot_id;
844 
845 		if (of_property_read_u32(child, "reg", &prot_id))
846 			continue;
847 
848 		if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
849 			dev_err(dev, "Out of range protocol %d\n", prot_id);
850 
851 		if (!scmi_is_protocol_implemented(handle, prot_id)) {
852 			dev_err(dev, "SCMI protocol %d not implemented\n",
853 				prot_id);
854 			continue;
855 		}
856 
857 		scmi_create_protocol_device(child, info, prot_id);
858 	}
859 
860 	return 0;
861 }
862 
863 static struct platform_driver scmi_driver = {
864 	.driver = {
865 		   .name = "arm-scmi",
866 		   .of_match_table = scmi_of_match,
867 		   },
868 	.probe = scmi_probe,
869 	.remove = scmi_remove,
870 };
871 
872 module_platform_driver(scmi_driver);
873 
874 MODULE_ALIAS("platform: arm-scmi");
875 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
876 MODULE_DESCRIPTION("ARM SCMI protocol driver");
877 MODULE_LICENSE("GPL v2");
878