Commit | Line | Data |
---|---|---|
aa4f886f SH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * System Control and Management Interface (SCMI) Message Protocol driver | |
4 | * | |
5 | * SCMI Message Protocol is used between the System Control Processor(SCP) | |
6 | * and the Application Processors(AP). The Message Handling Unit(MHU) | |
7 | * provides a mechanism for inter-processor communication between SCP's | |
8 | * Cortex M3 and AP. | |
9 | * | |
10 | * SCP offers control and management of the core/cluster power states, | |
11 | * various power domain DVFS including the core/cluster, certain system | |
12 | * clocks configuration, thermal sensors and many others. | |
13 | * | |
48dc16e2 | 14 | * Copyright (C) 2018-2021 ARM Ltd. |
aa4f886f SH |
15 | */ |
16 | ||
17 | #include <linux/bitmap.h> | |
23934efe | 18 | #include <linux/device.h> |
aa4f886f | 19 | #include <linux/export.h> |
48dc16e2 | 20 | #include <linux/idr.h> |
aa4f886f SH |
21 | #include <linux/io.h> |
22 | #include <linux/kernel.h> | |
d4c3751a | 23 | #include <linux/ktime.h> |
9ca5a183 | 24 | #include <linux/hashtable.h> |
d4f9dddd | 25 | #include <linux/list.h> |
aa4f886f SH |
26 | #include <linux/module.h> |
27 | #include <linux/of_address.h> | |
28 | #include <linux/of_device.h> | |
d4c3751a | 29 | #include <linux/processor.h> |
48dc16e2 | 30 | #include <linux/refcount.h> |
aa4f886f SH |
31 | #include <linux/slab.h> |
32 | ||
33 | #include "common.h" | |
6b8a6913 | 34 | #include "notify.h" |
aa4f886f | 35 | |
729d3530 LL |
36 | #define CREATE_TRACE_POINTS |
37 | #include <trace/events/scmi.h> | |
38 | ||
aa4f886f SH |
39 | enum 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 */ | |
aa4f886f SH |
51 | }; |
52 | ||
1baf47c2 | 53 | /* List of all SCMI devices active in system */ |
aa4f886f SH |
54 | static LIST_HEAD(scmi_list); |
55 | /* Protection for the entire list */ | |
56 | static DEFINE_MUTEX(scmi_list_mutex); | |
729d3530 LL |
57 | /* Track the unique id for the transfers for debug & profiling purpose */ |
58 | static atomic_t transfer_last_id; | |
aa4f886f | 59 | |
d4f9dddd CM |
60 | static DEFINE_IDR(scmi_requested_devices); |
61 | static DEFINE_MUTEX(scmi_requested_devices_mtx); | |
62 | ||
63 | struct scmi_requested_dev { | |
64 | const struct scmi_device_id *id_table; | |
65 | struct list_head node; | |
66 | }; | |
67 | ||
aa4f886f SH |
68 | /** |
69 | * struct scmi_xfers_info - Structure to manage transfer information | |
70 | * | |
aa4f886f SH |
71 | * @xfer_alloc_table: Bitmap table for allocated messages. |
72 | * Index of this bitmap table is also used for message | |
73 | * sequence identifier. | |
74 | * @xfer_lock: Protection for message allocation | |
9ca5a183 CM |
75 | * @free_xfers: A free list for available to use xfers. It is initialized with |
76 | * a number of xfers equal to the maximum allowed in-flight | |
77 | * messages. | |
78 | * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the | |
79 | * currently in-flight messages. | |
aa4f886f SH |
80 | */ |
81 | struct scmi_xfers_info { | |
aa4f886f | 82 | unsigned long *xfer_alloc_table; |
aa4f886f | 83 | spinlock_t xfer_lock; |
9ca5a183 CM |
84 | struct hlist_head free_xfers; |
85 | DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ); | |
aa4f886f SH |
86 | }; |
87 | ||
48dc16e2 CM |
88 | /** |
89 | * struct scmi_protocol_instance - Describe an initialized protocol instance. | |
d7b6cc56 | 90 | * @handle: Reference to the SCMI handle associated to this protocol instance. |
48dc16e2 CM |
91 | * @proto: A reference to the protocol descriptor. |
92 | * @gid: A reference for per-protocol devres management. | |
93 | * @users: A refcount to track effective users of this protocol. | |
d7b6cc56 CM |
94 | * @priv: Reference for optional protocol private data. |
95 | * @ph: An embedded protocol handle that will be passed down to protocol | |
96 | * initialization code to identify this instance. | |
48dc16e2 CM |
97 | * |
98 | * Each protocol is initialized independently once for each SCMI platform in | |
99 | * which is defined by DT and implemented by the SCMI server fw. | |
100 | */ | |
101 | struct scmi_protocol_instance { | |
d7b6cc56 | 102 | const struct scmi_handle *handle; |
48dc16e2 CM |
103 | const struct scmi_protocol *proto; |
104 | void *gid; | |
105 | refcount_t users; | |
d7b6cc56 CM |
106 | void *priv; |
107 | struct scmi_protocol_handle ph; | |
48dc16e2 CM |
108 | }; |
109 | ||
d7b6cc56 CM |
110 | #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph) |
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 | |
b6f20ff8 SH |
117 | * @version: SCMI revision information containing protocol version, |
118 | * implementation version and (sub-)vendor identification. | |
71af05a7 | 119 | * @handle: Instance of SCMI handle to send to clients |
38c927fb | 120 | * @tx_minfo: Universal Transmit Message management info |
4ebd8f6d | 121 | * @rx_minfo: Universal Receive Message management info |
3748daf7 | 122 | * @tx_idr: IDR object to map protocol id to Tx channel info pointer |
46cc7c28 | 123 | * @rx_idr: IDR object to map protocol id to Rx channel info pointer |
48dc16e2 CM |
124 | * @protocols: IDR for protocols' instance descriptors initialized for |
125 | * this SCMI instance: populated on protocol's first attempted | |
126 | * usage. | |
127 | * @protocols_mtx: A mutex to protect protocols instances initialization. | |
1baf47c2 | 128 | * @protocols_imp: List of protocols implemented, currently maximum of |
b6f20ff8 | 129 | * MAX_PROTOCOLS_IMP elements allocated by the base protocol |
d4f9dddd CM |
130 | * @active_protocols: IDR storing device_nodes for protocols actually defined |
131 | * in the DT and confirmed as implemented by fw. | |
a02d7c93 | 132 | * @notify_priv: Pointer to private data structure specific to notifications. |
1baf47c2 | 133 | * @node: List head |
aa4f886f SH |
134 | * @users: Number of users of this instance |
135 | */ | |
136 | struct scmi_info { | |
137 | struct device *dev; | |
138 | const struct scmi_desc *desc; | |
b6f20ff8 | 139 | struct scmi_revision_info version; |
aa4f886f | 140 | struct scmi_handle handle; |
38c927fb | 141 | struct scmi_xfers_info tx_minfo; |
4ebd8f6d | 142 | struct scmi_xfers_info rx_minfo; |
907b6d14 | 143 | struct idr tx_idr; |
46cc7c28 | 144 | struct idr rx_idr; |
48dc16e2 CM |
145 | struct idr protocols; |
146 | /* Ensure mutual exclusive access to protocols instance array */ | |
147 | struct mutex protocols_mtx; | |
b6f20ff8 | 148 | u8 *protocols_imp; |
d4f9dddd | 149 | struct idr active_protocols; |
a02d7c93 | 150 | void *notify_priv; |
aa4f886f SH |
151 | struct list_head node; |
152 | int users; | |
153 | }; | |
154 | ||
aa4f886f SH |
155 | #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle) |
156 | ||
aa4f886f SH |
157 | static const int scmi_linux_errmap[] = { |
158 | /* better than switch case as long as return value is continuous */ | |
159 | 0, /* SCMI_SUCCESS */ | |
160 | -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */ | |
161 | -EINVAL, /* SCMI_ERR_PARAM */ | |
162 | -EACCES, /* SCMI_ERR_ACCESS */ | |
163 | -ENOENT, /* SCMI_ERR_ENTRY */ | |
164 | -ERANGE, /* SCMI_ERR_RANGE */ | |
165 | -EBUSY, /* SCMI_ERR_BUSY */ | |
166 | -ECOMM, /* SCMI_ERR_COMMS */ | |
167 | -EIO, /* SCMI_ERR_GENERIC */ | |
168 | -EREMOTEIO, /* SCMI_ERR_HARDWARE */ | |
169 | -EPROTO, /* SCMI_ERR_PROTOCOL */ | |
170 | }; | |
171 | ||
172 | static inline int scmi_to_linux_errno(int errno) | |
173 | { | |
7a691f16 SH |
174 | int err_idx = -errno; |
175 | ||
176 | if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap)) | |
177 | return scmi_linux_errmap[err_idx]; | |
aa4f886f SH |
178 | return -EIO; |
179 | } | |
180 | ||
a02d7c93 CM |
181 | void scmi_notification_instance_data_set(const struct scmi_handle *handle, |
182 | void *priv) | |
183 | { | |
184 | struct scmi_info *info = handle_to_scmi_info(handle); | |
185 | ||
186 | info->notify_priv = priv; | |
187 | /* Ensure updated protocol private date are visible */ | |
188 | smp_wmb(); | |
189 | } | |
190 | ||
191 | void *scmi_notification_instance_data_get(const struct scmi_handle *handle) | |
192 | { | |
193 | struct scmi_info *info = handle_to_scmi_info(handle); | |
194 | ||
195 | /* Ensure protocols_private_data has been updated */ | |
196 | smp_rmb(); | |
197 | return info->notify_priv; | |
198 | } | |
199 | ||
9ca5a183 CM |
200 | /** |
201 | * scmi_xfer_token_set - Reserve and set new token for the xfer at hand | |
202 | * | |
203 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
204 | * @xfer: The xfer to act upon | |
205 | * | |
206 | * Pick the next unused monotonically increasing token and set it into | |
207 | * xfer->hdr.seq: picking a monotonically increasing value avoids immediate | |
208 | * reuse of freshly completed or timed-out xfers, thus mitigating the risk | |
209 | * of incorrect association of a late and expired xfer with a live in-flight | |
210 | * transaction, both happening to re-use the same token identifier. | |
211 | * | |
212 | * Since platform is NOT required to answer our request in-order we should | |
213 | * account for a few rare but possible scenarios: | |
214 | * | |
215 | * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token | |
216 | * using find_next_zero_bit() starting from candidate next_token bit | |
217 | * | |
218 | * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we | |
219 | * are plenty of free tokens at start, so try a second pass using | |
220 | * find_next_zero_bit() and starting from 0. | |
221 | * | |
222 | * X = used in-flight | |
223 | * | |
224 | * Normal | |
225 | * ------ | |
226 | * | |
227 | * |- xfer_id picked | |
228 | * -----------+---------------------------------------------------------- | |
229 | * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X| | |
230 | * ---------------------------------------------------------------------- | |
231 | * ^ | |
232 | * |- next_token | |
233 | * | |
234 | * Out-of-order pending at start | |
235 | * ----------------------------- | |
236 | * | |
237 | * |- xfer_id picked, last_token fixed | |
238 | * -----+---------------------------------------------------------------- | |
239 | * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| | | |
240 | * ---------------------------------------------------------------------- | |
241 | * ^ | |
242 | * |- next_token | |
243 | * | |
244 | * | |
245 | * Out-of-order pending at end | |
246 | * --------------------------- | |
247 | * | |
248 | * |- xfer_id picked, last_token fixed | |
249 | * -----+---------------------------------------------------------------- | |
250 | * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X| | |
251 | * ---------------------------------------------------------------------- | |
252 | * ^ | |
253 | * |- next_token | |
254 | * | |
255 | * Context: Assumes to be called with @xfer_lock already acquired. | |
256 | * | |
257 | * Return: 0 on Success or error | |
258 | */ | |
259 | static int scmi_xfer_token_set(struct scmi_xfers_info *minfo, | |
260 | struct scmi_xfer *xfer) | |
261 | { | |
262 | unsigned long xfer_id, next_token; | |
263 | ||
264 | /* | |
265 | * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1] | |
266 | * using the pre-allocated transfer_id as a base. | |
267 | * Note that the global transfer_id is shared across all message types | |
268 | * so there could be holes in the allocated set of monotonic sequence | |
269 | * numbers, but that is going to limit the effectiveness of the | |
270 | * mitigation only in very rare limit conditions. | |
271 | */ | |
272 | next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1)); | |
273 | ||
274 | /* Pick the next available xfer_id >= next_token */ | |
275 | xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, | |
276 | MSG_TOKEN_MAX, next_token); | |
277 | if (xfer_id == MSG_TOKEN_MAX) { | |
278 | /* | |
279 | * After heavily out-of-order responses, there are no free | |
280 | * tokens ahead, but only at start of xfer_alloc_table so | |
281 | * try again from the beginning. | |
282 | */ | |
283 | xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, | |
284 | MSG_TOKEN_MAX, 0); | |
285 | /* | |
286 | * Something is wrong if we got here since there can be a | |
287 | * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages | |
288 | * but we have not found any free token [0, MSG_TOKEN_MAX - 1]. | |
289 | */ | |
290 | if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX)) | |
291 | return -ENOMEM; | |
292 | } | |
293 | ||
294 | /* Update +/- last_token accordingly if we skipped some hole */ | |
295 | if (xfer_id != next_token) | |
296 | atomic_add((int)(xfer_id - next_token), &transfer_last_id); | |
297 | ||
298 | /* Set in-flight */ | |
299 | set_bit(xfer_id, minfo->xfer_alloc_table); | |
300 | xfer->hdr.seq = (u16)xfer_id; | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | /** | |
306 | * scmi_xfer_token_clear - Release the token | |
307 | * | |
308 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
309 | * @xfer: The xfer to act upon | |
310 | */ | |
311 | static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo, | |
312 | struct scmi_xfer *xfer) | |
313 | { | |
314 | clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table); | |
315 | } | |
316 | ||
aa4f886f | 317 | /** |
1baf47c2 | 318 | * scmi_xfer_get() - Allocate one message |
aa4f886f | 319 | * |
1baf47c2 | 320 | * @handle: Pointer to SCMI entity handle |
38c927fb | 321 | * @minfo: Pointer to Tx/Rx Message management info based on channel type |
9ca5a183 CM |
322 | * @set_pending: If true a monotonic token is picked and the xfer is added to |
323 | * the pending hash table. | |
aa4f886f | 324 | * |
5b65af8f | 325 | * Helper function which is used by various message functions that are |
aa4f886f SH |
326 | * exposed to clients of this driver for allocating a message traffic event. |
327 | * | |
9ca5a183 CM |
328 | * Picks an xfer from the free list @free_xfers (if any available) and, if |
329 | * required, sets a monotonically increasing token and stores the inflight xfer | |
330 | * into the @pending_xfers hashtable for later retrieval. | |
331 | * | |
332 | * The successfully initialized xfer is refcounted. | |
333 | * | |
334 | * Context: Holds @xfer_lock while manipulating @xfer_alloc_table and | |
335 | * @free_xfers. | |
aa4f886f SH |
336 | * |
337 | * Return: 0 if all went fine, else corresponding error. | |
338 | */ | |
38c927fb | 339 | static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle, |
9ca5a183 CM |
340 | struct scmi_xfers_info *minfo, |
341 | bool set_pending) | |
aa4f886f | 342 | { |
9ca5a183 CM |
343 | int ret; |
344 | unsigned long flags; | |
aa4f886f | 345 | struct scmi_xfer *xfer; |
aa4f886f | 346 | |
aa4f886f | 347 | spin_lock_irqsave(&minfo->xfer_lock, flags); |
9ca5a183 | 348 | if (hlist_empty(&minfo->free_xfers)) { |
aa4f886f SH |
349 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); |
350 | return ERR_PTR(-ENOMEM); | |
351 | } | |
aa4f886f | 352 | |
9ca5a183 CM |
353 | /* grab an xfer from the free_list */ |
354 | xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node); | |
355 | hlist_del_init(&xfer->node); | |
aa4f886f | 356 | |
9ca5a183 CM |
357 | /* |
358 | * Allocate transfer_id early so that can be used also as base for | |
359 | * monotonic sequence number generation if needed. | |
360 | */ | |
729d3530 | 361 | xfer->transfer_id = atomic_inc_return(&transfer_last_id); |
aa4f886f | 362 | |
9ca5a183 CM |
363 | if (set_pending) { |
364 | /* Pick and set monotonic token */ | |
365 | ret = scmi_xfer_token_set(minfo, xfer); | |
366 | if (!ret) { | |
367 | hash_add(minfo->pending_xfers, &xfer->node, | |
368 | xfer->hdr.seq); | |
369 | xfer->pending = true; | |
370 | } else { | |
371 | dev_err(handle->dev, | |
372 | "Failed to get monotonic token %d\n", ret); | |
373 | hlist_add_head(&xfer->node, &minfo->free_xfers); | |
374 | xfer = ERR_PTR(ret); | |
375 | } | |
376 | } | |
ed7c04c1 CM |
377 | |
378 | if (!IS_ERR(xfer)) { | |
379 | refcount_set(&xfer->users, 1); | |
380 | atomic_set(&xfer->busy, SCMI_XFER_FREE); | |
381 | } | |
9ca5a183 CM |
382 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); |
383 | ||
aa4f886f SH |
384 | return xfer; |
385 | } | |
386 | ||
387 | /** | |
38c927fb | 388 | * __scmi_xfer_put() - Release a message |
aa4f886f | 389 | * |
38c927fb | 390 | * @minfo: Pointer to Tx/Rx Message management info based on channel type |
1baf47c2 | 391 | * @xfer: message that was reserved by scmi_xfer_get |
aa4f886f | 392 | * |
9ca5a183 CM |
393 | * After refcount check, possibly release an xfer, clearing the token slot, |
394 | * removing xfer from @pending_xfers and putting it back into free_xfers. | |
395 | * | |
aa4f886f SH |
396 | * This holds a spinlock to maintain integrity of internal data structures. |
397 | */ | |
38c927fb SH |
398 | static void |
399 | __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer) | |
aa4f886f SH |
400 | { |
401 | unsigned long flags; | |
aa4f886f | 402 | |
aa4f886f | 403 | spin_lock_irqsave(&minfo->xfer_lock, flags); |
ed7c04c1 CM |
404 | if (refcount_dec_and_test(&xfer->users)) { |
405 | if (xfer->pending) { | |
406 | scmi_xfer_token_clear(minfo, xfer); | |
407 | hash_del(&xfer->node); | |
408 | xfer->pending = false; | |
409 | } | |
410 | hlist_add_head(&xfer->node, &minfo->free_xfers); | |
9ca5a183 | 411 | } |
aa4f886f SH |
412 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); |
413 | } | |
414 | ||
9ca5a183 CM |
415 | /** |
416 | * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id | |
417 | * | |
418 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
419 | * @xfer_id: Token ID to lookup in @pending_xfers | |
420 | * | |
421 | * Refcounting is untouched. | |
422 | * | |
423 | * Context: Assumes to be called with @xfer_lock already acquired. | |
424 | * | |
425 | * Return: A valid xfer on Success or error otherwise | |
426 | */ | |
427 | static struct scmi_xfer * | |
428 | scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id) | |
429 | { | |
430 | struct scmi_xfer *xfer = NULL; | |
431 | ||
432 | if (test_bit(xfer_id, minfo->xfer_alloc_table)) | |
433 | xfer = XFER_FIND(minfo->pending_xfers, xfer_id); | |
434 | ||
435 | return xfer ?: ERR_PTR(-EINVAL); | |
436 | } | |
437 | ||
ed7c04c1 CM |
438 | /** |
439 | * scmi_msg_response_validate - Validate message type against state of related | |
440 | * xfer | |
441 | * | |
442 | * @cinfo: A reference to the channel descriptor. | |
443 | * @msg_type: Message type to check | |
444 | * @xfer: A reference to the xfer to validate against @msg_type | |
445 | * | |
446 | * This function checks if @msg_type is congruent with the current state of | |
447 | * a pending @xfer; if an asynchronous delayed response is received before the | |
448 | * related synchronous response (Out-of-Order Delayed Response) the missing | |
449 | * synchronous response is assumed to be OK and completed, carrying on with the | |
450 | * Delayed Response: this is done to address the case in which the underlying | |
451 | * SCMI transport can deliver such out-of-order responses. | |
452 | * | |
453 | * Context: Assumes to be called with xfer->lock already acquired. | |
454 | * | |
455 | * Return: 0 on Success, error otherwise | |
456 | */ | |
457 | static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo, | |
458 | u8 msg_type, | |
459 | struct scmi_xfer *xfer) | |
460 | { | |
461 | /* | |
462 | * Even if a response was indeed expected on this slot at this point, | |
463 | * a buggy platform could wrongly reply feeding us an unexpected | |
464 | * delayed response we're not prepared to handle: bail-out safely | |
465 | * blaming firmware. | |
466 | */ | |
467 | if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) { | |
468 | dev_err(cinfo->dev, | |
469 | "Delayed Response for %d not expected! Buggy F/W ?\n", | |
470 | xfer->hdr.seq); | |
471 | return -EINVAL; | |
472 | } | |
473 | ||
474 | switch (xfer->state) { | |
475 | case SCMI_XFER_SENT_OK: | |
476 | if (msg_type == MSG_TYPE_DELAYED_RESP) { | |
477 | /* | |
478 | * Delayed Response expected but delivered earlier. | |
479 | * Assume message RESPONSE was OK and skip state. | |
480 | */ | |
481 | xfer->hdr.status = SCMI_SUCCESS; | |
482 | xfer->state = SCMI_XFER_RESP_OK; | |
483 | complete(&xfer->done); | |
484 | dev_warn(cinfo->dev, | |
485 | "Received valid OoO Delayed Response for %d\n", | |
486 | xfer->hdr.seq); | |
487 | } | |
488 | break; | |
489 | case SCMI_XFER_RESP_OK: | |
490 | if (msg_type != MSG_TYPE_DELAYED_RESP) | |
491 | return -EINVAL; | |
492 | break; | |
493 | case SCMI_XFER_DRESP_OK: | |
494 | /* No further message expected once in SCMI_XFER_DRESP_OK */ | |
495 | return -EINVAL; | |
496 | } | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
501 | /** | |
502 | * scmi_xfer_state_update - Update xfer state | |
503 | * | |
504 | * @xfer: A reference to the xfer to update | |
505 | * @msg_type: Type of message being processed. | |
506 | * | |
507 | * Note that this message is assumed to have been already successfully validated | |
508 | * by @scmi_msg_response_validate(), so here we just update the state. | |
509 | * | |
510 | * Context: Assumes to be called on an xfer exclusively acquired using the | |
511 | * busy flag. | |
512 | */ | |
513 | static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type) | |
514 | { | |
515 | xfer->hdr.type = msg_type; | |
516 | ||
517 | /* Unknown command types were already discarded earlier */ | |
518 | if (xfer->hdr.type == MSG_TYPE_COMMAND) | |
519 | xfer->state = SCMI_XFER_RESP_OK; | |
520 | else | |
521 | xfer->state = SCMI_XFER_DRESP_OK; | |
522 | } | |
523 | ||
524 | static bool scmi_xfer_acquired(struct scmi_xfer *xfer) | |
525 | { | |
526 | int ret; | |
527 | ||
528 | ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY); | |
529 | ||
530 | return ret == SCMI_XFER_FREE; | |
531 | } | |
532 | ||
533 | /** | |
534 | * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer | |
535 | * | |
536 | * @cinfo: A reference to the channel descriptor. | |
537 | * @msg_hdr: A message header to use as lookup key | |
538 | * | |
539 | * When a valid xfer is found for the sequence number embedded in the provided | |
540 | * msg_hdr, reference counting is properly updated and exclusive access to this | |
541 | * xfer is granted till released with @scmi_xfer_command_release. | |
542 | * | |
543 | * Return: A valid @xfer on Success or error otherwise. | |
544 | */ | |
545 | static inline struct scmi_xfer * | |
546 | scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr) | |
547 | { | |
548 | int ret; | |
549 | unsigned long flags; | |
550 | struct scmi_xfer *xfer; | |
551 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
552 | struct scmi_xfers_info *minfo = &info->tx_minfo; | |
553 | u8 msg_type = MSG_XTRACT_TYPE(msg_hdr); | |
554 | u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr); | |
555 | ||
556 | /* Are we even expecting this? */ | |
557 | spin_lock_irqsave(&minfo->xfer_lock, flags); | |
558 | xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id); | |
559 | if (IS_ERR(xfer)) { | |
560 | dev_err(cinfo->dev, | |
561 | "Message for %d type %d is not expected!\n", | |
562 | xfer_id, msg_type); | |
563 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
564 | return xfer; | |
565 | } | |
566 | refcount_inc(&xfer->users); | |
567 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
568 | ||
569 | spin_lock_irqsave(&xfer->lock, flags); | |
570 | ret = scmi_msg_response_validate(cinfo, msg_type, xfer); | |
571 | /* | |
572 | * If a pending xfer was found which was also in a congruent state with | |
573 | * the received message, acquire exclusive access to it setting the busy | |
574 | * flag. | |
575 | * Spins only on the rare limit condition of concurrent reception of | |
576 | * RESP and DRESP for the same xfer. | |
577 | */ | |
578 | if (!ret) { | |
579 | spin_until_cond(scmi_xfer_acquired(xfer)); | |
580 | scmi_xfer_state_update(xfer, msg_type); | |
581 | } | |
582 | spin_unlock_irqrestore(&xfer->lock, flags); | |
583 | ||
584 | if (ret) { | |
585 | dev_err(cinfo->dev, | |
586 | "Invalid message type:%d for %d - HDR:0x%X state:%d\n", | |
587 | msg_type, xfer_id, msg_hdr, xfer->state); | |
588 | /* On error the refcount incremented above has to be dropped */ | |
589 | __scmi_xfer_put(minfo, xfer); | |
590 | xfer = ERR_PTR(-EINVAL); | |
591 | } | |
592 | ||
593 | return xfer; | |
594 | } | |
595 | ||
596 | static inline void scmi_xfer_command_release(struct scmi_info *info, | |
597 | struct scmi_xfer *xfer) | |
598 | { | |
599 | atomic_set(&xfer->busy, SCMI_XFER_FREE); | |
600 | __scmi_xfer_put(&info->tx_minfo, xfer); | |
601 | } | |
602 | ||
e9b21c96 CM |
603 | static inline void scmi_clear_channel(struct scmi_info *info, |
604 | struct scmi_chan_info *cinfo) | |
605 | { | |
606 | if (info->desc->ops->clear_channel) | |
607 | info->desc->ops->clear_channel(cinfo); | |
608 | } | |
609 | ||
4d09852b | 610 | static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr) |
2747a967 | 611 | { |
5c8a47a5 | 612 | struct scmi_xfer *xfer; |
4d09852b SH |
613 | struct device *dev = cinfo->dev; |
614 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
615 | struct scmi_xfers_info *minfo = &info->rx_minfo; | |
72a5eb9d | 616 | ktime_t ts; |
4d09852b | 617 | |
72a5eb9d | 618 | ts = ktime_get_boottime(); |
9ca5a183 | 619 | xfer = scmi_xfer_get(cinfo->handle, minfo, false); |
4d09852b SH |
620 | if (IS_ERR(xfer)) { |
621 | dev_err(dev, "failed to get free message slot (%ld)\n", | |
622 | PTR_ERR(xfer)); | |
e9b21c96 | 623 | scmi_clear_channel(info, cinfo); |
4d09852b SH |
624 | return; |
625 | } | |
626 | ||
627 | unpack_scmi_header(msg_hdr, &xfer->hdr); | |
4d09852b SH |
628 | info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size, |
629 | xfer); | |
6b8a6913 CM |
630 | scmi_notify(cinfo->handle, xfer->hdr.protocol_id, |
631 | xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts); | |
4d09852b SH |
632 | |
633 | trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, | |
634 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
635 | MSG_TYPE_NOTIFICATION); | |
58ecdf03 | 636 | |
4d09852b SH |
637 | __scmi_xfer_put(minfo, xfer); |
638 | ||
e9b21c96 | 639 | scmi_clear_channel(info, cinfo); |
4d09852b SH |
640 | } |
641 | ||
ed7c04c1 | 642 | static void scmi_handle_response(struct scmi_chan_info *cinfo, u32 msg_hdr) |
4d09852b SH |
643 | { |
644 | struct scmi_xfer *xfer; | |
4d09852b | 645 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); |
2747a967 | 646 | |
ed7c04c1 | 647 | xfer = scmi_xfer_command_acquire(cinfo, msg_hdr); |
9ca5a183 | 648 | if (IS_ERR(xfer)) { |
e9b21c96 | 649 | scmi_clear_channel(info, cinfo); |
c5bceb98 CM |
650 | return; |
651 | } | |
2747a967 | 652 | |
0cb7af47 | 653 | /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */ |
ed7c04c1 | 654 | if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) |
0cb7af47 CM |
655 | xfer->rx.len = info->desc->max_msg_size; |
656 | ||
5c8a47a5 | 657 | info->desc->ops->fetch_response(cinfo, xfer); |
58ecdf03 | 658 | |
729d3530 LL |
659 | trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, |
660 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
ed7c04c1 | 661 | xfer->hdr.type); |
729d3530 | 662 | |
ed7c04c1 | 663 | if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) { |
e9b21c96 | 664 | scmi_clear_channel(info, cinfo); |
58ecdf03 | 665 | complete(xfer->async_done); |
d04fb2b2 | 666 | } else { |
58ecdf03 | 667 | complete(&xfer->done); |
d04fb2b2 | 668 | } |
ed7c04c1 CM |
669 | |
670 | scmi_xfer_command_release(info, xfer); | |
2747a967 SH |
671 | } |
672 | ||
4d09852b SH |
673 | /** |
674 | * scmi_rx_callback() - callback for receiving messages | |
675 | * | |
676 | * @cinfo: SCMI channel info | |
677 | * @msg_hdr: Message header | |
678 | * | |
679 | * Processes one received message to appropriate transfer information and | |
680 | * signals completion of the transfer. | |
681 | * | |
682 | * NOTE: This function will be invoked in IRQ context, hence should be | |
683 | * as optimal as possible. | |
684 | */ | |
685 | void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr) | |
686 | { | |
4d09852b SH |
687 | u8 msg_type = MSG_XTRACT_TYPE(msg_hdr); |
688 | ||
689 | switch (msg_type) { | |
690 | case MSG_TYPE_NOTIFICATION: | |
691 | scmi_handle_notification(cinfo, msg_hdr); | |
692 | break; | |
693 | case MSG_TYPE_COMMAND: | |
694 | case MSG_TYPE_DELAYED_RESP: | |
ed7c04c1 | 695 | scmi_handle_response(cinfo, msg_hdr); |
4d09852b SH |
696 | break; |
697 | default: | |
698 | WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type); | |
699 | break; | |
700 | } | |
701 | } | |
702 | ||
38c927fb | 703 | /** |
a4a20b09 | 704 | * xfer_put() - Release a transmit message |
38c927fb | 705 | * |
a4a20b09 | 706 | * @ph: Pointer to SCMI protocol handle |
ed7c04c1 | 707 | * @xfer: message that was reserved by xfer_get_init |
38c927fb | 708 | */ |
a4a20b09 CM |
709 | static void xfer_put(const struct scmi_protocol_handle *ph, |
710 | struct scmi_xfer *xfer) | |
38c927fb | 711 | { |
a4a20b09 CM |
712 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
713 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
38c927fb SH |
714 | |
715 | __scmi_xfer_put(&info->tx_minfo, xfer); | |
716 | } | |
717 | ||
d4c3751a SH |
718 | #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC) |
719 | ||
5c8a47a5 | 720 | static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo, |
d4c3751a SH |
721 | struct scmi_xfer *xfer, ktime_t stop) |
722 | { | |
5c8a47a5 | 723 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); |
d4c3751a | 724 | |
ed7c04c1 CM |
725 | /* |
726 | * Poll also on xfer->done so that polling can be forcibly terminated | |
727 | * in case of out-of-order receptions of delayed responses | |
728 | */ | |
5c8a47a5 | 729 | return info->desc->ops->poll_done(cinfo, xfer) || |
ed7c04c1 | 730 | try_wait_for_completion(&xfer->done) || |
5c8a47a5 | 731 | ktime_after(ktime_get(), stop); |
d4c3751a SH |
732 | } |
733 | ||
aa4f886f | 734 | /** |
a4a20b09 | 735 | * do_xfer() - Do one transfer |
aa4f886f | 736 | * |
a4a20b09 | 737 | * @ph: Pointer to SCMI protocol handle |
aa4f886f SH |
738 | * @xfer: Transfer to initiate and wait for response |
739 | * | |
740 | * Return: -ETIMEDOUT in case of no response, if transmit error, | |
1baf47c2 SH |
741 | * return corresponding error, else if all goes well, |
742 | * return 0. | |
aa4f886f | 743 | */ |
a4a20b09 CM |
744 | static int do_xfer(const struct scmi_protocol_handle *ph, |
745 | struct scmi_xfer *xfer) | |
aa4f886f SH |
746 | { |
747 | int ret; | |
748 | int timeout; | |
a4a20b09 CM |
749 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
750 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
aa4f886f | 751 | struct device *dev = info->dev; |
907b6d14 SH |
752 | struct scmi_chan_info *cinfo; |
753 | ||
a4a20b09 | 754 | /* |
61832b35 | 755 | * Initialise protocol id now from protocol handle to avoid it being |
a4a20b09 | 756 | * overridden by mistake (or malice) by the protocol code mangling with |
61832b35 | 757 | * the scmi_xfer structure prior to this. |
a4a20b09 CM |
758 | */ |
759 | xfer->hdr.protocol_id = pi->proto->id; | |
e30d91d4 | 760 | reinit_completion(&xfer->done); |
a4a20b09 | 761 | |
907b6d14 SH |
762 | cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id); |
763 | if (unlikely(!cinfo)) | |
764 | return -EINVAL; | |
aa4f886f | 765 | |
729d3530 LL |
766 | trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id, |
767 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
768 | xfer->hdr.poll_completion); | |
769 | ||
ed7c04c1 CM |
770 | xfer->state = SCMI_XFER_SENT_OK; |
771 | /* | |
772 | * Even though spinlocking is not needed here since no race is possible | |
773 | * on xfer->state due to the monotonically increasing tokens allocation, | |
774 | * we must anyway ensure xfer->state initialization is not re-ordered | |
775 | * after the .send_message() to be sure that on the RX path an early | |
776 | * ISR calling scmi_rx_callback() cannot see an old stale xfer->state. | |
777 | */ | |
778 | smp_mb(); | |
779 | ||
5c8a47a5 | 780 | ret = info->desc->ops->send_message(cinfo, xfer); |
aa4f886f | 781 | if (ret < 0) { |
5c8a47a5 | 782 | dev_dbg(dev, "Failed to send message %d\n", ret); |
aa4f886f SH |
783 | return ret; |
784 | } | |
785 | ||
d4c3751a SH |
786 | if (xfer->hdr.poll_completion) { |
787 | ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS); | |
788 | ||
fbc4d81a | 789 | spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop)); |
d4c3751a | 790 | |
ed7c04c1 CM |
791 | if (ktime_before(ktime_get(), stop)) { |
792 | unsigned long flags; | |
793 | ||
794 | /* | |
795 | * Do not fetch_response if an out-of-order delayed | |
796 | * response is being processed. | |
797 | */ | |
798 | spin_lock_irqsave(&xfer->lock, flags); | |
799 | if (xfer->state == SCMI_XFER_SENT_OK) { | |
800 | info->desc->ops->fetch_response(cinfo, xfer); | |
801 | xfer->state = SCMI_XFER_RESP_OK; | |
802 | } | |
803 | spin_unlock_irqrestore(&xfer->lock, flags); | |
804 | } else { | |
d4c3751a | 805 | ret = -ETIMEDOUT; |
ed7c04c1 | 806 | } |
d4c3751a SH |
807 | } else { |
808 | /* And we wait for the response. */ | |
809 | timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms); | |
810 | if (!wait_for_completion_timeout(&xfer->done, timeout)) { | |
5c8a47a5 | 811 | dev_err(dev, "timed out in resp(caller: %pS)\n", |
d4c3751a SH |
812 | (void *)_RET_IP_); |
813 | ret = -ETIMEDOUT; | |
814 | } | |
aa4f886f | 815 | } |
d4c3751a SH |
816 | |
817 | if (!ret && xfer->hdr.status) | |
818 | ret = scmi_to_linux_errno(xfer->hdr.status); | |
819 | ||
5c8a47a5 VK |
820 | if (info->desc->ops->mark_txdone) |
821 | info->desc->ops->mark_txdone(cinfo, ret); | |
aa4f886f | 822 | |
729d3530 | 823 | trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id, |
bad0d73b | 824 | xfer->hdr.protocol_id, xfer->hdr.seq, ret); |
729d3530 | 825 | |
aa4f886f SH |
826 | return ret; |
827 | } | |
828 | ||
a4a20b09 CM |
829 | static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph, |
830 | struct scmi_xfer *xfer) | |
831 | { | |
832 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
833 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
834 | ||
835 | xfer->rx.len = info->desc->max_msg_size; | |
836 | } | |
837 | ||
58ecdf03 SH |
838 | #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC) |
839 | ||
840 | /** | |
a4a20b09 | 841 | * do_xfer_with_response() - Do one transfer and wait until the delayed |
58ecdf03 SH |
842 | * response is received |
843 | * | |
a4a20b09 | 844 | * @ph: Pointer to SCMI protocol handle |
58ecdf03 SH |
845 | * @xfer: Transfer to initiate and wait for response |
846 | * | |
847 | * Return: -ETIMEDOUT in case of no delayed response, if transmit error, | |
848 | * return corresponding error, else if all goes well, return 0. | |
849 | */ | |
a4a20b09 CM |
850 | static int do_xfer_with_response(const struct scmi_protocol_handle *ph, |
851 | struct scmi_xfer *xfer) | |
58ecdf03 SH |
852 | { |
853 | int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT); | |
854 | DECLARE_COMPLETION_ONSTACK(async_response); | |
855 | ||
856 | xfer->async_done = &async_response; | |
857 | ||
a4a20b09 | 858 | ret = do_xfer(ph, xfer); |
f1748b1e CM |
859 | if (!ret) { |
860 | if (!wait_for_completion_timeout(xfer->async_done, timeout)) | |
861 | ret = -ETIMEDOUT; | |
862 | else if (xfer->hdr.status) | |
863 | ret = scmi_to_linux_errno(xfer->hdr.status); | |
864 | } | |
58ecdf03 SH |
865 | |
866 | xfer->async_done = NULL; | |
867 | return ret; | |
868 | } | |
869 | ||
aa4f886f | 870 | /** |
a4a20b09 | 871 | * xfer_get_init() - Allocate and initialise one message for transmit |
aa4f886f | 872 | * |
a4a20b09 | 873 | * @ph: Pointer to SCMI protocol handle |
aa4f886f | 874 | * @msg_id: Message identifier |
aa4f886f SH |
875 | * @tx_size: transmit message size |
876 | * @rx_size: receive message size | |
877 | * @p: pointer to the allocated and initialised message | |
878 | * | |
14e297b3 | 879 | * This function allocates the message using @scmi_xfer_get and |
aa4f886f SH |
880 | * initialise the header. |
881 | * | |
882 | * Return: 0 if all went fine with @p pointing to message, else | |
883 | * corresponding error. | |
884 | */ | |
a4a20b09 CM |
885 | static int xfer_get_init(const struct scmi_protocol_handle *ph, |
886 | u8 msg_id, size_t tx_size, size_t rx_size, | |
887 | struct scmi_xfer **p) | |
aa4f886f SH |
888 | { |
889 | int ret; | |
890 | struct scmi_xfer *xfer; | |
a4a20b09 CM |
891 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
892 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
38c927fb | 893 | struct scmi_xfers_info *minfo = &info->tx_minfo; |
aa4f886f SH |
894 | struct device *dev = info->dev; |
895 | ||
896 | /* Ensure we have sane transfer sizes */ | |
897 | if (rx_size > info->desc->max_msg_size || | |
898 | tx_size > info->desc->max_msg_size) | |
899 | return -ERANGE; | |
900 | ||
9ca5a183 | 901 | xfer = scmi_xfer_get(pi->handle, minfo, true); |
aa4f886f SH |
902 | if (IS_ERR(xfer)) { |
903 | ret = PTR_ERR(xfer); | |
904 | dev_err(dev, "failed to get free message slot(%d)\n", ret); | |
905 | return ret; | |
906 | } | |
907 | ||
908 | xfer->tx.len = tx_size; | |
909 | xfer->rx.len = rx_size ? : info->desc->max_msg_size; | |
63b282f1 | 910 | xfer->hdr.type = MSG_TYPE_COMMAND; |
aa4f886f | 911 | xfer->hdr.id = msg_id; |
aa4f886f SH |
912 | xfer->hdr.poll_completion = false; |
913 | ||
914 | *p = xfer; | |
1baf47c2 | 915 | |
aa4f886f SH |
916 | return 0; |
917 | } | |
918 | ||
b6f20ff8 | 919 | /** |
a4a20b09 | 920 | * version_get() - command to get the revision of the SCMI entity |
b6f20ff8 | 921 | * |
a4a20b09 | 922 | * @ph: Pointer to SCMI protocol handle |
1baf47c2 | 923 | * @version: Holds returned version of protocol. |
b6f20ff8 SH |
924 | * |
925 | * Updates the SCMI information in the internal data structure. | |
926 | * | |
927 | * Return: 0 if all went fine, else return appropriate error. | |
928 | */ | |
a4a20b09 | 929 | static int version_get(const struct scmi_protocol_handle *ph, u32 *version) |
b6f20ff8 SH |
930 | { |
931 | int ret; | |
932 | __le32 *rev_info; | |
933 | struct scmi_xfer *t; | |
934 | ||
a4a20b09 | 935 | ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t); |
b6f20ff8 SH |
936 | if (ret) |
937 | return ret; | |
938 | ||
a4a20b09 | 939 | ret = do_xfer(ph, t); |
b6f20ff8 SH |
940 | if (!ret) { |
941 | rev_info = t->rx.buf; | |
942 | *version = le32_to_cpu(*rev_info); | |
943 | } | |
944 | ||
a4a20b09 | 945 | xfer_put(ph, t); |
b6f20ff8 SH |
946 | return ret; |
947 | } | |
948 | ||
d7b6cc56 CM |
949 | /** |
950 | * scmi_set_protocol_priv - Set protocol specific data at init time | |
951 | * | |
952 | * @ph: A reference to the protocol handle. | |
953 | * @priv: The private data to set. | |
954 | * | |
955 | * Return: 0 on Success | |
956 | */ | |
957 | static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph, | |
958 | void *priv) | |
959 | { | |
960 | struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
961 | ||
962 | pi->priv = priv; | |
963 | ||
964 | return 0; | |
965 | } | |
966 | ||
967 | /** | |
968 | * scmi_get_protocol_priv - Set protocol specific data at init time | |
969 | * | |
970 | * @ph: A reference to the protocol handle. | |
971 | * | |
972 | * Return: Protocol private data if any was set. | |
973 | */ | |
974 | static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph) | |
975 | { | |
976 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
977 | ||
978 | return pi->priv; | |
979 | } | |
980 | ||
a4a20b09 CM |
981 | static const struct scmi_xfer_ops xfer_ops = { |
982 | .version_get = version_get, | |
983 | .xfer_get_init = xfer_get_init, | |
984 | .reset_rx_to_maxsz = reset_rx_to_maxsz, | |
985 | .do_xfer = do_xfer, | |
986 | .do_xfer_with_response = do_xfer_with_response, | |
987 | .xfer_put = xfer_put, | |
988 | }; | |
989 | ||
3d5d6e84 CM |
990 | /** |
991 | * scmi_revision_area_get - Retrieve version memory area. | |
992 | * | |
993 | * @ph: A reference to the protocol handle. | |
994 | * | |
995 | * A helper to grab the version memory area reference during SCMI Base protocol | |
996 | * initialization. | |
997 | * | |
998 | * Return: A reference to the version memory area associated to the SCMI | |
999 | * instance underlying this protocol handle. | |
1000 | */ | |
1001 | struct scmi_revision_info * | |
1002 | scmi_revision_area_get(const struct scmi_protocol_handle *ph) | |
1003 | { | |
1004 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
1005 | ||
1006 | return pi->handle->version; | |
1007 | } | |
1008 | ||
48dc16e2 CM |
1009 | /** |
1010 | * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol | |
1011 | * instance descriptor. | |
1012 | * @info: The reference to the related SCMI instance. | |
1013 | * @proto: The protocol descriptor. | |
1014 | * | |
1015 | * Allocate a new protocol instance descriptor, using the provided @proto | |
1016 | * description, against the specified SCMI instance @info, and initialize it; | |
1017 | * all resources management is handled via a dedicated per-protocol devres | |
1018 | * group. | |
1019 | * | |
1020 | * Context: Assumes to be called with @protocols_mtx already acquired. | |
1021 | * Return: A reference to a freshly allocated and initialized protocol instance | |
f5800e0b CM |
1022 | * or ERR_PTR on failure. On failure the @proto reference is at first |
1023 | * put using @scmi_protocol_put() before releasing all the devres group. | |
48dc16e2 CM |
1024 | */ |
1025 | static struct scmi_protocol_instance * | |
1026 | scmi_alloc_init_protocol_instance(struct scmi_info *info, | |
1027 | const struct scmi_protocol *proto) | |
1028 | { | |
1029 | int ret = -ENOMEM; | |
1030 | void *gid; | |
1031 | struct scmi_protocol_instance *pi; | |
f0e73cee | 1032 | const struct scmi_handle *handle = &info->handle; |
48dc16e2 CM |
1033 | |
1034 | /* Protocol specific devres group */ | |
1035 | gid = devres_open_group(handle->dev, NULL, GFP_KERNEL); | |
f5800e0b CM |
1036 | if (!gid) { |
1037 | scmi_protocol_put(proto->id); | |
48dc16e2 | 1038 | goto out; |
f5800e0b | 1039 | } |
48dc16e2 CM |
1040 | |
1041 | pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL); | |
1042 | if (!pi) | |
1043 | goto clean; | |
1044 | ||
1045 | pi->gid = gid; | |
1046 | pi->proto = proto; | |
d7b6cc56 CM |
1047 | pi->handle = handle; |
1048 | pi->ph.dev = handle->dev; | |
a4a20b09 | 1049 | pi->ph.xops = &xfer_ops; |
d7b6cc56 CM |
1050 | pi->ph.set_priv = scmi_set_protocol_priv; |
1051 | pi->ph.get_priv = scmi_get_protocol_priv; | |
48dc16e2 CM |
1052 | refcount_set(&pi->users, 1); |
1053 | /* proto->init is assured NON NULL by scmi_protocol_register */ | |
a4a20b09 | 1054 | ret = pi->proto->instance_init(&pi->ph); |
48dc16e2 CM |
1055 | if (ret) |
1056 | goto clean; | |
1057 | ||
1058 | ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1, | |
1059 | GFP_KERNEL); | |
1060 | if (ret != proto->id) | |
1061 | goto clean; | |
1062 | ||
533c7095 CM |
1063 | /* |
1064 | * Warn but ignore events registration errors since we do not want | |
1065 | * to skip whole protocols if their notifications are messed up. | |
1066 | */ | |
1067 | if (pi->proto->events) { | |
1068 | ret = scmi_register_protocol_events(handle, pi->proto->id, | |
b9f7fd90 | 1069 | &pi->ph, |
533c7095 CM |
1070 | pi->proto->events); |
1071 | if (ret) | |
1072 | dev_warn(handle->dev, | |
1073 | "Protocol:%X - Events Registration Failed - err:%d\n", | |
1074 | pi->proto->id, ret); | |
1075 | } | |
1076 | ||
48dc16e2 CM |
1077 | devres_close_group(handle->dev, pi->gid); |
1078 | dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id); | |
1079 | ||
1080 | return pi; | |
1081 | ||
1082 | clean: | |
f5800e0b CM |
1083 | /* Take care to put the protocol module's owner before releasing all */ |
1084 | scmi_protocol_put(proto->id); | |
48dc16e2 CM |
1085 | devres_release_group(handle->dev, gid); |
1086 | out: | |
1087 | return ERR_PTR(ret); | |
1088 | } | |
1089 | ||
1090 | /** | |
1091 | * scmi_get_protocol_instance - Protocol initialization helper. | |
1092 | * @handle: A reference to the SCMI platform instance. | |
1093 | * @protocol_id: The protocol being requested. | |
1094 | * | |
1095 | * In case the required protocol has never been requested before for this | |
1096 | * instance, allocate and initialize all the needed structures while handling | |
1097 | * resource allocation with a dedicated per-protocol devres subgroup. | |
1098 | * | |
f5800e0b CM |
1099 | * Return: A reference to an initialized protocol instance or error on failure: |
1100 | * in particular returns -EPROBE_DEFER when the desired protocol could | |
1101 | * NOT be found. | |
48dc16e2 CM |
1102 | */ |
1103 | static struct scmi_protocol_instance * __must_check | |
f0e73cee | 1104 | scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id) |
48dc16e2 CM |
1105 | { |
1106 | struct scmi_protocol_instance *pi; | |
1107 | struct scmi_info *info = handle_to_scmi_info(handle); | |
1108 | ||
1109 | mutex_lock(&info->protocols_mtx); | |
1110 | pi = idr_find(&info->protocols, protocol_id); | |
1111 | ||
1112 | if (pi) { | |
1113 | refcount_inc(&pi->users); | |
1114 | } else { | |
1115 | const struct scmi_protocol *proto; | |
1116 | ||
1117 | /* Fails if protocol not registered on bus */ | |
1118 | proto = scmi_protocol_get(protocol_id); | |
1119 | if (proto) | |
1120 | pi = scmi_alloc_init_protocol_instance(info, proto); | |
1121 | else | |
f5800e0b | 1122 | pi = ERR_PTR(-EPROBE_DEFER); |
48dc16e2 CM |
1123 | } |
1124 | mutex_unlock(&info->protocols_mtx); | |
1125 | ||
1126 | return pi; | |
1127 | } | |
1128 | ||
1129 | /** | |
1130 | * scmi_protocol_acquire - Protocol acquire | |
1131 | * @handle: A reference to the SCMI platform instance. | |
1132 | * @protocol_id: The protocol being requested. | |
1133 | * | |
1134 | * Register a new user for the requested protocol on the specified SCMI | |
1135 | * platform instance, possibly triggering its initialization on first user. | |
1136 | * | |
1137 | * Return: 0 if protocol was acquired successfully. | |
1138 | */ | |
f0e73cee | 1139 | int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id) |
48dc16e2 CM |
1140 | { |
1141 | return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id)); | |
1142 | } | |
1143 | ||
1144 | /** | |
1145 | * scmi_protocol_release - Protocol de-initialization helper. | |
1146 | * @handle: A reference to the SCMI platform instance. | |
1147 | * @protocol_id: The protocol being requested. | |
1148 | * | |
1149 | * Remove one user for the specified protocol and triggers de-initialization | |
1150 | * and resources de-allocation once the last user has gone. | |
1151 | */ | |
f0e73cee | 1152 | void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id) |
48dc16e2 CM |
1153 | { |
1154 | struct scmi_info *info = handle_to_scmi_info(handle); | |
1155 | struct scmi_protocol_instance *pi; | |
1156 | ||
1157 | mutex_lock(&info->protocols_mtx); | |
1158 | pi = idr_find(&info->protocols, protocol_id); | |
1159 | if (WARN_ON(!pi)) | |
1160 | goto out; | |
1161 | ||
1162 | if (refcount_dec_and_test(&pi->users)) { | |
1163 | void *gid = pi->gid; | |
1164 | ||
533c7095 CM |
1165 | if (pi->proto->events) |
1166 | scmi_deregister_protocol_events(handle, protocol_id); | |
1167 | ||
48dc16e2 | 1168 | if (pi->proto->instance_deinit) |
a4a20b09 | 1169 | pi->proto->instance_deinit(&pi->ph); |
48dc16e2 CM |
1170 | |
1171 | idr_remove(&info->protocols, protocol_id); | |
1172 | ||
f5800e0b CM |
1173 | scmi_protocol_put(protocol_id); |
1174 | ||
48dc16e2 CM |
1175 | devres_release_group(handle->dev, gid); |
1176 | dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", | |
1177 | protocol_id); | |
1178 | } | |
1179 | ||
1180 | out: | |
1181 | mutex_unlock(&info->protocols_mtx); | |
1182 | } | |
1183 | ||
8d3581c2 | 1184 | void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph, |
b6f20ff8 SH |
1185 | u8 *prot_imp) |
1186 | { | |
8d3581c2 CM |
1187 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
1188 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
b6f20ff8 SH |
1189 | |
1190 | info->protocols_imp = prot_imp; | |
1191 | } | |
1192 | ||
bc40081d SH |
1193 | static bool |
1194 | scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id) | |
1195 | { | |
1196 | int i; | |
1197 | struct scmi_info *info = handle_to_scmi_info(handle); | |
1198 | ||
1199 | if (!info->protocols_imp) | |
1200 | return false; | |
1201 | ||
1202 | for (i = 0; i < MAX_PROTOCOLS_IMP; i++) | |
1203 | if (info->protocols_imp[i] == prot_id) | |
1204 | return true; | |
1205 | return false; | |
1206 | } | |
1207 | ||
23934efe | 1208 | struct scmi_protocol_devres { |
f0e73cee | 1209 | const struct scmi_handle *handle; |
23934efe CM |
1210 | u8 protocol_id; |
1211 | }; | |
1212 | ||
1213 | static void scmi_devm_release_protocol(struct device *dev, void *res) | |
1214 | { | |
1215 | struct scmi_protocol_devres *dres = res; | |
1216 | ||
1217 | scmi_protocol_release(dres->handle, dres->protocol_id); | |
1218 | } | |
1219 | ||
1220 | /** | |
1221 | * scmi_devm_protocol_get - Devres managed get protocol operations and handle | |
1222 | * @sdev: A reference to an scmi_device whose embedded struct device is to | |
1223 | * be used for devres accounting. | |
1224 | * @protocol_id: The protocol being requested. | |
1225 | * @ph: A pointer reference used to pass back the associated protocol handle. | |
1226 | * | |
1227 | * Get hold of a protocol accounting for its usage, eventually triggering its | |
1228 | * initialization, and returning the protocol specific operations and related | |
1229 | * protocol handle which will be used as first argument in most of the | |
1230 | * protocols operations methods. | |
1231 | * Being a devres based managed method, protocol hold will be automatically | |
1232 | * released, and possibly de-initialized on last user, once the SCMI driver | |
1233 | * owning the scmi_device is unbound from it. | |
1234 | * | |
1235 | * Return: A reference to the requested protocol operations or error. | |
1236 | * Must be checked for errors by caller. | |
1237 | */ | |
1238 | static const void __must_check * | |
1239 | scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id, | |
1240 | struct scmi_protocol_handle **ph) | |
1241 | { | |
1242 | struct scmi_protocol_instance *pi; | |
1243 | struct scmi_protocol_devres *dres; | |
1244 | struct scmi_handle *handle = sdev->handle; | |
1245 | ||
1246 | if (!ph) | |
1247 | return ERR_PTR(-EINVAL); | |
1248 | ||
1249 | dres = devres_alloc(scmi_devm_release_protocol, | |
1250 | sizeof(*dres), GFP_KERNEL); | |
1251 | if (!dres) | |
1252 | return ERR_PTR(-ENOMEM); | |
1253 | ||
1254 | pi = scmi_get_protocol_instance(handle, protocol_id); | |
1255 | if (IS_ERR(pi)) { | |
1256 | devres_free(dres); | |
1257 | return pi; | |
1258 | } | |
1259 | ||
1260 | dres->handle = handle; | |
1261 | dres->protocol_id = protocol_id; | |
1262 | devres_add(&sdev->dev, dres); | |
1263 | ||
1264 | *ph = &pi->ph; | |
1265 | ||
1266 | return pi->proto->ops; | |
1267 | } | |
1268 | ||
1269 | static int scmi_devm_protocol_match(struct device *dev, void *res, void *data) | |
1270 | { | |
1271 | struct scmi_protocol_devres *dres = res; | |
1272 | ||
1273 | if (WARN_ON(!dres || !data)) | |
1274 | return 0; | |
1275 | ||
1276 | return dres->protocol_id == *((u8 *)data); | |
1277 | } | |
1278 | ||
1279 | /** | |
1280 | * scmi_devm_protocol_put - Devres managed put protocol operations and handle | |
1281 | * @sdev: A reference to an scmi_device whose embedded struct device is to | |
1282 | * be used for devres accounting. | |
1283 | * @protocol_id: The protocol being requested. | |
1284 | * | |
1285 | * Explicitly release a protocol hold previously obtained calling the above | |
1286 | * @scmi_devm_protocol_get. | |
1287 | */ | |
1288 | static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id) | |
1289 | { | |
1290 | int ret; | |
1291 | ||
1292 | ret = devres_release(&sdev->dev, scmi_devm_release_protocol, | |
1293 | scmi_devm_protocol_match, &protocol_id); | |
1294 | WARN_ON(ret); | |
1295 | } | |
1296 | ||
d4f9dddd CM |
1297 | static inline |
1298 | struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info) | |
1299 | { | |
1300 | info->users++; | |
1301 | return &info->handle; | |
1302 | } | |
1303 | ||
aa4f886f | 1304 | /** |
14e297b3 | 1305 | * scmi_handle_get() - Get the SCMI handle for a device |
aa4f886f SH |
1306 | * |
1307 | * @dev: pointer to device for which we want SCMI handle | |
1308 | * | |
1309 | * NOTE: The function does not track individual clients of the framework | |
1baf47c2 | 1310 | * and is expected to be maintained by caller of SCMI protocol library. |
aa4f886f SH |
1311 | * scmi_handle_put must be balanced with successful scmi_handle_get |
1312 | * | |
1313 | * Return: pointer to handle if successful, NULL on error | |
1314 | */ | |
1315 | struct scmi_handle *scmi_handle_get(struct device *dev) | |
1316 | { | |
1317 | struct list_head *p; | |
1318 | struct scmi_info *info; | |
1319 | struct scmi_handle *handle = NULL; | |
1320 | ||
1321 | mutex_lock(&scmi_list_mutex); | |
1322 | list_for_each(p, &scmi_list) { | |
1323 | info = list_entry(p, struct scmi_info, node); | |
1324 | if (dev->parent == info->dev) { | |
d4f9dddd | 1325 | handle = scmi_handle_get_from_info_unlocked(info); |
aa4f886f SH |
1326 | break; |
1327 | } | |
1328 | } | |
1329 | mutex_unlock(&scmi_list_mutex); | |
1330 | ||
1331 | return handle; | |
1332 | } | |
1333 | ||
1334 | /** | |
1335 | * scmi_handle_put() - Release the handle acquired by scmi_handle_get | |
1336 | * | |
1337 | * @handle: handle acquired by scmi_handle_get | |
1338 | * | |
1339 | * NOTE: The function does not track individual clients of the framework | |
1baf47c2 | 1340 | * and is expected to be maintained by caller of SCMI protocol library. |
aa4f886f SH |
1341 | * scmi_handle_put must be balanced with successful scmi_handle_get |
1342 | * | |
1343 | * Return: 0 is successfully released | |
1344 | * if null was passed, it returns -EINVAL; | |
1345 | */ | |
1346 | int scmi_handle_put(const struct scmi_handle *handle) | |
1347 | { | |
1348 | struct scmi_info *info; | |
1349 | ||
1350 | if (!handle) | |
1351 | return -EINVAL; | |
1352 | ||
1353 | info = handle_to_scmi_info(handle); | |
1354 | mutex_lock(&scmi_list_mutex); | |
1355 | if (!WARN_ON(!info->users)) | |
1356 | info->users--; | |
1357 | mutex_unlock(&scmi_list_mutex); | |
1358 | ||
1359 | return 0; | |
1360 | } | |
1361 | ||
4ebd8f6d SH |
1362 | static int __scmi_xfer_info_init(struct scmi_info *sinfo, |
1363 | struct scmi_xfers_info *info) | |
aa4f886f SH |
1364 | { |
1365 | int i; | |
1366 | struct scmi_xfer *xfer; | |
1367 | struct device *dev = sinfo->dev; | |
1368 | const struct scmi_desc *desc = sinfo->desc; | |
aa4f886f SH |
1369 | |
1370 | /* Pre-allocated messages, no more than what hdr.seq can support */ | |
bdb8742d CM |
1371 | if (WARN_ON(!desc->max_msg || desc->max_msg > MSG_TOKEN_MAX)) { |
1372 | dev_err(dev, | |
1373 | "Invalid maximum messages %d, not in range [1 - %lu]\n", | |
354b2e36 | 1374 | desc->max_msg, MSG_TOKEN_MAX); |
aa4f886f SH |
1375 | return -EINVAL; |
1376 | } | |
1377 | ||
9ca5a183 | 1378 | hash_init(info->pending_xfers); |
aa4f886f | 1379 | |
9ca5a183 CM |
1380 | /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */ |
1381 | info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(MSG_TOKEN_MAX), | |
aa4f886f SH |
1382 | sizeof(long), GFP_KERNEL); |
1383 | if (!info->xfer_alloc_table) | |
1384 | return -ENOMEM; | |
1385 | ||
9ca5a183 CM |
1386 | /* |
1387 | * Preallocate a number of xfers equal to max inflight messages, | |
1388 | * pre-initialize the buffer pointer to pre-allocated buffers and | |
1389 | * attach all of them to the free list | |
1390 | */ | |
1391 | INIT_HLIST_HEAD(&info->free_xfers); | |
1392 | for (i = 0; i < desc->max_msg; i++) { | |
1393 | xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL); | |
1394 | if (!xfer) | |
1395 | return -ENOMEM; | |
1396 | ||
aa4f886f SH |
1397 | xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size, |
1398 | GFP_KERNEL); | |
1399 | if (!xfer->rx.buf) | |
1400 | return -ENOMEM; | |
1401 | ||
1402 | xfer->tx.buf = xfer->rx.buf; | |
1403 | init_completion(&xfer->done); | |
ed7c04c1 | 1404 | spin_lock_init(&xfer->lock); |
9ca5a183 CM |
1405 | |
1406 | /* Add initialized xfer to the free list */ | |
1407 | hlist_add_head(&xfer->node, &info->free_xfers); | |
aa4f886f SH |
1408 | } |
1409 | ||
1410 | spin_lock_init(&info->xfer_lock); | |
1411 | ||
1412 | return 0; | |
1413 | } | |
1414 | ||
4ebd8f6d SH |
1415 | static int scmi_xfer_info_init(struct scmi_info *sinfo) |
1416 | { | |
1417 | int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo); | |
1418 | ||
1419 | if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE)) | |
1420 | ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo); | |
1421 | ||
1422 | return ret; | |
1423 | } | |
1424 | ||
5c8a47a5 VK |
1425 | static int scmi_chan_setup(struct scmi_info *info, struct device *dev, |
1426 | int prot_id, bool tx) | |
aa4f886f | 1427 | { |
3748daf7 | 1428 | int ret, idx; |
fbc4d81a | 1429 | struct scmi_chan_info *cinfo; |
46cc7c28 | 1430 | struct idr *idr; |
3748daf7 SH |
1431 | |
1432 | /* Transmit channel is first entry i.e. index 0 */ | |
1433 | idx = tx ? 0 : 1; | |
46cc7c28 | 1434 | idr = tx ? &info->tx_idr : &info->rx_idr; |
aa4f886f | 1435 | |
11040889 SH |
1436 | /* check if already allocated, used for multiple device per protocol */ |
1437 | cinfo = idr_find(idr, prot_id); | |
1438 | if (cinfo) | |
1439 | return 0; | |
1440 | ||
5c8a47a5 | 1441 | if (!info->desc->ops->chan_available(dev, idx)) { |
46cc7c28 SH |
1442 | cinfo = idr_find(idr, SCMI_PROTOCOL_BASE); |
1443 | if (unlikely(!cinfo)) /* Possible only if platform has no Rx */ | |
1444 | return -EINVAL; | |
907b6d14 SH |
1445 | goto idr_alloc; |
1446 | } | |
1447 | ||
fbc4d81a SH |
1448 | cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL); |
1449 | if (!cinfo) | |
1450 | return -ENOMEM; | |
1451 | ||
fbc4d81a SH |
1452 | cinfo->dev = dev; |
1453 | ||
5c8a47a5 VK |
1454 | ret = info->desc->ops->chan_setup(cinfo, info->dev, tx); |
1455 | if (ret) | |
aa4f886f | 1456 | return ret; |
aa4f886f | 1457 | |
907b6d14 | 1458 | idr_alloc: |
46cc7c28 | 1459 | ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL); |
907b6d14 SH |
1460 | if (ret != prot_id) { |
1461 | dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret); | |
1462 | return ret; | |
1463 | } | |
1464 | ||
1465 | cinfo->handle = &info->handle; | |
aa4f886f SH |
1466 | return 0; |
1467 | } | |
1468 | ||
46cc7c28 | 1469 | static inline int |
5c8a47a5 | 1470 | scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id) |
46cc7c28 | 1471 | { |
5c8a47a5 | 1472 | int ret = scmi_chan_setup(info, dev, prot_id, true); |
46cc7c28 SH |
1473 | |
1474 | if (!ret) /* Rx is optional, hence no error check */ | |
5c8a47a5 | 1475 | scmi_chan_setup(info, dev, prot_id, false); |
46cc7c28 SH |
1476 | |
1477 | return ret; | |
1478 | } | |
1479 | ||
d4f9dddd CM |
1480 | /** |
1481 | * scmi_get_protocol_device - Helper to get/create an SCMI device. | |
1482 | * | |
1483 | * @np: A device node representing a valid active protocols for the referred | |
1484 | * SCMI instance. | |
1485 | * @info: The referred SCMI instance for which we are getting/creating this | |
1486 | * device. | |
1487 | * @prot_id: The protocol ID. | |
1488 | * @name: The device name. | |
1489 | * | |
1490 | * Referring to the specific SCMI instance identified by @info, this helper | |
1491 | * takes care to return a properly initialized device matching the requested | |
1492 | * @proto_id and @name: if device was still not existent it is created as a | |
1493 | * child of the specified SCMI instance @info and its transport properly | |
1494 | * initialized as usual. | |
b98cf55e CM |
1495 | * |
1496 | * Return: A properly initialized scmi device, NULL otherwise. | |
d4f9dddd CM |
1497 | */ |
1498 | static inline struct scmi_device * | |
1499 | scmi_get_protocol_device(struct device_node *np, struct scmi_info *info, | |
1500 | int prot_id, const char *name) | |
bc40081d SH |
1501 | { |
1502 | struct scmi_device *sdev; | |
1503 | ||
d4f9dddd CM |
1504 | /* Already created for this parent SCMI instance ? */ |
1505 | sdev = scmi_child_dev_find(info->dev, prot_id, name); | |
1506 | if (sdev) | |
1507 | return sdev; | |
1508 | ||
1509 | pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id); | |
1510 | ||
ee7a9c9f | 1511 | sdev = scmi_device_create(np, info->dev, prot_id, name); |
bc40081d SH |
1512 | if (!sdev) { |
1513 | dev_err(info->dev, "failed to create %d protocol device\n", | |
1514 | prot_id); | |
d4f9dddd | 1515 | return NULL; |
bc40081d SH |
1516 | } |
1517 | ||
5c8a47a5 | 1518 | if (scmi_txrx_setup(info, &sdev->dev, prot_id)) { |
907b6d14 SH |
1519 | dev_err(&sdev->dev, "failed to setup transport\n"); |
1520 | scmi_device_destroy(sdev); | |
d4f9dddd | 1521 | return NULL; |
907b6d14 SH |
1522 | } |
1523 | ||
d4f9dddd CM |
1524 | return sdev; |
1525 | } | |
1526 | ||
1527 | static inline void | |
1528 | scmi_create_protocol_device(struct device_node *np, struct scmi_info *info, | |
1529 | int prot_id, const char *name) | |
1530 | { | |
1531 | struct scmi_device *sdev; | |
1532 | ||
1533 | sdev = scmi_get_protocol_device(np, info, prot_id, name); | |
1534 | if (!sdev) | |
1535 | return; | |
1536 | ||
bc40081d SH |
1537 | /* setup handle now as the transport is ready */ |
1538 | scmi_set_handle(sdev); | |
1539 | } | |
1540 | ||
d4f9dddd CM |
1541 | /** |
1542 | * scmi_create_protocol_devices - Create devices for all pending requests for | |
1543 | * this SCMI instance. | |
1544 | * | |
1545 | * @np: The device node describing the protocol | |
1546 | * @info: The SCMI instance descriptor | |
1547 | * @prot_id: The protocol ID | |
1548 | * | |
1549 | * All devices previously requested for this instance (if any) are found and | |
1550 | * created by scanning the proper @&scmi_requested_devices entry. | |
1551 | */ | |
1552 | static void scmi_create_protocol_devices(struct device_node *np, | |
1553 | struct scmi_info *info, int prot_id) | |
1554 | { | |
1555 | struct list_head *phead; | |
9c5c463f | 1556 | |
d4f9dddd CM |
1557 | mutex_lock(&scmi_requested_devices_mtx); |
1558 | phead = idr_find(&scmi_requested_devices, prot_id); | |
1559 | if (phead) { | |
1560 | struct scmi_requested_dev *rdev; | |
9c5c463f | 1561 | |
d4f9dddd CM |
1562 | list_for_each_entry(rdev, phead, node) |
1563 | scmi_create_protocol_device(np, info, prot_id, | |
1564 | rdev->id_table->name); | |
1565 | } | |
1566 | mutex_unlock(&scmi_requested_devices_mtx); | |
1567 | } | |
1568 | ||
1569 | /** | |
1570 | * scmi_protocol_device_request - Helper to request a device | |
1571 | * | |
1572 | * @id_table: A protocol/name pair descriptor for the device to be created. | |
1573 | * | |
1574 | * This helper let an SCMI driver request specific devices identified by the | |
1575 | * @id_table to be created for each active SCMI instance. | |
1576 | * | |
1577 | * The requested device name MUST NOT be already existent for any protocol; | |
1578 | * at first the freshly requested @id_table is annotated in the IDR table | |
1579 | * @scmi_requested_devices, then a matching device is created for each already | |
1580 | * active SCMI instance. (if any) | |
1581 | * | |
1582 | * This way the requested device is created straight-away for all the already | |
1583 | * initialized(probed) SCMI instances (handles) and it remains also annotated | |
1584 | * as pending creation if the requesting SCMI driver was loaded before some | |
1585 | * SCMI instance and related transports were available: when such late instance | |
1586 | * is probed, its probe will take care to scan the list of pending requested | |
1587 | * devices and create those on its own (see @scmi_create_protocol_devices and | |
1588 | * its enclosing loop) | |
1589 | * | |
1590 | * Return: 0 on Success | |
1591 | */ | |
1592 | int scmi_protocol_device_request(const struct scmi_device_id *id_table) | |
9c5c463f | 1593 | { |
d4f9dddd CM |
1594 | int ret = 0; |
1595 | unsigned int id = 0; | |
1596 | struct list_head *head, *phead = NULL; | |
1597 | struct scmi_requested_dev *rdev; | |
1598 | struct scmi_info *info; | |
9c5c463f | 1599 | |
d4f9dddd CM |
1600 | pr_debug("Requesting SCMI device (%s) for protocol %x\n", |
1601 | id_table->name, id_table->protocol_id); | |
1602 | ||
1603 | /* | |
1604 | * Search for the matching protocol rdev list and then search | |
1605 | * of any existent equally named device...fails if any duplicate found. | |
1606 | */ | |
1607 | mutex_lock(&scmi_requested_devices_mtx); | |
1608 | idr_for_each_entry(&scmi_requested_devices, head, id) { | |
1609 | if (!phead) { | |
1610 | /* A list found registered in the IDR is never empty */ | |
1611 | rdev = list_first_entry(head, struct scmi_requested_dev, | |
1612 | node); | |
1613 | if (rdev->id_table->protocol_id == | |
1614 | id_table->protocol_id) | |
1615 | phead = head; | |
1616 | } | |
1617 | list_for_each_entry(rdev, head, node) { | |
1618 | if (!strcmp(rdev->id_table->name, id_table->name)) { | |
1619 | pr_err("Ignoring duplicate request [%d] %s\n", | |
1620 | rdev->id_table->protocol_id, | |
1621 | rdev->id_table->name); | |
1622 | ret = -EINVAL; | |
1623 | goto out; | |
1624 | } | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | /* | |
1629 | * No duplicate found for requested id_table, so let's create a new | |
1630 | * requested device entry for this new valid request. | |
1631 | */ | |
1632 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); | |
1633 | if (!rdev) { | |
1634 | ret = -ENOMEM; | |
1635 | goto out; | |
1636 | } | |
1637 | rdev->id_table = id_table; | |
1638 | ||
1639 | /* | |
1640 | * Append the new requested device table descriptor to the head of the | |
1641 | * related protocol list, eventually creating such head if not already | |
1642 | * there. | |
1643 | */ | |
1644 | if (!phead) { | |
1645 | phead = kzalloc(sizeof(*phead), GFP_KERNEL); | |
1646 | if (!phead) { | |
1647 | kfree(rdev); | |
1648 | ret = -ENOMEM; | |
1649 | goto out; | |
1650 | } | |
1651 | INIT_LIST_HEAD(phead); | |
1652 | ||
1653 | ret = idr_alloc(&scmi_requested_devices, (void *)phead, | |
1654 | id_table->protocol_id, | |
1655 | id_table->protocol_id + 1, GFP_KERNEL); | |
1656 | if (ret != id_table->protocol_id) { | |
1657 | pr_err("Failed to save SCMI device - ret:%d\n", ret); | |
1658 | kfree(rdev); | |
1659 | kfree(phead); | |
1660 | ret = -EINVAL; | |
1661 | goto out; | |
1662 | } | |
1663 | ret = 0; | |
1664 | } | |
1665 | list_add(&rdev->node, phead); | |
1666 | ||
1667 | /* | |
1668 | * Now effectively create and initialize the requested device for every | |
1669 | * already initialized SCMI instance which has registered the requested | |
1670 | * protocol as a valid active one: i.e. defined in DT and supported by | |
1671 | * current platform FW. | |
1672 | */ | |
1673 | mutex_lock(&scmi_list_mutex); | |
1674 | list_for_each_entry(info, &scmi_list, node) { | |
1675 | struct device_node *child; | |
1676 | ||
1677 | child = idr_find(&info->active_protocols, | |
1678 | id_table->protocol_id); | |
1679 | if (child) { | |
1680 | struct scmi_device *sdev; | |
1681 | ||
1682 | sdev = scmi_get_protocol_device(child, info, | |
1683 | id_table->protocol_id, | |
1684 | id_table->name); | |
1685 | /* Set handle if not already set: device existed */ | |
1686 | if (sdev && !sdev->handle) | |
1687 | sdev->handle = | |
1688 | scmi_handle_get_from_info_unlocked(info); | |
1689 | } else { | |
1690 | dev_err(info->dev, | |
1691 | "Failed. SCMI protocol %d not active.\n", | |
1692 | id_table->protocol_id); | |
1693 | } | |
1694 | } | |
1695 | mutex_unlock(&scmi_list_mutex); | |
1696 | ||
1697 | out: | |
1698 | mutex_unlock(&scmi_requested_devices_mtx); | |
9c5c463f | 1699 | |
d4f9dddd CM |
1700 | return ret; |
1701 | } | |
9c5c463f | 1702 | |
d4f9dddd CM |
1703 | /** |
1704 | * scmi_protocol_device_unrequest - Helper to unrequest a device | |
1705 | * | |
1706 | * @id_table: A protocol/name pair descriptor for the device to be unrequested. | |
1707 | * | |
1708 | * An helper to let an SCMI driver release its request about devices; note that | |
1709 | * devices are created and initialized once the first SCMI driver request them | |
1710 | * but they destroyed only on SCMI core unloading/unbinding. | |
1711 | * | |
1712 | * The current SCMI transport layer uses such devices as internal references and | |
1713 | * as such they could be shared as same transport between multiple drivers so | |
1714 | * that cannot be safely destroyed till the whole SCMI stack is removed. | |
1715 | * (unless adding further burden of refcounting.) | |
1716 | */ | |
1717 | void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table) | |
1718 | { | |
1719 | struct list_head *phead; | |
1720 | ||
1721 | pr_debug("Unrequesting SCMI device (%s) for protocol %x\n", | |
1722 | id_table->name, id_table->protocol_id); | |
1723 | ||
1724 | mutex_lock(&scmi_requested_devices_mtx); | |
1725 | phead = idr_find(&scmi_requested_devices, id_table->protocol_id); | |
1726 | if (phead) { | |
1727 | struct scmi_requested_dev *victim, *tmp; | |
1728 | ||
1729 | list_for_each_entry_safe(victim, tmp, phead, node) { | |
1730 | if (!strcmp(victim->id_table->name, id_table->name)) { | |
1731 | list_del(&victim->node); | |
1732 | kfree(victim); | |
1733 | break; | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | if (list_empty(phead)) { | |
1738 | idr_remove(&scmi_requested_devices, | |
1739 | id_table->protocol_id); | |
1740 | kfree(phead); | |
9c5c463f SH |
1741 | } |
1742 | } | |
d4f9dddd | 1743 | mutex_unlock(&scmi_requested_devices_mtx); |
9c5c463f SH |
1744 | } |
1745 | ||
aa4f886f SH |
1746 | static int scmi_probe(struct platform_device *pdev) |
1747 | { | |
1748 | int ret; | |
1749 | struct scmi_handle *handle; | |
1750 | const struct scmi_desc *desc; | |
1751 | struct scmi_info *info; | |
1752 | struct device *dev = &pdev->dev; | |
bc40081d | 1753 | struct device_node *child, *np = dev->of_node; |
aa4f886f | 1754 | |
d9350f21 AP |
1755 | desc = of_device_get_match_data(dev); |
1756 | if (!desc) | |
1757 | return -EINVAL; | |
aa4f886f SH |
1758 | |
1759 | info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); | |
1760 | if (!info) | |
1761 | return -ENOMEM; | |
1762 | ||
1763 | info->dev = dev; | |
1764 | info->desc = desc; | |
1765 | INIT_LIST_HEAD(&info->node); | |
48dc16e2 CM |
1766 | idr_init(&info->protocols); |
1767 | mutex_init(&info->protocols_mtx); | |
d4f9dddd | 1768 | idr_init(&info->active_protocols); |
aa4f886f | 1769 | |
aa4f886f | 1770 | platform_set_drvdata(pdev, info); |
907b6d14 | 1771 | idr_init(&info->tx_idr); |
46cc7c28 | 1772 | idr_init(&info->rx_idr); |
aa4f886f SH |
1773 | |
1774 | handle = &info->handle; | |
1775 | handle->dev = info->dev; | |
b6f20ff8 | 1776 | handle->version = &info->version; |
23934efe CM |
1777 | handle->devm_protocol_get = scmi_devm_protocol_get; |
1778 | handle->devm_protocol_put = scmi_devm_protocol_put; | |
aa4f886f | 1779 | |
5c8a47a5 | 1780 | ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE); |
aa4f886f SH |
1781 | if (ret) |
1782 | return ret; | |
1783 | ||
4ebd8f6d SH |
1784 | ret = scmi_xfer_info_init(info); |
1785 | if (ret) | |
1786 | return ret; | |
1787 | ||
6b8a6913 CM |
1788 | if (scmi_notification_init(handle)) |
1789 | dev_err(dev, "SCMI Notifications NOT available.\n"); | |
1790 | ||
8d3581c2 CM |
1791 | /* |
1792 | * Trigger SCMI Base protocol initialization. | |
1793 | * It's mandatory and won't be ever released/deinit until the | |
1794 | * SCMI stack is shutdown/unloaded as a whole. | |
1795 | */ | |
1796 | ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE); | |
b6f20ff8 | 1797 | if (ret) { |
8d3581c2 | 1798 | dev_err(dev, "unable to communicate with SCMI\n"); |
b6f20ff8 SH |
1799 | return ret; |
1800 | } | |
1801 | ||
aa4f886f SH |
1802 | mutex_lock(&scmi_list_mutex); |
1803 | list_add_tail(&info->node, &scmi_list); | |
1804 | mutex_unlock(&scmi_list_mutex); | |
1805 | ||
bc40081d SH |
1806 | for_each_available_child_of_node(np, child) { |
1807 | u32 prot_id; | |
1808 | ||
1809 | if (of_property_read_u32(child, "reg", &prot_id)) | |
1810 | continue; | |
1811 | ||
354b2e36 SH |
1812 | if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id)) |
1813 | dev_err(dev, "Out of range protocol %d\n", prot_id); | |
bc40081d SH |
1814 | |
1815 | if (!scmi_is_protocol_implemented(handle, prot_id)) { | |
1816 | dev_err(dev, "SCMI protocol %d not implemented\n", | |
1817 | prot_id); | |
1818 | continue; | |
1819 | } | |
1820 | ||
d4f9dddd CM |
1821 | /* |
1822 | * Save this valid DT protocol descriptor amongst | |
1823 | * @active_protocols for this SCMI instance/ | |
1824 | */ | |
1825 | ret = idr_alloc(&info->active_protocols, child, | |
1826 | prot_id, prot_id + 1, GFP_KERNEL); | |
1827 | if (ret != prot_id) { | |
1828 | dev_err(dev, "SCMI protocol %d already activated. Skip\n", | |
1829 | prot_id); | |
1830 | continue; | |
1831 | } | |
1832 | ||
1833 | of_node_get(child); | |
9c5c463f | 1834 | scmi_create_protocol_devices(child, info, prot_id); |
bc40081d SH |
1835 | } |
1836 | ||
aa4f886f SH |
1837 | return 0; |
1838 | } | |
1839 | ||
5c8a47a5 | 1840 | void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id) |
2747a967 | 1841 | { |
2747a967 | 1842 | idr_remove(idr, id); |
2747a967 SH |
1843 | } |
1844 | ||
1845 | static int scmi_remove(struct platform_device *pdev) | |
1846 | { | |
d4f9dddd | 1847 | int ret = 0, id; |
2747a967 SH |
1848 | struct scmi_info *info = platform_get_drvdata(pdev); |
1849 | struct idr *idr = &info->tx_idr; | |
d4f9dddd | 1850 | struct device_node *child; |
2747a967 SH |
1851 | |
1852 | mutex_lock(&scmi_list_mutex); | |
1853 | if (info->users) | |
1854 | ret = -EBUSY; | |
1855 | else | |
1856 | list_del(&info->node); | |
1857 | mutex_unlock(&scmi_list_mutex); | |
1858 | ||
1859 | if (ret) | |
1860 | return ret; | |
1861 | ||
a90b6543 CM |
1862 | scmi_notification_exit(&info->handle); |
1863 | ||
48dc16e2 CM |
1864 | mutex_lock(&info->protocols_mtx); |
1865 | idr_destroy(&info->protocols); | |
1866 | mutex_unlock(&info->protocols_mtx); | |
1867 | ||
d4f9dddd CM |
1868 | idr_for_each_entry(&info->active_protocols, child, id) |
1869 | of_node_put(child); | |
1870 | idr_destroy(&info->active_protocols); | |
1871 | ||
2747a967 | 1872 | /* Safe to free channels since no more users */ |
5c8a47a5 | 1873 | ret = idr_for_each(idr, info->desc->ops->chan_free, idr); |
2747a967 SH |
1874 | idr_destroy(&info->tx_idr); |
1875 | ||
46cc7c28 | 1876 | idr = &info->rx_idr; |
5c8a47a5 | 1877 | ret = idr_for_each(idr, info->desc->ops->chan_free, idr); |
46cc7c28 SH |
1878 | idr_destroy(&info->rx_idr); |
1879 | ||
2747a967 SH |
1880 | return ret; |
1881 | } | |
1882 | ||
4605e224 SH |
1883 | static ssize_t protocol_version_show(struct device *dev, |
1884 | struct device_attribute *attr, char *buf) | |
1885 | { | |
1886 | struct scmi_info *info = dev_get_drvdata(dev); | |
1887 | ||
1888 | return sprintf(buf, "%u.%u\n", info->version.major_ver, | |
1889 | info->version.minor_ver); | |
1890 | } | |
1891 | static DEVICE_ATTR_RO(protocol_version); | |
1892 | ||
1893 | static ssize_t firmware_version_show(struct device *dev, | |
1894 | struct device_attribute *attr, char *buf) | |
1895 | { | |
1896 | struct scmi_info *info = dev_get_drvdata(dev); | |
1897 | ||
1898 | return sprintf(buf, "0x%x\n", info->version.impl_ver); | |
1899 | } | |
1900 | static DEVICE_ATTR_RO(firmware_version); | |
1901 | ||
1902 | static ssize_t vendor_id_show(struct device *dev, | |
1903 | struct device_attribute *attr, char *buf) | |
1904 | { | |
1905 | struct scmi_info *info = dev_get_drvdata(dev); | |
1906 | ||
1907 | return sprintf(buf, "%s\n", info->version.vendor_id); | |
1908 | } | |
1909 | static DEVICE_ATTR_RO(vendor_id); | |
1910 | ||
1911 | static ssize_t sub_vendor_id_show(struct device *dev, | |
1912 | struct device_attribute *attr, char *buf) | |
1913 | { | |
1914 | struct scmi_info *info = dev_get_drvdata(dev); | |
1915 | ||
1916 | return sprintf(buf, "%s\n", info->version.sub_vendor_id); | |
1917 | } | |
1918 | static DEVICE_ATTR_RO(sub_vendor_id); | |
1919 | ||
1920 | static struct attribute *versions_attrs[] = { | |
1921 | &dev_attr_firmware_version.attr, | |
1922 | &dev_attr_protocol_version.attr, | |
1923 | &dev_attr_vendor_id.attr, | |
1924 | &dev_attr_sub_vendor_id.attr, | |
1925 | NULL, | |
1926 | }; | |
1927 | ATTRIBUTE_GROUPS(versions); | |
1928 | ||
2747a967 SH |
1929 | /* Each compatible listed below must have descriptor associated with it */ |
1930 | static const struct of_device_id scmi_of_match[] = { | |
ab7766b7 | 1931 | #ifdef CONFIG_MAILBOX |
5c8a47a5 | 1932 | { .compatible = "arm,scmi", .data = &scmi_mailbox_desc }, |
ab7766b7 | 1933 | #endif |
d7642823 | 1934 | #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY |
1dc65580 PF |
1935 | { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc}, |
1936 | #endif | |
2747a967 SH |
1937 | { /* Sentinel */ }, |
1938 | }; | |
1939 | ||
1940 | MODULE_DEVICE_TABLE(of, scmi_of_match); | |
1941 | ||
aa4f886f SH |
1942 | static struct platform_driver scmi_driver = { |
1943 | .driver = { | |
1944 | .name = "arm-scmi", | |
1945 | .of_match_table = scmi_of_match, | |
4605e224 | 1946 | .dev_groups = versions_groups, |
aa4f886f SH |
1947 | }, |
1948 | .probe = scmi_probe, | |
1949 | .remove = scmi_remove, | |
1950 | }; | |
1951 | ||
ceac257d CM |
1952 | /** |
1953 | * __scmi_transports_setup - Common helper to call transport-specific | |
1954 | * .init/.exit code if provided. | |
1955 | * | |
1956 | * @init: A flag to distinguish between init and exit. | |
1957 | * | |
1958 | * Note that, if provided, we invoke .init/.exit functions for all the | |
1959 | * transports currently compiled in. | |
1960 | * | |
1961 | * Return: 0 on Success. | |
1962 | */ | |
1963 | static inline int __scmi_transports_setup(bool init) | |
1964 | { | |
1965 | int ret = 0; | |
1966 | const struct of_device_id *trans; | |
1967 | ||
1968 | for (trans = scmi_of_match; trans->data; trans++) { | |
1969 | const struct scmi_desc *tdesc = trans->data; | |
1970 | ||
1971 | if ((init && !tdesc->transport_init) || | |
1972 | (!init && !tdesc->transport_exit)) | |
1973 | continue; | |
1974 | ||
1975 | if (init) | |
1976 | ret = tdesc->transport_init(); | |
1977 | else | |
1978 | tdesc->transport_exit(); | |
1979 | ||
1980 | if (ret) { | |
1981 | pr_err("SCMI transport %s FAILED initialization!\n", | |
1982 | trans->compatible); | |
1983 | break; | |
1984 | } | |
1985 | } | |
1986 | ||
1987 | return ret; | |
1988 | } | |
1989 | ||
1990 | static int __init scmi_transports_init(void) | |
1991 | { | |
1992 | return __scmi_transports_setup(true); | |
1993 | } | |
1994 | ||
1995 | static void __exit scmi_transports_exit(void) | |
1996 | { | |
1997 | __scmi_transports_setup(false); | |
1998 | } | |
1999 | ||
5a2f0a0b SH |
2000 | static int __init scmi_driver_init(void) |
2001 | { | |
ceac257d CM |
2002 | int ret; |
2003 | ||
5a2f0a0b SH |
2004 | scmi_bus_init(); |
2005 | ||
ceac257d CM |
2006 | /* Initialize any compiled-in transport which provided an init/exit */ |
2007 | ret = scmi_transports_init(); | |
2008 | if (ret) | |
2009 | return ret; | |
2010 | ||
48dc16e2 CM |
2011 | scmi_base_register(); |
2012 | ||
1eaf18e3 SH |
2013 | scmi_clock_register(); |
2014 | scmi_perf_register(); | |
2015 | scmi_power_register(); | |
2016 | scmi_reset_register(); | |
2017 | scmi_sensors_register(); | |
2add5cac | 2018 | scmi_voltage_register(); |
1eaf18e3 SH |
2019 | scmi_system_register(); |
2020 | ||
5a2f0a0b SH |
2021 | return platform_driver_register(&scmi_driver); |
2022 | } | |
1eaf18e3 | 2023 | subsys_initcall(scmi_driver_init); |
5a2f0a0b SH |
2024 | |
2025 | static void __exit scmi_driver_exit(void) | |
2026 | { | |
48dc16e2 | 2027 | scmi_base_unregister(); |
5a2f0a0b | 2028 | |
1eaf18e3 SH |
2029 | scmi_clock_unregister(); |
2030 | scmi_perf_unregister(); | |
2031 | scmi_power_unregister(); | |
2032 | scmi_reset_unregister(); | |
2033 | scmi_sensors_unregister(); | |
2add5cac | 2034 | scmi_voltage_unregister(); |
1eaf18e3 SH |
2035 | scmi_system_unregister(); |
2036 | ||
48dc16e2 CM |
2037 | scmi_bus_exit(); |
2038 | ||
ceac257d CM |
2039 | scmi_transports_exit(); |
2040 | ||
5a2f0a0b SH |
2041 | platform_driver_unregister(&scmi_driver); |
2042 | } | |
2043 | module_exit(scmi_driver_exit); | |
aa4f886f SH |
2044 | |
2045 | MODULE_ALIAS("platform: arm-scmi"); | |
2046 | MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); | |
2047 | MODULE_DESCRIPTION("ARM SCMI protocol driver"); | |
2048 | MODULE_LICENSE("GPL v2"); |