1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Message Protocol driver
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
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.
14 * Copyright (C) 2018 ARM Ltd.
17 #include <linux/bitmap.h>
18 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/ktime.h>
22 #include <linux/mailbox_client.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/of_device.h>
26 #include <linux/processor.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
32 #define MSG_ID_MASK GENMASK(7, 0)
33 #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
34 #define MSG_TYPE_MASK GENMASK(9, 8)
35 #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
36 #define MSG_TYPE_COMMAND 0
37 #define MSG_TYPE_DELAYED_RESP 2
38 #define MSG_TYPE_NOTIFICATION 3
39 #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
40 #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
41 #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
42 #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
43 #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
45 enum scmi_error_codes
{
46 SCMI_SUCCESS
= 0, /* Success */
47 SCMI_ERR_SUPPORT
= -1, /* Not supported */
48 SCMI_ERR_PARAMS
= -2, /* Invalid Parameters */
49 SCMI_ERR_ACCESS
= -3, /* Invalid access/permission denied */
50 SCMI_ERR_ENTRY
= -4, /* Not found */
51 SCMI_ERR_RANGE
= -5, /* Value out of range */
52 SCMI_ERR_BUSY
= -6, /* Device busy */
53 SCMI_ERR_COMMS
= -7, /* Communication Error */
54 SCMI_ERR_GENERIC
= -8, /* Generic Error */
55 SCMI_ERR_HARDWARE
= -9, /* Hardware Error */
56 SCMI_ERR_PROTOCOL
= -10,/* Protocol Error */
60 /* List of all SCMI devices active in system */
61 static LIST_HEAD(scmi_list
);
62 /* Protection for the entire list */
63 static DEFINE_MUTEX(scmi_list_mutex
);
66 * struct scmi_xfers_info - Structure to manage transfer information
68 * @xfer_block: Preallocated Message array
69 * @xfer_alloc_table: Bitmap table for allocated messages.
70 * Index of this bitmap table is also used for message
71 * sequence identifier.
72 * @xfer_lock: Protection for message allocation
74 struct scmi_xfers_info
{
75 struct scmi_xfer
*xfer_block
;
76 unsigned long *xfer_alloc_table
;
81 * struct scmi_desc - Description of SoC integration
83 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
84 * @max_msg: Maximum number of messages that can be pending
85 * simultaneously in the system
86 * @max_msg_size: Maximum size of data per message that can be handled.
89 int max_rx_timeout_ms
;
95 * struct scmi_chan_info - Structure representing a SCMI channel information
98 * @chan: Transmit/Receive mailbox channel
99 * @payload: Transmit/Receive mailbox channel payload area
100 * @dev: Reference to device in the SCMI hierarchy corresponding to this
102 * @handle: Pointer to SCMI entity handle
104 struct scmi_chan_info
{
105 struct mbox_client cl
;
106 struct mbox_chan
*chan
;
107 void __iomem
*payload
;
109 struct scmi_handle
*handle
;
113 * struct scmi_info - Structure representing a SCMI instance
115 * @dev: Device pointer
116 * @desc: SoC description for this instance
117 * @handle: Instance of SCMI handle to send to clients
118 * @version: SCMI revision information containing protocol version,
119 * implementation version and (sub-)vendor identification.
120 * @tx_minfo: Universal Transmit Message management info
121 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
122 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
123 * @protocols_imp: List of protocols implemented, currently maximum of
124 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
126 * @users: Number of users of this instance
130 const struct scmi_desc
*desc
;
131 struct scmi_revision_info version
;
132 struct scmi_handle handle
;
133 struct scmi_xfers_info tx_minfo
;
137 struct list_head node
;
141 #define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl)
142 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
145 * SCMI specification requires all parameters, message headers, return
146 * arguments or any protocol data to be expressed in little endian
149 struct scmi_shared_mem
{
151 __le32 channel_status
;
152 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
153 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
156 #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
162 static const int scmi_linux_errmap
[] = {
163 /* better than switch case as long as return value is continuous */
164 0, /* SCMI_SUCCESS */
165 -EOPNOTSUPP
, /* SCMI_ERR_SUPPORT */
166 -EINVAL
, /* SCMI_ERR_PARAM */
167 -EACCES
, /* SCMI_ERR_ACCESS */
168 -ENOENT
, /* SCMI_ERR_ENTRY */
169 -ERANGE
, /* SCMI_ERR_RANGE */
170 -EBUSY
, /* SCMI_ERR_BUSY */
171 -ECOMM
, /* SCMI_ERR_COMMS */
172 -EIO
, /* SCMI_ERR_GENERIC */
173 -EREMOTEIO
, /* SCMI_ERR_HARDWARE */
174 -EPROTO
, /* SCMI_ERR_PROTOCOL */
177 static inline int scmi_to_linux_errno(int errno
)
179 if (errno
< SCMI_SUCCESS
&& errno
> SCMI_ERR_MAX
)
180 return scmi_linux_errmap
[-errno
];
185 * scmi_dump_header_dbg() - Helper to dump a message header.
187 * @dev: Device pointer corresponding to the SCMI entity
188 * @hdr: pointer to header.
190 static inline void scmi_dump_header_dbg(struct device
*dev
,
191 struct scmi_msg_hdr
*hdr
)
193 dev_dbg(dev
, "Message ID: %x Sequence ID: %x Protocol: %x\n",
194 hdr
->id
, hdr
->seq
, hdr
->protocol_id
);
197 static void scmi_fetch_response(struct scmi_xfer
*xfer
,
198 struct scmi_shared_mem __iomem
*mem
)
200 xfer
->hdr
.status
= ioread32(mem
->msg_payload
);
201 /* Skip the length of header and status in payload area i.e 8 bytes */
202 xfer
->rx
.len
= min_t(size_t, xfer
->rx
.len
, ioread32(&mem
->length
) - 8);
204 /* Take a copy to the rx buffer.. */
205 memcpy_fromio(xfer
->rx
.buf
, mem
->msg_payload
+ 4, xfer
->rx
.len
);
209 * pack_scmi_header() - packs and returns 32-bit header
211 * @hdr: pointer to header containing all the information on message id,
212 * protocol id and sequence id.
214 * Return: 32-bit packed message header to be sent to the platform.
216 static inline u32
pack_scmi_header(struct scmi_msg_hdr
*hdr
)
218 return FIELD_PREP(MSG_ID_MASK
, hdr
->id
) |
219 FIELD_PREP(MSG_TOKEN_ID_MASK
, hdr
->seq
) |
220 FIELD_PREP(MSG_PROTOCOL_ID_MASK
, hdr
->protocol_id
);
224 * unpack_scmi_header() - unpacks and records message and protocol id
226 * @msg_hdr: 32-bit packed message header sent from the platform
227 * @hdr: pointer to header to fetch message and protocol id.
229 static inline void unpack_scmi_header(u32 msg_hdr
, struct scmi_msg_hdr
*hdr
)
231 hdr
->id
= MSG_XTRACT_ID(msg_hdr
);
232 hdr
->protocol_id
= MSG_XTRACT_PROT_ID(msg_hdr
);
236 * scmi_tx_prepare() - mailbox client callback to prepare for the transfer
238 * @cl: client pointer
239 * @m: mailbox message
241 * This function prepares the shared memory which contains the header and the
244 static void scmi_tx_prepare(struct mbox_client
*cl
, void *m
)
246 struct scmi_xfer
*t
= m
;
247 struct scmi_chan_info
*cinfo
= client_to_scmi_chan_info(cl
);
248 struct scmi_shared_mem __iomem
*mem
= cinfo
->payload
;
251 * Ideally channel must be free by now unless OS timeout last
252 * request and platform continued to process the same, wait
253 * until it releases the shared memory, otherwise we may endup
254 * overwriting its response with new message payload or vice-versa
256 spin_until_cond(ioread32(&mem
->channel_status
) &
257 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE
);
258 /* Mark channel busy + clear error */
259 iowrite32(0x0, &mem
->channel_status
);
260 iowrite32(t
->hdr
.poll_completion
? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED
,
262 iowrite32(sizeof(mem
->msg_header
) + t
->tx
.len
, &mem
->length
);
263 iowrite32(pack_scmi_header(&t
->hdr
), &mem
->msg_header
);
265 memcpy_toio(mem
->msg_payload
, t
->tx
.buf
, t
->tx
.len
);
269 * scmi_xfer_get() - Allocate one message
271 * @handle: Pointer to SCMI entity handle
272 * @minfo: Pointer to Tx/Rx Message management info based on channel type
274 * Helper function which is used by various message functions that are
275 * exposed to clients of this driver for allocating a message traffic event.
277 * This function can sleep depending on pending requests already in the system
278 * for the SCMI entity. Further, this also holds a spinlock to maintain
279 * integrity of internal data structures.
281 * Return: 0 if all went fine, else corresponding error.
283 static struct scmi_xfer
*scmi_xfer_get(const struct scmi_handle
*handle
,
284 struct scmi_xfers_info
*minfo
)
287 struct scmi_xfer
*xfer
;
288 unsigned long flags
, bit_pos
;
289 struct scmi_info
*info
= handle_to_scmi_info(handle
);
291 /* Keep the locked section as small as possible */
292 spin_lock_irqsave(&minfo
->xfer_lock
, flags
);
293 bit_pos
= find_first_zero_bit(minfo
->xfer_alloc_table
,
294 info
->desc
->max_msg
);
295 if (bit_pos
== info
->desc
->max_msg
) {
296 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
297 return ERR_PTR(-ENOMEM
);
299 set_bit(bit_pos
, minfo
->xfer_alloc_table
);
300 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
304 xfer
= &minfo
->xfer_block
[xfer_id
];
305 xfer
->hdr
.seq
= xfer_id
;
306 reinit_completion(&xfer
->done
);
312 * __scmi_xfer_put() - Release a message
314 * @minfo: Pointer to Tx/Rx Message management info based on channel type
315 * @xfer: message that was reserved by scmi_xfer_get
317 * This holds a spinlock to maintain integrity of internal data structures.
320 __scmi_xfer_put(struct scmi_xfers_info
*minfo
, struct scmi_xfer
*xfer
)
325 * Keep the locked section as small as possible
326 * NOTE: we might escape with smp_mb and no lock here..
327 * but just be conservative and symmetric.
329 spin_lock_irqsave(&minfo
->xfer_lock
, flags
);
330 clear_bit(xfer
->hdr
.seq
, minfo
->xfer_alloc_table
);
331 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
335 * scmi_rx_callback() - mailbox client callback for receive messages
337 * @cl: client pointer
338 * @m: mailbox message
340 * Processes one received message to appropriate transfer information and
341 * signals completion of the transfer.
343 * NOTE: This function will be invoked in IRQ context, hence should be
344 * as optimal as possible.
346 static void scmi_rx_callback(struct mbox_client
*cl
, void *m
)
351 struct scmi_xfer
*xfer
;
352 struct scmi_chan_info
*cinfo
= client_to_scmi_chan_info(cl
);
353 struct device
*dev
= cinfo
->dev
;
354 struct scmi_info
*info
= handle_to_scmi_info(cinfo
->handle
);
355 struct scmi_xfers_info
*minfo
= &info
->tx_minfo
;
356 struct scmi_shared_mem __iomem
*mem
= cinfo
->payload
;
358 msg_hdr
= ioread32(&mem
->msg_header
);
359 msg_type
= MSG_XTRACT_TYPE(msg_hdr
);
360 xfer_id
= MSG_XTRACT_TOKEN(msg_hdr
);
362 if (msg_type
== MSG_TYPE_NOTIFICATION
)
363 return; /* Notifications not yet supported */
365 /* Are we even expecting this? */
366 if (!test_bit(xfer_id
, minfo
->xfer_alloc_table
)) {
367 dev_err(dev
, "message for %d is not expected!\n", xfer_id
);
371 xfer
= &minfo
->xfer_block
[xfer_id
];
373 scmi_dump_header_dbg(dev
, &xfer
->hdr
);
375 scmi_fetch_response(xfer
, mem
);
377 if (msg_type
== MSG_TYPE_DELAYED_RESP
)
378 complete(xfer
->async_done
);
380 complete(&xfer
->done
);
384 * scmi_xfer_put() - Release a transmit message
386 * @handle: Pointer to SCMI entity handle
387 * @xfer: message that was reserved by scmi_xfer_get
389 void scmi_xfer_put(const struct scmi_handle
*handle
, struct scmi_xfer
*xfer
)
391 struct scmi_info
*info
= handle_to_scmi_info(handle
);
393 __scmi_xfer_put(&info
->tx_minfo
, xfer
);
397 scmi_xfer_poll_done(const struct scmi_chan_info
*cinfo
, struct scmi_xfer
*xfer
)
399 struct scmi_shared_mem __iomem
*mem
= cinfo
->payload
;
400 u16 xfer_id
= MSG_XTRACT_TOKEN(ioread32(&mem
->msg_header
));
402 if (xfer
->hdr
.seq
!= xfer_id
)
405 return ioread32(&mem
->channel_status
) &
406 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR
|
407 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE
);
410 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
412 static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info
*cinfo
,
413 struct scmi_xfer
*xfer
, ktime_t stop
)
415 ktime_t __cur
= ktime_get();
417 return scmi_xfer_poll_done(cinfo
, xfer
) || ktime_after(__cur
, stop
);
421 * scmi_do_xfer() - Do one transfer
423 * @handle: Pointer to SCMI entity handle
424 * @xfer: Transfer to initiate and wait for response
426 * Return: -ETIMEDOUT in case of no response, if transmit error,
427 * return corresponding error, else if all goes well,
430 int scmi_do_xfer(const struct scmi_handle
*handle
, struct scmi_xfer
*xfer
)
434 struct scmi_info
*info
= handle_to_scmi_info(handle
);
435 struct device
*dev
= info
->dev
;
436 struct scmi_chan_info
*cinfo
;
438 cinfo
= idr_find(&info
->tx_idr
, xfer
->hdr
.protocol_id
);
439 if (unlikely(!cinfo
))
442 ret
= mbox_send_message(cinfo
->chan
, xfer
);
444 dev_dbg(dev
, "mbox send fail %d\n", ret
);
448 /* mbox_send_message returns non-negative value on success, so reset */
451 if (xfer
->hdr
.poll_completion
) {
452 ktime_t stop
= ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS
);
454 spin_until_cond(scmi_xfer_done_no_timeout(cinfo
, xfer
, stop
));
456 if (ktime_before(ktime_get(), stop
))
457 scmi_fetch_response(xfer
, cinfo
->payload
);
461 /* And we wait for the response. */
462 timeout
= msecs_to_jiffies(info
->desc
->max_rx_timeout_ms
);
463 if (!wait_for_completion_timeout(&xfer
->done
, timeout
)) {
464 dev_err(dev
, "mbox timed out in resp(caller: %pS)\n",
470 if (!ret
&& xfer
->hdr
.status
)
471 ret
= scmi_to_linux_errno(xfer
->hdr
.status
);
474 * NOTE: we might prefer not to need the mailbox ticker to manage the
475 * transfer queueing since the protocol layer queues things by itself.
476 * Unfortunately, we have to kick the mailbox framework after we have
477 * received our message.
479 mbox_client_txdone(cinfo
->chan
, ret
);
484 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
487 * scmi_do_xfer_with_response() - Do one transfer and wait until the delayed
488 * response is received
490 * @handle: Pointer to SCMI entity handle
491 * @xfer: Transfer to initiate and wait for response
493 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
494 * return corresponding error, else if all goes well, return 0.
496 int scmi_do_xfer_with_response(const struct scmi_handle
*handle
,
497 struct scmi_xfer
*xfer
)
499 int ret
, timeout
= msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT
);
500 DECLARE_COMPLETION_ONSTACK(async_response
);
502 xfer
->async_done
= &async_response
;
504 ret
= scmi_do_xfer(handle
, xfer
);
505 if (!ret
&& !wait_for_completion_timeout(xfer
->async_done
, timeout
))
508 xfer
->async_done
= NULL
;
513 * scmi_xfer_get_init() - Allocate and initialise one message for transmit
515 * @handle: Pointer to SCMI entity handle
516 * @msg_id: Message identifier
517 * @prot_id: Protocol identifier for the message
518 * @tx_size: transmit message size
519 * @rx_size: receive message size
520 * @p: pointer to the allocated and initialised message
522 * This function allocates the message using @scmi_xfer_get and
523 * initialise the header.
525 * Return: 0 if all went fine with @p pointing to message, else
526 * corresponding error.
528 int scmi_xfer_get_init(const struct scmi_handle
*handle
, u8 msg_id
, u8 prot_id
,
529 size_t tx_size
, size_t rx_size
, struct scmi_xfer
**p
)
532 struct scmi_xfer
*xfer
;
533 struct scmi_info
*info
= handle_to_scmi_info(handle
);
534 struct scmi_xfers_info
*minfo
= &info
->tx_minfo
;
535 struct device
*dev
= info
->dev
;
537 /* Ensure we have sane transfer sizes */
538 if (rx_size
> info
->desc
->max_msg_size
||
539 tx_size
> info
->desc
->max_msg_size
)
542 xfer
= scmi_xfer_get(handle
, minfo
);
545 dev_err(dev
, "failed to get free message slot(%d)\n", ret
);
549 xfer
->tx
.len
= tx_size
;
550 xfer
->rx
.len
= rx_size
? : info
->desc
->max_msg_size
;
551 xfer
->hdr
.id
= msg_id
;
552 xfer
->hdr
.protocol_id
= prot_id
;
553 xfer
->hdr
.poll_completion
= false;
561 * scmi_version_get() - command to get the revision of the SCMI entity
563 * @handle: Pointer to SCMI entity handle
564 * @protocol: Protocol identifier for the message
565 * @version: Holds returned version of protocol.
567 * Updates the SCMI information in the internal data structure.
569 * Return: 0 if all went fine, else return appropriate error.
571 int scmi_version_get(const struct scmi_handle
*handle
, u8 protocol
,
578 ret
= scmi_xfer_get_init(handle
, PROTOCOL_VERSION
, protocol
, 0,
579 sizeof(*version
), &t
);
583 ret
= scmi_do_xfer(handle
, t
);
585 rev_info
= t
->rx
.buf
;
586 *version
= le32_to_cpu(*rev_info
);
589 scmi_xfer_put(handle
, t
);
593 void scmi_setup_protocol_implemented(const struct scmi_handle
*handle
,
596 struct scmi_info
*info
= handle_to_scmi_info(handle
);
598 info
->protocols_imp
= prot_imp
;
602 scmi_is_protocol_implemented(const struct scmi_handle
*handle
, u8 prot_id
)
605 struct scmi_info
*info
= handle_to_scmi_info(handle
);
607 if (!info
->protocols_imp
)
610 for (i
= 0; i
< MAX_PROTOCOLS_IMP
; i
++)
611 if (info
->protocols_imp
[i
] == prot_id
)
617 * scmi_handle_get() - Get the SCMI handle for a device
619 * @dev: pointer to device for which we want SCMI handle
621 * NOTE: The function does not track individual clients of the framework
622 * and is expected to be maintained by caller of SCMI protocol library.
623 * scmi_handle_put must be balanced with successful scmi_handle_get
625 * Return: pointer to handle if successful, NULL on error
627 struct scmi_handle
*scmi_handle_get(struct device
*dev
)
630 struct scmi_info
*info
;
631 struct scmi_handle
*handle
= NULL
;
633 mutex_lock(&scmi_list_mutex
);
634 list_for_each(p
, &scmi_list
) {
635 info
= list_entry(p
, struct scmi_info
, node
);
636 if (dev
->parent
== info
->dev
) {
637 handle
= &info
->handle
;
642 mutex_unlock(&scmi_list_mutex
);
648 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
650 * @handle: handle acquired by scmi_handle_get
652 * NOTE: The function does not track individual clients of the framework
653 * and is expected to be maintained by caller of SCMI protocol library.
654 * scmi_handle_put must be balanced with successful scmi_handle_get
656 * Return: 0 is successfully released
657 * if null was passed, it returns -EINVAL;
659 int scmi_handle_put(const struct scmi_handle
*handle
)
661 struct scmi_info
*info
;
666 info
= handle_to_scmi_info(handle
);
667 mutex_lock(&scmi_list_mutex
);
668 if (!WARN_ON(!info
->users
))
670 mutex_unlock(&scmi_list_mutex
);
675 static int scmi_xfer_info_init(struct scmi_info
*sinfo
)
678 struct scmi_xfer
*xfer
;
679 struct device
*dev
= sinfo
->dev
;
680 const struct scmi_desc
*desc
= sinfo
->desc
;
681 struct scmi_xfers_info
*info
= &sinfo
->tx_minfo
;
683 /* Pre-allocated messages, no more than what hdr.seq can support */
684 if (WARN_ON(desc
->max_msg
>= MSG_TOKEN_MAX
)) {
685 dev_err(dev
, "Maximum message of %d exceeds supported %ld\n",
686 desc
->max_msg
, MSG_TOKEN_MAX
);
690 info
->xfer_block
= devm_kcalloc(dev
, desc
->max_msg
,
691 sizeof(*info
->xfer_block
), GFP_KERNEL
);
692 if (!info
->xfer_block
)
695 info
->xfer_alloc_table
= devm_kcalloc(dev
, BITS_TO_LONGS(desc
->max_msg
),
696 sizeof(long), GFP_KERNEL
);
697 if (!info
->xfer_alloc_table
)
700 /* Pre-initialize the buffer pointer to pre-allocated buffers */
701 for (i
= 0, xfer
= info
->xfer_block
; i
< desc
->max_msg
; i
++, xfer
++) {
702 xfer
->rx
.buf
= devm_kcalloc(dev
, sizeof(u8
), desc
->max_msg_size
,
707 xfer
->tx
.buf
= xfer
->rx
.buf
;
708 init_completion(&xfer
->done
);
711 spin_lock_init(&info
->xfer_lock
);
716 static int scmi_mailbox_check(struct device_node
*np
, int idx
)
718 return of_parse_phandle_with_args(np
, "mboxes", "#mbox-cells",
722 static int scmi_mbox_chan_setup(struct scmi_info
*info
, struct device
*dev
,
723 int prot_id
, bool tx
)
727 resource_size_t size
;
728 struct device_node
*shmem
, *np
= dev
->of_node
;
729 struct scmi_chan_info
*cinfo
;
730 struct mbox_client
*cl
;
732 const char *desc
= tx
? "Tx" : "Rx";
734 /* Transmit channel is first entry i.e. index 0 */
736 idr
= tx
? &info
->tx_idr
: &info
->rx_idr
;
738 if (scmi_mailbox_check(np
, idx
)) {
739 cinfo
= idr_find(idr
, SCMI_PROTOCOL_BASE
);
740 if (unlikely(!cinfo
)) /* Possible only if platform has no Rx */
745 cinfo
= devm_kzalloc(info
->dev
, sizeof(*cinfo
), GFP_KERNEL
);
753 cl
->rx_callback
= scmi_rx_callback
;
754 cl
->tx_prepare
= tx
? scmi_tx_prepare
: NULL
;
755 cl
->tx_block
= false;
756 cl
->knows_txdone
= tx
;
758 shmem
= of_parse_phandle(np
, "shmem", idx
);
759 ret
= of_address_to_resource(shmem
, 0, &res
);
762 dev_err(dev
, "failed to get SCMI %s payload memory\n", desc
);
766 size
= resource_size(&res
);
767 cinfo
->payload
= devm_ioremap(info
->dev
, res
.start
, size
);
768 if (!cinfo
->payload
) {
769 dev_err(dev
, "failed to ioremap SCMI %s payload\n", desc
);
770 return -EADDRNOTAVAIL
;
773 cinfo
->chan
= mbox_request_channel(cl
, idx
);
774 if (IS_ERR(cinfo
->chan
)) {
775 ret
= PTR_ERR(cinfo
->chan
);
776 if (ret
!= -EPROBE_DEFER
)
777 dev_err(dev
, "failed to request SCMI %s mailbox\n",
783 ret
= idr_alloc(idr
, cinfo
, prot_id
, prot_id
+ 1, GFP_KERNEL
);
784 if (ret
!= prot_id
) {
785 dev_err(dev
, "unable to allocate SCMI idr slot err %d\n", ret
);
789 cinfo
->handle
= &info
->handle
;
794 scmi_mbox_txrx_setup(struct scmi_info
*info
, struct device
*dev
, int prot_id
)
796 int ret
= scmi_mbox_chan_setup(info
, dev
, prot_id
, true);
798 if (!ret
) /* Rx is optional, hence no error check */
799 scmi_mbox_chan_setup(info
, dev
, prot_id
, false);
805 scmi_create_protocol_device(struct device_node
*np
, struct scmi_info
*info
,
808 struct scmi_device
*sdev
;
810 sdev
= scmi_device_create(np
, info
->dev
, prot_id
);
812 dev_err(info
->dev
, "failed to create %d protocol device\n",
817 if (scmi_mbox_txrx_setup(info
, &sdev
->dev
, prot_id
)) {
818 dev_err(&sdev
->dev
, "failed to setup transport\n");
819 scmi_device_destroy(sdev
);
823 /* setup handle now as the transport is ready */
824 scmi_set_handle(sdev
);
827 static int scmi_probe(struct platform_device
*pdev
)
830 struct scmi_handle
*handle
;
831 const struct scmi_desc
*desc
;
832 struct scmi_info
*info
;
833 struct device
*dev
= &pdev
->dev
;
834 struct device_node
*child
, *np
= dev
->of_node
;
836 /* Only mailbox method supported, check for the presence of one */
837 if (scmi_mailbox_check(np
, 0)) {
838 dev_err(dev
, "no mailbox found in %pOF\n", np
);
842 desc
= of_device_get_match_data(dev
);
846 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
852 INIT_LIST_HEAD(&info
->node
);
854 ret
= scmi_xfer_info_init(info
);
858 platform_set_drvdata(pdev
, info
);
859 idr_init(&info
->tx_idr
);
860 idr_init(&info
->rx_idr
);
862 handle
= &info
->handle
;
863 handle
->dev
= info
->dev
;
864 handle
->version
= &info
->version
;
866 ret
= scmi_mbox_txrx_setup(info
, dev
, SCMI_PROTOCOL_BASE
);
870 ret
= scmi_base_protocol_init(handle
);
872 dev_err(dev
, "unable to communicate with SCMI(%d)\n", ret
);
876 mutex_lock(&scmi_list_mutex
);
877 list_add_tail(&info
->node
, &scmi_list
);
878 mutex_unlock(&scmi_list_mutex
);
880 for_each_available_child_of_node(np
, child
) {
883 if (of_property_read_u32(child
, "reg", &prot_id
))
886 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK
, prot_id
))
887 dev_err(dev
, "Out of range protocol %d\n", prot_id
);
889 if (!scmi_is_protocol_implemented(handle
, prot_id
)) {
890 dev_err(dev
, "SCMI protocol %d not implemented\n",
895 scmi_create_protocol_device(child
, info
, prot_id
);
901 static int scmi_mbox_free_channel(int id
, void *p
, void *data
)
903 struct scmi_chan_info
*cinfo
= p
;
904 struct idr
*idr
= data
;
906 if (!IS_ERR_OR_NULL(cinfo
->chan
)) {
907 mbox_free_channel(cinfo
->chan
);
916 static int scmi_remove(struct platform_device
*pdev
)
919 struct scmi_info
*info
= platform_get_drvdata(pdev
);
920 struct idr
*idr
= &info
->tx_idr
;
922 mutex_lock(&scmi_list_mutex
);
926 list_del(&info
->node
);
927 mutex_unlock(&scmi_list_mutex
);
932 /* Safe to free channels since no more users */
933 ret
= idr_for_each(idr
, scmi_mbox_free_channel
, idr
);
934 idr_destroy(&info
->tx_idr
);
937 ret
= idr_for_each(idr
, scmi_mbox_free_channel
, idr
);
938 idr_destroy(&info
->rx_idr
);
943 static const struct scmi_desc scmi_generic_desc
= {
944 .max_rx_timeout_ms
= 30, /* We may increase this if required */
945 .max_msg
= 20, /* Limited by MBOX_TX_QUEUE_LEN */
949 /* Each compatible listed below must have descriptor associated with it */
950 static const struct of_device_id scmi_of_match
[] = {
951 { .compatible
= "arm,scmi", .data
= &scmi_generic_desc
},
955 MODULE_DEVICE_TABLE(of
, scmi_of_match
);
957 static struct platform_driver scmi_driver
= {
960 .of_match_table
= scmi_of_match
,
963 .remove
= scmi_remove
,
966 module_platform_driver(scmi_driver
);
968 MODULE_ALIAS("platform: arm-scmi");
969 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
970 MODULE_DESCRIPTION("ARM SCMI protocol driver");
971 MODULE_LICENSE("GPL v2");