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