Merge branches 'pm-cpuidle', 'pm-core' and 'pm-sleep'
[linux-block.git] / drivers / firmware / arm_scmi / driver.c
CommitLineData
aa4f886f
SH
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 *
48dc16e2 14 * Copyright (C) 2018-2021 ARM Ltd.
aa4f886f
SH
15 */
16
17#include <linux/bitmap.h>
23934efe 18#include <linux/device.h>
aa4f886f 19#include <linux/export.h>
48dc16e2 20#include <linux/idr.h>
aa4f886f 21#include <linux/io.h>
6f9ea4da 22#include <linux/io-64-nonatomic-hi-lo.h>
aa4f886f 23#include <linux/kernel.h>
d4c3751a 24#include <linux/ktime.h>
9ca5a183 25#include <linux/hashtable.h>
d4f9dddd 26#include <linux/list.h>
aa4f886f
SH
27#include <linux/module.h>
28#include <linux/of_address.h>
29#include <linux/of_device.h>
d4c3751a 30#include <linux/processor.h>
48dc16e2 31#include <linux/refcount.h>
aa4f886f
SH
32#include <linux/slab.h>
33
34#include "common.h"
6b8a6913 35#include "notify.h"
aa4f886f 36
729d3530
LL
37#define CREATE_TRACE_POINTS
38#include <trace/events/scmi.h>
39
aa4f886f
SH
40enum scmi_error_codes {
41 SCMI_SUCCESS = 0, /* Success */
42 SCMI_ERR_SUPPORT = -1, /* Not supported */
43 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
44 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
45 SCMI_ERR_ENTRY = -4, /* Not found */
46 SCMI_ERR_RANGE = -5, /* Value out of range */
47 SCMI_ERR_BUSY = -6, /* Device busy */
48 SCMI_ERR_COMMS = -7, /* Communication Error */
49 SCMI_ERR_GENERIC = -8, /* Generic Error */
50 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
51 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
aa4f886f
SH
52};
53
1baf47c2 54/* List of all SCMI devices active in system */
aa4f886f
SH
55static LIST_HEAD(scmi_list);
56/* Protection for the entire list */
57static DEFINE_MUTEX(scmi_list_mutex);
729d3530
LL
58/* Track the unique id for the transfers for debug & profiling purpose */
59static atomic_t transfer_last_id;
aa4f886f 60
d4f9dddd
CM
61static DEFINE_IDR(scmi_requested_devices);
62static DEFINE_MUTEX(scmi_requested_devices_mtx);
63
a0db3962
CM
64/* Track globally the creation of SCMI SystemPower related devices */
65static bool scmi_syspower_registered;
66/* Protect access to scmi_syspower_registered */
67static DEFINE_MUTEX(scmi_syspower_mtx);
68
d4f9dddd
CM
69struct scmi_requested_dev {
70 const struct scmi_device_id *id_table;
71 struct list_head node;
72};
73
aa4f886f
SH
74/**
75 * struct scmi_xfers_info - Structure to manage transfer information
76 *
aa4f886f
SH
77 * @xfer_alloc_table: Bitmap table for allocated messages.
78 * Index of this bitmap table is also used for message
79 * sequence identifier.
80 * @xfer_lock: Protection for message allocation
c92c3e38 81 * @max_msg: Maximum number of messages that can be pending
9ca5a183
CM
82 * @free_xfers: A free list for available to use xfers. It is initialized with
83 * a number of xfers equal to the maximum allowed in-flight
84 * messages.
85 * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
86 * currently in-flight messages.
aa4f886f
SH
87 */
88struct scmi_xfers_info {
aa4f886f 89 unsigned long *xfer_alloc_table;
aa4f886f 90 spinlock_t xfer_lock;
c92c3e38 91 int max_msg;
9ca5a183
CM
92 struct hlist_head free_xfers;
93 DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
aa4f886f
SH
94};
95
48dc16e2
CM
96/**
97 * struct scmi_protocol_instance - Describe an initialized protocol instance.
d7b6cc56 98 * @handle: Reference to the SCMI handle associated to this protocol instance.
48dc16e2
CM
99 * @proto: A reference to the protocol descriptor.
100 * @gid: A reference for per-protocol devres management.
101 * @users: A refcount to track effective users of this protocol.
d7b6cc56
CM
102 * @priv: Reference for optional protocol private data.
103 * @ph: An embedded protocol handle that will be passed down to protocol
104 * initialization code to identify this instance.
48dc16e2
CM
105 *
106 * Each protocol is initialized independently once for each SCMI platform in
107 * which is defined by DT and implemented by the SCMI server fw.
108 */
109struct scmi_protocol_instance {
d7b6cc56 110 const struct scmi_handle *handle;
48dc16e2
CM
111 const struct scmi_protocol *proto;
112 void *gid;
113 refcount_t users;
d7b6cc56
CM
114 void *priv;
115 struct scmi_protocol_handle ph;
48dc16e2
CM
116};
117
d7b6cc56
CM
118#define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
119
aa4f886f 120/**
1baf47c2 121 * struct scmi_info - Structure representing a SCMI instance
aa4f886f
SH
122 *
123 * @dev: Device pointer
124 * @desc: SoC description for this instance
b6f20ff8
SH
125 * @version: SCMI revision information containing protocol version,
126 * implementation version and (sub-)vendor identification.
71af05a7 127 * @handle: Instance of SCMI handle to send to clients
38c927fb 128 * @tx_minfo: Universal Transmit Message management info
4ebd8f6d 129 * @rx_minfo: Universal Receive Message management info
3748daf7 130 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
46cc7c28 131 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
48dc16e2
CM
132 * @protocols: IDR for protocols' instance descriptors initialized for
133 * this SCMI instance: populated on protocol's first attempted
134 * usage.
135 * @protocols_mtx: A mutex to protect protocols instances initialization.
1baf47c2 136 * @protocols_imp: List of protocols implemented, currently maximum of
776b6c8a
CM
137 * scmi_revision_info.num_protocols elements allocated by the
138 * base protocol
d4f9dddd
CM
139 * @active_protocols: IDR storing device_nodes for protocols actually defined
140 * in the DT and confirmed as implemented by fw.
05976c5f
CM
141 * @atomic_threshold: Optional system wide DT-configured threshold, expressed
142 * in microseconds, for atomic operations.
143 * Only SCMI synchronous commands reported by the platform
144 * to have an execution latency lesser-equal to the threshold
145 * should be considered for atomic mode operation: such
146 * decision is finally left up to the SCMI drivers.
a02d7c93 147 * @notify_priv: Pointer to private data structure specific to notifications.
1baf47c2 148 * @node: List head
aa4f886f
SH
149 * @users: Number of users of this instance
150 */
151struct scmi_info {
152 struct device *dev;
153 const struct scmi_desc *desc;
b6f20ff8 154 struct scmi_revision_info version;
aa4f886f 155 struct scmi_handle handle;
38c927fb 156 struct scmi_xfers_info tx_minfo;
4ebd8f6d 157 struct scmi_xfers_info rx_minfo;
907b6d14 158 struct idr tx_idr;
46cc7c28 159 struct idr rx_idr;
48dc16e2
CM
160 struct idr protocols;
161 /* Ensure mutual exclusive access to protocols instance array */
162 struct mutex protocols_mtx;
b6f20ff8 163 u8 *protocols_imp;
d4f9dddd 164 struct idr active_protocols;
05976c5f 165 unsigned int atomic_threshold;
a02d7c93 166 void *notify_priv;
aa4f886f
SH
167 struct list_head node;
168 int users;
169};
170
aa4f886f
SH
171#define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
172
aa4f886f
SH
173static const int scmi_linux_errmap[] = {
174 /* better than switch case as long as return value is continuous */
175 0, /* SCMI_SUCCESS */
176 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
177 -EINVAL, /* SCMI_ERR_PARAM */
178 -EACCES, /* SCMI_ERR_ACCESS */
179 -ENOENT, /* SCMI_ERR_ENTRY */
180 -ERANGE, /* SCMI_ERR_RANGE */
181 -EBUSY, /* SCMI_ERR_BUSY */
182 -ECOMM, /* SCMI_ERR_COMMS */
183 -EIO, /* SCMI_ERR_GENERIC */
184 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
185 -EPROTO, /* SCMI_ERR_PROTOCOL */
186};
187
188static inline int scmi_to_linux_errno(int errno)
189{
7a691f16
SH
190 int err_idx = -errno;
191
192 if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
193 return scmi_linux_errmap[err_idx];
aa4f886f
SH
194 return -EIO;
195}
196
a02d7c93
CM
197void scmi_notification_instance_data_set(const struct scmi_handle *handle,
198 void *priv)
199{
200 struct scmi_info *info = handle_to_scmi_info(handle);
201
202 info->notify_priv = priv;
203 /* Ensure updated protocol private date are visible */
204 smp_wmb();
205}
206
207void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
208{
209 struct scmi_info *info = handle_to_scmi_info(handle);
210
211 /* Ensure protocols_private_data has been updated */
212 smp_rmb();
213 return info->notify_priv;
214}
215
9ca5a183
CM
216/**
217 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
218 *
219 * @minfo: Pointer to Tx/Rx Message management info based on channel type
220 * @xfer: The xfer to act upon
221 *
222 * Pick the next unused monotonically increasing token and set it into
223 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
224 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
225 * of incorrect association of a late and expired xfer with a live in-flight
226 * transaction, both happening to re-use the same token identifier.
227 *
228 * Since platform is NOT required to answer our request in-order we should
229 * account for a few rare but possible scenarios:
230 *
231 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
232 * using find_next_zero_bit() starting from candidate next_token bit
233 *
234 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
235 * are plenty of free tokens at start, so try a second pass using
236 * find_next_zero_bit() and starting from 0.
237 *
238 * X = used in-flight
239 *
240 * Normal
241 * ------
242 *
243 * |- xfer_id picked
244 * -----------+----------------------------------------------------------
245 * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
246 * ----------------------------------------------------------------------
247 * ^
248 * |- next_token
249 *
250 * Out-of-order pending at start
251 * -----------------------------
252 *
253 * |- xfer_id picked, last_token fixed
254 * -----+----------------------------------------------------------------
255 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
256 * ----------------------------------------------------------------------
257 * ^
258 * |- next_token
259 *
260 *
261 * Out-of-order pending at end
262 * ---------------------------
263 *
264 * |- xfer_id picked, last_token fixed
265 * -----+----------------------------------------------------------------
266 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
267 * ----------------------------------------------------------------------
268 * ^
269 * |- next_token
270 *
271 * Context: Assumes to be called with @xfer_lock already acquired.
272 *
273 * Return: 0 on Success or error
274 */
275static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
276 struct scmi_xfer *xfer)
277{
278 unsigned long xfer_id, next_token;
279
280 /*
281 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
282 * using the pre-allocated transfer_id as a base.
283 * Note that the global transfer_id is shared across all message types
284 * so there could be holes in the allocated set of monotonic sequence
285 * numbers, but that is going to limit the effectiveness of the
286 * mitigation only in very rare limit conditions.
287 */
288 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
289
290 /* Pick the next available xfer_id >= next_token */
291 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
292 MSG_TOKEN_MAX, next_token);
293 if (xfer_id == MSG_TOKEN_MAX) {
294 /*
295 * After heavily out-of-order responses, there are no free
296 * tokens ahead, but only at start of xfer_alloc_table so
297 * try again from the beginning.
298 */
299 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
300 MSG_TOKEN_MAX, 0);
301 /*
302 * Something is wrong if we got here since there can be a
303 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
304 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
305 */
306 if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
307 return -ENOMEM;
308 }
309
310 /* Update +/- last_token accordingly if we skipped some hole */
311 if (xfer_id != next_token)
312 atomic_add((int)(xfer_id - next_token), &transfer_last_id);
313
314 /* Set in-flight */
315 set_bit(xfer_id, minfo->xfer_alloc_table);
316 xfer->hdr.seq = (u16)xfer_id;
317
318 return 0;
319}
320
321/**
322 * scmi_xfer_token_clear - Release the token
323 *
324 * @minfo: Pointer to Tx/Rx Message management info based on channel type
325 * @xfer: The xfer to act upon
326 */
327static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
328 struct scmi_xfer *xfer)
329{
330 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
331}
332
aa4f886f 333/**
1baf47c2 334 * scmi_xfer_get() - Allocate one message
aa4f886f 335 *
1baf47c2 336 * @handle: Pointer to SCMI entity handle
38c927fb 337 * @minfo: Pointer to Tx/Rx Message management info based on channel type
9ca5a183
CM
338 * @set_pending: If true a monotonic token is picked and the xfer is added to
339 * the pending hash table.
aa4f886f 340 *
5b65af8f 341 * Helper function which is used by various message functions that are
aa4f886f
SH
342 * exposed to clients of this driver for allocating a message traffic event.
343 *
9ca5a183
CM
344 * Picks an xfer from the free list @free_xfers (if any available) and, if
345 * required, sets a monotonically increasing token and stores the inflight xfer
346 * into the @pending_xfers hashtable for later retrieval.
347 *
348 * The successfully initialized xfer is refcounted.
349 *
350 * Context: Holds @xfer_lock while manipulating @xfer_alloc_table and
351 * @free_xfers.
aa4f886f
SH
352 *
353 * Return: 0 if all went fine, else corresponding error.
354 */
38c927fb 355static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
9ca5a183
CM
356 struct scmi_xfers_info *minfo,
357 bool set_pending)
aa4f886f 358{
9ca5a183
CM
359 int ret;
360 unsigned long flags;
aa4f886f 361 struct scmi_xfer *xfer;
aa4f886f 362
aa4f886f 363 spin_lock_irqsave(&minfo->xfer_lock, flags);
9ca5a183 364 if (hlist_empty(&minfo->free_xfers)) {
aa4f886f
SH
365 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
366 return ERR_PTR(-ENOMEM);
367 }
aa4f886f 368
9ca5a183
CM
369 /* grab an xfer from the free_list */
370 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
371 hlist_del_init(&xfer->node);
aa4f886f 372
9ca5a183
CM
373 /*
374 * Allocate transfer_id early so that can be used also as base for
375 * monotonic sequence number generation if needed.
376 */
729d3530 377 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
aa4f886f 378
9ca5a183
CM
379 if (set_pending) {
380 /* Pick and set monotonic token */
381 ret = scmi_xfer_token_set(minfo, xfer);
382 if (!ret) {
383 hash_add(minfo->pending_xfers, &xfer->node,
384 xfer->hdr.seq);
385 xfer->pending = true;
386 } else {
387 dev_err(handle->dev,
388 "Failed to get monotonic token %d\n", ret);
389 hlist_add_head(&xfer->node, &minfo->free_xfers);
390 xfer = ERR_PTR(ret);
391 }
392 }
ed7c04c1
CM
393
394 if (!IS_ERR(xfer)) {
395 refcount_set(&xfer->users, 1);
396 atomic_set(&xfer->busy, SCMI_XFER_FREE);
397 }
9ca5a183
CM
398 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
399
aa4f886f
SH
400 return xfer;
401}
402
403/**
38c927fb 404 * __scmi_xfer_put() - Release a message
aa4f886f 405 *
38c927fb 406 * @minfo: Pointer to Tx/Rx Message management info based on channel type
1baf47c2 407 * @xfer: message that was reserved by scmi_xfer_get
aa4f886f 408 *
9ca5a183
CM
409 * After refcount check, possibly release an xfer, clearing the token slot,
410 * removing xfer from @pending_xfers and putting it back into free_xfers.
411 *
aa4f886f
SH
412 * This holds a spinlock to maintain integrity of internal data structures.
413 */
38c927fb
SH
414static void
415__scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
aa4f886f
SH
416{
417 unsigned long flags;
aa4f886f 418
aa4f886f 419 spin_lock_irqsave(&minfo->xfer_lock, flags);
ed7c04c1
CM
420 if (refcount_dec_and_test(&xfer->users)) {
421 if (xfer->pending) {
422 scmi_xfer_token_clear(minfo, xfer);
423 hash_del(&xfer->node);
424 xfer->pending = false;
425 }
426 hlist_add_head(&xfer->node, &minfo->free_xfers);
9ca5a183 427 }
aa4f886f
SH
428 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
429}
430
9ca5a183
CM
431/**
432 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
433 *
434 * @minfo: Pointer to Tx/Rx Message management info based on channel type
435 * @xfer_id: Token ID to lookup in @pending_xfers
436 *
437 * Refcounting is untouched.
438 *
439 * Context: Assumes to be called with @xfer_lock already acquired.
440 *
441 * Return: A valid xfer on Success or error otherwise
442 */
443static struct scmi_xfer *
444scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
445{
446 struct scmi_xfer *xfer = NULL;
447
448 if (test_bit(xfer_id, minfo->xfer_alloc_table))
449 xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
450
451 return xfer ?: ERR_PTR(-EINVAL);
452}
453
ed7c04c1
CM
454/**
455 * scmi_msg_response_validate - Validate message type against state of related
456 * xfer
457 *
458 * @cinfo: A reference to the channel descriptor.
459 * @msg_type: Message type to check
460 * @xfer: A reference to the xfer to validate against @msg_type
461 *
462 * This function checks if @msg_type is congruent with the current state of
463 * a pending @xfer; if an asynchronous delayed response is received before the
464 * related synchronous response (Out-of-Order Delayed Response) the missing
465 * synchronous response is assumed to be OK and completed, carrying on with the
466 * Delayed Response: this is done to address the case in which the underlying
467 * SCMI transport can deliver such out-of-order responses.
468 *
469 * Context: Assumes to be called with xfer->lock already acquired.
470 *
471 * Return: 0 on Success, error otherwise
472 */
473static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
474 u8 msg_type,
475 struct scmi_xfer *xfer)
476{
477 /*
478 * Even if a response was indeed expected on this slot at this point,
479 * a buggy platform could wrongly reply feeding us an unexpected
480 * delayed response we're not prepared to handle: bail-out safely
481 * blaming firmware.
482 */
483 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
484 dev_err(cinfo->dev,
485 "Delayed Response for %d not expected! Buggy F/W ?\n",
486 xfer->hdr.seq);
487 return -EINVAL;
488 }
489
490 switch (xfer->state) {
491 case SCMI_XFER_SENT_OK:
492 if (msg_type == MSG_TYPE_DELAYED_RESP) {
493 /*
494 * Delayed Response expected but delivered earlier.
495 * Assume message RESPONSE was OK and skip state.
496 */
497 xfer->hdr.status = SCMI_SUCCESS;
498 xfer->state = SCMI_XFER_RESP_OK;
499 complete(&xfer->done);
500 dev_warn(cinfo->dev,
501 "Received valid OoO Delayed Response for %d\n",
502 xfer->hdr.seq);
503 }
504 break;
505 case SCMI_XFER_RESP_OK:
506 if (msg_type != MSG_TYPE_DELAYED_RESP)
507 return -EINVAL;
508 break;
509 case SCMI_XFER_DRESP_OK:
510 /* No further message expected once in SCMI_XFER_DRESP_OK */
511 return -EINVAL;
512 }
513
514 return 0;
515}
516
517/**
518 * scmi_xfer_state_update - Update xfer state
519 *
520 * @xfer: A reference to the xfer to update
521 * @msg_type: Type of message being processed.
522 *
523 * Note that this message is assumed to have been already successfully validated
524 * by @scmi_msg_response_validate(), so here we just update the state.
525 *
526 * Context: Assumes to be called on an xfer exclusively acquired using the
527 * busy flag.
528 */
529static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
530{
531 xfer->hdr.type = msg_type;
532
533 /* Unknown command types were already discarded earlier */
534 if (xfer->hdr.type == MSG_TYPE_COMMAND)
535 xfer->state = SCMI_XFER_RESP_OK;
536 else
537 xfer->state = SCMI_XFER_DRESP_OK;
538}
539
540static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
541{
542 int ret;
543
544 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
545
546 return ret == SCMI_XFER_FREE;
547}
548
549/**
550 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
551 *
552 * @cinfo: A reference to the channel descriptor.
553 * @msg_hdr: A message header to use as lookup key
554 *
555 * When a valid xfer is found for the sequence number embedded in the provided
556 * msg_hdr, reference counting is properly updated and exclusive access to this
557 * xfer is granted till released with @scmi_xfer_command_release.
558 *
559 * Return: A valid @xfer on Success or error otherwise.
560 */
561static inline struct scmi_xfer *
562scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
563{
564 int ret;
565 unsigned long flags;
566 struct scmi_xfer *xfer;
567 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
568 struct scmi_xfers_info *minfo = &info->tx_minfo;
569 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
570 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
571
572 /* Are we even expecting this? */
573 spin_lock_irqsave(&minfo->xfer_lock, flags);
574 xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
575 if (IS_ERR(xfer)) {
576 dev_err(cinfo->dev,
577 "Message for %d type %d is not expected!\n",
578 xfer_id, msg_type);
579 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
580 return xfer;
581 }
582 refcount_inc(&xfer->users);
583 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
584
585 spin_lock_irqsave(&xfer->lock, flags);
586 ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
587 /*
588 * If a pending xfer was found which was also in a congruent state with
589 * the received message, acquire exclusive access to it setting the busy
590 * flag.
591 * Spins only on the rare limit condition of concurrent reception of
592 * RESP and DRESP for the same xfer.
593 */
594 if (!ret) {
595 spin_until_cond(scmi_xfer_acquired(xfer));
596 scmi_xfer_state_update(xfer, msg_type);
597 }
598 spin_unlock_irqrestore(&xfer->lock, flags);
599
600 if (ret) {
601 dev_err(cinfo->dev,
602 "Invalid message type:%d for %d - HDR:0x%X state:%d\n",
603 msg_type, xfer_id, msg_hdr, xfer->state);
604 /* On error the refcount incremented above has to be dropped */
605 __scmi_xfer_put(minfo, xfer);
606 xfer = ERR_PTR(-EINVAL);
607 }
608
609 return xfer;
610}
611
612static inline void scmi_xfer_command_release(struct scmi_info *info,
613 struct scmi_xfer *xfer)
614{
615 atomic_set(&xfer->busy, SCMI_XFER_FREE);
616 __scmi_xfer_put(&info->tx_minfo, xfer);
617}
618
e9b21c96
CM
619static inline void scmi_clear_channel(struct scmi_info *info,
620 struct scmi_chan_info *cinfo)
621{
622 if (info->desc->ops->clear_channel)
623 info->desc->ops->clear_channel(cinfo);
624}
625
a690b7e6
CM
626static inline bool is_polling_required(struct scmi_chan_info *cinfo,
627 struct scmi_info *info)
628{
629 return cinfo->no_completion_irq || info->desc->force_polling;
630}
631
632static inline bool is_transport_polling_capable(struct scmi_info *info)
633{
31d2f803
CM
634 return info->desc->ops->poll_done ||
635 info->desc->sync_cmds_completed_on_ret;
a690b7e6
CM
636}
637
638static inline bool is_polling_enabled(struct scmi_chan_info *cinfo,
639 struct scmi_info *info)
640{
641 return is_polling_required(cinfo, info) &&
642 is_transport_polling_capable(info);
643}
644
13fba878
CM
645static void scmi_handle_notification(struct scmi_chan_info *cinfo,
646 u32 msg_hdr, void *priv)
2747a967 647{
5c8a47a5 648 struct scmi_xfer *xfer;
4d09852b
SH
649 struct device *dev = cinfo->dev;
650 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
651 struct scmi_xfers_info *minfo = &info->rx_minfo;
72a5eb9d 652 ktime_t ts;
4d09852b 653
72a5eb9d 654 ts = ktime_get_boottime();
9ca5a183 655 xfer = scmi_xfer_get(cinfo->handle, minfo, false);
4d09852b
SH
656 if (IS_ERR(xfer)) {
657 dev_err(dev, "failed to get free message slot (%ld)\n",
658 PTR_ERR(xfer));
e9b21c96 659 scmi_clear_channel(info, cinfo);
4d09852b
SH
660 return;
661 }
662
663 unpack_scmi_header(msg_hdr, &xfer->hdr);
13fba878 664 if (priv)
5a3b7185
CM
665 /* Ensure order between xfer->priv store and following ops */
666 smp_store_mb(xfer->priv, priv);
4d09852b
SH
667 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
668 xfer);
b60e0886
CM
669
670 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id, "NOTI",
671 xfer->hdr.seq, xfer->hdr.status,
672 xfer->rx.buf, xfer->rx.len);
673
6b8a6913
CM
674 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
675 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
4d09852b
SH
676
677 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
678 xfer->hdr.protocol_id, xfer->hdr.seq,
679 MSG_TYPE_NOTIFICATION);
58ecdf03 680
4d09852b
SH
681 __scmi_xfer_put(minfo, xfer);
682
e9b21c96 683 scmi_clear_channel(info, cinfo);
4d09852b
SH
684}
685
13fba878
CM
686static void scmi_handle_response(struct scmi_chan_info *cinfo,
687 u32 msg_hdr, void *priv)
4d09852b
SH
688{
689 struct scmi_xfer *xfer;
4d09852b 690 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
2747a967 691
ed7c04c1 692 xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
9ca5a183 693 if (IS_ERR(xfer)) {
98f0d68f
CM
694 if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
695 scmi_clear_channel(info, cinfo);
c5bceb98
CM
696 return;
697 }
2747a967 698
0cb7af47 699 /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
ed7c04c1 700 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
0cb7af47
CM
701 xfer->rx.len = info->desc->max_msg_size;
702
13fba878 703 if (priv)
5a3b7185
CM
704 /* Ensure order between xfer->priv store and following ops */
705 smp_store_mb(xfer->priv, priv);
5c8a47a5 706 info->desc->ops->fetch_response(cinfo, xfer);
58ecdf03 707
b60e0886
CM
708 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id,
709 xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
710 "DLYD" : "RESP",
711 xfer->hdr.seq, xfer->hdr.status,
712 xfer->rx.buf, xfer->rx.len);
713
729d3530
LL
714 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
715 xfer->hdr.protocol_id, xfer->hdr.seq,
ed7c04c1 716 xfer->hdr.type);
729d3530 717
ed7c04c1 718 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
e9b21c96 719 scmi_clear_channel(info, cinfo);
58ecdf03 720 complete(xfer->async_done);
d04fb2b2 721 } else {
58ecdf03 722 complete(&xfer->done);
d04fb2b2 723 }
ed7c04c1
CM
724
725 scmi_xfer_command_release(info, xfer);
2747a967
SH
726}
727
4d09852b
SH
728/**
729 * scmi_rx_callback() - callback for receiving messages
730 *
731 * @cinfo: SCMI channel info
732 * @msg_hdr: Message header
13fba878 733 * @priv: Transport specific private data.
4d09852b
SH
734 *
735 * Processes one received message to appropriate transfer information and
736 * signals completion of the transfer.
737 *
738 * NOTE: This function will be invoked in IRQ context, hence should be
739 * as optimal as possible.
740 */
13fba878 741void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv)
4d09852b 742{
4d09852b
SH
743 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
744
745 switch (msg_type) {
746 case MSG_TYPE_NOTIFICATION:
13fba878 747 scmi_handle_notification(cinfo, msg_hdr, priv);
4d09852b
SH
748 break;
749 case MSG_TYPE_COMMAND:
750 case MSG_TYPE_DELAYED_RESP:
13fba878 751 scmi_handle_response(cinfo, msg_hdr, priv);
4d09852b
SH
752 break;
753 default:
754 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
755 break;
756 }
757}
758
38c927fb 759/**
a4a20b09 760 * xfer_put() - Release a transmit message
38c927fb 761 *
a4a20b09 762 * @ph: Pointer to SCMI protocol handle
ed7c04c1 763 * @xfer: message that was reserved by xfer_get_init
38c927fb 764 */
a4a20b09
CM
765static void xfer_put(const struct scmi_protocol_handle *ph,
766 struct scmi_xfer *xfer)
38c927fb 767{
a4a20b09
CM
768 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
769 struct scmi_info *info = handle_to_scmi_info(pi->handle);
38c927fb
SH
770
771 __scmi_xfer_put(&info->tx_minfo, xfer);
772}
773
5c8a47a5 774static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
d4c3751a
SH
775 struct scmi_xfer *xfer, ktime_t stop)
776{
5c8a47a5 777 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
d4c3751a 778
ed7c04c1
CM
779 /*
780 * Poll also on xfer->done so that polling can be forcibly terminated
781 * in case of out-of-order receptions of delayed responses
782 */
5c8a47a5 783 return info->desc->ops->poll_done(cinfo, xfer) ||
ed7c04c1 784 try_wait_for_completion(&xfer->done) ||
5c8a47a5 785 ktime_after(ktime_get(), stop);
d4c3751a
SH
786}
787
5a731aeb
CM
788/**
789 * scmi_wait_for_message_response - An helper to group all the possible ways of
790 * waiting for a synchronous message response.
791 *
792 * @cinfo: SCMI channel info
793 * @xfer: Reference to the transfer being waited for.
794 *
795 * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
796 * configuration flags like xfer->hdr.poll_completion.
797 *
798 * Return: 0 on Success, error otherwise.
799 */
800static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
801 struct scmi_xfer *xfer)
802{
803 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
804 struct device *dev = info->dev;
805 int ret = 0, timeout_ms = info->desc->max_rx_timeout_ms;
806
f872af09
CM
807 trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
808 xfer->hdr.protocol_id, xfer->hdr.seq,
809 timeout_ms,
810 xfer->hdr.poll_completion);
811
5a731aeb 812 if (xfer->hdr.poll_completion) {
31d2f803
CM
813 /*
814 * Real polling is needed only if transport has NOT declared
815 * itself to support synchronous commands replies.
816 */
817 if (!info->desc->sync_cmds_completed_on_ret) {
818 /*
819 * Poll on xfer using transport provided .poll_done();
820 * assumes no completion interrupt was available.
821 */
822 ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
823
824 spin_until_cond(scmi_xfer_done_no_timeout(cinfo,
825 xfer, stop));
826 if (ktime_after(ktime_get(), stop)) {
827 dev_err(dev,
828 "timed out in resp(caller: %pS) - polling\n",
829 (void *)_RET_IP_);
830 ret = -ETIMEDOUT;
831 }
832 }
5a731aeb 833
31d2f803 834 if (!ret) {
5a731aeb
CM
835 unsigned long flags;
836
837 /*
838 * Do not fetch_response if an out-of-order delayed
839 * response is being processed.
840 */
841 spin_lock_irqsave(&xfer->lock, flags);
842 if (xfer->state == SCMI_XFER_SENT_OK) {
843 info->desc->ops->fetch_response(cinfo, xfer);
844 xfer->state = SCMI_XFER_RESP_OK;
845 }
846 spin_unlock_irqrestore(&xfer->lock, flags);
b60e0886
CM
847
848 /* Trace polled replies. */
849 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id,
850 "RESP",
851 xfer->hdr.seq, xfer->hdr.status,
852 xfer->rx.buf, xfer->rx.len);
5a731aeb
CM
853 }
854 } else {
855 /* And we wait for the response. */
856 if (!wait_for_completion_timeout(&xfer->done,
857 msecs_to_jiffies(timeout_ms))) {
858 dev_err(dev, "timed out in resp(caller: %pS)\n",
859 (void *)_RET_IP_);
860 ret = -ETIMEDOUT;
861 }
862 }
863
864 return ret;
865}
866
aa4f886f 867/**
a4a20b09 868 * do_xfer() - Do one transfer
aa4f886f 869 *
a4a20b09 870 * @ph: Pointer to SCMI protocol handle
aa4f886f
SH
871 * @xfer: Transfer to initiate and wait for response
872 *
873 * Return: -ETIMEDOUT in case of no response, if transmit error,
1baf47c2
SH
874 * return corresponding error, else if all goes well,
875 * return 0.
aa4f886f 876 */
a4a20b09
CM
877static int do_xfer(const struct scmi_protocol_handle *ph,
878 struct scmi_xfer *xfer)
aa4f886f
SH
879{
880 int ret;
a4a20b09
CM
881 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
882 struct scmi_info *info = handle_to_scmi_info(pi->handle);
aa4f886f 883 struct device *dev = info->dev;
907b6d14
SH
884 struct scmi_chan_info *cinfo;
885
a690b7e6 886 /* Check for polling request on custom command xfers at first */
31d2f803 887 if (xfer->hdr.poll_completion && !is_transport_polling_capable(info)) {
2930abcf
CM
888 dev_warn_once(dev,
889 "Polling mode is not supported by transport.\n");
890 return -EINVAL;
891 }
892
d211ddeb
CM
893 cinfo = idr_find(&info->tx_idr, pi->proto->id);
894 if (unlikely(!cinfo))
895 return -EINVAL;
896
a690b7e6
CM
897 /* True ONLY if also supported by transport. */
898 if (is_polling_enabled(cinfo, info))
899 xfer->hdr.poll_completion = true;
900
a4a20b09 901 /*
61832b35 902 * Initialise protocol id now from protocol handle to avoid it being
a4a20b09 903 * overridden by mistake (or malice) by the protocol code mangling with
61832b35 904 * the scmi_xfer structure prior to this.
a4a20b09
CM
905 */
906 xfer->hdr.protocol_id = pi->proto->id;
e30d91d4 907 reinit_completion(&xfer->done);
a4a20b09 908
729d3530
LL
909 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
910 xfer->hdr.protocol_id, xfer->hdr.seq,
911 xfer->hdr.poll_completion);
912
f6ca5059
CM
913 /* Clear any stale status */
914 xfer->hdr.status = SCMI_SUCCESS;
ed7c04c1
CM
915 xfer->state = SCMI_XFER_SENT_OK;
916 /*
917 * Even though spinlocking is not needed here since no race is possible
918 * on xfer->state due to the monotonically increasing tokens allocation,
919 * we must anyway ensure xfer->state initialization is not re-ordered
920 * after the .send_message() to be sure that on the RX path an early
921 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
922 */
923 smp_mb();
924
5c8a47a5 925 ret = info->desc->ops->send_message(cinfo, xfer);
aa4f886f 926 if (ret < 0) {
5c8a47a5 927 dev_dbg(dev, "Failed to send message %d\n", ret);
aa4f886f
SH
928 return ret;
929 }
930
b60e0886
CM
931 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id, "CMND",
932 xfer->hdr.seq, xfer->hdr.status,
933 xfer->tx.buf, xfer->tx.len);
934
5a731aeb 935 ret = scmi_wait_for_message_response(cinfo, xfer);
d4c3751a
SH
936 if (!ret && xfer->hdr.status)
937 ret = scmi_to_linux_errno(xfer->hdr.status);
938
5c8a47a5 939 if (info->desc->ops->mark_txdone)
94d0cd1d 940 info->desc->ops->mark_txdone(cinfo, ret, xfer);
aa4f886f 941
729d3530 942 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
bad0d73b 943 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
729d3530 944
aa4f886f
SH
945 return ret;
946}
947
a4a20b09
CM
948static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
949 struct scmi_xfer *xfer)
950{
951 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
952 struct scmi_info *info = handle_to_scmi_info(pi->handle);
953
954 xfer->rx.len = info->desc->max_msg_size;
955}
956
58ecdf03
SH
957#define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
958
959/**
a4a20b09 960 * do_xfer_with_response() - Do one transfer and wait until the delayed
58ecdf03
SH
961 * response is received
962 *
a4a20b09 963 * @ph: Pointer to SCMI protocol handle
58ecdf03
SH
964 * @xfer: Transfer to initiate and wait for response
965 *
69255e74
CM
966 * Using asynchronous commands in atomic/polling mode should be avoided since
967 * it could cause long busy-waiting here, so ignore polling for the delayed
968 * response and WARN if it was requested for this command transaction since
969 * upper layers should refrain from issuing such kind of requests.
970 *
971 * The only other option would have been to refrain from using any asynchronous
972 * command even if made available, when an atomic transport is detected, and
973 * instead forcibly use the synchronous version (thing that can be easily
974 * attained at the protocol layer), but this would also have led to longer
975 * stalls of the channel for synchronous commands and possibly timeouts.
976 * (in other words there is usually a good reason if a platform provides an
977 * asynchronous version of a command and we should prefer to use it...just not
978 * when using atomic/polling mode)
979 *
58ecdf03
SH
980 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
981 * return corresponding error, else if all goes well, return 0.
982 */
a4a20b09
CM
983static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
984 struct scmi_xfer *xfer)
58ecdf03
SH
985{
986 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
987 DECLARE_COMPLETION_ONSTACK(async_response);
988
989 xfer->async_done = &async_response;
990
69255e74
CM
991 /*
992 * Delayed responses should not be polled, so an async command should
993 * not have been used when requiring an atomic/poll context; WARN and
994 * perform instead a sleeping wait.
995 * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
996 */
997 WARN_ON_ONCE(xfer->hdr.poll_completion);
998
a4a20b09 999 ret = do_xfer(ph, xfer);
f1748b1e 1000 if (!ret) {
69255e74
CM
1001 if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
1002 dev_err(ph->dev,
1003 "timed out in delayed resp(caller: %pS)\n",
1004 (void *)_RET_IP_);
f1748b1e 1005 ret = -ETIMEDOUT;
69255e74 1006 } else if (xfer->hdr.status) {
f1748b1e 1007 ret = scmi_to_linux_errno(xfer->hdr.status);
69255e74 1008 }
f1748b1e 1009 }
58ecdf03
SH
1010
1011 xfer->async_done = NULL;
1012 return ret;
1013}
1014
aa4f886f 1015/**
a4a20b09 1016 * xfer_get_init() - Allocate and initialise one message for transmit
aa4f886f 1017 *
a4a20b09 1018 * @ph: Pointer to SCMI protocol handle
aa4f886f 1019 * @msg_id: Message identifier
aa4f886f
SH
1020 * @tx_size: transmit message size
1021 * @rx_size: receive message size
1022 * @p: pointer to the allocated and initialised message
1023 *
14e297b3 1024 * This function allocates the message using @scmi_xfer_get and
aa4f886f
SH
1025 * initialise the header.
1026 *
1027 * Return: 0 if all went fine with @p pointing to message, else
1028 * corresponding error.
1029 */
a4a20b09
CM
1030static int xfer_get_init(const struct scmi_protocol_handle *ph,
1031 u8 msg_id, size_t tx_size, size_t rx_size,
1032 struct scmi_xfer **p)
aa4f886f
SH
1033{
1034 int ret;
1035 struct scmi_xfer *xfer;
a4a20b09
CM
1036 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1037 struct scmi_info *info = handle_to_scmi_info(pi->handle);
38c927fb 1038 struct scmi_xfers_info *minfo = &info->tx_minfo;
aa4f886f
SH
1039 struct device *dev = info->dev;
1040
1041 /* Ensure we have sane transfer sizes */
1042 if (rx_size > info->desc->max_msg_size ||
1043 tx_size > info->desc->max_msg_size)
1044 return -ERANGE;
1045
9ca5a183 1046 xfer = scmi_xfer_get(pi->handle, minfo, true);
aa4f886f
SH
1047 if (IS_ERR(xfer)) {
1048 ret = PTR_ERR(xfer);
1049 dev_err(dev, "failed to get free message slot(%d)\n", ret);
1050 return ret;
1051 }
1052
1053 xfer->tx.len = tx_size;
1054 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
63b282f1 1055 xfer->hdr.type = MSG_TYPE_COMMAND;
aa4f886f 1056 xfer->hdr.id = msg_id;
aa4f886f
SH
1057 xfer->hdr.poll_completion = false;
1058
1059 *p = xfer;
1baf47c2 1060
aa4f886f
SH
1061 return 0;
1062}
1063
b6f20ff8 1064/**
a4a20b09 1065 * version_get() - command to get the revision of the SCMI entity
b6f20ff8 1066 *
a4a20b09 1067 * @ph: Pointer to SCMI protocol handle
1baf47c2 1068 * @version: Holds returned version of protocol.
b6f20ff8
SH
1069 *
1070 * Updates the SCMI information in the internal data structure.
1071 *
1072 * Return: 0 if all went fine, else return appropriate error.
1073 */
a4a20b09 1074static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
b6f20ff8
SH
1075{
1076 int ret;
1077 __le32 *rev_info;
1078 struct scmi_xfer *t;
1079
a4a20b09 1080 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
b6f20ff8
SH
1081 if (ret)
1082 return ret;
1083
a4a20b09 1084 ret = do_xfer(ph, t);
b6f20ff8
SH
1085 if (!ret) {
1086 rev_info = t->rx.buf;
1087 *version = le32_to_cpu(*rev_info);
1088 }
1089
a4a20b09 1090 xfer_put(ph, t);
b6f20ff8
SH
1091 return ret;
1092}
1093
d7b6cc56
CM
1094/**
1095 * scmi_set_protocol_priv - Set protocol specific data at init time
1096 *
1097 * @ph: A reference to the protocol handle.
1098 * @priv: The private data to set.
1099 *
1100 * Return: 0 on Success
1101 */
1102static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
1103 void *priv)
1104{
1105 struct scmi_protocol_instance *pi = ph_to_pi(ph);
1106
1107 pi->priv = priv;
1108
1109 return 0;
1110}
1111
1112/**
1113 * scmi_get_protocol_priv - Set protocol specific data at init time
1114 *
1115 * @ph: A reference to the protocol handle.
1116 *
1117 * Return: Protocol private data if any was set.
1118 */
1119static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
1120{
1121 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1122
1123 return pi->priv;
1124}
1125
a4a20b09
CM
1126static const struct scmi_xfer_ops xfer_ops = {
1127 .version_get = version_get,
1128 .xfer_get_init = xfer_get_init,
1129 .reset_rx_to_maxsz = reset_rx_to_maxsz,
1130 .do_xfer = do_xfer,
1131 .do_xfer_with_response = do_xfer_with_response,
1132 .xfer_put = xfer_put,
1133};
1134
5c873d12
CM
1135struct scmi_msg_resp_domain_name_get {
1136 __le32 flags;
1137 u8 name[SCMI_MAX_STR_SIZE];
1138};
1139
1140/**
1141 * scmi_common_extended_name_get - Common helper to get extended resources name
1142 * @ph: A protocol handle reference.
1143 * @cmd_id: The specific command ID to use.
1144 * @res_id: The specific resource ID to use.
1145 * @name: A pointer to the preallocated area where the retrieved name will be
1146 * stored as a NULL terminated string.
1147 * @len: The len in bytes of the @name char array.
1148 *
1149 * Return: 0 on Succcess
1150 */
1151static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
1152 u8 cmd_id, u32 res_id, char *name,
1153 size_t len)
1154{
1155 int ret;
1156 struct scmi_xfer *t;
1157 struct scmi_msg_resp_domain_name_get *resp;
1158
1159 ret = ph->xops->xfer_get_init(ph, cmd_id, sizeof(res_id),
1160 sizeof(*resp), &t);
1161 if (ret)
1162 goto out;
1163
1164 put_unaligned_le32(res_id, t->tx.buf);
1165 resp = t->rx.buf;
1166
1167 ret = ph->xops->do_xfer(ph, t);
1168 if (!ret)
1169 strscpy(name, resp->name, len);
1170
1171 ph->xops->xfer_put(ph, t);
1172out:
1173 if (ret)
1174 dev_warn(ph->dev,
1175 "Failed to get extended name - id:%u (ret:%d). Using %s\n",
1176 res_id, ret, name);
1177 return ret;
1178}
1179
36b6ea0f
CM
1180/**
1181 * struct scmi_iterator - Iterator descriptor
1182 * @msg: A reference to the message TX buffer; filled by @prepare_message with
1183 * a proper custom command payload for each multi-part command request.
1184 * @resp: A reference to the response RX buffer; used by @update_state and
1185 * @process_response to parse the multi-part replies.
1186 * @t: A reference to the underlying xfer initialized and used transparently by
1187 * the iterator internal routines.
1188 * @ph: A reference to the associated protocol handle to be used.
1189 * @ops: A reference to the custom provided iterator operations.
1190 * @state: The current iterator state; used and updated in turn by the iterators
1191 * internal routines and by the caller-provided @scmi_iterator_ops.
1192 * @priv: A reference to optional private data as provided by the caller and
1193 * passed back to the @@scmi_iterator_ops.
1194 */
1195struct scmi_iterator {
1196 void *msg;
1197 void *resp;
1198 struct scmi_xfer *t;
1199 const struct scmi_protocol_handle *ph;
1200 struct scmi_iterator_ops *ops;
1201 struct scmi_iterator_state state;
1202 void *priv;
1203};
1204
1205static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
1206 struct scmi_iterator_ops *ops,
1207 unsigned int max_resources, u8 msg_id,
1208 size_t tx_size, void *priv)
1209{
1210 int ret;
1211 struct scmi_iterator *i;
1212
1213 i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
1214 if (!i)
1215 return ERR_PTR(-ENOMEM);
1216
1217 i->ph = ph;
1218 i->ops = ops;
1219 i->priv = priv;
1220
1221 ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
1222 if (ret) {
1223 devm_kfree(ph->dev, i);
1224 return ERR_PTR(ret);
1225 }
1226
1227 i->state.max_resources = max_resources;
1228 i->msg = i->t->tx.buf;
1229 i->resp = i->t->rx.buf;
1230
1231 return i;
1232}
1233
1234static int scmi_iterator_run(void *iter)
1235{
1236 int ret = -EINVAL;
c7f8852d
CM
1237 struct scmi_iterator_ops *iops;
1238 const struct scmi_protocol_handle *ph;
1239 struct scmi_iterator_state *st;
36b6ea0f 1240 struct scmi_iterator *i = iter;
36b6ea0f 1241
c7f8852d 1242 if (!i || !i->ops || !i->ph)
36b6ea0f
CM
1243 return ret;
1244
c7f8852d
CM
1245 iops = i->ops;
1246 ph = i->ph;
1247 st = &i->state;
1248
36b6ea0f
CM
1249 do {
1250 iops->prepare_message(i->msg, st->desc_index, i->priv);
c7f8852d 1251 ret = ph->xops->do_xfer(ph, i->t);
36b6ea0f
CM
1252 if (ret)
1253 break;
1254
754f04ca 1255 st->rx_len = i->t->rx.len;
36b6ea0f
CM
1256 ret = iops->update_state(st, i->resp, i->priv);
1257 if (ret)
1258 break;
1259
1260 if (st->num_returned > st->max_resources - st->desc_index) {
1261 dev_err(ph->dev,
1262 "No. of resources can't exceed %d\n",
1263 st->max_resources);
1264 ret = -EINVAL;
1265 break;
1266 }
1267
1268 for (st->loop_idx = 0; st->loop_idx < st->num_returned;
1269 st->loop_idx++) {
1270 ret = iops->process_response(ph, i->resp, st, i->priv);
1271 if (ret)
1272 goto out;
1273 }
1274
1275 st->desc_index += st->num_returned;
c7f8852d 1276 ph->xops->reset_rx_to_maxsz(ph, i->t);
36b6ea0f
CM
1277 /*
1278 * check for both returned and remaining to avoid infinite
1279 * loop due to buggy firmware
1280 */
1281 } while (st->num_returned && st->num_remaining);
1282
1283out:
1284 /* Finalize and destroy iterator */
c7f8852d 1285 ph->xops->xfer_put(ph, i->t);
36b6ea0f
CM
1286 devm_kfree(ph->dev, i);
1287
1288 return ret;
1289}
1290
6f9ea4da
CM
1291struct scmi_msg_get_fc_info {
1292 __le32 domain;
1293 __le32 message_id;
1294};
1295
1296struct scmi_msg_resp_desc_fc {
1297 __le32 attr;
1298#define SUPPORTS_DOORBELL(x) ((x) & BIT(0))
1299#define DOORBELL_REG_WIDTH(x) FIELD_GET(GENMASK(2, 1), (x))
1300 __le32 rate_limit;
1301 __le32 chan_addr_low;
1302 __le32 chan_addr_high;
1303 __le32 chan_size;
1304 __le32 db_addr_low;
1305 __le32 db_addr_high;
1306 __le32 db_set_lmask;
1307 __le32 db_set_hmask;
1308 __le32 db_preserve_lmask;
1309 __le32 db_preserve_hmask;
1310};
1311
1312static void
1313scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph,
1314 u8 describe_id, u32 message_id, u32 valid_size,
1315 u32 domain, void __iomem **p_addr,
1316 struct scmi_fc_db_info **p_db)
1317{
1318 int ret;
1319 u32 flags;
1320 u64 phys_addr;
1321 u8 size;
1322 void __iomem *addr;
1323 struct scmi_xfer *t;
1324 struct scmi_fc_db_info *db = NULL;
1325 struct scmi_msg_get_fc_info *info;
1326 struct scmi_msg_resp_desc_fc *resp;
1327 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1328
1329 if (!p_addr) {
1330 ret = -EINVAL;
1331 goto err_out;
1332 }
1333
1334 ret = ph->xops->xfer_get_init(ph, describe_id,
1335 sizeof(*info), sizeof(*resp), &t);
1336 if (ret)
1337 goto err_out;
1338
1339 info = t->tx.buf;
1340 info->domain = cpu_to_le32(domain);
1341 info->message_id = cpu_to_le32(message_id);
1342
1343 /*
1344 * Bail out on error leaving fc_info addresses zeroed; this includes
1345 * the case in which the requested domain/message_id does NOT support
1346 * fastchannels at all.
1347 */
1348 ret = ph->xops->do_xfer(ph, t);
1349 if (ret)
1350 goto err_xfer;
1351
1352 resp = t->rx.buf;
1353 flags = le32_to_cpu(resp->attr);
1354 size = le32_to_cpu(resp->chan_size);
1355 if (size != valid_size) {
1356 ret = -EINVAL;
1357 goto err_xfer;
1358 }
1359
1360 phys_addr = le32_to_cpu(resp->chan_addr_low);
1361 phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32;
1362 addr = devm_ioremap(ph->dev, phys_addr, size);
1363 if (!addr) {
1364 ret = -EADDRNOTAVAIL;
1365 goto err_xfer;
1366 }
1367
1368 *p_addr = addr;
1369
1370 if (p_db && SUPPORTS_DOORBELL(flags)) {
1371 db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL);
1372 if (!db) {
1373 ret = -ENOMEM;
1374 goto err_db;
1375 }
1376
1377 size = 1 << DOORBELL_REG_WIDTH(flags);
1378 phys_addr = le32_to_cpu(resp->db_addr_low);
1379 phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32;
1380 addr = devm_ioremap(ph->dev, phys_addr, size);
1381 if (!addr) {
1382 ret = -EADDRNOTAVAIL;
1383 goto err_db_mem;
1384 }
1385
1386 db->addr = addr;
1387 db->width = size;
1388 db->set = le32_to_cpu(resp->db_set_lmask);
1389 db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32;
1390 db->mask = le32_to_cpu(resp->db_preserve_lmask);
1391 db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32;
1392
1393 *p_db = db;
1394 }
1395
1396 ph->xops->xfer_put(ph, t);
1397
1398 dev_dbg(ph->dev,
1399 "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n",
1400 pi->proto->id, message_id, domain);
1401
1402 return;
1403
1404err_db_mem:
1405 devm_kfree(ph->dev, db);
1406
1407err_db:
1408 *p_addr = NULL;
1409
1410err_xfer:
1411 ph->xops->xfer_put(ph, t);
1412
1413err_out:
1414 dev_warn(ph->dev,
1415 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n",
1416 pi->proto->id, message_id, domain, ret);
1417}
1418
1419#define SCMI_PROTO_FC_RING_DB(w) \
1420do { \
1421 u##w val = 0; \
1422 \
1423 if (db->mask) \
1424 val = ioread##w(db->addr) & db->mask; \
1425 iowrite##w((u##w)db->set | val, db->addr); \
1426} while (0)
1427
1428static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
1429{
1430 if (!db || !db->addr)
1431 return;
1432
1433 if (db->width == 1)
1434 SCMI_PROTO_FC_RING_DB(8);
1435 else if (db->width == 2)
1436 SCMI_PROTO_FC_RING_DB(16);
1437 else if (db->width == 4)
1438 SCMI_PROTO_FC_RING_DB(32);
1439 else /* db->width == 8 */
1440#ifdef CONFIG_64BIT
1441 SCMI_PROTO_FC_RING_DB(64);
1442#else
1443 {
1444 u64 val = 0;
1445
1446 if (db->mask)
1447 val = ioread64_hi_lo(db->addr) & db->mask;
1448 iowrite64_hi_lo(db->set | val, db->addr);
1449 }
1450#endif
1451}
1452
5c873d12
CM
1453static const struct scmi_proto_helpers_ops helpers_ops = {
1454 .extended_name_get = scmi_common_extended_name_get,
36b6ea0f
CM
1455 .iter_response_init = scmi_iterator_init,
1456 .iter_response_run = scmi_iterator_run,
6f9ea4da
CM
1457 .fastchannel_init = scmi_common_fastchannel_init,
1458 .fastchannel_db_ring = scmi_common_fastchannel_db_ring,
5c873d12
CM
1459};
1460
3d5d6e84
CM
1461/**
1462 * scmi_revision_area_get - Retrieve version memory area.
1463 *
1464 * @ph: A reference to the protocol handle.
1465 *
1466 * A helper to grab the version memory area reference during SCMI Base protocol
1467 * initialization.
1468 *
1469 * Return: A reference to the version memory area associated to the SCMI
1470 * instance underlying this protocol handle.
1471 */
1472struct scmi_revision_info *
1473scmi_revision_area_get(const struct scmi_protocol_handle *ph)
1474{
1475 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1476
1477 return pi->handle->version;
1478}
1479
48dc16e2
CM
1480/**
1481 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
1482 * instance descriptor.
1483 * @info: The reference to the related SCMI instance.
1484 * @proto: The protocol descriptor.
1485 *
1486 * Allocate a new protocol instance descriptor, using the provided @proto
1487 * description, against the specified SCMI instance @info, and initialize it;
1488 * all resources management is handled via a dedicated per-protocol devres
1489 * group.
1490 *
1491 * Context: Assumes to be called with @protocols_mtx already acquired.
1492 * Return: A reference to a freshly allocated and initialized protocol instance
f5800e0b
CM
1493 * or ERR_PTR on failure. On failure the @proto reference is at first
1494 * put using @scmi_protocol_put() before releasing all the devres group.
48dc16e2
CM
1495 */
1496static struct scmi_protocol_instance *
1497scmi_alloc_init_protocol_instance(struct scmi_info *info,
1498 const struct scmi_protocol *proto)
1499{
1500 int ret = -ENOMEM;
1501 void *gid;
1502 struct scmi_protocol_instance *pi;
f0e73cee 1503 const struct scmi_handle *handle = &info->handle;
48dc16e2
CM
1504
1505 /* Protocol specific devres group */
1506 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
f5800e0b
CM
1507 if (!gid) {
1508 scmi_protocol_put(proto->id);
48dc16e2 1509 goto out;
f5800e0b 1510 }
48dc16e2
CM
1511
1512 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
1513 if (!pi)
1514 goto clean;
1515
1516 pi->gid = gid;
1517 pi->proto = proto;
d7b6cc56
CM
1518 pi->handle = handle;
1519 pi->ph.dev = handle->dev;
a4a20b09 1520 pi->ph.xops = &xfer_ops;
5c873d12 1521 pi->ph.hops = &helpers_ops;
d7b6cc56
CM
1522 pi->ph.set_priv = scmi_set_protocol_priv;
1523 pi->ph.get_priv = scmi_get_protocol_priv;
48dc16e2
CM
1524 refcount_set(&pi->users, 1);
1525 /* proto->init is assured NON NULL by scmi_protocol_register */
a4a20b09 1526 ret = pi->proto->instance_init(&pi->ph);
48dc16e2
CM
1527 if (ret)
1528 goto clean;
1529
1530 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
1531 GFP_KERNEL);
1532 if (ret != proto->id)
1533 goto clean;
1534
533c7095
CM
1535 /*
1536 * Warn but ignore events registration errors since we do not want
1537 * to skip whole protocols if their notifications are messed up.
1538 */
1539 if (pi->proto->events) {
1540 ret = scmi_register_protocol_events(handle, pi->proto->id,
b9f7fd90 1541 &pi->ph,
533c7095
CM
1542 pi->proto->events);
1543 if (ret)
1544 dev_warn(handle->dev,
1545 "Protocol:%X - Events Registration Failed - err:%d\n",
1546 pi->proto->id, ret);
1547 }
1548
48dc16e2
CM
1549 devres_close_group(handle->dev, pi->gid);
1550 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
1551
1552 return pi;
1553
1554clean:
f5800e0b
CM
1555 /* Take care to put the protocol module's owner before releasing all */
1556 scmi_protocol_put(proto->id);
48dc16e2
CM
1557 devres_release_group(handle->dev, gid);
1558out:
1559 return ERR_PTR(ret);
1560}
1561
1562/**
1563 * scmi_get_protocol_instance - Protocol initialization helper.
1564 * @handle: A reference to the SCMI platform instance.
1565 * @protocol_id: The protocol being requested.
1566 *
1567 * In case the required protocol has never been requested before for this
1568 * instance, allocate and initialize all the needed structures while handling
1569 * resource allocation with a dedicated per-protocol devres subgroup.
1570 *
f5800e0b
CM
1571 * Return: A reference to an initialized protocol instance or error on failure:
1572 * in particular returns -EPROBE_DEFER when the desired protocol could
1573 * NOT be found.
48dc16e2
CM
1574 */
1575static struct scmi_protocol_instance * __must_check
f0e73cee 1576scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
48dc16e2
CM
1577{
1578 struct scmi_protocol_instance *pi;
1579 struct scmi_info *info = handle_to_scmi_info(handle);
1580
1581 mutex_lock(&info->protocols_mtx);
1582 pi = idr_find(&info->protocols, protocol_id);
1583
1584 if (pi) {
1585 refcount_inc(&pi->users);
1586 } else {
1587 const struct scmi_protocol *proto;
1588
1589 /* Fails if protocol not registered on bus */
1590 proto = scmi_protocol_get(protocol_id);
1591 if (proto)
1592 pi = scmi_alloc_init_protocol_instance(info, proto);
1593 else
f5800e0b 1594 pi = ERR_PTR(-EPROBE_DEFER);
48dc16e2
CM
1595 }
1596 mutex_unlock(&info->protocols_mtx);
1597
1598 return pi;
1599}
1600
1601/**
1602 * scmi_protocol_acquire - Protocol acquire
1603 * @handle: A reference to the SCMI platform instance.
1604 * @protocol_id: The protocol being requested.
1605 *
1606 * Register a new user for the requested protocol on the specified SCMI
1607 * platform instance, possibly triggering its initialization on first user.
1608 *
1609 * Return: 0 if protocol was acquired successfully.
1610 */
f0e73cee 1611int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
48dc16e2
CM
1612{
1613 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
1614}
1615
1616/**
1617 * scmi_protocol_release - Protocol de-initialization helper.
1618 * @handle: A reference to the SCMI platform instance.
1619 * @protocol_id: The protocol being requested.
1620 *
1621 * Remove one user for the specified protocol and triggers de-initialization
1622 * and resources de-allocation once the last user has gone.
1623 */
f0e73cee 1624void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
48dc16e2
CM
1625{
1626 struct scmi_info *info = handle_to_scmi_info(handle);
1627 struct scmi_protocol_instance *pi;
1628
1629 mutex_lock(&info->protocols_mtx);
1630 pi = idr_find(&info->protocols, protocol_id);
1631 if (WARN_ON(!pi))
1632 goto out;
1633
1634 if (refcount_dec_and_test(&pi->users)) {
1635 void *gid = pi->gid;
1636
533c7095
CM
1637 if (pi->proto->events)
1638 scmi_deregister_protocol_events(handle, protocol_id);
1639
48dc16e2 1640 if (pi->proto->instance_deinit)
a4a20b09 1641 pi->proto->instance_deinit(&pi->ph);
48dc16e2
CM
1642
1643 idr_remove(&info->protocols, protocol_id);
1644
f5800e0b
CM
1645 scmi_protocol_put(protocol_id);
1646
48dc16e2
CM
1647 devres_release_group(handle->dev, gid);
1648 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
1649 protocol_id);
1650 }
1651
1652out:
1653 mutex_unlock(&info->protocols_mtx);
1654}
1655
8d3581c2 1656void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
b6f20ff8
SH
1657 u8 *prot_imp)
1658{
8d3581c2
CM
1659 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1660 struct scmi_info *info = handle_to_scmi_info(pi->handle);
b6f20ff8
SH
1661
1662 info->protocols_imp = prot_imp;
1663}
1664
bc40081d
SH
1665static bool
1666scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
1667{
1668 int i;
1669 struct scmi_info *info = handle_to_scmi_info(handle);
776b6c8a 1670 struct scmi_revision_info *rev = handle->version;
bc40081d
SH
1671
1672 if (!info->protocols_imp)
1673 return false;
1674
776b6c8a 1675 for (i = 0; i < rev->num_protocols; i++)
bc40081d
SH
1676 if (info->protocols_imp[i] == prot_id)
1677 return true;
1678 return false;
1679}
1680
23934efe 1681struct scmi_protocol_devres {
f0e73cee 1682 const struct scmi_handle *handle;
23934efe
CM
1683 u8 protocol_id;
1684};
1685
1686static void scmi_devm_release_protocol(struct device *dev, void *res)
1687{
1688 struct scmi_protocol_devres *dres = res;
1689
1690 scmi_protocol_release(dres->handle, dres->protocol_id);
1691}
1692
d9107999
CM
1693static struct scmi_protocol_instance __must_check *
1694scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id)
1695{
1696 struct scmi_protocol_instance *pi;
1697 struct scmi_protocol_devres *dres;
1698
1699 dres = devres_alloc(scmi_devm_release_protocol,
1700 sizeof(*dres), GFP_KERNEL);
1701 if (!dres)
1702 return ERR_PTR(-ENOMEM);
1703
1704 pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
1705 if (IS_ERR(pi)) {
1706 devres_free(dres);
1707 return pi;
1708 }
1709
1710 dres->handle = sdev->handle;
1711 dres->protocol_id = protocol_id;
1712 devres_add(&sdev->dev, dres);
1713
1714 return pi;
1715}
1716
23934efe
CM
1717/**
1718 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
1719 * @sdev: A reference to an scmi_device whose embedded struct device is to
1720 * be used for devres accounting.
1721 * @protocol_id: The protocol being requested.
1722 * @ph: A pointer reference used to pass back the associated protocol handle.
1723 *
1724 * Get hold of a protocol accounting for its usage, eventually triggering its
1725 * initialization, and returning the protocol specific operations and related
1726 * protocol handle which will be used as first argument in most of the
1727 * protocols operations methods.
1728 * Being a devres based managed method, protocol hold will be automatically
1729 * released, and possibly de-initialized on last user, once the SCMI driver
1730 * owning the scmi_device is unbound from it.
1731 *
1732 * Return: A reference to the requested protocol operations or error.
1733 * Must be checked for errors by caller.
1734 */
1735static const void __must_check *
1736scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
1737 struct scmi_protocol_handle **ph)
1738{
1739 struct scmi_protocol_instance *pi;
23934efe
CM
1740
1741 if (!ph)
1742 return ERR_PTR(-EINVAL);
1743
d9107999
CM
1744 pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
1745 if (IS_ERR(pi))
23934efe 1746 return pi;
23934efe
CM
1747
1748 *ph = &pi->ph;
1749
1750 return pi->proto->ops;
1751}
1752
d9107999
CM
1753/**
1754 * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
1755 * @sdev: A reference to an scmi_device whose embedded struct device is to
1756 * be used for devres accounting.
1757 * @protocol_id: The protocol being requested.
1758 *
1759 * Get hold of a protocol accounting for its usage, possibly triggering its
1760 * initialization but without getting access to its protocol specific operations
1761 * and handle.
1762 *
1763 * Being a devres based managed method, protocol hold will be automatically
1764 * released, and possibly de-initialized on last user, once the SCMI driver
1765 * owning the scmi_device is unbound from it.
1766 *
1767 * Return: 0 on SUCCESS
1768 */
1769static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev,
1770 u8 protocol_id)
1771{
1772 struct scmi_protocol_instance *pi;
1773
1774 pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
1775 if (IS_ERR(pi))
1776 return PTR_ERR(pi);
1777
1778 return 0;
1779}
1780
23934efe
CM
1781static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
1782{
1783 struct scmi_protocol_devres *dres = res;
1784
1785 if (WARN_ON(!dres || !data))
1786 return 0;
1787
1788 return dres->protocol_id == *((u8 *)data);
1789}
1790
1791/**
1792 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
1793 * @sdev: A reference to an scmi_device whose embedded struct device is to
1794 * be used for devres accounting.
1795 * @protocol_id: The protocol being requested.
1796 *
1797 * Explicitly release a protocol hold previously obtained calling the above
1798 * @scmi_devm_protocol_get.
1799 */
1800static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
1801{
1802 int ret;
1803
1804 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
1805 scmi_devm_protocol_match, &protocol_id);
1806 WARN_ON(ret);
1807}
1808
69255e74
CM
1809/**
1810 * scmi_is_transport_atomic - Method to check if underlying transport for an
1811 * SCMI instance is configured as atomic.
1812 *
1813 * @handle: A reference to the SCMI platform instance.
05976c5f
CM
1814 * @atomic_threshold: An optional return value for the system wide currently
1815 * configured threshold for atomic operations.
69255e74
CM
1816 *
1817 * Return: True if transport is configured as atomic
1818 */
05976c5f
CM
1819static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
1820 unsigned int *atomic_threshold)
69255e74 1821{
05976c5f 1822 bool ret;
69255e74
CM
1823 struct scmi_info *info = handle_to_scmi_info(handle);
1824
05976c5f
CM
1825 ret = info->desc->atomic_enabled && is_transport_polling_capable(info);
1826 if (ret && atomic_threshold)
1827 *atomic_threshold = info->atomic_threshold;
1828
1829 return ret;
69255e74
CM
1830}
1831
d4f9dddd
CM
1832static inline
1833struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info)
1834{
1835 info->users++;
1836 return &info->handle;
1837}
1838
aa4f886f 1839/**
14e297b3 1840 * scmi_handle_get() - Get the SCMI handle for a device
aa4f886f
SH
1841 *
1842 * @dev: pointer to device for which we want SCMI handle
1843 *
1844 * NOTE: The function does not track individual clients of the framework
1baf47c2 1845 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
1846 * scmi_handle_put must be balanced with successful scmi_handle_get
1847 *
1848 * Return: pointer to handle if successful, NULL on error
1849 */
1850struct scmi_handle *scmi_handle_get(struct device *dev)
1851{
1852 struct list_head *p;
1853 struct scmi_info *info;
1854 struct scmi_handle *handle = NULL;
1855
1856 mutex_lock(&scmi_list_mutex);
1857 list_for_each(p, &scmi_list) {
1858 info = list_entry(p, struct scmi_info, node);
1859 if (dev->parent == info->dev) {
d4f9dddd 1860 handle = scmi_handle_get_from_info_unlocked(info);
aa4f886f
SH
1861 break;
1862 }
1863 }
1864 mutex_unlock(&scmi_list_mutex);
1865
1866 return handle;
1867}
1868
1869/**
1870 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
1871 *
1872 * @handle: handle acquired by scmi_handle_get
1873 *
1874 * NOTE: The function does not track individual clients of the framework
1baf47c2 1875 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
1876 * scmi_handle_put must be balanced with successful scmi_handle_get
1877 *
1878 * Return: 0 is successfully released
1879 * if null was passed, it returns -EINVAL;
1880 */
1881int scmi_handle_put(const struct scmi_handle *handle)
1882{
1883 struct scmi_info *info;
1884
1885 if (!handle)
1886 return -EINVAL;
1887
1888 info = handle_to_scmi_info(handle);
1889 mutex_lock(&scmi_list_mutex);
1890 if (!WARN_ON(!info->users))
1891 info->users--;
1892 mutex_unlock(&scmi_list_mutex);
1893
1894 return 0;
1895}
1896
4ebd8f6d
SH
1897static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1898 struct scmi_xfers_info *info)
aa4f886f
SH
1899{
1900 int i;
1901 struct scmi_xfer *xfer;
1902 struct device *dev = sinfo->dev;
1903 const struct scmi_desc *desc = sinfo->desc;
aa4f886f
SH
1904
1905 /* Pre-allocated messages, no more than what hdr.seq can support */
c92c3e38 1906 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
bdb8742d
CM
1907 dev_err(dev,
1908 "Invalid maximum messages %d, not in range [1 - %lu]\n",
c92c3e38 1909 info->max_msg, MSG_TOKEN_MAX);
aa4f886f
SH
1910 return -EINVAL;
1911 }
1912
9ca5a183 1913 hash_init(info->pending_xfers);
aa4f886f 1914
9ca5a183
CM
1915 /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
1916 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(MSG_TOKEN_MAX),
aa4f886f
SH
1917 sizeof(long), GFP_KERNEL);
1918 if (!info->xfer_alloc_table)
1919 return -ENOMEM;
1920
9ca5a183
CM
1921 /*
1922 * Preallocate a number of xfers equal to max inflight messages,
1923 * pre-initialize the buffer pointer to pre-allocated buffers and
1924 * attach all of them to the free list
1925 */
1926 INIT_HLIST_HEAD(&info->free_xfers);
c92c3e38 1927 for (i = 0; i < info->max_msg; i++) {
9ca5a183
CM
1928 xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
1929 if (!xfer)
1930 return -ENOMEM;
1931
aa4f886f
SH
1932 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1933 GFP_KERNEL);
1934 if (!xfer->rx.buf)
1935 return -ENOMEM;
1936
1937 xfer->tx.buf = xfer->rx.buf;
1938 init_completion(&xfer->done);
ed7c04c1 1939 spin_lock_init(&xfer->lock);
9ca5a183
CM
1940
1941 /* Add initialized xfer to the free list */
1942 hlist_add_head(&xfer->node, &info->free_xfers);
aa4f886f
SH
1943 }
1944
1945 spin_lock_init(&info->xfer_lock);
1946
1947 return 0;
1948}
1949
c92c3e38
IS
1950static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
1951{
1952 const struct scmi_desc *desc = sinfo->desc;
1953
1954 if (!desc->ops->get_max_msg) {
1955 sinfo->tx_minfo.max_msg = desc->max_msg;
1956 sinfo->rx_minfo.max_msg = desc->max_msg;
1957 } else {
1958 struct scmi_chan_info *base_cinfo;
1959
1960 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
1961 if (!base_cinfo)
1962 return -EINVAL;
1963 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
1964
1965 /* RX channel is optional so can be skipped */
1966 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
1967 if (base_cinfo)
1968 sinfo->rx_minfo.max_msg =
1969 desc->ops->get_max_msg(base_cinfo);
1970 }
1971
1972 return 0;
1973}
1974
4ebd8f6d
SH
1975static int scmi_xfer_info_init(struct scmi_info *sinfo)
1976{
c92c3e38
IS
1977 int ret;
1978
1979 ret = scmi_channels_max_msg_configure(sinfo);
1980 if (ret)
1981 return ret;
4ebd8f6d 1982
c92c3e38 1983 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
4ebd8f6d
SH
1984 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1985 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1986
1987 return ret;
1988}
1989
5c8a47a5
VK
1990static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1991 int prot_id, bool tx)
aa4f886f 1992{
3748daf7 1993 int ret, idx;
fbc4d81a 1994 struct scmi_chan_info *cinfo;
46cc7c28 1995 struct idr *idr;
3748daf7
SH
1996
1997 /* Transmit channel is first entry i.e. index 0 */
1998 idx = tx ? 0 : 1;
46cc7c28 1999 idr = tx ? &info->tx_idr : &info->rx_idr;
aa4f886f 2000
11040889
SH
2001 /* check if already allocated, used for multiple device per protocol */
2002 cinfo = idr_find(idr, prot_id);
2003 if (cinfo)
2004 return 0;
2005
5c8a47a5 2006 if (!info->desc->ops->chan_available(dev, idx)) {
46cc7c28
SH
2007 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
2008 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
2009 return -EINVAL;
907b6d14
SH
2010 goto idr_alloc;
2011 }
2012
fbc4d81a
SH
2013 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
2014 if (!cinfo)
2015 return -ENOMEM;
2016
fbc4d81a 2017 cinfo->dev = dev;
59172b21 2018 cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms;
fbc4d81a 2019
5c8a47a5
VK
2020 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
2021 if (ret)
aa4f886f 2022 return ret;
aa4f886f 2023
a690b7e6
CM
2024 if (tx && is_polling_required(cinfo, info)) {
2025 if (is_transport_polling_capable(info))
2026 dev_info(dev,
2027 "Enabled polling mode TX channel - prot_id:%d\n",
2028 prot_id);
2029 else
2030 dev_warn(dev,
2031 "Polling mode NOT supported by transport.\n");
2032 }
2033
907b6d14 2034idr_alloc:
46cc7c28 2035 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
907b6d14
SH
2036 if (ret != prot_id) {
2037 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
2038 return ret;
2039 }
2040
2041 cinfo->handle = &info->handle;
aa4f886f
SH
2042 return 0;
2043}
2044
46cc7c28 2045static inline int
5c8a47a5 2046scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
46cc7c28 2047{
5c8a47a5 2048 int ret = scmi_chan_setup(info, dev, prot_id, true);
46cc7c28 2049
be9ba1f7
CM
2050 if (!ret) {
2051 /* Rx is optional, report only memory errors */
2052 ret = scmi_chan_setup(info, dev, prot_id, false);
2053 if (ret && ret != -ENOMEM)
2054 ret = 0;
2055 }
46cc7c28
SH
2056
2057 return ret;
2058}
2059
d4f9dddd
CM
2060/**
2061 * scmi_get_protocol_device - Helper to get/create an SCMI device.
2062 *
2063 * @np: A device node representing a valid active protocols for the referred
2064 * SCMI instance.
2065 * @info: The referred SCMI instance for which we are getting/creating this
2066 * device.
2067 * @prot_id: The protocol ID.
2068 * @name: The device name.
2069 *
2070 * Referring to the specific SCMI instance identified by @info, this helper
2071 * takes care to return a properly initialized device matching the requested
2072 * @proto_id and @name: if device was still not existent it is created as a
2073 * child of the specified SCMI instance @info and its transport properly
2074 * initialized as usual.
b98cf55e
CM
2075 *
2076 * Return: A properly initialized scmi device, NULL otherwise.
d4f9dddd
CM
2077 */
2078static inline struct scmi_device *
2079scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
2080 int prot_id, const char *name)
bc40081d
SH
2081{
2082 struct scmi_device *sdev;
2083
d4f9dddd
CM
2084 /* Already created for this parent SCMI instance ? */
2085 sdev = scmi_child_dev_find(info->dev, prot_id, name);
2086 if (sdev)
2087 return sdev;
2088
a0db3962
CM
2089 mutex_lock(&scmi_syspower_mtx);
2090 if (prot_id == SCMI_PROTOCOL_SYSTEM && scmi_syspower_registered) {
2091 dev_warn(info->dev,
2092 "SCMI SystemPower protocol device must be unique !\n");
2093 mutex_unlock(&scmi_syspower_mtx);
2094
2095 return NULL;
2096 }
2097
d4f9dddd
CM
2098 pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
2099
ee7a9c9f 2100 sdev = scmi_device_create(np, info->dev, prot_id, name);
bc40081d
SH
2101 if (!sdev) {
2102 dev_err(info->dev, "failed to create %d protocol device\n",
2103 prot_id);
a0db3962
CM
2104 mutex_unlock(&scmi_syspower_mtx);
2105
d4f9dddd 2106 return NULL;
bc40081d
SH
2107 }
2108
5c8a47a5 2109 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
907b6d14
SH
2110 dev_err(&sdev->dev, "failed to setup transport\n");
2111 scmi_device_destroy(sdev);
a0db3962
CM
2112 mutex_unlock(&scmi_syspower_mtx);
2113
d4f9dddd 2114 return NULL;
907b6d14
SH
2115 }
2116
a0db3962
CM
2117 if (prot_id == SCMI_PROTOCOL_SYSTEM)
2118 scmi_syspower_registered = true;
2119
2120 mutex_unlock(&scmi_syspower_mtx);
2121
d4f9dddd
CM
2122 return sdev;
2123}
2124
2125static inline void
2126scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
2127 int prot_id, const char *name)
2128{
2129 struct scmi_device *sdev;
2130
2131 sdev = scmi_get_protocol_device(np, info, prot_id, name);
2132 if (!sdev)
2133 return;
2134
bc40081d
SH
2135 /* setup handle now as the transport is ready */
2136 scmi_set_handle(sdev);
2137}
2138
d4f9dddd
CM
2139/**
2140 * scmi_create_protocol_devices - Create devices for all pending requests for
2141 * this SCMI instance.
2142 *
2143 * @np: The device node describing the protocol
2144 * @info: The SCMI instance descriptor
2145 * @prot_id: The protocol ID
2146 *
2147 * All devices previously requested for this instance (if any) are found and
2148 * created by scanning the proper @&scmi_requested_devices entry.
2149 */
2150static void scmi_create_protocol_devices(struct device_node *np,
2151 struct scmi_info *info, int prot_id)
2152{
2153 struct list_head *phead;
9c5c463f 2154
d4f9dddd
CM
2155 mutex_lock(&scmi_requested_devices_mtx);
2156 phead = idr_find(&scmi_requested_devices, prot_id);
2157 if (phead) {
2158 struct scmi_requested_dev *rdev;
9c5c463f 2159
d4f9dddd
CM
2160 list_for_each_entry(rdev, phead, node)
2161 scmi_create_protocol_device(np, info, prot_id,
2162 rdev->id_table->name);
2163 }
2164 mutex_unlock(&scmi_requested_devices_mtx);
2165}
2166
2167/**
2168 * scmi_protocol_device_request - Helper to request a device
2169 *
2170 * @id_table: A protocol/name pair descriptor for the device to be created.
2171 *
2172 * This helper let an SCMI driver request specific devices identified by the
2173 * @id_table to be created for each active SCMI instance.
2174 *
2175 * The requested device name MUST NOT be already existent for any protocol;
2176 * at first the freshly requested @id_table is annotated in the IDR table
2177 * @scmi_requested_devices, then a matching device is created for each already
2178 * active SCMI instance. (if any)
2179 *
2180 * This way the requested device is created straight-away for all the already
2181 * initialized(probed) SCMI instances (handles) and it remains also annotated
2182 * as pending creation if the requesting SCMI driver was loaded before some
2183 * SCMI instance and related transports were available: when such late instance
2184 * is probed, its probe will take care to scan the list of pending requested
2185 * devices and create those on its own (see @scmi_create_protocol_devices and
2186 * its enclosing loop)
2187 *
2188 * Return: 0 on Success
2189 */
2190int scmi_protocol_device_request(const struct scmi_device_id *id_table)
9c5c463f 2191{
d4f9dddd
CM
2192 int ret = 0;
2193 unsigned int id = 0;
2194 struct list_head *head, *phead = NULL;
2195 struct scmi_requested_dev *rdev;
2196 struct scmi_info *info;
9c5c463f 2197
d4f9dddd
CM
2198 pr_debug("Requesting SCMI device (%s) for protocol %x\n",
2199 id_table->name, id_table->protocol_id);
2200
2201 /*
2202 * Search for the matching protocol rdev list and then search
2203 * of any existent equally named device...fails if any duplicate found.
2204 */
2205 mutex_lock(&scmi_requested_devices_mtx);
2206 idr_for_each_entry(&scmi_requested_devices, head, id) {
2207 if (!phead) {
2208 /* A list found registered in the IDR is never empty */
2209 rdev = list_first_entry(head, struct scmi_requested_dev,
2210 node);
2211 if (rdev->id_table->protocol_id ==
2212 id_table->protocol_id)
2213 phead = head;
2214 }
2215 list_for_each_entry(rdev, head, node) {
2216 if (!strcmp(rdev->id_table->name, id_table->name)) {
2217 pr_err("Ignoring duplicate request [%d] %s\n",
2218 rdev->id_table->protocol_id,
2219 rdev->id_table->name);
2220 ret = -EINVAL;
2221 goto out;
2222 }
2223 }
2224 }
2225
2226 /*
2227 * No duplicate found for requested id_table, so let's create a new
2228 * requested device entry for this new valid request.
2229 */
2230 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2231 if (!rdev) {
2232 ret = -ENOMEM;
2233 goto out;
2234 }
2235 rdev->id_table = id_table;
2236
2237 /*
2238 * Append the new requested device table descriptor to the head of the
2239 * related protocol list, eventually creating such head if not already
2240 * there.
2241 */
2242 if (!phead) {
2243 phead = kzalloc(sizeof(*phead), GFP_KERNEL);
2244 if (!phead) {
2245 kfree(rdev);
2246 ret = -ENOMEM;
2247 goto out;
2248 }
2249 INIT_LIST_HEAD(phead);
2250
2251 ret = idr_alloc(&scmi_requested_devices, (void *)phead,
2252 id_table->protocol_id,
2253 id_table->protocol_id + 1, GFP_KERNEL);
2254 if (ret != id_table->protocol_id) {
2255 pr_err("Failed to save SCMI device - ret:%d\n", ret);
2256 kfree(rdev);
2257 kfree(phead);
2258 ret = -EINVAL;
2259 goto out;
2260 }
2261 ret = 0;
2262 }
2263 list_add(&rdev->node, phead);
2264
2265 /*
2266 * Now effectively create and initialize the requested device for every
2267 * already initialized SCMI instance which has registered the requested
2268 * protocol as a valid active one: i.e. defined in DT and supported by
2269 * current platform FW.
2270 */
2271 mutex_lock(&scmi_list_mutex);
2272 list_for_each_entry(info, &scmi_list, node) {
2273 struct device_node *child;
2274
2275 child = idr_find(&info->active_protocols,
2276 id_table->protocol_id);
2277 if (child) {
2278 struct scmi_device *sdev;
2279
2280 sdev = scmi_get_protocol_device(child, info,
2281 id_table->protocol_id,
2282 id_table->name);
3f4071cb
CM
2283 if (sdev) {
2284 /* Set handle if not already set: device existed */
2285 if (!sdev->handle)
2286 sdev->handle =
2287 scmi_handle_get_from_info_unlocked(info);
2288 /* Relink consumer and suppliers */
2289 if (sdev->handle)
2290 scmi_device_link_add(&sdev->dev,
2291 sdev->handle->dev);
2292 }
d4f9dddd
CM
2293 } else {
2294 dev_err(info->dev,
2295 "Failed. SCMI protocol %d not active.\n",
2296 id_table->protocol_id);
2297 }
2298 }
2299 mutex_unlock(&scmi_list_mutex);
2300
2301out:
2302 mutex_unlock(&scmi_requested_devices_mtx);
9c5c463f 2303
d4f9dddd
CM
2304 return ret;
2305}
9c5c463f 2306
d4f9dddd
CM
2307/**
2308 * scmi_protocol_device_unrequest - Helper to unrequest a device
2309 *
2310 * @id_table: A protocol/name pair descriptor for the device to be unrequested.
2311 *
2312 * An helper to let an SCMI driver release its request about devices; note that
2313 * devices are created and initialized once the first SCMI driver request them
2314 * but they destroyed only on SCMI core unloading/unbinding.
2315 *
2316 * The current SCMI transport layer uses such devices as internal references and
2317 * as such they could be shared as same transport between multiple drivers so
2318 * that cannot be safely destroyed till the whole SCMI stack is removed.
2319 * (unless adding further burden of refcounting.)
2320 */
2321void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
2322{
2323 struct list_head *phead;
2324
2325 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
2326 id_table->name, id_table->protocol_id);
2327
2328 mutex_lock(&scmi_requested_devices_mtx);
2329 phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
2330 if (phead) {
2331 struct scmi_requested_dev *victim, *tmp;
2332
2333 list_for_each_entry_safe(victim, tmp, phead, node) {
2334 if (!strcmp(victim->id_table->name, id_table->name)) {
2335 list_del(&victim->node);
2336 kfree(victim);
2337 break;
2338 }
2339 }
2340
2341 if (list_empty(phead)) {
2342 idr_remove(&scmi_requested_devices,
2343 id_table->protocol_id);
2344 kfree(phead);
9c5c463f
SH
2345 }
2346 }
d4f9dddd 2347 mutex_unlock(&scmi_requested_devices_mtx);
9c5c463f
SH
2348}
2349
1e7cbfaa
RB
2350static int scmi_cleanup_txrx_channels(struct scmi_info *info)
2351{
2352 int ret;
2353 struct idr *idr = &info->tx_idr;
2354
2355 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
2356 idr_destroy(&info->tx_idr);
2357
2358 idr = &info->rx_idr;
2359 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
2360 idr_destroy(&info->rx_idr);
2361
2362 return ret;
2363}
2364
aa4f886f
SH
2365static int scmi_probe(struct platform_device *pdev)
2366{
2367 int ret;
2368 struct scmi_handle *handle;
2369 const struct scmi_desc *desc;
2370 struct scmi_info *info;
2371 struct device *dev = &pdev->dev;
bc40081d 2372 struct device_node *child, *np = dev->of_node;
aa4f886f 2373
d9350f21
AP
2374 desc = of_device_get_match_data(dev);
2375 if (!desc)
2376 return -EINVAL;
aa4f886f
SH
2377
2378 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
2379 if (!info)
2380 return -ENOMEM;
2381
2382 info->dev = dev;
2383 info->desc = desc;
2384 INIT_LIST_HEAD(&info->node);
48dc16e2
CM
2385 idr_init(&info->protocols);
2386 mutex_init(&info->protocols_mtx);
d4f9dddd 2387 idr_init(&info->active_protocols);
aa4f886f 2388
aa4f886f 2389 platform_set_drvdata(pdev, info);
907b6d14 2390 idr_init(&info->tx_idr);
46cc7c28 2391 idr_init(&info->rx_idr);
aa4f886f
SH
2392
2393 handle = &info->handle;
2394 handle->dev = info->dev;
b6f20ff8 2395 handle->version = &info->version;
d9107999 2396 handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
23934efe
CM
2397 handle->devm_protocol_get = scmi_devm_protocol_get;
2398 handle->devm_protocol_put = scmi_devm_protocol_put;
aa4f886f 2399
05976c5f
CM
2400 /* System wide atomic threshold for atomic ops .. if any */
2401 if (!of_property_read_u32(np, "atomic-threshold-us",
2402 &info->atomic_threshold))
2403 dev_info(dev,
2404 "SCMI System wide atomic threshold set to %d us\n",
2405 info->atomic_threshold);
69255e74 2406 handle->is_transport_atomic = scmi_is_transport_atomic;
aa4f886f 2407
78852812
PH
2408 if (desc->ops->link_supplier) {
2409 ret = desc->ops->link_supplier(dev);
2410 if (ret)
2411 return ret;
2412 }
2413
5c8a47a5 2414 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
aa4f886f
SH
2415 if (ret)
2416 return ret;
2417
4ebd8f6d
SH
2418 ret = scmi_xfer_info_init(info);
2419 if (ret)
1e7cbfaa 2420 goto clear_txrx_setup;
4ebd8f6d 2421
6b8a6913
CM
2422 if (scmi_notification_init(handle))
2423 dev_err(dev, "SCMI Notifications NOT available.\n");
2424
69255e74
CM
2425 if (info->desc->atomic_enabled && !is_transport_polling_capable(info))
2426 dev_err(dev,
2427 "Transport is not polling capable. Atomic mode not supported.\n");
2428
8d3581c2
CM
2429 /*
2430 * Trigger SCMI Base protocol initialization.
2431 * It's mandatory and won't be ever released/deinit until the
2432 * SCMI stack is shutdown/unloaded as a whole.
2433 */
2434 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
b6f20ff8 2435 if (ret) {
8d3581c2 2436 dev_err(dev, "unable to communicate with SCMI\n");
1e7cbfaa 2437 goto notification_exit;
b6f20ff8
SH
2438 }
2439
aa4f886f
SH
2440 mutex_lock(&scmi_list_mutex);
2441 list_add_tail(&info->node, &scmi_list);
2442 mutex_unlock(&scmi_list_mutex);
2443
bc40081d
SH
2444 for_each_available_child_of_node(np, child) {
2445 u32 prot_id;
2446
2447 if (of_property_read_u32(child, "reg", &prot_id))
2448 continue;
2449
354b2e36
SH
2450 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
2451 dev_err(dev, "Out of range protocol %d\n", prot_id);
bc40081d
SH
2452
2453 if (!scmi_is_protocol_implemented(handle, prot_id)) {
2454 dev_err(dev, "SCMI protocol %d not implemented\n",
2455 prot_id);
2456 continue;
2457 }
2458
d4f9dddd
CM
2459 /*
2460 * Save this valid DT protocol descriptor amongst
2461 * @active_protocols for this SCMI instance/
2462 */
2463 ret = idr_alloc(&info->active_protocols, child,
2464 prot_id, prot_id + 1, GFP_KERNEL);
2465 if (ret != prot_id) {
2466 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
2467 prot_id);
2468 continue;
2469 }
2470
2471 of_node_get(child);
9c5c463f 2472 scmi_create_protocol_devices(child, info, prot_id);
bc40081d
SH
2473 }
2474
aa4f886f 2475 return 0;
1e7cbfaa
RB
2476
2477notification_exit:
2478 scmi_notification_exit(&info->handle);
2479clear_txrx_setup:
2480 scmi_cleanup_txrx_channels(info);
2481 return ret;
aa4f886f
SH
2482}
2483
5c8a47a5 2484void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
2747a967 2485{
2747a967 2486 idr_remove(idr, id);
2747a967
SH
2487}
2488
2489static int scmi_remove(struct platform_device *pdev)
2490{
3f4071cb 2491 int ret, id;
2747a967 2492 struct scmi_info *info = platform_get_drvdata(pdev);
d4f9dddd 2493 struct device_node *child;
2747a967
SH
2494
2495 mutex_lock(&scmi_list_mutex);
2496 if (info->users)
3f4071cb
CM
2497 dev_warn(&pdev->dev,
2498 "Still active SCMI users will be forcibly unbound.\n");
2499 list_del(&info->node);
2747a967
SH
2500 mutex_unlock(&scmi_list_mutex);
2501
a90b6543
CM
2502 scmi_notification_exit(&info->handle);
2503
48dc16e2
CM
2504 mutex_lock(&info->protocols_mtx);
2505 idr_destroy(&info->protocols);
2506 mutex_unlock(&info->protocols_mtx);
2507
d4f9dddd
CM
2508 idr_for_each_entry(&info->active_protocols, child, id)
2509 of_node_put(child);
2510 idr_destroy(&info->active_protocols);
2511
2747a967 2512 /* Safe to free channels since no more users */
3f4071cb
CM
2513 ret = scmi_cleanup_txrx_channels(info);
2514 if (ret)
2515 dev_warn(&pdev->dev, "Failed to cleanup SCMI channels.\n");
2516
2517 return 0;
2747a967
SH
2518}
2519
4605e224
SH
2520static ssize_t protocol_version_show(struct device *dev,
2521 struct device_attribute *attr, char *buf)
2522{
2523 struct scmi_info *info = dev_get_drvdata(dev);
2524
2525 return sprintf(buf, "%u.%u\n", info->version.major_ver,
2526 info->version.minor_ver);
2527}
2528static DEVICE_ATTR_RO(protocol_version);
2529
2530static ssize_t firmware_version_show(struct device *dev,
2531 struct device_attribute *attr, char *buf)
2532{
2533 struct scmi_info *info = dev_get_drvdata(dev);
2534
2535 return sprintf(buf, "0x%x\n", info->version.impl_ver);
2536}
2537static DEVICE_ATTR_RO(firmware_version);
2538
2539static ssize_t vendor_id_show(struct device *dev,
2540 struct device_attribute *attr, char *buf)
2541{
2542 struct scmi_info *info = dev_get_drvdata(dev);
2543
2544 return sprintf(buf, "%s\n", info->version.vendor_id);
2545}
2546static DEVICE_ATTR_RO(vendor_id);
2547
2548static ssize_t sub_vendor_id_show(struct device *dev,
2549 struct device_attribute *attr, char *buf)
2550{
2551 struct scmi_info *info = dev_get_drvdata(dev);
2552
2553 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
2554}
2555static DEVICE_ATTR_RO(sub_vendor_id);
2556
2557static struct attribute *versions_attrs[] = {
2558 &dev_attr_firmware_version.attr,
2559 &dev_attr_protocol_version.attr,
2560 &dev_attr_vendor_id.attr,
2561 &dev_attr_sub_vendor_id.attr,
2562 NULL,
2563};
2564ATTRIBUTE_GROUPS(versions);
2565
2747a967
SH
2566/* Each compatible listed below must have descriptor associated with it */
2567static const struct of_device_id scmi_of_match[] = {
e8419c24 2568#ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
5c8a47a5 2569 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
ab7766b7 2570#endif
5f90f189
EC
2571#ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE
2572 { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
2573#endif
e8419c24 2574#ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
1dc65580 2575 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
46abe13b
IS
2576#endif
2577#ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
2578 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
1dc65580 2579#endif
2747a967
SH
2580 { /* Sentinel */ },
2581};
2582
2583MODULE_DEVICE_TABLE(of, scmi_of_match);
2584
aa4f886f
SH
2585static struct platform_driver scmi_driver = {
2586 .driver = {
2587 .name = "arm-scmi",
fd96fbc8 2588 .suppress_bind_attrs = true,
aa4f886f 2589 .of_match_table = scmi_of_match,
4605e224 2590 .dev_groups = versions_groups,
aa4f886f
SH
2591 },
2592 .probe = scmi_probe,
2593 .remove = scmi_remove,
2594};
2595
ceac257d
CM
2596/**
2597 * __scmi_transports_setup - Common helper to call transport-specific
2598 * .init/.exit code if provided.
2599 *
2600 * @init: A flag to distinguish between init and exit.
2601 *
2602 * Note that, if provided, we invoke .init/.exit functions for all the
2603 * transports currently compiled in.
2604 *
2605 * Return: 0 on Success.
2606 */
2607static inline int __scmi_transports_setup(bool init)
2608{
2609 int ret = 0;
2610 const struct of_device_id *trans;
2611
2612 for (trans = scmi_of_match; trans->data; trans++) {
2613 const struct scmi_desc *tdesc = trans->data;
2614
2615 if ((init && !tdesc->transport_init) ||
2616 (!init && !tdesc->transport_exit))
2617 continue;
2618
2619 if (init)
2620 ret = tdesc->transport_init();
2621 else
2622 tdesc->transport_exit();
2623
2624 if (ret) {
2625 pr_err("SCMI transport %s FAILED initialization!\n",
2626 trans->compatible);
2627 break;
2628 }
2629 }
2630
2631 return ret;
2632}
2633
2634static int __init scmi_transports_init(void)
2635{
2636 return __scmi_transports_setup(true);
2637}
2638
2639static void __exit scmi_transports_exit(void)
2640{
2641 __scmi_transports_setup(false);
2642}
2643
5a2f0a0b
SH
2644static int __init scmi_driver_init(void)
2645{
ceac257d
CM
2646 int ret;
2647
c0397c85
CM
2648 /* Bail out if no SCMI transport was configured */
2649 if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
2650 return -EINVAL;
5a2f0a0b 2651
c0397c85 2652 scmi_bus_init();
e8419c24 2653
ceac257d
CM
2654 /* Initialize any compiled-in transport which provided an init/exit */
2655 ret = scmi_transports_init();
2656 if (ret)
2657 return ret;
2658
48dc16e2
CM
2659 scmi_base_register();
2660
1eaf18e3
SH
2661 scmi_clock_register();
2662 scmi_perf_register();
2663 scmi_power_register();
2664 scmi_reset_register();
2665 scmi_sensors_register();
2add5cac 2666 scmi_voltage_register();
1eaf18e3 2667 scmi_system_register();
0316f99c 2668 scmi_powercap_register();
1eaf18e3 2669
5a2f0a0b
SH
2670 return platform_driver_register(&scmi_driver);
2671}
1eaf18e3 2672subsys_initcall(scmi_driver_init);
5a2f0a0b
SH
2673
2674static void __exit scmi_driver_exit(void)
2675{
48dc16e2 2676 scmi_base_unregister();
5a2f0a0b 2677
1eaf18e3
SH
2678 scmi_clock_unregister();
2679 scmi_perf_unregister();
2680 scmi_power_unregister();
2681 scmi_reset_unregister();
2682 scmi_sensors_unregister();
2add5cac 2683 scmi_voltage_unregister();
1eaf18e3 2684 scmi_system_unregister();
0316f99c 2685 scmi_powercap_unregister();
1eaf18e3 2686
48dc16e2
CM
2687 scmi_bus_exit();
2688
ceac257d
CM
2689 scmi_transports_exit();
2690
5a2f0a0b
SH
2691 platform_driver_unregister(&scmi_driver);
2692}
2693module_exit(scmi_driver_exit);
aa4f886f 2694
1ba603f5 2695MODULE_ALIAS("platform:arm-scmi");
aa4f886f
SH
2696MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
2697MODULE_DESCRIPTION("ARM SCMI protocol driver");
2698MODULE_LICENSE("GPL v2");