Linux Audio

Check our new training course

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