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