Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[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
SH
32#define MSG_ID_MASK GENMASK(7, 0)
33#define MSG_TYPE_MASK GENMASK(9, 8)
34#define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
35#define MSG_TOKEN_ID_MASK GENMASK(27, 18)
36#define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
37#define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
aa4f886f
SH
38
39enum scmi_error_codes {
40 SCMI_SUCCESS = 0, /* Success */
41 SCMI_ERR_SUPPORT = -1, /* Not supported */
42 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
43 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
44 SCMI_ERR_ENTRY = -4, /* Not found */
45 SCMI_ERR_RANGE = -5, /* Value out of range */
46 SCMI_ERR_BUSY = -6, /* Device busy */
47 SCMI_ERR_COMMS = -7, /* Communication Error */
48 SCMI_ERR_GENERIC = -8, /* Generic Error */
49 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
50 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
51 SCMI_ERR_MAX
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);
58
59/**
60 * struct scmi_xfers_info - Structure to manage transfer information
61 *
62 * @xfer_block: Preallocated Message array
63 * @xfer_alloc_table: Bitmap table for allocated messages.
64 * Index of this bitmap table is also used for message
65 * sequence identifier.
66 * @xfer_lock: Protection for message allocation
67 */
68struct scmi_xfers_info {
69 struct scmi_xfer *xfer_block;
70 unsigned long *xfer_alloc_table;
aa4f886f
SH
71 spinlock_t xfer_lock;
72};
73
74/**
75 * struct scmi_desc - Description of SoC integration
76 *
77 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
78 * @max_msg: Maximum number of messages that can be pending
79 * simultaneously in the system
80 * @max_msg_size: Maximum size of data per message that can be handled.
81 */
82struct scmi_desc {
83 int max_rx_timeout_ms;
84 int max_msg;
85 int max_msg_size;
86};
87
fbc4d81a
SH
88/**
89 * struct scmi_chan_info - Structure representing a SCMI channel informfation
90 *
91 * @cl: Mailbox Client
92 * @chan: Transmit/Receive mailbox channel
93 * @payload: Transmit/Receive mailbox channel payload area
94 * @dev: Reference to device in the SCMI hierarchy corresponding to this
95 * channel
1baf47c2 96 * @handle: Pointer to SCMI entity handle
fbc4d81a
SH
97 */
98struct scmi_chan_info {
99 struct mbox_client cl;
100 struct mbox_chan *chan;
101 void __iomem *payload;
102 struct device *dev;
907b6d14 103 struct scmi_handle *handle;
fbc4d81a
SH
104};
105
aa4f886f 106/**
1baf47c2 107 * struct scmi_info - Structure representing a SCMI instance
aa4f886f
SH
108 *
109 * @dev: Device pointer
110 * @desc: SoC description for this instance
111 * @handle: Instance of SCMI handle to send to clients
b6f20ff8
SH
112 * @version: SCMI revision information containing protocol version,
113 * implementation version and (sub-)vendor identification.
aa4f886f 114 * @minfo: Message info
907b6d14 115 * @tx_idr: IDR object to map protocol id to channel info pointer
1baf47c2 116 * @protocols_imp: List of protocols implemented, currently maximum of
b6f20ff8 117 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
1baf47c2 118 * @node: List head
aa4f886f
SH
119 * @users: Number of users of this instance
120 */
121struct scmi_info {
122 struct device *dev;
123 const struct scmi_desc *desc;
b6f20ff8 124 struct scmi_revision_info version;
aa4f886f 125 struct scmi_handle handle;
aa4f886f 126 struct scmi_xfers_info minfo;
907b6d14 127 struct idr tx_idr;
b6f20ff8 128 u8 *protocols_imp;
aa4f886f
SH
129 struct list_head node;
130 int users;
131};
132
fbc4d81a 133#define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl)
aa4f886f
SH
134#define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
135
136/*
137 * SCMI specification requires all parameters, message headers, return
138 * arguments or any protocol data to be expressed in little endian
139 * format only.
140 */
141struct scmi_shared_mem {
142 __le32 reserved;
143 __le32 channel_status;
144#define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
145#define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
146 __le32 reserved1[2];
147 __le32 flags;
148#define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
149 __le32 length;
150 __le32 msg_header;
151 u8 msg_payload[0];
152};
153
154static const int scmi_linux_errmap[] = {
155 /* better than switch case as long as return value is continuous */
156 0, /* SCMI_SUCCESS */
157 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
158 -EINVAL, /* SCMI_ERR_PARAM */
159 -EACCES, /* SCMI_ERR_ACCESS */
160 -ENOENT, /* SCMI_ERR_ENTRY */
161 -ERANGE, /* SCMI_ERR_RANGE */
162 -EBUSY, /* SCMI_ERR_BUSY */
163 -ECOMM, /* SCMI_ERR_COMMS */
164 -EIO, /* SCMI_ERR_GENERIC */
165 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
166 -EPROTO, /* SCMI_ERR_PROTOCOL */
167};
168
169static inline int scmi_to_linux_errno(int errno)
170{
171 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
172 return scmi_linux_errmap[-errno];
173 return -EIO;
174}
175
176/**
177 * scmi_dump_header_dbg() - Helper to dump a message header.
178 *
179 * @dev: Device pointer corresponding to the SCMI entity
180 * @hdr: pointer to header.
181 */
182static inline void scmi_dump_header_dbg(struct device *dev,
183 struct scmi_msg_hdr *hdr)
184{
185 dev_dbg(dev, "Command ID: %x Sequence ID: %x Protocol: %x\n",
186 hdr->id, hdr->seq, hdr->protocol_id);
187}
188
189static void scmi_fetch_response(struct scmi_xfer *xfer,
190 struct scmi_shared_mem __iomem *mem)
191{
192 xfer->hdr.status = ioread32(mem->msg_payload);
193 /* Skip the length of header and statues in payload area i.e 8 bytes*/
194 xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8);
195
196 /* Take a copy to the rx buffer.. */
197 memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len);
198}
199
200/**
201 * scmi_rx_callback() - mailbox client callback for receive messages
202 *
203 * @cl: client pointer
204 * @m: mailbox message
205 *
206 * Processes one received message to appropriate transfer information and
207 * signals completion of the transfer.
208 *
209 * NOTE: This function will be invoked in IRQ context, hence should be
210 * as optimal as possible.
211 */
212static void scmi_rx_callback(struct mbox_client *cl, void *m)
213{
214 u16 xfer_id;
215 struct scmi_xfer *xfer;
fbc4d81a
SH
216 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
217 struct device *dev = cinfo->dev;
907b6d14 218 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
aa4f886f 219 struct scmi_xfers_info *minfo = &info->minfo;
fbc4d81a 220 struct scmi_shared_mem __iomem *mem = cinfo->payload;
aa4f886f
SH
221
222 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
223
1baf47c2 224 /* Are we even expecting this? */
aa4f886f
SH
225 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
226 dev_err(dev, "message for %d is not expected!\n", xfer_id);
227 return;
228 }
229
230 xfer = &minfo->xfer_block[xfer_id];
231
232 scmi_dump_header_dbg(dev, &xfer->hdr);
233 /* Is the message of valid length? */
234 if (xfer->rx.len > info->desc->max_msg_size) {
235 dev_err(dev, "unable to handle %zu xfer(max %d)\n",
236 xfer->rx.len, info->desc->max_msg_size);
237 return;
238 }
239
240 scmi_fetch_response(xfer, mem);
241 complete(&xfer->done);
242}
243
244/**
245 * pack_scmi_header() - packs and returns 32-bit header
246 *
247 * @hdr: pointer to header containing all the information on message id,
248 * protocol id and sequence id.
1baf47c2
SH
249 *
250 * Return: 32-bit packed command header to be sent to the platform.
aa4f886f
SH
251 */
252static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
253{
354b2e36
SH
254 return FIELD_PREP(MSG_ID_MASK, hdr->id) |
255 FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
256 FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
aa4f886f
SH
257}
258
259/**
260 * scmi_tx_prepare() - mailbox client callback to prepare for the transfer
261 *
262 * @cl: client pointer
263 * @m: mailbox message
264 *
265 * This function prepares the shared memory which contains the header and the
266 * payload.
267 */
268static void scmi_tx_prepare(struct mbox_client *cl, void *m)
269{
270 struct scmi_xfer *t = m;
fbc4d81a
SH
271 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
272 struct scmi_shared_mem __iomem *mem = cinfo->payload;
aa4f886f
SH
273
274 /* Mark channel busy + clear error */
275 iowrite32(0x0, &mem->channel_status);
276 iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
277 &mem->flags);
278 iowrite32(sizeof(mem->msg_header) + t->tx.len, &mem->length);
279 iowrite32(pack_scmi_header(&t->hdr), &mem->msg_header);
280 if (t->tx.buf)
281 memcpy_toio(mem->msg_payload, t->tx.buf, t->tx.len);
282}
283
284/**
1baf47c2 285 * scmi_xfer_get() - Allocate one message
aa4f886f 286 *
1baf47c2 287 * @handle: Pointer to SCMI entity handle
aa4f886f
SH
288 *
289 * Helper function which is used by various command functions that are
290 * exposed to clients of this driver for allocating a message traffic event.
291 *
292 * This function can sleep depending on pending requests already in the system
293 * for the SCMI entity. Further, this also holds a spinlock to maintain
294 * integrity of internal data structures.
295 *
296 * Return: 0 if all went fine, else corresponding error.
297 */
14e297b3 298static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle)
aa4f886f
SH
299{
300 u16 xfer_id;
301 struct scmi_xfer *xfer;
302 unsigned long flags, bit_pos;
303 struct scmi_info *info = handle_to_scmi_info(handle);
304 struct scmi_xfers_info *minfo = &info->minfo;
305
306 /* Keep the locked section as small as possible */
307 spin_lock_irqsave(&minfo->xfer_lock, flags);
308 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
309 info->desc->max_msg);
310 if (bit_pos == info->desc->max_msg) {
311 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
312 return ERR_PTR(-ENOMEM);
313 }
314 set_bit(bit_pos, minfo->xfer_alloc_table);
315 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
316
317 xfer_id = bit_pos;
318
319 xfer = &minfo->xfer_block[xfer_id];
320 xfer->hdr.seq = xfer_id;
321 reinit_completion(&xfer->done);
322
323 return xfer;
324}
325
326/**
14e297b3 327 * scmi_xfer_put() - Release a message
aa4f886f 328 *
1baf47c2
SH
329 * @handle: Pointer to SCMI entity handle
330 * @xfer: message that was reserved by scmi_xfer_get
aa4f886f
SH
331 *
332 * This holds a spinlock to maintain integrity of internal data structures.
333 */
14e297b3 334void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
aa4f886f
SH
335{
336 unsigned long flags;
337 struct scmi_info *info = handle_to_scmi_info(handle);
338 struct scmi_xfers_info *minfo = &info->minfo;
339
340 /*
341 * Keep the locked section as small as possible
342 * NOTE: we might escape with smp_mb and no lock here..
343 * but just be conservative and symmetric.
344 */
345 spin_lock_irqsave(&minfo->xfer_lock, flags);
346 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
347 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
348}
349
d4c3751a 350static bool
fbc4d81a 351scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
d4c3751a 352{
fbc4d81a
SH
353 struct scmi_shared_mem __iomem *mem = cinfo->payload;
354 u16 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
d4c3751a
SH
355
356 if (xfer->hdr.seq != xfer_id)
357 return false;
358
fbc4d81a 359 return ioread32(&mem->channel_status) &
d4c3751a
SH
360 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
361 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
362}
363
364#define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
365
fbc4d81a 366static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info *cinfo,
d4c3751a
SH
367 struct scmi_xfer *xfer, ktime_t stop)
368{
369 ktime_t __cur = ktime_get();
370
fbc4d81a 371 return scmi_xfer_poll_done(cinfo, xfer) || ktime_after(__cur, stop);
d4c3751a
SH
372}
373
aa4f886f
SH
374/**
375 * scmi_do_xfer() - Do one transfer
376 *
1baf47c2 377 * @handle: Pointer to SCMI entity handle
aa4f886f
SH
378 * @xfer: Transfer to initiate and wait for response
379 *
380 * Return: -ETIMEDOUT in case of no response, if transmit error,
1baf47c2
SH
381 * return corresponding error, else if all goes well,
382 * return 0.
aa4f886f
SH
383 */
384int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer)
385{
386 int ret;
387 int timeout;
388 struct scmi_info *info = handle_to_scmi_info(handle);
389 struct device *dev = info->dev;
907b6d14
SH
390 struct scmi_chan_info *cinfo;
391
392 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
393 if (unlikely(!cinfo))
394 return -EINVAL;
aa4f886f 395
fbc4d81a 396 ret = mbox_send_message(cinfo->chan, xfer);
aa4f886f
SH
397 if (ret < 0) {
398 dev_dbg(dev, "mbox send fail %d\n", ret);
399 return ret;
400 }
401
402 /* mbox_send_message returns non-negative value on success, so reset */
403 ret = 0;
404
d4c3751a
SH
405 if (xfer->hdr.poll_completion) {
406 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
407
fbc4d81a 408 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
d4c3751a
SH
409
410 if (ktime_before(ktime_get(), stop))
fbc4d81a 411 scmi_fetch_response(xfer, cinfo->payload);
d4c3751a
SH
412 else
413 ret = -ETIMEDOUT;
414 } else {
415 /* And we wait for the response. */
416 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
417 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
418 dev_err(dev, "mbox timed out in resp(caller: %pS)\n",
419 (void *)_RET_IP_);
420 ret = -ETIMEDOUT;
421 }
aa4f886f 422 }
d4c3751a
SH
423
424 if (!ret && xfer->hdr.status)
425 ret = scmi_to_linux_errno(xfer->hdr.status);
426
aa4f886f
SH
427 /*
428 * NOTE: we might prefer not to need the mailbox ticker to manage the
429 * transfer queueing since the protocol layer queues things by itself.
430 * Unfortunately, we have to kick the mailbox framework after we have
431 * received our message.
432 */
fbc4d81a 433 mbox_client_txdone(cinfo->chan, ret);
aa4f886f
SH
434
435 return ret;
436}
437
438/**
14e297b3 439 * scmi_xfer_get_init() - Allocate and initialise one message
aa4f886f 440 *
1baf47c2 441 * @handle: Pointer to SCMI entity handle
aa4f886f 442 * @msg_id: Message identifier
1baf47c2 443 * @prot_id: Protocol identifier for the message
aa4f886f
SH
444 * @tx_size: transmit message size
445 * @rx_size: receive message size
446 * @p: pointer to the allocated and initialised message
447 *
14e297b3 448 * This function allocates the message using @scmi_xfer_get and
aa4f886f
SH
449 * initialise the header.
450 *
451 * Return: 0 if all went fine with @p pointing to message, else
452 * corresponding error.
453 */
14e297b3 454int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id,
aa4f886f
SH
455 size_t tx_size, size_t rx_size, struct scmi_xfer **p)
456{
457 int ret;
458 struct scmi_xfer *xfer;
459 struct scmi_info *info = handle_to_scmi_info(handle);
460 struct device *dev = info->dev;
461
462 /* Ensure we have sane transfer sizes */
463 if (rx_size > info->desc->max_msg_size ||
464 tx_size > info->desc->max_msg_size)
465 return -ERANGE;
466
14e297b3 467 xfer = scmi_xfer_get(handle);
aa4f886f
SH
468 if (IS_ERR(xfer)) {
469 ret = PTR_ERR(xfer);
470 dev_err(dev, "failed to get free message slot(%d)\n", ret);
471 return ret;
472 }
473
474 xfer->tx.len = tx_size;
475 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
476 xfer->hdr.id = msg_id;
477 xfer->hdr.protocol_id = prot_id;
478 xfer->hdr.poll_completion = false;
479
480 *p = xfer;
1baf47c2 481
aa4f886f
SH
482 return 0;
483}
484
b6f20ff8
SH
485/**
486 * scmi_version_get() - command to get the revision of the SCMI entity
487 *
1baf47c2
SH
488 * @handle: Pointer to SCMI entity handle
489 * @protocol: Protocol identifier for the message
490 * @version: Holds returned version of protocol.
b6f20ff8
SH
491 *
492 * Updates the SCMI information in the internal data structure.
493 *
494 * Return: 0 if all went fine, else return appropriate error.
495 */
496int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
497 u32 *version)
498{
499 int ret;
500 __le32 *rev_info;
501 struct scmi_xfer *t;
502
14e297b3 503 ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0,
b6f20ff8
SH
504 sizeof(*version), &t);
505 if (ret)
506 return ret;
507
508 ret = scmi_do_xfer(handle, t);
509 if (!ret) {
510 rev_info = t->rx.buf;
511 *version = le32_to_cpu(*rev_info);
512 }
513
14e297b3 514 scmi_xfer_put(handle, t);
b6f20ff8
SH
515 return ret;
516}
517
518void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
519 u8 *prot_imp)
520{
521 struct scmi_info *info = handle_to_scmi_info(handle);
522
523 info->protocols_imp = prot_imp;
524}
525
bc40081d
SH
526static bool
527scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
528{
529 int i;
530 struct scmi_info *info = handle_to_scmi_info(handle);
531
532 if (!info->protocols_imp)
533 return false;
534
535 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
536 if (info->protocols_imp[i] == prot_id)
537 return true;
538 return false;
539}
540
aa4f886f 541/**
14e297b3 542 * scmi_handle_get() - Get the SCMI handle for a device
aa4f886f
SH
543 *
544 * @dev: pointer to device for which we want SCMI handle
545 *
546 * NOTE: The function does not track individual clients of the framework
1baf47c2 547 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
548 * scmi_handle_put must be balanced with successful scmi_handle_get
549 *
550 * Return: pointer to handle if successful, NULL on error
551 */
552struct scmi_handle *scmi_handle_get(struct device *dev)
553{
554 struct list_head *p;
555 struct scmi_info *info;
556 struct scmi_handle *handle = NULL;
557
558 mutex_lock(&scmi_list_mutex);
559 list_for_each(p, &scmi_list) {
560 info = list_entry(p, struct scmi_info, node);
561 if (dev->parent == info->dev) {
562 handle = &info->handle;
563 info->users++;
564 break;
565 }
566 }
567 mutex_unlock(&scmi_list_mutex);
568
569 return handle;
570}
571
572/**
573 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
574 *
575 * @handle: handle acquired by scmi_handle_get
576 *
577 * NOTE: The function does not track individual clients of the framework
1baf47c2 578 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
579 * scmi_handle_put must be balanced with successful scmi_handle_get
580 *
581 * Return: 0 is successfully released
582 * if null was passed, it returns -EINVAL;
583 */
584int scmi_handle_put(const struct scmi_handle *handle)
585{
586 struct scmi_info *info;
587
588 if (!handle)
589 return -EINVAL;
590
591 info = handle_to_scmi_info(handle);
592 mutex_lock(&scmi_list_mutex);
593 if (!WARN_ON(!info->users))
594 info->users--;
595 mutex_unlock(&scmi_list_mutex);
596
597 return 0;
598}
599
600static const struct scmi_desc scmi_generic_desc = {
1baf47c2 601 .max_rx_timeout_ms = 30, /* We may increase this if required */
aa4f886f
SH
602 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
603 .max_msg_size = 128,
604};
605
606/* Each compatible listed below must have descriptor associated with it */
607static const struct of_device_id scmi_of_match[] = {
608 { .compatible = "arm,scmi", .data = &scmi_generic_desc },
609 { /* Sentinel */ },
610};
611
612MODULE_DEVICE_TABLE(of, scmi_of_match);
613
614static int scmi_xfer_info_init(struct scmi_info *sinfo)
615{
616 int i;
617 struct scmi_xfer *xfer;
618 struct device *dev = sinfo->dev;
619 const struct scmi_desc *desc = sinfo->desc;
620 struct scmi_xfers_info *info = &sinfo->minfo;
621
622 /* Pre-allocated messages, no more than what hdr.seq can support */
354b2e36
SH
623 if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
624 dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
625 desc->max_msg, MSG_TOKEN_MAX);
aa4f886f
SH
626 return -EINVAL;
627 }
628
629 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
630 sizeof(*info->xfer_block), GFP_KERNEL);
631 if (!info->xfer_block)
632 return -ENOMEM;
633
634 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
635 sizeof(long), GFP_KERNEL);
636 if (!info->xfer_alloc_table)
637 return -ENOMEM;
638
aa4f886f
SH
639 /* Pre-initialize the buffer pointer to pre-allocated buffers */
640 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
641 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
642 GFP_KERNEL);
643 if (!xfer->rx.buf)
644 return -ENOMEM;
645
646 xfer->tx.buf = xfer->rx.buf;
647 init_completion(&xfer->done);
648 }
649
650 spin_lock_init(&info->xfer_lock);
651
652 return 0;
653}
654
655static int scmi_mailbox_check(struct device_node *np)
656{
657 struct of_phandle_args arg;
658
659 return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells", 0, &arg);
660}
661
907b6d14 662static int scmi_mbox_free_channel(int id, void *p, void *data)
aa4f886f 663{
907b6d14
SH
664 struct scmi_chan_info *cinfo = p;
665 struct idr *idr = data;
666
fbc4d81a
SH
667 if (!IS_ERR_OR_NULL(cinfo->chan)) {
668 mbox_free_channel(cinfo->chan);
669 cinfo->chan = NULL;
aa4f886f
SH
670 }
671
907b6d14
SH
672 idr_remove(idr, id);
673
aa4f886f
SH
674 return 0;
675}
676
677static int scmi_remove(struct platform_device *pdev)
678{
679 int ret = 0;
680 struct scmi_info *info = platform_get_drvdata(pdev);
907b6d14 681 struct idr *idr = &info->tx_idr;
aa4f886f
SH
682
683 mutex_lock(&scmi_list_mutex);
684 if (info->users)
685 ret = -EBUSY;
686 else
687 list_del(&info->node);
688 mutex_unlock(&scmi_list_mutex);
689
632de8f5
SH
690 if (ret)
691 return ret;
692
693 /* Safe to free channels since no more users */
694 ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
695 idr_destroy(&info->tx_idr);
aa4f886f
SH
696
697 return ret;
698}
699
907b6d14
SH
700static inline int
701scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev, int prot_id)
aa4f886f
SH
702{
703 int ret;
704 struct resource res;
705 resource_size_t size;
aa4f886f 706 struct device_node *shmem, *np = dev->of_node;
fbc4d81a 707 struct scmi_chan_info *cinfo;
aa4f886f
SH
708 struct mbox_client *cl;
709
907b6d14
SH
710 if (scmi_mailbox_check(np)) {
711 cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
712 goto idr_alloc;
713 }
714
fbc4d81a
SH
715 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
716 if (!cinfo)
717 return -ENOMEM;
718
fbc4d81a
SH
719 cinfo->dev = dev;
720
721 cl = &cinfo->cl;
aa4f886f
SH
722 cl->dev = dev;
723 cl->rx_callback = scmi_rx_callback;
724 cl->tx_prepare = scmi_tx_prepare;
725 cl->tx_block = false;
726 cl->knows_txdone = true;
727
728 shmem = of_parse_phandle(np, "shmem", 0);
729 ret = of_address_to_resource(shmem, 0, &res);
730 of_node_put(shmem);
731 if (ret) {
732 dev_err(dev, "failed to get SCMI Tx payload mem resource\n");
733 return ret;
734 }
735
736 size = resource_size(&res);
fbc4d81a
SH
737 cinfo->payload = devm_ioremap(info->dev, res.start, size);
738 if (!cinfo->payload) {
aa4f886f
SH
739 dev_err(dev, "failed to ioremap SCMI Tx payload\n");
740 return -EADDRNOTAVAIL;
741 }
742
743 /* Transmit channel is first entry i.e. index 0 */
fbc4d81a
SH
744 cinfo->chan = mbox_request_channel(cl, 0);
745 if (IS_ERR(cinfo->chan)) {
746 ret = PTR_ERR(cinfo->chan);
aa4f886f
SH
747 if (ret != -EPROBE_DEFER)
748 dev_err(dev, "failed to request SCMI Tx mailbox\n");
749 return ret;
750 }
751
907b6d14
SH
752idr_alloc:
753 ret = idr_alloc(&info->tx_idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
754 if (ret != prot_id) {
755 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
756 return ret;
757 }
758
759 cinfo->handle = &info->handle;
aa4f886f
SH
760 return 0;
761}
762
bc40081d
SH
763static inline void
764scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
765 int prot_id)
766{
767 struct scmi_device *sdev;
768
769 sdev = scmi_device_create(np, info->dev, prot_id);
770 if (!sdev) {
771 dev_err(info->dev, "failed to create %d protocol device\n",
772 prot_id);
773 return;
774 }
775
907b6d14
SH
776 if (scmi_mbox_chan_setup(info, &sdev->dev, prot_id)) {
777 dev_err(&sdev->dev, "failed to setup transport\n");
778 scmi_device_destroy(sdev);
31c60855 779 return;
907b6d14
SH
780 }
781
bc40081d
SH
782 /* setup handle now as the transport is ready */
783 scmi_set_handle(sdev);
784}
785
aa4f886f
SH
786static int scmi_probe(struct platform_device *pdev)
787{
788 int ret;
789 struct scmi_handle *handle;
790 const struct scmi_desc *desc;
791 struct scmi_info *info;
792 struct device *dev = &pdev->dev;
bc40081d 793 struct device_node *child, *np = dev->of_node;
aa4f886f
SH
794
795 /* Only mailbox method supported, check for the presence of one */
796 if (scmi_mailbox_check(np)) {
797 dev_err(dev, "no mailbox found in %pOF\n", np);
798 return -EINVAL;
799 }
800
801 desc = of_match_device(scmi_of_match, dev)->data;
802
803 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
804 if (!info)
805 return -ENOMEM;
806
807 info->dev = dev;
808 info->desc = desc;
809 INIT_LIST_HEAD(&info->node);
810
811 ret = scmi_xfer_info_init(info);
812 if (ret)
813 return ret;
814
815 platform_set_drvdata(pdev, info);
907b6d14 816 idr_init(&info->tx_idr);
aa4f886f
SH
817
818 handle = &info->handle;
819 handle->dev = info->dev;
b6f20ff8 820 handle->version = &info->version;
aa4f886f 821
907b6d14 822 ret = scmi_mbox_chan_setup(info, dev, SCMI_PROTOCOL_BASE);
aa4f886f
SH
823 if (ret)
824 return ret;
825
b6f20ff8
SH
826 ret = scmi_base_protocol_init(handle);
827 if (ret) {
828 dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
b6f20ff8
SH
829 return ret;
830 }
831
aa4f886f
SH
832 mutex_lock(&scmi_list_mutex);
833 list_add_tail(&info->node, &scmi_list);
834 mutex_unlock(&scmi_list_mutex);
835
bc40081d
SH
836 for_each_available_child_of_node(np, child) {
837 u32 prot_id;
838
839 if (of_property_read_u32(child, "reg", &prot_id))
840 continue;
841
354b2e36
SH
842 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
843 dev_err(dev, "Out of range protocol %d\n", prot_id);
bc40081d
SH
844
845 if (!scmi_is_protocol_implemented(handle, prot_id)) {
846 dev_err(dev, "SCMI protocol %d not implemented\n",
847 prot_id);
848 continue;
849 }
850
851 scmi_create_protocol_device(child, info, prot_id);
852 }
853
aa4f886f
SH
854 return 0;
855}
856
857static struct platform_driver scmi_driver = {
858 .driver = {
859 .name = "arm-scmi",
860 .of_match_table = scmi_of_match,
861 },
862 .probe = scmi_probe,
863 .remove = scmi_remove,
864};
865
866module_platform_driver(scmi_driver);
867
868MODULE_ALIAS("platform: arm-scmi");
869MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
870MODULE_DESCRIPTION("ARM SCMI protocol driver");
871MODULE_LICENSE("GPL v2");