• 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-2020 ARM Ltd.
15  */
16 
17 #include <linux/bitmap.h>
18 #include <linux/device.h>
19 #include <linux/export.h>
20 #include <linux/idr.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/ktime.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/processor.h>
29 #include <linux/refcount.h>
30 #include <linux/slab.h>
31 
32 #include "common.h"
33 #include "notify.h"
34 
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/scmi.h>
37 #undef CREATE_TRACE_POINTS
38 #include <trace/hooks/scmi.h>
39 
40 enum scmi_error_codes {
41 	SCMI_SUCCESS = 0,	/* Success */
42 	SCMI_ERR_SUPPORT = -1,	/* Not supported */
43 	SCMI_ERR_PARAMS = -2,	/* Invalid Parameters */
44 	SCMI_ERR_ACCESS = -3,	/* Invalid access/permission denied */
45 	SCMI_ERR_ENTRY = -4,	/* Not found */
46 	SCMI_ERR_RANGE = -5,	/* Value out of range */
47 	SCMI_ERR_BUSY = -6,	/* Device busy */
48 	SCMI_ERR_COMMS = -7,	/* Communication Error */
49 	SCMI_ERR_GENERIC = -8,	/* Generic Error */
50 	SCMI_ERR_HARDWARE = -9,	/* Hardware Error */
51 	SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
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 /* Track the unique id for the transfers for debug & profiling purpose */
59 static atomic_t transfer_last_id;
60 
61 static DEFINE_IDR(scmi_requested_devices);
62 static DEFINE_MUTEX(scmi_requested_devices_mtx);
63 
64 struct scmi_requested_dev {
65 	const struct scmi_device_id *id_table;
66 	struct list_head node;
67 };
68 
69 /**
70  * struct scmi_xfers_info - Structure to manage transfer information
71  *
72  * @xfer_block: Preallocated Message array
73  * @xfer_alloc_table: Bitmap table for allocated messages.
74  *	Index of this bitmap table is also used for message
75  *	sequence identifier.
76  * @xfer_lock: Protection for message allocation
77  */
78 struct scmi_xfers_info {
79 	struct scmi_xfer *xfer_block;
80 	unsigned long *xfer_alloc_table;
81 	spinlock_t xfer_lock;
82 };
83 
84 /**
85  * struct scmi_protocol_instance  - Describe an initialized protocol instance.
86  * @handle: Reference to the SCMI handle associated to this protocol instance.
87  * @proto: A reference to the protocol descriptor.
88  * @gid: A reference for per-protocol devres management.
89  * @users: A refcount to track effective users of this protocol.
90  * @priv: Reference for optional protocol private data.
91  * @ph: An embedded protocol handle that will be passed down to protocol
92  *	initialization code to identify this instance.
93  *
94  * Each protocol is initialized independently once for each SCMI platform in
95  * which is defined by DT and implemented by the SCMI server fw.
96  */
97 struct scmi_protocol_instance {
98 	const struct scmi_handle	*handle;
99 	const struct scmi_protocol	*proto;
100 	void				*gid;
101 	refcount_t			users;
102 	void				*priv;
103 	struct scmi_protocol_handle	ph;
104 };
105 
106 #define ph_to_pi(h)	container_of(h, struct scmi_protocol_instance, ph)
107 
108 /**
109  * struct scmi_info - Structure representing a SCMI instance
110  *
111  * @dev: Device pointer
112  * @desc: SoC description for this instance
113  * @version: SCMI revision information containing protocol version,
114  *	implementation version and (sub-)vendor identification.
115  * @handle: Instance of SCMI handle to send to clients
116  * @tx_minfo: Universal Transmit Message management info
117  * @rx_minfo: Universal Receive Message management info
118  * @tx_idr: IDR object to map protocol id to Tx channel info pointer
119  * @rx_idr: IDR object to map protocol id to Rx channel info pointer
120  * @protocols: IDR for protocols' instance descriptors initialized for
121  *	       this SCMI instance: populated on protocol's first attempted
122  *	       usage.
123  * @protocols_mtx: A mutex to protect protocols instances initialization.
124  * @protocols_imp: List of protocols implemented, currently maximum of
125  *	MAX_PROTOCOLS_IMP elements allocated by the base protocol
126  * @active_protocols: IDR storing device_nodes for protocols actually defined
127  *		      in the DT and confirmed as implemented by fw.
128  * @notify_priv: Pointer to private data structure specific to notifications.
129  * @node: List head
130  * @users: Number of users of this instance
131  */
132 struct scmi_info {
133 	struct device *dev;
134 	const struct scmi_desc *desc;
135 	struct scmi_revision_info version;
136 	struct scmi_handle handle;
137 	struct scmi_xfers_info tx_minfo;
138 	struct scmi_xfers_info rx_minfo;
139 	struct idr tx_idr;
140 	struct idr rx_idr;
141 	struct idr protocols;
142 	/* Ensure mutual exclusive access to protocols instance array */
143 	struct mutex protocols_mtx;
144 	u8 *protocols_imp;
145 	struct idr active_protocols;
146 	void *notify_priv;
147 	struct list_head node;
148 	int users;
149 };
150 
151 #define handle_to_scmi_info(h)	container_of(h, struct scmi_info, handle)
152 
153 static const int scmi_linux_errmap[] = {
154 	/* better than switch case as long as return value is continuous */
155 	0,			/* SCMI_SUCCESS */
156 	-EOPNOTSUPP,		/* SCMI_ERR_SUPPORT */
157 	-EINVAL,		/* SCMI_ERR_PARAM */
158 	-EACCES,		/* SCMI_ERR_ACCESS */
159 	-ENOENT,		/* SCMI_ERR_ENTRY */
160 	-ERANGE,		/* SCMI_ERR_RANGE */
161 	-EBUSY,			/* SCMI_ERR_BUSY */
162 	-ECOMM,			/* SCMI_ERR_COMMS */
163 	-EIO,			/* SCMI_ERR_GENERIC */
164 	-EREMOTEIO,		/* SCMI_ERR_HARDWARE */
165 	-EPROTO,		/* SCMI_ERR_PROTOCOL */
166 };
167 
scmi_to_linux_errno(int errno)168 static inline int scmi_to_linux_errno(int errno)
169 {
170 	int err_idx = -errno;
171 
172 	if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
173 		return scmi_linux_errmap[err_idx];
174 	return -EIO;
175 }
176 
177 /**
178  * scmi_dump_header_dbg() - Helper to dump a message header.
179  *
180  * @dev: Device pointer corresponding to the SCMI entity
181  * @hdr: pointer to header.
182  */
scmi_dump_header_dbg(struct device * dev,struct scmi_msg_hdr * hdr)183 static inline void scmi_dump_header_dbg(struct device *dev,
184 					struct scmi_msg_hdr *hdr)
185 {
186 	dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
187 		hdr->id, hdr->seq, hdr->protocol_id);
188 }
189 
scmi_set_notification_instance_data(const struct scmi_handle * handle,void * priv)190 void scmi_set_notification_instance_data(const struct scmi_handle *handle,
191 					 void *priv)
192 {
193 	struct scmi_info *info = handle_to_scmi_info(handle);
194 
195 	info->notify_priv = priv;
196 	/* Ensure updated protocol private date are visible */
197 	smp_wmb();
198 }
199 
scmi_get_notification_instance_data(const struct scmi_handle * handle)200 void *scmi_get_notification_instance_data(const struct scmi_handle *handle)
201 {
202 	struct scmi_info *info = handle_to_scmi_info(handle);
203 
204 	/* Ensure protocols_private_data has been updated */
205 	smp_rmb();
206 	return info->notify_priv;
207 }
208 
209 /**
210  * scmi_xfer_get() - Allocate one message
211  *
212  * @handle: Pointer to SCMI entity handle
213  * @minfo: Pointer to Tx/Rx Message management info based on channel type
214  *
215  * Helper function which is used by various message functions that are
216  * exposed to clients of this driver for allocating a message traffic event.
217  *
218  * This function can sleep depending on pending requests already in the system
219  * for the SCMI entity. Further, this also holds a spinlock to maintain
220  * integrity of internal data structures.
221  *
222  * Return: 0 if all went fine, else corresponding error.
223  */
scmi_xfer_get(const struct scmi_handle * handle,struct scmi_xfers_info * minfo)224 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
225 				       struct scmi_xfers_info *minfo)
226 {
227 	u16 xfer_id;
228 	struct scmi_xfer *xfer;
229 	unsigned long flags, bit_pos;
230 	struct scmi_info *info = handle_to_scmi_info(handle);
231 
232 	/* Keep the locked section as small as possible */
233 	spin_lock_irqsave(&minfo->xfer_lock, flags);
234 	bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
235 				      info->desc->max_msg);
236 	if (bit_pos == info->desc->max_msg) {
237 		spin_unlock_irqrestore(&minfo->xfer_lock, flags);
238 		return ERR_PTR(-ENOMEM);
239 	}
240 	set_bit(bit_pos, minfo->xfer_alloc_table);
241 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
242 
243 	xfer_id = bit_pos;
244 
245 	xfer = &minfo->xfer_block[xfer_id];
246 	xfer->hdr.seq = xfer_id;
247 	reinit_completion(&xfer->done);
248 	xfer->transfer_id = atomic_inc_return(&transfer_last_id);
249 
250 	return xfer;
251 }
252 
253 /**
254  * __scmi_xfer_put() - Release a message
255  *
256  * @minfo: Pointer to Tx/Rx Message management info based on channel type
257  * @xfer: message that was reserved by scmi_xfer_get
258  *
259  * This holds a spinlock to maintain integrity of internal data structures.
260  */
261 static void
__scmi_xfer_put(struct scmi_xfers_info * minfo,struct scmi_xfer * xfer)262 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
263 {
264 	unsigned long flags;
265 
266 	/*
267 	 * Keep the locked section as small as possible
268 	 * NOTE: we might escape with smp_mb and no lock here..
269 	 * but just be conservative and symmetric.
270 	 */
271 	spin_lock_irqsave(&minfo->xfer_lock, flags);
272 	clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
273 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
274 }
275 
scmi_handle_notification(struct scmi_chan_info * cinfo,u32 msg_hdr)276 static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr)
277 {
278 	struct scmi_xfer *xfer;
279 	struct device *dev = cinfo->dev;
280 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
281 	struct scmi_xfers_info *minfo = &info->rx_minfo;
282 	ktime_t ts;
283 
284 	ts = ktime_get_boottime();
285 	xfer = scmi_xfer_get(cinfo->handle, minfo);
286 	if (IS_ERR(xfer)) {
287 		dev_err(dev, "failed to get free message slot (%ld)\n",
288 			PTR_ERR(xfer));
289 		info->desc->ops->clear_channel(cinfo);
290 		return;
291 	}
292 
293 	unpack_scmi_header(msg_hdr, &xfer->hdr);
294 	scmi_dump_header_dbg(dev, &xfer->hdr);
295 	info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
296 					    xfer);
297 	scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
298 		    xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
299 
300 	trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
301 			   xfer->hdr.protocol_id, xfer->hdr.seq,
302 			   MSG_TYPE_NOTIFICATION);
303 
304 	__scmi_xfer_put(minfo, xfer);
305 
306 	info->desc->ops->clear_channel(cinfo);
307 }
308 
scmi_handle_response(struct scmi_chan_info * cinfo,u16 xfer_id,u8 msg_type)309 static void scmi_handle_response(struct scmi_chan_info *cinfo,
310 				 u16 xfer_id, u8 msg_type)
311 {
312 	struct scmi_xfer *xfer;
313 	struct device *dev = cinfo->dev;
314 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
315 	struct scmi_xfers_info *minfo = &info->tx_minfo;
316 
317 	/* Are we even expecting this? */
318 	if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
319 		dev_err(dev, "message for %d is not expected!\n", xfer_id);
320 		info->desc->ops->clear_channel(cinfo);
321 		return;
322 	}
323 
324 	xfer = &minfo->xfer_block[xfer_id];
325 	/*
326 	 * Even if a response was indeed expected on this slot at this point,
327 	 * a buggy platform could wrongly reply feeding us an unexpected
328 	 * delayed response we're not prepared to handle: bail-out safely
329 	 * blaming firmware.
330 	 */
331 	if (unlikely(msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done)) {
332 		dev_err(dev,
333 			"Delayed Response for %d not expected! Buggy F/W ?\n",
334 			xfer_id);
335 		info->desc->ops->clear_channel(cinfo);
336 		/* It was unexpected, so nobody will clear the xfer if not us */
337 		__scmi_xfer_put(minfo, xfer);
338 		return;
339 	}
340 
341 	/* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
342 	if (msg_type == MSG_TYPE_DELAYED_RESP)
343 		xfer->rx.len = info->desc->max_msg_size;
344 
345 	scmi_dump_header_dbg(dev, &xfer->hdr);
346 
347 	info->desc->ops->fetch_response(cinfo, xfer);
348 
349 	trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
350 			   xfer->hdr.protocol_id, xfer->hdr.seq,
351 			   msg_type);
352 
353 	if (msg_type == MSG_TYPE_DELAYED_RESP) {
354 		info->desc->ops->clear_channel(cinfo);
355 		complete(xfer->async_done);
356 	} else {
357 		complete(&xfer->done);
358 	}
359 }
360 
361 /**
362  * scmi_rx_callback() - callback for receiving messages
363  *
364  * @cinfo: SCMI channel info
365  * @msg_hdr: Message header
366  *
367  * Processes one received message to appropriate transfer information and
368  * signals completion of the transfer.
369  *
370  * NOTE: This function will be invoked in IRQ context, hence should be
371  * as optimal as possible.
372  */
scmi_rx_callback(struct scmi_chan_info * cinfo,u32 msg_hdr)373 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr)
374 {
375 	u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
376 	u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
377 
378 	switch (msg_type) {
379 	case MSG_TYPE_NOTIFICATION:
380 		scmi_handle_notification(cinfo, msg_hdr);
381 		break;
382 	case MSG_TYPE_COMMAND:
383 	case MSG_TYPE_DELAYED_RESP:
384 		scmi_handle_response(cinfo, xfer_id, msg_type);
385 		break;
386 	default:
387 		WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
388 		break;
389 	}
390 }
391 
392 /**
393  * xfer_put() - Release a transmit message
394  *
395  * @ph: Pointer to SCMI protocol handle
396  * @xfer: message that was reserved by scmi_xfer_get
397  */
xfer_put(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)398 static void xfer_put(const struct scmi_protocol_handle *ph,
399 		     struct scmi_xfer *xfer)
400 {
401 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
402 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
403 
404 	__scmi_xfer_put(&info->tx_minfo, xfer);
405 }
406 
407 #define SCMI_MAX_POLL_TO_NS	(100 * NSEC_PER_USEC)
408 
scmi_xfer_done_no_timeout(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer,ktime_t stop)409 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
410 				      struct scmi_xfer *xfer, ktime_t stop)
411 {
412 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
413 
414 	return info->desc->ops->poll_done(cinfo, xfer) ||
415 	       ktime_after(ktime_get(), stop);
416 }
417 
418 /**
419  * do_xfer() - Do one transfer
420  *
421  * @ph: Pointer to SCMI protocol handle
422  * @xfer: Transfer to initiate and wait for response
423  *
424  * Return: -ETIMEDOUT in case of no response, if transmit error,
425  *	return corresponding error, else if all goes well,
426  *	return 0.
427  */
do_xfer(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)428 static int do_xfer(const struct scmi_protocol_handle *ph,
429 		   struct scmi_xfer *xfer)
430 {
431 	int ret;
432 	int timeout;
433 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
434 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
435 	struct device *dev = info->dev;
436 	struct scmi_chan_info *cinfo;
437 
438 	/*
439 	 * Re-instate protocol id here from protocol handle so that cannot be
440 	 * overridden by mistake (or malice) by the protocol code mangling with
441 	 * the scmi_xfer structure.
442 	 */
443 	xfer->hdr.protocol_id = pi->proto->id;
444 
445 	cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
446 	if (unlikely(!cinfo))
447 		return -EINVAL;
448 
449 	trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
450 			      xfer->hdr.protocol_id, xfer->hdr.seq,
451 			      xfer->hdr.poll_completion);
452 
453 	ret = info->desc->ops->send_message(cinfo, xfer);
454 	if (ret < 0) {
455 		dev_dbg(dev, "Failed to send message %d\n", ret);
456 		return ret;
457 	}
458 
459 	if (xfer->hdr.poll_completion) {
460 		ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
461 
462 		spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
463 
464 		if (ktime_before(ktime_get(), stop))
465 			info->desc->ops->fetch_response(cinfo, xfer);
466 		else
467 			ret = -ETIMEDOUT;
468 	} else {
469 		/* And we wait for the response. */
470 		timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
471 		trace_android_vh_scmi_timeout_sync(&timeout);
472 		if (!wait_for_completion_timeout(&xfer->done, timeout)) {
473 			dev_err(dev, "timed out in resp(caller: %pS)\n",
474 				(void *)_RET_IP_);
475 			ret = -ETIMEDOUT;
476 		}
477 	}
478 
479 	if (!ret && xfer->hdr.status)
480 		ret = scmi_to_linux_errno(xfer->hdr.status);
481 
482 	if (info->desc->ops->mark_txdone)
483 		info->desc->ops->mark_txdone(cinfo, ret);
484 
485 	trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
486 			    xfer->hdr.protocol_id, xfer->hdr.seq, ret);
487 
488 	return ret;
489 }
490 
reset_rx_to_maxsz(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)491 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
492 			      struct scmi_xfer *xfer)
493 {
494 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
495 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
496 
497 	xfer->rx.len = info->desc->max_msg_size;
498 }
499 
500 #define SCMI_MAX_RESPONSE_TIMEOUT	(2 * MSEC_PER_SEC)
501 
502 /**
503  * do_xfer_with_response() - Do one transfer and wait until the delayed
504  *	response is received
505  *
506  * @ph: Pointer to SCMI protocol handle
507  * @xfer: Transfer to initiate and wait for response
508  *
509  * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
510  *	return corresponding error, else if all goes well, return 0.
511  */
do_xfer_with_response(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)512 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
513 				 struct scmi_xfer *xfer)
514 {
515 	int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
516 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
517 	DECLARE_COMPLETION_ONSTACK(async_response);
518 
519 	xfer->hdr.protocol_id = pi->proto->id;
520 
521 	xfer->async_done = &async_response;
522 
523 	ret = do_xfer(ph, xfer);
524 	if (!ret) {
525 		if (!wait_for_completion_timeout(xfer->async_done, timeout))
526 			ret = -ETIMEDOUT;
527 		else if (xfer->hdr.status)
528 			ret = scmi_to_linux_errno(xfer->hdr.status);
529 	}
530 
531 	xfer->async_done = NULL;
532 	return ret;
533 }
534 
535 /**
536  * xfer_get_init() - Allocate and initialise one message for transmit
537  *
538  * @ph: Pointer to SCMI protocol handle
539  * @msg_id: Message identifier
540  * @tx_size: transmit message size
541  * @rx_size: receive message size
542  * @p: pointer to the allocated and initialised message
543  *
544  * This function allocates the message using @scmi_xfer_get and
545  * initialise the header.
546  *
547  * Return: 0 if all went fine with @p pointing to message, else
548  *	corresponding error.
549  */
xfer_get_init(const struct scmi_protocol_handle * ph,u8 msg_id,size_t tx_size,size_t rx_size,struct scmi_xfer ** p)550 static int xfer_get_init(const struct scmi_protocol_handle *ph,
551 			 u8 msg_id, size_t tx_size, size_t rx_size,
552 			 struct scmi_xfer **p)
553 {
554 	int ret;
555 	struct scmi_xfer *xfer;
556 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
557 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
558 	struct scmi_xfers_info *minfo = &info->tx_minfo;
559 	struct device *dev = info->dev;
560 
561 	/* Ensure we have sane transfer sizes */
562 	if (rx_size > info->desc->max_msg_size ||
563 	    tx_size > info->desc->max_msg_size)
564 		return -ERANGE;
565 
566 	xfer = scmi_xfer_get(pi->handle, minfo);
567 	if (IS_ERR(xfer)) {
568 		ret = PTR_ERR(xfer);
569 		dev_err(dev, "failed to get free message slot(%d)\n", ret);
570 		return ret;
571 	}
572 
573 	xfer->tx.len = tx_size;
574 	xfer->rx.len = rx_size ? : info->desc->max_msg_size;
575 	xfer->hdr.id = msg_id;
576 	xfer->hdr.protocol_id = pi->proto->id;
577 	xfer->hdr.poll_completion = false;
578 
579 	*p = xfer;
580 
581 	return 0;
582 }
583 
584 /**
585  * version_get() - command to get the revision of the SCMI entity
586  *
587  * @ph: Pointer to SCMI protocol handle
588  * @version: Holds returned version of protocol.
589  *
590  * Updates the SCMI information in the internal data structure.
591  *
592  * Return: 0 if all went fine, else return appropriate error.
593  */
version_get(const struct scmi_protocol_handle * ph,u32 * version)594 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
595 {
596 	int ret;
597 	__le32 *rev_info;
598 	struct scmi_xfer *t;
599 
600 	ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
601 	if (ret)
602 		return ret;
603 
604 	ret = do_xfer(ph, t);
605 	if (!ret) {
606 		rev_info = t->rx.buf;
607 		*version = le32_to_cpu(*rev_info);
608 	}
609 
610 	xfer_put(ph, t);
611 	return ret;
612 }
613 
614 /**
615  * scmi_set_protocol_priv  - Set protocol specific data at init time
616  *
617  * @ph: A reference to the protocol handle.
618  * @priv: The private data to set.
619  *
620  * Return: 0 on Success
621  */
scmi_set_protocol_priv(const struct scmi_protocol_handle * ph,void * priv)622 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
623 				  void *priv)
624 {
625 	struct scmi_protocol_instance *pi = ph_to_pi(ph);
626 
627 	pi->priv = priv;
628 
629 	return 0;
630 }
631 
632 /**
633  * scmi_get_protocol_priv  - Set protocol specific data at init time
634  *
635  * @ph: A reference to the protocol handle.
636  *
637  * Return: Protocol private data if any was set.
638  */
scmi_get_protocol_priv(const struct scmi_protocol_handle * ph)639 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
640 {
641 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
642 
643 	return pi->priv;
644 }
645 
646 static const struct scmi_xfer_ops xfer_ops = {
647 	.version_get = version_get,
648 	.xfer_get_init = xfer_get_init,
649 	.reset_rx_to_maxsz = reset_rx_to_maxsz,
650 	.do_xfer = do_xfer,
651 	.do_xfer_with_response = do_xfer_with_response,
652 	.xfer_put = xfer_put,
653 };
654 
655 /**
656  * scmi_get_revision_area  - Retrieve version memory area.
657  *
658  * @ph: A reference to the protocol handle.
659  *
660  * A helper to grab the version memory area reference during SCMI Base protocol
661  * initialization.
662  *
663  * Return: A reference to the version memory area associated to the SCMI
664  *	   instance underlying this protocol handle.
665  */
666 struct scmi_revision_info *
scmi_get_revision_area(const struct scmi_protocol_handle * ph)667 scmi_get_revision_area(const struct scmi_protocol_handle *ph)
668 {
669 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
670 
671 	return pi->handle->version;
672 }
673 
674 /**
675  * scmi_get_protocol_instance  - Protocol initialization helper.
676  * @handle: A reference to the SCMI platform instance.
677  * @protocol_id: The protocol being requested.
678  *
679  * In case the required protocol has never been requested before for this
680  * instance, allocate and initialize all the needed structures while handling
681  * resource allocation with a dedicated per-protocol devres subgroup.
682  *
683  * Return: A reference to an initialized protocol instance or error on failure.
684  */
685 static struct scmi_protocol_instance * __must_check
scmi_get_protocol_instance(const struct scmi_handle * handle,u8 protocol_id)686 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
687 {
688 	int ret = -ENOMEM;
689 	void *gid;
690 	struct scmi_protocol_instance *pi;
691 	struct scmi_info *info = handle_to_scmi_info(handle);
692 
693 	mutex_lock(&info->protocols_mtx);
694 	pi = idr_find(&info->protocols, protocol_id);
695 
696 	if (pi) {
697 		refcount_inc(&pi->users);
698 	} else {
699 		const struct scmi_protocol *proto;
700 
701 		/* Fail if protocol not registered on bus */
702 		proto = scmi_get_protocol(protocol_id);
703 		if (!proto) {
704 			ret = -EPROBE_DEFER;
705 			goto out;
706 		}
707 
708 		/* Protocol specific devres group */
709 		gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
710 		if (!gid)
711 			goto out;
712 
713 		pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
714 		if (!pi)
715 			goto clean;
716 
717 		pi->gid = gid;
718 		pi->proto = proto;
719 		pi->handle = handle;
720 		pi->ph.dev = handle->dev;
721 		pi->ph.xops = &xfer_ops;
722 		pi->ph.set_priv = scmi_set_protocol_priv;
723 		pi->ph.get_priv = scmi_get_protocol_priv;
724 		refcount_set(&pi->users, 1);
725 		/* proto->init is assured NON NULL by scmi_protocol_register */
726 		ret = pi->proto->init_instance(&pi->ph);
727 		if (ret)
728 			goto clean;
729 
730 		ret = idr_alloc(&info->protocols, pi,
731 				protocol_id, protocol_id + 1, GFP_KERNEL);
732 		if (ret != protocol_id)
733 			goto clean;
734 
735 		if (pi->proto->events)
736 			scmi_register_protocol_events(handle, pi->proto->id,
737 						      &pi->ph,
738 						      pi->proto->events);
739 
740 		devres_close_group(handle->dev, pi->gid);
741 		dev_dbg(handle->dev, "Initialized protocol: 0x%X\n",
742 			protocol_id);
743 	}
744 	mutex_unlock(&info->protocols_mtx);
745 
746 	return pi;
747 
748 clean:
749 	scmi_put_protocol(protocol_id);
750 	devres_release_group(handle->dev, gid);
751 out:
752 	mutex_unlock(&info->protocols_mtx);
753 	return ERR_PTR(ret);
754 }
755 
756 /**
757  * scmi_acquire_protocol  - Protocol acquire
758  * @handle: A reference to the SCMI platform instance.
759  * @protocol_id: The protocol being requested.
760  *
761  * Register a new user for the requested protocol on the specified SCMI
762  * platform instance, possibly triggering its initialization on first user.
763  *
764  * Return: 0 if protocol was acquired successfully.
765  */
scmi_acquire_protocol(const struct scmi_handle * handle,u8 protocol_id)766 int scmi_acquire_protocol(const struct scmi_handle *handle, u8 protocol_id)
767 {
768 	return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
769 }
770 
771 /**
772  * scmi_release_protocol  - Protocol de-initialization helper.
773  * @handle: A reference to the SCMI platform instance.
774  * @protocol_id: The protocol being requested.
775  *
776  * Remove one user for the specified protocol and triggers de-initialization
777  * and resources de-allocation once the last user has gone.
778  */
scmi_release_protocol(const struct scmi_handle * handle,u8 protocol_id)779 void scmi_release_protocol(const struct scmi_handle *handle, u8 protocol_id)
780 {
781 	struct scmi_info *info = handle_to_scmi_info(handle);
782 	struct scmi_protocol_instance *pi;
783 
784 	mutex_lock(&info->protocols_mtx);
785 	pi = idr_find(&info->protocols, protocol_id);
786 	if (WARN_ON(!pi))
787 		goto out;
788 
789 	if (refcount_dec_and_test(&pi->users)) {
790 		void *gid = pi->gid;
791 
792 		if (pi->proto->events)
793 			scmi_deregister_protocol_events(handle, protocol_id);
794 
795 		if (pi->proto->deinit_instance)
796 			pi->proto->deinit_instance(&pi->ph);
797 
798 		idr_remove(&info->protocols, protocol_id);
799 
800 		scmi_put_protocol(protocol_id);
801 
802 		devres_release_group(handle->dev, gid);
803 		dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
804 			protocol_id);
805 	}
806 
807 out:
808 	mutex_unlock(&info->protocols_mtx);
809 }
810 
scmi_setup_protocol_implemented(const struct scmi_protocol_handle * ph,u8 * prot_imp)811 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
812 				     u8 *prot_imp)
813 {
814 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
815 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
816 
817 	info->protocols_imp = prot_imp;
818 }
819 
820 static bool
scmi_is_protocol_implemented(const struct scmi_handle * handle,u8 prot_id)821 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
822 {
823 	int i;
824 	struct scmi_info *info = handle_to_scmi_info(handle);
825 
826 	if (!info->protocols_imp)
827 		return false;
828 
829 	for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
830 		if (info->protocols_imp[i] == prot_id)
831 			return true;
832 	return false;
833 }
834 
835 struct scmi_protocol_devres {
836 	const struct scmi_handle *handle;
837 	u8 protocol_id;
838 };
839 
scmi_devm_release_protocol(struct device * dev,void * res)840 static void scmi_devm_release_protocol(struct device *dev, void *res)
841 {
842 	struct scmi_protocol_devres *dres = res;
843 
844 	scmi_release_protocol(dres->handle, dres->protocol_id);
845 }
846 
847 static struct scmi_protocol_instance __must_check *
__scmi_devres_get_protocol_instance(struct scmi_device * sdev,u8 protocol_id)848 __scmi_devres_get_protocol_instance(struct scmi_device *sdev, u8 protocol_id)
849 {
850 	struct scmi_protocol_devres *dres;
851 	struct scmi_protocol_instance *pi;
852 
853 	dres = devres_alloc(scmi_devm_release_protocol,
854 			    sizeof(*dres), GFP_KERNEL);
855 	if (!dres)
856 		return ERR_PTR(-ENOMEM);
857 
858 	pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
859 	if (IS_ERR(pi)) {
860 		devres_free(dres);
861 		return pi;
862 	}
863 
864 	dres->handle = sdev->handle;
865 	dres->protocol_id = protocol_id;
866 	devres_add(&sdev->dev, dres);
867 
868 	return pi;
869 }
870 
871 /**
872  * scmi_devm_get_protocol  - Devres managed get protocol operations and handle
873  * @sdev: A reference to an scmi_device whose embedded struct device is to
874  *	  be used for devres accounting.
875  * @protocol_id: The protocol being requested.
876  * @ph: A pointer reference used to pass back the associated protocol handle.
877  *
878  * Get hold of a protocol accounting for its usage, eventually triggering its
879  * initialization, and returning the protocol specific operations and related
880  * protocol handle which will be used as first argument in most of the
881  * protocols operations methods.
882  * Being a devres based managed method, protocol hold will be automatically
883  * released, and possibly de-initialized on last user, once the SCMI driver
884  * owning the scmi_device is unbound from it.
885  *
886  * Return: A reference to the requested protocol operations or error.
887  *	   Must be checked for errors by caller.
888  */
889 static const void __must_check *
scmi_devm_get_protocol(struct scmi_device * sdev,u8 protocol_id,struct scmi_protocol_handle ** ph)890 scmi_devm_get_protocol(struct scmi_device *sdev, u8 protocol_id,
891 		       struct scmi_protocol_handle **ph)
892 {
893 	struct scmi_protocol_instance *pi;
894 
895 	if (!ph)
896 		return ERR_PTR(-EINVAL);
897 
898 	pi = __scmi_devres_get_protocol_instance(sdev, protocol_id);
899 	if (IS_ERR(pi))
900 		return pi;
901 
902 	*ph = &pi->ph;
903 
904 	return pi->proto->ops;
905 }
906 
907 /**
908  * scmi_devm_acquire_protocol  - Devres managed helper to get hold of a protocol
909  * @sdev: A reference to an scmi_device whose embedded struct device is to
910  *	  be used for devres accounting.
911  * @protocol_id: The protocol being requested.
912  *
913  * Get hold of a protocol accounting for its usage, possibly triggering its
914  * initialization but without getting access to its protocol specific operations
915  * and handle.
916  *
917  * Being a devres based managed method, protocol hold will be automatically
918  * released, and possibly de-initialized on last user, once the SCMI driver
919  * owning the scmi_device is unbound from it.
920  *
921  * Return: 0 on SUCCESS
922  */
scmi_devm_acquire_protocol(struct scmi_device * sdev,u8 protocol_id)923 static int __must_check scmi_devm_acquire_protocol(struct scmi_device *sdev,
924 						   u8 protocol_id)
925 {
926 	struct scmi_protocol_instance *pi;
927 
928 	pi = __scmi_devres_get_protocol_instance(sdev, protocol_id);
929 	if (IS_ERR(pi))
930 		return PTR_ERR(pi);
931 
932 	return 0;
933 }
934 
scmi_devm_protocol_match(struct device * dev,void * res,void * data)935 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
936 {
937 	struct scmi_protocol_devres *dres = res;
938 
939 	if (WARN_ON(!dres || !data))
940 		return 0;
941 
942 	return dres->protocol_id == *((u8 *)data);
943 }
944 
945 /**
946  * scmi_devm_put_protocol  - Devres managed put protocol operations and handle
947  * @sdev: A reference to an scmi_device whose embedded struct device is to
948  *	  be used for devres accounting.
949  * @protocol_id: The protocol being requested.
950  *
951  * Explicitly release a protocol hold previously obtained calling the above
952  * @scmi_devm_get_protocol_ops.
953  */
scmi_devm_put_protocol(struct scmi_device * sdev,u8 protocol_id)954 static void scmi_devm_put_protocol(struct scmi_device *sdev, u8 protocol_id)
955 {
956 	int ret;
957 
958 	ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
959 			     scmi_devm_protocol_match, &protocol_id);
960 	WARN_ON(ret);
961 }
962 
963 static inline
scmi_handle_get_from_info(struct scmi_info * info)964 struct scmi_handle *scmi_handle_get_from_info(struct scmi_info *info)
965 {
966 	info->users++;
967 	return &info->handle;
968 }
969 
970 /**
971  * scmi_handle_get() - Get the SCMI handle for a device
972  *
973  * @dev: pointer to device for which we want SCMI handle
974  *
975  * NOTE: The function does not track individual clients of the framework
976  * and is expected to be maintained by caller of SCMI protocol library.
977  * scmi_handle_put must be balanced with successful scmi_handle_get
978  *
979  * Return: pointer to handle if successful, NULL on error
980  */
scmi_handle_get(struct device * dev)981 struct scmi_handle *scmi_handle_get(struct device *dev)
982 {
983 	struct list_head *p;
984 	struct scmi_info *info;
985 	struct scmi_handle *handle = NULL;
986 
987 	mutex_lock(&scmi_list_mutex);
988 	list_for_each(p, &scmi_list) {
989 		info = list_entry(p, struct scmi_info, node);
990 		if (dev->parent == info->dev) {
991 			handle = scmi_handle_get_from_info(info);
992 			break;
993 		}
994 	}
995 	mutex_unlock(&scmi_list_mutex);
996 
997 	return handle;
998 }
999 
1000 /**
1001  * scmi_handle_put() - Release the handle acquired by scmi_handle_get
1002  *
1003  * @handle: handle acquired by scmi_handle_get
1004  *
1005  * NOTE: The function does not track individual clients of the framework
1006  * and is expected to be maintained by caller of SCMI protocol library.
1007  * scmi_handle_put must be balanced with successful scmi_handle_get
1008  *
1009  * Return: 0 is successfully released
1010  *	if null was passed, it returns -EINVAL;
1011  */
scmi_handle_put(const struct scmi_handle * handle)1012 int scmi_handle_put(const struct scmi_handle *handle)
1013 {
1014 	struct scmi_info *info;
1015 
1016 	if (!handle)
1017 		return -EINVAL;
1018 
1019 	info = handle_to_scmi_info(handle);
1020 	mutex_lock(&scmi_list_mutex);
1021 	if (!WARN_ON(!info->users))
1022 		info->users--;
1023 	mutex_unlock(&scmi_list_mutex);
1024 
1025 	return 0;
1026 }
1027 
__scmi_xfer_info_init(struct scmi_info * sinfo,struct scmi_xfers_info * info)1028 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1029 				 struct scmi_xfers_info *info)
1030 {
1031 	int i;
1032 	struct scmi_xfer *xfer;
1033 	struct device *dev = sinfo->dev;
1034 	const struct scmi_desc *desc = sinfo->desc;
1035 
1036 	/* Pre-allocated messages, no more than what hdr.seq can support */
1037 	if (WARN_ON(!desc->max_msg || desc->max_msg > MSG_TOKEN_MAX)) {
1038 		dev_err(dev,
1039 			"Invalid maximum messages %d, not in range [1 - %lu]\n",
1040 			desc->max_msg, MSG_TOKEN_MAX);
1041 		return -EINVAL;
1042 	}
1043 
1044 	info->xfer_block = devm_kcalloc(dev, desc->max_msg,
1045 					sizeof(*info->xfer_block), GFP_KERNEL);
1046 	if (!info->xfer_block)
1047 		return -ENOMEM;
1048 
1049 	info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
1050 					      sizeof(long), GFP_KERNEL);
1051 	if (!info->xfer_alloc_table)
1052 		return -ENOMEM;
1053 
1054 	/* Pre-initialize the buffer pointer to pre-allocated buffers */
1055 	for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
1056 		xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1057 					    GFP_KERNEL);
1058 		if (!xfer->rx.buf)
1059 			return -ENOMEM;
1060 
1061 		xfer->tx.buf = xfer->rx.buf;
1062 		init_completion(&xfer->done);
1063 	}
1064 
1065 	spin_lock_init(&info->xfer_lock);
1066 
1067 	return 0;
1068 }
1069 
scmi_xfer_info_init(struct scmi_info * sinfo)1070 static int scmi_xfer_info_init(struct scmi_info *sinfo)
1071 {
1072 	int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
1073 
1074 	if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1075 		ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1076 
1077 	return ret;
1078 }
1079 
scmi_chan_setup(struct scmi_info * info,struct device * dev,int prot_id,bool tx)1080 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1081 			   int prot_id, bool tx)
1082 {
1083 	int ret, idx;
1084 	struct scmi_chan_info *cinfo;
1085 	struct idr *idr;
1086 
1087 	/* Transmit channel is first entry i.e. index 0 */
1088 	idx = tx ? 0 : 1;
1089 	idr = tx ? &info->tx_idr : &info->rx_idr;
1090 
1091 	/* check if already allocated, used for multiple device per protocol */
1092 	cinfo = idr_find(idr, prot_id);
1093 	if (cinfo)
1094 		return 0;
1095 
1096 	if (!info->desc->ops->chan_available(dev, idx)) {
1097 		cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1098 		if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1099 			return -EINVAL;
1100 		goto idr_alloc;
1101 	}
1102 
1103 	cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1104 	if (!cinfo)
1105 		return -ENOMEM;
1106 
1107 	cinfo->dev = dev;
1108 
1109 	ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1110 	if (ret)
1111 		return ret;
1112 
1113 idr_alloc:
1114 	ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
1115 	if (ret != prot_id) {
1116 		dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1117 		return ret;
1118 	}
1119 
1120 	cinfo->handle = &info->handle;
1121 	return 0;
1122 }
1123 
1124 static inline int
scmi_txrx_setup(struct scmi_info * info,struct device * dev,int prot_id)1125 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
1126 {
1127 	int ret = scmi_chan_setup(info, dev, prot_id, true);
1128 
1129 	if (!ret) {
1130 		/* Rx is optional, report only memory errors */
1131 		ret = scmi_chan_setup(info, dev, prot_id, false);
1132 		if (ret && ret != -ENOMEM)
1133 			ret = 0;
1134 	}
1135 
1136 	return ret;
1137 }
1138 
1139 /**
1140  * scmi_get_protocol_device  - Helper to get/create an SCMI device.
1141  *
1142  * @np: A device node representing a valid active protocols for the referred
1143  * SCMI instance.
1144  * @info: The referred SCMI instance for which we are getting/creating this
1145  * device.
1146  * @prot_id: The protocol ID.
1147  * @name: The device name.
1148  *
1149  * Referring to the specific SCMI instance identified by @info, this helper
1150  * takes care to return a properly initialized device matching the requested
1151  * @proto_id and @name: if device was still not existent it is created as a
1152  * child of the specified SCMI instance @info and its transport properly
1153  * initialized as usual.
1154  */
1155 static inline struct scmi_device *
scmi_get_protocol_device(struct device_node * np,struct scmi_info * info,int prot_id,const char * name)1156 scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
1157 			 int prot_id, const char *name)
1158 {
1159 	struct scmi_device *sdev;
1160 
1161 	/* Already created for this parent SCMI instance ? */
1162 	sdev = scmi_find_child_dev(info->dev, prot_id, name);
1163 	if (sdev)
1164 		return sdev;
1165 
1166 	pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
1167 
1168 	sdev = scmi_device_create(np, info->dev, prot_id, name);
1169 	if (!sdev) {
1170 		dev_err(info->dev, "failed to create %d protocol device\n",
1171 			prot_id);
1172 		return NULL;
1173 	}
1174 
1175 	if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
1176 		dev_err(&sdev->dev, "failed to setup transport\n");
1177 		scmi_device_destroy(sdev);
1178 		return NULL;
1179 	}
1180 
1181 	return sdev;
1182 }
1183 
1184 static inline void
scmi_create_protocol_device(struct device_node * np,struct scmi_info * info,int prot_id,const char * name)1185 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
1186 			    int prot_id, const char *name)
1187 {
1188 	struct scmi_device *sdev;
1189 
1190 	sdev = scmi_get_protocol_device(np, info, prot_id, name);
1191 	if (!sdev)
1192 		return;
1193 
1194 	/* setup handle now as the transport is ready */
1195 	scmi_set_handle(sdev);
1196 }
1197 
1198 /**
1199  * scmi_create_protocol_devices  - Create devices for all pending requests for
1200  * this SCMI instance.
1201  *
1202  * @np: The device node describing the protocol
1203  * @info: The SCMI instance descriptor
1204  * @prot_id: The protocol ID
1205  *
1206  * All devices previously requested for this instance (if any) are found and
1207  * created by scanning the proper @&scmi_requested_devices entry.
1208  */
scmi_create_protocol_devices(struct device_node * np,struct scmi_info * info,int prot_id)1209 static void scmi_create_protocol_devices(struct device_node *np,
1210 					 struct scmi_info *info, int prot_id)
1211 {
1212 	struct list_head *phead;
1213 
1214 	mutex_lock(&scmi_requested_devices_mtx);
1215 	phead = idr_find(&scmi_requested_devices, prot_id);
1216 	if (phead) {
1217 		struct scmi_requested_dev *rdev;
1218 
1219 		list_for_each_entry(rdev, phead, node)
1220 			scmi_create_protocol_device(np, info, prot_id,
1221 						    rdev->id_table->name);
1222 	}
1223 	mutex_unlock(&scmi_requested_devices_mtx);
1224 }
1225 
1226 /**
1227  * scmi_request_protocol_device  - Helper to request a device
1228  *
1229  * @id_table: A protocol/name pair descriptor for the device to be created.
1230  *
1231  * This helper let an SCMI driver request specific devices identified by the
1232  * @id_table to be created for each active SCMI instance.
1233  *
1234  * The requested device name MUST NOT be already existent for any protocol;
1235  * at first the freshly requested @id_table is annotated in the IDR table
1236  * @scmi_requested_devices, then a matching device is created for each already
1237  * active SCMI instance. (if any)
1238  *
1239  * This way the requested device is created straight-away for all the already
1240  * initialized(probed) SCMI instances (handles) and it remains also annotated
1241  * as pending creation if the requesting SCMI driver was loaded before some
1242  * SCMI instance and related transports were available: when such late instance
1243  * is probed, its probe will take care to scan the list of pending requested
1244  * devices and create those on its own (see @scmi_create_protocol_devices and
1245  * its enclosing loop)
1246  *
1247  * Return: 0 on Success
1248  */
scmi_request_protocol_device(const struct scmi_device_id * id_table)1249 int scmi_request_protocol_device(const struct scmi_device_id *id_table)
1250 {
1251 	int ret = 0;
1252 	unsigned int id = 0;
1253 	struct list_head *head, *phead = NULL;
1254 	struct scmi_requested_dev *rdev;
1255 	struct scmi_info *info;
1256 
1257 	pr_debug("Requesting SCMI device (%s) for protocol %x\n",
1258 		 id_table->name, id_table->protocol_id);
1259 
1260 	/*
1261 	 * Search for the matching protocol rdev list and then search
1262 	 * of any existent equally named device...fails if any duplicate found.
1263 	 */
1264 	mutex_lock(&scmi_requested_devices_mtx);
1265 	idr_for_each_entry(&scmi_requested_devices, head, id) {
1266 		if (!phead) {
1267 			/* A list found registered in the IDR is never empty */
1268 			rdev = list_first_entry(head, struct scmi_requested_dev,
1269 						node);
1270 			if (rdev->id_table->protocol_id ==
1271 			    id_table->protocol_id)
1272 				phead = head;
1273 		}
1274 		list_for_each_entry(rdev, head, node) {
1275 			if (!strcmp(rdev->id_table->name, id_table->name)) {
1276 				pr_err("Ignoring duplicate request [%d] %s\n",
1277 				       rdev->id_table->protocol_id,
1278 				       rdev->id_table->name);
1279 				ret = -EINVAL;
1280 				goto out;
1281 			}
1282 		}
1283 	}
1284 
1285 	/*
1286 	 * No duplicate found for requested id_table, so let's create a new
1287 	 * requested device entry for this new valid request.
1288 	 */
1289 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1290 	if (!rdev) {
1291 		ret = -ENOMEM;
1292 		goto out;
1293 	}
1294 	rdev->id_table = id_table;
1295 
1296 	/*
1297 	 * Append the new requested device table descriptor to the head of the
1298 	 * related protocol list, eventually creating such head if not already
1299 	 * there.
1300 	 */
1301 	if (!phead) {
1302 		phead = kzalloc(sizeof(*phead), GFP_KERNEL);
1303 		if (!phead) {
1304 			kfree(rdev);
1305 			ret = -ENOMEM;
1306 			goto out;
1307 		}
1308 		INIT_LIST_HEAD(phead);
1309 
1310 		ret = idr_alloc(&scmi_requested_devices, (void *)phead,
1311 				id_table->protocol_id,
1312 				id_table->protocol_id + 1, GFP_KERNEL);
1313 		if (ret != id_table->protocol_id) {
1314 			pr_err("Failed to save SCMI device - ret:%d\n", ret);
1315 			kfree(rdev);
1316 			kfree(phead);
1317 			ret = -EINVAL;
1318 			goto out;
1319 		}
1320 		ret = 0;
1321 	}
1322 	list_add(&rdev->node, phead);
1323 
1324 	/*
1325 	 * Now effectively create and initialize the requested device for every
1326 	 * already initialized SCMI instance which has registered the requested
1327 	 * protocol as a valid active one: i.e. defined in DT and supported by
1328 	 * current platform FW.
1329 	 */
1330 	mutex_lock(&scmi_list_mutex);
1331 	list_for_each_entry(info, &scmi_list, node) {
1332 		struct device_node *child;
1333 
1334 		child = idr_find(&info->active_protocols,
1335 				 id_table->protocol_id);
1336 		if (child) {
1337 			struct scmi_device *sdev;
1338 
1339 			sdev = scmi_get_protocol_device(child, info,
1340 							id_table->protocol_id,
1341 							id_table->name);
1342 			/* Set handle if not already set: device existed */
1343 			if (sdev && !sdev->handle)
1344 				sdev->handle = scmi_handle_get_from_info(info);
1345 		} else {
1346 			dev_err(info->dev,
1347 				"Failed. SCMI protocol %d not active.\n",
1348 				id_table->protocol_id);
1349 		}
1350 	}
1351 	mutex_unlock(&scmi_list_mutex);
1352 
1353 out:
1354 	mutex_unlock(&scmi_requested_devices_mtx);
1355 
1356 	return ret;
1357 }
1358 
1359 /**
1360  * scmi_unrequest_protocol_device  - Helper to unrequest a device
1361  *
1362  * @id_table: A protocol/name pair descriptor for the device to be unrequested.
1363  *
1364  * An helper to let an SCMI driver release its request about devices; note that
1365  * devices are created and initialized once the first SCMI driver request them
1366  * but they destroyed only on SCMI core unloading/unbinding.
1367  *
1368  * The current SCMI transport layer uses such devices as internal references and
1369  * as such they could be shared as same transport between multiple drivers so
1370  * that cannot be safely destroyed till the whole SCMI stack is removed.
1371  * (unless adding further burden of refcounting.)
1372  */
scmi_unrequest_protocol_device(const struct scmi_device_id * id_table)1373 void scmi_unrequest_protocol_device(const struct scmi_device_id *id_table)
1374 {
1375 	struct list_head *phead;
1376 
1377 	pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
1378 		 id_table->name, id_table->protocol_id);
1379 
1380 	mutex_lock(&scmi_requested_devices_mtx);
1381 	phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
1382 	if (phead) {
1383 		struct scmi_requested_dev *victim, *tmp;
1384 
1385 		list_for_each_entry_safe(victim, tmp, phead, node) {
1386 			if (!strcmp(victim->id_table->name, id_table->name)) {
1387 				list_del(&victim->node);
1388 				kfree(victim);
1389 				break;
1390 			}
1391 		}
1392 
1393 		if (list_empty(phead)) {
1394 			idr_remove(&scmi_requested_devices,
1395 				   id_table->protocol_id);
1396 			kfree(phead);
1397 		}
1398 	}
1399 	mutex_unlock(&scmi_requested_devices_mtx);
1400 }
1401 
scmi_cleanup_txrx_channels(struct scmi_info * info)1402 static int scmi_cleanup_txrx_channels(struct scmi_info *info)
1403 {
1404 	int ret;
1405 	struct idr *idr = &info->tx_idr;
1406 
1407 	ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1408 	idr_destroy(&info->tx_idr);
1409 
1410 	idr = &info->rx_idr;
1411 	ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1412 	idr_destroy(&info->rx_idr);
1413 
1414 	return ret;
1415 }
1416 
scmi_probe(struct platform_device * pdev)1417 static int scmi_probe(struct platform_device *pdev)
1418 {
1419 	int ret;
1420 	struct scmi_handle *handle;
1421 	const struct scmi_desc *desc;
1422 	struct scmi_info *info;
1423 	struct device *dev = &pdev->dev;
1424 	struct device_node *child, *np = dev->of_node;
1425 
1426 	desc = of_device_get_match_data(dev);
1427 	if (!desc)
1428 		return -EINVAL;
1429 
1430 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1431 	if (!info)
1432 		return -ENOMEM;
1433 
1434 	info->dev = dev;
1435 	info->desc = desc;
1436 	INIT_LIST_HEAD(&info->node);
1437 	idr_init(&info->protocols);
1438 	mutex_init(&info->protocols_mtx);
1439 	idr_init(&info->active_protocols);
1440 
1441 	platform_set_drvdata(pdev, info);
1442 	idr_init(&info->tx_idr);
1443 	idr_init(&info->rx_idr);
1444 
1445 	handle = &info->handle;
1446 	handle->dev = info->dev;
1447 	handle->version = &info->version;
1448 	handle->devm_acquire_protocol = scmi_devm_acquire_protocol;
1449 	handle->devm_get_protocol = scmi_devm_get_protocol;
1450 	handle->devm_put_protocol = scmi_devm_put_protocol;
1451 
1452 	ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
1453 	if (ret)
1454 		return ret;
1455 
1456 	ret = scmi_xfer_info_init(info);
1457 	if (ret)
1458 		goto clear_txrx_setup;
1459 
1460 	if (scmi_notification_init(handle))
1461 		dev_err(dev, "SCMI Notifications NOT available.\n");
1462 
1463 	/*
1464 	 * Trigger SCMI Base protocol initialization.
1465 	 * It's mandatory and won't be ever released/deinit until the
1466 	 * SCMI stack is shutdown/unloaded as a whole.
1467 	 */
1468 	ret = scmi_acquire_protocol(handle, SCMI_PROTOCOL_BASE);
1469 	if (ret) {
1470 		dev_err(dev, "unable to communicate with SCMI\n");
1471 		goto notification_exit;
1472 	}
1473 
1474 	mutex_lock(&scmi_list_mutex);
1475 	list_add_tail(&info->node, &scmi_list);
1476 	mutex_unlock(&scmi_list_mutex);
1477 
1478 	for_each_available_child_of_node(np, child) {
1479 		u32 prot_id;
1480 
1481 		if (of_property_read_u32(child, "reg", &prot_id))
1482 			continue;
1483 
1484 		if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
1485 			dev_err(dev, "Out of range protocol %d\n", prot_id);
1486 
1487 		if (!scmi_is_protocol_implemented(handle, prot_id)) {
1488 			dev_err(dev, "SCMI protocol %d not implemented\n",
1489 				prot_id);
1490 			continue;
1491 		}
1492 
1493 		/*
1494 		 * Save this valid DT protocol descriptor amongst
1495 		 * @active_protocols for this SCMI instance/
1496 		 */
1497 		ret = idr_alloc(&info->active_protocols, child,
1498 				prot_id, prot_id + 1, GFP_KERNEL);
1499 		if (ret != prot_id) {
1500 			dev_err(dev, "SCMI protocol %d already activated. Skip\n",
1501 				prot_id);
1502 			continue;
1503 		}
1504 
1505 		of_node_get(child);
1506 		scmi_create_protocol_devices(child, info, prot_id);
1507 	}
1508 
1509 	return 0;
1510 
1511 notification_exit:
1512 	scmi_notification_exit(&info->handle);
1513 clear_txrx_setup:
1514 	scmi_cleanup_txrx_channels(info);
1515 	return ret;
1516 }
1517 
scmi_free_channel(struct scmi_chan_info * cinfo,struct idr * idr,int id)1518 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
1519 {
1520 	idr_remove(idr, id);
1521 }
1522 
scmi_remove(struct platform_device * pdev)1523 static int scmi_remove(struct platform_device *pdev)
1524 {
1525 	int ret = 0, id;
1526 	struct scmi_info *info = platform_get_drvdata(pdev);
1527 	struct device_node *child;
1528 
1529 	mutex_lock(&scmi_list_mutex);
1530 	if (info->users)
1531 		ret = -EBUSY;
1532 	else
1533 		list_del(&info->node);
1534 	mutex_unlock(&scmi_list_mutex);
1535 
1536 	if (ret)
1537 		return ret;
1538 
1539 	scmi_notification_exit(&info->handle);
1540 
1541 	mutex_lock(&info->protocols_mtx);
1542 	idr_destroy(&info->protocols);
1543 	mutex_unlock(&info->protocols_mtx);
1544 
1545 	idr_for_each_entry(&info->active_protocols, child, id)
1546 		of_node_put(child);
1547 	idr_destroy(&info->active_protocols);
1548 
1549 	/* Safe to free channels since no more users */
1550 	return scmi_cleanup_txrx_channels(info);
1551 }
1552 
protocol_version_show(struct device * dev,struct device_attribute * attr,char * buf)1553 static ssize_t protocol_version_show(struct device *dev,
1554 				     struct device_attribute *attr, char *buf)
1555 {
1556 	struct scmi_info *info = dev_get_drvdata(dev);
1557 
1558 	return sprintf(buf, "%u.%u\n", info->version.major_ver,
1559 		       info->version.minor_ver);
1560 }
1561 static DEVICE_ATTR_RO(protocol_version);
1562 
firmware_version_show(struct device * dev,struct device_attribute * attr,char * buf)1563 static ssize_t firmware_version_show(struct device *dev,
1564 				     struct device_attribute *attr, char *buf)
1565 {
1566 	struct scmi_info *info = dev_get_drvdata(dev);
1567 
1568 	return sprintf(buf, "0x%x\n", info->version.impl_ver);
1569 }
1570 static DEVICE_ATTR_RO(firmware_version);
1571 
vendor_id_show(struct device * dev,struct device_attribute * attr,char * buf)1572 static ssize_t vendor_id_show(struct device *dev,
1573 			      struct device_attribute *attr, char *buf)
1574 {
1575 	struct scmi_info *info = dev_get_drvdata(dev);
1576 
1577 	return sprintf(buf, "%s\n", info->version.vendor_id);
1578 }
1579 static DEVICE_ATTR_RO(vendor_id);
1580 
sub_vendor_id_show(struct device * dev,struct device_attribute * attr,char * buf)1581 static ssize_t sub_vendor_id_show(struct device *dev,
1582 				  struct device_attribute *attr, char *buf)
1583 {
1584 	struct scmi_info *info = dev_get_drvdata(dev);
1585 
1586 	return sprintf(buf, "%s\n", info->version.sub_vendor_id);
1587 }
1588 static DEVICE_ATTR_RO(sub_vendor_id);
1589 
1590 static struct attribute *versions_attrs[] = {
1591 	&dev_attr_firmware_version.attr,
1592 	&dev_attr_protocol_version.attr,
1593 	&dev_attr_vendor_id.attr,
1594 	&dev_attr_sub_vendor_id.attr,
1595 	NULL,
1596 };
1597 ATTRIBUTE_GROUPS(versions);
1598 
1599 /* Each compatible listed below must have descriptor associated with it */
1600 static const struct of_device_id scmi_of_match[] = {
1601 #ifdef CONFIG_MAILBOX
1602 	{ .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
1603 #endif
1604 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
1605 	{ .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
1606 #endif
1607 	{ /* Sentinel */ },
1608 };
1609 
1610 MODULE_DEVICE_TABLE(of, scmi_of_match);
1611 
1612 static struct platform_driver scmi_driver = {
1613 	.driver = {
1614 		   .name = "arm-scmi",
1615 		   .suppress_bind_attrs = true,
1616 		   .of_match_table = scmi_of_match,
1617 		   .dev_groups = versions_groups,
1618 		   },
1619 	.probe = scmi_probe,
1620 	.remove = scmi_remove,
1621 };
1622 
scmi_driver_init(void)1623 static int __init scmi_driver_init(void)
1624 {
1625 	scmi_bus_init();
1626 
1627 	scmi_base_register();
1628 
1629 	scmi_clock_register();
1630 	scmi_perf_register();
1631 	scmi_power_register();
1632 	scmi_reset_register();
1633 	scmi_sensors_register();
1634 	scmi_voltage_register();
1635 	scmi_system_register();
1636 
1637 	return platform_driver_register(&scmi_driver);
1638 }
1639 subsys_initcall(scmi_driver_init);
1640 
scmi_driver_exit(void)1641 static void __exit scmi_driver_exit(void)
1642 {
1643 	scmi_base_unregister();
1644 
1645 	scmi_clock_unregister();
1646 	scmi_perf_unregister();
1647 	scmi_power_unregister();
1648 	scmi_reset_unregister();
1649 	scmi_sensors_unregister();
1650 	scmi_voltage_unregister();
1651 	scmi_system_unregister();
1652 
1653 	scmi_bus_exit();
1654 
1655 	platform_driver_unregister(&scmi_driver);
1656 }
1657 module_exit(scmi_driver_exit);
1658 
1659 MODULE_ALIAS("platform:arm-scmi");
1660 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1661 MODULE_DESCRIPTION("ARM SCMI protocol driver");
1662 MODULE_LICENSE("GPL v2");
1663