Linux 5.5-rc1
[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 *
14 * Copyright (C) 2018 ARM Ltd.
15 */
16
17#include <linux/bitmap.h>
18#include <linux/export.h>
19#include <linux/io.h>
20#include <linux/kernel.h>
d4c3751a 21#include <linux/ktime.h>
aa4f886f
SH
22#include <linux/mailbox_client.h>
23#include <linux/module.h>
24#include <linux/of_address.h>
25#include <linux/of_device.h>
d4c3751a 26#include <linux/processor.h>
aa4f886f
SH
27#include <linux/semaphore.h>
28#include <linux/slab.h>
29
30#include "common.h"
31
354b2e36 32#define MSG_ID_MASK GENMASK(7, 0)
22d1f761 33#define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
354b2e36 34#define MSG_TYPE_MASK GENMASK(9, 8)
22d1f761
SH
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
354b2e36 39#define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
22d1f761 40#define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
354b2e36
SH
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)
aa4f886f
SH
44
45enum 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 */
57 SCMI_ERR_MAX
58};
59
1baf47c2 60/* List of all SCMI devices active in system */
aa4f886f
SH
61static LIST_HEAD(scmi_list);
62/* Protection for the entire list */
63static DEFINE_MUTEX(scmi_list_mutex);
64
65/**
66 * struct scmi_xfers_info - Structure to manage transfer information
67 *
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
73 */
74struct scmi_xfers_info {
75 struct scmi_xfer *xfer_block;
76 unsigned long *xfer_alloc_table;
aa4f886f
SH
77 spinlock_t xfer_lock;
78};
79
80/**
81 * struct scmi_desc - Description of SoC integration
82 *
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.
87 */
88struct scmi_desc {
89 int max_rx_timeout_ms;
90 int max_msg;
91 int max_msg_size;
92};
93
fbc4d81a 94/**
c29a6289 95 * struct scmi_chan_info - Structure representing a SCMI channel information
fbc4d81a
SH
96 *
97 * @cl: Mailbox Client
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
101 * channel
1baf47c2 102 * @handle: Pointer to SCMI entity handle
fbc4d81a
SH
103 */
104struct scmi_chan_info {
105 struct mbox_client cl;
106 struct mbox_chan *chan;
107 void __iomem *payload;
108 struct device *dev;
907b6d14 109 struct scmi_handle *handle;
fbc4d81a
SH
110};
111
aa4f886f 112/**
1baf47c2 113 * struct scmi_info - Structure representing a SCMI instance
aa4f886f
SH
114 *
115 * @dev: Device pointer
116 * @desc: SoC description for this instance
117 * @handle: Instance of SCMI handle to send to clients
b6f20ff8
SH
118 * @version: SCMI revision information containing protocol version,
119 * implementation version and (sub-)vendor identification.
38c927fb 120 * @tx_minfo: Universal Transmit Message management info
3748daf7 121 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
46cc7c28 122 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
1baf47c2 123 * @protocols_imp: List of protocols implemented, currently maximum of
b6f20ff8 124 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
1baf47c2 125 * @node: List head
aa4f886f
SH
126 * @users: Number of users of this instance
127 */
128struct scmi_info {
129 struct device *dev;
130 const struct scmi_desc *desc;
b6f20ff8 131 struct scmi_revision_info version;
aa4f886f 132 struct scmi_handle handle;
38c927fb 133 struct scmi_xfers_info tx_minfo;
907b6d14 134 struct idr tx_idr;
46cc7c28 135 struct idr rx_idr;
b6f20ff8 136 u8 *protocols_imp;
aa4f886f
SH
137 struct list_head node;
138 int users;
139};
140
fbc4d81a 141#define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl)
aa4f886f
SH
142#define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
143
144/*
145 * SCMI specification requires all parameters, message headers, return
146 * arguments or any protocol data to be expressed in little endian
147 * format only.
148 */
149struct scmi_shared_mem {
150 __le32 reserved;
151 __le32 channel_status;
152#define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
153#define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
154 __le32 reserved1[2];
155 __le32 flags;
156#define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
157 __le32 length;
158 __le32 msg_header;
159 u8 msg_payload[0];
160};
161
162static 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 */
175};
176
177static inline int scmi_to_linux_errno(int errno)
178{
179 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
180 return scmi_linux_errmap[-errno];
181 return -EIO;
182}
183
184/**
185 * scmi_dump_header_dbg() - Helper to dump a message header.
186 *
187 * @dev: Device pointer corresponding to the SCMI entity
188 * @hdr: pointer to header.
189 */
190static inline void scmi_dump_header_dbg(struct device *dev,
191 struct scmi_msg_hdr *hdr)
192{
5b65af8f 193 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
aa4f886f
SH
194 hdr->id, hdr->seq, hdr->protocol_id);
195}
196
197static void scmi_fetch_response(struct scmi_xfer *xfer,
198 struct scmi_shared_mem __iomem *mem)
199{
200 xfer->hdr.status = ioread32(mem->msg_payload);
c29a6289 201 /* Skip the length of header and status in payload area i.e 8 bytes */
aa4f886f
SH
202 xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8);
203
204 /* Take a copy to the rx buffer.. */
205 memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len);
206}
207
aa4f886f
SH
208/**
209 * pack_scmi_header() - packs and returns 32-bit header
210 *
211 * @hdr: pointer to header containing all the information on message id,
212 * protocol id and sequence id.
1baf47c2 213 *
5b65af8f 214 * Return: 32-bit packed message header to be sent to the platform.
aa4f886f
SH
215 */
216static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
217{
354b2e36
SH
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);
aa4f886f
SH
221}
222
22d1f761
SH
223/**
224 * unpack_scmi_header() - unpacks and records message and protocol id
225 *
226 * @msg_hdr: 32-bit packed message header sent from the platform
227 * @hdr: pointer to header to fetch message and protocol id.
228 */
229static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
230{
231 hdr->id = MSG_XTRACT_ID(msg_hdr);
232 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
233}
234
aa4f886f
SH
235/**
236 * scmi_tx_prepare() - mailbox client callback to prepare for the transfer
237 *
238 * @cl: client pointer
239 * @m: mailbox message
240 *
241 * This function prepares the shared memory which contains the header and the
242 * payload.
243 */
244static void scmi_tx_prepare(struct mbox_client *cl, void *m)
245{
246 struct scmi_xfer *t = m;
fbc4d81a
SH
247 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
248 struct scmi_shared_mem __iomem *mem = cinfo->payload;
aa4f886f 249
9dc34d63
SH
250 /*
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
255 */
256 spin_until_cond(ioread32(&mem->channel_status) &
257 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
aa4f886f
SH
258 /* Mark channel busy + clear error */
259 iowrite32(0x0, &mem->channel_status);
260 iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
261 &mem->flags);
262 iowrite32(sizeof(mem->msg_header) + t->tx.len, &mem->length);
263 iowrite32(pack_scmi_header(&t->hdr), &mem->msg_header);
264 if (t->tx.buf)
265 memcpy_toio(mem->msg_payload, t->tx.buf, t->tx.len);
266}
267
268/**
1baf47c2 269 * scmi_xfer_get() - Allocate one message
aa4f886f 270 *
1baf47c2 271 * @handle: Pointer to SCMI entity handle
38c927fb 272 * @minfo: Pointer to Tx/Rx Message management info based on channel type
aa4f886f 273 *
5b65af8f 274 * Helper function which is used by various message functions that are
aa4f886f
SH
275 * exposed to clients of this driver for allocating a message traffic event.
276 *
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.
280 *
281 * Return: 0 if all went fine, else corresponding error.
282 */
38c927fb
SH
283static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
284 struct scmi_xfers_info *minfo)
aa4f886f
SH
285{
286 u16 xfer_id;
287 struct scmi_xfer *xfer;
288 unsigned long flags, bit_pos;
289 struct scmi_info *info = handle_to_scmi_info(handle);
aa4f886f
SH
290
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);
298 }
299 set_bit(bit_pos, minfo->xfer_alloc_table);
300 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
301
302 xfer_id = bit_pos;
303
304 xfer = &minfo->xfer_block[xfer_id];
305 xfer->hdr.seq = xfer_id;
306 reinit_completion(&xfer->done);
307
308 return xfer;
309}
310
311/**
38c927fb 312 * __scmi_xfer_put() - Release a message
aa4f886f 313 *
38c927fb 314 * @minfo: Pointer to Tx/Rx Message management info based on channel type
1baf47c2 315 * @xfer: message that was reserved by scmi_xfer_get
aa4f886f
SH
316 *
317 * This holds a spinlock to maintain integrity of internal data structures.
318 */
38c927fb
SH
319static void
320__scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
aa4f886f
SH
321{
322 unsigned long flags;
aa4f886f
SH
323
324 /*
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.
328 */
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);
332}
333
2747a967
SH
334/**
335 * scmi_rx_callback() - mailbox client callback for receive messages
336 *
337 * @cl: client pointer
338 * @m: mailbox message
339 *
340 * Processes one received message to appropriate transfer information and
341 * signals completion of the transfer.
342 *
343 * NOTE: This function will be invoked in IRQ context, hence should be
344 * as optimal as possible.
345 */
346static void scmi_rx_callback(struct mbox_client *cl, void *m)
347{
58ecdf03
SH
348 u8 msg_type;
349 u32 msg_hdr;
2747a967
SH
350 u16 xfer_id;
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);
38c927fb 355 struct scmi_xfers_info *minfo = &info->tx_minfo;
2747a967
SH
356 struct scmi_shared_mem __iomem *mem = cinfo->payload;
357
58ecdf03
SH
358 msg_hdr = ioread32(&mem->msg_header);
359 msg_type = MSG_XTRACT_TYPE(msg_hdr);
360 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
361
362 if (msg_type == MSG_TYPE_NOTIFICATION)
363 return; /* Notifications not yet supported */
2747a967
SH
364
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);
368 return;
369 }
370
371 xfer = &minfo->xfer_block[xfer_id];
372
373 scmi_dump_header_dbg(dev, &xfer->hdr);
374
375 scmi_fetch_response(xfer, mem);
58ecdf03
SH
376
377 if (msg_type == MSG_TYPE_DELAYED_RESP)
378 complete(xfer->async_done);
379 else
380 complete(&xfer->done);
2747a967
SH
381}
382
38c927fb
SH
383/**
384 * scmi_xfer_put() - Release a transmit message
385 *
386 * @handle: Pointer to SCMI entity handle
387 * @xfer: message that was reserved by scmi_xfer_get
388 */
389void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
390{
391 struct scmi_info *info = handle_to_scmi_info(handle);
392
393 __scmi_xfer_put(&info->tx_minfo, xfer);
394}
395
d4c3751a 396static bool
fbc4d81a 397scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
d4c3751a 398{
fbc4d81a
SH
399 struct scmi_shared_mem __iomem *mem = cinfo->payload;
400 u16 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
d4c3751a
SH
401
402 if (xfer->hdr.seq != xfer_id)
403 return false;
404
fbc4d81a 405 return ioread32(&mem->channel_status) &
d4c3751a
SH
406 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
407 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
408}
409
410#define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
411
fbc4d81a 412static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info *cinfo,
d4c3751a
SH
413 struct scmi_xfer *xfer, ktime_t stop)
414{
415 ktime_t __cur = ktime_get();
416
fbc4d81a 417 return scmi_xfer_poll_done(cinfo, xfer) || ktime_after(__cur, stop);
d4c3751a
SH
418}
419
aa4f886f
SH
420/**
421 * scmi_do_xfer() - Do one transfer
422 *
1baf47c2 423 * @handle: Pointer to SCMI entity handle
aa4f886f
SH
424 * @xfer: Transfer to initiate and wait for response
425 *
426 * Return: -ETIMEDOUT in case of no response, if transmit error,
1baf47c2
SH
427 * return corresponding error, else if all goes well,
428 * return 0.
aa4f886f
SH
429 */
430int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer)
431{
432 int ret;
433 int timeout;
434 struct scmi_info *info = handle_to_scmi_info(handle);
435 struct device *dev = info->dev;
907b6d14
SH
436 struct scmi_chan_info *cinfo;
437
438 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
439 if (unlikely(!cinfo))
440 return -EINVAL;
aa4f886f 441
fbc4d81a 442 ret = mbox_send_message(cinfo->chan, xfer);
aa4f886f
SH
443 if (ret < 0) {
444 dev_dbg(dev, "mbox send fail %d\n", ret);
445 return ret;
446 }
447
448 /* mbox_send_message returns non-negative value on success, so reset */
449 ret = 0;
450
d4c3751a
SH
451 if (xfer->hdr.poll_completion) {
452 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
453
fbc4d81a 454 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
d4c3751a
SH
455
456 if (ktime_before(ktime_get(), stop))
fbc4d81a 457 scmi_fetch_response(xfer, cinfo->payload);
d4c3751a
SH
458 else
459 ret = -ETIMEDOUT;
460 } else {
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",
465 (void *)_RET_IP_);
466 ret = -ETIMEDOUT;
467 }
aa4f886f 468 }
d4c3751a
SH
469
470 if (!ret && xfer->hdr.status)
471 ret = scmi_to_linux_errno(xfer->hdr.status);
472
aa4f886f
SH
473 /*
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.
478 */
fbc4d81a 479 mbox_client_txdone(cinfo->chan, ret);
aa4f886f
SH
480
481 return ret;
482}
483
58ecdf03
SH
484#define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
485
486/**
487 * scmi_do_xfer_with_response() - Do one transfer and wait until the delayed
488 * response is received
489 *
490 * @handle: Pointer to SCMI entity handle
491 * @xfer: Transfer to initiate and wait for response
492 *
493 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
494 * return corresponding error, else if all goes well, return 0.
495 */
496int scmi_do_xfer_with_response(const struct scmi_handle *handle,
497 struct scmi_xfer *xfer)
498{
499 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
500 DECLARE_COMPLETION_ONSTACK(async_response);
501
502 xfer->async_done = &async_response;
503
504 ret = scmi_do_xfer(handle, xfer);
505 if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout))
506 ret = -ETIMEDOUT;
507
508 xfer->async_done = NULL;
509 return ret;
510}
511
aa4f886f 512/**
38c927fb 513 * scmi_xfer_get_init() - Allocate and initialise one message for transmit
aa4f886f 514 *
1baf47c2 515 * @handle: Pointer to SCMI entity handle
aa4f886f 516 * @msg_id: Message identifier
1baf47c2 517 * @prot_id: Protocol identifier for the message
aa4f886f
SH
518 * @tx_size: transmit message size
519 * @rx_size: receive message size
520 * @p: pointer to the allocated and initialised message
521 *
14e297b3 522 * This function allocates the message using @scmi_xfer_get and
aa4f886f
SH
523 * initialise the header.
524 *
525 * Return: 0 if all went fine with @p pointing to message, else
526 * corresponding error.
527 */
14e297b3 528int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id,
aa4f886f
SH
529 size_t tx_size, size_t rx_size, struct scmi_xfer **p)
530{
531 int ret;
532 struct scmi_xfer *xfer;
533 struct scmi_info *info = handle_to_scmi_info(handle);
38c927fb 534 struct scmi_xfers_info *minfo = &info->tx_minfo;
aa4f886f
SH
535 struct device *dev = info->dev;
536
537 /* Ensure we have sane transfer sizes */
538 if (rx_size > info->desc->max_msg_size ||
539 tx_size > info->desc->max_msg_size)
540 return -ERANGE;
541
38c927fb 542 xfer = scmi_xfer_get(handle, minfo);
aa4f886f
SH
543 if (IS_ERR(xfer)) {
544 ret = PTR_ERR(xfer);
545 dev_err(dev, "failed to get free message slot(%d)\n", ret);
546 return ret;
547 }
548
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;
554
555 *p = xfer;
1baf47c2 556
aa4f886f
SH
557 return 0;
558}
559
b6f20ff8
SH
560/**
561 * scmi_version_get() - command to get the revision of the SCMI entity
562 *
1baf47c2
SH
563 * @handle: Pointer to SCMI entity handle
564 * @protocol: Protocol identifier for the message
565 * @version: Holds returned version of protocol.
b6f20ff8
SH
566 *
567 * Updates the SCMI information in the internal data structure.
568 *
569 * Return: 0 if all went fine, else return appropriate error.
570 */
571int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
572 u32 *version)
573{
574 int ret;
575 __le32 *rev_info;
576 struct scmi_xfer *t;
577
14e297b3 578 ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0,
b6f20ff8
SH
579 sizeof(*version), &t);
580 if (ret)
581 return ret;
582
583 ret = scmi_do_xfer(handle, t);
584 if (!ret) {
585 rev_info = t->rx.buf;
586 *version = le32_to_cpu(*rev_info);
587 }
588
14e297b3 589 scmi_xfer_put(handle, t);
b6f20ff8
SH
590 return ret;
591}
592
593void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
594 u8 *prot_imp)
595{
596 struct scmi_info *info = handle_to_scmi_info(handle);
597
598 info->protocols_imp = prot_imp;
599}
600
bc40081d
SH
601static bool
602scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
603{
604 int i;
605 struct scmi_info *info = handle_to_scmi_info(handle);
606
607 if (!info->protocols_imp)
608 return false;
609
610 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
611 if (info->protocols_imp[i] == prot_id)
612 return true;
613 return false;
614}
615
aa4f886f 616/**
14e297b3 617 * scmi_handle_get() - Get the SCMI handle for a device
aa4f886f
SH
618 *
619 * @dev: pointer to device for which we want SCMI handle
620 *
621 * NOTE: The function does not track individual clients of the framework
1baf47c2 622 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
623 * scmi_handle_put must be balanced with successful scmi_handle_get
624 *
625 * Return: pointer to handle if successful, NULL on error
626 */
627struct scmi_handle *scmi_handle_get(struct device *dev)
628{
629 struct list_head *p;
630 struct scmi_info *info;
631 struct scmi_handle *handle = NULL;
632
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;
638 info->users++;
639 break;
640 }
641 }
642 mutex_unlock(&scmi_list_mutex);
643
644 return handle;
645}
646
647/**
648 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
649 *
650 * @handle: handle acquired by scmi_handle_get
651 *
652 * NOTE: The function does not track individual clients of the framework
1baf47c2 653 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
654 * scmi_handle_put must be balanced with successful scmi_handle_get
655 *
656 * Return: 0 is successfully released
657 * if null was passed, it returns -EINVAL;
658 */
659int scmi_handle_put(const struct scmi_handle *handle)
660{
661 struct scmi_info *info;
662
663 if (!handle)
664 return -EINVAL;
665
666 info = handle_to_scmi_info(handle);
667 mutex_lock(&scmi_list_mutex);
668 if (!WARN_ON(!info->users))
669 info->users--;
670 mutex_unlock(&scmi_list_mutex);
671
672 return 0;
673}
674
aa4f886f
SH
675static int scmi_xfer_info_init(struct scmi_info *sinfo)
676{
677 int i;
678 struct scmi_xfer *xfer;
679 struct device *dev = sinfo->dev;
680 const struct scmi_desc *desc = sinfo->desc;
38c927fb 681 struct scmi_xfers_info *info = &sinfo->tx_minfo;
aa4f886f
SH
682
683 /* Pre-allocated messages, no more than what hdr.seq can support */
354b2e36
SH
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);
aa4f886f
SH
687 return -EINVAL;
688 }
689
690 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
691 sizeof(*info->xfer_block), GFP_KERNEL);
692 if (!info->xfer_block)
693 return -ENOMEM;
694
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)
698 return -ENOMEM;
699
aa4f886f
SH
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,
703 GFP_KERNEL);
704 if (!xfer->rx.buf)
705 return -ENOMEM;
706
707 xfer->tx.buf = xfer->rx.buf;
708 init_completion(&xfer->done);
709 }
710
711 spin_lock_init(&info->xfer_lock);
712
713 return 0;
714}
715
3748daf7 716static int scmi_mailbox_check(struct device_node *np, int idx)
aa4f886f 717{
3748daf7
SH
718 return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells",
719 idx, NULL);
aa4f886f
SH
720}
721
3748daf7
SH
722static int scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev,
723 int prot_id, bool tx)
aa4f886f 724{
3748daf7 725 int ret, idx;
aa4f886f
SH
726 struct resource res;
727 resource_size_t size;
aa4f886f 728 struct device_node *shmem, *np = dev->of_node;
fbc4d81a 729 struct scmi_chan_info *cinfo;
aa4f886f 730 struct mbox_client *cl;
46cc7c28 731 struct idr *idr;
3748daf7
SH
732 const char *desc = tx ? "Tx" : "Rx";
733
734 /* Transmit channel is first entry i.e. index 0 */
735 idx = tx ? 0 : 1;
46cc7c28 736 idr = tx ? &info->tx_idr : &info->rx_idr;
aa4f886f 737
3748daf7 738 if (scmi_mailbox_check(np, idx)) {
46cc7c28
SH
739 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
740 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
741 return -EINVAL;
907b6d14
SH
742 goto idr_alloc;
743 }
744
fbc4d81a
SH
745 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
746 if (!cinfo)
747 return -ENOMEM;
748
fbc4d81a
SH
749 cinfo->dev = dev;
750
751 cl = &cinfo->cl;
aa4f886f
SH
752 cl->dev = dev;
753 cl->rx_callback = scmi_rx_callback;
3748daf7 754 cl->tx_prepare = tx ? scmi_tx_prepare : NULL;
aa4f886f 755 cl->tx_block = false;
3748daf7 756 cl->knows_txdone = tx;
aa4f886f 757
3748daf7 758 shmem = of_parse_phandle(np, "shmem", idx);
aa4f886f
SH
759 ret = of_address_to_resource(shmem, 0, &res);
760 of_node_put(shmem);
761 if (ret) {
3748daf7 762 dev_err(dev, "failed to get SCMI %s payload memory\n", desc);
aa4f886f
SH
763 return ret;
764 }
765
766 size = resource_size(&res);
fbc4d81a
SH
767 cinfo->payload = devm_ioremap(info->dev, res.start, size);
768 if (!cinfo->payload) {
3748daf7 769 dev_err(dev, "failed to ioremap SCMI %s payload\n", desc);
aa4f886f
SH
770 return -EADDRNOTAVAIL;
771 }
772
3748daf7 773 cinfo->chan = mbox_request_channel(cl, idx);
fbc4d81a
SH
774 if (IS_ERR(cinfo->chan)) {
775 ret = PTR_ERR(cinfo->chan);
aa4f886f 776 if (ret != -EPROBE_DEFER)
3748daf7
SH
777 dev_err(dev, "failed to request SCMI %s mailbox\n",
778 desc);
aa4f886f
SH
779 return ret;
780 }
781
907b6d14 782idr_alloc:
46cc7c28 783 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
907b6d14
SH
784 if (ret != prot_id) {
785 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
786 return ret;
787 }
788
789 cinfo->handle = &info->handle;
aa4f886f
SH
790 return 0;
791}
792
46cc7c28
SH
793static inline int
794scmi_mbox_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
795{
796 int ret = scmi_mbox_chan_setup(info, dev, prot_id, true);
797
798 if (!ret) /* Rx is optional, hence no error check */
799 scmi_mbox_chan_setup(info, dev, prot_id, false);
800
801 return ret;
802}
803
bc40081d
SH
804static inline void
805scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
806 int prot_id)
807{
808 struct scmi_device *sdev;
809
810 sdev = scmi_device_create(np, info->dev, prot_id);
811 if (!sdev) {
812 dev_err(info->dev, "failed to create %d protocol device\n",
813 prot_id);
814 return;
815 }
816
46cc7c28 817 if (scmi_mbox_txrx_setup(info, &sdev->dev, prot_id)) {
907b6d14
SH
818 dev_err(&sdev->dev, "failed to setup transport\n");
819 scmi_device_destroy(sdev);
31c60855 820 return;
907b6d14
SH
821 }
822
bc40081d
SH
823 /* setup handle now as the transport is ready */
824 scmi_set_handle(sdev);
825}
826
aa4f886f
SH
827static int scmi_probe(struct platform_device *pdev)
828{
829 int ret;
830 struct scmi_handle *handle;
831 const struct scmi_desc *desc;
832 struct scmi_info *info;
833 struct device *dev = &pdev->dev;
bc40081d 834 struct device_node *child, *np = dev->of_node;
aa4f886f
SH
835
836 /* Only mailbox method supported, check for the presence of one */
3748daf7 837 if (scmi_mailbox_check(np, 0)) {
aa4f886f
SH
838 dev_err(dev, "no mailbox found in %pOF\n", np);
839 return -EINVAL;
840 }
841
d9350f21
AP
842 desc = of_device_get_match_data(dev);
843 if (!desc)
844 return -EINVAL;
aa4f886f
SH
845
846 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
847 if (!info)
848 return -ENOMEM;
849
850 info->dev = dev;
851 info->desc = desc;
852 INIT_LIST_HEAD(&info->node);
853
854 ret = scmi_xfer_info_init(info);
855 if (ret)
856 return ret;
857
858 platform_set_drvdata(pdev, info);
907b6d14 859 idr_init(&info->tx_idr);
46cc7c28 860 idr_init(&info->rx_idr);
aa4f886f
SH
861
862 handle = &info->handle;
863 handle->dev = info->dev;
b6f20ff8 864 handle->version = &info->version;
aa4f886f 865
46cc7c28 866 ret = scmi_mbox_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
aa4f886f
SH
867 if (ret)
868 return ret;
869
b6f20ff8
SH
870 ret = scmi_base_protocol_init(handle);
871 if (ret) {
872 dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
b6f20ff8
SH
873 return ret;
874 }
875
aa4f886f
SH
876 mutex_lock(&scmi_list_mutex);
877 list_add_tail(&info->node, &scmi_list);
878 mutex_unlock(&scmi_list_mutex);
879
bc40081d
SH
880 for_each_available_child_of_node(np, child) {
881 u32 prot_id;
882
883 if (of_property_read_u32(child, "reg", &prot_id))
884 continue;
885
354b2e36
SH
886 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
887 dev_err(dev, "Out of range protocol %d\n", prot_id);
bc40081d
SH
888
889 if (!scmi_is_protocol_implemented(handle, prot_id)) {
890 dev_err(dev, "SCMI protocol %d not implemented\n",
891 prot_id);
892 continue;
893 }
894
895 scmi_create_protocol_device(child, info, prot_id);
896 }
897
aa4f886f
SH
898 return 0;
899}
900
2747a967
SH
901static int scmi_mbox_free_channel(int id, void *p, void *data)
902{
903 struct scmi_chan_info *cinfo = p;
904 struct idr *idr = data;
905
906 if (!IS_ERR_OR_NULL(cinfo->chan)) {
907 mbox_free_channel(cinfo->chan);
908 cinfo->chan = NULL;
909 }
910
911 idr_remove(idr, id);
912
913 return 0;
914}
915
916static int scmi_remove(struct platform_device *pdev)
917{
918 int ret = 0;
919 struct scmi_info *info = platform_get_drvdata(pdev);
920 struct idr *idr = &info->tx_idr;
921
922 mutex_lock(&scmi_list_mutex);
923 if (info->users)
924 ret = -EBUSY;
925 else
926 list_del(&info->node);
927 mutex_unlock(&scmi_list_mutex);
928
929 if (ret)
930 return ret;
931
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);
935
46cc7c28
SH
936 idr = &info->rx_idr;
937 ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
938 idr_destroy(&info->rx_idr);
939
2747a967
SH
940 return ret;
941}
942
943static 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 */
946 .max_msg_size = 128,
947};
948
949/* Each compatible listed below must have descriptor associated with it */
950static const struct of_device_id scmi_of_match[] = {
951 { .compatible = "arm,scmi", .data = &scmi_generic_desc },
952 { /* Sentinel */ },
953};
954
955MODULE_DEVICE_TABLE(of, scmi_of_match);
956
aa4f886f
SH
957static struct platform_driver scmi_driver = {
958 .driver = {
959 .name = "arm-scmi",
960 .of_match_table = scmi_of_match,
961 },
962 .probe = scmi_probe,
963 .remove = scmi_remove,
964};
965
966module_platform_driver(scmi_driver);
967
968MODULE_ALIAS("platform: arm-scmi");
969MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
970MODULE_DESCRIPTION("ARM SCMI protocol driver");
971MODULE_LICENSE("GPL v2");