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