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