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 | ||
ee5dceda CM |
17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
18 | ||
aa4f886f | 19 | #include <linux/bitmap.h> |
c3d4aed7 | 20 | #include <linux/debugfs.h> |
23934efe | 21 | #include <linux/device.h> |
aa4f886f | 22 | #include <linux/export.h> |
48dc16e2 | 23 | #include <linux/idr.h> |
aa4f886f | 24 | #include <linux/io.h> |
6f9ea4da | 25 | #include <linux/io-64-nonatomic-hi-lo.h> |
aa4f886f | 26 | #include <linux/kernel.h> |
d4c3751a | 27 | #include <linux/ktime.h> |
9ca5a183 | 28 | #include <linux/hashtable.h> |
d4f9dddd | 29 | #include <linux/list.h> |
aa4f886f | 30 | #include <linux/module.h> |
5b45759c RH |
31 | #include <linux/of.h> |
32 | #include <linux/platform_device.h> | |
d4c3751a | 33 | #include <linux/processor.h> |
48dc16e2 | 34 | #include <linux/refcount.h> |
aa4f886f | 35 | #include <linux/slab.h> |
fc110108 | 36 | #include <linux/xarray.h> |
aa4f886f SH |
37 | |
38 | #include "common.h" | |
6b8a6913 | 39 | #include "notify.h" |
aa4f886f | 40 | |
7063887b CM |
41 | #include "raw_mode.h" |
42 | ||
729d3530 LL |
43 | #define CREATE_TRACE_POINTS |
44 | #include <trace/events/scmi.h> | |
45 | ||
75c86dc7 CM |
46 | static DEFINE_IDA(scmi_id); |
47 | ||
fc110108 | 48 | static DEFINE_XARRAY(scmi_protocols); |
9115c20a | 49 | |
1baf47c2 | 50 | /* List of all SCMI devices active in system */ |
aa4f886f SH |
51 | static LIST_HEAD(scmi_list); |
52 | /* Protection for the entire list */ | |
53 | static DEFINE_MUTEX(scmi_list_mutex); | |
729d3530 LL |
54 | /* Track the unique id for the transfers for debug & profiling purpose */ |
55 | static atomic_t transfer_last_id; | |
aa4f886f | 56 | |
c3d4aed7 CM |
57 | static struct dentry *scmi_top_dentry; |
58 | ||
aa4f886f SH |
59 | /** |
60 | * struct scmi_xfers_info - Structure to manage transfer information | |
61 | * | |
aa4f886f SH |
62 | * @xfer_alloc_table: Bitmap table for allocated messages. |
63 | * Index of this bitmap table is also used for message | |
64 | * sequence identifier. | |
65 | * @xfer_lock: Protection for message allocation | |
c92c3e38 | 66 | * @max_msg: Maximum number of messages that can be pending |
9ca5a183 CM |
67 | * @free_xfers: A free list for available to use xfers. It is initialized with |
68 | * a number of xfers equal to the maximum allowed in-flight | |
69 | * messages. | |
70 | * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the | |
71 | * currently in-flight messages. | |
aa4f886f SH |
72 | */ |
73 | struct scmi_xfers_info { | |
aa4f886f | 74 | unsigned long *xfer_alloc_table; |
aa4f886f | 75 | spinlock_t xfer_lock; |
c92c3e38 | 76 | int max_msg; |
9ca5a183 CM |
77 | struct hlist_head free_xfers; |
78 | DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ); | |
aa4f886f SH |
79 | }; |
80 | ||
48dc16e2 CM |
81 | /** |
82 | * struct scmi_protocol_instance - Describe an initialized protocol instance. | |
d7b6cc56 | 83 | * @handle: Reference to the SCMI handle associated to this protocol instance. |
48dc16e2 CM |
84 | * @proto: A reference to the protocol descriptor. |
85 | * @gid: A reference for per-protocol devres management. | |
86 | * @users: A refcount to track effective users of this protocol. | |
d7b6cc56 | 87 | * @priv: Reference for optional protocol private data. |
b5efc28a | 88 | * @version: Protocol version supported by the platform as detected at runtime. |
8c80c42a CM |
89 | * @negotiated_version: When the platform supports a newer protocol version, |
90 | * the agent will try to negotiate with the platform the | |
91 | * usage of the newest version known to it, since | |
92 | * backward compatibility is NOT automatically assured. | |
93 | * This field is NON-zero when a successful negotiation | |
94 | * has completed. | |
d7b6cc56 CM |
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 | 106 | void *priv; |
b5efc28a | 107 | unsigned int version; |
8c80c42a | 108 | unsigned int negotiated_version; |
d7b6cc56 | 109 | struct scmi_protocol_handle ph; |
48dc16e2 CM |
110 | }; |
111 | ||
d7b6cc56 CM |
112 | #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph) |
113 | ||
c3d4aed7 CM |
114 | /** |
115 | * struct scmi_debug_info - Debug common info | |
116 | * @top_dentry: A reference to the top debugfs dentry | |
117 | * @name: Name of this SCMI instance | |
118 | * @type: Type of this SCMI instance | |
119 | * @is_atomic: Flag to state if the transport of this instance is atomic | |
120 | */ | |
121 | struct scmi_debug_info { | |
122 | struct dentry *top_dentry; | |
123 | const char *name; | |
124 | const char *type; | |
125 | bool is_atomic; | |
126 | }; | |
127 | ||
aa4f886f | 128 | /** |
1baf47c2 | 129 | * struct scmi_info - Structure representing a SCMI instance |
aa4f886f | 130 | * |
75c86dc7 | 131 | * @id: A sequence number starting from zero identifying this instance |
aa4f886f SH |
132 | * @dev: Device pointer |
133 | * @desc: SoC description for this instance | |
b6f20ff8 SH |
134 | * @version: SCMI revision information containing protocol version, |
135 | * implementation version and (sub-)vendor identification. | |
71af05a7 | 136 | * @handle: Instance of SCMI handle to send to clients |
38c927fb | 137 | * @tx_minfo: Universal Transmit Message management info |
4ebd8f6d | 138 | * @rx_minfo: Universal Receive Message management info |
3748daf7 | 139 | * @tx_idr: IDR object to map protocol id to Tx channel info pointer |
46cc7c28 | 140 | * @rx_idr: IDR object to map protocol id to Rx channel info pointer |
48dc16e2 CM |
141 | * @protocols: IDR for protocols' instance descriptors initialized for |
142 | * this SCMI instance: populated on protocol's first attempted | |
143 | * usage. | |
144 | * @protocols_mtx: A mutex to protect protocols instances initialization. | |
1baf47c2 | 145 | * @protocols_imp: List of protocols implemented, currently maximum of |
776b6c8a CM |
146 | * scmi_revision_info.num_protocols elements allocated by the |
147 | * base protocol | |
d4f9dddd CM |
148 | * @active_protocols: IDR storing device_nodes for protocols actually defined |
149 | * in the DT and confirmed as implemented by fw. | |
05976c5f CM |
150 | * @atomic_threshold: Optional system wide DT-configured threshold, expressed |
151 | * in microseconds, for atomic operations. | |
152 | * Only SCMI synchronous commands reported by the platform | |
153 | * to have an execution latency lesser-equal to the threshold | |
154 | * should be considered for atomic mode operation: such | |
155 | * decision is finally left up to the SCMI drivers. | |
a02d7c93 | 156 | * @notify_priv: Pointer to private data structure specific to notifications. |
1baf47c2 | 157 | * @node: List head |
aa4f886f | 158 | * @users: Number of users of this instance |
53b8c25d CM |
159 | * @bus_nb: A notifier to listen for device bind/unbind on the scmi bus |
160 | * @dev_req_nb: A notifier to listen for device request/unrequest on the scmi | |
161 | * bus | |
ee5dceda | 162 | * @devreq_mtx: A mutex to serialize device creation for this SCMI instance |
c3d4aed7 | 163 | * @dbg: A pointer to debugfs related data (if any) |
7063887b | 164 | * @raw: An opaque reference handle used by SCMI Raw mode. |
aa4f886f SH |
165 | */ |
166 | struct scmi_info { | |
75c86dc7 | 167 | int id; |
aa4f886f SH |
168 | struct device *dev; |
169 | const struct scmi_desc *desc; | |
b6f20ff8 | 170 | struct scmi_revision_info version; |
aa4f886f | 171 | struct scmi_handle handle; |
38c927fb | 172 | struct scmi_xfers_info tx_minfo; |
4ebd8f6d | 173 | struct scmi_xfers_info rx_minfo; |
907b6d14 | 174 | struct idr tx_idr; |
46cc7c28 | 175 | struct idr rx_idr; |
48dc16e2 CM |
176 | struct idr protocols; |
177 | /* Ensure mutual exclusive access to protocols instance array */ | |
178 | struct mutex protocols_mtx; | |
b6f20ff8 | 179 | u8 *protocols_imp; |
d4f9dddd | 180 | struct idr active_protocols; |
05976c5f | 181 | unsigned int atomic_threshold; |
a02d7c93 | 182 | void *notify_priv; |
aa4f886f SH |
183 | struct list_head node; |
184 | int users; | |
53b8c25d CM |
185 | struct notifier_block bus_nb; |
186 | struct notifier_block dev_req_nb; | |
ee5dceda CM |
187 | /* Serialize device creation process for this instance */ |
188 | struct mutex devreq_mtx; | |
c3d4aed7 | 189 | struct scmi_debug_info *dbg; |
7063887b | 190 | void *raw; |
aa4f886f SH |
191 | }; |
192 | ||
aa4f886f | 193 | #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle) |
53b8c25d CM |
194 | #define bus_nb_to_scmi_info(nb) container_of(nb, struct scmi_info, bus_nb) |
195 | #define req_nb_to_scmi_info(nb) container_of(nb, struct scmi_info, dev_req_nb) | |
aa4f886f | 196 | |
fc110108 CM |
197 | static unsigned long |
198 | scmi_vendor_protocol_signature(unsigned int protocol_id, char *vendor_id, | |
199 | char *sub_vendor_id, u32 impl_ver) | |
9115c20a | 200 | { |
fc110108 CM |
201 | char *signature, *p; |
202 | unsigned long hash = 0; | |
9115c20a | 203 | |
fc110108 CM |
204 | /* vendor_id/sub_vendor_id guaranteed <= SCMI_SHORT_NAME_MAX_SIZE */ |
205 | signature = kasprintf(GFP_KERNEL, "%02X|%s|%s|0x%08X", protocol_id, | |
206 | vendor_id ?: "", sub_vendor_id ?: "", impl_ver); | |
207 | if (!signature) | |
208 | return 0; | |
209 | ||
210 | p = signature; | |
211 | while (*p) | |
212 | hash = partial_name_hash(tolower(*p++), hash); | |
213 | hash = end_name_hash(hash); | |
214 | ||
215 | kfree(signature); | |
216 | ||
217 | return hash; | |
218 | } | |
219 | ||
220 | static unsigned long | |
221 | scmi_protocol_key_calculate(int protocol_id, char *vendor_id, | |
222 | char *sub_vendor_id, u32 impl_ver) | |
223 | { | |
224 | if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE) | |
225 | return protocol_id; | |
226 | else | |
227 | return scmi_vendor_protocol_signature(protocol_id, vendor_id, | |
228 | sub_vendor_id, impl_ver); | |
229 | } | |
230 | ||
231 | static const struct scmi_protocol * | |
232 | __scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id, | |
233 | char *sub_vendor_id, u32 impl_ver) | |
234 | { | |
235 | unsigned long key; | |
236 | struct scmi_protocol *proto = NULL; | |
237 | ||
238 | key = scmi_protocol_key_calculate(protocol_id, vendor_id, | |
239 | sub_vendor_id, impl_ver); | |
240 | if (key) | |
241 | proto = xa_load(&scmi_protocols, key); | |
242 | ||
243 | return proto; | |
244 | } | |
245 | ||
246 | static const struct scmi_protocol * | |
247 | scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id, | |
248 | char *sub_vendor_id, u32 impl_ver) | |
249 | { | |
250 | const struct scmi_protocol *proto = NULL; | |
251 | ||
252 | /* Searching for closest match ...*/ | |
253 | proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id, | |
254 | sub_vendor_id, impl_ver); | |
255 | if (proto) | |
256 | return proto; | |
257 | ||
258 | /* Any match just on vendor/sub_vendor ? */ | |
259 | if (impl_ver) { | |
260 | proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id, | |
261 | sub_vendor_id, 0); | |
262 | if (proto) | |
263 | return proto; | |
264 | } | |
265 | ||
266 | /* Any match just on the vendor ? */ | |
267 | if (sub_vendor_id) | |
268 | proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id, | |
269 | NULL, 0); | |
270 | return proto; | |
271 | } | |
272 | ||
273 | static const struct scmi_protocol * | |
274 | scmi_protocol_get(int protocol_id, struct scmi_revision_info *version) | |
275 | { | |
276 | const struct scmi_protocol *proto = NULL; | |
277 | ||
278 | if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE) | |
279 | proto = xa_load(&scmi_protocols, protocol_id); | |
280 | else | |
281 | proto = scmi_vendor_protocol_lookup(protocol_id, | |
282 | version->vendor_id, | |
283 | version->sub_vendor_id, | |
284 | version->impl_ver); | |
9115c20a CM |
285 | if (!proto || !try_module_get(proto->owner)) { |
286 | pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id); | |
287 | return NULL; | |
288 | } | |
289 | ||
290 | pr_debug("Found SCMI Protocol 0x%x\n", protocol_id); | |
291 | ||
fc110108 CM |
292 | if (protocol_id >= SCMI_PROTOCOL_VENDOR_BASE) |
293 | pr_info("Loaded SCMI Vendor Protocol 0x%x - %s %s %X\n", | |
294 | protocol_id, proto->vendor_id ?: "", | |
295 | proto->sub_vendor_id ?: "", proto->impl_ver); | |
296 | ||
9115c20a CM |
297 | return proto; |
298 | } | |
299 | ||
fc110108 | 300 | static void scmi_protocol_put(const struct scmi_protocol *proto) |
9115c20a | 301 | { |
9115c20a CM |
302 | if (proto) |
303 | module_put(proto->owner); | |
304 | } | |
305 | ||
fc110108 CM |
306 | static int scmi_vendor_protocol_check(const struct scmi_protocol *proto) |
307 | { | |
308 | if (!proto->vendor_id) { | |
309 | pr_err("missing vendor_id for protocol 0x%x\n", proto->id); | |
310 | return -EINVAL; | |
311 | } | |
312 | ||
313 | if (strlen(proto->vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) { | |
314 | pr_err("malformed vendor_id for protocol 0x%x\n", proto->id); | |
315 | return -EINVAL; | |
316 | } | |
317 | ||
318 | if (proto->sub_vendor_id && | |
319 | strlen(proto->sub_vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) { | |
320 | pr_err("malformed sub_vendor_id for protocol 0x%x\n", | |
321 | proto->id); | |
322 | return -EINVAL; | |
323 | } | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
9115c20a CM |
328 | int scmi_protocol_register(const struct scmi_protocol *proto) |
329 | { | |
330 | int ret; | |
fc110108 | 331 | unsigned long key; |
9115c20a CM |
332 | |
333 | if (!proto) { | |
334 | pr_err("invalid protocol\n"); | |
335 | return -EINVAL; | |
336 | } | |
337 | ||
338 | if (!proto->instance_init) { | |
339 | pr_err("missing init for protocol 0x%x\n", proto->id); | |
340 | return -EINVAL; | |
341 | } | |
342 | ||
fc110108 CM |
343 | if (proto->id >= SCMI_PROTOCOL_VENDOR_BASE && |
344 | scmi_vendor_protocol_check(proto)) | |
345 | return -EINVAL; | |
346 | ||
347 | /* | |
348 | * Calculate a protocol key to register this protocol with the core; | |
349 | * key value 0 is considered invalid. | |
350 | */ | |
351 | key = scmi_protocol_key_calculate(proto->id, proto->vendor_id, | |
352 | proto->sub_vendor_id, | |
353 | proto->impl_ver); | |
354 | if (!key) | |
355 | return -EINVAL; | |
356 | ||
357 | ret = xa_insert(&scmi_protocols, key, (void *)proto, GFP_KERNEL); | |
358 | if (ret) { | |
359 | pr_err("unable to allocate SCMI protocol slot for 0x%x - err %d\n", | |
9115c20a CM |
360 | proto->id, ret); |
361 | return ret; | |
362 | } | |
363 | ||
364 | pr_debug("Registered SCMI Protocol 0x%x\n", proto->id); | |
365 | ||
366 | return 0; | |
367 | } | |
368 | EXPORT_SYMBOL_GPL(scmi_protocol_register); | |
369 | ||
370 | void scmi_protocol_unregister(const struct scmi_protocol *proto) | |
371 | { | |
fc110108 CM |
372 | unsigned long key; |
373 | ||
374 | key = scmi_protocol_key_calculate(proto->id, proto->vendor_id, | |
375 | proto->sub_vendor_id, | |
376 | proto->impl_ver); | |
377 | if (!key) | |
378 | return; | |
379 | ||
380 | xa_erase(&scmi_protocols, key); | |
9115c20a CM |
381 | |
382 | pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id); | |
383 | } | |
384 | EXPORT_SYMBOL_GPL(scmi_protocol_unregister); | |
385 | ||
ee5dceda CM |
386 | /** |
387 | * scmi_create_protocol_devices - Create devices for all pending requests for | |
388 | * this SCMI instance. | |
389 | * | |
390 | * @np: The device node describing the protocol | |
391 | * @info: The SCMI instance descriptor | |
392 | * @prot_id: The protocol ID | |
393 | * @name: The optional name of the device to be created: if not provided this | |
394 | * call will lead to the creation of all the devices currently requested | |
395 | * for the specified protocol. | |
396 | */ | |
397 | static void scmi_create_protocol_devices(struct device_node *np, | |
398 | struct scmi_info *info, | |
399 | int prot_id, const char *name) | |
400 | { | |
401 | struct scmi_device *sdev; | |
402 | ||
403 | mutex_lock(&info->devreq_mtx); | |
404 | sdev = scmi_device_create(np, info->dev, prot_id, name); | |
405 | if (name && !sdev) | |
406 | dev_err(info->dev, | |
407 | "failed to create device for protocol 0x%X (%s)\n", | |
408 | prot_id, name); | |
409 | mutex_unlock(&info->devreq_mtx); | |
410 | } | |
411 | ||
412 | static void scmi_destroy_protocol_devices(struct scmi_info *info, | |
413 | int prot_id, const char *name) | |
414 | { | |
415 | mutex_lock(&info->devreq_mtx); | |
416 | scmi_device_destroy(info->dev, prot_id, name); | |
417 | mutex_unlock(&info->devreq_mtx); | |
418 | } | |
419 | ||
a02d7c93 CM |
420 | void scmi_notification_instance_data_set(const struct scmi_handle *handle, |
421 | void *priv) | |
422 | { | |
423 | struct scmi_info *info = handle_to_scmi_info(handle); | |
424 | ||
425 | info->notify_priv = priv; | |
426 | /* Ensure updated protocol private date are visible */ | |
427 | smp_wmb(); | |
428 | } | |
429 | ||
430 | void *scmi_notification_instance_data_get(const struct scmi_handle *handle) | |
431 | { | |
432 | struct scmi_info *info = handle_to_scmi_info(handle); | |
433 | ||
434 | /* Ensure protocols_private_data has been updated */ | |
435 | smp_rmb(); | |
436 | return info->notify_priv; | |
437 | } | |
438 | ||
9ca5a183 CM |
439 | /** |
440 | * scmi_xfer_token_set - Reserve and set new token for the xfer at hand | |
441 | * | |
442 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
443 | * @xfer: The xfer to act upon | |
444 | * | |
445 | * Pick the next unused monotonically increasing token and set it into | |
446 | * xfer->hdr.seq: picking a monotonically increasing value avoids immediate | |
447 | * reuse of freshly completed or timed-out xfers, thus mitigating the risk | |
448 | * of incorrect association of a late and expired xfer with a live in-flight | |
449 | * transaction, both happening to re-use the same token identifier. | |
450 | * | |
451 | * Since platform is NOT required to answer our request in-order we should | |
452 | * account for a few rare but possible scenarios: | |
453 | * | |
454 | * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token | |
455 | * using find_next_zero_bit() starting from candidate next_token bit | |
456 | * | |
457 | * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we | |
458 | * are plenty of free tokens at start, so try a second pass using | |
459 | * find_next_zero_bit() and starting from 0. | |
460 | * | |
461 | * X = used in-flight | |
462 | * | |
463 | * Normal | |
464 | * ------ | |
465 | * | |
466 | * |- xfer_id picked | |
467 | * -----------+---------------------------------------------------------- | |
468 | * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X| | |
469 | * ---------------------------------------------------------------------- | |
470 | * ^ | |
471 | * |- next_token | |
472 | * | |
473 | * Out-of-order pending at start | |
474 | * ----------------------------- | |
475 | * | |
476 | * |- xfer_id picked, last_token fixed | |
477 | * -----+---------------------------------------------------------------- | |
478 | * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| | | |
479 | * ---------------------------------------------------------------------- | |
480 | * ^ | |
481 | * |- next_token | |
482 | * | |
483 | * | |
484 | * Out-of-order pending at end | |
485 | * --------------------------- | |
486 | * | |
487 | * |- xfer_id picked, last_token fixed | |
488 | * -----+---------------------------------------------------------------- | |
489 | * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X| | |
490 | * ---------------------------------------------------------------------- | |
491 | * ^ | |
492 | * |- next_token | |
493 | * | |
494 | * Context: Assumes to be called with @xfer_lock already acquired. | |
495 | * | |
496 | * Return: 0 on Success or error | |
497 | */ | |
498 | static int scmi_xfer_token_set(struct scmi_xfers_info *minfo, | |
499 | struct scmi_xfer *xfer) | |
500 | { | |
501 | unsigned long xfer_id, next_token; | |
502 | ||
503 | /* | |
504 | * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1] | |
505 | * using the pre-allocated transfer_id as a base. | |
506 | * Note that the global transfer_id is shared across all message types | |
507 | * so there could be holes in the allocated set of monotonic sequence | |
508 | * numbers, but that is going to limit the effectiveness of the | |
509 | * mitigation only in very rare limit conditions. | |
510 | */ | |
511 | next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1)); | |
512 | ||
513 | /* Pick the next available xfer_id >= next_token */ | |
514 | xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, | |
515 | MSG_TOKEN_MAX, next_token); | |
516 | if (xfer_id == MSG_TOKEN_MAX) { | |
517 | /* | |
518 | * After heavily out-of-order responses, there are no free | |
519 | * tokens ahead, but only at start of xfer_alloc_table so | |
520 | * try again from the beginning. | |
521 | */ | |
522 | xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, | |
523 | MSG_TOKEN_MAX, 0); | |
524 | /* | |
525 | * Something is wrong if we got here since there can be a | |
526 | * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages | |
527 | * but we have not found any free token [0, MSG_TOKEN_MAX - 1]. | |
528 | */ | |
529 | if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX)) | |
530 | return -ENOMEM; | |
531 | } | |
532 | ||
533 | /* Update +/- last_token accordingly if we skipped some hole */ | |
534 | if (xfer_id != next_token) | |
535 | atomic_add((int)(xfer_id - next_token), &transfer_last_id); | |
536 | ||
9ca5a183 CM |
537 | xfer->hdr.seq = (u16)xfer_id; |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | /** | |
543 | * scmi_xfer_token_clear - Release the token | |
544 | * | |
545 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
546 | * @xfer: The xfer to act upon | |
547 | */ | |
548 | static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo, | |
549 | struct scmi_xfer *xfer) | |
550 | { | |
551 | clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table); | |
552 | } | |
553 | ||
b0e924a9 CM |
554 | /** |
555 | * scmi_xfer_inflight_register_unlocked - Register the xfer as in-flight | |
556 | * | |
557 | * @xfer: The xfer to register | |
558 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
559 | * | |
560 | * Note that this helper assumes that the xfer to be registered as in-flight | |
561 | * had been built using an xfer sequence number which still corresponds to a | |
562 | * free slot in the xfer_alloc_table. | |
563 | * | |
564 | * Context: Assumes to be called with @xfer_lock already acquired. | |
565 | */ | |
566 | static inline void | |
567 | scmi_xfer_inflight_register_unlocked(struct scmi_xfer *xfer, | |
568 | struct scmi_xfers_info *minfo) | |
569 | { | |
570 | /* Set in-flight */ | |
571 | set_bit(xfer->hdr.seq, minfo->xfer_alloc_table); | |
572 | hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq); | |
573 | xfer->pending = true; | |
574 | } | |
575 | ||
3095a3e2 CM |
576 | /** |
577 | * scmi_xfer_inflight_register - Try to register an xfer as in-flight | |
578 | * | |
579 | * @xfer: The xfer to register | |
580 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
581 | * | |
582 | * Note that this helper does NOT assume anything about the sequence number | |
583 | * that was baked into the provided xfer, so it checks at first if it can | |
584 | * be mapped to a free slot and fails with an error if another xfer with the | |
585 | * same sequence number is currently still registered as in-flight. | |
586 | * | |
587 | * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer | |
588 | * could not rbe mapped to a free slot in the xfer_alloc_table. | |
589 | */ | |
590 | static int scmi_xfer_inflight_register(struct scmi_xfer *xfer, | |
591 | struct scmi_xfers_info *minfo) | |
592 | { | |
593 | int ret = 0; | |
594 | unsigned long flags; | |
595 | ||
596 | spin_lock_irqsave(&minfo->xfer_lock, flags); | |
597 | if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table)) | |
598 | scmi_xfer_inflight_register_unlocked(xfer, minfo); | |
599 | else | |
600 | ret = -EBUSY; | |
601 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
602 | ||
603 | return ret; | |
604 | } | |
605 | ||
606 | /** | |
607 | * scmi_xfer_raw_inflight_register - An helper to register the given xfer as in | |
608 | * flight on the TX channel, if possible. | |
609 | * | |
610 | * @handle: Pointer to SCMI entity handle | |
611 | * @xfer: The xfer to register | |
612 | * | |
613 | * Return: 0 on Success, error otherwise | |
614 | */ | |
615 | int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle, | |
616 | struct scmi_xfer *xfer) | |
617 | { | |
618 | struct scmi_info *info = handle_to_scmi_info(handle); | |
619 | ||
620 | return scmi_xfer_inflight_register(xfer, &info->tx_minfo); | |
621 | } | |
622 | ||
b0e924a9 CM |
623 | /** |
624 | * scmi_xfer_pending_set - Pick a proper sequence number and mark the xfer | |
625 | * as pending in-flight | |
626 | * | |
627 | * @xfer: The xfer to act upon | |
628 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
629 | * | |
630 | * Return: 0 on Success or error otherwise | |
631 | */ | |
632 | static inline int scmi_xfer_pending_set(struct scmi_xfer *xfer, | |
633 | struct scmi_xfers_info *minfo) | |
634 | { | |
635 | int ret; | |
636 | unsigned long flags; | |
637 | ||
638 | spin_lock_irqsave(&minfo->xfer_lock, flags); | |
639 | /* Set a new monotonic token as the xfer sequence number */ | |
640 | ret = scmi_xfer_token_set(minfo, xfer); | |
641 | if (!ret) | |
642 | scmi_xfer_inflight_register_unlocked(xfer, minfo); | |
643 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
644 | ||
645 | return ret; | |
646 | } | |
647 | ||
aa4f886f | 648 | /** |
1baf47c2 | 649 | * scmi_xfer_get() - Allocate one message |
aa4f886f | 650 | * |
1baf47c2 | 651 | * @handle: Pointer to SCMI entity handle |
38c927fb | 652 | * @minfo: Pointer to Tx/Rx Message management info based on channel type |
aa4f886f | 653 | * |
5b65af8f | 654 | * Helper function which is used by various message functions that are |
aa4f886f SH |
655 | * exposed to clients of this driver for allocating a message traffic event. |
656 | * | |
b0e924a9 CM |
657 | * Picks an xfer from the free list @free_xfers (if any available) and perform |
658 | * a basic initialization. | |
659 | * | |
660 | * Note that, at this point, still no sequence number is assigned to the | |
661 | * allocated xfer, nor it is registered as a pending transaction. | |
9ca5a183 CM |
662 | * |
663 | * The successfully initialized xfer is refcounted. | |
664 | * | |
b0e924a9 | 665 | * Context: Holds @xfer_lock while manipulating @free_xfers. |
aa4f886f | 666 | * |
b0e924a9 | 667 | * Return: An initialized xfer if all went fine, else pointer error. |
aa4f886f | 668 | */ |
38c927fb | 669 | static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle, |
b0e924a9 | 670 | struct scmi_xfers_info *minfo) |
aa4f886f | 671 | { |
9ca5a183 | 672 | unsigned long flags; |
aa4f886f | 673 | struct scmi_xfer *xfer; |
aa4f886f | 674 | |
aa4f886f | 675 | spin_lock_irqsave(&minfo->xfer_lock, flags); |
9ca5a183 | 676 | if (hlist_empty(&minfo->free_xfers)) { |
aa4f886f SH |
677 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); |
678 | return ERR_PTR(-ENOMEM); | |
679 | } | |
aa4f886f | 680 | |
9ca5a183 CM |
681 | /* grab an xfer from the free_list */ |
682 | xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node); | |
683 | hlist_del_init(&xfer->node); | |
aa4f886f | 684 | |
9ca5a183 CM |
685 | /* |
686 | * Allocate transfer_id early so that can be used also as base for | |
687 | * monotonic sequence number generation if needed. | |
688 | */ | |
729d3530 | 689 | xfer->transfer_id = atomic_inc_return(&transfer_last_id); |
aa4f886f | 690 | |
b0e924a9 CM |
691 | refcount_set(&xfer->users, 1); |
692 | atomic_set(&xfer->busy, SCMI_XFER_FREE); | |
9ca5a183 CM |
693 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); |
694 | ||
aa4f886f SH |
695 | return xfer; |
696 | } | |
697 | ||
3095a3e2 CM |
698 | /** |
699 | * scmi_xfer_raw_get - Helper to get a bare free xfer from the TX channel | |
700 | * | |
701 | * @handle: Pointer to SCMI entity handle | |
702 | * | |
703 | * Note that xfer is taken from the TX channel structures. | |
704 | * | |
705 | * Return: A valid xfer on Success, or an error-pointer otherwise | |
706 | */ | |
707 | struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle) | |
708 | { | |
709 | struct scmi_xfer *xfer; | |
710 | struct scmi_info *info = handle_to_scmi_info(handle); | |
711 | ||
712 | xfer = scmi_xfer_get(handle, &info->tx_minfo); | |
713 | if (!IS_ERR(xfer)) | |
714 | xfer->flags |= SCMI_XFER_FLAG_IS_RAW; | |
715 | ||
716 | return xfer; | |
717 | } | |
718 | ||
719 | /** | |
720 | * scmi_xfer_raw_channel_get - Helper to get a reference to the proper channel | |
721 | * to use for a specific protocol_id Raw transaction. | |
722 | * | |
723 | * @handle: Pointer to SCMI entity handle | |
724 | * @protocol_id: Identifier of the protocol | |
725 | * | |
726 | * Note that in a regular SCMI stack, usually, a protocol has to be defined in | |
727 | * the DT to have an associated channel and be usable; but in Raw mode any | |
728 | * protocol in range is allowed, re-using the Base channel, so as to enable | |
729 | * fuzzing on any protocol without the need of a fully compiled DT. | |
730 | * | |
731 | * Return: A reference to the channel to use, or an ERR_PTR | |
732 | */ | |
733 | struct scmi_chan_info * | |
734 | scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id) | |
735 | { | |
736 | struct scmi_chan_info *cinfo; | |
737 | struct scmi_info *info = handle_to_scmi_info(handle); | |
738 | ||
739 | cinfo = idr_find(&info->tx_idr, protocol_id); | |
740 | if (!cinfo) { | |
741 | if (protocol_id == SCMI_PROTOCOL_BASE) | |
742 | return ERR_PTR(-EINVAL); | |
743 | /* Use Base channel for protocols not defined for DT */ | |
744 | cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE); | |
745 | if (!cinfo) | |
746 | return ERR_PTR(-EINVAL); | |
747 | dev_warn_once(handle->dev, | |
748 | "Using Base channel for protocol 0x%X\n", | |
749 | protocol_id); | |
750 | } | |
751 | ||
752 | return cinfo; | |
753 | } | |
754 | ||
aa4f886f | 755 | /** |
38c927fb | 756 | * __scmi_xfer_put() - Release a message |
aa4f886f | 757 | * |
38c927fb | 758 | * @minfo: Pointer to Tx/Rx Message management info based on channel type |
1baf47c2 | 759 | * @xfer: message that was reserved by scmi_xfer_get |
aa4f886f | 760 | * |
9ca5a183 CM |
761 | * After refcount check, possibly release an xfer, clearing the token slot, |
762 | * removing xfer from @pending_xfers and putting it back into free_xfers. | |
763 | * | |
aa4f886f SH |
764 | * This holds a spinlock to maintain integrity of internal data structures. |
765 | */ | |
38c927fb SH |
766 | static void |
767 | __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer) | |
aa4f886f SH |
768 | { |
769 | unsigned long flags; | |
aa4f886f | 770 | |
aa4f886f | 771 | spin_lock_irqsave(&minfo->xfer_lock, flags); |
ed7c04c1 CM |
772 | if (refcount_dec_and_test(&xfer->users)) { |
773 | if (xfer->pending) { | |
774 | scmi_xfer_token_clear(minfo, xfer); | |
775 | hash_del(&xfer->node); | |
776 | xfer->pending = false; | |
777 | } | |
778 | hlist_add_head(&xfer->node, &minfo->free_xfers); | |
9ca5a183 | 779 | } |
aa4f886f SH |
780 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); |
781 | } | |
782 | ||
3095a3e2 CM |
783 | /** |
784 | * scmi_xfer_raw_put - Release an xfer that was taken by @scmi_xfer_raw_get | |
785 | * | |
786 | * @handle: Pointer to SCMI entity handle | |
787 | * @xfer: A reference to the xfer to put | |
788 | * | |
789 | * Note that as with other xfer_put() handlers the xfer is really effectively | |
790 | * released only if there are no more users on the system. | |
791 | */ | |
792 | void scmi_xfer_raw_put(const struct scmi_handle *handle, struct scmi_xfer *xfer) | |
793 | { | |
794 | struct scmi_info *info = handle_to_scmi_info(handle); | |
795 | ||
796 | xfer->flags &= ~SCMI_XFER_FLAG_IS_RAW; | |
7860701d | 797 | xfer->flags &= ~SCMI_XFER_FLAG_CHAN_SET; |
3095a3e2 CM |
798 | return __scmi_xfer_put(&info->tx_minfo, xfer); |
799 | } | |
800 | ||
9ca5a183 CM |
801 | /** |
802 | * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id | |
803 | * | |
804 | * @minfo: Pointer to Tx/Rx Message management info based on channel type | |
805 | * @xfer_id: Token ID to lookup in @pending_xfers | |
806 | * | |
807 | * Refcounting is untouched. | |
808 | * | |
809 | * Context: Assumes to be called with @xfer_lock already acquired. | |
810 | * | |
811 | * Return: A valid xfer on Success or error otherwise | |
812 | */ | |
813 | static struct scmi_xfer * | |
814 | scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id) | |
815 | { | |
816 | struct scmi_xfer *xfer = NULL; | |
817 | ||
818 | if (test_bit(xfer_id, minfo->xfer_alloc_table)) | |
819 | xfer = XFER_FIND(minfo->pending_xfers, xfer_id); | |
820 | ||
821 | return xfer ?: ERR_PTR(-EINVAL); | |
822 | } | |
823 | ||
5dc0e0b1 CM |
824 | /** |
825 | * scmi_bad_message_trace - A helper to trace weird messages | |
826 | * | |
827 | * @cinfo: A reference to the channel descriptor on which the message was | |
828 | * received | |
829 | * @msg_hdr: Message header to track | |
830 | * @err: A specific error code used as a status value in traces. | |
831 | * | |
832 | * This helper can be used to trace any kind of weird, incomplete, unexpected, | |
833 | * timed-out message that arrives and as such, can be traced only referring to | |
834 | * the header content, since the payload is missing/unreliable. | |
835 | */ | |
836 | void scmi_bad_message_trace(struct scmi_chan_info *cinfo, u32 msg_hdr, | |
837 | enum scmi_bad_msg err) | |
838 | { | |
839 | char *tag; | |
840 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
841 | ||
842 | switch (MSG_XTRACT_TYPE(msg_hdr)) { | |
843 | case MSG_TYPE_COMMAND: | |
844 | tag = "!RESP"; | |
845 | break; | |
846 | case MSG_TYPE_DELAYED_RESP: | |
847 | tag = "!DLYD"; | |
848 | break; | |
849 | case MSG_TYPE_NOTIFICATION: | |
850 | tag = "!NOTI"; | |
851 | break; | |
852 | default: | |
853 | tag = "!UNKN"; | |
854 | break; | |
855 | } | |
856 | ||
857 | trace_scmi_msg_dump(info->id, cinfo->id, | |
858 | MSG_XTRACT_PROT_ID(msg_hdr), | |
859 | MSG_XTRACT_ID(msg_hdr), tag, | |
860 | MSG_XTRACT_TOKEN(msg_hdr), err, NULL, 0); | |
861 | } | |
862 | ||
ed7c04c1 CM |
863 | /** |
864 | * scmi_msg_response_validate - Validate message type against state of related | |
865 | * xfer | |
866 | * | |
867 | * @cinfo: A reference to the channel descriptor. | |
868 | * @msg_type: Message type to check | |
869 | * @xfer: A reference to the xfer to validate against @msg_type | |
870 | * | |
871 | * This function checks if @msg_type is congruent with the current state of | |
872 | * a pending @xfer; if an asynchronous delayed response is received before the | |
873 | * related synchronous response (Out-of-Order Delayed Response) the missing | |
874 | * synchronous response is assumed to be OK and completed, carrying on with the | |
875 | * Delayed Response: this is done to address the case in which the underlying | |
876 | * SCMI transport can deliver such out-of-order responses. | |
877 | * | |
878 | * Context: Assumes to be called with xfer->lock already acquired. | |
879 | * | |
880 | * Return: 0 on Success, error otherwise | |
881 | */ | |
882 | static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo, | |
883 | u8 msg_type, | |
884 | struct scmi_xfer *xfer) | |
885 | { | |
886 | /* | |
887 | * Even if a response was indeed expected on this slot at this point, | |
888 | * a buggy platform could wrongly reply feeding us an unexpected | |
889 | * delayed response we're not prepared to handle: bail-out safely | |
890 | * blaming firmware. | |
891 | */ | |
892 | if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) { | |
893 | dev_err(cinfo->dev, | |
894 | "Delayed Response for %d not expected! Buggy F/W ?\n", | |
895 | xfer->hdr.seq); | |
896 | return -EINVAL; | |
897 | } | |
898 | ||
899 | switch (xfer->state) { | |
900 | case SCMI_XFER_SENT_OK: | |
901 | if (msg_type == MSG_TYPE_DELAYED_RESP) { | |
902 | /* | |
903 | * Delayed Response expected but delivered earlier. | |
904 | * Assume message RESPONSE was OK and skip state. | |
905 | */ | |
906 | xfer->hdr.status = SCMI_SUCCESS; | |
907 | xfer->state = SCMI_XFER_RESP_OK; | |
908 | complete(&xfer->done); | |
909 | dev_warn(cinfo->dev, | |
910 | "Received valid OoO Delayed Response for %d\n", | |
911 | xfer->hdr.seq); | |
912 | } | |
913 | break; | |
914 | case SCMI_XFER_RESP_OK: | |
915 | if (msg_type != MSG_TYPE_DELAYED_RESP) | |
916 | return -EINVAL; | |
917 | break; | |
918 | case SCMI_XFER_DRESP_OK: | |
919 | /* No further message expected once in SCMI_XFER_DRESP_OK */ | |
920 | return -EINVAL; | |
921 | } | |
922 | ||
923 | return 0; | |
924 | } | |
925 | ||
926 | /** | |
927 | * scmi_xfer_state_update - Update xfer state | |
928 | * | |
929 | * @xfer: A reference to the xfer to update | |
930 | * @msg_type: Type of message being processed. | |
931 | * | |
932 | * Note that this message is assumed to have been already successfully validated | |
933 | * by @scmi_msg_response_validate(), so here we just update the state. | |
934 | * | |
935 | * Context: Assumes to be called on an xfer exclusively acquired using the | |
936 | * busy flag. | |
937 | */ | |
938 | static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type) | |
939 | { | |
940 | xfer->hdr.type = msg_type; | |
941 | ||
942 | /* Unknown command types were already discarded earlier */ | |
943 | if (xfer->hdr.type == MSG_TYPE_COMMAND) | |
944 | xfer->state = SCMI_XFER_RESP_OK; | |
945 | else | |
946 | xfer->state = SCMI_XFER_DRESP_OK; | |
947 | } | |
948 | ||
949 | static bool scmi_xfer_acquired(struct scmi_xfer *xfer) | |
950 | { | |
951 | int ret; | |
952 | ||
953 | ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY); | |
954 | ||
955 | return ret == SCMI_XFER_FREE; | |
956 | } | |
957 | ||
958 | /** | |
959 | * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer | |
960 | * | |
961 | * @cinfo: A reference to the channel descriptor. | |
962 | * @msg_hdr: A message header to use as lookup key | |
963 | * | |
964 | * When a valid xfer is found for the sequence number embedded in the provided | |
965 | * msg_hdr, reference counting is properly updated and exclusive access to this | |
966 | * xfer is granted till released with @scmi_xfer_command_release. | |
967 | * | |
968 | * Return: A valid @xfer on Success or error otherwise. | |
969 | */ | |
970 | static inline struct scmi_xfer * | |
971 | scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr) | |
972 | { | |
973 | int ret; | |
974 | unsigned long flags; | |
975 | struct scmi_xfer *xfer; | |
976 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
977 | struct scmi_xfers_info *minfo = &info->tx_minfo; | |
978 | u8 msg_type = MSG_XTRACT_TYPE(msg_hdr); | |
979 | u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr); | |
980 | ||
981 | /* Are we even expecting this? */ | |
982 | spin_lock_irqsave(&minfo->xfer_lock, flags); | |
983 | xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id); | |
984 | if (IS_ERR(xfer)) { | |
985 | dev_err(cinfo->dev, | |
986 | "Message for %d type %d is not expected!\n", | |
987 | xfer_id, msg_type); | |
988 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
5076ab66 CM |
989 | |
990 | scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNEXPECTED); | |
991 | ||
ed7c04c1 CM |
992 | return xfer; |
993 | } | |
994 | refcount_inc(&xfer->users); | |
995 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
996 | ||
997 | spin_lock_irqsave(&xfer->lock, flags); | |
998 | ret = scmi_msg_response_validate(cinfo, msg_type, xfer); | |
999 | /* | |
1000 | * If a pending xfer was found which was also in a congruent state with | |
1001 | * the received message, acquire exclusive access to it setting the busy | |
1002 | * flag. | |
1003 | * Spins only on the rare limit condition of concurrent reception of | |
1004 | * RESP and DRESP for the same xfer. | |
1005 | */ | |
1006 | if (!ret) { | |
1007 | spin_until_cond(scmi_xfer_acquired(xfer)); | |
1008 | scmi_xfer_state_update(xfer, msg_type); | |
1009 | } | |
1010 | spin_unlock_irqrestore(&xfer->lock, flags); | |
1011 | ||
1012 | if (ret) { | |
1013 | dev_err(cinfo->dev, | |
1014 | "Invalid message type:%d for %d - HDR:0x%X state:%d\n", | |
1015 | msg_type, xfer_id, msg_hdr, xfer->state); | |
5076ab66 CM |
1016 | |
1017 | scmi_bad_message_trace(cinfo, msg_hdr, MSG_INVALID); | |
1018 | ||
ed7c04c1 CM |
1019 | /* On error the refcount incremented above has to be dropped */ |
1020 | __scmi_xfer_put(minfo, xfer); | |
1021 | xfer = ERR_PTR(-EINVAL); | |
1022 | } | |
1023 | ||
1024 | return xfer; | |
1025 | } | |
1026 | ||
1027 | static inline void scmi_xfer_command_release(struct scmi_info *info, | |
1028 | struct scmi_xfer *xfer) | |
1029 | { | |
1030 | atomic_set(&xfer->busy, SCMI_XFER_FREE); | |
1031 | __scmi_xfer_put(&info->tx_minfo, xfer); | |
1032 | } | |
1033 | ||
e9b21c96 CM |
1034 | static inline void scmi_clear_channel(struct scmi_info *info, |
1035 | struct scmi_chan_info *cinfo) | |
1036 | { | |
1037 | if (info->desc->ops->clear_channel) | |
1038 | info->desc->ops->clear_channel(cinfo); | |
1039 | } | |
1040 | ||
13fba878 CM |
1041 | static void scmi_handle_notification(struct scmi_chan_info *cinfo, |
1042 | u32 msg_hdr, void *priv) | |
2747a967 | 1043 | { |
5c8a47a5 | 1044 | struct scmi_xfer *xfer; |
4d09852b SH |
1045 | struct device *dev = cinfo->dev; |
1046 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
1047 | struct scmi_xfers_info *minfo = &info->rx_minfo; | |
72a5eb9d | 1048 | ktime_t ts; |
4d09852b | 1049 | |
72a5eb9d | 1050 | ts = ktime_get_boottime(); |
b0e924a9 | 1051 | xfer = scmi_xfer_get(cinfo->handle, minfo); |
4d09852b SH |
1052 | if (IS_ERR(xfer)) { |
1053 | dev_err(dev, "failed to get free message slot (%ld)\n", | |
1054 | PTR_ERR(xfer)); | |
5076ab66 CM |
1055 | |
1056 | scmi_bad_message_trace(cinfo, msg_hdr, MSG_NOMEM); | |
1057 | ||
e9b21c96 | 1058 | scmi_clear_channel(info, cinfo); |
4d09852b SH |
1059 | return; |
1060 | } | |
1061 | ||
1062 | unpack_scmi_header(msg_hdr, &xfer->hdr); | |
13fba878 | 1063 | if (priv) |
5a3b7185 CM |
1064 | /* Ensure order between xfer->priv store and following ops */ |
1065 | smp_store_mb(xfer->priv, priv); | |
4d09852b SH |
1066 | info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size, |
1067 | xfer); | |
b60e0886 | 1068 | |
8b2bd711 CM |
1069 | trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, |
1070 | xfer->hdr.id, "NOTI", xfer->hdr.seq, | |
1071 | xfer->hdr.status, xfer->rx.buf, xfer->rx.len); | |
b60e0886 | 1072 | |
6b8a6913 CM |
1073 | scmi_notify(cinfo->handle, xfer->hdr.protocol_id, |
1074 | xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts); | |
4d09852b SH |
1075 | |
1076 | trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, | |
1077 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
1078 | MSG_TYPE_NOTIFICATION); | |
58ecdf03 | 1079 | |
7063887b CM |
1080 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) { |
1081 | xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr); | |
7860701d CM |
1082 | scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE, |
1083 | cinfo->id); | |
7063887b CM |
1084 | } |
1085 | ||
4d09852b SH |
1086 | __scmi_xfer_put(minfo, xfer); |
1087 | ||
e9b21c96 | 1088 | scmi_clear_channel(info, cinfo); |
4d09852b SH |
1089 | } |
1090 | ||
13fba878 CM |
1091 | static void scmi_handle_response(struct scmi_chan_info *cinfo, |
1092 | u32 msg_hdr, void *priv) | |
4d09852b SH |
1093 | { |
1094 | struct scmi_xfer *xfer; | |
4d09852b | 1095 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); |
2747a967 | 1096 | |
ed7c04c1 | 1097 | xfer = scmi_xfer_command_acquire(cinfo, msg_hdr); |
9ca5a183 | 1098 | if (IS_ERR(xfer)) { |
7063887b CM |
1099 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) |
1100 | scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv); | |
1101 | ||
98f0d68f CM |
1102 | if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP) |
1103 | scmi_clear_channel(info, cinfo); | |
c5bceb98 CM |
1104 | return; |
1105 | } | |
2747a967 | 1106 | |
0cb7af47 | 1107 | /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */ |
ed7c04c1 | 1108 | if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) |
0cb7af47 CM |
1109 | xfer->rx.len = info->desc->max_msg_size; |
1110 | ||
13fba878 | 1111 | if (priv) |
5a3b7185 CM |
1112 | /* Ensure order between xfer->priv store and following ops */ |
1113 | smp_store_mb(xfer->priv, priv); | |
5c8a47a5 | 1114 | info->desc->ops->fetch_response(cinfo, xfer); |
58ecdf03 | 1115 | |
8b2bd711 CM |
1116 | trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, |
1117 | xfer->hdr.id, | |
b60e0886 | 1118 | xfer->hdr.type == MSG_TYPE_DELAYED_RESP ? |
3c3d818a CM |
1119 | (!SCMI_XFER_IS_RAW(xfer) ? "DLYD" : "dlyd") : |
1120 | (!SCMI_XFER_IS_RAW(xfer) ? "RESP" : "resp"), | |
b60e0886 CM |
1121 | xfer->hdr.seq, xfer->hdr.status, |
1122 | xfer->rx.buf, xfer->rx.len); | |
1123 | ||
729d3530 LL |
1124 | trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, |
1125 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
ed7c04c1 | 1126 | xfer->hdr.type); |
729d3530 | 1127 | |
ed7c04c1 | 1128 | if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) { |
e9b21c96 | 1129 | scmi_clear_channel(info, cinfo); |
58ecdf03 | 1130 | complete(xfer->async_done); |
d04fb2b2 | 1131 | } else { |
58ecdf03 | 1132 | complete(&xfer->done); |
d04fb2b2 | 1133 | } |
ed7c04c1 | 1134 | |
7063887b CM |
1135 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) { |
1136 | /* | |
1137 | * When in polling mode avoid to queue the Raw xfer on the IRQ | |
1138 | * RX path since it will be already queued at the end of the TX | |
1139 | * poll loop. | |
1140 | */ | |
1141 | if (!xfer->hdr.poll_completion) | |
7860701d CM |
1142 | scmi_raw_message_report(info->raw, xfer, |
1143 | SCMI_RAW_REPLY_QUEUE, | |
1144 | cinfo->id); | |
7063887b CM |
1145 | } |
1146 | ||
ed7c04c1 | 1147 | scmi_xfer_command_release(info, xfer); |
2747a967 SH |
1148 | } |
1149 | ||
4d09852b SH |
1150 | /** |
1151 | * scmi_rx_callback() - callback for receiving messages | |
1152 | * | |
1153 | * @cinfo: SCMI channel info | |
1154 | * @msg_hdr: Message header | |
13fba878 | 1155 | * @priv: Transport specific private data. |
4d09852b SH |
1156 | * |
1157 | * Processes one received message to appropriate transfer information and | |
1158 | * signals completion of the transfer. | |
1159 | * | |
1160 | * NOTE: This function will be invoked in IRQ context, hence should be | |
1161 | * as optimal as possible. | |
1162 | */ | |
13fba878 | 1163 | void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv) |
4d09852b | 1164 | { |
4d09852b SH |
1165 | u8 msg_type = MSG_XTRACT_TYPE(msg_hdr); |
1166 | ||
1167 | switch (msg_type) { | |
1168 | case MSG_TYPE_NOTIFICATION: | |
13fba878 | 1169 | scmi_handle_notification(cinfo, msg_hdr, priv); |
4d09852b SH |
1170 | break; |
1171 | case MSG_TYPE_COMMAND: | |
1172 | case MSG_TYPE_DELAYED_RESP: | |
13fba878 | 1173 | scmi_handle_response(cinfo, msg_hdr, priv); |
4d09852b SH |
1174 | break; |
1175 | default: | |
1176 | WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type); | |
5076ab66 | 1177 | scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNKNOWN); |
4d09852b SH |
1178 | break; |
1179 | } | |
1180 | } | |
1181 | ||
38c927fb | 1182 | /** |
a4a20b09 | 1183 | * xfer_put() - Release a transmit message |
38c927fb | 1184 | * |
a4a20b09 | 1185 | * @ph: Pointer to SCMI protocol handle |
ed7c04c1 | 1186 | * @xfer: message that was reserved by xfer_get_init |
38c927fb | 1187 | */ |
a4a20b09 CM |
1188 | static void xfer_put(const struct scmi_protocol_handle *ph, |
1189 | struct scmi_xfer *xfer) | |
38c927fb | 1190 | { |
a4a20b09 CM |
1191 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
1192 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
38c927fb SH |
1193 | |
1194 | __scmi_xfer_put(&info->tx_minfo, xfer); | |
1195 | } | |
1196 | ||
5c8a47a5 | 1197 | static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo, |
d4c3751a SH |
1198 | struct scmi_xfer *xfer, ktime_t stop) |
1199 | { | |
5c8a47a5 | 1200 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); |
d4c3751a | 1201 | |
ed7c04c1 CM |
1202 | /* |
1203 | * Poll also on xfer->done so that polling can be forcibly terminated | |
1204 | * in case of out-of-order receptions of delayed responses | |
1205 | */ | |
5c8a47a5 | 1206 | return info->desc->ops->poll_done(cinfo, xfer) || |
ed7c04c1 | 1207 | try_wait_for_completion(&xfer->done) || |
5c8a47a5 | 1208 | ktime_after(ktime_get(), stop); |
d4c3751a SH |
1209 | } |
1210 | ||
07cdfc44 CM |
1211 | static int scmi_wait_for_reply(struct device *dev, const struct scmi_desc *desc, |
1212 | struct scmi_chan_info *cinfo, | |
1213 | struct scmi_xfer *xfer, unsigned int timeout_ms) | |
5a731aeb | 1214 | { |
07cdfc44 | 1215 | int ret = 0; |
f872af09 | 1216 | |
5a731aeb | 1217 | if (xfer->hdr.poll_completion) { |
31d2f803 CM |
1218 | /* |
1219 | * Real polling is needed only if transport has NOT declared | |
1220 | * itself to support synchronous commands replies. | |
1221 | */ | |
07cdfc44 | 1222 | if (!desc->sync_cmds_completed_on_ret) { |
31d2f803 CM |
1223 | /* |
1224 | * Poll on xfer using transport provided .poll_done(); | |
1225 | * assumes no completion interrupt was available. | |
1226 | */ | |
1227 | ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms); | |
1228 | ||
1229 | spin_until_cond(scmi_xfer_done_no_timeout(cinfo, | |
1230 | xfer, stop)); | |
1231 | if (ktime_after(ktime_get(), stop)) { | |
1232 | dev_err(dev, | |
1233 | "timed out in resp(caller: %pS) - polling\n", | |
1234 | (void *)_RET_IP_); | |
1235 | ret = -ETIMEDOUT; | |
1236 | } | |
1237 | } | |
5a731aeb | 1238 | |
31d2f803 | 1239 | if (!ret) { |
5a731aeb | 1240 | unsigned long flags; |
8b2bd711 CM |
1241 | struct scmi_info *info = |
1242 | handle_to_scmi_info(cinfo->handle); | |
5a731aeb CM |
1243 | |
1244 | /* | |
1245 | * Do not fetch_response if an out-of-order delayed | |
1246 | * response is being processed. | |
1247 | */ | |
1248 | spin_lock_irqsave(&xfer->lock, flags); | |
1249 | if (xfer->state == SCMI_XFER_SENT_OK) { | |
07cdfc44 | 1250 | desc->ops->fetch_response(cinfo, xfer); |
5a731aeb CM |
1251 | xfer->state = SCMI_XFER_RESP_OK; |
1252 | } | |
1253 | spin_unlock_irqrestore(&xfer->lock, flags); | |
b60e0886 CM |
1254 | |
1255 | /* Trace polled replies. */ | |
8b2bd711 CM |
1256 | trace_scmi_msg_dump(info->id, cinfo->id, |
1257 | xfer->hdr.protocol_id, xfer->hdr.id, | |
3c3d818a CM |
1258 | !SCMI_XFER_IS_RAW(xfer) ? |
1259 | "RESP" : "resp", | |
b60e0886 CM |
1260 | xfer->hdr.seq, xfer->hdr.status, |
1261 | xfer->rx.buf, xfer->rx.len); | |
7063887b CM |
1262 | |
1263 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) { | |
1264 | struct scmi_info *info = | |
1265 | handle_to_scmi_info(cinfo->handle); | |
1266 | ||
1267 | scmi_raw_message_report(info->raw, xfer, | |
7860701d CM |
1268 | SCMI_RAW_REPLY_QUEUE, |
1269 | cinfo->id); | |
7063887b | 1270 | } |
5a731aeb CM |
1271 | } |
1272 | } else { | |
1273 | /* And we wait for the response. */ | |
1274 | if (!wait_for_completion_timeout(&xfer->done, | |
1275 | msecs_to_jiffies(timeout_ms))) { | |
1276 | dev_err(dev, "timed out in resp(caller: %pS)\n", | |
1277 | (void *)_RET_IP_); | |
1278 | ret = -ETIMEDOUT; | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | return ret; | |
1283 | } | |
1284 | ||
07cdfc44 CM |
1285 | /** |
1286 | * scmi_wait_for_message_response - An helper to group all the possible ways of | |
1287 | * waiting for a synchronous message response. | |
1288 | * | |
1289 | * @cinfo: SCMI channel info | |
1290 | * @xfer: Reference to the transfer being waited for. | |
1291 | * | |
1292 | * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on | |
1293 | * configuration flags like xfer->hdr.poll_completion. | |
1294 | * | |
1295 | * Return: 0 on Success, error otherwise. | |
1296 | */ | |
1297 | static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo, | |
1298 | struct scmi_xfer *xfer) | |
1299 | { | |
1300 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
1301 | struct device *dev = info->dev; | |
1302 | ||
1303 | trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id, | |
1304 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
1305 | info->desc->max_rx_timeout_ms, | |
1306 | xfer->hdr.poll_completion); | |
1307 | ||
1308 | return scmi_wait_for_reply(dev, info->desc, cinfo, xfer, | |
1309 | info->desc->max_rx_timeout_ms); | |
1310 | } | |
1311 | ||
3095a3e2 CM |
1312 | /** |
1313 | * scmi_xfer_raw_wait_for_message_response - An helper to wait for a message | |
1314 | * reply to an xfer raw request on a specific channel for the required timeout. | |
1315 | * | |
1316 | * @cinfo: SCMI channel info | |
1317 | * @xfer: Reference to the transfer being waited for. | |
1318 | * @timeout_ms: The maximum timeout in milliseconds | |
1319 | * | |
1320 | * Return: 0 on Success, error otherwise. | |
1321 | */ | |
1322 | int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo, | |
1323 | struct scmi_xfer *xfer, | |
1324 | unsigned int timeout_ms) | |
1325 | { | |
1326 | int ret; | |
1327 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); | |
1328 | struct device *dev = info->dev; | |
1329 | ||
1330 | ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms); | |
1331 | if (ret) | |
1332 | dev_dbg(dev, "timed out in RAW response - HDR:%08X\n", | |
1333 | pack_scmi_header(&xfer->hdr)); | |
1334 | ||
1335 | return ret; | |
1336 | } | |
1337 | ||
aa4f886f | 1338 | /** |
a4a20b09 | 1339 | * do_xfer() - Do one transfer |
aa4f886f | 1340 | * |
a4a20b09 | 1341 | * @ph: Pointer to SCMI protocol handle |
aa4f886f SH |
1342 | * @xfer: Transfer to initiate and wait for response |
1343 | * | |
1344 | * Return: -ETIMEDOUT in case of no response, if transmit error, | |
1baf47c2 SH |
1345 | * return corresponding error, else if all goes well, |
1346 | * return 0. | |
aa4f886f | 1347 | */ |
a4a20b09 CM |
1348 | static int do_xfer(const struct scmi_protocol_handle *ph, |
1349 | struct scmi_xfer *xfer) | |
aa4f886f SH |
1350 | { |
1351 | int ret; | |
a4a20b09 CM |
1352 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
1353 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
aa4f886f | 1354 | struct device *dev = info->dev; |
907b6d14 SH |
1355 | struct scmi_chan_info *cinfo; |
1356 | ||
a690b7e6 | 1357 | /* Check for polling request on custom command xfers at first */ |
f21c2b0b CM |
1358 | if (xfer->hdr.poll_completion && |
1359 | !is_transport_polling_capable(info->desc)) { | |
2930abcf CM |
1360 | dev_warn_once(dev, |
1361 | "Polling mode is not supported by transport.\n"); | |
1362 | return -EINVAL; | |
1363 | } | |
1364 | ||
d211ddeb CM |
1365 | cinfo = idr_find(&info->tx_idr, pi->proto->id); |
1366 | if (unlikely(!cinfo)) | |
1367 | return -EINVAL; | |
1368 | ||
a690b7e6 | 1369 | /* True ONLY if also supported by transport. */ |
f21c2b0b | 1370 | if (is_polling_enabled(cinfo, info->desc)) |
a690b7e6 CM |
1371 | xfer->hdr.poll_completion = true; |
1372 | ||
a4a20b09 | 1373 | /* |
61832b35 | 1374 | * Initialise protocol id now from protocol handle to avoid it being |
a4a20b09 | 1375 | * overridden by mistake (or malice) by the protocol code mangling with |
61832b35 | 1376 | * the scmi_xfer structure prior to this. |
a4a20b09 CM |
1377 | */ |
1378 | xfer->hdr.protocol_id = pi->proto->id; | |
e30d91d4 | 1379 | reinit_completion(&xfer->done); |
a4a20b09 | 1380 | |
729d3530 LL |
1381 | trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id, |
1382 | xfer->hdr.protocol_id, xfer->hdr.seq, | |
1383 | xfer->hdr.poll_completion); | |
1384 | ||
f6ca5059 CM |
1385 | /* Clear any stale status */ |
1386 | xfer->hdr.status = SCMI_SUCCESS; | |
ed7c04c1 CM |
1387 | xfer->state = SCMI_XFER_SENT_OK; |
1388 | /* | |
1389 | * Even though spinlocking is not needed here since no race is possible | |
1390 | * on xfer->state due to the monotonically increasing tokens allocation, | |
1391 | * we must anyway ensure xfer->state initialization is not re-ordered | |
1392 | * after the .send_message() to be sure that on the RX path an early | |
1393 | * ISR calling scmi_rx_callback() cannot see an old stale xfer->state. | |
1394 | */ | |
1395 | smp_mb(); | |
1396 | ||
5c8a47a5 | 1397 | ret = info->desc->ops->send_message(cinfo, xfer); |
aa4f886f | 1398 | if (ret < 0) { |
5c8a47a5 | 1399 | dev_dbg(dev, "Failed to send message %d\n", ret); |
aa4f886f SH |
1400 | return ret; |
1401 | } | |
1402 | ||
8b2bd711 CM |
1403 | trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, |
1404 | xfer->hdr.id, "CMND", xfer->hdr.seq, | |
1405 | xfer->hdr.status, xfer->tx.buf, xfer->tx.len); | |
b60e0886 | 1406 | |
5a731aeb | 1407 | ret = scmi_wait_for_message_response(cinfo, xfer); |
d4c3751a SH |
1408 | if (!ret && xfer->hdr.status) |
1409 | ret = scmi_to_linux_errno(xfer->hdr.status); | |
1410 | ||
5c8a47a5 | 1411 | if (info->desc->ops->mark_txdone) |
94d0cd1d | 1412 | info->desc->ops->mark_txdone(cinfo, ret, xfer); |
aa4f886f | 1413 | |
729d3530 | 1414 | trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id, |
bad0d73b | 1415 | xfer->hdr.protocol_id, xfer->hdr.seq, ret); |
729d3530 | 1416 | |
aa4f886f SH |
1417 | return ret; |
1418 | } | |
1419 | ||
a4a20b09 CM |
1420 | static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph, |
1421 | struct scmi_xfer *xfer) | |
1422 | { | |
1423 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
1424 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
1425 | ||
1426 | xfer->rx.len = info->desc->max_msg_size; | |
1427 | } | |
1428 | ||
58ecdf03 | 1429 | /** |
a4a20b09 | 1430 | * do_xfer_with_response() - Do one transfer and wait until the delayed |
58ecdf03 SH |
1431 | * response is received |
1432 | * | |
a4a20b09 | 1433 | * @ph: Pointer to SCMI protocol handle |
58ecdf03 SH |
1434 | * @xfer: Transfer to initiate and wait for response |
1435 | * | |
69255e74 CM |
1436 | * Using asynchronous commands in atomic/polling mode should be avoided since |
1437 | * it could cause long busy-waiting here, so ignore polling for the delayed | |
1438 | * response and WARN if it was requested for this command transaction since | |
1439 | * upper layers should refrain from issuing such kind of requests. | |
1440 | * | |
1441 | * The only other option would have been to refrain from using any asynchronous | |
1442 | * command even if made available, when an atomic transport is detected, and | |
1443 | * instead forcibly use the synchronous version (thing that can be easily | |
1444 | * attained at the protocol layer), but this would also have led to longer | |
1445 | * stalls of the channel for synchronous commands and possibly timeouts. | |
1446 | * (in other words there is usually a good reason if a platform provides an | |
1447 | * asynchronous version of a command and we should prefer to use it...just not | |
1448 | * when using atomic/polling mode) | |
1449 | * | |
58ecdf03 SH |
1450 | * Return: -ETIMEDOUT in case of no delayed response, if transmit error, |
1451 | * return corresponding error, else if all goes well, return 0. | |
1452 | */ | |
a4a20b09 CM |
1453 | static int do_xfer_with_response(const struct scmi_protocol_handle *ph, |
1454 | struct scmi_xfer *xfer) | |
58ecdf03 SH |
1455 | { |
1456 | int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT); | |
1457 | DECLARE_COMPLETION_ONSTACK(async_response); | |
1458 | ||
1459 | xfer->async_done = &async_response; | |
1460 | ||
69255e74 CM |
1461 | /* |
1462 | * Delayed responses should not be polled, so an async command should | |
1463 | * not have been used when requiring an atomic/poll context; WARN and | |
1464 | * perform instead a sleeping wait. | |
1465 | * (Note Async + IgnoreDelayedResponses are sent via do_xfer) | |
1466 | */ | |
1467 | WARN_ON_ONCE(xfer->hdr.poll_completion); | |
1468 | ||
a4a20b09 | 1469 | ret = do_xfer(ph, xfer); |
f1748b1e | 1470 | if (!ret) { |
69255e74 CM |
1471 | if (!wait_for_completion_timeout(xfer->async_done, timeout)) { |
1472 | dev_err(ph->dev, | |
1473 | "timed out in delayed resp(caller: %pS)\n", | |
1474 | (void *)_RET_IP_); | |
f1748b1e | 1475 | ret = -ETIMEDOUT; |
69255e74 | 1476 | } else if (xfer->hdr.status) { |
f1748b1e | 1477 | ret = scmi_to_linux_errno(xfer->hdr.status); |
69255e74 | 1478 | } |
f1748b1e | 1479 | } |
58ecdf03 SH |
1480 | |
1481 | xfer->async_done = NULL; | |
1482 | return ret; | |
1483 | } | |
1484 | ||
aa4f886f | 1485 | /** |
a4a20b09 | 1486 | * xfer_get_init() - Allocate and initialise one message for transmit |
aa4f886f | 1487 | * |
a4a20b09 | 1488 | * @ph: Pointer to SCMI protocol handle |
aa4f886f | 1489 | * @msg_id: Message identifier |
aa4f886f SH |
1490 | * @tx_size: transmit message size |
1491 | * @rx_size: receive message size | |
1492 | * @p: pointer to the allocated and initialised message | |
1493 | * | |
14e297b3 | 1494 | * This function allocates the message using @scmi_xfer_get and |
aa4f886f SH |
1495 | * initialise the header. |
1496 | * | |
1497 | * Return: 0 if all went fine with @p pointing to message, else | |
1498 | * corresponding error. | |
1499 | */ | |
a4a20b09 CM |
1500 | static int xfer_get_init(const struct scmi_protocol_handle *ph, |
1501 | u8 msg_id, size_t tx_size, size_t rx_size, | |
1502 | struct scmi_xfer **p) | |
aa4f886f SH |
1503 | { |
1504 | int ret; | |
1505 | struct scmi_xfer *xfer; | |
a4a20b09 CM |
1506 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
1507 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
38c927fb | 1508 | struct scmi_xfers_info *minfo = &info->tx_minfo; |
aa4f886f SH |
1509 | struct device *dev = info->dev; |
1510 | ||
1511 | /* Ensure we have sane transfer sizes */ | |
1512 | if (rx_size > info->desc->max_msg_size || | |
1513 | tx_size > info->desc->max_msg_size) | |
1514 | return -ERANGE; | |
1515 | ||
b0e924a9 | 1516 | xfer = scmi_xfer_get(pi->handle, minfo); |
aa4f886f SH |
1517 | if (IS_ERR(xfer)) { |
1518 | ret = PTR_ERR(xfer); | |
1519 | dev_err(dev, "failed to get free message slot(%d)\n", ret); | |
1520 | return ret; | |
1521 | } | |
1522 | ||
b0e924a9 CM |
1523 | /* Pick a sequence number and register this xfer as in-flight */ |
1524 | ret = scmi_xfer_pending_set(xfer, minfo); | |
1525 | if (ret) { | |
1526 | dev_err(pi->handle->dev, | |
1527 | "Failed to get monotonic token %d\n", ret); | |
1528 | __scmi_xfer_put(minfo, xfer); | |
1529 | return ret; | |
1530 | } | |
1531 | ||
aa4f886f SH |
1532 | xfer->tx.len = tx_size; |
1533 | xfer->rx.len = rx_size ? : info->desc->max_msg_size; | |
63b282f1 | 1534 | xfer->hdr.type = MSG_TYPE_COMMAND; |
aa4f886f | 1535 | xfer->hdr.id = msg_id; |
aa4f886f SH |
1536 | xfer->hdr.poll_completion = false; |
1537 | ||
1538 | *p = xfer; | |
1baf47c2 | 1539 | |
aa4f886f SH |
1540 | return 0; |
1541 | } | |
1542 | ||
b6f20ff8 | 1543 | /** |
a4a20b09 | 1544 | * version_get() - command to get the revision of the SCMI entity |
b6f20ff8 | 1545 | * |
a4a20b09 | 1546 | * @ph: Pointer to SCMI protocol handle |
1baf47c2 | 1547 | * @version: Holds returned version of protocol. |
b6f20ff8 SH |
1548 | * |
1549 | * Updates the SCMI information in the internal data structure. | |
1550 | * | |
1551 | * Return: 0 if all went fine, else return appropriate error. | |
1552 | */ | |
a4a20b09 | 1553 | static int version_get(const struct scmi_protocol_handle *ph, u32 *version) |
b6f20ff8 SH |
1554 | { |
1555 | int ret; | |
1556 | __le32 *rev_info; | |
1557 | struct scmi_xfer *t; | |
1558 | ||
a4a20b09 | 1559 | ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t); |
b6f20ff8 SH |
1560 | if (ret) |
1561 | return ret; | |
1562 | ||
a4a20b09 | 1563 | ret = do_xfer(ph, t); |
b6f20ff8 SH |
1564 | if (!ret) { |
1565 | rev_info = t->rx.buf; | |
1566 | *version = le32_to_cpu(*rev_info); | |
1567 | } | |
1568 | ||
a4a20b09 | 1569 | xfer_put(ph, t); |
b6f20ff8 SH |
1570 | return ret; |
1571 | } | |
1572 | ||
d7b6cc56 CM |
1573 | /** |
1574 | * scmi_set_protocol_priv - Set protocol specific data at init time | |
1575 | * | |
1576 | * @ph: A reference to the protocol handle. | |
1577 | * @priv: The private data to set. | |
b5efc28a | 1578 | * @version: The detected protocol version for the core to register. |
d7b6cc56 CM |
1579 | * |
1580 | * Return: 0 on Success | |
1581 | */ | |
1582 | static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph, | |
b5efc28a | 1583 | void *priv, u32 version) |
d7b6cc56 CM |
1584 | { |
1585 | struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
1586 | ||
1587 | pi->priv = priv; | |
b5efc28a | 1588 | pi->version = version; |
d7b6cc56 CM |
1589 | |
1590 | return 0; | |
1591 | } | |
1592 | ||
1593 | /** | |
1594 | * scmi_get_protocol_priv - Set protocol specific data at init time | |
1595 | * | |
1596 | * @ph: A reference to the protocol handle. | |
1597 | * | |
1598 | * Return: Protocol private data if any was set. | |
1599 | */ | |
1600 | static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph) | |
1601 | { | |
1602 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
1603 | ||
1604 | return pi->priv; | |
1605 | } | |
1606 | ||
a4a20b09 CM |
1607 | static const struct scmi_xfer_ops xfer_ops = { |
1608 | .version_get = version_get, | |
1609 | .xfer_get_init = xfer_get_init, | |
1610 | .reset_rx_to_maxsz = reset_rx_to_maxsz, | |
1611 | .do_xfer = do_xfer, | |
1612 | .do_xfer_with_response = do_xfer_with_response, | |
1613 | .xfer_put = xfer_put, | |
1614 | }; | |
1615 | ||
5c873d12 CM |
1616 | struct scmi_msg_resp_domain_name_get { |
1617 | __le32 flags; | |
1618 | u8 name[SCMI_MAX_STR_SIZE]; | |
1619 | }; | |
1620 | ||
1621 | /** | |
1622 | * scmi_common_extended_name_get - Common helper to get extended resources name | |
1623 | * @ph: A protocol handle reference. | |
1624 | * @cmd_id: The specific command ID to use. | |
1625 | * @res_id: The specific resource ID to use. | |
e4e6e8f1 | 1626 | * @flags: A pointer to specific flags to use, if any. |
5c873d12 CM |
1627 | * @name: A pointer to the preallocated area where the retrieved name will be |
1628 | * stored as a NULL terminated string. | |
1629 | * @len: The len in bytes of the @name char array. | |
1630 | * | |
1631 | * Return: 0 on Succcess | |
1632 | */ | |
1633 | static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph, | |
e4e6e8f1 CM |
1634 | u8 cmd_id, u32 res_id, u32 *flags, |
1635 | char *name, size_t len) | |
5c873d12 CM |
1636 | { |
1637 | int ret; | |
e4e6e8f1 | 1638 | size_t txlen; |
5c873d12 CM |
1639 | struct scmi_xfer *t; |
1640 | struct scmi_msg_resp_domain_name_get *resp; | |
1641 | ||
e4e6e8f1 CM |
1642 | txlen = !flags ? sizeof(res_id) : sizeof(res_id) + sizeof(*flags); |
1643 | ret = ph->xops->xfer_get_init(ph, cmd_id, txlen, sizeof(*resp), &t); | |
5c873d12 CM |
1644 | if (ret) |
1645 | goto out; | |
1646 | ||
1647 | put_unaligned_le32(res_id, t->tx.buf); | |
e4e6e8f1 CM |
1648 | if (flags) |
1649 | put_unaligned_le32(*flags, t->tx.buf + sizeof(res_id)); | |
5c873d12 CM |
1650 | resp = t->rx.buf; |
1651 | ||
1652 | ret = ph->xops->do_xfer(ph, t); | |
1653 | if (!ret) | |
1654 | strscpy(name, resp->name, len); | |
1655 | ||
1656 | ph->xops->xfer_put(ph, t); | |
1657 | out: | |
1658 | if (ret) | |
1659 | dev_warn(ph->dev, | |
1660 | "Failed to get extended name - id:%u (ret:%d). Using %s\n", | |
1661 | res_id, ret, name); | |
1662 | return ret; | |
1663 | } | |
1664 | ||
4869b5cc PF |
1665 | /** |
1666 | * scmi_common_get_max_msg_size - Get maximum message size | |
1667 | * @ph: A protocol handle reference. | |
1668 | * | |
1669 | * Return: Maximum message size for the current protocol. | |
1670 | */ | |
1671 | static int scmi_common_get_max_msg_size(const struct scmi_protocol_handle *ph) | |
1672 | { | |
1673 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
1674 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
1675 | ||
1676 | return info->desc->max_msg_size; | |
1677 | } | |
1678 | ||
36b6ea0f CM |
1679 | /** |
1680 | * struct scmi_iterator - Iterator descriptor | |
1681 | * @msg: A reference to the message TX buffer; filled by @prepare_message with | |
1682 | * a proper custom command payload for each multi-part command request. | |
1683 | * @resp: A reference to the response RX buffer; used by @update_state and | |
1684 | * @process_response to parse the multi-part replies. | |
1685 | * @t: A reference to the underlying xfer initialized and used transparently by | |
1686 | * the iterator internal routines. | |
1687 | * @ph: A reference to the associated protocol handle to be used. | |
1688 | * @ops: A reference to the custom provided iterator operations. | |
1689 | * @state: The current iterator state; used and updated in turn by the iterators | |
1690 | * internal routines and by the caller-provided @scmi_iterator_ops. | |
1691 | * @priv: A reference to optional private data as provided by the caller and | |
1692 | * passed back to the @@scmi_iterator_ops. | |
1693 | */ | |
1694 | struct scmi_iterator { | |
1695 | void *msg; | |
1696 | void *resp; | |
1697 | struct scmi_xfer *t; | |
1698 | const struct scmi_protocol_handle *ph; | |
1699 | struct scmi_iterator_ops *ops; | |
1700 | struct scmi_iterator_state state; | |
1701 | void *priv; | |
1702 | }; | |
1703 | ||
1704 | static void *scmi_iterator_init(const struct scmi_protocol_handle *ph, | |
1705 | struct scmi_iterator_ops *ops, | |
1706 | unsigned int max_resources, u8 msg_id, | |
1707 | size_t tx_size, void *priv) | |
1708 | { | |
1709 | int ret; | |
1710 | struct scmi_iterator *i; | |
1711 | ||
1712 | i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL); | |
1713 | if (!i) | |
1714 | return ERR_PTR(-ENOMEM); | |
1715 | ||
1716 | i->ph = ph; | |
1717 | i->ops = ops; | |
1718 | i->priv = priv; | |
1719 | ||
1720 | ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t); | |
1721 | if (ret) { | |
1722 | devm_kfree(ph->dev, i); | |
1723 | return ERR_PTR(ret); | |
1724 | } | |
1725 | ||
1726 | i->state.max_resources = max_resources; | |
1727 | i->msg = i->t->tx.buf; | |
1728 | i->resp = i->t->rx.buf; | |
1729 | ||
1730 | return i; | |
1731 | } | |
1732 | ||
1733 | static int scmi_iterator_run(void *iter) | |
1734 | { | |
1735 | int ret = -EINVAL; | |
c7f8852d CM |
1736 | struct scmi_iterator_ops *iops; |
1737 | const struct scmi_protocol_handle *ph; | |
1738 | struct scmi_iterator_state *st; | |
36b6ea0f | 1739 | struct scmi_iterator *i = iter; |
36b6ea0f | 1740 | |
c7f8852d | 1741 | if (!i || !i->ops || !i->ph) |
36b6ea0f CM |
1742 | return ret; |
1743 | ||
c7f8852d CM |
1744 | iops = i->ops; |
1745 | ph = i->ph; | |
1746 | st = &i->state; | |
1747 | ||
36b6ea0f CM |
1748 | do { |
1749 | iops->prepare_message(i->msg, st->desc_index, i->priv); | |
c7f8852d | 1750 | ret = ph->xops->do_xfer(ph, i->t); |
36b6ea0f CM |
1751 | if (ret) |
1752 | break; | |
1753 | ||
754f04ca | 1754 | st->rx_len = i->t->rx.len; |
36b6ea0f CM |
1755 | ret = iops->update_state(st, i->resp, i->priv); |
1756 | if (ret) | |
1757 | break; | |
1758 | ||
1759 | if (st->num_returned > st->max_resources - st->desc_index) { | |
1760 | dev_err(ph->dev, | |
1761 | "No. of resources can't exceed %d\n", | |
1762 | st->max_resources); | |
1763 | ret = -EINVAL; | |
1764 | break; | |
1765 | } | |
1766 | ||
1767 | for (st->loop_idx = 0; st->loop_idx < st->num_returned; | |
1768 | st->loop_idx++) { | |
1769 | ret = iops->process_response(ph, i->resp, st, i->priv); | |
1770 | if (ret) | |
1771 | goto out; | |
1772 | } | |
1773 | ||
1774 | st->desc_index += st->num_returned; | |
c7f8852d | 1775 | ph->xops->reset_rx_to_maxsz(ph, i->t); |
36b6ea0f CM |
1776 | /* |
1777 | * check for both returned and remaining to avoid infinite | |
1778 | * loop due to buggy firmware | |
1779 | */ | |
1780 | } while (st->num_returned && st->num_remaining); | |
1781 | ||
1782 | out: | |
1783 | /* Finalize and destroy iterator */ | |
c7f8852d | 1784 | ph->xops->xfer_put(ph, i->t); |
36b6ea0f CM |
1785 | devm_kfree(ph->dev, i); |
1786 | ||
1787 | return ret; | |
1788 | } | |
1789 | ||
6f9ea4da CM |
1790 | struct scmi_msg_get_fc_info { |
1791 | __le32 domain; | |
1792 | __le32 message_id; | |
1793 | }; | |
1794 | ||
1795 | struct scmi_msg_resp_desc_fc { | |
1796 | __le32 attr; | |
1797 | #define SUPPORTS_DOORBELL(x) ((x) & BIT(0)) | |
1798 | #define DOORBELL_REG_WIDTH(x) FIELD_GET(GENMASK(2, 1), (x)) | |
1799 | __le32 rate_limit; | |
1800 | __le32 chan_addr_low; | |
1801 | __le32 chan_addr_high; | |
1802 | __le32 chan_size; | |
1803 | __le32 db_addr_low; | |
1804 | __le32 db_addr_high; | |
1805 | __le32 db_set_lmask; | |
1806 | __le32 db_set_hmask; | |
1807 | __le32 db_preserve_lmask; | |
1808 | __le32 db_preserve_hmask; | |
1809 | }; | |
1810 | ||
1811 | static void | |
1812 | scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph, | |
1813 | u8 describe_id, u32 message_id, u32 valid_size, | |
1814 | u32 domain, void __iomem **p_addr, | |
2441caa8 | 1815 | struct scmi_fc_db_info **p_db, u32 *rate_limit) |
6f9ea4da CM |
1816 | { |
1817 | int ret; | |
1818 | u32 flags; | |
1819 | u64 phys_addr; | |
1820 | u8 size; | |
1821 | void __iomem *addr; | |
1822 | struct scmi_xfer *t; | |
1823 | struct scmi_fc_db_info *db = NULL; | |
1824 | struct scmi_msg_get_fc_info *info; | |
1825 | struct scmi_msg_resp_desc_fc *resp; | |
1826 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
1827 | ||
1828 | if (!p_addr) { | |
1829 | ret = -EINVAL; | |
1830 | goto err_out; | |
1831 | } | |
1832 | ||
1833 | ret = ph->xops->xfer_get_init(ph, describe_id, | |
1834 | sizeof(*info), sizeof(*resp), &t); | |
1835 | if (ret) | |
1836 | goto err_out; | |
1837 | ||
1838 | info = t->tx.buf; | |
1839 | info->domain = cpu_to_le32(domain); | |
1840 | info->message_id = cpu_to_le32(message_id); | |
1841 | ||
1842 | /* | |
1843 | * Bail out on error leaving fc_info addresses zeroed; this includes | |
1844 | * the case in which the requested domain/message_id does NOT support | |
1845 | * fastchannels at all. | |
1846 | */ | |
1847 | ret = ph->xops->do_xfer(ph, t); | |
1848 | if (ret) | |
1849 | goto err_xfer; | |
1850 | ||
1851 | resp = t->rx.buf; | |
1852 | flags = le32_to_cpu(resp->attr); | |
1853 | size = le32_to_cpu(resp->chan_size); | |
1854 | if (size != valid_size) { | |
1855 | ret = -EINVAL; | |
1856 | goto err_xfer; | |
1857 | } | |
1858 | ||
2441caa8 PG |
1859 | if (rate_limit) |
1860 | *rate_limit = le32_to_cpu(resp->rate_limit) & GENMASK(19, 0); | |
1861 | ||
6f9ea4da CM |
1862 | phys_addr = le32_to_cpu(resp->chan_addr_low); |
1863 | phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32; | |
1864 | addr = devm_ioremap(ph->dev, phys_addr, size); | |
1865 | if (!addr) { | |
1866 | ret = -EADDRNOTAVAIL; | |
1867 | goto err_xfer; | |
1868 | } | |
1869 | ||
1870 | *p_addr = addr; | |
1871 | ||
1872 | if (p_db && SUPPORTS_DOORBELL(flags)) { | |
1873 | db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL); | |
1874 | if (!db) { | |
1875 | ret = -ENOMEM; | |
1876 | goto err_db; | |
1877 | } | |
1878 | ||
1879 | size = 1 << DOORBELL_REG_WIDTH(flags); | |
1880 | phys_addr = le32_to_cpu(resp->db_addr_low); | |
1881 | phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32; | |
1882 | addr = devm_ioremap(ph->dev, phys_addr, size); | |
1883 | if (!addr) { | |
1884 | ret = -EADDRNOTAVAIL; | |
1885 | goto err_db_mem; | |
1886 | } | |
1887 | ||
1888 | db->addr = addr; | |
1889 | db->width = size; | |
1890 | db->set = le32_to_cpu(resp->db_set_lmask); | |
1891 | db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32; | |
1892 | db->mask = le32_to_cpu(resp->db_preserve_lmask); | |
1893 | db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32; | |
1894 | ||
1895 | *p_db = db; | |
1896 | } | |
1897 | ||
1898 | ph->xops->xfer_put(ph, t); | |
1899 | ||
1900 | dev_dbg(ph->dev, | |
1901 | "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n", | |
1902 | pi->proto->id, message_id, domain); | |
1903 | ||
1904 | return; | |
1905 | ||
1906 | err_db_mem: | |
1907 | devm_kfree(ph->dev, db); | |
1908 | ||
1909 | err_db: | |
1910 | *p_addr = NULL; | |
1911 | ||
1912 | err_xfer: | |
1913 | ph->xops->xfer_put(ph, t); | |
1914 | ||
1915 | err_out: | |
1916 | dev_warn(ph->dev, | |
1917 | "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n", | |
1918 | pi->proto->id, message_id, domain, ret); | |
1919 | } | |
1920 | ||
1921 | #define SCMI_PROTO_FC_RING_DB(w) \ | |
1922 | do { \ | |
1923 | u##w val = 0; \ | |
1924 | \ | |
1925 | if (db->mask) \ | |
1926 | val = ioread##w(db->addr) & db->mask; \ | |
1927 | iowrite##w((u##w)db->set | val, db->addr); \ | |
1928 | } while (0) | |
1929 | ||
1930 | static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db) | |
1931 | { | |
1932 | if (!db || !db->addr) | |
1933 | return; | |
1934 | ||
1935 | if (db->width == 1) | |
1936 | SCMI_PROTO_FC_RING_DB(8); | |
1937 | else if (db->width == 2) | |
1938 | SCMI_PROTO_FC_RING_DB(16); | |
1939 | else if (db->width == 4) | |
1940 | SCMI_PROTO_FC_RING_DB(32); | |
1941 | else /* db->width == 8 */ | |
1942 | #ifdef CONFIG_64BIT | |
1943 | SCMI_PROTO_FC_RING_DB(64); | |
1944 | #else | |
1945 | { | |
1946 | u64 val = 0; | |
1947 | ||
1948 | if (db->mask) | |
1949 | val = ioread64_hi_lo(db->addr) & db->mask; | |
1950 | iowrite64_hi_lo(db->set | val, db->addr); | |
1951 | } | |
1952 | #endif | |
1953 | } | |
1954 | ||
637b6d6c CM |
1955 | /** |
1956 | * scmi_protocol_msg_check - Check protocol message attributes | |
1957 | * | |
1958 | * @ph: A reference to the protocol handle. | |
1959 | * @message_id: The ID of the message to check. | |
1960 | * @attributes: A parameter to optionally return the retrieved message | |
1961 | * attributes, in case of Success. | |
1962 | * | |
1963 | * An helper to check protocol message attributes for a specific protocol | |
1964 | * and message pair. | |
1965 | * | |
1966 | * Return: 0 on SUCCESS | |
1967 | */ | |
1968 | static int scmi_protocol_msg_check(const struct scmi_protocol_handle *ph, | |
1969 | u32 message_id, u32 *attributes) | |
1970 | { | |
1971 | int ret; | |
1972 | struct scmi_xfer *t; | |
1973 | ||
1974 | ret = xfer_get_init(ph, PROTOCOL_MESSAGE_ATTRIBUTES, | |
1975 | sizeof(__le32), 0, &t); | |
1976 | if (ret) | |
1977 | return ret; | |
1978 | ||
1979 | put_unaligned_le32(message_id, t->tx.buf); | |
1980 | ret = do_xfer(ph, t); | |
1981 | if (!ret && attributes) | |
1982 | *attributes = get_unaligned_le32(t->rx.buf); | |
1983 | xfer_put(ph, t); | |
1984 | ||
1985 | return ret; | |
1986 | } | |
1987 | ||
5c873d12 CM |
1988 | static const struct scmi_proto_helpers_ops helpers_ops = { |
1989 | .extended_name_get = scmi_common_extended_name_get, | |
4869b5cc | 1990 | .get_max_msg_size = scmi_common_get_max_msg_size, |
36b6ea0f CM |
1991 | .iter_response_init = scmi_iterator_init, |
1992 | .iter_response_run = scmi_iterator_run, | |
637b6d6c | 1993 | .protocol_msg_check = scmi_protocol_msg_check, |
6f9ea4da CM |
1994 | .fastchannel_init = scmi_common_fastchannel_init, |
1995 | .fastchannel_db_ring = scmi_common_fastchannel_db_ring, | |
5c873d12 CM |
1996 | }; |
1997 | ||
3d5d6e84 CM |
1998 | /** |
1999 | * scmi_revision_area_get - Retrieve version memory area. | |
2000 | * | |
2001 | * @ph: A reference to the protocol handle. | |
2002 | * | |
2003 | * A helper to grab the version memory area reference during SCMI Base protocol | |
2004 | * initialization. | |
2005 | * | |
2006 | * Return: A reference to the version memory area associated to the SCMI | |
2007 | * instance underlying this protocol handle. | |
2008 | */ | |
2009 | struct scmi_revision_info * | |
2010 | scmi_revision_area_get(const struct scmi_protocol_handle *ph) | |
2011 | { | |
2012 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
2013 | ||
2014 | return pi->handle->version; | |
2015 | } | |
2016 | ||
8c80c42a CM |
2017 | /** |
2018 | * scmi_protocol_version_negotiate - Negotiate protocol version | |
2019 | * | |
2020 | * @ph: A reference to the protocol handle. | |
2021 | * | |
2022 | * An helper to negotiate a protocol version different from the latest | |
2023 | * advertised as supported from the platform: on Success backward | |
2024 | * compatibility is assured by the platform. | |
2025 | * | |
2026 | * Return: 0 on Success | |
2027 | */ | |
2028 | static int scmi_protocol_version_negotiate(struct scmi_protocol_handle *ph) | |
2029 | { | |
2030 | int ret; | |
2031 | struct scmi_xfer *t; | |
2032 | struct scmi_protocol_instance *pi = ph_to_pi(ph); | |
2033 | ||
2034 | /* At first check if NEGOTIATE_PROTOCOL_VERSION is supported ... */ | |
2035 | ret = scmi_protocol_msg_check(ph, NEGOTIATE_PROTOCOL_VERSION, NULL); | |
2036 | if (ret) | |
2037 | return ret; | |
2038 | ||
2039 | /* ... then attempt protocol version negotiation */ | |
2040 | ret = xfer_get_init(ph, NEGOTIATE_PROTOCOL_VERSION, | |
2041 | sizeof(__le32), 0, &t); | |
2042 | if (ret) | |
2043 | return ret; | |
2044 | ||
2045 | put_unaligned_le32(pi->proto->supported_version, t->tx.buf); | |
2046 | ret = do_xfer(ph, t); | |
2047 | if (!ret) | |
2048 | pi->negotiated_version = pi->proto->supported_version; | |
2049 | ||
2050 | xfer_put(ph, t); | |
2051 | ||
2052 | return ret; | |
2053 | } | |
2054 | ||
48dc16e2 CM |
2055 | /** |
2056 | * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol | |
2057 | * instance descriptor. | |
2058 | * @info: The reference to the related SCMI instance. | |
2059 | * @proto: The protocol descriptor. | |
2060 | * | |
2061 | * Allocate a new protocol instance descriptor, using the provided @proto | |
2062 | * description, against the specified SCMI instance @info, and initialize it; | |
2063 | * all resources management is handled via a dedicated per-protocol devres | |
2064 | * group. | |
2065 | * | |
2066 | * Context: Assumes to be called with @protocols_mtx already acquired. | |
2067 | * Return: A reference to a freshly allocated and initialized protocol instance | |
f5800e0b CM |
2068 | * or ERR_PTR on failure. On failure the @proto reference is at first |
2069 | * put using @scmi_protocol_put() before releasing all the devres group. | |
48dc16e2 CM |
2070 | */ |
2071 | static struct scmi_protocol_instance * | |
2072 | scmi_alloc_init_protocol_instance(struct scmi_info *info, | |
2073 | const struct scmi_protocol *proto) | |
2074 | { | |
2075 | int ret = -ENOMEM; | |
2076 | void *gid; | |
2077 | struct scmi_protocol_instance *pi; | |
f0e73cee | 2078 | const struct scmi_handle *handle = &info->handle; |
48dc16e2 CM |
2079 | |
2080 | /* Protocol specific devres group */ | |
2081 | gid = devres_open_group(handle->dev, NULL, GFP_KERNEL); | |
f5800e0b | 2082 | if (!gid) { |
fc110108 | 2083 | scmi_protocol_put(proto); |
48dc16e2 | 2084 | goto out; |
f5800e0b | 2085 | } |
48dc16e2 CM |
2086 | |
2087 | pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL); | |
2088 | if (!pi) | |
2089 | goto clean; | |
2090 | ||
2091 | pi->gid = gid; | |
2092 | pi->proto = proto; | |
d7b6cc56 CM |
2093 | pi->handle = handle; |
2094 | pi->ph.dev = handle->dev; | |
a4a20b09 | 2095 | pi->ph.xops = &xfer_ops; |
5c873d12 | 2096 | pi->ph.hops = &helpers_ops; |
d7b6cc56 CM |
2097 | pi->ph.set_priv = scmi_set_protocol_priv; |
2098 | pi->ph.get_priv = scmi_get_protocol_priv; | |
48dc16e2 CM |
2099 | refcount_set(&pi->users, 1); |
2100 | /* proto->init is assured NON NULL by scmi_protocol_register */ | |
a4a20b09 | 2101 | ret = pi->proto->instance_init(&pi->ph); |
48dc16e2 CM |
2102 | if (ret) |
2103 | goto clean; | |
2104 | ||
2105 | ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1, | |
2106 | GFP_KERNEL); | |
2107 | if (ret != proto->id) | |
2108 | goto clean; | |
2109 | ||
533c7095 CM |
2110 | /* |
2111 | * Warn but ignore events registration errors since we do not want | |
2112 | * to skip whole protocols if their notifications are messed up. | |
2113 | */ | |
2114 | if (pi->proto->events) { | |
2115 | ret = scmi_register_protocol_events(handle, pi->proto->id, | |
b9f7fd90 | 2116 | &pi->ph, |
533c7095 CM |
2117 | pi->proto->events); |
2118 | if (ret) | |
2119 | dev_warn(handle->dev, | |
2120 | "Protocol:%X - Events Registration Failed - err:%d\n", | |
2121 | pi->proto->id, ret); | |
2122 | } | |
2123 | ||
48dc16e2 CM |
2124 | devres_close_group(handle->dev, pi->gid); |
2125 | dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id); | |
2126 | ||
8c80c42a CM |
2127 | if (pi->version > proto->supported_version) { |
2128 | ret = scmi_protocol_version_negotiate(&pi->ph); | |
2129 | if (!ret) { | |
2130 | dev_info(handle->dev, | |
2131 | "Protocol 0x%X successfully negotiated version 0x%X\n", | |
2132 | proto->id, pi->negotiated_version); | |
2133 | } else { | |
2134 | dev_warn(handle->dev, | |
2135 | "Detected UNSUPPORTED higher version 0x%X for protocol 0x%X.\n", | |
2136 | pi->version, pi->proto->id); | |
2137 | dev_warn(handle->dev, | |
2138 | "Trying version 0x%X. Backward compatibility is NOT assured.\n", | |
2139 | pi->proto->supported_version); | |
2140 | } | |
2141 | } | |
b5efc28a | 2142 | |
48dc16e2 CM |
2143 | return pi; |
2144 | ||
2145 | clean: | |
f5800e0b | 2146 | /* Take care to put the protocol module's owner before releasing all */ |
fc110108 | 2147 | scmi_protocol_put(proto); |
48dc16e2 CM |
2148 | devres_release_group(handle->dev, gid); |
2149 | out: | |
2150 | return ERR_PTR(ret); | |
2151 | } | |
2152 | ||
2153 | /** | |
2154 | * scmi_get_protocol_instance - Protocol initialization helper. | |
2155 | * @handle: A reference to the SCMI platform instance. | |
2156 | * @protocol_id: The protocol being requested. | |
2157 | * | |
2158 | * In case the required protocol has never been requested before for this | |
2159 | * instance, allocate and initialize all the needed structures while handling | |
2160 | * resource allocation with a dedicated per-protocol devres subgroup. | |
2161 | * | |
f5800e0b CM |
2162 | * Return: A reference to an initialized protocol instance or error on failure: |
2163 | * in particular returns -EPROBE_DEFER when the desired protocol could | |
2164 | * NOT be found. | |
48dc16e2 CM |
2165 | */ |
2166 | static struct scmi_protocol_instance * __must_check | |
f0e73cee | 2167 | scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id) |
48dc16e2 CM |
2168 | { |
2169 | struct scmi_protocol_instance *pi; | |
2170 | struct scmi_info *info = handle_to_scmi_info(handle); | |
2171 | ||
2172 | mutex_lock(&info->protocols_mtx); | |
2173 | pi = idr_find(&info->protocols, protocol_id); | |
2174 | ||
2175 | if (pi) { | |
2176 | refcount_inc(&pi->users); | |
2177 | } else { | |
2178 | const struct scmi_protocol *proto; | |
2179 | ||
2180 | /* Fails if protocol not registered on bus */ | |
fc110108 | 2181 | proto = scmi_protocol_get(protocol_id, &info->version); |
48dc16e2 CM |
2182 | if (proto) |
2183 | pi = scmi_alloc_init_protocol_instance(info, proto); | |
2184 | else | |
f5800e0b | 2185 | pi = ERR_PTR(-EPROBE_DEFER); |
48dc16e2 CM |
2186 | } |
2187 | mutex_unlock(&info->protocols_mtx); | |
2188 | ||
2189 | return pi; | |
2190 | } | |
2191 | ||
2192 | /** | |
2193 | * scmi_protocol_acquire - Protocol acquire | |
2194 | * @handle: A reference to the SCMI platform instance. | |
2195 | * @protocol_id: The protocol being requested. | |
2196 | * | |
2197 | * Register a new user for the requested protocol on the specified SCMI | |
2198 | * platform instance, possibly triggering its initialization on first user. | |
2199 | * | |
2200 | * Return: 0 if protocol was acquired successfully. | |
2201 | */ | |
f0e73cee | 2202 | int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id) |
48dc16e2 CM |
2203 | { |
2204 | return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id)); | |
2205 | } | |
2206 | ||
2207 | /** | |
2208 | * scmi_protocol_release - Protocol de-initialization helper. | |
2209 | * @handle: A reference to the SCMI platform instance. | |
2210 | * @protocol_id: The protocol being requested. | |
2211 | * | |
2212 | * Remove one user for the specified protocol and triggers de-initialization | |
2213 | * and resources de-allocation once the last user has gone. | |
2214 | */ | |
f0e73cee | 2215 | void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id) |
48dc16e2 CM |
2216 | { |
2217 | struct scmi_info *info = handle_to_scmi_info(handle); | |
2218 | struct scmi_protocol_instance *pi; | |
2219 | ||
2220 | mutex_lock(&info->protocols_mtx); | |
2221 | pi = idr_find(&info->protocols, protocol_id); | |
2222 | if (WARN_ON(!pi)) | |
2223 | goto out; | |
2224 | ||
2225 | if (refcount_dec_and_test(&pi->users)) { | |
2226 | void *gid = pi->gid; | |
2227 | ||
533c7095 CM |
2228 | if (pi->proto->events) |
2229 | scmi_deregister_protocol_events(handle, protocol_id); | |
2230 | ||
48dc16e2 | 2231 | if (pi->proto->instance_deinit) |
a4a20b09 | 2232 | pi->proto->instance_deinit(&pi->ph); |
48dc16e2 CM |
2233 | |
2234 | idr_remove(&info->protocols, protocol_id); | |
2235 | ||
fc110108 | 2236 | scmi_protocol_put(pi->proto); |
f5800e0b | 2237 | |
48dc16e2 CM |
2238 | devres_release_group(handle->dev, gid); |
2239 | dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", | |
2240 | protocol_id); | |
2241 | } | |
2242 | ||
2243 | out: | |
2244 | mutex_unlock(&info->protocols_mtx); | |
2245 | } | |
2246 | ||
8d3581c2 | 2247 | void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph, |
b6f20ff8 SH |
2248 | u8 *prot_imp) |
2249 | { | |
8d3581c2 CM |
2250 | const struct scmi_protocol_instance *pi = ph_to_pi(ph); |
2251 | struct scmi_info *info = handle_to_scmi_info(pi->handle); | |
b6f20ff8 SH |
2252 | |
2253 | info->protocols_imp = prot_imp; | |
2254 | } | |
2255 | ||
bc40081d SH |
2256 | static bool |
2257 | scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id) | |
2258 | { | |
2259 | int i; | |
2260 | struct scmi_info *info = handle_to_scmi_info(handle); | |
776b6c8a | 2261 | struct scmi_revision_info *rev = handle->version; |
bc40081d SH |
2262 | |
2263 | if (!info->protocols_imp) | |
2264 | return false; | |
2265 | ||
776b6c8a | 2266 | for (i = 0; i < rev->num_protocols; i++) |
bc40081d SH |
2267 | if (info->protocols_imp[i] == prot_id) |
2268 | return true; | |
2269 | return false; | |
2270 | } | |
2271 | ||
23934efe | 2272 | struct scmi_protocol_devres { |
f0e73cee | 2273 | const struct scmi_handle *handle; |
23934efe CM |
2274 | u8 protocol_id; |
2275 | }; | |
2276 | ||
2277 | static void scmi_devm_release_protocol(struct device *dev, void *res) | |
2278 | { | |
2279 | struct scmi_protocol_devres *dres = res; | |
2280 | ||
2281 | scmi_protocol_release(dres->handle, dres->protocol_id); | |
2282 | } | |
2283 | ||
d9107999 CM |
2284 | static struct scmi_protocol_instance __must_check * |
2285 | scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id) | |
2286 | { | |
2287 | struct scmi_protocol_instance *pi; | |
2288 | struct scmi_protocol_devres *dres; | |
2289 | ||
2290 | dres = devres_alloc(scmi_devm_release_protocol, | |
2291 | sizeof(*dres), GFP_KERNEL); | |
2292 | if (!dres) | |
2293 | return ERR_PTR(-ENOMEM); | |
2294 | ||
2295 | pi = scmi_get_protocol_instance(sdev->handle, protocol_id); | |
2296 | if (IS_ERR(pi)) { | |
2297 | devres_free(dres); | |
2298 | return pi; | |
2299 | } | |
2300 | ||
2301 | dres->handle = sdev->handle; | |
2302 | dres->protocol_id = protocol_id; | |
2303 | devres_add(&sdev->dev, dres); | |
2304 | ||
2305 | return pi; | |
2306 | } | |
2307 | ||
23934efe CM |
2308 | /** |
2309 | * scmi_devm_protocol_get - Devres managed get protocol operations and handle | |
2310 | * @sdev: A reference to an scmi_device whose embedded struct device is to | |
2311 | * be used for devres accounting. | |
2312 | * @protocol_id: The protocol being requested. | |
2313 | * @ph: A pointer reference used to pass back the associated protocol handle. | |
2314 | * | |
2315 | * Get hold of a protocol accounting for its usage, eventually triggering its | |
2316 | * initialization, and returning the protocol specific operations and related | |
2317 | * protocol handle which will be used as first argument in most of the | |
2318 | * protocols operations methods. | |
2319 | * Being a devres based managed method, protocol hold will be automatically | |
2320 | * released, and possibly de-initialized on last user, once the SCMI driver | |
2321 | * owning the scmi_device is unbound from it. | |
2322 | * | |
2323 | * Return: A reference to the requested protocol operations or error. | |
2324 | * Must be checked for errors by caller. | |
2325 | */ | |
2326 | static const void __must_check * | |
2327 | scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id, | |
2328 | struct scmi_protocol_handle **ph) | |
2329 | { | |
2330 | struct scmi_protocol_instance *pi; | |
23934efe CM |
2331 | |
2332 | if (!ph) | |
2333 | return ERR_PTR(-EINVAL); | |
2334 | ||
d9107999 CM |
2335 | pi = scmi_devres_protocol_instance_get(sdev, protocol_id); |
2336 | if (IS_ERR(pi)) | |
23934efe | 2337 | return pi; |
23934efe CM |
2338 | |
2339 | *ph = &pi->ph; | |
2340 | ||
2341 | return pi->proto->ops; | |
2342 | } | |
2343 | ||
d9107999 CM |
2344 | /** |
2345 | * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol | |
2346 | * @sdev: A reference to an scmi_device whose embedded struct device is to | |
2347 | * be used for devres accounting. | |
2348 | * @protocol_id: The protocol being requested. | |
2349 | * | |
2350 | * Get hold of a protocol accounting for its usage, possibly triggering its | |
2351 | * initialization but without getting access to its protocol specific operations | |
2352 | * and handle. | |
2353 | * | |
2354 | * Being a devres based managed method, protocol hold will be automatically | |
2355 | * released, and possibly de-initialized on last user, once the SCMI driver | |
2356 | * owning the scmi_device is unbound from it. | |
2357 | * | |
2358 | * Return: 0 on SUCCESS | |
2359 | */ | |
2360 | static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev, | |
2361 | u8 protocol_id) | |
2362 | { | |
2363 | struct scmi_protocol_instance *pi; | |
2364 | ||
2365 | pi = scmi_devres_protocol_instance_get(sdev, protocol_id); | |
2366 | if (IS_ERR(pi)) | |
2367 | return PTR_ERR(pi); | |
2368 | ||
2369 | return 0; | |
2370 | } | |
2371 | ||
23934efe CM |
2372 | static int scmi_devm_protocol_match(struct device *dev, void *res, void *data) |
2373 | { | |
2374 | struct scmi_protocol_devres *dres = res; | |
2375 | ||
2376 | if (WARN_ON(!dres || !data)) | |
2377 | return 0; | |
2378 | ||
2379 | return dres->protocol_id == *((u8 *)data); | |
2380 | } | |
2381 | ||
2382 | /** | |
2383 | * scmi_devm_protocol_put - Devres managed put protocol operations and handle | |
2384 | * @sdev: A reference to an scmi_device whose embedded struct device is to | |
2385 | * be used for devres accounting. | |
2386 | * @protocol_id: The protocol being requested. | |
2387 | * | |
2388 | * Explicitly release a protocol hold previously obtained calling the above | |
2389 | * @scmi_devm_protocol_get. | |
2390 | */ | |
2391 | static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id) | |
2392 | { | |
2393 | int ret; | |
2394 | ||
2395 | ret = devres_release(&sdev->dev, scmi_devm_release_protocol, | |
2396 | scmi_devm_protocol_match, &protocol_id); | |
2397 | WARN_ON(ret); | |
2398 | } | |
2399 | ||
69255e74 CM |
2400 | /** |
2401 | * scmi_is_transport_atomic - Method to check if underlying transport for an | |
2402 | * SCMI instance is configured as atomic. | |
2403 | * | |
2404 | * @handle: A reference to the SCMI platform instance. | |
05976c5f CM |
2405 | * @atomic_threshold: An optional return value for the system wide currently |
2406 | * configured threshold for atomic operations. | |
69255e74 CM |
2407 | * |
2408 | * Return: True if transport is configured as atomic | |
2409 | */ | |
05976c5f CM |
2410 | static bool scmi_is_transport_atomic(const struct scmi_handle *handle, |
2411 | unsigned int *atomic_threshold) | |
69255e74 | 2412 | { |
05976c5f | 2413 | bool ret; |
69255e74 CM |
2414 | struct scmi_info *info = handle_to_scmi_info(handle); |
2415 | ||
f21c2b0b CM |
2416 | ret = info->desc->atomic_enabled && |
2417 | is_transport_polling_capable(info->desc); | |
05976c5f CM |
2418 | if (ret && atomic_threshold) |
2419 | *atomic_threshold = info->atomic_threshold; | |
2420 | ||
2421 | return ret; | |
69255e74 CM |
2422 | } |
2423 | ||
aa4f886f | 2424 | /** |
14e297b3 | 2425 | * scmi_handle_get() - Get the SCMI handle for a device |
aa4f886f SH |
2426 | * |
2427 | * @dev: pointer to device for which we want SCMI handle | |
2428 | * | |
2429 | * NOTE: The function does not track individual clients of the framework | |
1baf47c2 | 2430 | * and is expected to be maintained by caller of SCMI protocol library. |
aa4f886f SH |
2431 | * scmi_handle_put must be balanced with successful scmi_handle_get |
2432 | * | |
2433 | * Return: pointer to handle if successful, NULL on error | |
2434 | */ | |
971fc066 | 2435 | static struct scmi_handle *scmi_handle_get(struct device *dev) |
aa4f886f SH |
2436 | { |
2437 | struct list_head *p; | |
2438 | struct scmi_info *info; | |
2439 | struct scmi_handle *handle = NULL; | |
2440 | ||
2441 | mutex_lock(&scmi_list_mutex); | |
2442 | list_for_each(p, &scmi_list) { | |
2443 | info = list_entry(p, struct scmi_info, node); | |
2444 | if (dev->parent == info->dev) { | |
971fc066 CM |
2445 | info->users++; |
2446 | handle = &info->handle; | |
aa4f886f SH |
2447 | break; |
2448 | } | |
2449 | } | |
2450 | mutex_unlock(&scmi_list_mutex); | |
2451 | ||
2452 | return handle; | |
2453 | } | |
2454 | ||
2455 | /** | |
2456 | * scmi_handle_put() - Release the handle acquired by scmi_handle_get | |
2457 | * | |
2458 | * @handle: handle acquired by scmi_handle_get | |
2459 | * | |
2460 | * NOTE: The function does not track individual clients of the framework | |
1baf47c2 | 2461 | * and is expected to be maintained by caller of SCMI protocol library. |
aa4f886f SH |
2462 | * scmi_handle_put must be balanced with successful scmi_handle_get |
2463 | * | |
2464 | * Return: 0 is successfully released | |
2465 | * if null was passed, it returns -EINVAL; | |
2466 | */ | |
971fc066 | 2467 | static int scmi_handle_put(const struct scmi_handle *handle) |
aa4f886f SH |
2468 | { |
2469 | struct scmi_info *info; | |
2470 | ||
2471 | if (!handle) | |
2472 | return -EINVAL; | |
2473 | ||
2474 | info = handle_to_scmi_info(handle); | |
2475 | mutex_lock(&scmi_list_mutex); | |
2476 | if (!WARN_ON(!info->users)) | |
2477 | info->users--; | |
2478 | mutex_unlock(&scmi_list_mutex); | |
2479 | ||
2480 | return 0; | |
2481 | } | |
2482 | ||
971fc066 CM |
2483 | static void scmi_device_link_add(struct device *consumer, |
2484 | struct device *supplier) | |
2485 | { | |
2486 | struct device_link *link; | |
2487 | ||
2488 | link = device_link_add(consumer, supplier, DL_FLAG_AUTOREMOVE_CONSUMER); | |
2489 | ||
2490 | WARN_ON(!link); | |
2491 | } | |
2492 | ||
2493 | static void scmi_set_handle(struct scmi_device *scmi_dev) | |
2494 | { | |
2495 | scmi_dev->handle = scmi_handle_get(&scmi_dev->dev); | |
2496 | if (scmi_dev->handle) | |
2497 | scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev); | |
2498 | } | |
2499 | ||
4ebd8f6d SH |
2500 | static int __scmi_xfer_info_init(struct scmi_info *sinfo, |
2501 | struct scmi_xfers_info *info) | |
aa4f886f SH |
2502 | { |
2503 | int i; | |
2504 | struct scmi_xfer *xfer; | |
2505 | struct device *dev = sinfo->dev; | |
2506 | const struct scmi_desc *desc = sinfo->desc; | |
aa4f886f SH |
2507 | |
2508 | /* Pre-allocated messages, no more than what hdr.seq can support */ | |
c92c3e38 | 2509 | if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) { |
bdb8742d CM |
2510 | dev_err(dev, |
2511 | "Invalid maximum messages %d, not in range [1 - %lu]\n", | |
c92c3e38 | 2512 | info->max_msg, MSG_TOKEN_MAX); |
aa4f886f SH |
2513 | return -EINVAL; |
2514 | } | |
2515 | ||
9ca5a183 | 2516 | hash_init(info->pending_xfers); |
aa4f886f | 2517 | |
9ca5a183 | 2518 | /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */ |
d617808e CJ |
2519 | info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX, |
2520 | GFP_KERNEL); | |
aa4f886f SH |
2521 | if (!info->xfer_alloc_table) |
2522 | return -ENOMEM; | |
2523 | ||
9ca5a183 CM |
2524 | /* |
2525 | * Preallocate a number of xfers equal to max inflight messages, | |
2526 | * pre-initialize the buffer pointer to pre-allocated buffers and | |
2527 | * attach all of them to the free list | |
2528 | */ | |
2529 | INIT_HLIST_HEAD(&info->free_xfers); | |
c92c3e38 | 2530 | for (i = 0; i < info->max_msg; i++) { |
9ca5a183 CM |
2531 | xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL); |
2532 | if (!xfer) | |
2533 | return -ENOMEM; | |
2534 | ||
aa4f886f SH |
2535 | xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size, |
2536 | GFP_KERNEL); | |
2537 | if (!xfer->rx.buf) | |
2538 | return -ENOMEM; | |
2539 | ||
2540 | xfer->tx.buf = xfer->rx.buf; | |
2541 | init_completion(&xfer->done); | |
ed7c04c1 | 2542 | spin_lock_init(&xfer->lock); |
9ca5a183 CM |
2543 | |
2544 | /* Add initialized xfer to the free list */ | |
2545 | hlist_add_head(&xfer->node, &info->free_xfers); | |
aa4f886f SH |
2546 | } |
2547 | ||
2548 | spin_lock_init(&info->xfer_lock); | |
2549 | ||
2550 | return 0; | |
2551 | } | |
2552 | ||
c92c3e38 IS |
2553 | static int scmi_channels_max_msg_configure(struct scmi_info *sinfo) |
2554 | { | |
2555 | const struct scmi_desc *desc = sinfo->desc; | |
2556 | ||
2557 | if (!desc->ops->get_max_msg) { | |
2558 | sinfo->tx_minfo.max_msg = desc->max_msg; | |
2559 | sinfo->rx_minfo.max_msg = desc->max_msg; | |
2560 | } else { | |
2561 | struct scmi_chan_info *base_cinfo; | |
2562 | ||
2563 | base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE); | |
2564 | if (!base_cinfo) | |
2565 | return -EINVAL; | |
2566 | sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo); | |
2567 | ||
2568 | /* RX channel is optional so can be skipped */ | |
2569 | base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE); | |
2570 | if (base_cinfo) | |
2571 | sinfo->rx_minfo.max_msg = | |
2572 | desc->ops->get_max_msg(base_cinfo); | |
2573 | } | |
2574 | ||
2575 | return 0; | |
2576 | } | |
2577 | ||
4ebd8f6d SH |
2578 | static int scmi_xfer_info_init(struct scmi_info *sinfo) |
2579 | { | |
c92c3e38 IS |
2580 | int ret; |
2581 | ||
2582 | ret = scmi_channels_max_msg_configure(sinfo); | |
2583 | if (ret) | |
2584 | return ret; | |
4ebd8f6d | 2585 | |
c92c3e38 | 2586 | ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo); |
b2ccba9e | 2587 | if (!ret && !idr_is_empty(&sinfo->rx_idr)) |
4ebd8f6d SH |
2588 | ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo); |
2589 | ||
2590 | return ret; | |
2591 | } | |
2592 | ||
05a2801d | 2593 | static int scmi_chan_setup(struct scmi_info *info, struct device_node *of_node, |
5c8a47a5 | 2594 | int prot_id, bool tx) |
aa4f886f | 2595 | { |
3748daf7 | 2596 | int ret, idx; |
05a2801d | 2597 | char name[32]; |
fbc4d81a | 2598 | struct scmi_chan_info *cinfo; |
46cc7c28 | 2599 | struct idr *idr; |
05a2801d | 2600 | struct scmi_device *tdev = NULL; |
3748daf7 SH |
2601 | |
2602 | /* Transmit channel is first entry i.e. index 0 */ | |
2603 | idx = tx ? 0 : 1; | |
46cc7c28 | 2604 | idr = tx ? &info->tx_idr : &info->rx_idr; |
aa4f886f | 2605 | |
05a2801d | 2606 | if (!info->desc->ops->chan_available(of_node, idx)) { |
46cc7c28 SH |
2607 | cinfo = idr_find(idr, SCMI_PROTOCOL_BASE); |
2608 | if (unlikely(!cinfo)) /* Possible only if platform has no Rx */ | |
2609 | return -EINVAL; | |
907b6d14 SH |
2610 | goto idr_alloc; |
2611 | } | |
2612 | ||
fbc4d81a SH |
2613 | cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL); |
2614 | if (!cinfo) | |
2615 | return -ENOMEM; | |
2616 | ||
59172b21 | 2617 | cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms; |
fbc4d81a | 2618 | |
05a2801d CM |
2619 | /* Create a unique name for this transport device */ |
2620 | snprintf(name, 32, "__scmi_transport_device_%s_%02X", | |
2621 | idx ? "rx" : "tx", prot_id); | |
2622 | /* Create a uniquely named, dedicated transport device for this chan */ | |
2623 | tdev = scmi_device_create(of_node, info->dev, prot_id, name); | |
2624 | if (!tdev) { | |
2c3e6744 CM |
2625 | dev_err(info->dev, |
2626 | "failed to create transport device (%s)\n", name); | |
05a2801d CM |
2627 | devm_kfree(info->dev, cinfo); |
2628 | return -EINVAL; | |
2629 | } | |
2630 | of_node_get(of_node); | |
2631 | ||
75c86dc7 | 2632 | cinfo->id = prot_id; |
05a2801d | 2633 | cinfo->dev = &tdev->dev; |
5c8a47a5 | 2634 | ret = info->desc->ops->chan_setup(cinfo, info->dev, tx); |
05a2801d CM |
2635 | if (ret) { |
2636 | of_node_put(of_node); | |
2c3e6744 | 2637 | scmi_device_destroy(info->dev, prot_id, name); |
05a2801d | 2638 | devm_kfree(info->dev, cinfo); |
aa4f886f | 2639 | return ret; |
05a2801d | 2640 | } |
aa4f886f | 2641 | |
f21c2b0b CM |
2642 | if (tx && is_polling_required(cinfo, info->desc)) { |
2643 | if (is_transport_polling_capable(info->desc)) | |
05a2801d | 2644 | dev_info(&tdev->dev, |
a690b7e6 CM |
2645 | "Enabled polling mode TX channel - prot_id:%d\n", |
2646 | prot_id); | |
2647 | else | |
05a2801d | 2648 | dev_warn(&tdev->dev, |
a690b7e6 CM |
2649 | "Polling mode NOT supported by transport.\n"); |
2650 | } | |
2651 | ||
907b6d14 | 2652 | idr_alloc: |
46cc7c28 | 2653 | ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL); |
907b6d14 | 2654 | if (ret != prot_id) { |
05a2801d CM |
2655 | dev_err(info->dev, |
2656 | "unable to allocate SCMI idr slot err %d\n", ret); | |
2657 | /* Destroy channel and device only if created by this call. */ | |
2658 | if (tdev) { | |
2659 | of_node_put(of_node); | |
2c3e6744 | 2660 | scmi_device_destroy(info->dev, prot_id, name); |
05a2801d CM |
2661 | devm_kfree(info->dev, cinfo); |
2662 | } | |
907b6d14 SH |
2663 | return ret; |
2664 | } | |
2665 | ||
2666 | cinfo->handle = &info->handle; | |
aa4f886f SH |
2667 | return 0; |
2668 | } | |
2669 | ||
46cc7c28 | 2670 | static inline int |
05a2801d CM |
2671 | scmi_txrx_setup(struct scmi_info *info, struct device_node *of_node, |
2672 | int prot_id) | |
46cc7c28 | 2673 | { |
05a2801d | 2674 | int ret = scmi_chan_setup(info, of_node, prot_id, true); |
46cc7c28 | 2675 | |
be9ba1f7 CM |
2676 | if (!ret) { |
2677 | /* Rx is optional, report only memory errors */ | |
05a2801d | 2678 | ret = scmi_chan_setup(info, of_node, prot_id, false); |
be9ba1f7 CM |
2679 | if (ret && ret != -ENOMEM) |
2680 | ret = 0; | |
2681 | } | |
46cc7c28 | 2682 | |
3a7d93d1 CM |
2683 | if (ret) |
2684 | dev_err(info->dev, | |
2685 | "failed to setup channel for protocol:0x%X\n", prot_id); | |
2686 | ||
46cc7c28 SH |
2687 | return ret; |
2688 | } | |
2689 | ||
05a2801d CM |
2690 | /** |
2691 | * scmi_channels_setup - Helper to initialize all required channels | |
2692 | * | |
2693 | * @info: The SCMI instance descriptor. | |
2694 | * | |
2695 | * Initialize all the channels found described in the DT against the underlying | |
2696 | * configured transport using custom defined dedicated devices instead of | |
2697 | * borrowing devices from the SCMI drivers; this way channels are initialized | |
2698 | * upfront during core SCMI stack probing and are no more coupled with SCMI | |
2699 | * devices used by SCMI drivers. | |
2700 | * | |
2701 | * Note that, even though a pair of TX/RX channels is associated to each | |
2702 | * protocol defined in the DT, a distinct freshly initialized channel is | |
2703 | * created only if the DT node for the protocol at hand describes a dedicated | |
2704 | * channel: in all the other cases the common BASE protocol channel is reused. | |
2705 | * | |
2706 | * Return: 0 on Success | |
2707 | */ | |
2708 | static int scmi_channels_setup(struct scmi_info *info) | |
2709 | { | |
2710 | int ret; | |
2711 | struct device_node *child, *top_np = info->dev->of_node; | |
2712 | ||
2713 | /* Initialize a common generic channel at first */ | |
2714 | ret = scmi_txrx_setup(info, top_np, SCMI_PROTOCOL_BASE); | |
2715 | if (ret) | |
2716 | return ret; | |
2717 | ||
2718 | for_each_available_child_of_node(top_np, child) { | |
2719 | u32 prot_id; | |
2720 | ||
2721 | if (of_property_read_u32(child, "reg", &prot_id)) | |
2722 | continue; | |
2723 | ||
2724 | if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id)) | |
2725 | dev_err(info->dev, | |
2726 | "Out of range protocol %d\n", prot_id); | |
2727 | ||
2728 | ret = scmi_txrx_setup(info, child, prot_id); | |
2729 | if (ret) { | |
2730 | of_node_put(child); | |
2731 | return ret; | |
2732 | } | |
2733 | } | |
2734 | ||
2735 | return 0; | |
2736 | } | |
2737 | ||
05a2801d | 2738 | static int scmi_chan_destroy(int id, void *p, void *idr) |
1e7cbfaa | 2739 | { |
05a2801d CM |
2740 | struct scmi_chan_info *cinfo = p; |
2741 | ||
2742 | if (cinfo->dev) { | |
2c3e6744 | 2743 | struct scmi_info *info = handle_to_scmi_info(cinfo->handle); |
05a2801d | 2744 | struct scmi_device *sdev = to_scmi_dev(cinfo->dev); |
1e7cbfaa | 2745 | |
05a2801d | 2746 | of_node_put(cinfo->dev->of_node); |
2c3e6744 | 2747 | scmi_device_destroy(info->dev, id, sdev->name); |
05a2801d CM |
2748 | cinfo->dev = NULL; |
2749 | } | |
1e7cbfaa | 2750 | |
05a2801d | 2751 | idr_remove(idr, id); |
1e7cbfaa | 2752 | |
05a2801d CM |
2753 | return 0; |
2754 | } | |
2755 | ||
2756 | static void scmi_cleanup_channels(struct scmi_info *info, struct idr *idr) | |
2757 | { | |
2758 | /* At first free all channels at the transport layer ... */ | |
2759 | idr_for_each(idr, info->desc->ops->chan_free, idr); | |
2760 | ||
2761 | /* ...then destroy all underlying devices */ | |
2762 | idr_for_each(idr, scmi_chan_destroy, idr); | |
2763 | ||
2764 | idr_destroy(idr); | |
2765 | } | |
2766 | ||
2767 | static void scmi_cleanup_txrx_channels(struct scmi_info *info) | |
2768 | { | |
2769 | scmi_cleanup_channels(info, &info->tx_idr); | |
2770 | ||
2771 | scmi_cleanup_channels(info, &info->rx_idr); | |
1e7cbfaa RB |
2772 | } |
2773 | ||
53b8c25d CM |
2774 | static int scmi_bus_notifier(struct notifier_block *nb, |
2775 | unsigned long action, void *data) | |
2776 | { | |
2777 | struct scmi_info *info = bus_nb_to_scmi_info(nb); | |
2778 | struct scmi_device *sdev = to_scmi_dev(data); | |
2779 | ||
2780 | /* Skip transport devices and devices of different SCMI instances */ | |
2781 | if (!strncmp(sdev->name, "__scmi_transport_device", 23) || | |
2782 | sdev->dev.parent != info->dev) | |
2783 | return NOTIFY_DONE; | |
2784 | ||
2785 | switch (action) { | |
2786 | case BUS_NOTIFY_BIND_DRIVER: | |
971fc066 CM |
2787 | /* setup handle now as the transport is ready */ |
2788 | scmi_set_handle(sdev); | |
53b8c25d CM |
2789 | break; |
2790 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
971fc066 CM |
2791 | scmi_handle_put(sdev->handle); |
2792 | sdev->handle = NULL; | |
53b8c25d CM |
2793 | break; |
2794 | default: | |
2795 | return NOTIFY_DONE; | |
2796 | } | |
2797 | ||
2798 | dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev), | |
2799 | sdev->name, action == BUS_NOTIFY_BIND_DRIVER ? | |
2800 | "about to be BOUND." : "UNBOUND."); | |
2801 | ||
2802 | return NOTIFY_OK; | |
2803 | } | |
2804 | ||
2805 | static int scmi_device_request_notifier(struct notifier_block *nb, | |
2806 | unsigned long action, void *data) | |
2807 | { | |
2808 | struct device_node *np; | |
2809 | struct scmi_device_id *id_table = data; | |
2810 | struct scmi_info *info = req_nb_to_scmi_info(nb); | |
2811 | ||
2812 | np = idr_find(&info->active_protocols, id_table->protocol_id); | |
2813 | if (!np) | |
2814 | return NOTIFY_DONE; | |
2815 | ||
2816 | dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n", | |
2817 | action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-", | |
2818 | id_table->name, id_table->protocol_id); | |
2819 | ||
2820 | switch (action) { | |
2821 | case SCMI_BUS_NOTIFY_DEVICE_REQUEST: | |
ee5dceda CM |
2822 | scmi_create_protocol_devices(np, info, id_table->protocol_id, |
2823 | id_table->name); | |
53b8c25d CM |
2824 | break; |
2825 | case SCMI_BUS_NOTIFY_DEVICE_UNREQUEST: | |
ee5dceda CM |
2826 | scmi_destroy_protocol_devices(info, id_table->protocol_id, |
2827 | id_table->name); | |
53b8c25d CM |
2828 | break; |
2829 | default: | |
2830 | return NOTIFY_DONE; | |
2831 | } | |
2832 | ||
2833 | return NOTIFY_OK; | |
2834 | } | |
2835 | ||
c3d4aed7 CM |
2836 | static void scmi_debugfs_common_cleanup(void *d) |
2837 | { | |
2838 | struct scmi_debug_info *dbg = d; | |
2839 | ||
2840 | if (!dbg) | |
2841 | return; | |
2842 | ||
2843 | debugfs_remove_recursive(dbg->top_dentry); | |
2844 | kfree(dbg->name); | |
2845 | kfree(dbg->type); | |
2846 | } | |
2847 | ||
2848 | static struct scmi_debug_info *scmi_debugfs_common_setup(struct scmi_info *info) | |
2849 | { | |
2850 | char top_dir[16]; | |
2851 | struct dentry *trans, *top_dentry; | |
2852 | struct scmi_debug_info *dbg; | |
2853 | const char *c_ptr = NULL; | |
2854 | ||
2855 | dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL); | |
2856 | if (!dbg) | |
2857 | return NULL; | |
2858 | ||
2859 | dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL); | |
2860 | if (!dbg->name) { | |
2861 | devm_kfree(info->dev, dbg); | |
2862 | return NULL; | |
2863 | } | |
2864 | ||
2865 | of_property_read_string(info->dev->of_node, "compatible", &c_ptr); | |
2866 | dbg->type = kstrdup(c_ptr, GFP_KERNEL); | |
2867 | if (!dbg->type) { | |
2868 | kfree(dbg->name); | |
2869 | devm_kfree(info->dev, dbg); | |
2870 | return NULL; | |
2871 | } | |
2872 | ||
2873 | snprintf(top_dir, 16, "%d", info->id); | |
2874 | top_dentry = debugfs_create_dir(top_dir, scmi_top_dentry); | |
2875 | trans = debugfs_create_dir("transport", top_dentry); | |
2876 | ||
2877 | dbg->is_atomic = info->desc->atomic_enabled && | |
2878 | is_transport_polling_capable(info->desc); | |
2879 | ||
2880 | debugfs_create_str("instance_name", 0400, top_dentry, | |
2881 | (char **)&dbg->name); | |
2882 | ||
2883 | debugfs_create_u32("atomic_threshold_us", 0400, top_dentry, | |
2884 | &info->atomic_threshold); | |
2885 | ||
2886 | debugfs_create_str("type", 0400, trans, (char **)&dbg->type); | |
2887 | ||
2888 | debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic); | |
2889 | ||
2890 | debugfs_create_u32("max_rx_timeout_ms", 0400, trans, | |
2891 | (u32 *)&info->desc->max_rx_timeout_ms); | |
2892 | ||
2893 | debugfs_create_u32("max_msg_size", 0400, trans, | |
2894 | (u32 *)&info->desc->max_msg_size); | |
2895 | ||
2896 | debugfs_create_u32("tx_max_msg", 0400, trans, | |
2897 | (u32 *)&info->tx_minfo.max_msg); | |
2898 | ||
2899 | debugfs_create_u32("rx_max_msg", 0400, trans, | |
2900 | (u32 *)&info->rx_minfo.max_msg); | |
2901 | ||
2902 | dbg->top_dentry = top_dentry; | |
2903 | ||
2904 | if (devm_add_action_or_reset(info->dev, | |
2905 | scmi_debugfs_common_cleanup, dbg)) { | |
2906 | scmi_debugfs_common_cleanup(dbg); | |
2907 | return NULL; | |
2908 | } | |
2909 | ||
2910 | return dbg; | |
2911 | } | |
2912 | ||
7063887b CM |
2913 | static int scmi_debugfs_raw_mode_setup(struct scmi_info *info) |
2914 | { | |
7860701d CM |
2915 | int id, num_chans = 0, ret = 0; |
2916 | struct scmi_chan_info *cinfo; | |
2917 | u8 channels[SCMI_MAX_CHANNELS] = {}; | |
2918 | DECLARE_BITMAP(protos, SCMI_MAX_CHANNELS) = {}; | |
7063887b CM |
2919 | |
2920 | if (!info->dbg) | |
2921 | return -EINVAL; | |
2922 | ||
7860701d CM |
2923 | /* Enumerate all channels to collect their ids */ |
2924 | idr_for_each_entry(&info->tx_idr, cinfo, id) { | |
2925 | /* | |
2926 | * Cannot happen, but be defensive. | |
2927 | * Zero as num_chans is ok, warn and carry on. | |
2928 | */ | |
2929 | if (num_chans >= SCMI_MAX_CHANNELS || !cinfo) { | |
2930 | dev_warn(info->dev, | |
2931 | "SCMI RAW - Error enumerating channels\n"); | |
2932 | break; | |
2933 | } | |
2934 | ||
2935 | if (!test_bit(cinfo->id, protos)) { | |
2936 | channels[num_chans++] = cinfo->id; | |
2937 | set_bit(cinfo->id, protos); | |
2938 | } | |
2939 | } | |
2940 | ||
7063887b | 2941 | info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry, |
7860701d CM |
2942 | info->id, channels, num_chans, |
2943 | info->desc, info->tx_minfo.max_msg); | |
7063887b CM |
2944 | if (IS_ERR(info->raw)) { |
2945 | dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n"); | |
2946 | ret = PTR_ERR(info->raw); | |
2947 | info->raw = NULL; | |
2948 | } | |
2949 | ||
2950 | return ret; | |
2951 | } | |
2952 | ||
aa4f886f SH |
2953 | static int scmi_probe(struct platform_device *pdev) |
2954 | { | |
2955 | int ret; | |
3a7d93d1 | 2956 | char *err_str = "probe failure\n"; |
aa4f886f SH |
2957 | struct scmi_handle *handle; |
2958 | const struct scmi_desc *desc; | |
2959 | struct scmi_info *info; | |
b2b76e97 | 2960 | bool coex = IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX); |
aa4f886f | 2961 | struct device *dev = &pdev->dev; |
bc40081d | 2962 | struct device_node *child, *np = dev->of_node; |
aa4f886f | 2963 | |
d9350f21 AP |
2964 | desc = of_device_get_match_data(dev); |
2965 | if (!desc) | |
2966 | return -EINVAL; | |
aa4f886f SH |
2967 | |
2968 | info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); | |
2969 | if (!info) | |
2970 | return -ENOMEM; | |
2971 | ||
75c86dc7 CM |
2972 | info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL); |
2973 | if (info->id < 0) | |
2974 | return info->id; | |
2975 | ||
aa4f886f SH |
2976 | info->dev = dev; |
2977 | info->desc = desc; | |
53b8c25d CM |
2978 | info->bus_nb.notifier_call = scmi_bus_notifier; |
2979 | info->dev_req_nb.notifier_call = scmi_device_request_notifier; | |
aa4f886f | 2980 | INIT_LIST_HEAD(&info->node); |
48dc16e2 CM |
2981 | idr_init(&info->protocols); |
2982 | mutex_init(&info->protocols_mtx); | |
d4f9dddd | 2983 | idr_init(&info->active_protocols); |
ee5dceda | 2984 | mutex_init(&info->devreq_mtx); |
aa4f886f | 2985 | |
aa4f886f | 2986 | platform_set_drvdata(pdev, info); |
907b6d14 | 2987 | idr_init(&info->tx_idr); |
46cc7c28 | 2988 | idr_init(&info->rx_idr); |
aa4f886f SH |
2989 | |
2990 | handle = &info->handle; | |
2991 | handle->dev = info->dev; | |
b6f20ff8 | 2992 | handle->version = &info->version; |
d9107999 | 2993 | handle->devm_protocol_acquire = scmi_devm_protocol_acquire; |
23934efe CM |
2994 | handle->devm_protocol_get = scmi_devm_protocol_get; |
2995 | handle->devm_protocol_put = scmi_devm_protocol_put; | |
aa4f886f | 2996 | |
05976c5f CM |
2997 | /* System wide atomic threshold for atomic ops .. if any */ |
2998 | if (!of_property_read_u32(np, "atomic-threshold-us", | |
2999 | &info->atomic_threshold)) | |
3000 | dev_info(dev, | |
3001 | "SCMI System wide atomic threshold set to %d us\n", | |
3002 | info->atomic_threshold); | |
69255e74 | 3003 | handle->is_transport_atomic = scmi_is_transport_atomic; |
aa4f886f | 3004 | |
78852812 PH |
3005 | if (desc->ops->link_supplier) { |
3006 | ret = desc->ops->link_supplier(dev); | |
3a7d93d1 CM |
3007 | if (ret) { |
3008 | err_str = "transport not ready\n"; | |
75c86dc7 | 3009 | goto clear_ida; |
3a7d93d1 | 3010 | } |
78852812 PH |
3011 | } |
3012 | ||
05a2801d CM |
3013 | /* Setup all channels described in the DT at first */ |
3014 | ret = scmi_channels_setup(info); | |
3a7d93d1 CM |
3015 | if (ret) { |
3016 | err_str = "failed to setup channels\n"; | |
75c86dc7 | 3017 | goto clear_ida; |
3a7d93d1 | 3018 | } |
aa4f886f | 3019 | |
53b8c25d | 3020 | ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb); |
3a7d93d1 CM |
3021 | if (ret) { |
3022 | err_str = "failed to register bus notifier\n"; | |
1e7cbfaa | 3023 | goto clear_txrx_setup; |
3a7d93d1 | 3024 | } |
4ebd8f6d | 3025 | |
53b8c25d CM |
3026 | ret = blocking_notifier_chain_register(&scmi_requested_devices_nh, |
3027 | &info->dev_req_nb); | |
3a7d93d1 CM |
3028 | if (ret) { |
3029 | err_str = "failed to register device notifier\n"; | |
53b8c25d | 3030 | goto clear_bus_notifier; |
3a7d93d1 | 3031 | } |
53b8c25d CM |
3032 | |
3033 | ret = scmi_xfer_info_init(info); | |
3a7d93d1 CM |
3034 | if (ret) { |
3035 | err_str = "failed to init xfers pool\n"; | |
53b8c25d | 3036 | goto clear_dev_req_notifier; |
3a7d93d1 | 3037 | } |
53b8c25d | 3038 | |
c3d4aed7 CM |
3039 | if (scmi_top_dentry) { |
3040 | info->dbg = scmi_debugfs_common_setup(info); | |
3041 | if (!info->dbg) | |
3042 | dev_warn(dev, "Failed to setup SCMI debugfs.\n"); | |
7063887b CM |
3043 | |
3044 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) { | |
3045 | ret = scmi_debugfs_raw_mode_setup(info); | |
9c54633e CM |
3046 | if (!coex) { |
3047 | if (ret) | |
3048 | goto clear_dev_req_notifier; | |
3049 | ||
8ab5059d DC |
3050 | /* Bail out anyway when coex disabled. */ |
3051 | return 0; | |
9c54633e | 3052 | } |
7063887b | 3053 | |
9c54633e CM |
3054 | /* Coex enabled, carry on in any case. */ |
3055 | dev_info(dev, "SCMI RAW Mode COEX enabled !\n"); | |
7063887b | 3056 | } |
c3d4aed7 CM |
3057 | } |
3058 | ||
6b8a6913 CM |
3059 | if (scmi_notification_init(handle)) |
3060 | dev_err(dev, "SCMI Notifications NOT available.\n"); | |
3061 | ||
f21c2b0b CM |
3062 | if (info->desc->atomic_enabled && |
3063 | !is_transport_polling_capable(info->desc)) | |
69255e74 CM |
3064 | dev_err(dev, |
3065 | "Transport is not polling capable. Atomic mode not supported.\n"); | |
3066 | ||
8d3581c2 CM |
3067 | /* |
3068 | * Trigger SCMI Base protocol initialization. | |
3069 | * It's mandatory and won't be ever released/deinit until the | |
3070 | * SCMI stack is shutdown/unloaded as a whole. | |
3071 | */ | |
3072 | ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE); | |
b6f20ff8 | 3073 | if (ret) { |
3a7d93d1 CM |
3074 | err_str = "unable to communicate with SCMI\n"; |
3075 | if (coex) { | |
495667d4 | 3076 | dev_err(dev, "%s", err_str); |
b2b76e97 | 3077 | return 0; |
3a7d93d1 | 3078 | } |
1e7cbfaa | 3079 | goto notification_exit; |
b6f20ff8 SH |
3080 | } |
3081 | ||
aa4f886f SH |
3082 | mutex_lock(&scmi_list_mutex); |
3083 | list_add_tail(&info->node, &scmi_list); | |
3084 | mutex_unlock(&scmi_list_mutex); | |
3085 | ||
bc40081d SH |
3086 | for_each_available_child_of_node(np, child) { |
3087 | u32 prot_id; | |
3088 | ||
3089 | if (of_property_read_u32(child, "reg", &prot_id)) | |
3090 | continue; | |
3091 | ||
354b2e36 SH |
3092 | if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id)) |
3093 | dev_err(dev, "Out of range protocol %d\n", prot_id); | |
bc40081d SH |
3094 | |
3095 | if (!scmi_is_protocol_implemented(handle, prot_id)) { | |
3096 | dev_err(dev, "SCMI protocol %d not implemented\n", | |
3097 | prot_id); | |
3098 | continue; | |
3099 | } | |
3100 | ||
d4f9dddd CM |
3101 | /* |
3102 | * Save this valid DT protocol descriptor amongst | |
3103 | * @active_protocols for this SCMI instance/ | |
3104 | */ | |
3105 | ret = idr_alloc(&info->active_protocols, child, | |
3106 | prot_id, prot_id + 1, GFP_KERNEL); | |
3107 | if (ret != prot_id) { | |
3108 | dev_err(dev, "SCMI protocol %d already activated. Skip\n", | |
3109 | prot_id); | |
3110 | continue; | |
3111 | } | |
3112 | ||
3113 | of_node_get(child); | |
ee5dceda | 3114 | scmi_create_protocol_devices(child, info, prot_id, NULL); |
bc40081d SH |
3115 | } |
3116 | ||
aa4f886f | 3117 | return 0; |
1e7cbfaa RB |
3118 | |
3119 | notification_exit: | |
7063887b CM |
3120 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) |
3121 | scmi_raw_mode_cleanup(info->raw); | |
1e7cbfaa | 3122 | scmi_notification_exit(&info->handle); |
53b8c25d CM |
3123 | clear_dev_req_notifier: |
3124 | blocking_notifier_chain_unregister(&scmi_requested_devices_nh, | |
3125 | &info->dev_req_nb); | |
3126 | clear_bus_notifier: | |
3127 | bus_unregister_notifier(&scmi_bus_type, &info->bus_nb); | |
1e7cbfaa RB |
3128 | clear_txrx_setup: |
3129 | scmi_cleanup_txrx_channels(info); | |
75c86dc7 CM |
3130 | clear_ida: |
3131 | ida_free(&scmi_id, info->id); | |
3a7d93d1 | 3132 | |
495667d4 | 3133 | return dev_err_probe(dev, ret, "%s", err_str); |
aa4f886f SH |
3134 | } |
3135 | ||
927e1130 | 3136 | static void scmi_remove(struct platform_device *pdev) |
2747a967 | 3137 | { |
05a2801d | 3138 | int id; |
2747a967 | 3139 | struct scmi_info *info = platform_get_drvdata(pdev); |
d4f9dddd | 3140 | struct device_node *child; |
2747a967 | 3141 | |
7063887b CM |
3142 | if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) |
3143 | scmi_raw_mode_cleanup(info->raw); | |
3144 | ||
2747a967 SH |
3145 | mutex_lock(&scmi_list_mutex); |
3146 | if (info->users) | |
3f4071cb CM |
3147 | dev_warn(&pdev->dev, |
3148 | "Still active SCMI users will be forcibly unbound.\n"); | |
3149 | list_del(&info->node); | |
2747a967 SH |
3150 | mutex_unlock(&scmi_list_mutex); |
3151 | ||
a90b6543 CM |
3152 | scmi_notification_exit(&info->handle); |
3153 | ||
48dc16e2 CM |
3154 | mutex_lock(&info->protocols_mtx); |
3155 | idr_destroy(&info->protocols); | |
3156 | mutex_unlock(&info->protocols_mtx); | |
3157 | ||
d4f9dddd CM |
3158 | idr_for_each_entry(&info->active_protocols, child, id) |
3159 | of_node_put(child); | |
3160 | idr_destroy(&info->active_protocols); | |
3161 | ||
53b8c25d CM |
3162 | blocking_notifier_chain_unregister(&scmi_requested_devices_nh, |
3163 | &info->dev_req_nb); | |
3164 | bus_unregister_notifier(&scmi_bus_type, &info->bus_nb); | |
3165 | ||
2747a967 | 3166 | /* Safe to free channels since no more users */ |
05a2801d | 3167 | scmi_cleanup_txrx_channels(info); |
3f4071cb | 3168 | |
75c86dc7 | 3169 | ida_free(&scmi_id, info->id); |
2747a967 SH |
3170 | } |
3171 | ||
4605e224 SH |
3172 | static ssize_t protocol_version_show(struct device *dev, |
3173 | struct device_attribute *attr, char *buf) | |
3174 | { | |
3175 | struct scmi_info *info = dev_get_drvdata(dev); | |
3176 | ||
3177 | return sprintf(buf, "%u.%u\n", info->version.major_ver, | |
3178 | info->version.minor_ver); | |
3179 | } | |
3180 | static DEVICE_ATTR_RO(protocol_version); | |
3181 | ||
3182 | static ssize_t firmware_version_show(struct device *dev, | |
3183 | struct device_attribute *attr, char *buf) | |
3184 | { | |
3185 | struct scmi_info *info = dev_get_drvdata(dev); | |
3186 | ||
3187 | return sprintf(buf, "0x%x\n", info->version.impl_ver); | |
3188 | } | |
3189 | static DEVICE_ATTR_RO(firmware_version); | |
3190 | ||
3191 | static ssize_t vendor_id_show(struct device *dev, | |
3192 | struct device_attribute *attr, char *buf) | |
3193 | { | |
3194 | struct scmi_info *info = dev_get_drvdata(dev); | |
3195 | ||
3196 | return sprintf(buf, "%s\n", info->version.vendor_id); | |
3197 | } | |
3198 | static DEVICE_ATTR_RO(vendor_id); | |
3199 | ||
3200 | static ssize_t sub_vendor_id_show(struct device *dev, | |
3201 | struct device_attribute *attr, char *buf) | |
3202 | { | |
3203 | struct scmi_info *info = dev_get_drvdata(dev); | |
3204 | ||
3205 | return sprintf(buf, "%s\n", info->version.sub_vendor_id); | |
3206 | } | |
3207 | static DEVICE_ATTR_RO(sub_vendor_id); | |
3208 | ||
3209 | static struct attribute *versions_attrs[] = { | |
3210 | &dev_attr_firmware_version.attr, | |
3211 | &dev_attr_protocol_version.attr, | |
3212 | &dev_attr_vendor_id.attr, | |
3213 | &dev_attr_sub_vendor_id.attr, | |
3214 | NULL, | |
3215 | }; | |
3216 | ATTRIBUTE_GROUPS(versions); | |
3217 | ||
2747a967 SH |
3218 | /* Each compatible listed below must have descriptor associated with it */ |
3219 | static const struct of_device_id scmi_of_match[] = { | |
e8419c24 | 3220 | #ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX |
5c8a47a5 | 3221 | { .compatible = "arm,scmi", .data = &scmi_mailbox_desc }, |
ab7766b7 | 3222 | #endif |
5f90f189 EC |
3223 | #ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE |
3224 | { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc }, | |
3225 | #endif | |
e8419c24 | 3226 | #ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC |
1dc65580 | 3227 | { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc}, |
5f2ea10a | 3228 | { .compatible = "arm,scmi-smc-param", .data = &scmi_smc_desc}, |
da405477 | 3229 | { .compatible = "qcom,scmi-smc", .data = &scmi_smc_desc}, |
46abe13b IS |
3230 | #endif |
3231 | #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO | |
3232 | { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc}, | |
1dc65580 | 3233 | #endif |
2747a967 SH |
3234 | { /* Sentinel */ }, |
3235 | }; | |
3236 | ||
3237 | MODULE_DEVICE_TABLE(of, scmi_of_match); | |
3238 | ||
aa4f886f SH |
3239 | static struct platform_driver scmi_driver = { |
3240 | .driver = { | |
3241 | .name = "arm-scmi", | |
fd96fbc8 | 3242 | .suppress_bind_attrs = true, |
aa4f886f | 3243 | .of_match_table = scmi_of_match, |
4605e224 | 3244 | .dev_groups = versions_groups, |
aa4f886f SH |
3245 | }, |
3246 | .probe = scmi_probe, | |
927e1130 | 3247 | .remove_new = scmi_remove, |
aa4f886f SH |
3248 | }; |
3249 | ||
ceac257d CM |
3250 | /** |
3251 | * __scmi_transports_setup - Common helper to call transport-specific | |
3252 | * .init/.exit code if provided. | |
3253 | * | |
3254 | * @init: A flag to distinguish between init and exit. | |
3255 | * | |
3256 | * Note that, if provided, we invoke .init/.exit functions for all the | |
3257 | * transports currently compiled in. | |
3258 | * | |
3259 | * Return: 0 on Success. | |
3260 | */ | |
3261 | static inline int __scmi_transports_setup(bool init) | |
3262 | { | |
3263 | int ret = 0; | |
3264 | const struct of_device_id *trans; | |
3265 | ||
3266 | for (trans = scmi_of_match; trans->data; trans++) { | |
3267 | const struct scmi_desc *tdesc = trans->data; | |
3268 | ||
3269 | if ((init && !tdesc->transport_init) || | |
3270 | (!init && !tdesc->transport_exit)) | |
3271 | continue; | |
3272 | ||
3273 | if (init) | |
3274 | ret = tdesc->transport_init(); | |
3275 | else | |
3276 | tdesc->transport_exit(); | |
3277 | ||
3278 | if (ret) { | |
3279 | pr_err("SCMI transport %s FAILED initialization!\n", | |
3280 | trans->compatible); | |
3281 | break; | |
3282 | } | |
3283 | } | |
3284 | ||
3285 | return ret; | |
3286 | } | |
3287 | ||
3288 | static int __init scmi_transports_init(void) | |
3289 | { | |
3290 | return __scmi_transports_setup(true); | |
3291 | } | |
3292 | ||
3293 | static void __exit scmi_transports_exit(void) | |
3294 | { | |
3295 | __scmi_transports_setup(false); | |
3296 | } | |
3297 | ||
c3d4aed7 CM |
3298 | static struct dentry *scmi_debugfs_init(void) |
3299 | { | |
3300 | struct dentry *d; | |
3301 | ||
3302 | d = debugfs_create_dir("scmi", NULL); | |
3303 | if (IS_ERR(d)) { | |
3304 | pr_err("Could NOT create SCMI top dentry.\n"); | |
3305 | return NULL; | |
3306 | } | |
3307 | ||
3308 | return d; | |
3309 | } | |
3310 | ||
5a2f0a0b SH |
3311 | static int __init scmi_driver_init(void) |
3312 | { | |
ceac257d CM |
3313 | int ret; |
3314 | ||
c0397c85 CM |
3315 | /* Bail out if no SCMI transport was configured */ |
3316 | if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT))) | |
3317 | return -EINVAL; | |
5a2f0a0b | 3318 | |
ceac257d CM |
3319 | /* Initialize any compiled-in transport which provided an init/exit */ |
3320 | ret = scmi_transports_init(); | |
3321 | if (ret) | |
3322 | return ret; | |
3323 | ||
c3d4aed7 CM |
3324 | if (IS_ENABLED(CONFIG_ARM_SCMI_NEED_DEBUGFS)) |
3325 | scmi_top_dentry = scmi_debugfs_init(); | |
3326 | ||
48dc16e2 CM |
3327 | scmi_base_register(); |
3328 | ||
1eaf18e3 SH |
3329 | scmi_clock_register(); |
3330 | scmi_perf_register(); | |
3331 | scmi_power_register(); | |
3332 | scmi_reset_register(); | |
3333 | scmi_sensors_register(); | |
2add5cac | 3334 | scmi_voltage_register(); |
1eaf18e3 | 3335 | scmi_system_register(); |
0316f99c | 3336 | scmi_powercap_register(); |
1eaf18e3 | 3337 | |
5a2f0a0b SH |
3338 | return platform_driver_register(&scmi_driver); |
3339 | } | |
37b5be82 | 3340 | module_init(scmi_driver_init); |
5a2f0a0b SH |
3341 | |
3342 | static void __exit scmi_driver_exit(void) | |
3343 | { | |
48dc16e2 | 3344 | scmi_base_unregister(); |
5a2f0a0b | 3345 | |
1eaf18e3 SH |
3346 | scmi_clock_unregister(); |
3347 | scmi_perf_unregister(); | |
3348 | scmi_power_unregister(); | |
3349 | scmi_reset_unregister(); | |
3350 | scmi_sensors_unregister(); | |
2add5cac | 3351 | scmi_voltage_unregister(); |
1eaf18e3 | 3352 | scmi_system_unregister(); |
0316f99c | 3353 | scmi_powercap_unregister(); |
1eaf18e3 | 3354 | |
ceac257d CM |
3355 | scmi_transports_exit(); |
3356 | ||
5a2f0a0b | 3357 | platform_driver_unregister(&scmi_driver); |
c3d4aed7 CM |
3358 | |
3359 | debugfs_remove_recursive(scmi_top_dentry); | |
5a2f0a0b SH |
3360 | } |
3361 | module_exit(scmi_driver_exit); | |
aa4f886f | 3362 | |
1ba603f5 | 3363 | MODULE_ALIAS("platform:arm-scmi"); |
aa4f886f SH |
3364 | MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); |
3365 | MODULE_DESCRIPTION("ARM SCMI protocol driver"); | |
3366 | MODULE_LICENSE("GPL v2"); |