1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * System Control and Management Interface (SCMI) Message Protocol 4 * driver common header file containing some definitions, structures 5 * and function prototypes used in all the different SCMI protocols. 6 * 7 * Copyright (C) 2018-2024 ARM Ltd. 8 */ 9 #ifndef _SCMI_COMMON_H 10 #define _SCMI_COMMON_H 11 12 #include <linux/bitfield.h> 13 #include <linux/completion.h> 14 #include <linux/device.h> 15 #include <linux/errno.h> 16 #include <linux/kernel.h> 17 #include <linux/hashtable.h> 18 #include <linux/list.h> 19 #include <linux/module.h> 20 #include <linux/property.h> 21 #include <linux/refcount.h> 22 #include <linux/scmi_protocol.h> 23 #include <linux/spinlock.h> 24 #include <linux/types.h> 25 26 #include <linux/unaligned.h> 27 28 #include "protocols.h" 29 #include "notify.h" 30 31 #define SCMI_MAX_CHANNELS 256 32 33 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC) 34 35 #define SCMI_SHMEM_MAX_PAYLOAD_SIZE 104 36 37 #define SCMI_TRANSPORT_DEVNAME_PREFIX "__scmi_transport_device" 38 39 enum scmi_error_codes { 40 SCMI_SUCCESS = 0, /* Success */ 41 SCMI_ERR_SUPPORT = -1, /* Not supported */ 42 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */ 43 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */ 44 SCMI_ERR_ENTRY = -4, /* Not found */ 45 SCMI_ERR_RANGE = -5, /* Value out of range */ 46 SCMI_ERR_BUSY = -6, /* Device busy */ 47 SCMI_ERR_COMMS = -7, /* Communication Error */ 48 SCMI_ERR_GENERIC = -8, /* Generic Error */ 49 SCMI_ERR_HARDWARE = -9, /* Hardware Error */ 50 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */ 51 }; 52 53 static const int scmi_linux_errmap[] = { 54 /* better than switch case as long as return value is continuous */ 55 0, /* SCMI_SUCCESS */ 56 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */ 57 -EINVAL, /* SCMI_ERR_PARAM */ 58 -EACCES, /* SCMI_ERR_ACCESS */ 59 -ENOENT, /* SCMI_ERR_ENTRY */ 60 -ERANGE, /* SCMI_ERR_RANGE */ 61 -EBUSY, /* SCMI_ERR_BUSY */ 62 -ECOMM, /* SCMI_ERR_COMMS */ 63 -EIO, /* SCMI_ERR_GENERIC */ 64 -EREMOTEIO, /* SCMI_ERR_HARDWARE */ 65 -EPROTO, /* SCMI_ERR_PROTOCOL */ 66 }; 67 68 static inline int scmi_to_linux_errno(int errno) 69 { 70 int err_idx = -errno; 71 72 if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap)) 73 return scmi_linux_errmap[err_idx]; 74 return -EIO; 75 } 76 77 #define MSG_ID_MASK GENMASK(7, 0) 78 #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr)) 79 #define MSG_TYPE_MASK GENMASK(9, 8) 80 #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr)) 81 #define MSG_TYPE_COMMAND 0 82 #define MSG_TYPE_DELAYED_RESP 2 83 #define MSG_TYPE_NOTIFICATION 3 84 #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10) 85 #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr)) 86 #define MSG_TOKEN_ID_MASK GENMASK(27, 18) 87 #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr)) 88 #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1) 89 90 /* 91 * Size of @pending_xfers hashtable included in @scmi_xfers_info; ideally, in 92 * order to minimize space and collisions, this should equal max_msg, i.e. the 93 * maximum number of in-flight messages on a specific platform, but such value 94 * is only available at runtime while kernel hashtables are statically sized: 95 * pick instead as a fixed static size the maximum number of entries that can 96 * fit the whole table into one 4k page. 97 */ 98 #define SCMI_PENDING_XFERS_HT_ORDER_SZ 9 99 100 /** 101 * pack_scmi_header() - packs and returns 32-bit header 102 * 103 * @hdr: pointer to header containing all the information on message id, 104 * protocol id, sequence id and type. 105 * 106 * Return: 32-bit packed message header to be sent to the platform. 107 */ 108 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr) 109 { 110 return FIELD_PREP(MSG_ID_MASK, hdr->id) | 111 FIELD_PREP(MSG_TYPE_MASK, hdr->type) | 112 FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) | 113 FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id); 114 } 115 116 /** 117 * unpack_scmi_header() - unpacks and records message and protocol id 118 * 119 * @msg_hdr: 32-bit packed message header sent from the platform 120 * @hdr: pointer to header to fetch message and protocol id. 121 */ 122 static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr) 123 { 124 hdr->id = MSG_XTRACT_ID(msg_hdr); 125 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr); 126 hdr->type = MSG_XTRACT_TYPE(msg_hdr); 127 } 128 129 /* 130 * An helper macro to lookup an xfer from the @pending_xfers hashtable 131 * using the message sequence number token as a key. 132 */ 133 #define XFER_FIND(__ht, __k) \ 134 ({ \ 135 typeof(__k) k_ = __k; \ 136 struct scmi_xfer *xfer_ = NULL; \ 137 \ 138 hash_for_each_possible((__ht), xfer_, node, k_) \ 139 if (xfer_->hdr.seq == k_) \ 140 break; \ 141 xfer_; \ 142 }) 143 144 struct scmi_base_info * 145 scmi_revision_area_get(const struct scmi_protocol_handle *ph); 146 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph, 147 u8 *prot_imp); 148 149 extern const struct bus_type scmi_bus_type; 150 151 #define SCMI_BUS_NOTIFY_DEVICE_REQUEST 0 152 #define SCMI_BUS_NOTIFY_DEVICE_UNREQUEST 1 153 extern struct blocking_notifier_head scmi_requested_devices_nh; 154 155 struct scmi_device *scmi_device_create(struct device_node *np, 156 struct device *parent, int protocol, 157 const char *name); 158 void scmi_device_destroy(struct device *parent, int protocol, const char *name); 159 160 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id); 161 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id); 162 163 /* SCMI Transport */ 164 /** 165 * struct scmi_chan_info - Structure representing a SCMI channel information 166 * 167 * @id: An identifier for this channel: this matches the protocol number 168 * used to initialize this channel 169 * @dev: Reference to device in the SCMI hierarchy corresponding to this 170 * channel 171 * @is_p2a: A flag to identify a channel as P2A (RX) 172 * @rx_timeout_ms: The configured RX timeout in milliseconds. 173 * @max_msg_size: Maximum size of message payload. 174 * @handle: Pointer to SCMI entity handle 175 * @no_completion_irq: Flag to indicate that this channel has no completion 176 * interrupt mechanism for synchronous commands. 177 * This can be dynamically set by transports at run-time 178 * inside their provided .chan_setup(). 179 * @transport_info: Transport layer related information 180 */ 181 struct scmi_chan_info { 182 int id; 183 struct device *dev; 184 bool is_p2a; 185 unsigned int rx_timeout_ms; 186 unsigned int max_msg_size; 187 struct scmi_handle *handle; 188 bool no_completion_irq; 189 void *transport_info; 190 }; 191 192 /** 193 * struct scmi_transport_ops - Structure representing a SCMI transport ops 194 * 195 * @chan_available: Callback to check if channel is available or not 196 * @chan_setup: Callback to allocate and setup a channel 197 * @chan_free: Callback to free a channel 198 * @get_max_msg: Optional callback to provide max_msg dynamically 199 * Returns the maximum number of messages for the channel type 200 * (tx or rx) that can be pending simultaneously in the system 201 * @send_message: Callback to send a message 202 * @mark_txdone: Callback to mark tx as done 203 * @fetch_response: Callback to fetch response 204 * @fetch_notification: Callback to fetch notification 205 * @clear_channel: Callback to clear a channel 206 * @poll_done: Callback to poll transfer status 207 */ 208 struct scmi_transport_ops { 209 bool (*chan_available)(struct device_node *of_node, int idx); 210 int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev, 211 bool tx); 212 int (*chan_free)(int id, void *p, void *data); 213 unsigned int (*get_max_msg)(struct scmi_chan_info *base_cinfo); 214 int (*send_message)(struct scmi_chan_info *cinfo, 215 struct scmi_xfer *xfer); 216 void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret, 217 struct scmi_xfer *xfer); 218 void (*fetch_response)(struct scmi_chan_info *cinfo, 219 struct scmi_xfer *xfer); 220 void (*fetch_notification)(struct scmi_chan_info *cinfo, 221 size_t max_len, struct scmi_xfer *xfer); 222 void (*clear_channel)(struct scmi_chan_info *cinfo); 223 bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer); 224 }; 225 226 /** 227 * struct scmi_desc - Description of SoC integration 228 * 229 * @ops: Pointer to the transport specific ops structure 230 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) 231 * @max_msg: Maximum number of messages for a channel type (tx or rx) that can 232 * be pending simultaneously in the system. May be overridden by the 233 * get_max_msg op. 234 * @max_msg_size: Maximum size of data payload per message that can be handled. 235 * @atomic_threshold: Optional system wide DT-configured threshold, expressed 236 * in microseconds, for atomic operations. 237 * Only SCMI synchronous commands reported by the platform 238 * to have an execution latency lesser-equal to the threshold 239 * should be considered for atomic mode operation: such 240 * decision is finally left up to the SCMI drivers. 241 * @no_completion_irq: Flag to indicate that this transport has no completion 242 * interrupt and has to be polled. This is similar to the 243 * force_polling below, except this is set via DT property. 244 * @force_polling: Flag to force this whole transport to use SCMI core polling 245 * mechanism instead of completion interrupts even if available. 246 * @sync_cmds_completed_on_ret: Flag to indicate that the transport assures 247 * synchronous-command messages are atomically 248 * completed on .send_message: no need to poll 249 * actively waiting for a response. 250 * Used by core internally only when polling is 251 * selected as a waiting for reply method: i.e. 252 * if a completion irq was found use that anyway. 253 * @atomic_enabled: Flag to indicate that this transport, which is assured not 254 * to sleep anywhere on the TX path, can be used in atomic mode 255 * when requested. 256 */ 257 struct scmi_desc { 258 const struct scmi_transport_ops *ops; 259 int max_rx_timeout_ms; 260 int max_msg; 261 int max_msg_size; 262 unsigned int atomic_threshold; 263 bool no_completion_irq; 264 const bool force_polling; 265 const bool sync_cmds_completed_on_ret; 266 const bool atomic_enabled; 267 }; 268 269 static inline bool is_polling_required(struct scmi_chan_info *cinfo, 270 const struct scmi_desc *desc) 271 { 272 return cinfo->no_completion_irq || desc->force_polling; 273 } 274 275 static inline bool is_transport_polling_capable(const struct scmi_desc *desc) 276 { 277 return desc->ops->poll_done || desc->sync_cmds_completed_on_ret; 278 } 279 280 static inline bool is_polling_enabled(struct scmi_chan_info *cinfo, 281 const struct scmi_desc *desc) 282 { 283 return is_polling_required(cinfo, desc) && 284 is_transport_polling_capable(desc); 285 } 286 287 void scmi_xfer_raw_put(const struct scmi_handle *handle, 288 struct scmi_xfer *xfer); 289 struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle); 290 struct scmi_chan_info * 291 scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id); 292 293 int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle, 294 struct scmi_xfer *xfer); 295 296 int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo, 297 struct scmi_xfer *xfer, 298 unsigned int timeout_ms); 299 300 enum debug_counters { 301 SENT_OK, 302 SENT_FAIL, 303 SENT_FAIL_POLLING_UNSUPPORTED, 304 SENT_FAIL_CHANNEL_NOT_FOUND, 305 RESPONSE_OK, 306 NOTIFICATION_OK, 307 DELAYED_RESPONSE_OK, 308 XFERS_RESPONSE_TIMEOUT, 309 XFERS_RESPONSE_POLLED_TIMEOUT, 310 RESPONSE_POLLED_OK, 311 ERR_MSG_UNEXPECTED, 312 ERR_MSG_INVALID, 313 ERR_MSG_NOMEM, 314 ERR_PROTOCOL, 315 XFERS_INFLIGHT, 316 SCMI_DEBUG_COUNTERS_LAST 317 }; 318 319 /** 320 * struct scmi_debug_info - Debug common info 321 * @top_dentry: A reference to the top debugfs dentry 322 * @name: Name of this SCMI instance 323 * @type: Type of this SCMI instance 324 * @is_atomic: Flag to state if the transport of this instance is atomic 325 * @counters: An array of atomic_c's used for tracking statistics (if enabled) 326 */ 327 struct scmi_debug_info { 328 struct dentry *top_dentry; 329 const char *name; 330 const char *type; 331 bool is_atomic; 332 atomic_t counters[SCMI_DEBUG_COUNTERS_LAST]; 333 }; 334 335 static inline void scmi_inc_count(struct scmi_debug_info *dbg, int stat) 336 { 337 if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS)) { 338 if (dbg) 339 atomic_inc(&dbg->counters[stat]); 340 } 341 } 342 343 static inline void scmi_dec_count(struct scmi_debug_info *dbg, int stat) 344 { 345 if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS)) { 346 if (dbg) 347 atomic_dec(&dbg->counters[stat]); 348 } 349 } 350 351 enum scmi_bad_msg { 352 MSG_UNEXPECTED = -1, 353 MSG_INVALID = -2, 354 MSG_UNKNOWN = -3, 355 MSG_NOMEM = -4, 356 MSG_MBOX_SPURIOUS = -5, 357 }; 358 359 /* Used for compactness and signature validation of the function pointers being 360 * passed. 361 */ 362 typedef void (*shmem_copy_toio_t)(void __iomem *to, const void *from, 363 size_t count); 364 typedef void (*shmem_copy_fromio_t)(void *to, const void __iomem *from, 365 size_t count); 366 367 /** 368 * struct scmi_shmem_io_ops - I/O operations to read from/write to 369 * Shared Memory 370 * 371 * @toio: Copy data to the shared memory area 372 * @fromio: Copy data from the shared memory area 373 */ 374 struct scmi_shmem_io_ops { 375 shmem_copy_fromio_t fromio; 376 shmem_copy_toio_t toio; 377 }; 378 379 /* shmem related declarations */ 380 struct scmi_shared_mem; 381 382 /** 383 * struct scmi_shared_mem_operations - Transport core operations for 384 * Shared Memory 385 * 386 * @tx_prepare: Prepare the @xfer message for transmission on the chosen @shmem 387 * @read_header: Read header of the message currently hold in @shmem 388 * @fetch_response: Copy the message response from @shmem into @xfer 389 * @fetch_notification: Copy the message notification from @shmem into @xfer 390 * @clear_channel: Clear the @shmem channel busy flag 391 * @poll_done: Check if poll has completed for @xfer on @shmem 392 * @channel_free: Check if @shmem channel is marked as free 393 * @channel_intr_enabled: Check is @shmem channel has requested a completion irq 394 * @setup_iomap: Setup IO shared memory for channel @cinfo 395 */ 396 struct scmi_shared_mem_operations { 397 void (*tx_prepare)(struct scmi_shared_mem __iomem *shmem, 398 struct scmi_xfer *xfer, 399 struct scmi_chan_info *cinfo, 400 shmem_copy_toio_t toio); 401 u32 (*read_header)(struct scmi_shared_mem __iomem *shmem); 402 403 void (*fetch_response)(struct scmi_shared_mem __iomem *shmem, 404 struct scmi_xfer *xfer, 405 shmem_copy_fromio_t fromio); 406 void (*fetch_notification)(struct scmi_shared_mem __iomem *shmem, 407 size_t max_len, struct scmi_xfer *xfer, 408 shmem_copy_fromio_t fromio); 409 void (*clear_channel)(struct scmi_shared_mem __iomem *shmem); 410 bool (*poll_done)(struct scmi_shared_mem __iomem *shmem, 411 struct scmi_xfer *xfer); 412 bool (*channel_free)(struct scmi_shared_mem __iomem *shmem); 413 bool (*channel_intr_enabled)(struct scmi_shared_mem __iomem *shmem); 414 void __iomem *(*setup_iomap)(struct scmi_chan_info *cinfo, 415 struct device *dev, 416 bool tx, struct resource *res, 417 struct scmi_shmem_io_ops **ops); 418 }; 419 420 const struct scmi_shared_mem_operations *scmi_shared_mem_operations_get(void); 421 422 /* declarations for message passing transports */ 423 struct scmi_msg_payld; 424 425 /* Maximum overhead of message w.r.t. struct scmi_desc.max_msg_size */ 426 #define SCMI_MSG_MAX_PROT_OVERHEAD (2 * sizeof(__le32)) 427 428 /** 429 * struct scmi_message_operations - Transport core operations for Message 430 * 431 * @response_size: Get calculated response size for @xfer 432 * @command_size: Get calculated command size for @xfer 433 * @tx_prepare: Prepare the @xfer message for transmission on the provided @msg 434 * @read_header: Read header of the message currently hold in @msg 435 * @fetch_response: Copy the message response from @msg into @xfer 436 * @fetch_notification: Copy the message notification from @msg into @xfer 437 */ 438 struct scmi_message_operations { 439 size_t (*response_size)(struct scmi_xfer *xfer); 440 size_t (*command_size)(struct scmi_xfer *xfer); 441 void (*tx_prepare)(struct scmi_msg_payld *msg, struct scmi_xfer *xfer); 442 u32 (*read_header)(struct scmi_msg_payld *msg); 443 void (*fetch_response)(struct scmi_msg_payld *msg, size_t len, 444 struct scmi_xfer *xfer); 445 void (*fetch_notification)(struct scmi_msg_payld *msg, size_t len, 446 size_t max_len, struct scmi_xfer *xfer); 447 }; 448 449 const struct scmi_message_operations *scmi_message_operations_get(void); 450 451 /** 452 * struct scmi_transport_core_operations - Transpoert core operations 453 * 454 * @bad_message_trace: An helper to report a malformed/unexpected message 455 * @rx_callback: Callback to report received messages 456 * @shmem: Datagram operations for shared memory based transports 457 * @msg: Datagram operations for message based transports 458 */ 459 struct scmi_transport_core_operations { 460 void (*bad_message_trace)(struct scmi_chan_info *cinfo, 461 u32 msg_hdr, enum scmi_bad_msg err); 462 void (*rx_callback)(struct scmi_chan_info *cinfo, u32 msg_hdr, 463 void *priv); 464 const struct scmi_shared_mem_operations *shmem; 465 const struct scmi_message_operations *msg; 466 }; 467 468 /** 469 * struct scmi_transport_handle - Transport instance handle 470 * @supplier_get: A helper to retrieve the device descriptor, identifying the 471 * transport driver serving this SCMI instance, which will be 472 * used as a supplier for the core SCMI driver: returning an 473 * error here causes the probe sequence to be interrupted and 474 * return that same error code, so that each transport can decide 475 * which policy to implement by choosing an appropriate error. 476 * @supplier_put: A helper to signal that the specified transport supplier is 477 * no more being used and it is made available again. 478 * 479 * Note that these helpers are needed and provided only by those transports 480 * whose initialization relies on some other subsystem and whose relations to 481 * the core SCMI driver is not tracked by firmware descriptions. 482 */ 483 struct scmi_transport_handle { 484 struct device __must_check *(*supplier_get) 485 (const struct scmi_transport_handle *th); 486 int (*supplier_put)(const struct scmi_transport_handle *th, 487 struct device *dev); 488 }; 489 490 /** 491 * struct scmi_transport - A structure representing a configured transport 492 * 493 * @supplier: Device representing the transport and acting as a supplier for 494 * the core SCMI stack 495 * @desc: Transport descriptor 496 * @core_ops: A pointer to a pointer used by the core SCMI stack to make the 497 * core transport operations accessible to the transports. 498 * @th: An optional pointer to the transport handle 499 */ 500 struct scmi_transport { 501 struct device *supplier; 502 struct scmi_desc desc; 503 struct scmi_transport_core_operations **core_ops; 504 const struct scmi_transport_handle *th; 505 }; 506 507 /** 508 * struct scmi_transport_supplier - Transport descriptor 509 * @mtx: A mutex to protect @available 510 * @available: A reference to an initialized transport device, when available. 511 * This reference is implicitly used to track the status of the 512 * supplier and it can cycle through the following 3 states: 513 * 1. NOT_READY - PTR_ERR(-EPROBE_DEFER): no supplier available; 514 * this is the transport initial state. 515 * 2. AVAILABLE - <supplier_dev>: a transport supplier has been 516 * initialized and it is available, ready to use. 517 * 3. BUSY _ PTR_ERR(-EBUSY): transport supplier is currently in use. 518 * @th: An embedded transport handle object that embeds the helpers 519 * implementing the above mentioned logic 520 * 521 * Note that this transport driver enforces single instance probing. 522 */ 523 struct scmi_transport_supplier { 524 /* Protect @available */ 525 struct mutex mtx; 526 struct device *available; 527 const struct scmi_transport_handle th; 528 }; 529 530 #define to_sup(t) container_of(t, struct scmi_transport_supplier, th) 531 532 /** 533 * scmi_transport_supplier_put - A helper to dispose of a supplier 534 * @th: A reference to the transport handle to use 535 * @supplier: A reference to the device supplier to manage, cannot be NULL 536 * or ERR_PTR. 537 * 538 * Note that putting a supplier will have different effect based on the 539 * current state of scmi_transport_supplier.available: 540 * - NOT_READY/BUSY: @supplier will be set as the new available device: this 541 * can be used to made available a supplier OR stop using one. 542 * - AVAILABLE: if the @supplier we are disposing of matches the currently 543 * available one, roll back to NOT_READY state. 544 * Any other attempt to override an available supplier with a 545 * new one is rejected, effectively enforcing one single supplier. 546 * 547 * Return: 0 on Success, errno otherwise. 548 */ 549 static inline int 550 scmi_transport_supplier_put(const struct scmi_transport_handle *th, 551 struct device *supplier) 552 { 553 struct scmi_transport_supplier *sup = to_sup(th); 554 555 /* Nothing to do when the provided supplier was never real */ 556 if (IS_ERR_OR_NULL(supplier)) 557 return 0; 558 559 guard(mutex)(&sup->mtx); 560 switch (PTR_ERR_OR_ZERO(sup->available)) { 561 case -EPROBE_DEFER: 562 case -EBUSY: 563 sup->available = supplier; 564 break; 565 case 0: 566 /* Putting a supplier when in the AVAILABLE state causes a 567 * transition back to the NOT_READY state, BUT only if the 568 * supplier we are disposing of was exactly the device that was 569 * previously made readily available. 570 */ 571 if (supplier != sup->available) 572 return -EINVAL; 573 sup->available = ERR_PTR(-EPROBE_DEFER); 574 break; 575 default: 576 return -EINVAL; 577 } 578 579 return 0; 580 } 581 582 /** 583 * scmi_transport_supplier_get - A helper to get hold of a supplier 584 * @th: A reference to the transport handle to use 585 * 586 * Note that, trying to get a supplier device can return: 587 * - a ready to use supplier device, (subsequently made unavailable) 588 * - PTR_ERR(-EPROBE_DEFER): no supplier is available 589 * - PTR_ERR(-BUSY): supplier was already taken by a previous get 590 * 591 * This allows the probe to defer and wait when a possible device can 592 * be reasonably expected to appear. 593 * 594 * Return: a usable supplier device on Success or PTR_ERR on Failure. 595 */ 596 static inline struct device * 597 scmi_transport_supplier_get(const struct scmi_transport_handle *th) 598 { 599 struct scmi_transport_supplier *sup = to_sup(th); 600 struct device *supplier; 601 602 guard(mutex)(&sup->mtx); 603 supplier = sup->available; 604 if (!IS_ERR(sup->available)) 605 sup->available = ERR_PTR(-EBUSY); 606 607 return supplier; 608 } 609 610 #define DEFINE_SCMI_TRANSPORT_SUPPLIER(__supplier) \ 611 struct scmi_transport_supplier __supplier = { \ 612 .mtx = __MUTEX_INITIALIZER(__supplier.mtx), \ 613 .available = INIT_ERR_PTR(-EPROBE_DEFER), \ 614 .th.supplier_get = scmi_transport_supplier_get, \ 615 .th.supplier_put = scmi_transport_supplier_put, \ 616 } 617 618 #define DEFINE_SCMI_TRANSPORT_DRIVER(__tag, __drv, __desc, __match, __core_ops)\ 619 static void __tag##_dev_free(void *data) \ 620 { \ 621 struct platform_device *spdev = data; \ 622 struct scmi_transport *strans; \ 623 \ 624 strans = dev_get_platdata(&spdev->dev); \ 625 if (strans && strans->th) \ 626 strans->th->supplier_put(strans->th, strans->supplier); \ 627 \ 628 platform_device_unregister(spdev); \ 629 } \ 630 \ 631 static int __tag##_probe(struct platform_device *pdev) \ 632 { \ 633 struct device *dev = &pdev->dev, *supplier; \ 634 struct platform_device *spdev; \ 635 struct scmi_transport strans; \ 636 int ret; \ 637 \ 638 supplier = dev; \ 639 strans.th = device_get_match_data(dev); \ 640 if (strans.th) { \ 641 supplier = strans.th->supplier_get(strans.th); \ 642 if (IS_ERR(supplier)) \ 643 return PTR_ERR(supplier); \ 644 } \ 645 \ 646 spdev = platform_device_alloc("arm-scmi", PLATFORM_DEVID_AUTO); \ 647 if (!spdev) { \ 648 ret = -ENOMEM; \ 649 goto err_mem; \ 650 } \ 651 \ 652 device_set_of_node_from_dev(&spdev->dev, dev); \ 653 \ 654 strans.supplier = supplier; \ 655 memcpy(&strans.desc, &(__desc), sizeof(strans.desc)); \ 656 strans.core_ops = &(__core_ops); \ 657 \ 658 ret = platform_device_add_data(spdev, &strans, sizeof(strans)); \ 659 if (ret) \ 660 goto err; \ 661 \ 662 spdev->dev.parent = dev; \ 663 ret = platform_device_add(spdev); \ 664 if (ret) \ 665 goto err; \ 666 \ 667 return devm_add_action_or_reset(dev, __tag##_dev_free, spdev); \ 668 \ 669 err: \ 670 platform_device_put(spdev); \ 671 err_mem: \ 672 if (strans.th) \ 673 strans.th->supplier_put(strans.th, supplier); \ 674 \ 675 return ret; \ 676 } \ 677 \ 678 static struct platform_driver __drv = { \ 679 .driver = { \ 680 .name = #__tag "_transport", \ 681 .of_match_table = __match, \ 682 }, \ 683 .probe = __tag##_probe, \ 684 } 685 686 void scmi_notification_instance_data_set(const struct scmi_handle *handle, 687 void *priv); 688 void *scmi_notification_instance_data_get(const struct scmi_handle *handle); 689 int scmi_inflight_count(const struct scmi_handle *handle); 690 #endif /* _SCMI_COMMON_H */ 691