firmware: arm_scmi: Cleanup unused core transfer helper wrappers
[linux-2.6-microblaze.git] / drivers / firmware / arm_scmi / driver.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Message Protocol driver
4  *
5  * SCMI Message Protocol is used between the System Control Processor(SCP)
6  * and the Application Processors(AP). The Message Handling Unit(MHU)
7  * provides a mechanism for inter-processor communication between SCP's
8  * Cortex M3 and AP.
9  *
10  * SCP offers control and management of the core/cluster power states,
11  * various power domain DVFS including the core/cluster, certain system
12  * clocks configuration, thermal sensors and many others.
13  *
14  * Copyright (C) 2018-2021 ARM Ltd.
15  */
16
17 #include <linux/bitmap.h>
18 #include <linux/device.h>
19 #include <linux/export.h>
20 #include <linux/idr.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/ktime.h>
24 #include <linux/module.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/processor.h>
28 #include <linux/refcount.h>
29 #include <linux/slab.h>
30
31 #include "common.h"
32 #include "notify.h"
33
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/scmi.h>
36
37 enum scmi_error_codes {
38         SCMI_SUCCESS = 0,       /* Success */
39         SCMI_ERR_SUPPORT = -1,  /* Not supported */
40         SCMI_ERR_PARAMS = -2,   /* Invalid Parameters */
41         SCMI_ERR_ACCESS = -3,   /* Invalid access/permission denied */
42         SCMI_ERR_ENTRY = -4,    /* Not found */
43         SCMI_ERR_RANGE = -5,    /* Value out of range */
44         SCMI_ERR_BUSY = -6,     /* Device busy */
45         SCMI_ERR_COMMS = -7,    /* Communication Error */
46         SCMI_ERR_GENERIC = -8,  /* Generic Error */
47         SCMI_ERR_HARDWARE = -9, /* Hardware Error */
48         SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
49         SCMI_ERR_MAX
50 };
51
52 /* List of all SCMI devices active in system */
53 static LIST_HEAD(scmi_list);
54 /* Protection for the entire list */
55 static DEFINE_MUTEX(scmi_list_mutex);
56 /* Track the unique id for the transfers for debug & profiling purpose */
57 static atomic_t transfer_last_id;
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  */
68 struct scmi_xfers_info {
69         struct scmi_xfer *xfer_block;
70         unsigned long *xfer_alloc_table;
71         spinlock_t xfer_lock;
72 };
73
74 /**
75  * struct scmi_protocol_instance  - Describe an initialized protocol instance.
76  * @handle: Reference to the SCMI handle associated to this protocol instance.
77  * @proto: A reference to the protocol descriptor.
78  * @gid: A reference for per-protocol devres management.
79  * @users: A refcount to track effective users of this protocol.
80  * @priv: Reference for optional protocol private data.
81  * @ph: An embedded protocol handle that will be passed down to protocol
82  *      initialization code to identify this instance.
83  *
84  * Each protocol is initialized independently once for each SCMI platform in
85  * which is defined by DT and implemented by the SCMI server fw.
86  */
87 struct scmi_protocol_instance {
88         const struct scmi_handle        *handle;
89         const struct scmi_protocol      *proto;
90         void                            *gid;
91         refcount_t                      users;
92         void                            *priv;
93         struct scmi_protocol_handle     ph;
94 };
95
96 #define ph_to_pi(h)     container_of(h, struct scmi_protocol_instance, ph)
97
98 /**
99  * struct scmi_info - Structure representing a SCMI instance
100  *
101  * @dev: Device pointer
102  * @desc: SoC description for this instance
103  * @version: SCMI revision information containing protocol version,
104  *      implementation version and (sub-)vendor identification.
105  * @handle: Instance of SCMI handle to send to clients
106  * @tx_minfo: Universal Transmit Message management info
107  * @rx_minfo: Universal Receive Message management info
108  * @tx_idr: IDR object to map protocol id to Tx channel info pointer
109  * @rx_idr: IDR object to map protocol id to Rx channel info pointer
110  * @protocols: IDR for protocols' instance descriptors initialized for
111  *             this SCMI instance: populated on protocol's first attempted
112  *             usage.
113  * @protocols_mtx: A mutex to protect protocols instances initialization.
114  * @protocols_imp: List of protocols implemented, currently maximum of
115  *      MAX_PROTOCOLS_IMP elements allocated by the base protocol
116  * @node: List head
117  * @users: Number of users of this instance
118  */
119 struct scmi_info {
120         struct device *dev;
121         const struct scmi_desc *desc;
122         struct scmi_revision_info version;
123         struct scmi_handle handle;
124         struct scmi_xfers_info tx_minfo;
125         struct scmi_xfers_info rx_minfo;
126         struct idr tx_idr;
127         struct idr rx_idr;
128         struct idr protocols;
129         /* Ensure mutual exclusive access to protocols instance array */
130         struct mutex protocols_mtx;
131         u8 *protocols_imp;
132         struct list_head node;
133         int users;
134 };
135
136 #define handle_to_scmi_info(h)  container_of(h, struct scmi_info, handle)
137
138 static const int scmi_linux_errmap[] = {
139         /* better than switch case as long as return value is continuous */
140         0,                      /* SCMI_SUCCESS */
141         -EOPNOTSUPP,            /* SCMI_ERR_SUPPORT */
142         -EINVAL,                /* SCMI_ERR_PARAM */
143         -EACCES,                /* SCMI_ERR_ACCESS */
144         -ENOENT,                /* SCMI_ERR_ENTRY */
145         -ERANGE,                /* SCMI_ERR_RANGE */
146         -EBUSY,                 /* SCMI_ERR_BUSY */
147         -ECOMM,                 /* SCMI_ERR_COMMS */
148         -EIO,                   /* SCMI_ERR_GENERIC */
149         -EREMOTEIO,             /* SCMI_ERR_HARDWARE */
150         -EPROTO,                /* SCMI_ERR_PROTOCOL */
151 };
152
153 static inline int scmi_to_linux_errno(int errno)
154 {
155         if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
156                 return scmi_linux_errmap[-errno];
157         return -EIO;
158 }
159
160 /**
161  * scmi_dump_header_dbg() - Helper to dump a message header.
162  *
163  * @dev: Device pointer corresponding to the SCMI entity
164  * @hdr: pointer to header.
165  */
166 static inline void scmi_dump_header_dbg(struct device *dev,
167                                         struct scmi_msg_hdr *hdr)
168 {
169         dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
170                 hdr->id, hdr->seq, hdr->protocol_id);
171 }
172
173 /**
174  * scmi_xfer_get() - Allocate one message
175  *
176  * @handle: Pointer to SCMI entity handle
177  * @minfo: Pointer to Tx/Rx Message management info based on channel type
178  *
179  * Helper function which is used by various message functions that are
180  * exposed to clients of this driver for allocating a message traffic event.
181  *
182  * This function can sleep depending on pending requests already in the system
183  * for the SCMI entity. Further, this also holds a spinlock to maintain
184  * integrity of internal data structures.
185  *
186  * Return: 0 if all went fine, else corresponding error.
187  */
188 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
189                                        struct scmi_xfers_info *minfo)
190 {
191         u16 xfer_id;
192         struct scmi_xfer *xfer;
193         unsigned long flags, bit_pos;
194         struct scmi_info *info = handle_to_scmi_info(handle);
195
196         /* Keep the locked section as small as possible */
197         spin_lock_irqsave(&minfo->xfer_lock, flags);
198         bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
199                                       info->desc->max_msg);
200         if (bit_pos == info->desc->max_msg) {
201                 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
202                 return ERR_PTR(-ENOMEM);
203         }
204         set_bit(bit_pos, minfo->xfer_alloc_table);
205         spin_unlock_irqrestore(&minfo->xfer_lock, flags);
206
207         xfer_id = bit_pos;
208
209         xfer = &minfo->xfer_block[xfer_id];
210         xfer->hdr.seq = xfer_id;
211         reinit_completion(&xfer->done);
212         xfer->transfer_id = atomic_inc_return(&transfer_last_id);
213
214         return xfer;
215 }
216
217 /**
218  * __scmi_xfer_put() - Release a message
219  *
220  * @minfo: Pointer to Tx/Rx Message management info based on channel type
221  * @xfer: message that was reserved by scmi_xfer_get
222  *
223  * This holds a spinlock to maintain integrity of internal data structures.
224  */
225 static void
226 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
227 {
228         unsigned long flags;
229
230         /*
231          * Keep the locked section as small as possible
232          * NOTE: we might escape with smp_mb and no lock here..
233          * but just be conservative and symmetric.
234          */
235         spin_lock_irqsave(&minfo->xfer_lock, flags);
236         clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
237         spin_unlock_irqrestore(&minfo->xfer_lock, flags);
238 }
239
240 static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr)
241 {
242         struct scmi_xfer *xfer;
243         struct device *dev = cinfo->dev;
244         struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
245         struct scmi_xfers_info *minfo = &info->rx_minfo;
246         ktime_t ts;
247
248         ts = ktime_get_boottime();
249         xfer = scmi_xfer_get(cinfo->handle, minfo);
250         if (IS_ERR(xfer)) {
251                 dev_err(dev, "failed to get free message slot (%ld)\n",
252                         PTR_ERR(xfer));
253                 info->desc->ops->clear_channel(cinfo);
254                 return;
255         }
256
257         unpack_scmi_header(msg_hdr, &xfer->hdr);
258         scmi_dump_header_dbg(dev, &xfer->hdr);
259         info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
260                                             xfer);
261         scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
262                     xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
263
264         trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
265                            xfer->hdr.protocol_id, xfer->hdr.seq,
266                            MSG_TYPE_NOTIFICATION);
267
268         __scmi_xfer_put(minfo, xfer);
269
270         info->desc->ops->clear_channel(cinfo);
271 }
272
273 static void scmi_handle_response(struct scmi_chan_info *cinfo,
274                                  u16 xfer_id, u8 msg_type)
275 {
276         struct scmi_xfer *xfer;
277         struct device *dev = cinfo->dev;
278         struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
279         struct scmi_xfers_info *minfo = &info->tx_minfo;
280
281         /* Are we even expecting this? */
282         if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
283                 dev_err(dev, "message for %d is not expected!\n", xfer_id);
284                 info->desc->ops->clear_channel(cinfo);
285                 return;
286         }
287
288         xfer = &minfo->xfer_block[xfer_id];
289         /*
290          * Even if a response was indeed expected on this slot at this point,
291          * a buggy platform could wrongly reply feeding us an unexpected
292          * delayed response we're not prepared to handle: bail-out safely
293          * blaming firmware.
294          */
295         if (unlikely(msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done)) {
296                 dev_err(dev,
297                         "Delayed Response for %d not expected! Buggy F/W ?\n",
298                         xfer_id);
299                 info->desc->ops->clear_channel(cinfo);
300                 /* It was unexpected, so nobody will clear the xfer if not us */
301                 __scmi_xfer_put(minfo, xfer);
302                 return;
303         }
304
305         scmi_dump_header_dbg(dev, &xfer->hdr);
306
307         info->desc->ops->fetch_response(cinfo, xfer);
308
309         trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
310                            xfer->hdr.protocol_id, xfer->hdr.seq,
311                            msg_type);
312
313         if (msg_type == MSG_TYPE_DELAYED_RESP) {
314                 info->desc->ops->clear_channel(cinfo);
315                 complete(xfer->async_done);
316         } else {
317                 complete(&xfer->done);
318         }
319 }
320
321 /**
322  * scmi_rx_callback() - callback for receiving messages
323  *
324  * @cinfo: SCMI channel info
325  * @msg_hdr: Message header
326  *
327  * Processes one received message to appropriate transfer information and
328  * signals completion of the transfer.
329  *
330  * NOTE: This function will be invoked in IRQ context, hence should be
331  * as optimal as possible.
332  */
333 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr)
334 {
335         u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
336         u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
337
338         switch (msg_type) {
339         case MSG_TYPE_NOTIFICATION:
340                 scmi_handle_notification(cinfo, msg_hdr);
341                 break;
342         case MSG_TYPE_COMMAND:
343         case MSG_TYPE_DELAYED_RESP:
344                 scmi_handle_response(cinfo, xfer_id, msg_type);
345                 break;
346         default:
347                 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
348                 break;
349         }
350 }
351
352 /**
353  * xfer_put() - Release a transmit message
354  *
355  * @ph: Pointer to SCMI protocol handle
356  * @xfer: message that was reserved by scmi_xfer_get
357  */
358 static void xfer_put(const struct scmi_protocol_handle *ph,
359                      struct scmi_xfer *xfer)
360 {
361         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
362         struct scmi_info *info = handle_to_scmi_info(pi->handle);
363
364         __scmi_xfer_put(&info->tx_minfo, xfer);
365 }
366
367 #define SCMI_MAX_POLL_TO_NS     (100 * NSEC_PER_USEC)
368
369 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
370                                       struct scmi_xfer *xfer, ktime_t stop)
371 {
372         struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
373
374         return info->desc->ops->poll_done(cinfo, xfer) ||
375                ktime_after(ktime_get(), stop);
376 }
377
378 /**
379  * do_xfer() - Do one transfer
380  *
381  * @ph: Pointer to SCMI protocol handle
382  * @xfer: Transfer to initiate and wait for response
383  *
384  * Return: -ETIMEDOUT in case of no response, if transmit error,
385  *      return corresponding error, else if all goes well,
386  *      return 0.
387  */
388 static int do_xfer(const struct scmi_protocol_handle *ph,
389                    struct scmi_xfer *xfer)
390 {
391         int ret;
392         int timeout;
393         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
394         struct scmi_info *info = handle_to_scmi_info(pi->handle);
395         struct device *dev = info->dev;
396         struct scmi_chan_info *cinfo;
397
398         /*
399          * Re-instate protocol id here from protocol handle so that cannot be
400          * overridden by mistake (or malice) by the protocol code mangling with
401          * the scmi_xfer structure.
402          */
403         xfer->hdr.protocol_id = pi->proto->id;
404
405         cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
406         if (unlikely(!cinfo))
407                 return -EINVAL;
408
409         trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
410                               xfer->hdr.protocol_id, xfer->hdr.seq,
411                               xfer->hdr.poll_completion);
412
413         ret = info->desc->ops->send_message(cinfo, xfer);
414         if (ret < 0) {
415                 dev_dbg(dev, "Failed to send message %d\n", ret);
416                 return ret;
417         }
418
419         if (xfer->hdr.poll_completion) {
420                 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
421
422                 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
423
424                 if (ktime_before(ktime_get(), stop))
425                         info->desc->ops->fetch_response(cinfo, xfer);
426                 else
427                         ret = -ETIMEDOUT;
428         } else {
429                 /* And we wait for the response. */
430                 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
431                 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
432                         dev_err(dev, "timed out in resp(caller: %pS)\n",
433                                 (void *)_RET_IP_);
434                         ret = -ETIMEDOUT;
435                 }
436         }
437
438         if (!ret && xfer->hdr.status)
439                 ret = scmi_to_linux_errno(xfer->hdr.status);
440
441         if (info->desc->ops->mark_txdone)
442                 info->desc->ops->mark_txdone(cinfo, ret);
443
444         trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
445                             xfer->hdr.protocol_id, xfer->hdr.seq, ret);
446
447         return ret;
448 }
449
450 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
451                               struct scmi_xfer *xfer)
452 {
453         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
454         struct scmi_info *info = handle_to_scmi_info(pi->handle);
455
456         xfer->rx.len = info->desc->max_msg_size;
457 }
458
459 #define SCMI_MAX_RESPONSE_TIMEOUT       (2 * MSEC_PER_SEC)
460
461 /**
462  * do_xfer_with_response() - Do one transfer and wait until the delayed
463  *      response is received
464  *
465  * @ph: Pointer to SCMI protocol handle
466  * @xfer: Transfer to initiate and wait for response
467  *
468  * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
469  *      return corresponding error, else if all goes well, return 0.
470  */
471 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
472                                  struct scmi_xfer *xfer)
473 {
474         int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
475         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
476         DECLARE_COMPLETION_ONSTACK(async_response);
477
478         xfer->hdr.protocol_id = pi->proto->id;
479
480         xfer->async_done = &async_response;
481
482         ret = do_xfer(ph, xfer);
483         if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout))
484                 ret = -ETIMEDOUT;
485
486         xfer->async_done = NULL;
487         return ret;
488 }
489
490 /**
491  * xfer_get_init() - Allocate and initialise one message for transmit
492  *
493  * @ph: Pointer to SCMI protocol handle
494  * @msg_id: Message identifier
495  * @tx_size: transmit message size
496  * @rx_size: receive message size
497  * @p: pointer to the allocated and initialised message
498  *
499  * This function allocates the message using @scmi_xfer_get and
500  * initialise the header.
501  *
502  * Return: 0 if all went fine with @p pointing to message, else
503  *      corresponding error.
504  */
505 static int xfer_get_init(const struct scmi_protocol_handle *ph,
506                          u8 msg_id, size_t tx_size, size_t rx_size,
507                          struct scmi_xfer **p)
508 {
509         int ret;
510         struct scmi_xfer *xfer;
511         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
512         struct scmi_info *info = handle_to_scmi_info(pi->handle);
513         struct scmi_xfers_info *minfo = &info->tx_minfo;
514         struct device *dev = info->dev;
515
516         /* Ensure we have sane transfer sizes */
517         if (rx_size > info->desc->max_msg_size ||
518             tx_size > info->desc->max_msg_size)
519                 return -ERANGE;
520
521         xfer = scmi_xfer_get(pi->handle, minfo);
522         if (IS_ERR(xfer)) {
523                 ret = PTR_ERR(xfer);
524                 dev_err(dev, "failed to get free message slot(%d)\n", ret);
525                 return ret;
526         }
527
528         xfer->tx.len = tx_size;
529         xfer->rx.len = rx_size ? : info->desc->max_msg_size;
530         xfer->hdr.id = msg_id;
531         xfer->hdr.protocol_id = pi->proto->id;
532         xfer->hdr.poll_completion = false;
533
534         *p = xfer;
535
536         return 0;
537 }
538
539 /**
540  * version_get() - command to get the revision of the SCMI entity
541  *
542  * @ph: Pointer to SCMI protocol handle
543  * @version: Holds returned version of protocol.
544  *
545  * Updates the SCMI information in the internal data structure.
546  *
547  * Return: 0 if all went fine, else return appropriate error.
548  */
549 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
550 {
551         int ret;
552         __le32 *rev_info;
553         struct scmi_xfer *t;
554
555         ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
556         if (ret)
557                 return ret;
558
559         ret = do_xfer(ph, t);
560         if (!ret) {
561                 rev_info = t->rx.buf;
562                 *version = le32_to_cpu(*rev_info);
563         }
564
565         xfer_put(ph, t);
566         return ret;
567 }
568
569 /**
570  * scmi_set_protocol_priv  - Set protocol specific data at init time
571  *
572  * @ph: A reference to the protocol handle.
573  * @priv: The private data to set.
574  *
575  * Return: 0 on Success
576  */
577 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
578                                   void *priv)
579 {
580         struct scmi_protocol_instance *pi = ph_to_pi(ph);
581
582         pi->priv = priv;
583
584         return 0;
585 }
586
587 /**
588  * scmi_get_protocol_priv  - Set protocol specific data at init time
589  *
590  * @ph: A reference to the protocol handle.
591  *
592  * Return: Protocol private data if any was set.
593  */
594 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
595 {
596         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
597
598         return pi->priv;
599 }
600
601 static const struct scmi_xfer_ops xfer_ops = {
602         .version_get = version_get,
603         .xfer_get_init = xfer_get_init,
604         .reset_rx_to_maxsz = reset_rx_to_maxsz,
605         .do_xfer = do_xfer,
606         .do_xfer_with_response = do_xfer_with_response,
607         .xfer_put = xfer_put,
608 };
609
610 /**
611  * scmi_revision_area_get  - Retrieve version memory area.
612  *
613  * @ph: A reference to the protocol handle.
614  *
615  * A helper to grab the version memory area reference during SCMI Base protocol
616  * initialization.
617  *
618  * Return: A reference to the version memory area associated to the SCMI
619  *         instance underlying this protocol handle.
620  */
621 struct scmi_revision_info *
622 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
623 {
624         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
625
626         return pi->handle->version;
627 }
628
629 /**
630  * scmi_alloc_init_protocol_instance  - Allocate and initialize a protocol
631  * instance descriptor.
632  * @info: The reference to the related SCMI instance.
633  * @proto: The protocol descriptor.
634  *
635  * Allocate a new protocol instance descriptor, using the provided @proto
636  * description, against the specified SCMI instance @info, and initialize it;
637  * all resources management is handled via a dedicated per-protocol devres
638  * group.
639  *
640  * Context: Assumes to be called with @protocols_mtx already acquired.
641  * Return: A reference to a freshly allocated and initialized protocol instance
642  *         or ERR_PTR on failure.
643  */
644 static struct scmi_protocol_instance *
645 scmi_alloc_init_protocol_instance(struct scmi_info *info,
646                                   const struct scmi_protocol *proto)
647 {
648         int ret = -ENOMEM;
649         void *gid;
650         struct scmi_protocol_instance *pi;
651         const struct scmi_handle *handle = &info->handle;
652
653         /* Protocol specific devres group */
654         gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
655         if (!gid)
656                 goto out;
657
658         pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
659         if (!pi)
660                 goto clean;
661
662         pi->gid = gid;
663         pi->proto = proto;
664         pi->handle = handle;
665         pi->ph.dev = handle->dev;
666         pi->ph.xops = &xfer_ops;
667         pi->ph.set_priv = scmi_set_protocol_priv;
668         pi->ph.get_priv = scmi_get_protocol_priv;
669         refcount_set(&pi->users, 1);
670         /* proto->init is assured NON NULL by scmi_protocol_register */
671         ret = pi->proto->instance_init(&pi->ph);
672         if (ret)
673                 goto clean;
674
675         ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
676                         GFP_KERNEL);
677         if (ret != proto->id)
678                 goto clean;
679
680         /*
681          * Warn but ignore events registration errors since we do not want
682          * to skip whole protocols if their notifications are messed up.
683          */
684         if (pi->proto->events) {
685                 ret = scmi_register_protocol_events(handle, pi->proto->id,
686                                                     &pi->ph,
687                                                     pi->proto->events);
688                 if (ret)
689                         dev_warn(handle->dev,
690                                  "Protocol:%X - Events Registration Failed - err:%d\n",
691                                  pi->proto->id, ret);
692         }
693
694         devres_close_group(handle->dev, pi->gid);
695         dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
696
697         return pi;
698
699 clean:
700         devres_release_group(handle->dev, gid);
701 out:
702         return ERR_PTR(ret);
703 }
704
705 /**
706  * scmi_get_protocol_instance  - Protocol initialization helper.
707  * @handle: A reference to the SCMI platform instance.
708  * @protocol_id: The protocol being requested.
709  *
710  * In case the required protocol has never been requested before for this
711  * instance, allocate and initialize all the needed structures while handling
712  * resource allocation with a dedicated per-protocol devres subgroup.
713  *
714  * Return: A reference to an initialized protocol instance or error on failure.
715  */
716 static struct scmi_protocol_instance * __must_check
717 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
718 {
719         struct scmi_protocol_instance *pi;
720         struct scmi_info *info = handle_to_scmi_info(handle);
721
722         mutex_lock(&info->protocols_mtx);
723         pi = idr_find(&info->protocols, protocol_id);
724
725         if (pi) {
726                 refcount_inc(&pi->users);
727         } else {
728                 const struct scmi_protocol *proto;
729
730                 /* Fails if protocol not registered on bus */
731                 proto = scmi_protocol_get(protocol_id);
732                 if (proto)
733                         pi = scmi_alloc_init_protocol_instance(info, proto);
734                 else
735                         pi = ERR_PTR(-ENODEV);
736         }
737         mutex_unlock(&info->protocols_mtx);
738
739         return pi;
740 }
741
742 /**
743  * scmi_protocol_acquire  - Protocol acquire
744  * @handle: A reference to the SCMI platform instance.
745  * @protocol_id: The protocol being requested.
746  *
747  * Register a new user for the requested protocol on the specified SCMI
748  * platform instance, possibly triggering its initialization on first user.
749  *
750  * Return: 0 if protocol was acquired successfully.
751  */
752 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
753 {
754         return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
755 }
756
757 /**
758  * scmi_protocol_release  - Protocol de-initialization helper.
759  * @handle: A reference to the SCMI platform instance.
760  * @protocol_id: The protocol being requested.
761  *
762  * Remove one user for the specified protocol and triggers de-initialization
763  * and resources de-allocation once the last user has gone.
764  */
765 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
766 {
767         struct scmi_info *info = handle_to_scmi_info(handle);
768         struct scmi_protocol_instance *pi;
769
770         mutex_lock(&info->protocols_mtx);
771         pi = idr_find(&info->protocols, protocol_id);
772         if (WARN_ON(!pi))
773                 goto out;
774
775         if (refcount_dec_and_test(&pi->users)) {
776                 void *gid = pi->gid;
777
778                 if (pi->proto->events)
779                         scmi_deregister_protocol_events(handle, protocol_id);
780
781                 if (pi->proto->instance_deinit)
782                         pi->proto->instance_deinit(&pi->ph);
783
784                 idr_remove(&info->protocols, protocol_id);
785
786                 devres_release_group(handle->dev, gid);
787                 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
788                         protocol_id);
789         }
790
791 out:
792         mutex_unlock(&info->protocols_mtx);
793 }
794
795 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
796                                      u8 *prot_imp)
797 {
798         const struct scmi_protocol_instance *pi = ph_to_pi(ph);
799         struct scmi_info *info = handle_to_scmi_info(pi->handle);
800
801         info->protocols_imp = prot_imp;
802 }
803
804 static bool
805 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
806 {
807         int i;
808         struct scmi_info *info = handle_to_scmi_info(handle);
809
810         if (!info->protocols_imp)
811                 return false;
812
813         for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
814                 if (info->protocols_imp[i] == prot_id)
815                         return true;
816         return false;
817 }
818
819 struct scmi_protocol_devres {
820         const struct scmi_handle *handle;
821         u8 protocol_id;
822 };
823
824 static void scmi_devm_release_protocol(struct device *dev, void *res)
825 {
826         struct scmi_protocol_devres *dres = res;
827
828         scmi_protocol_release(dres->handle, dres->protocol_id);
829 }
830
831 /**
832  * scmi_devm_protocol_get  - Devres managed get protocol operations and handle
833  * @sdev: A reference to an scmi_device whose embedded struct device is to
834  *        be used for devres accounting.
835  * @protocol_id: The protocol being requested.
836  * @ph: A pointer reference used to pass back the associated protocol handle.
837  *
838  * Get hold of a protocol accounting for its usage, eventually triggering its
839  * initialization, and returning the protocol specific operations and related
840  * protocol handle which will be used as first argument in most of the
841  * protocols operations methods.
842  * Being a devres based managed method, protocol hold will be automatically
843  * released, and possibly de-initialized on last user, once the SCMI driver
844  * owning the scmi_device is unbound from it.
845  *
846  * Return: A reference to the requested protocol operations or error.
847  *         Must be checked for errors by caller.
848  */
849 static const void __must_check *
850 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
851                        struct scmi_protocol_handle **ph)
852 {
853         struct scmi_protocol_instance *pi;
854         struct scmi_protocol_devres *dres;
855         struct scmi_handle *handle = sdev->handle;
856
857         if (!ph)
858                 return ERR_PTR(-EINVAL);
859
860         dres = devres_alloc(scmi_devm_release_protocol,
861                             sizeof(*dres), GFP_KERNEL);
862         if (!dres)
863                 return ERR_PTR(-ENOMEM);
864
865         pi = scmi_get_protocol_instance(handle, protocol_id);
866         if (IS_ERR(pi)) {
867                 devres_free(dres);
868                 return pi;
869         }
870
871         dres->handle = handle;
872         dres->protocol_id = protocol_id;
873         devres_add(&sdev->dev, dres);
874
875         *ph = &pi->ph;
876
877         return pi->proto->ops;
878 }
879
880 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
881 {
882         struct scmi_protocol_devres *dres = res;
883
884         if (WARN_ON(!dres || !data))
885                 return 0;
886
887         return dres->protocol_id == *((u8 *)data);
888 }
889
890 /**
891  * scmi_devm_protocol_put  - Devres managed put protocol operations and handle
892  * @sdev: A reference to an scmi_device whose embedded struct device is to
893  *        be used for devres accounting.
894  * @protocol_id: The protocol being requested.
895  *
896  * Explicitly release a protocol hold previously obtained calling the above
897  * @scmi_devm_protocol_get.
898  */
899 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
900 {
901         int ret;
902
903         ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
904                              scmi_devm_protocol_match, &protocol_id);
905         WARN_ON(ret);
906 }
907
908 /**
909  * scmi_handle_get() - Get the SCMI handle for a device
910  *
911  * @dev: pointer to device for which we want SCMI handle
912  *
913  * NOTE: The function does not track individual clients of the framework
914  * and is expected to be maintained by caller of SCMI protocol library.
915  * scmi_handle_put must be balanced with successful scmi_handle_get
916  *
917  * Return: pointer to handle if successful, NULL on error
918  */
919 struct scmi_handle *scmi_handle_get(struct device *dev)
920 {
921         struct list_head *p;
922         struct scmi_info *info;
923         struct scmi_handle *handle = NULL;
924
925         mutex_lock(&scmi_list_mutex);
926         list_for_each(p, &scmi_list) {
927                 info = list_entry(p, struct scmi_info, node);
928                 if (dev->parent == info->dev) {
929                         handle = &info->handle;
930                         info->users++;
931                         break;
932                 }
933         }
934         mutex_unlock(&scmi_list_mutex);
935
936         return handle;
937 }
938
939 /**
940  * scmi_handle_put() - Release the handle acquired by scmi_handle_get
941  *
942  * @handle: handle acquired by scmi_handle_get
943  *
944  * NOTE: The function does not track individual clients of the framework
945  * and is expected to be maintained by caller of SCMI protocol library.
946  * scmi_handle_put must be balanced with successful scmi_handle_get
947  *
948  * Return: 0 is successfully released
949  *      if null was passed, it returns -EINVAL;
950  */
951 int scmi_handle_put(const struct scmi_handle *handle)
952 {
953         struct scmi_info *info;
954
955         if (!handle)
956                 return -EINVAL;
957
958         info = handle_to_scmi_info(handle);
959         mutex_lock(&scmi_list_mutex);
960         if (!WARN_ON(!info->users))
961                 info->users--;
962         mutex_unlock(&scmi_list_mutex);
963
964         return 0;
965 }
966
967 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
968                                  struct scmi_xfers_info *info)
969 {
970         int i;
971         struct scmi_xfer *xfer;
972         struct device *dev = sinfo->dev;
973         const struct scmi_desc *desc = sinfo->desc;
974
975         /* Pre-allocated messages, no more than what hdr.seq can support */
976         if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
977                 dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
978                         desc->max_msg, MSG_TOKEN_MAX);
979                 return -EINVAL;
980         }
981
982         info->xfer_block = devm_kcalloc(dev, desc->max_msg,
983                                         sizeof(*info->xfer_block), GFP_KERNEL);
984         if (!info->xfer_block)
985                 return -ENOMEM;
986
987         info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
988                                               sizeof(long), GFP_KERNEL);
989         if (!info->xfer_alloc_table)
990                 return -ENOMEM;
991
992         /* Pre-initialize the buffer pointer to pre-allocated buffers */
993         for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
994                 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
995                                             GFP_KERNEL);
996                 if (!xfer->rx.buf)
997                         return -ENOMEM;
998
999                 xfer->tx.buf = xfer->rx.buf;
1000                 init_completion(&xfer->done);
1001         }
1002
1003         spin_lock_init(&info->xfer_lock);
1004
1005         return 0;
1006 }
1007
1008 static int scmi_xfer_info_init(struct scmi_info *sinfo)
1009 {
1010         int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
1011
1012         if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1013                 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1014
1015         return ret;
1016 }
1017
1018 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1019                            int prot_id, bool tx)
1020 {
1021         int ret, idx;
1022         struct scmi_chan_info *cinfo;
1023         struct idr *idr;
1024
1025         /* Transmit channel is first entry i.e. index 0 */
1026         idx = tx ? 0 : 1;
1027         idr = tx ? &info->tx_idr : &info->rx_idr;
1028
1029         /* check if already allocated, used for multiple device per protocol */
1030         cinfo = idr_find(idr, prot_id);
1031         if (cinfo)
1032                 return 0;
1033
1034         if (!info->desc->ops->chan_available(dev, idx)) {
1035                 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1036                 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1037                         return -EINVAL;
1038                 goto idr_alloc;
1039         }
1040
1041         cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1042         if (!cinfo)
1043                 return -ENOMEM;
1044
1045         cinfo->dev = dev;
1046
1047         ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1048         if (ret)
1049                 return ret;
1050
1051 idr_alloc:
1052         ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
1053         if (ret != prot_id) {
1054                 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1055                 return ret;
1056         }
1057
1058         cinfo->handle = &info->handle;
1059         return 0;
1060 }
1061
1062 static inline int
1063 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
1064 {
1065         int ret = scmi_chan_setup(info, dev, prot_id, true);
1066
1067         if (!ret) /* Rx is optional, hence no error check */
1068                 scmi_chan_setup(info, dev, prot_id, false);
1069
1070         return ret;
1071 }
1072
1073 static inline void
1074 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
1075                             int prot_id, const char *name)
1076 {
1077         struct scmi_device *sdev;
1078
1079         sdev = scmi_device_create(np, info->dev, prot_id, name);
1080         if (!sdev) {
1081                 dev_err(info->dev, "failed to create %d protocol device\n",
1082                         prot_id);
1083                 return;
1084         }
1085
1086         if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
1087                 dev_err(&sdev->dev, "failed to setup transport\n");
1088                 scmi_device_destroy(sdev);
1089                 return;
1090         }
1091
1092         /* setup handle now as the transport is ready */
1093         scmi_set_handle(sdev);
1094 }
1095
1096 #define MAX_SCMI_DEV_PER_PROTOCOL       2
1097 struct scmi_prot_devnames {
1098         int protocol_id;
1099         char *names[MAX_SCMI_DEV_PER_PROTOCOL];
1100 };
1101
1102 static struct scmi_prot_devnames devnames[] = {
1103         { SCMI_PROTOCOL_POWER,  { "genpd" },},
1104         { SCMI_PROTOCOL_SYSTEM, { "syspower" },},
1105         { SCMI_PROTOCOL_PERF,   { "cpufreq" },},
1106         { SCMI_PROTOCOL_CLOCK,  { "clocks" },},
1107         { SCMI_PROTOCOL_SENSOR, { "hwmon", "iiodev" },},
1108         { SCMI_PROTOCOL_RESET,  { "reset" },},
1109         { SCMI_PROTOCOL_VOLTAGE,  { "regulator" },},
1110 };
1111
1112 static inline void
1113 scmi_create_protocol_devices(struct device_node *np, struct scmi_info *info,
1114                              int prot_id)
1115 {
1116         int loop, cnt;
1117
1118         for (loop = 0; loop < ARRAY_SIZE(devnames); loop++) {
1119                 if (devnames[loop].protocol_id != prot_id)
1120                         continue;
1121
1122                 for (cnt = 0; cnt < ARRAY_SIZE(devnames[loop].names); cnt++) {
1123                         const char *name = devnames[loop].names[cnt];
1124
1125                         if (name)
1126                                 scmi_create_protocol_device(np, info, prot_id,
1127                                                             name);
1128                 }
1129         }
1130 }
1131
1132 static int scmi_probe(struct platform_device *pdev)
1133 {
1134         int ret;
1135         struct scmi_handle *handle;
1136         const struct scmi_desc *desc;
1137         struct scmi_info *info;
1138         struct device *dev = &pdev->dev;
1139         struct device_node *child, *np = dev->of_node;
1140
1141         desc = of_device_get_match_data(dev);
1142         if (!desc)
1143                 return -EINVAL;
1144
1145         info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1146         if (!info)
1147                 return -ENOMEM;
1148
1149         info->dev = dev;
1150         info->desc = desc;
1151         INIT_LIST_HEAD(&info->node);
1152         idr_init(&info->protocols);
1153         mutex_init(&info->protocols_mtx);
1154
1155         platform_set_drvdata(pdev, info);
1156         idr_init(&info->tx_idr);
1157         idr_init(&info->rx_idr);
1158
1159         handle = &info->handle;
1160         handle->dev = info->dev;
1161         handle->version = &info->version;
1162         handle->devm_protocol_get = scmi_devm_protocol_get;
1163         handle->devm_protocol_put = scmi_devm_protocol_put;
1164
1165         ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
1166         if (ret)
1167                 return ret;
1168
1169         ret = scmi_xfer_info_init(info);
1170         if (ret)
1171                 return ret;
1172
1173         if (scmi_notification_init(handle))
1174                 dev_err(dev, "SCMI Notifications NOT available.\n");
1175
1176         /*
1177          * Trigger SCMI Base protocol initialization.
1178          * It's mandatory and won't be ever released/deinit until the
1179          * SCMI stack is shutdown/unloaded as a whole.
1180          */
1181         ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
1182         if (ret) {
1183                 dev_err(dev, "unable to communicate with SCMI\n");
1184                 return ret;
1185         }
1186
1187         mutex_lock(&scmi_list_mutex);
1188         list_add_tail(&info->node, &scmi_list);
1189         mutex_unlock(&scmi_list_mutex);
1190
1191         for_each_available_child_of_node(np, child) {
1192                 u32 prot_id;
1193
1194                 if (of_property_read_u32(child, "reg", &prot_id))
1195                         continue;
1196
1197                 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
1198                         dev_err(dev, "Out of range protocol %d\n", prot_id);
1199
1200                 if (!scmi_is_protocol_implemented(handle, prot_id)) {
1201                         dev_err(dev, "SCMI protocol %d not implemented\n",
1202                                 prot_id);
1203                         continue;
1204                 }
1205
1206                 scmi_create_protocol_devices(child, info, prot_id);
1207         }
1208
1209         return 0;
1210 }
1211
1212 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
1213 {
1214         idr_remove(idr, id);
1215 }
1216
1217 static int scmi_remove(struct platform_device *pdev)
1218 {
1219         int ret = 0;
1220         struct scmi_info *info = platform_get_drvdata(pdev);
1221         struct idr *idr = &info->tx_idr;
1222
1223         mutex_lock(&scmi_list_mutex);
1224         if (info->users)
1225                 ret = -EBUSY;
1226         else
1227                 list_del(&info->node);
1228         mutex_unlock(&scmi_list_mutex);
1229
1230         if (ret)
1231                 return ret;
1232
1233         scmi_notification_exit(&info->handle);
1234
1235         mutex_lock(&info->protocols_mtx);
1236         idr_destroy(&info->protocols);
1237         mutex_unlock(&info->protocols_mtx);
1238
1239         /* Safe to free channels since no more users */
1240         ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1241         idr_destroy(&info->tx_idr);
1242
1243         idr = &info->rx_idr;
1244         ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1245         idr_destroy(&info->rx_idr);
1246
1247         return ret;
1248 }
1249
1250 static ssize_t protocol_version_show(struct device *dev,
1251                                      struct device_attribute *attr, char *buf)
1252 {
1253         struct scmi_info *info = dev_get_drvdata(dev);
1254
1255         return sprintf(buf, "%u.%u\n", info->version.major_ver,
1256                        info->version.minor_ver);
1257 }
1258 static DEVICE_ATTR_RO(protocol_version);
1259
1260 static ssize_t firmware_version_show(struct device *dev,
1261                                      struct device_attribute *attr, char *buf)
1262 {
1263         struct scmi_info *info = dev_get_drvdata(dev);
1264
1265         return sprintf(buf, "0x%x\n", info->version.impl_ver);
1266 }
1267 static DEVICE_ATTR_RO(firmware_version);
1268
1269 static ssize_t vendor_id_show(struct device *dev,
1270                               struct device_attribute *attr, char *buf)
1271 {
1272         struct scmi_info *info = dev_get_drvdata(dev);
1273
1274         return sprintf(buf, "%s\n", info->version.vendor_id);
1275 }
1276 static DEVICE_ATTR_RO(vendor_id);
1277
1278 static ssize_t sub_vendor_id_show(struct device *dev,
1279                                   struct device_attribute *attr, char *buf)
1280 {
1281         struct scmi_info *info = dev_get_drvdata(dev);
1282
1283         return sprintf(buf, "%s\n", info->version.sub_vendor_id);
1284 }
1285 static DEVICE_ATTR_RO(sub_vendor_id);
1286
1287 static struct attribute *versions_attrs[] = {
1288         &dev_attr_firmware_version.attr,
1289         &dev_attr_protocol_version.attr,
1290         &dev_attr_vendor_id.attr,
1291         &dev_attr_sub_vendor_id.attr,
1292         NULL,
1293 };
1294 ATTRIBUTE_GROUPS(versions);
1295
1296 /* Each compatible listed below must have descriptor associated with it */
1297 static const struct of_device_id scmi_of_match[] = {
1298         { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
1299 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
1300         { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
1301 #endif
1302         { /* Sentinel */ },
1303 };
1304
1305 MODULE_DEVICE_TABLE(of, scmi_of_match);
1306
1307 static struct platform_driver scmi_driver = {
1308         .driver = {
1309                    .name = "arm-scmi",
1310                    .of_match_table = scmi_of_match,
1311                    .dev_groups = versions_groups,
1312                    },
1313         .probe = scmi_probe,
1314         .remove = scmi_remove,
1315 };
1316
1317 static int __init scmi_driver_init(void)
1318 {
1319         scmi_bus_init();
1320
1321         scmi_base_register();
1322
1323         scmi_clock_register();
1324         scmi_perf_register();
1325         scmi_power_register();
1326         scmi_reset_register();
1327         scmi_sensors_register();
1328         scmi_voltage_register();
1329         scmi_system_register();
1330
1331         return platform_driver_register(&scmi_driver);
1332 }
1333 subsys_initcall(scmi_driver_init);
1334
1335 static void __exit scmi_driver_exit(void)
1336 {
1337         scmi_base_unregister();
1338
1339         scmi_clock_unregister();
1340         scmi_perf_unregister();
1341         scmi_power_unregister();
1342         scmi_reset_unregister();
1343         scmi_sensors_unregister();
1344         scmi_voltage_unregister();
1345         scmi_system_unregister();
1346
1347         scmi_bus_exit();
1348
1349         platform_driver_unregister(&scmi_driver);
1350 }
1351 module_exit(scmi_driver_exit);
1352
1353 MODULE_ALIAS("platform: arm-scmi");
1354 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1355 MODULE_DESCRIPTION("ARM SCMI protocol driver");
1356 MODULE_LICENSE("GPL v2");