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