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