xref: /linux/drivers/firmware/arm_scmi/driver.c (revision 2145af01)
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-2021 ARM Ltd.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/export.h>
23 #include <linux/idr.h>
24 #include <linux/io.h>
25 #include <linux/io-64-nonatomic-hi-lo.h>
26 #include <linux/kernel.h>
27 #include <linux/ktime.h>
28 #include <linux/hashtable.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/of.h>
32 #include <linux/platform_device.h>
33 #include <linux/processor.h>
34 #include <linux/refcount.h>
35 #include <linux/slab.h>
36 #include <linux/xarray.h>
37 
38 #include "common.h"
39 #include "notify.h"
40 
41 #include "raw_mode.h"
42 
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/scmi.h>
45 
46 static DEFINE_IDA(scmi_id);
47 
48 static DEFINE_XARRAY(scmi_protocols);
49 
50 /* List of all SCMI devices active in system */
51 static LIST_HEAD(scmi_list);
52 /* Protection for the entire list */
53 static DEFINE_MUTEX(scmi_list_mutex);
54 /* Track the unique id for the transfers for debug & profiling purpose */
55 static atomic_t transfer_last_id;
56 
57 static struct dentry *scmi_top_dentry;
58 
59 /**
60  * struct scmi_xfers_info - Structure to manage transfer information
61  *
62  * @xfer_alloc_table: Bitmap table for allocated messages.
63  *	Index of this bitmap table is also used for message
64  *	sequence identifier.
65  * @xfer_lock: Protection for message allocation
66  * @max_msg: Maximum number of messages that can be pending
67  * @free_xfers: A free list for available to use xfers. It is initialized with
68  *		a number of xfers equal to the maximum allowed in-flight
69  *		messages.
70  * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
71  *		   currently in-flight messages.
72  */
73 struct scmi_xfers_info {
74 	unsigned long *xfer_alloc_table;
75 	spinlock_t xfer_lock;
76 	int max_msg;
77 	struct hlist_head free_xfers;
78 	DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
79 };
80 
81 /**
82  * struct scmi_protocol_instance  - Describe an initialized protocol instance.
83  * @handle: Reference to the SCMI handle associated to this protocol instance.
84  * @proto: A reference to the protocol descriptor.
85  * @gid: A reference for per-protocol devres management.
86  * @users: A refcount to track effective users of this protocol.
87  * @priv: Reference for optional protocol private data.
88  * @version: Protocol version supported by the platform as detected at runtime.
89  * @negotiated_version: When the platform supports a newer protocol version,
90  *			the agent will try to negotiate with the platform the
91  *			usage of the newest version known to it, since
92  *			backward compatibility is NOT automatically assured.
93  *			This field is NON-zero when a successful negotiation
94  *			has completed.
95  * @ph: An embedded protocol handle that will be passed down to protocol
96  *	initialization code to identify this instance.
97  *
98  * Each protocol is initialized independently once for each SCMI platform in
99  * which is defined by DT and implemented by the SCMI server fw.
100  */
101 struct scmi_protocol_instance {
102 	const struct scmi_handle	*handle;
103 	const struct scmi_protocol	*proto;
104 	void				*gid;
105 	refcount_t			users;
106 	void				*priv;
107 	unsigned int			version;
108 	unsigned int			negotiated_version;
109 	struct scmi_protocol_handle	ph;
110 };
111 
112 #define ph_to_pi(h)	container_of(h, struct scmi_protocol_instance, ph)
113 
114 /**
115  * struct scmi_debug_info  - Debug common info
116  * @top_dentry: A reference to the top debugfs dentry
117  * @name: Name of this SCMI instance
118  * @type: Type of this SCMI instance
119  * @is_atomic: Flag to state if the transport of this instance is atomic
120  */
121 struct scmi_debug_info {
122 	struct dentry *top_dentry;
123 	const char *name;
124 	const char *type;
125 	bool is_atomic;
126 };
127 
128 /**
129  * struct scmi_info - Structure representing a SCMI instance
130  *
131  * @id: A sequence number starting from zero identifying this instance
132  * @dev: Device pointer
133  * @desc: SoC description for this instance
134  * @version: SCMI revision information containing protocol version,
135  *	implementation version and (sub-)vendor identification.
136  * @handle: Instance of SCMI handle to send to clients
137  * @tx_minfo: Universal Transmit Message management info
138  * @rx_minfo: Universal Receive Message management info
139  * @tx_idr: IDR object to map protocol id to Tx channel info pointer
140  * @rx_idr: IDR object to map protocol id to Rx channel info pointer
141  * @protocols: IDR for protocols' instance descriptors initialized for
142  *	       this SCMI instance: populated on protocol's first attempted
143  *	       usage.
144  * @protocols_mtx: A mutex to protect protocols instances initialization.
145  * @protocols_imp: List of protocols implemented, currently maximum of
146  *		   scmi_revision_info.num_protocols elements allocated by the
147  *		   base protocol
148  * @active_protocols: IDR storing device_nodes for protocols actually defined
149  *		      in the DT and confirmed as implemented by fw.
150  * @atomic_threshold: Optional system wide DT-configured threshold, expressed
151  *		      in microseconds, for atomic operations.
152  *		      Only SCMI synchronous commands reported by the platform
153  *		      to have an execution latency lesser-equal to the threshold
154  *		      should be considered for atomic mode operation: such
155  *		      decision is finally left up to the SCMI drivers.
156  * @notify_priv: Pointer to private data structure specific to notifications.
157  * @node: List head
158  * @users: Number of users of this instance
159  * @bus_nb: A notifier to listen for device bind/unbind on the scmi bus
160  * @dev_req_nb: A notifier to listen for device request/unrequest on the scmi
161  *		bus
162  * @devreq_mtx: A mutex to serialize device creation for this SCMI instance
163  * @dbg: A pointer to debugfs related data (if any)
164  * @raw: An opaque reference handle used by SCMI Raw mode.
165  */
166 struct scmi_info {
167 	int id;
168 	struct device *dev;
169 	const struct scmi_desc *desc;
170 	struct scmi_revision_info version;
171 	struct scmi_handle handle;
172 	struct scmi_xfers_info tx_minfo;
173 	struct scmi_xfers_info rx_minfo;
174 	struct idr tx_idr;
175 	struct idr rx_idr;
176 	struct idr protocols;
177 	/* Ensure mutual exclusive access to protocols instance array */
178 	struct mutex protocols_mtx;
179 	u8 *protocols_imp;
180 	struct idr active_protocols;
181 	unsigned int atomic_threshold;
182 	void *notify_priv;
183 	struct list_head node;
184 	int users;
185 	struct notifier_block bus_nb;
186 	struct notifier_block dev_req_nb;
187 	/* Serialize device creation process for this instance */
188 	struct mutex devreq_mtx;
189 	struct scmi_debug_info *dbg;
190 	void *raw;
191 };
192 
193 #define handle_to_scmi_info(h)	container_of(h, struct scmi_info, handle)
194 #define bus_nb_to_scmi_info(nb)	container_of(nb, struct scmi_info, bus_nb)
195 #define req_nb_to_scmi_info(nb)	container_of(nb, struct scmi_info, dev_req_nb)
196 
197 static unsigned long
scmi_vendor_protocol_signature(unsigned int protocol_id,char * vendor_id,char * sub_vendor_id,u32 impl_ver)198 scmi_vendor_protocol_signature(unsigned int protocol_id, char *vendor_id,
199 			       char *sub_vendor_id, u32 impl_ver)
200 {
201 	char *signature, *p;
202 	unsigned long hash = 0;
203 
204 	/* vendor_id/sub_vendor_id guaranteed <= SCMI_SHORT_NAME_MAX_SIZE */
205 	signature = kasprintf(GFP_KERNEL, "%02X|%s|%s|0x%08X", protocol_id,
206 			      vendor_id ?: "", sub_vendor_id ?: "", impl_ver);
207 	if (!signature)
208 		return 0;
209 
210 	p = signature;
211 	while (*p)
212 		hash = partial_name_hash(tolower(*p++), hash);
213 	hash = end_name_hash(hash);
214 
215 	kfree(signature);
216 
217 	return hash;
218 }
219 
220 static unsigned long
scmi_protocol_key_calculate(int protocol_id,char * vendor_id,char * sub_vendor_id,u32 impl_ver)221 scmi_protocol_key_calculate(int protocol_id, char *vendor_id,
222 			    char *sub_vendor_id, u32 impl_ver)
223 {
224 	if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
225 		return protocol_id;
226 	else
227 		return scmi_vendor_protocol_signature(protocol_id, vendor_id,
228 						      sub_vendor_id, impl_ver);
229 }
230 
231 static const struct scmi_protocol *
__scmi_vendor_protocol_lookup(int protocol_id,char * vendor_id,char * sub_vendor_id,u32 impl_ver)232 __scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
233 			      char *sub_vendor_id, u32 impl_ver)
234 {
235 	unsigned long key;
236 	struct scmi_protocol *proto = NULL;
237 
238 	key = scmi_protocol_key_calculate(protocol_id, vendor_id,
239 					  sub_vendor_id, impl_ver);
240 	if (key)
241 		proto = xa_load(&scmi_protocols, key);
242 
243 	return proto;
244 }
245 
246 static const struct scmi_protocol *
scmi_vendor_protocol_lookup(int protocol_id,char * vendor_id,char * sub_vendor_id,u32 impl_ver)247 scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
248 			    char *sub_vendor_id, u32 impl_ver)
249 {
250 	const struct scmi_protocol *proto = NULL;
251 
252 	/* Searching for closest match ...*/
253 	proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
254 					      sub_vendor_id, impl_ver);
255 	if (proto)
256 		return proto;
257 
258 	/* Any match just on vendor/sub_vendor ? */
259 	if (impl_ver) {
260 		proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
261 						      sub_vendor_id, 0);
262 		if (proto)
263 			return proto;
264 	}
265 
266 	/* Any match just on the vendor ? */
267 	if (sub_vendor_id)
268 		proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
269 						      NULL, 0);
270 	return proto;
271 }
272 
273 static const struct scmi_protocol *
scmi_protocol_get(int protocol_id,struct scmi_revision_info * version)274 scmi_protocol_get(int protocol_id, struct scmi_revision_info *version)
275 {
276 	const struct scmi_protocol *proto = NULL;
277 
278 	if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
279 		proto = xa_load(&scmi_protocols, protocol_id);
280 	else
281 		proto = scmi_vendor_protocol_lookup(protocol_id,
282 						    version->vendor_id,
283 						    version->sub_vendor_id,
284 						    version->impl_ver);
285 	if (!proto || !try_module_get(proto->owner)) {
286 		pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id);
287 		return NULL;
288 	}
289 
290 	pr_debug("Found SCMI Protocol 0x%x\n", protocol_id);
291 
292 	if (protocol_id >= SCMI_PROTOCOL_VENDOR_BASE)
293 		pr_info("Loaded SCMI Vendor Protocol 0x%x - %s %s %X\n",
294 			protocol_id, proto->vendor_id ?: "",
295 			proto->sub_vendor_id ?: "", proto->impl_ver);
296 
297 	return proto;
298 }
299 
scmi_protocol_put(const struct scmi_protocol * proto)300 static void scmi_protocol_put(const struct scmi_protocol *proto)
301 {
302 	if (proto)
303 		module_put(proto->owner);
304 }
305 
scmi_vendor_protocol_check(const struct scmi_protocol * proto)306 static int scmi_vendor_protocol_check(const struct scmi_protocol *proto)
307 {
308 	if (!proto->vendor_id) {
309 		pr_err("missing vendor_id for protocol 0x%x\n", proto->id);
310 		return -EINVAL;
311 	}
312 
313 	if (strlen(proto->vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
314 		pr_err("malformed vendor_id for protocol 0x%x\n", proto->id);
315 		return -EINVAL;
316 	}
317 
318 	if (proto->sub_vendor_id &&
319 	    strlen(proto->sub_vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
320 		pr_err("malformed sub_vendor_id for protocol 0x%x\n",
321 		       proto->id);
322 		return -EINVAL;
323 	}
324 
325 	return 0;
326 }
327 
scmi_protocol_register(const struct scmi_protocol * proto)328 int scmi_protocol_register(const struct scmi_protocol *proto)
329 {
330 	int ret;
331 	unsigned long key;
332 
333 	if (!proto) {
334 		pr_err("invalid protocol\n");
335 		return -EINVAL;
336 	}
337 
338 	if (!proto->instance_init) {
339 		pr_err("missing init for protocol 0x%x\n", proto->id);
340 		return -EINVAL;
341 	}
342 
343 	if (proto->id >= SCMI_PROTOCOL_VENDOR_BASE &&
344 	    scmi_vendor_protocol_check(proto))
345 		return -EINVAL;
346 
347 	/*
348 	 * Calculate a protocol key to register this protocol with the core;
349 	 * key value 0 is considered invalid.
350 	 */
351 	key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
352 					  proto->sub_vendor_id,
353 					  proto->impl_ver);
354 	if (!key)
355 		return -EINVAL;
356 
357 	ret = xa_insert(&scmi_protocols, key, (void *)proto, GFP_KERNEL);
358 	if (ret) {
359 		pr_err("unable to allocate SCMI protocol slot for 0x%x - err %d\n",
360 		       proto->id, ret);
361 		return ret;
362 	}
363 
364 	pr_debug("Registered SCMI Protocol 0x%x\n", proto->id);
365 
366 	return 0;
367 }
368 EXPORT_SYMBOL_GPL(scmi_protocol_register);
369 
scmi_protocol_unregister(const struct scmi_protocol * proto)370 void scmi_protocol_unregister(const struct scmi_protocol *proto)
371 {
372 	unsigned long key;
373 
374 	key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
375 					  proto->sub_vendor_id,
376 					  proto->impl_ver);
377 	if (!key)
378 		return;
379 
380 	xa_erase(&scmi_protocols, key);
381 
382 	pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id);
383 }
384 EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
385 
386 /**
387  * scmi_create_protocol_devices  - Create devices for all pending requests for
388  * this SCMI instance.
389  *
390  * @np: The device node describing the protocol
391  * @info: The SCMI instance descriptor
392  * @prot_id: The protocol ID
393  * @name: The optional name of the device to be created: if not provided this
394  *	  call will lead to the creation of all the devices currently requested
395  *	  for the specified protocol.
396  */
scmi_create_protocol_devices(struct device_node * np,struct scmi_info * info,int prot_id,const char * name)397 static void scmi_create_protocol_devices(struct device_node *np,
398 					 struct scmi_info *info,
399 					 int prot_id, const char *name)
400 {
401 	struct scmi_device *sdev;
402 
403 	mutex_lock(&info->devreq_mtx);
404 	sdev = scmi_device_create(np, info->dev, prot_id, name);
405 	if (name && !sdev)
406 		dev_err(info->dev,
407 			"failed to create device for protocol 0x%X (%s)\n",
408 			prot_id, name);
409 	mutex_unlock(&info->devreq_mtx);
410 }
411 
scmi_destroy_protocol_devices(struct scmi_info * info,int prot_id,const char * name)412 static void scmi_destroy_protocol_devices(struct scmi_info *info,
413 					  int prot_id, const char *name)
414 {
415 	mutex_lock(&info->devreq_mtx);
416 	scmi_device_destroy(info->dev, prot_id, name);
417 	mutex_unlock(&info->devreq_mtx);
418 }
419 
scmi_notification_instance_data_set(const struct scmi_handle * handle,void * priv)420 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
421 					 void *priv)
422 {
423 	struct scmi_info *info = handle_to_scmi_info(handle);
424 
425 	info->notify_priv = priv;
426 	/* Ensure updated protocol private date are visible */
427 	smp_wmb();
428 }
429 
scmi_notification_instance_data_get(const struct scmi_handle * handle)430 void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
431 {
432 	struct scmi_info *info = handle_to_scmi_info(handle);
433 
434 	/* Ensure protocols_private_data has been updated */
435 	smp_rmb();
436 	return info->notify_priv;
437 }
438 
439 /**
440  * scmi_xfer_token_set  - Reserve and set new token for the xfer at hand
441  *
442  * @minfo: Pointer to Tx/Rx Message management info based on channel type
443  * @xfer: The xfer to act upon
444  *
445  * Pick the next unused monotonically increasing token and set it into
446  * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
447  * reuse of freshly completed or timed-out xfers, thus mitigating the risk
448  * of incorrect association of a late and expired xfer with a live in-flight
449  * transaction, both happening to re-use the same token identifier.
450  *
451  * Since platform is NOT required to answer our request in-order we should
452  * account for a few rare but possible scenarios:
453  *
454  *  - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
455  *    using find_next_zero_bit() starting from candidate next_token bit
456  *
457  *  - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
458  *    are plenty of free tokens at start, so try a second pass using
459  *    find_next_zero_bit() and starting from 0.
460  *
461  *  X = used in-flight
462  *
463  * Normal
464  * ------
465  *
466  *		|- xfer_id picked
467  *   -----------+----------------------------------------------------------
468  *   | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
469  *   ----------------------------------------------------------------------
470  *		^
471  *		|- next_token
472  *
473  * Out-of-order pending at start
474  * -----------------------------
475  *
476  *	  |- xfer_id picked, last_token fixed
477  *   -----+----------------------------------------------------------------
478  *   |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
479  *   ----------------------------------------------------------------------
480  *    ^
481  *    |- next_token
482  *
483  *
484  * Out-of-order pending at end
485  * ---------------------------
486  *
487  *	  |- xfer_id picked, last_token fixed
488  *   -----+----------------------------------------------------------------
489  *   |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
490  *   ----------------------------------------------------------------------
491  *								^
492  *								|- next_token
493  *
494  * Context: Assumes to be called with @xfer_lock already acquired.
495  *
496  * Return: 0 on Success or error
497  */
scmi_xfer_token_set(struct scmi_xfers_info * minfo,struct scmi_xfer * xfer)498 static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
499 			       struct scmi_xfer *xfer)
500 {
501 	unsigned long xfer_id, next_token;
502 
503 	/*
504 	 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
505 	 * using the pre-allocated transfer_id as a base.
506 	 * Note that the global transfer_id is shared across all message types
507 	 * so there could be holes in the allocated set of monotonic sequence
508 	 * numbers, but that is going to limit the effectiveness of the
509 	 * mitigation only in very rare limit conditions.
510 	 */
511 	next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
512 
513 	/* Pick the next available xfer_id >= next_token */
514 	xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
515 				     MSG_TOKEN_MAX, next_token);
516 	if (xfer_id == MSG_TOKEN_MAX) {
517 		/*
518 		 * After heavily out-of-order responses, there are no free
519 		 * tokens ahead, but only at start of xfer_alloc_table so
520 		 * try again from the beginning.
521 		 */
522 		xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
523 					     MSG_TOKEN_MAX, 0);
524 		/*
525 		 * Something is wrong if we got here since there can be a
526 		 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
527 		 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
528 		 */
529 		if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
530 			return -ENOMEM;
531 	}
532 
533 	/* Update +/- last_token accordingly if we skipped some hole */
534 	if (xfer_id != next_token)
535 		atomic_add((int)(xfer_id - next_token), &transfer_last_id);
536 
537 	xfer->hdr.seq = (u16)xfer_id;
538 
539 	return 0;
540 }
541 
542 /**
543  * scmi_xfer_token_clear  - Release the token
544  *
545  * @minfo: Pointer to Tx/Rx Message management info based on channel type
546  * @xfer: The xfer to act upon
547  */
scmi_xfer_token_clear(struct scmi_xfers_info * minfo,struct scmi_xfer * xfer)548 static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
549 					 struct scmi_xfer *xfer)
550 {
551 	clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
552 }
553 
554 /**
555  * scmi_xfer_inflight_register_unlocked  - Register the xfer as in-flight
556  *
557  * @xfer: The xfer to register
558  * @minfo: Pointer to Tx/Rx Message management info based on channel type
559  *
560  * Note that this helper assumes that the xfer to be registered as in-flight
561  * had been built using an xfer sequence number which still corresponds to a
562  * free slot in the xfer_alloc_table.
563  *
564  * Context: Assumes to be called with @xfer_lock already acquired.
565  */
566 static inline void
scmi_xfer_inflight_register_unlocked(struct scmi_xfer * xfer,struct scmi_xfers_info * minfo)567 scmi_xfer_inflight_register_unlocked(struct scmi_xfer *xfer,
568 				     struct scmi_xfers_info *minfo)
569 {
570 	/* Set in-flight */
571 	set_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
572 	hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq);
573 	xfer->pending = true;
574 }
575 
576 /**
577  * scmi_xfer_inflight_register  - Try to register an xfer as in-flight
578  *
579  * @xfer: The xfer to register
580  * @minfo: Pointer to Tx/Rx Message management info based on channel type
581  *
582  * Note that this helper does NOT assume anything about the sequence number
583  * that was baked into the provided xfer, so it checks at first if it can
584  * be mapped to a free slot and fails with an error if another xfer with the
585  * same sequence number is currently still registered as in-flight.
586  *
587  * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer
588  *	   could not rbe mapped to a free slot in the xfer_alloc_table.
589  */
scmi_xfer_inflight_register(struct scmi_xfer * xfer,struct scmi_xfers_info * minfo)590 static int scmi_xfer_inflight_register(struct scmi_xfer *xfer,
591 				       struct scmi_xfers_info *minfo)
592 {
593 	int ret = 0;
594 	unsigned long flags;
595 
596 	spin_lock_irqsave(&minfo->xfer_lock, flags);
597 	if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table))
598 		scmi_xfer_inflight_register_unlocked(xfer, minfo);
599 	else
600 		ret = -EBUSY;
601 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
602 
603 	return ret;
604 }
605 
606 /**
607  * scmi_xfer_raw_inflight_register  - An helper to register the given xfer as in
608  * flight on the TX channel, if possible.
609  *
610  * @handle: Pointer to SCMI entity handle
611  * @xfer: The xfer to register
612  *
613  * Return: 0 on Success, error otherwise
614  */
scmi_xfer_raw_inflight_register(const struct scmi_handle * handle,struct scmi_xfer * xfer)615 int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle,
616 				    struct scmi_xfer *xfer)
617 {
618 	struct scmi_info *info = handle_to_scmi_info(handle);
619 
620 	return scmi_xfer_inflight_register(xfer, &info->tx_minfo);
621 }
622 
623 /**
624  * scmi_xfer_pending_set  - Pick a proper sequence number and mark the xfer
625  * as pending in-flight
626  *
627  * @xfer: The xfer to act upon
628  * @minfo: Pointer to Tx/Rx Message management info based on channel type
629  *
630  * Return: 0 on Success or error otherwise
631  */
scmi_xfer_pending_set(struct scmi_xfer * xfer,struct scmi_xfers_info * minfo)632 static inline int scmi_xfer_pending_set(struct scmi_xfer *xfer,
633 					struct scmi_xfers_info *minfo)
634 {
635 	int ret;
636 	unsigned long flags;
637 
638 	spin_lock_irqsave(&minfo->xfer_lock, flags);
639 	/* Set a new monotonic token as the xfer sequence number */
640 	ret = scmi_xfer_token_set(minfo, xfer);
641 	if (!ret)
642 		scmi_xfer_inflight_register_unlocked(xfer, minfo);
643 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
644 
645 	return ret;
646 }
647 
648 /**
649  * scmi_xfer_get() - Allocate one message
650  *
651  * @handle: Pointer to SCMI entity handle
652  * @minfo: Pointer to Tx/Rx Message management info based on channel type
653  *
654  * Helper function which is used by various message functions that are
655  * exposed to clients of this driver for allocating a message traffic event.
656  *
657  * Picks an xfer from the free list @free_xfers (if any available) and perform
658  * a basic initialization.
659  *
660  * Note that, at this point, still no sequence number is assigned to the
661  * allocated xfer, nor it is registered as a pending transaction.
662  *
663  * The successfully initialized xfer is refcounted.
664  *
665  * Context: Holds @xfer_lock while manipulating @free_xfers.
666  *
667  * Return: An initialized xfer if all went fine, else pointer error.
668  */
scmi_xfer_get(const struct scmi_handle * handle,struct scmi_xfers_info * minfo)669 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
670 				       struct scmi_xfers_info *minfo)
671 {
672 	unsigned long flags;
673 	struct scmi_xfer *xfer;
674 
675 	spin_lock_irqsave(&minfo->xfer_lock, flags);
676 	if (hlist_empty(&minfo->free_xfers)) {
677 		spin_unlock_irqrestore(&minfo->xfer_lock, flags);
678 		return ERR_PTR(-ENOMEM);
679 	}
680 
681 	/* grab an xfer from the free_list */
682 	xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
683 	hlist_del_init(&xfer->node);
684 
685 	/*
686 	 * Allocate transfer_id early so that can be used also as base for
687 	 * monotonic sequence number generation if needed.
688 	 */
689 	xfer->transfer_id = atomic_inc_return(&transfer_last_id);
690 
691 	refcount_set(&xfer->users, 1);
692 	atomic_set(&xfer->busy, SCMI_XFER_FREE);
693 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
694 
695 	return xfer;
696 }
697 
698 /**
699  * scmi_xfer_raw_get  - Helper to get a bare free xfer from the TX channel
700  *
701  * @handle: Pointer to SCMI entity handle
702  *
703  * Note that xfer is taken from the TX channel structures.
704  *
705  * Return: A valid xfer on Success, or an error-pointer otherwise
706  */
scmi_xfer_raw_get(const struct scmi_handle * handle)707 struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle)
708 {
709 	struct scmi_xfer *xfer;
710 	struct scmi_info *info = handle_to_scmi_info(handle);
711 
712 	xfer = scmi_xfer_get(handle, &info->tx_minfo);
713 	if (!IS_ERR(xfer))
714 		xfer->flags |= SCMI_XFER_FLAG_IS_RAW;
715 
716 	return xfer;
717 }
718 
719 /**
720  * scmi_xfer_raw_channel_get  - Helper to get a reference to the proper channel
721  * to use for a specific protocol_id Raw transaction.
722  *
723  * @handle: Pointer to SCMI entity handle
724  * @protocol_id: Identifier of the protocol
725  *
726  * Note that in a regular SCMI stack, usually, a protocol has to be defined in
727  * the DT to have an associated channel and be usable; but in Raw mode any
728  * protocol in range is allowed, re-using the Base channel, so as to enable
729  * fuzzing on any protocol without the need of a fully compiled DT.
730  *
731  * Return: A reference to the channel to use, or an ERR_PTR
732  */
733 struct scmi_chan_info *
scmi_xfer_raw_channel_get(const struct scmi_handle * handle,u8 protocol_id)734 scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id)
735 {
736 	struct scmi_chan_info *cinfo;
737 	struct scmi_info *info = handle_to_scmi_info(handle);
738 
739 	cinfo = idr_find(&info->tx_idr, protocol_id);
740 	if (!cinfo) {
741 		if (protocol_id == SCMI_PROTOCOL_BASE)
742 			return ERR_PTR(-EINVAL);
743 		/* Use Base channel for protocols not defined for DT */
744 		cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
745 		if (!cinfo)
746 			return ERR_PTR(-EINVAL);
747 		dev_warn_once(handle->dev,
748 			      "Using Base channel for protocol 0x%X\n",
749 			      protocol_id);
750 	}
751 
752 	return cinfo;
753 }
754 
755 /**
756  * __scmi_xfer_put() - Release a message
757  *
758  * @minfo: Pointer to Tx/Rx Message management info based on channel type
759  * @xfer: message that was reserved by scmi_xfer_get
760  *
761  * After refcount check, possibly release an xfer, clearing the token slot,
762  * removing xfer from @pending_xfers and putting it back into free_xfers.
763  *
764  * This holds a spinlock to maintain integrity of internal data structures.
765  */
766 static void
__scmi_xfer_put(struct scmi_xfers_info * minfo,struct scmi_xfer * xfer)767 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
768 {
769 	unsigned long flags;
770 
771 	spin_lock_irqsave(&minfo->xfer_lock, flags);
772 	if (refcount_dec_and_test(&xfer->users)) {
773 		if (xfer->pending) {
774 			scmi_xfer_token_clear(minfo, xfer);
775 			hash_del(&xfer->node);
776 			xfer->pending = false;
777 		}
778 		hlist_add_head(&xfer->node, &minfo->free_xfers);
779 	}
780 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
781 }
782 
783 /**
784  * scmi_xfer_raw_put  - Release an xfer that was taken by @scmi_xfer_raw_get
785  *
786  * @handle: Pointer to SCMI entity handle
787  * @xfer: A reference to the xfer to put
788  *
789  * Note that as with other xfer_put() handlers the xfer is really effectively
790  * released only if there are no more users on the system.
791  */
scmi_xfer_raw_put(const struct scmi_handle * handle,struct scmi_xfer * xfer)792 void scmi_xfer_raw_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
793 {
794 	struct scmi_info *info = handle_to_scmi_info(handle);
795 
796 	xfer->flags &= ~SCMI_XFER_FLAG_IS_RAW;
797 	xfer->flags &= ~SCMI_XFER_FLAG_CHAN_SET;
798 	return __scmi_xfer_put(&info->tx_minfo, xfer);
799 }
800 
801 /**
802  * scmi_xfer_lookup_unlocked  -  Helper to lookup an xfer_id
803  *
804  * @minfo: Pointer to Tx/Rx Message management info based on channel type
805  * @xfer_id: Token ID to lookup in @pending_xfers
806  *
807  * Refcounting is untouched.
808  *
809  * Context: Assumes to be called with @xfer_lock already acquired.
810  *
811  * Return: A valid xfer on Success or error otherwise
812  */
813 static struct scmi_xfer *
scmi_xfer_lookup_unlocked(struct scmi_xfers_info * minfo,u16 xfer_id)814 scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
815 {
816 	struct scmi_xfer *xfer = NULL;
817 
818 	if (test_bit(xfer_id, minfo->xfer_alloc_table))
819 		xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
820 
821 	return xfer ?: ERR_PTR(-EINVAL);
822 }
823 
824 /**
825  * scmi_bad_message_trace  - A helper to trace weird messages
826  *
827  * @cinfo: A reference to the channel descriptor on which the message was
828  *	   received
829  * @msg_hdr: Message header to track
830  * @err: A specific error code used as a status value in traces.
831  *
832  * This helper can be used to trace any kind of weird, incomplete, unexpected,
833  * timed-out message that arrives and as such, can be traced only referring to
834  * the header content, since the payload is missing/unreliable.
835  */
scmi_bad_message_trace(struct scmi_chan_info * cinfo,u32 msg_hdr,enum scmi_bad_msg err)836 void scmi_bad_message_trace(struct scmi_chan_info *cinfo, u32 msg_hdr,
837 			    enum scmi_bad_msg err)
838 {
839 	char *tag;
840 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
841 
842 	switch (MSG_XTRACT_TYPE(msg_hdr)) {
843 	case MSG_TYPE_COMMAND:
844 		tag = "!RESP";
845 		break;
846 	case MSG_TYPE_DELAYED_RESP:
847 		tag = "!DLYD";
848 		break;
849 	case MSG_TYPE_NOTIFICATION:
850 		tag = "!NOTI";
851 		break;
852 	default:
853 		tag = "!UNKN";
854 		break;
855 	}
856 
857 	trace_scmi_msg_dump(info->id, cinfo->id,
858 			    MSG_XTRACT_PROT_ID(msg_hdr),
859 			    MSG_XTRACT_ID(msg_hdr), tag,
860 			    MSG_XTRACT_TOKEN(msg_hdr), err, NULL, 0);
861 }
862 
863 /**
864  * scmi_msg_response_validate  - Validate message type against state of related
865  * xfer
866  *
867  * @cinfo: A reference to the channel descriptor.
868  * @msg_type: Message type to check
869  * @xfer: A reference to the xfer to validate against @msg_type
870  *
871  * This function checks if @msg_type is congruent with the current state of
872  * a pending @xfer; if an asynchronous delayed response is received before the
873  * related synchronous response (Out-of-Order Delayed Response) the missing
874  * synchronous response is assumed to be OK and completed, carrying on with the
875  * Delayed Response: this is done to address the case in which the underlying
876  * SCMI transport can deliver such out-of-order responses.
877  *
878  * Context: Assumes to be called with xfer->lock already acquired.
879  *
880  * Return: 0 on Success, error otherwise
881  */
scmi_msg_response_validate(struct scmi_chan_info * cinfo,u8 msg_type,struct scmi_xfer * xfer)882 static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
883 					     u8 msg_type,
884 					     struct scmi_xfer *xfer)
885 {
886 	/*
887 	 * Even if a response was indeed expected on this slot at this point,
888 	 * a buggy platform could wrongly reply feeding us an unexpected
889 	 * delayed response we're not prepared to handle: bail-out safely
890 	 * blaming firmware.
891 	 */
892 	if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
893 		dev_err(cinfo->dev,
894 			"Delayed Response for %d not expected! Buggy F/W ?\n",
895 			xfer->hdr.seq);
896 		return -EINVAL;
897 	}
898 
899 	switch (xfer->state) {
900 	case SCMI_XFER_SENT_OK:
901 		if (msg_type == MSG_TYPE_DELAYED_RESP) {
902 			/*
903 			 * Delayed Response expected but delivered earlier.
904 			 * Assume message RESPONSE was OK and skip state.
905 			 */
906 			xfer->hdr.status = SCMI_SUCCESS;
907 			xfer->state = SCMI_XFER_RESP_OK;
908 			complete(&xfer->done);
909 			dev_warn(cinfo->dev,
910 				 "Received valid OoO Delayed Response for %d\n",
911 				 xfer->hdr.seq);
912 		}
913 		break;
914 	case SCMI_XFER_RESP_OK:
915 		if (msg_type != MSG_TYPE_DELAYED_RESP)
916 			return -EINVAL;
917 		break;
918 	case SCMI_XFER_DRESP_OK:
919 		/* No further message expected once in SCMI_XFER_DRESP_OK */
920 		return -EINVAL;
921 	}
922 
923 	return 0;
924 }
925 
926 /**
927  * scmi_xfer_state_update  - Update xfer state
928  *
929  * @xfer: A reference to the xfer to update
930  * @msg_type: Type of message being processed.
931  *
932  * Note that this message is assumed to have been already successfully validated
933  * by @scmi_msg_response_validate(), so here we just update the state.
934  *
935  * Context: Assumes to be called on an xfer exclusively acquired using the
936  *	    busy flag.
937  */
scmi_xfer_state_update(struct scmi_xfer * xfer,u8 msg_type)938 static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
939 {
940 	xfer->hdr.type = msg_type;
941 
942 	/* Unknown command types were already discarded earlier */
943 	if (xfer->hdr.type == MSG_TYPE_COMMAND)
944 		xfer->state = SCMI_XFER_RESP_OK;
945 	else
946 		xfer->state = SCMI_XFER_DRESP_OK;
947 }
948 
scmi_xfer_acquired(struct scmi_xfer * xfer)949 static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
950 {
951 	int ret;
952 
953 	ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
954 
955 	return ret == SCMI_XFER_FREE;
956 }
957 
958 /**
959  * scmi_xfer_command_acquire  -  Helper to lookup and acquire a command xfer
960  *
961  * @cinfo: A reference to the channel descriptor.
962  * @msg_hdr: A message header to use as lookup key
963  *
964  * When a valid xfer is found for the sequence number embedded in the provided
965  * msg_hdr, reference counting is properly updated and exclusive access to this
966  * xfer is granted till released with @scmi_xfer_command_release.
967  *
968  * Return: A valid @xfer on Success or error otherwise.
969  */
970 static inline struct scmi_xfer *
scmi_xfer_command_acquire(struct scmi_chan_info * cinfo,u32 msg_hdr)971 scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
972 {
973 	int ret;
974 	unsigned long flags;
975 	struct scmi_xfer *xfer;
976 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
977 	struct scmi_xfers_info *minfo = &info->tx_minfo;
978 	u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
979 	u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
980 
981 	/* Are we even expecting this? */
982 	spin_lock_irqsave(&minfo->xfer_lock, flags);
983 	xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
984 	if (IS_ERR(xfer)) {
985 		dev_err(cinfo->dev,
986 			"Message for %d type %d is not expected!\n",
987 			xfer_id, msg_type);
988 		spin_unlock_irqrestore(&minfo->xfer_lock, flags);
989 
990 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNEXPECTED);
991 
992 		return xfer;
993 	}
994 	refcount_inc(&xfer->users);
995 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
996 
997 	spin_lock_irqsave(&xfer->lock, flags);
998 	ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
999 	/*
1000 	 * If a pending xfer was found which was also in a congruent state with
1001 	 * the received message, acquire exclusive access to it setting the busy
1002 	 * flag.
1003 	 * Spins only on the rare limit condition of concurrent reception of
1004 	 * RESP and DRESP for the same xfer.
1005 	 */
1006 	if (!ret) {
1007 		spin_until_cond(scmi_xfer_acquired(xfer));
1008 		scmi_xfer_state_update(xfer, msg_type);
1009 	}
1010 	spin_unlock_irqrestore(&xfer->lock, flags);
1011 
1012 	if (ret) {
1013 		dev_err(cinfo->dev,
1014 			"Invalid message type:%d for %d - HDR:0x%X  state:%d\n",
1015 			msg_type, xfer_id, msg_hdr, xfer->state);
1016 
1017 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_INVALID);
1018 
1019 		/* On error the refcount incremented above has to be dropped */
1020 		__scmi_xfer_put(minfo, xfer);
1021 		xfer = ERR_PTR(-EINVAL);
1022 	}
1023 
1024 	return xfer;
1025 }
1026 
scmi_xfer_command_release(struct scmi_info * info,struct scmi_xfer * xfer)1027 static inline void scmi_xfer_command_release(struct scmi_info *info,
1028 					     struct scmi_xfer *xfer)
1029 {
1030 	atomic_set(&xfer->busy, SCMI_XFER_FREE);
1031 	__scmi_xfer_put(&info->tx_minfo, xfer);
1032 }
1033 
scmi_clear_channel(struct scmi_info * info,struct scmi_chan_info * cinfo)1034 static inline void scmi_clear_channel(struct scmi_info *info,
1035 				      struct scmi_chan_info *cinfo)
1036 {
1037 	if (info->desc->ops->clear_channel)
1038 		info->desc->ops->clear_channel(cinfo);
1039 }
1040 
scmi_handle_notification(struct scmi_chan_info * cinfo,u32 msg_hdr,void * priv)1041 static void scmi_handle_notification(struct scmi_chan_info *cinfo,
1042 				     u32 msg_hdr, void *priv)
1043 {
1044 	struct scmi_xfer *xfer;
1045 	struct device *dev = cinfo->dev;
1046 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1047 	struct scmi_xfers_info *minfo = &info->rx_minfo;
1048 	ktime_t ts;
1049 
1050 	ts = ktime_get_boottime();
1051 	xfer = scmi_xfer_get(cinfo->handle, minfo);
1052 	if (IS_ERR(xfer)) {
1053 		dev_err(dev, "failed to get free message slot (%ld)\n",
1054 			PTR_ERR(xfer));
1055 
1056 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_NOMEM);
1057 
1058 		scmi_clear_channel(info, cinfo);
1059 		return;
1060 	}
1061 
1062 	unpack_scmi_header(msg_hdr, &xfer->hdr);
1063 	if (priv)
1064 		/* Ensure order between xfer->priv store and following ops */
1065 		smp_store_mb(xfer->priv, priv);
1066 	info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
1067 					    xfer);
1068 
1069 	trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1070 			    xfer->hdr.id, "NOTI", xfer->hdr.seq,
1071 			    xfer->hdr.status, xfer->rx.buf, xfer->rx.len);
1072 
1073 	scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
1074 		    xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
1075 
1076 	trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
1077 			   xfer->hdr.protocol_id, xfer->hdr.seq,
1078 			   MSG_TYPE_NOTIFICATION);
1079 
1080 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1081 		xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr);
1082 		scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE,
1083 					cinfo->id);
1084 	}
1085 
1086 	__scmi_xfer_put(minfo, xfer);
1087 
1088 	scmi_clear_channel(info, cinfo);
1089 }
1090 
scmi_handle_response(struct scmi_chan_info * cinfo,u32 msg_hdr,void * priv)1091 static void scmi_handle_response(struct scmi_chan_info *cinfo,
1092 				 u32 msg_hdr, void *priv)
1093 {
1094 	struct scmi_xfer *xfer;
1095 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1096 
1097 	xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
1098 	if (IS_ERR(xfer)) {
1099 		if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
1100 			scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv);
1101 
1102 		if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
1103 			scmi_clear_channel(info, cinfo);
1104 		return;
1105 	}
1106 
1107 	/* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
1108 	if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
1109 		xfer->rx.len = info->desc->max_msg_size;
1110 
1111 	if (priv)
1112 		/* Ensure order between xfer->priv store and following ops */
1113 		smp_store_mb(xfer->priv, priv);
1114 	info->desc->ops->fetch_response(cinfo, xfer);
1115 
1116 	trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1117 			    xfer->hdr.id,
1118 			    xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
1119 			    (!SCMI_XFER_IS_RAW(xfer) ? "DLYD" : "dlyd") :
1120 			    (!SCMI_XFER_IS_RAW(xfer) ? "RESP" : "resp"),
1121 			    xfer->hdr.seq, xfer->hdr.status,
1122 			    xfer->rx.buf, xfer->rx.len);
1123 
1124 	trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
1125 			   xfer->hdr.protocol_id, xfer->hdr.seq,
1126 			   xfer->hdr.type);
1127 
1128 	if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
1129 		scmi_clear_channel(info, cinfo);
1130 		complete(xfer->async_done);
1131 	} else {
1132 		complete(&xfer->done);
1133 	}
1134 
1135 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1136 		/*
1137 		 * When in polling mode avoid to queue the Raw xfer on the IRQ
1138 		 * RX path since it will be already queued at the end of the TX
1139 		 * poll loop.
1140 		 */
1141 		if (!xfer->hdr.poll_completion)
1142 			scmi_raw_message_report(info->raw, xfer,
1143 						SCMI_RAW_REPLY_QUEUE,
1144 						cinfo->id);
1145 	}
1146 
1147 	scmi_xfer_command_release(info, xfer);
1148 }
1149 
1150 /**
1151  * scmi_rx_callback() - callback for receiving messages
1152  *
1153  * @cinfo: SCMI channel info
1154  * @msg_hdr: Message header
1155  * @priv: Transport specific private data.
1156  *
1157  * Processes one received message to appropriate transfer information and
1158  * signals completion of the transfer.
1159  *
1160  * NOTE: This function will be invoked in IRQ context, hence should be
1161  * as optimal as possible.
1162  */
scmi_rx_callback(struct scmi_chan_info * cinfo,u32 msg_hdr,void * priv)1163 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv)
1164 {
1165 	u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
1166 
1167 	switch (msg_type) {
1168 	case MSG_TYPE_NOTIFICATION:
1169 		scmi_handle_notification(cinfo, msg_hdr, priv);
1170 		break;
1171 	case MSG_TYPE_COMMAND:
1172 	case MSG_TYPE_DELAYED_RESP:
1173 		scmi_handle_response(cinfo, msg_hdr, priv);
1174 		break;
1175 	default:
1176 		WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
1177 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNKNOWN);
1178 		break;
1179 	}
1180 }
1181 
1182 /**
1183  * xfer_put() - Release a transmit message
1184  *
1185  * @ph: Pointer to SCMI protocol handle
1186  * @xfer: message that was reserved by xfer_get_init
1187  */
xfer_put(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)1188 static void xfer_put(const struct scmi_protocol_handle *ph,
1189 		     struct scmi_xfer *xfer)
1190 {
1191 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1192 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1193 
1194 	__scmi_xfer_put(&info->tx_minfo, xfer);
1195 }
1196 
scmi_xfer_done_no_timeout(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer,ktime_t stop)1197 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
1198 				      struct scmi_xfer *xfer, ktime_t stop)
1199 {
1200 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1201 
1202 	/*
1203 	 * Poll also on xfer->done so that polling can be forcibly terminated
1204 	 * in case of out-of-order receptions of delayed responses
1205 	 */
1206 	return info->desc->ops->poll_done(cinfo, xfer) ||
1207 	       try_wait_for_completion(&xfer->done) ||
1208 	       ktime_after(ktime_get(), stop);
1209 }
1210 
scmi_wait_for_reply(struct device * dev,const struct scmi_desc * desc,struct scmi_chan_info * cinfo,struct scmi_xfer * xfer,unsigned int timeout_ms)1211 static int scmi_wait_for_reply(struct device *dev, const struct scmi_desc *desc,
1212 			       struct scmi_chan_info *cinfo,
1213 			       struct scmi_xfer *xfer, unsigned int timeout_ms)
1214 {
1215 	int ret = 0;
1216 
1217 	if (xfer->hdr.poll_completion) {
1218 		/*
1219 		 * Real polling is needed only if transport has NOT declared
1220 		 * itself to support synchronous commands replies.
1221 		 */
1222 		if (!desc->sync_cmds_completed_on_ret) {
1223 			/*
1224 			 * Poll on xfer using transport provided .poll_done();
1225 			 * assumes no completion interrupt was available.
1226 			 */
1227 			ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
1228 
1229 			spin_until_cond(scmi_xfer_done_no_timeout(cinfo,
1230 								  xfer, stop));
1231 			if (ktime_after(ktime_get(), stop)) {
1232 				dev_err(dev,
1233 					"timed out in resp(caller: %pS) - polling\n",
1234 					(void *)_RET_IP_);
1235 				ret = -ETIMEDOUT;
1236 			}
1237 		}
1238 
1239 		if (!ret) {
1240 			unsigned long flags;
1241 			struct scmi_info *info =
1242 				handle_to_scmi_info(cinfo->handle);
1243 
1244 			/*
1245 			 * Do not fetch_response if an out-of-order delayed
1246 			 * response is being processed.
1247 			 */
1248 			spin_lock_irqsave(&xfer->lock, flags);
1249 			if (xfer->state == SCMI_XFER_SENT_OK) {
1250 				desc->ops->fetch_response(cinfo, xfer);
1251 				xfer->state = SCMI_XFER_RESP_OK;
1252 			}
1253 			spin_unlock_irqrestore(&xfer->lock, flags);
1254 
1255 			/* Trace polled replies. */
1256 			trace_scmi_msg_dump(info->id, cinfo->id,
1257 					    xfer->hdr.protocol_id, xfer->hdr.id,
1258 					    !SCMI_XFER_IS_RAW(xfer) ?
1259 					    "RESP" : "resp",
1260 					    xfer->hdr.seq, xfer->hdr.status,
1261 					    xfer->rx.buf, xfer->rx.len);
1262 
1263 			if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1264 				struct scmi_info *info =
1265 					handle_to_scmi_info(cinfo->handle);
1266 
1267 				scmi_raw_message_report(info->raw, xfer,
1268 							SCMI_RAW_REPLY_QUEUE,
1269 							cinfo->id);
1270 			}
1271 		}
1272 	} else {
1273 		/* And we wait for the response. */
1274 		if (!wait_for_completion_timeout(&xfer->done,
1275 						 msecs_to_jiffies(timeout_ms))) {
1276 			dev_err(dev, "timed out in resp(caller: %pS)\n",
1277 				(void *)_RET_IP_);
1278 			ret = -ETIMEDOUT;
1279 		}
1280 	}
1281 
1282 	return ret;
1283 }
1284 
1285 /**
1286  * scmi_wait_for_message_response  - An helper to group all the possible ways of
1287  * waiting for a synchronous message response.
1288  *
1289  * @cinfo: SCMI channel info
1290  * @xfer: Reference to the transfer being waited for.
1291  *
1292  * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
1293  * configuration flags like xfer->hdr.poll_completion.
1294  *
1295  * Return: 0 on Success, error otherwise.
1296  */
scmi_wait_for_message_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)1297 static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
1298 					  struct scmi_xfer *xfer)
1299 {
1300 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1301 	struct device *dev = info->dev;
1302 
1303 	trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
1304 				      xfer->hdr.protocol_id, xfer->hdr.seq,
1305 				      info->desc->max_rx_timeout_ms,
1306 				      xfer->hdr.poll_completion);
1307 
1308 	return scmi_wait_for_reply(dev, info->desc, cinfo, xfer,
1309 				   info->desc->max_rx_timeout_ms);
1310 }
1311 
1312 /**
1313  * scmi_xfer_raw_wait_for_message_response  - An helper to wait for a message
1314  * reply to an xfer raw request on a specific channel for the required timeout.
1315  *
1316  * @cinfo: SCMI channel info
1317  * @xfer: Reference to the transfer being waited for.
1318  * @timeout_ms: The maximum timeout in milliseconds
1319  *
1320  * Return: 0 on Success, error otherwise.
1321  */
scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer,unsigned int timeout_ms)1322 int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo,
1323 					    struct scmi_xfer *xfer,
1324 					    unsigned int timeout_ms)
1325 {
1326 	int ret;
1327 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1328 	struct device *dev = info->dev;
1329 
1330 	ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms);
1331 	if (ret)
1332 		dev_dbg(dev, "timed out in RAW response - HDR:%08X\n",
1333 			pack_scmi_header(&xfer->hdr));
1334 
1335 	return ret;
1336 }
1337 
1338 /**
1339  * do_xfer() - Do one transfer
1340  *
1341  * @ph: Pointer to SCMI protocol handle
1342  * @xfer: Transfer to initiate and wait for response
1343  *
1344  * Return: -ETIMEDOUT in case of no response, if transmit error,
1345  *	return corresponding error, else if all goes well,
1346  *	return 0.
1347  */
do_xfer(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)1348 static int do_xfer(const struct scmi_protocol_handle *ph,
1349 		   struct scmi_xfer *xfer)
1350 {
1351 	int ret;
1352 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1353 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1354 	struct device *dev = info->dev;
1355 	struct scmi_chan_info *cinfo;
1356 
1357 	/* Check for polling request on custom command xfers at first */
1358 	if (xfer->hdr.poll_completion &&
1359 	    !is_transport_polling_capable(info->desc)) {
1360 		dev_warn_once(dev,
1361 			      "Polling mode is not supported by transport.\n");
1362 		return -EINVAL;
1363 	}
1364 
1365 	cinfo = idr_find(&info->tx_idr, pi->proto->id);
1366 	if (unlikely(!cinfo))
1367 		return -EINVAL;
1368 
1369 	/* True ONLY if also supported by transport. */
1370 	if (is_polling_enabled(cinfo, info->desc))
1371 		xfer->hdr.poll_completion = true;
1372 
1373 	/*
1374 	 * Initialise protocol id now from protocol handle to avoid it being
1375 	 * overridden by mistake (or malice) by the protocol code mangling with
1376 	 * the scmi_xfer structure prior to this.
1377 	 */
1378 	xfer->hdr.protocol_id = pi->proto->id;
1379 	reinit_completion(&xfer->done);
1380 
1381 	trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
1382 			      xfer->hdr.protocol_id, xfer->hdr.seq,
1383 			      xfer->hdr.poll_completion);
1384 
1385 	/* Clear any stale status */
1386 	xfer->hdr.status = SCMI_SUCCESS;
1387 	xfer->state = SCMI_XFER_SENT_OK;
1388 	/*
1389 	 * Even though spinlocking is not needed here since no race is possible
1390 	 * on xfer->state due to the monotonically increasing tokens allocation,
1391 	 * we must anyway ensure xfer->state initialization is not re-ordered
1392 	 * after the .send_message() to be sure that on the RX path an early
1393 	 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
1394 	 */
1395 	smp_mb();
1396 
1397 	ret = info->desc->ops->send_message(cinfo, xfer);
1398 	if (ret < 0) {
1399 		dev_dbg(dev, "Failed to send message %d\n", ret);
1400 		return ret;
1401 	}
1402 
1403 	trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1404 			    xfer->hdr.id, "CMND", xfer->hdr.seq,
1405 			    xfer->hdr.status, xfer->tx.buf, xfer->tx.len);
1406 
1407 	ret = scmi_wait_for_message_response(cinfo, xfer);
1408 	if (!ret && xfer->hdr.status)
1409 		ret = scmi_to_linux_errno(xfer->hdr.status);
1410 
1411 	if (info->desc->ops->mark_txdone)
1412 		info->desc->ops->mark_txdone(cinfo, ret, xfer);
1413 
1414 	trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
1415 			    xfer->hdr.protocol_id, xfer->hdr.seq, ret);
1416 
1417 	return ret;
1418 }
1419 
reset_rx_to_maxsz(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)1420 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
1421 			      struct scmi_xfer *xfer)
1422 {
1423 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1424 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1425 
1426 	xfer->rx.len = info->desc->max_msg_size;
1427 }
1428 
1429 /**
1430  * do_xfer_with_response() - Do one transfer and wait until the delayed
1431  *	response is received
1432  *
1433  * @ph: Pointer to SCMI protocol handle
1434  * @xfer: Transfer to initiate and wait for response
1435  *
1436  * Using asynchronous commands in atomic/polling mode should be avoided since
1437  * it could cause long busy-waiting here, so ignore polling for the delayed
1438  * response and WARN if it was requested for this command transaction since
1439  * upper layers should refrain from issuing such kind of requests.
1440  *
1441  * The only other option would have been to refrain from using any asynchronous
1442  * command even if made available, when an atomic transport is detected, and
1443  * instead forcibly use the synchronous version (thing that can be easily
1444  * attained at the protocol layer), but this would also have led to longer
1445  * stalls of the channel for synchronous commands and possibly timeouts.
1446  * (in other words there is usually a good reason if a platform provides an
1447  *  asynchronous version of a command and we should prefer to use it...just not
1448  *  when using atomic/polling mode)
1449  *
1450  * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
1451  *	return corresponding error, else if all goes well, return 0.
1452  */
do_xfer_with_response(const struct scmi_protocol_handle * ph,struct scmi_xfer * xfer)1453 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
1454 				 struct scmi_xfer *xfer)
1455 {
1456 	int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
1457 	DECLARE_COMPLETION_ONSTACK(async_response);
1458 
1459 	xfer->async_done = &async_response;
1460 
1461 	/*
1462 	 * Delayed responses should not be polled, so an async command should
1463 	 * not have been used when requiring an atomic/poll context; WARN and
1464 	 * perform instead a sleeping wait.
1465 	 * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
1466 	 */
1467 	WARN_ON_ONCE(xfer->hdr.poll_completion);
1468 
1469 	ret = do_xfer(ph, xfer);
1470 	if (!ret) {
1471 		if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
1472 			dev_err(ph->dev,
1473 				"timed out in delayed resp(caller: %pS)\n",
1474 				(void *)_RET_IP_);
1475 			ret = -ETIMEDOUT;
1476 		} else if (xfer->hdr.status) {
1477 			ret = scmi_to_linux_errno(xfer->hdr.status);
1478 		}
1479 	}
1480 
1481 	xfer->async_done = NULL;
1482 	return ret;
1483 }
1484 
1485 /**
1486  * xfer_get_init() - Allocate and initialise one message for transmit
1487  *
1488  * @ph: Pointer to SCMI protocol handle
1489  * @msg_id: Message identifier
1490  * @tx_size: transmit message size
1491  * @rx_size: receive message size
1492  * @p: pointer to the allocated and initialised message
1493  *
1494  * This function allocates the message using @scmi_xfer_get and
1495  * initialise the header.
1496  *
1497  * Return: 0 if all went fine with @p pointing to message, else
1498  *	corresponding error.
1499  */
xfer_get_init(const struct scmi_protocol_handle * ph,u8 msg_id,size_t tx_size,size_t rx_size,struct scmi_xfer ** p)1500 static int xfer_get_init(const struct scmi_protocol_handle *ph,
1501 			 u8 msg_id, size_t tx_size, size_t rx_size,
1502 			 struct scmi_xfer **p)
1503 {
1504 	int ret;
1505 	struct scmi_xfer *xfer;
1506 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1507 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1508 	struct scmi_xfers_info *minfo = &info->tx_minfo;
1509 	struct device *dev = info->dev;
1510 
1511 	/* Ensure we have sane transfer sizes */
1512 	if (rx_size > info->desc->max_msg_size ||
1513 	    tx_size > info->desc->max_msg_size)
1514 		return -ERANGE;
1515 
1516 	xfer = scmi_xfer_get(pi->handle, minfo);
1517 	if (IS_ERR(xfer)) {
1518 		ret = PTR_ERR(xfer);
1519 		dev_err(dev, "failed to get free message slot(%d)\n", ret);
1520 		return ret;
1521 	}
1522 
1523 	/* Pick a sequence number and register this xfer as in-flight */
1524 	ret = scmi_xfer_pending_set(xfer, minfo);
1525 	if (ret) {
1526 		dev_err(pi->handle->dev,
1527 			"Failed to get monotonic token %d\n", ret);
1528 		__scmi_xfer_put(minfo, xfer);
1529 		return ret;
1530 	}
1531 
1532 	xfer->tx.len = tx_size;
1533 	xfer->rx.len = rx_size ? : info->desc->max_msg_size;
1534 	xfer->hdr.type = MSG_TYPE_COMMAND;
1535 	xfer->hdr.id = msg_id;
1536 	xfer->hdr.poll_completion = false;
1537 
1538 	*p = xfer;
1539 
1540 	return 0;
1541 }
1542 
1543 /**
1544  * version_get() - command to get the revision of the SCMI entity
1545  *
1546  * @ph: Pointer to SCMI protocol handle
1547  * @version: Holds returned version of protocol.
1548  *
1549  * Updates the SCMI information in the internal data structure.
1550  *
1551  * Return: 0 if all went fine, else return appropriate error.
1552  */
version_get(const struct scmi_protocol_handle * ph,u32 * version)1553 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
1554 {
1555 	int ret;
1556 	__le32 *rev_info;
1557 	struct scmi_xfer *t;
1558 
1559 	ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
1560 	if (ret)
1561 		return ret;
1562 
1563 	ret = do_xfer(ph, t);
1564 	if (!ret) {
1565 		rev_info = t->rx.buf;
1566 		*version = le32_to_cpu(*rev_info);
1567 	}
1568 
1569 	xfer_put(ph, t);
1570 	return ret;
1571 }
1572 
1573 /**
1574  * scmi_set_protocol_priv  - Set protocol specific data at init time
1575  *
1576  * @ph: A reference to the protocol handle.
1577  * @priv: The private data to set.
1578  * @version: The detected protocol version for the core to register.
1579  *
1580  * Return: 0 on Success
1581  */
scmi_set_protocol_priv(const struct scmi_protocol_handle * ph,void * priv,u32 version)1582 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
1583 				  void *priv, u32 version)
1584 {
1585 	struct scmi_protocol_instance *pi = ph_to_pi(ph);
1586 
1587 	pi->priv = priv;
1588 	pi->version = version;
1589 
1590 	return 0;
1591 }
1592 
1593 /**
1594  * scmi_get_protocol_priv  - Set protocol specific data at init time
1595  *
1596  * @ph: A reference to the protocol handle.
1597  *
1598  * Return: Protocol private data if any was set.
1599  */
scmi_get_protocol_priv(const struct scmi_protocol_handle * ph)1600 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
1601 {
1602 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1603 
1604 	return pi->priv;
1605 }
1606 
1607 static const struct scmi_xfer_ops xfer_ops = {
1608 	.version_get = version_get,
1609 	.xfer_get_init = xfer_get_init,
1610 	.reset_rx_to_maxsz = reset_rx_to_maxsz,
1611 	.do_xfer = do_xfer,
1612 	.do_xfer_with_response = do_xfer_with_response,
1613 	.xfer_put = xfer_put,
1614 };
1615 
1616 struct scmi_msg_resp_domain_name_get {
1617 	__le32 flags;
1618 	u8 name[SCMI_MAX_STR_SIZE];
1619 };
1620 
1621 /**
1622  * scmi_common_extended_name_get  - Common helper to get extended resources name
1623  * @ph: A protocol handle reference.
1624  * @cmd_id: The specific command ID to use.
1625  * @res_id: The specific resource ID to use.
1626  * @flags: A pointer to specific flags to use, if any.
1627  * @name: A pointer to the preallocated area where the retrieved name will be
1628  *	  stored as a NULL terminated string.
1629  * @len: The len in bytes of the @name char array.
1630  *
1631  * Return: 0 on Succcess
1632  */
scmi_common_extended_name_get(const struct scmi_protocol_handle * ph,u8 cmd_id,u32 res_id,u32 * flags,char * name,size_t len)1633 static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
1634 					 u8 cmd_id, u32 res_id, u32 *flags,
1635 					 char *name, size_t len)
1636 {
1637 	int ret;
1638 	size_t txlen;
1639 	struct scmi_xfer *t;
1640 	struct scmi_msg_resp_domain_name_get *resp;
1641 
1642 	txlen = !flags ? sizeof(res_id) : sizeof(res_id) + sizeof(*flags);
1643 	ret = ph->xops->xfer_get_init(ph, cmd_id, txlen, sizeof(*resp), &t);
1644 	if (ret)
1645 		goto out;
1646 
1647 	put_unaligned_le32(res_id, t->tx.buf);
1648 	if (flags)
1649 		put_unaligned_le32(*flags, t->tx.buf + sizeof(res_id));
1650 	resp = t->rx.buf;
1651 
1652 	ret = ph->xops->do_xfer(ph, t);
1653 	if (!ret)
1654 		strscpy(name, resp->name, len);
1655 
1656 	ph->xops->xfer_put(ph, t);
1657 out:
1658 	if (ret)
1659 		dev_warn(ph->dev,
1660 			 "Failed to get extended name - id:%u (ret:%d). Using %s\n",
1661 			 res_id, ret, name);
1662 	return ret;
1663 }
1664 
1665 /**
1666  * scmi_common_get_max_msg_size  - Get maximum message size
1667  * @ph: A protocol handle reference.
1668  *
1669  * Return: Maximum message size for the current protocol.
1670  */
scmi_common_get_max_msg_size(const struct scmi_protocol_handle * ph)1671 static int scmi_common_get_max_msg_size(const struct scmi_protocol_handle *ph)
1672 {
1673 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1674 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1675 
1676 	return info->desc->max_msg_size;
1677 }
1678 
1679 /**
1680  * struct scmi_iterator  - Iterator descriptor
1681  * @msg: A reference to the message TX buffer; filled by @prepare_message with
1682  *	 a proper custom command payload for each multi-part command request.
1683  * @resp: A reference to the response RX buffer; used by @update_state and
1684  *	  @process_response to parse the multi-part replies.
1685  * @t: A reference to the underlying xfer initialized and used transparently by
1686  *     the iterator internal routines.
1687  * @ph: A reference to the associated protocol handle to be used.
1688  * @ops: A reference to the custom provided iterator operations.
1689  * @state: The current iterator state; used and updated in turn by the iterators
1690  *	   internal routines and by the caller-provided @scmi_iterator_ops.
1691  * @priv: A reference to optional private data as provided by the caller and
1692  *	  passed back to the @@scmi_iterator_ops.
1693  */
1694 struct scmi_iterator {
1695 	void *msg;
1696 	void *resp;
1697 	struct scmi_xfer *t;
1698 	const struct scmi_protocol_handle *ph;
1699 	struct scmi_iterator_ops *ops;
1700 	struct scmi_iterator_state state;
1701 	void *priv;
1702 };
1703 
scmi_iterator_init(const struct scmi_protocol_handle * ph,struct scmi_iterator_ops * ops,unsigned int max_resources,u8 msg_id,size_t tx_size,void * priv)1704 static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
1705 				struct scmi_iterator_ops *ops,
1706 				unsigned int max_resources, u8 msg_id,
1707 				size_t tx_size, void *priv)
1708 {
1709 	int ret;
1710 	struct scmi_iterator *i;
1711 
1712 	i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
1713 	if (!i)
1714 		return ERR_PTR(-ENOMEM);
1715 
1716 	i->ph = ph;
1717 	i->ops = ops;
1718 	i->priv = priv;
1719 
1720 	ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
1721 	if (ret) {
1722 		devm_kfree(ph->dev, i);
1723 		return ERR_PTR(ret);
1724 	}
1725 
1726 	i->state.max_resources = max_resources;
1727 	i->msg = i->t->tx.buf;
1728 	i->resp = i->t->rx.buf;
1729 
1730 	return i;
1731 }
1732 
scmi_iterator_run(void * iter)1733 static int scmi_iterator_run(void *iter)
1734 {
1735 	int ret = -EINVAL;
1736 	struct scmi_iterator_ops *iops;
1737 	const struct scmi_protocol_handle *ph;
1738 	struct scmi_iterator_state *st;
1739 	struct scmi_iterator *i = iter;
1740 
1741 	if (!i || !i->ops || !i->ph)
1742 		return ret;
1743 
1744 	iops = i->ops;
1745 	ph = i->ph;
1746 	st = &i->state;
1747 
1748 	do {
1749 		iops->prepare_message(i->msg, st->desc_index, i->priv);
1750 		ret = ph->xops->do_xfer(ph, i->t);
1751 		if (ret)
1752 			break;
1753 
1754 		st->rx_len = i->t->rx.len;
1755 		ret = iops->update_state(st, i->resp, i->priv);
1756 		if (ret)
1757 			break;
1758 
1759 		if (st->num_returned > st->max_resources - st->desc_index) {
1760 			dev_err(ph->dev,
1761 				"No. of resources can't exceed %d\n",
1762 				st->max_resources);
1763 			ret = -EINVAL;
1764 			break;
1765 		}
1766 
1767 		for (st->loop_idx = 0; st->loop_idx < st->num_returned;
1768 		     st->loop_idx++) {
1769 			ret = iops->process_response(ph, i->resp, st, i->priv);
1770 			if (ret)
1771 				goto out;
1772 		}
1773 
1774 		st->desc_index += st->num_returned;
1775 		ph->xops->reset_rx_to_maxsz(ph, i->t);
1776 		/*
1777 		 * check for both returned and remaining to avoid infinite
1778 		 * loop due to buggy firmware
1779 		 */
1780 	} while (st->num_returned && st->num_remaining);
1781 
1782 out:
1783 	/* Finalize and destroy iterator */
1784 	ph->xops->xfer_put(ph, i->t);
1785 	devm_kfree(ph->dev, i);
1786 
1787 	return ret;
1788 }
1789 
1790 struct scmi_msg_get_fc_info {
1791 	__le32 domain;
1792 	__le32 message_id;
1793 };
1794 
1795 struct scmi_msg_resp_desc_fc {
1796 	__le32 attr;
1797 #define SUPPORTS_DOORBELL(x)		((x) & BIT(0))
1798 #define DOORBELL_REG_WIDTH(x)		FIELD_GET(GENMASK(2, 1), (x))
1799 	__le32 rate_limit;
1800 	__le32 chan_addr_low;
1801 	__le32 chan_addr_high;
1802 	__le32 chan_size;
1803 	__le32 db_addr_low;
1804 	__le32 db_addr_high;
1805 	__le32 db_set_lmask;
1806 	__le32 db_set_hmask;
1807 	__le32 db_preserve_lmask;
1808 	__le32 db_preserve_hmask;
1809 };
1810 
1811 static void
scmi_common_fastchannel_init(const struct scmi_protocol_handle * ph,u8 describe_id,u32 message_id,u32 valid_size,u32 domain,void __iomem ** p_addr,struct scmi_fc_db_info ** p_db,u32 * rate_limit)1812 scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph,
1813 			     u8 describe_id, u32 message_id, u32 valid_size,
1814 			     u32 domain, void __iomem **p_addr,
1815 			     struct scmi_fc_db_info **p_db, u32 *rate_limit)
1816 {
1817 	int ret;
1818 	u32 flags;
1819 	u64 phys_addr;
1820 	u8 size;
1821 	void __iomem *addr;
1822 	struct scmi_xfer *t;
1823 	struct scmi_fc_db_info *db = NULL;
1824 	struct scmi_msg_get_fc_info *info;
1825 	struct scmi_msg_resp_desc_fc *resp;
1826 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1827 
1828 	if (!p_addr) {
1829 		ret = -EINVAL;
1830 		goto err_out;
1831 	}
1832 
1833 	ret = ph->xops->xfer_get_init(ph, describe_id,
1834 				      sizeof(*info), sizeof(*resp), &t);
1835 	if (ret)
1836 		goto err_out;
1837 
1838 	info = t->tx.buf;
1839 	info->domain = cpu_to_le32(domain);
1840 	info->message_id = cpu_to_le32(message_id);
1841 
1842 	/*
1843 	 * Bail out on error leaving fc_info addresses zeroed; this includes
1844 	 * the case in which the requested domain/message_id does NOT support
1845 	 * fastchannels at all.
1846 	 */
1847 	ret = ph->xops->do_xfer(ph, t);
1848 	if (ret)
1849 		goto err_xfer;
1850 
1851 	resp = t->rx.buf;
1852 	flags = le32_to_cpu(resp->attr);
1853 	size = le32_to_cpu(resp->chan_size);
1854 	if (size != valid_size) {
1855 		ret = -EINVAL;
1856 		goto err_xfer;
1857 	}
1858 
1859 	if (rate_limit)
1860 		*rate_limit = le32_to_cpu(resp->rate_limit) & GENMASK(19, 0);
1861 
1862 	phys_addr = le32_to_cpu(resp->chan_addr_low);
1863 	phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32;
1864 	addr = devm_ioremap(ph->dev, phys_addr, size);
1865 	if (!addr) {
1866 		ret = -EADDRNOTAVAIL;
1867 		goto err_xfer;
1868 	}
1869 
1870 	*p_addr = addr;
1871 
1872 	if (p_db && SUPPORTS_DOORBELL(flags)) {
1873 		db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL);
1874 		if (!db) {
1875 			ret = -ENOMEM;
1876 			goto err_db;
1877 		}
1878 
1879 		size = 1 << DOORBELL_REG_WIDTH(flags);
1880 		phys_addr = le32_to_cpu(resp->db_addr_low);
1881 		phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32;
1882 		addr = devm_ioremap(ph->dev, phys_addr, size);
1883 		if (!addr) {
1884 			ret = -EADDRNOTAVAIL;
1885 			goto err_db_mem;
1886 		}
1887 
1888 		db->addr = addr;
1889 		db->width = size;
1890 		db->set = le32_to_cpu(resp->db_set_lmask);
1891 		db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32;
1892 		db->mask = le32_to_cpu(resp->db_preserve_lmask);
1893 		db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32;
1894 
1895 		*p_db = db;
1896 	}
1897 
1898 	ph->xops->xfer_put(ph, t);
1899 
1900 	dev_dbg(ph->dev,
1901 		"Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n",
1902 		pi->proto->id, message_id, domain);
1903 
1904 	return;
1905 
1906 err_db_mem:
1907 	devm_kfree(ph->dev, db);
1908 
1909 err_db:
1910 	*p_addr = NULL;
1911 
1912 err_xfer:
1913 	ph->xops->xfer_put(ph, t);
1914 
1915 err_out:
1916 	dev_warn(ph->dev,
1917 		 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n",
1918 		 pi->proto->id, message_id, domain, ret);
1919 }
1920 
1921 #define SCMI_PROTO_FC_RING_DB(w)			\
1922 do {							\
1923 	u##w val = 0;					\
1924 							\
1925 	if (db->mask)					\
1926 		val = ioread##w(db->addr) & db->mask;	\
1927 	iowrite##w((u##w)db->set | val, db->addr);	\
1928 } while (0)
1929 
scmi_common_fastchannel_db_ring(struct scmi_fc_db_info * db)1930 static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
1931 {
1932 	if (!db || !db->addr)
1933 		return;
1934 
1935 	if (db->width == 1)
1936 		SCMI_PROTO_FC_RING_DB(8);
1937 	else if (db->width == 2)
1938 		SCMI_PROTO_FC_RING_DB(16);
1939 	else if (db->width == 4)
1940 		SCMI_PROTO_FC_RING_DB(32);
1941 	else /* db->width == 8 */
1942 #ifdef CONFIG_64BIT
1943 		SCMI_PROTO_FC_RING_DB(64);
1944 #else
1945 	{
1946 		u64 val = 0;
1947 
1948 		if (db->mask)
1949 			val = ioread64_hi_lo(db->addr) & db->mask;
1950 		iowrite64_hi_lo(db->set | val, db->addr);
1951 	}
1952 #endif
1953 }
1954 
1955 /**
1956  * scmi_protocol_msg_check  - Check protocol message attributes
1957  *
1958  * @ph: A reference to the protocol handle.
1959  * @message_id: The ID of the message to check.
1960  * @attributes: A parameter to optionally return the retrieved message
1961  *		attributes, in case of Success.
1962  *
1963  * An helper to check protocol message attributes for a specific protocol
1964  * and message pair.
1965  *
1966  * Return: 0 on SUCCESS
1967  */
scmi_protocol_msg_check(const struct scmi_protocol_handle * ph,u32 message_id,u32 * attributes)1968 static int scmi_protocol_msg_check(const struct scmi_protocol_handle *ph,
1969 				   u32 message_id, u32 *attributes)
1970 {
1971 	int ret;
1972 	struct scmi_xfer *t;
1973 
1974 	ret = xfer_get_init(ph, PROTOCOL_MESSAGE_ATTRIBUTES,
1975 			    sizeof(__le32), 0, &t);
1976 	if (ret)
1977 		return ret;
1978 
1979 	put_unaligned_le32(message_id, t->tx.buf);
1980 	ret = do_xfer(ph, t);
1981 	if (!ret && attributes)
1982 		*attributes = get_unaligned_le32(t->rx.buf);
1983 	xfer_put(ph, t);
1984 
1985 	return ret;
1986 }
1987 
1988 static const struct scmi_proto_helpers_ops helpers_ops = {
1989 	.extended_name_get = scmi_common_extended_name_get,
1990 	.get_max_msg_size = scmi_common_get_max_msg_size,
1991 	.iter_response_init = scmi_iterator_init,
1992 	.iter_response_run = scmi_iterator_run,
1993 	.protocol_msg_check = scmi_protocol_msg_check,
1994 	.fastchannel_init = scmi_common_fastchannel_init,
1995 	.fastchannel_db_ring = scmi_common_fastchannel_db_ring,
1996 };
1997 
1998 /**
1999  * scmi_revision_area_get  - Retrieve version memory area.
2000  *
2001  * @ph: A reference to the protocol handle.
2002  *
2003  * A helper to grab the version memory area reference during SCMI Base protocol
2004  * initialization.
2005  *
2006  * Return: A reference to the version memory area associated to the SCMI
2007  *	   instance underlying this protocol handle.
2008  */
2009 struct scmi_revision_info *
scmi_revision_area_get(const struct scmi_protocol_handle * ph)2010 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
2011 {
2012 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
2013 
2014 	return pi->handle->version;
2015 }
2016 
2017 /**
2018  * scmi_protocol_version_negotiate  - Negotiate protocol version
2019  *
2020  * @ph: A reference to the protocol handle.
2021  *
2022  * An helper to negotiate a protocol version different from the latest
2023  * advertised as supported from the platform: on Success backward
2024  * compatibility is assured by the platform.
2025  *
2026  * Return: 0 on Success
2027  */
scmi_protocol_version_negotiate(struct scmi_protocol_handle * ph)2028 static int scmi_protocol_version_negotiate(struct scmi_protocol_handle *ph)
2029 {
2030 	int ret;
2031 	struct scmi_xfer *t;
2032 	struct scmi_protocol_instance *pi = ph_to_pi(ph);
2033 
2034 	/* At first check if NEGOTIATE_PROTOCOL_VERSION is supported ... */
2035 	ret = scmi_protocol_msg_check(ph, NEGOTIATE_PROTOCOL_VERSION, NULL);
2036 	if (ret)
2037 		return ret;
2038 
2039 	/* ... then attempt protocol version negotiation */
2040 	ret = xfer_get_init(ph, NEGOTIATE_PROTOCOL_VERSION,
2041 			    sizeof(__le32), 0, &t);
2042 	if (ret)
2043 		return ret;
2044 
2045 	put_unaligned_le32(pi->proto->supported_version, t->tx.buf);
2046 	ret = do_xfer(ph, t);
2047 	if (!ret)
2048 		pi->negotiated_version = pi->proto->supported_version;
2049 
2050 	xfer_put(ph, t);
2051 
2052 	return ret;
2053 }
2054 
2055 /**
2056  * scmi_alloc_init_protocol_instance  - Allocate and initialize a protocol
2057  * instance descriptor.
2058  * @info: The reference to the related SCMI instance.
2059  * @proto: The protocol descriptor.
2060  *
2061  * Allocate a new protocol instance descriptor, using the provided @proto
2062  * description, against the specified SCMI instance @info, and initialize it;
2063  * all resources management is handled via a dedicated per-protocol devres
2064  * group.
2065  *
2066  * Context: Assumes to be called with @protocols_mtx already acquired.
2067  * Return: A reference to a freshly allocated and initialized protocol instance
2068  *	   or ERR_PTR on failure. On failure the @proto reference is at first
2069  *	   put using @scmi_protocol_put() before releasing all the devres group.
2070  */
2071 static struct scmi_protocol_instance *
scmi_alloc_init_protocol_instance(struct scmi_info * info,const struct scmi_protocol * proto)2072 scmi_alloc_init_protocol_instance(struct scmi_info *info,
2073 				  const struct scmi_protocol *proto)
2074 {
2075 	int ret = -ENOMEM;
2076 	void *gid;
2077 	struct scmi_protocol_instance *pi;
2078 	const struct scmi_handle *handle = &info->handle;
2079 
2080 	/* Protocol specific devres group */
2081 	gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
2082 	if (!gid) {
2083 		scmi_protocol_put(proto);
2084 		goto out;
2085 	}
2086 
2087 	pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
2088 	if (!pi)
2089 		goto clean;
2090 
2091 	pi->gid = gid;
2092 	pi->proto = proto;
2093 	pi->handle = handle;
2094 	pi->ph.dev = handle->dev;
2095 	pi->ph.xops = &xfer_ops;
2096 	pi->ph.hops = &helpers_ops;
2097 	pi->ph.set_priv = scmi_set_protocol_priv;
2098 	pi->ph.get_priv = scmi_get_protocol_priv;
2099 	refcount_set(&pi->users, 1);
2100 	/* proto->init is assured NON NULL by scmi_protocol_register */
2101 	ret = pi->proto->instance_init(&pi->ph);
2102 	if (ret)
2103 		goto clean;
2104 
2105 	ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
2106 			GFP_KERNEL);
2107 	if (ret != proto->id)
2108 		goto clean;
2109 
2110 	/*
2111 	 * Warn but ignore events registration errors since we do not want
2112 	 * to skip whole protocols if their notifications are messed up.
2113 	 */
2114 	if (pi->proto->events) {
2115 		ret = scmi_register_protocol_events(handle, pi->proto->id,
2116 						    &pi->ph,
2117 						    pi->proto->events);
2118 		if (ret)
2119 			dev_warn(handle->dev,
2120 				 "Protocol:%X - Events Registration Failed - err:%d\n",
2121 				 pi->proto->id, ret);
2122 	}
2123 
2124 	devres_close_group(handle->dev, pi->gid);
2125 	dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
2126 
2127 	if (pi->version > proto->supported_version) {
2128 		ret = scmi_protocol_version_negotiate(&pi->ph);
2129 		if (!ret) {
2130 			dev_info(handle->dev,
2131 				 "Protocol 0x%X successfully negotiated version 0x%X\n",
2132 				 proto->id, pi->negotiated_version);
2133 		} else {
2134 			dev_warn(handle->dev,
2135 				 "Detected UNSUPPORTED higher version 0x%X for protocol 0x%X.\n",
2136 				 pi->version, pi->proto->id);
2137 			dev_warn(handle->dev,
2138 				 "Trying version 0x%X. Backward compatibility is NOT assured.\n",
2139 				 pi->proto->supported_version);
2140 		}
2141 	}
2142 
2143 	return pi;
2144 
2145 clean:
2146 	/* Take care to put the protocol module's owner before releasing all */
2147 	scmi_protocol_put(proto);
2148 	devres_release_group(handle->dev, gid);
2149 out:
2150 	return ERR_PTR(ret);
2151 }
2152 
2153 /**
2154  * scmi_get_protocol_instance  - Protocol initialization helper.
2155  * @handle: A reference to the SCMI platform instance.
2156  * @protocol_id: The protocol being requested.
2157  *
2158  * In case the required protocol has never been requested before for this
2159  * instance, allocate and initialize all the needed structures while handling
2160  * resource allocation with a dedicated per-protocol devres subgroup.
2161  *
2162  * Return: A reference to an initialized protocol instance or error on failure:
2163  *	   in particular returns -EPROBE_DEFER when the desired protocol could
2164  *	   NOT be found.
2165  */
2166 static struct scmi_protocol_instance * __must_check
scmi_get_protocol_instance(const struct scmi_handle * handle,u8 protocol_id)2167 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
2168 {
2169 	struct scmi_protocol_instance *pi;
2170 	struct scmi_info *info = handle_to_scmi_info(handle);
2171 
2172 	mutex_lock(&info->protocols_mtx);
2173 	pi = idr_find(&info->protocols, protocol_id);
2174 
2175 	if (pi) {
2176 		refcount_inc(&pi->users);
2177 	} else {
2178 		const struct scmi_protocol *proto;
2179 
2180 		/* Fails if protocol not registered on bus */
2181 		proto = scmi_protocol_get(protocol_id, &info->version);
2182 		if (proto)
2183 			pi = scmi_alloc_init_protocol_instance(info, proto);
2184 		else
2185 			pi = ERR_PTR(-EPROBE_DEFER);
2186 	}
2187 	mutex_unlock(&info->protocols_mtx);
2188 
2189 	return pi;
2190 }
2191 
2192 /**
2193  * scmi_protocol_acquire  - Protocol acquire
2194  * @handle: A reference to the SCMI platform instance.
2195  * @protocol_id: The protocol being requested.
2196  *
2197  * Register a new user for the requested protocol on the specified SCMI
2198  * platform instance, possibly triggering its initialization on first user.
2199  *
2200  * Return: 0 if protocol was acquired successfully.
2201  */
scmi_protocol_acquire(const struct scmi_handle * handle,u8 protocol_id)2202 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
2203 {
2204 	return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
2205 }
2206 
2207 /**
2208  * scmi_protocol_release  - Protocol de-initialization helper.
2209  * @handle: A reference to the SCMI platform instance.
2210  * @protocol_id: The protocol being requested.
2211  *
2212  * Remove one user for the specified protocol and triggers de-initialization
2213  * and resources de-allocation once the last user has gone.
2214  */
scmi_protocol_release(const struct scmi_handle * handle,u8 protocol_id)2215 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
2216 {
2217 	struct scmi_info *info = handle_to_scmi_info(handle);
2218 	struct scmi_protocol_instance *pi;
2219 
2220 	mutex_lock(&info->protocols_mtx);
2221 	pi = idr_find(&info->protocols, protocol_id);
2222 	if (WARN_ON(!pi))
2223 		goto out;
2224 
2225 	if (refcount_dec_and_test(&pi->users)) {
2226 		void *gid = pi->gid;
2227 
2228 		if (pi->proto->events)
2229 			scmi_deregister_protocol_events(handle, protocol_id);
2230 
2231 		if (pi->proto->instance_deinit)
2232 			pi->proto->instance_deinit(&pi->ph);
2233 
2234 		idr_remove(&info->protocols, protocol_id);
2235 
2236 		scmi_protocol_put(pi->proto);
2237 
2238 		devres_release_group(handle->dev, gid);
2239 		dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
2240 			protocol_id);
2241 	}
2242 
2243 out:
2244 	mutex_unlock(&info->protocols_mtx);
2245 }
2246 
scmi_setup_protocol_implemented(const struct scmi_protocol_handle * ph,u8 * prot_imp)2247 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
2248 				     u8 *prot_imp)
2249 {
2250 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
2251 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
2252 
2253 	info->protocols_imp = prot_imp;
2254 }
2255 
2256 static bool
scmi_is_protocol_implemented(const struct scmi_handle * handle,u8 prot_id)2257 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
2258 {
2259 	int i;
2260 	struct scmi_info *info = handle_to_scmi_info(handle);
2261 	struct scmi_revision_info *rev = handle->version;
2262 
2263 	if (!info->protocols_imp)
2264 		return false;
2265 
2266 	for (i = 0; i < rev->num_protocols; i++)
2267 		if (info->protocols_imp[i] == prot_id)
2268 			return true;
2269 	return false;
2270 }
2271 
2272 struct scmi_protocol_devres {
2273 	const struct scmi_handle *handle;
2274 	u8 protocol_id;
2275 };
2276 
scmi_devm_release_protocol(struct device * dev,void * res)2277 static void scmi_devm_release_protocol(struct device *dev, void *res)
2278 {
2279 	struct scmi_protocol_devres *dres = res;
2280 
2281 	scmi_protocol_release(dres->handle, dres->protocol_id);
2282 }
2283 
2284 static struct scmi_protocol_instance __must_check *
scmi_devres_protocol_instance_get(struct scmi_device * sdev,u8 protocol_id)2285 scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id)
2286 {
2287 	struct scmi_protocol_instance *pi;
2288 	struct scmi_protocol_devres *dres;
2289 
2290 	dres = devres_alloc(scmi_devm_release_protocol,
2291 			    sizeof(*dres), GFP_KERNEL);
2292 	if (!dres)
2293 		return ERR_PTR(-ENOMEM);
2294 
2295 	pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
2296 	if (IS_ERR(pi)) {
2297 		devres_free(dres);
2298 		return pi;
2299 	}
2300 
2301 	dres->handle = sdev->handle;
2302 	dres->protocol_id = protocol_id;
2303 	devres_add(&sdev->dev, dres);
2304 
2305 	return pi;
2306 }
2307 
2308 /**
2309  * scmi_devm_protocol_get  - Devres managed get protocol operations and handle
2310  * @sdev: A reference to an scmi_device whose embedded struct device is to
2311  *	  be used for devres accounting.
2312  * @protocol_id: The protocol being requested.
2313  * @ph: A pointer reference used to pass back the associated protocol handle.
2314  *
2315  * Get hold of a protocol accounting for its usage, eventually triggering its
2316  * initialization, and returning the protocol specific operations and related
2317  * protocol handle which will be used as first argument in most of the
2318  * protocols operations methods.
2319  * Being a devres based managed method, protocol hold will be automatically
2320  * released, and possibly de-initialized on last user, once the SCMI driver
2321  * owning the scmi_device is unbound from it.
2322  *
2323  * Return: A reference to the requested protocol operations or error.
2324  *	   Must be checked for errors by caller.
2325  */
2326 static const void __must_check *
scmi_devm_protocol_get(struct scmi_device * sdev,u8 protocol_id,struct scmi_protocol_handle ** ph)2327 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
2328 		       struct scmi_protocol_handle **ph)
2329 {
2330 	struct scmi_protocol_instance *pi;
2331 
2332 	if (!ph)
2333 		return ERR_PTR(-EINVAL);
2334 
2335 	pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
2336 	if (IS_ERR(pi))
2337 		return pi;
2338 
2339 	*ph = &pi->ph;
2340 
2341 	return pi->proto->ops;
2342 }
2343 
2344 /**
2345  * scmi_devm_protocol_acquire  - Devres managed helper to get hold of a protocol
2346  * @sdev: A reference to an scmi_device whose embedded struct device is to
2347  *	  be used for devres accounting.
2348  * @protocol_id: The protocol being requested.
2349  *
2350  * Get hold of a protocol accounting for its usage, possibly triggering its
2351  * initialization but without getting access to its protocol specific operations
2352  * and handle.
2353  *
2354  * Being a devres based managed method, protocol hold will be automatically
2355  * released, and possibly de-initialized on last user, once the SCMI driver
2356  * owning the scmi_device is unbound from it.
2357  *
2358  * Return: 0 on SUCCESS
2359  */
scmi_devm_protocol_acquire(struct scmi_device * sdev,u8 protocol_id)2360 static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev,
2361 						   u8 protocol_id)
2362 {
2363 	struct scmi_protocol_instance *pi;
2364 
2365 	pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
2366 	if (IS_ERR(pi))
2367 		return PTR_ERR(pi);
2368 
2369 	return 0;
2370 }
2371 
scmi_devm_protocol_match(struct device * dev,void * res,void * data)2372 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
2373 {
2374 	struct scmi_protocol_devres *dres = res;
2375 
2376 	if (WARN_ON(!dres || !data))
2377 		return 0;
2378 
2379 	return dres->protocol_id == *((u8 *)data);
2380 }
2381 
2382 /**
2383  * scmi_devm_protocol_put  - Devres managed put protocol operations and handle
2384  * @sdev: A reference to an scmi_device whose embedded struct device is to
2385  *	  be used for devres accounting.
2386  * @protocol_id: The protocol being requested.
2387  *
2388  * Explicitly release a protocol hold previously obtained calling the above
2389  * @scmi_devm_protocol_get.
2390  */
scmi_devm_protocol_put(struct scmi_device * sdev,u8 protocol_id)2391 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
2392 {
2393 	int ret;
2394 
2395 	ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
2396 			     scmi_devm_protocol_match, &protocol_id);
2397 	WARN_ON(ret);
2398 }
2399 
2400 /**
2401  * scmi_is_transport_atomic  - Method to check if underlying transport for an
2402  * SCMI instance is configured as atomic.
2403  *
2404  * @handle: A reference to the SCMI platform instance.
2405  * @atomic_threshold: An optional return value for the system wide currently
2406  *		      configured threshold for atomic operations.
2407  *
2408  * Return: True if transport is configured as atomic
2409  */
scmi_is_transport_atomic(const struct scmi_handle * handle,unsigned int * atomic_threshold)2410 static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
2411 				     unsigned int *atomic_threshold)
2412 {
2413 	bool ret;
2414 	struct scmi_info *info = handle_to_scmi_info(handle);
2415 
2416 	ret = info->desc->atomic_enabled &&
2417 		is_transport_polling_capable(info->desc);
2418 	if (ret && atomic_threshold)
2419 		*atomic_threshold = info->atomic_threshold;
2420 
2421 	return ret;
2422 }
2423 
2424 /**
2425  * scmi_handle_get() - Get the SCMI handle for a device
2426  *
2427  * @dev: pointer to device for which we want SCMI handle
2428  *
2429  * NOTE: The function does not track individual clients of the framework
2430  * and is expected to be maintained by caller of SCMI protocol library.
2431  * scmi_handle_put must be balanced with successful scmi_handle_get
2432  *
2433  * Return: pointer to handle if successful, NULL on error
2434  */
scmi_handle_get(struct device * dev)2435 static struct scmi_handle *scmi_handle_get(struct device *dev)
2436 {
2437 	struct list_head *p;
2438 	struct scmi_info *info;
2439 	struct scmi_handle *handle = NULL;
2440 
2441 	mutex_lock(&scmi_list_mutex);
2442 	list_for_each(p, &scmi_list) {
2443 		info = list_entry(p, struct scmi_info, node);
2444 		if (dev->parent == info->dev) {
2445 			info->users++;
2446 			handle = &info->handle;
2447 			break;
2448 		}
2449 	}
2450 	mutex_unlock(&scmi_list_mutex);
2451 
2452 	return handle;
2453 }
2454 
2455 /**
2456  * scmi_handle_put() - Release the handle acquired by scmi_handle_get
2457  *
2458  * @handle: handle acquired by scmi_handle_get
2459  *
2460  * NOTE: The function does not track individual clients of the framework
2461  * and is expected to be maintained by caller of SCMI protocol library.
2462  * scmi_handle_put must be balanced with successful scmi_handle_get
2463  *
2464  * Return: 0 is successfully released
2465  *	if null was passed, it returns -EINVAL;
2466  */
scmi_handle_put(const struct scmi_handle * handle)2467 static int scmi_handle_put(const struct scmi_handle *handle)
2468 {
2469 	struct scmi_info *info;
2470 
2471 	if (!handle)
2472 		return -EINVAL;
2473 
2474 	info = handle_to_scmi_info(handle);
2475 	mutex_lock(&scmi_list_mutex);
2476 	if (!WARN_ON(!info->users))
2477 		info->users--;
2478 	mutex_unlock(&scmi_list_mutex);
2479 
2480 	return 0;
2481 }
2482 
scmi_device_link_add(struct device * consumer,struct device * supplier)2483 static void scmi_device_link_add(struct device *consumer,
2484 				 struct device *supplier)
2485 {
2486 	struct device_link *link;
2487 
2488 	link = device_link_add(consumer, supplier, DL_FLAG_AUTOREMOVE_CONSUMER);
2489 
2490 	WARN_ON(!link);
2491 }
2492 
scmi_set_handle(struct scmi_device * scmi_dev)2493 static void scmi_set_handle(struct scmi_device *scmi_dev)
2494 {
2495 	scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
2496 	if (scmi_dev->handle)
2497 		scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev);
2498 }
2499 
__scmi_xfer_info_init(struct scmi_info * sinfo,struct scmi_xfers_info * info)2500 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
2501 				 struct scmi_xfers_info *info)
2502 {
2503 	int i;
2504 	struct scmi_xfer *xfer;
2505 	struct device *dev = sinfo->dev;
2506 	const struct scmi_desc *desc = sinfo->desc;
2507 
2508 	/* Pre-allocated messages, no more than what hdr.seq can support */
2509 	if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
2510 		dev_err(dev,
2511 			"Invalid maximum messages %d, not in range [1 - %lu]\n",
2512 			info->max_msg, MSG_TOKEN_MAX);
2513 		return -EINVAL;
2514 	}
2515 
2516 	hash_init(info->pending_xfers);
2517 
2518 	/* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
2519 	info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX,
2520 						    GFP_KERNEL);
2521 	if (!info->xfer_alloc_table)
2522 		return -ENOMEM;
2523 
2524 	/*
2525 	 * Preallocate a number of xfers equal to max inflight messages,
2526 	 * pre-initialize the buffer pointer to pre-allocated buffers and
2527 	 * attach all of them to the free list
2528 	 */
2529 	INIT_HLIST_HEAD(&info->free_xfers);
2530 	for (i = 0; i < info->max_msg; i++) {
2531 		xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
2532 		if (!xfer)
2533 			return -ENOMEM;
2534 
2535 		xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
2536 					    GFP_KERNEL);
2537 		if (!xfer->rx.buf)
2538 			return -ENOMEM;
2539 
2540 		xfer->tx.buf = xfer->rx.buf;
2541 		init_completion(&xfer->done);
2542 		spin_lock_init(&xfer->lock);
2543 
2544 		/* Add initialized xfer to the free list */
2545 		hlist_add_head(&xfer->node, &info->free_xfers);
2546 	}
2547 
2548 	spin_lock_init(&info->xfer_lock);
2549 
2550 	return 0;
2551 }
2552 
scmi_channels_max_msg_configure(struct scmi_info * sinfo)2553 static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
2554 {
2555 	const struct scmi_desc *desc = sinfo->desc;
2556 
2557 	if (!desc->ops->get_max_msg) {
2558 		sinfo->tx_minfo.max_msg = desc->max_msg;
2559 		sinfo->rx_minfo.max_msg = desc->max_msg;
2560 	} else {
2561 		struct scmi_chan_info *base_cinfo;
2562 
2563 		base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
2564 		if (!base_cinfo)
2565 			return -EINVAL;
2566 		sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
2567 
2568 		/* RX channel is optional so can be skipped */
2569 		base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
2570 		if (base_cinfo)
2571 			sinfo->rx_minfo.max_msg =
2572 				desc->ops->get_max_msg(base_cinfo);
2573 	}
2574 
2575 	return 0;
2576 }
2577 
scmi_xfer_info_init(struct scmi_info * sinfo)2578 static int scmi_xfer_info_init(struct scmi_info *sinfo)
2579 {
2580 	int ret;
2581 
2582 	ret = scmi_channels_max_msg_configure(sinfo);
2583 	if (ret)
2584 		return ret;
2585 
2586 	ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
2587 	if (!ret && !idr_is_empty(&sinfo->rx_idr))
2588 		ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
2589 
2590 	return ret;
2591 }
2592 
scmi_chan_setup(struct scmi_info * info,struct device_node * of_node,int prot_id,bool tx)2593 static int scmi_chan_setup(struct scmi_info *info, struct device_node *of_node,
2594 			   int prot_id, bool tx)
2595 {
2596 	int ret, idx;
2597 	char name[32];
2598 	struct scmi_chan_info *cinfo;
2599 	struct idr *idr;
2600 	struct scmi_device *tdev = NULL;
2601 
2602 	/* Transmit channel is first entry i.e. index 0 */
2603 	idx = tx ? 0 : 1;
2604 	idr = tx ? &info->tx_idr : &info->rx_idr;
2605 
2606 	if (!info->desc->ops->chan_available(of_node, idx)) {
2607 		cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
2608 		if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
2609 			return -EINVAL;
2610 		goto idr_alloc;
2611 	}
2612 
2613 	cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
2614 	if (!cinfo)
2615 		return -ENOMEM;
2616 
2617 	cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms;
2618 
2619 	/* Create a unique name for this transport device */
2620 	snprintf(name, 32, "__scmi_transport_device_%s_%02X",
2621 		 idx ? "rx" : "tx", prot_id);
2622 	/* Create a uniquely named, dedicated transport device for this chan */
2623 	tdev = scmi_device_create(of_node, info->dev, prot_id, name);
2624 	if (!tdev) {
2625 		dev_err(info->dev,
2626 			"failed to create transport device (%s)\n", name);
2627 		devm_kfree(info->dev, cinfo);
2628 		return -EINVAL;
2629 	}
2630 	of_node_get(of_node);
2631 
2632 	cinfo->id = prot_id;
2633 	cinfo->dev = &tdev->dev;
2634 	ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
2635 	if (ret) {
2636 		of_node_put(of_node);
2637 		scmi_device_destroy(info->dev, prot_id, name);
2638 		devm_kfree(info->dev, cinfo);
2639 		return ret;
2640 	}
2641 
2642 	if (tx && is_polling_required(cinfo, info->desc)) {
2643 		if (is_transport_polling_capable(info->desc))
2644 			dev_info(&tdev->dev,
2645 				 "Enabled polling mode TX channel - prot_id:%d\n",
2646 				 prot_id);
2647 		else
2648 			dev_warn(&tdev->dev,
2649 				 "Polling mode NOT supported by transport.\n");
2650 	}
2651 
2652 idr_alloc:
2653 	ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
2654 	if (ret != prot_id) {
2655 		dev_err(info->dev,
2656 			"unable to allocate SCMI idr slot err %d\n", ret);
2657 		/* Destroy channel and device only if created by this call. */
2658 		if (tdev) {
2659 			of_node_put(of_node);
2660 			scmi_device_destroy(info->dev, prot_id, name);
2661 			devm_kfree(info->dev, cinfo);
2662 		}
2663 		return ret;
2664 	}
2665 
2666 	cinfo->handle = &info->handle;
2667 	return 0;
2668 }
2669 
2670 static inline int
scmi_txrx_setup(struct scmi_info * info,struct device_node * of_node,int prot_id)2671 scmi_txrx_setup(struct scmi_info *info, struct device_node *of_node,
2672 		int prot_id)
2673 {
2674 	int ret = scmi_chan_setup(info, of_node, prot_id, true);
2675 
2676 	if (!ret) {
2677 		/* Rx is optional, report only memory errors */
2678 		ret = scmi_chan_setup(info, of_node, prot_id, false);
2679 		if (ret && ret != -ENOMEM)
2680 			ret = 0;
2681 	}
2682 
2683 	if (ret)
2684 		dev_err(info->dev,
2685 			"failed to setup channel for protocol:0x%X\n", prot_id);
2686 
2687 	return ret;
2688 }
2689 
2690 /**
2691  * scmi_channels_setup  - Helper to initialize all required channels
2692  *
2693  * @info: The SCMI instance descriptor.
2694  *
2695  * Initialize all the channels found described in the DT against the underlying
2696  * configured transport using custom defined dedicated devices instead of
2697  * borrowing devices from the SCMI drivers; this way channels are initialized
2698  * upfront during core SCMI stack probing and are no more coupled with SCMI
2699  * devices used by SCMI drivers.
2700  *
2701  * Note that, even though a pair of TX/RX channels is associated to each
2702  * protocol defined in the DT, a distinct freshly initialized channel is
2703  * created only if the DT node for the protocol at hand describes a dedicated
2704  * channel: in all the other cases the common BASE protocol channel is reused.
2705  *
2706  * Return: 0 on Success
2707  */
scmi_channels_setup(struct scmi_info * info)2708 static int scmi_channels_setup(struct scmi_info *info)
2709 {
2710 	int ret;
2711 	struct device_node *child, *top_np = info->dev->of_node;
2712 
2713 	/* Initialize a common generic channel at first */
2714 	ret = scmi_txrx_setup(info, top_np, SCMI_PROTOCOL_BASE);
2715 	if (ret)
2716 		return ret;
2717 
2718 	for_each_available_child_of_node(top_np, child) {
2719 		u32 prot_id;
2720 
2721 		if (of_property_read_u32(child, "reg", &prot_id))
2722 			continue;
2723 
2724 		if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
2725 			dev_err(info->dev,
2726 				"Out of range protocol %d\n", prot_id);
2727 
2728 		ret = scmi_txrx_setup(info, child, prot_id);
2729 		if (ret) {
2730 			of_node_put(child);
2731 			return ret;
2732 		}
2733 	}
2734 
2735 	return 0;
2736 }
2737 
scmi_chan_destroy(int id,void * p,void * idr)2738 static int scmi_chan_destroy(int id, void *p, void *idr)
2739 {
2740 	struct scmi_chan_info *cinfo = p;
2741 
2742 	if (cinfo->dev) {
2743 		struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
2744 		struct scmi_device *sdev = to_scmi_dev(cinfo->dev);
2745 
2746 		of_node_put(cinfo->dev->of_node);
2747 		scmi_device_destroy(info->dev, id, sdev->name);
2748 		cinfo->dev = NULL;
2749 	}
2750 
2751 	idr_remove(idr, id);
2752 
2753 	return 0;
2754 }
2755 
scmi_cleanup_channels(struct scmi_info * info,struct idr * idr)2756 static void scmi_cleanup_channels(struct scmi_info *info, struct idr *idr)
2757 {
2758 	/* At first free all channels at the transport layer ... */
2759 	idr_for_each(idr, info->desc->ops->chan_free, idr);
2760 
2761 	/* ...then destroy all underlying devices */
2762 	idr_for_each(idr, scmi_chan_destroy, idr);
2763 
2764 	idr_destroy(idr);
2765 }
2766 
scmi_cleanup_txrx_channels(struct scmi_info * info)2767 static void scmi_cleanup_txrx_channels(struct scmi_info *info)
2768 {
2769 	scmi_cleanup_channels(info, &info->tx_idr);
2770 
2771 	scmi_cleanup_channels(info, &info->rx_idr);
2772 }
2773 
scmi_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)2774 static int scmi_bus_notifier(struct notifier_block *nb,
2775 			     unsigned long action, void *data)
2776 {
2777 	struct scmi_info *info = bus_nb_to_scmi_info(nb);
2778 	struct scmi_device *sdev = to_scmi_dev(data);
2779 
2780 	/* Skip transport devices and devices of different SCMI instances */
2781 	if (!strncmp(sdev->name, "__scmi_transport_device", 23) ||
2782 	    sdev->dev.parent != info->dev)
2783 		return NOTIFY_DONE;
2784 
2785 	switch (action) {
2786 	case BUS_NOTIFY_BIND_DRIVER:
2787 		/* setup handle now as the transport is ready */
2788 		scmi_set_handle(sdev);
2789 		break;
2790 	case BUS_NOTIFY_UNBOUND_DRIVER:
2791 		scmi_handle_put(sdev->handle);
2792 		sdev->handle = NULL;
2793 		break;
2794 	default:
2795 		return NOTIFY_DONE;
2796 	}
2797 
2798 	dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev),
2799 		sdev->name, action == BUS_NOTIFY_BIND_DRIVER ?
2800 		"about to be BOUND." : "UNBOUND.");
2801 
2802 	return NOTIFY_OK;
2803 }
2804 
scmi_device_request_notifier(struct notifier_block * nb,unsigned long action,void * data)2805 static int scmi_device_request_notifier(struct notifier_block *nb,
2806 					unsigned long action, void *data)
2807 {
2808 	struct device_node *np;
2809 	struct scmi_device_id *id_table = data;
2810 	struct scmi_info *info = req_nb_to_scmi_info(nb);
2811 
2812 	np = idr_find(&info->active_protocols, id_table->protocol_id);
2813 	if (!np)
2814 		return NOTIFY_DONE;
2815 
2816 	dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n",
2817 		action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-",
2818 		id_table->name, id_table->protocol_id);
2819 
2820 	switch (action) {
2821 	case SCMI_BUS_NOTIFY_DEVICE_REQUEST:
2822 		scmi_create_protocol_devices(np, info, id_table->protocol_id,
2823 					     id_table->name);
2824 		break;
2825 	case SCMI_BUS_NOTIFY_DEVICE_UNREQUEST:
2826 		scmi_destroy_protocol_devices(info, id_table->protocol_id,
2827 					      id_table->name);
2828 		break;
2829 	default:
2830 		return NOTIFY_DONE;
2831 	}
2832 
2833 	return NOTIFY_OK;
2834 }
2835 
scmi_debugfs_common_cleanup(void * d)2836 static void scmi_debugfs_common_cleanup(void *d)
2837 {
2838 	struct scmi_debug_info *dbg = d;
2839 
2840 	if (!dbg)
2841 		return;
2842 
2843 	debugfs_remove_recursive(dbg->top_dentry);
2844 	kfree(dbg->name);
2845 	kfree(dbg->type);
2846 }
2847 
scmi_debugfs_common_setup(struct scmi_info * info)2848 static struct scmi_debug_info *scmi_debugfs_common_setup(struct scmi_info *info)
2849 {
2850 	char top_dir[16];
2851 	struct dentry *trans, *top_dentry;
2852 	struct scmi_debug_info *dbg;
2853 	const char *c_ptr = NULL;
2854 
2855 	dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL);
2856 	if (!dbg)
2857 		return NULL;
2858 
2859 	dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL);
2860 	if (!dbg->name) {
2861 		devm_kfree(info->dev, dbg);
2862 		return NULL;
2863 	}
2864 
2865 	of_property_read_string(info->dev->of_node, "compatible", &c_ptr);
2866 	dbg->type = kstrdup(c_ptr, GFP_KERNEL);
2867 	if (!dbg->type) {
2868 		kfree(dbg->name);
2869 		devm_kfree(info->dev, dbg);
2870 		return NULL;
2871 	}
2872 
2873 	snprintf(top_dir, 16, "%d", info->id);
2874 	top_dentry = debugfs_create_dir(top_dir, scmi_top_dentry);
2875 	trans = debugfs_create_dir("transport", top_dentry);
2876 
2877 	dbg->is_atomic = info->desc->atomic_enabled &&
2878 				is_transport_polling_capable(info->desc);
2879 
2880 	debugfs_create_str("instance_name", 0400, top_dentry,
2881 			   (char **)&dbg->name);
2882 
2883 	debugfs_create_u32("atomic_threshold_us", 0400, top_dentry,
2884 			   &info->atomic_threshold);
2885 
2886 	debugfs_create_str("type", 0400, trans, (char **)&dbg->type);
2887 
2888 	debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic);
2889 
2890 	debugfs_create_u32("max_rx_timeout_ms", 0400, trans,
2891 			   (u32 *)&info->desc->max_rx_timeout_ms);
2892 
2893 	debugfs_create_u32("max_msg_size", 0400, trans,
2894 			   (u32 *)&info->desc->max_msg_size);
2895 
2896 	debugfs_create_u32("tx_max_msg", 0400, trans,
2897 			   (u32 *)&info->tx_minfo.max_msg);
2898 
2899 	debugfs_create_u32("rx_max_msg", 0400, trans,
2900 			   (u32 *)&info->rx_minfo.max_msg);
2901 
2902 	dbg->top_dentry = top_dentry;
2903 
2904 	if (devm_add_action_or_reset(info->dev,
2905 				     scmi_debugfs_common_cleanup, dbg)) {
2906 		scmi_debugfs_common_cleanup(dbg);
2907 		return NULL;
2908 	}
2909 
2910 	return dbg;
2911 }
2912 
scmi_debugfs_raw_mode_setup(struct scmi_info * info)2913 static int scmi_debugfs_raw_mode_setup(struct scmi_info *info)
2914 {
2915 	int id, num_chans = 0, ret = 0;
2916 	struct scmi_chan_info *cinfo;
2917 	u8 channels[SCMI_MAX_CHANNELS] = {};
2918 	DECLARE_BITMAP(protos, SCMI_MAX_CHANNELS) = {};
2919 
2920 	if (!info->dbg)
2921 		return -EINVAL;
2922 
2923 	/* Enumerate all channels to collect their ids */
2924 	idr_for_each_entry(&info->tx_idr, cinfo, id) {
2925 		/*
2926 		 * Cannot happen, but be defensive.
2927 		 * Zero as num_chans is ok, warn and carry on.
2928 		 */
2929 		if (num_chans >= SCMI_MAX_CHANNELS || !cinfo) {
2930 			dev_warn(info->dev,
2931 				 "SCMI RAW - Error enumerating channels\n");
2932 			break;
2933 		}
2934 
2935 		if (!test_bit(cinfo->id, protos)) {
2936 			channels[num_chans++] = cinfo->id;
2937 			set_bit(cinfo->id, protos);
2938 		}
2939 	}
2940 
2941 	info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry,
2942 				       info->id, channels, num_chans,
2943 				       info->desc, info->tx_minfo.max_msg);
2944 	if (IS_ERR(info->raw)) {
2945 		dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n");
2946 		ret = PTR_ERR(info->raw);
2947 		info->raw = NULL;
2948 	}
2949 
2950 	return ret;
2951 }
2952 
scmi_probe(struct platform_device * pdev)2953 static int scmi_probe(struct platform_device *pdev)
2954 {
2955 	int ret;
2956 	char *err_str = "probe failure\n";
2957 	struct scmi_handle *handle;
2958 	const struct scmi_desc *desc;
2959 	struct scmi_info *info;
2960 	bool coex = IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX);
2961 	struct device *dev = &pdev->dev;
2962 	struct device_node *child, *np = dev->of_node;
2963 
2964 	desc = of_device_get_match_data(dev);
2965 	if (!desc)
2966 		return -EINVAL;
2967 
2968 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
2969 	if (!info)
2970 		return -ENOMEM;
2971 
2972 	info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL);
2973 	if (info->id < 0)
2974 		return info->id;
2975 
2976 	info->dev = dev;
2977 	info->desc = desc;
2978 	info->bus_nb.notifier_call = scmi_bus_notifier;
2979 	info->dev_req_nb.notifier_call = scmi_device_request_notifier;
2980 	INIT_LIST_HEAD(&info->node);
2981 	idr_init(&info->protocols);
2982 	mutex_init(&info->protocols_mtx);
2983 	idr_init(&info->active_protocols);
2984 	mutex_init(&info->devreq_mtx);
2985 
2986 	platform_set_drvdata(pdev, info);
2987 	idr_init(&info->tx_idr);
2988 	idr_init(&info->rx_idr);
2989 
2990 	handle = &info->handle;
2991 	handle->dev = info->dev;
2992 	handle->version = &info->version;
2993 	handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
2994 	handle->devm_protocol_get = scmi_devm_protocol_get;
2995 	handle->devm_protocol_put = scmi_devm_protocol_put;
2996 
2997 	/* System wide atomic threshold for atomic ops .. if any */
2998 	if (!of_property_read_u32(np, "atomic-threshold-us",
2999 				  &info->atomic_threshold))
3000 		dev_info(dev,
3001 			 "SCMI System wide atomic threshold set to %d us\n",
3002 			 info->atomic_threshold);
3003 	handle->is_transport_atomic = scmi_is_transport_atomic;
3004 
3005 	if (desc->ops->link_supplier) {
3006 		ret = desc->ops->link_supplier(dev);
3007 		if (ret) {
3008 			err_str = "transport not ready\n";
3009 			goto clear_ida;
3010 		}
3011 	}
3012 
3013 	/* Setup all channels described in the DT at first */
3014 	ret = scmi_channels_setup(info);
3015 	if (ret) {
3016 		err_str = "failed to setup channels\n";
3017 		goto clear_ida;
3018 	}
3019 
3020 	ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb);
3021 	if (ret) {
3022 		err_str = "failed to register bus notifier\n";
3023 		goto clear_txrx_setup;
3024 	}
3025 
3026 	ret = blocking_notifier_chain_register(&scmi_requested_devices_nh,
3027 					       &info->dev_req_nb);
3028 	if (ret) {
3029 		err_str = "failed to register device notifier\n";
3030 		goto clear_bus_notifier;
3031 	}
3032 
3033 	ret = scmi_xfer_info_init(info);
3034 	if (ret) {
3035 		err_str = "failed to init xfers pool\n";
3036 		goto clear_dev_req_notifier;
3037 	}
3038 
3039 	if (scmi_top_dentry) {
3040 		info->dbg = scmi_debugfs_common_setup(info);
3041 		if (!info->dbg)
3042 			dev_warn(dev, "Failed to setup SCMI debugfs.\n");
3043 
3044 		if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
3045 			ret = scmi_debugfs_raw_mode_setup(info);
3046 			if (!coex) {
3047 				if (ret)
3048 					goto clear_dev_req_notifier;
3049 
3050 				/* Bail out anyway when coex disabled. */
3051 				return 0;
3052 			}
3053 
3054 			/* Coex enabled, carry on in any case. */
3055 			dev_info(dev, "SCMI RAW Mode COEX enabled !\n");
3056 		}
3057 	}
3058 
3059 	if (scmi_notification_init(handle))
3060 		dev_err(dev, "SCMI Notifications NOT available.\n");
3061 
3062 	if (info->desc->atomic_enabled &&
3063 	    !is_transport_polling_capable(info->desc))
3064 		dev_err(dev,
3065 			"Transport is not polling capable. Atomic mode not supported.\n");
3066 
3067 	/*
3068 	 * Trigger SCMI Base protocol initialization.
3069 	 * It's mandatory and won't be ever released/deinit until the
3070 	 * SCMI stack is shutdown/unloaded as a whole.
3071 	 */
3072 	ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
3073 	if (ret) {
3074 		err_str = "unable to communicate with SCMI\n";
3075 		if (coex) {
3076 			dev_err(dev, "%s", err_str);
3077 			return 0;
3078 		}
3079 		goto notification_exit;
3080 	}
3081 
3082 	mutex_lock(&scmi_list_mutex);
3083 	list_add_tail(&info->node, &scmi_list);
3084 	mutex_unlock(&scmi_list_mutex);
3085 
3086 	for_each_available_child_of_node(np, child) {
3087 		u32 prot_id;
3088 
3089 		if (of_property_read_u32(child, "reg", &prot_id))
3090 			continue;
3091 
3092 		if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
3093 			dev_err(dev, "Out of range protocol %d\n", prot_id);
3094 
3095 		if (!scmi_is_protocol_implemented(handle, prot_id)) {
3096 			dev_err(dev, "SCMI protocol %d not implemented\n",
3097 				prot_id);
3098 			continue;
3099 		}
3100 
3101 		/*
3102 		 * Save this valid DT protocol descriptor amongst
3103 		 * @active_protocols for this SCMI instance/
3104 		 */
3105 		ret = idr_alloc(&info->active_protocols, child,
3106 				prot_id, prot_id + 1, GFP_KERNEL);
3107 		if (ret != prot_id) {
3108 			dev_err(dev, "SCMI protocol %d already activated. Skip\n",
3109 				prot_id);
3110 			continue;
3111 		}
3112 
3113 		of_node_get(child);
3114 		scmi_create_protocol_devices(child, info, prot_id, NULL);
3115 	}
3116 
3117 	return 0;
3118 
3119 notification_exit:
3120 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
3121 		scmi_raw_mode_cleanup(info->raw);
3122 	scmi_notification_exit(&info->handle);
3123 clear_dev_req_notifier:
3124 	blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
3125 					   &info->dev_req_nb);
3126 clear_bus_notifier:
3127 	bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
3128 clear_txrx_setup:
3129 	scmi_cleanup_txrx_channels(info);
3130 clear_ida:
3131 	ida_free(&scmi_id, info->id);
3132 
3133 	return dev_err_probe(dev, ret, "%s", err_str);
3134 }
3135 
scmi_remove(struct platform_device * pdev)3136 static void scmi_remove(struct platform_device *pdev)
3137 {
3138 	int id;
3139 	struct scmi_info *info = platform_get_drvdata(pdev);
3140 	struct device_node *child;
3141 
3142 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
3143 		scmi_raw_mode_cleanup(info->raw);
3144 
3145 	mutex_lock(&scmi_list_mutex);
3146 	if (info->users)
3147 		dev_warn(&pdev->dev,
3148 			 "Still active SCMI users will be forcibly unbound.\n");
3149 	list_del(&info->node);
3150 	mutex_unlock(&scmi_list_mutex);
3151 
3152 	scmi_notification_exit(&info->handle);
3153 
3154 	mutex_lock(&info->protocols_mtx);
3155 	idr_destroy(&info->protocols);
3156 	mutex_unlock(&info->protocols_mtx);
3157 
3158 	idr_for_each_entry(&info->active_protocols, child, id)
3159 		of_node_put(child);
3160 	idr_destroy(&info->active_protocols);
3161 
3162 	blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
3163 					   &info->dev_req_nb);
3164 	bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
3165 
3166 	/* Safe to free channels since no more users */
3167 	scmi_cleanup_txrx_channels(info);
3168 
3169 	ida_free(&scmi_id, info->id);
3170 }
3171 
protocol_version_show(struct device * dev,struct device_attribute * attr,char * buf)3172 static ssize_t protocol_version_show(struct device *dev,
3173 				     struct device_attribute *attr, char *buf)
3174 {
3175 	struct scmi_info *info = dev_get_drvdata(dev);
3176 
3177 	return sprintf(buf, "%u.%u\n", info->version.major_ver,
3178 		       info->version.minor_ver);
3179 }
3180 static DEVICE_ATTR_RO(protocol_version);
3181 
firmware_version_show(struct device * dev,struct device_attribute * attr,char * buf)3182 static ssize_t firmware_version_show(struct device *dev,
3183 				     struct device_attribute *attr, char *buf)
3184 {
3185 	struct scmi_info *info = dev_get_drvdata(dev);
3186 
3187 	return sprintf(buf, "0x%x\n", info->version.impl_ver);
3188 }
3189 static DEVICE_ATTR_RO(firmware_version);
3190 
vendor_id_show(struct device * dev,struct device_attribute * attr,char * buf)3191 static ssize_t vendor_id_show(struct device *dev,
3192 			      struct device_attribute *attr, char *buf)
3193 {
3194 	struct scmi_info *info = dev_get_drvdata(dev);
3195 
3196 	return sprintf(buf, "%s\n", info->version.vendor_id);
3197 }
3198 static DEVICE_ATTR_RO(vendor_id);
3199 
sub_vendor_id_show(struct device * dev,struct device_attribute * attr,char * buf)3200 static ssize_t sub_vendor_id_show(struct device *dev,
3201 				  struct device_attribute *attr, char *buf)
3202 {
3203 	struct scmi_info *info = dev_get_drvdata(dev);
3204 
3205 	return sprintf(buf, "%s\n", info->version.sub_vendor_id);
3206 }
3207 static DEVICE_ATTR_RO(sub_vendor_id);
3208 
3209 static struct attribute *versions_attrs[] = {
3210 	&dev_attr_firmware_version.attr,
3211 	&dev_attr_protocol_version.attr,
3212 	&dev_attr_vendor_id.attr,
3213 	&dev_attr_sub_vendor_id.attr,
3214 	NULL,
3215 };
3216 ATTRIBUTE_GROUPS(versions);
3217 
3218 /* Each compatible listed below must have descriptor associated with it */
3219 static const struct of_device_id scmi_of_match[] = {
3220 #ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
3221 	{ .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
3222 #endif
3223 #ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE
3224 	{ .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
3225 #endif
3226 #ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
3227 	{ .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
3228 	{ .compatible = "arm,scmi-smc-param", .data = &scmi_smc_desc},
3229 	{ .compatible = "qcom,scmi-smc", .data = &scmi_smc_desc},
3230 #endif
3231 #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
3232 	{ .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
3233 #endif
3234 	{ /* Sentinel */ },
3235 };
3236 
3237 MODULE_DEVICE_TABLE(of, scmi_of_match);
3238 
3239 static struct platform_driver scmi_driver = {
3240 	.driver = {
3241 		   .name = "arm-scmi",
3242 		   .suppress_bind_attrs = true,
3243 		   .of_match_table = scmi_of_match,
3244 		   .dev_groups = versions_groups,
3245 		   },
3246 	.probe = scmi_probe,
3247 	.remove_new = scmi_remove,
3248 };
3249 
3250 /**
3251  * __scmi_transports_setup  - Common helper to call transport-specific
3252  * .init/.exit code if provided.
3253  *
3254  * @init: A flag to distinguish between init and exit.
3255  *
3256  * Note that, if provided, we invoke .init/.exit functions for all the
3257  * transports currently compiled in.
3258  *
3259  * Return: 0 on Success.
3260  */
__scmi_transports_setup(bool init)3261 static inline int __scmi_transports_setup(bool init)
3262 {
3263 	int ret = 0;
3264 	const struct of_device_id *trans;
3265 
3266 	for (trans = scmi_of_match; trans->data; trans++) {
3267 		const struct scmi_desc *tdesc = trans->data;
3268 
3269 		if ((init && !tdesc->transport_init) ||
3270 		    (!init && !tdesc->transport_exit))
3271 			continue;
3272 
3273 		if (init)
3274 			ret = tdesc->transport_init();
3275 		else
3276 			tdesc->transport_exit();
3277 
3278 		if (ret) {
3279 			pr_err("SCMI transport %s FAILED initialization!\n",
3280 			       trans->compatible);
3281 			break;
3282 		}
3283 	}
3284 
3285 	return ret;
3286 }
3287 
scmi_transports_init(void)3288 static int __init scmi_transports_init(void)
3289 {
3290 	return __scmi_transports_setup(true);
3291 }
3292 
scmi_transports_exit(void)3293 static void __exit scmi_transports_exit(void)
3294 {
3295 	__scmi_transports_setup(false);
3296 }
3297 
scmi_debugfs_init(void)3298 static struct dentry *scmi_debugfs_init(void)
3299 {
3300 	struct dentry *d;
3301 
3302 	d = debugfs_create_dir("scmi", NULL);
3303 	if (IS_ERR(d)) {
3304 		pr_err("Could NOT create SCMI top dentry.\n");
3305 		return NULL;
3306 	}
3307 
3308 	return d;
3309 }
3310 
scmi_driver_init(void)3311 static int __init scmi_driver_init(void)
3312 {
3313 	int ret;
3314 
3315 	/* Bail out if no SCMI transport was configured */
3316 	if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
3317 		return -EINVAL;
3318 
3319 	/* Initialize any compiled-in transport which provided an init/exit */
3320 	ret = scmi_transports_init();
3321 	if (ret)
3322 		return ret;
3323 
3324 	if (IS_ENABLED(CONFIG_ARM_SCMI_NEED_DEBUGFS))
3325 		scmi_top_dentry = scmi_debugfs_init();
3326 
3327 	scmi_base_register();
3328 
3329 	scmi_clock_register();
3330 	scmi_perf_register();
3331 	scmi_power_register();
3332 	scmi_reset_register();
3333 	scmi_sensors_register();
3334 	scmi_voltage_register();
3335 	scmi_system_register();
3336 	scmi_powercap_register();
3337 	scmi_pinctrl_register();
3338 
3339 	return platform_driver_register(&scmi_driver);
3340 }
3341 module_init(scmi_driver_init);
3342 
scmi_driver_exit(void)3343 static void __exit scmi_driver_exit(void)
3344 {
3345 	scmi_base_unregister();
3346 
3347 	scmi_clock_unregister();
3348 	scmi_perf_unregister();
3349 	scmi_power_unregister();
3350 	scmi_reset_unregister();
3351 	scmi_sensors_unregister();
3352 	scmi_voltage_unregister();
3353 	scmi_system_unregister();
3354 	scmi_powercap_unregister();
3355 	scmi_pinctrl_unregister();
3356 
3357 	scmi_transports_exit();
3358 
3359 	platform_driver_unregister(&scmi_driver);
3360 
3361 	debugfs_remove_recursive(scmi_top_dentry);
3362 }
3363 module_exit(scmi_driver_exit);
3364 
3365 MODULE_ALIAS("platform:arm-scmi");
3366 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
3367 MODULE_DESCRIPTION("ARM SCMI protocol driver");
3368 MODULE_LICENSE("GPL v2");
3369