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 ARM Ltd. 15 */ 16 17 #include <linux/bitmap.h> 18 #include <linux/export.h> 19 #include <linux/io.h> 20 #include <linux/kernel.h> 21 #include <linux/ktime.h> 22 #include <linux/module.h> 23 #include <linux/of_address.h> 24 #include <linux/of_device.h> 25 #include <linux/processor.h> 26 #include <linux/slab.h> 27 28 #include "common.h" 29 #include "notify.h" 30 31 #define CREATE_TRACE_POINTS 32 #include <trace/events/scmi.h> 33 34 enum scmi_error_codes { 35 SCMI_SUCCESS = 0, /* Success */ 36 SCMI_ERR_SUPPORT = -1, /* Not supported */ 37 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */ 38 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */ 39 SCMI_ERR_ENTRY = -4, /* Not found */ 40 SCMI_ERR_RANGE = -5, /* Value out of range */ 41 SCMI_ERR_BUSY = -6, /* Device busy */ 42 SCMI_ERR_COMMS = -7, /* Communication Error */ 43 SCMI_ERR_GENERIC = -8, /* Generic Error */ 44 SCMI_ERR_HARDWARE = -9, /* Hardware Error */ 45 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */ 46 SCMI_ERR_MAX 47 }; 48 49 /* List of all SCMI devices active in system */ 50 static LIST_HEAD(scmi_list); 51 /* Protection for the entire list */ 52 static DEFINE_MUTEX(scmi_list_mutex); 53 /* Track the unique id for the transfers for debug & profiling purpose */ 54 static atomic_t transfer_last_id; 55 56 /** 57 * struct scmi_xfers_info - Structure to manage transfer information 58 * 59 * @xfer_block: Preallocated Message array 60 * @xfer_alloc_table: Bitmap table for allocated messages. 61 * Index of this bitmap table is also used for message 62 * sequence identifier. 63 * @xfer_lock: Protection for message allocation 64 */ 65 struct scmi_xfers_info { 66 struct scmi_xfer *xfer_block; 67 unsigned long *xfer_alloc_table; 68 spinlock_t xfer_lock; 69 }; 70 71 /** 72 * struct scmi_info - Structure representing a SCMI instance 73 * 74 * @dev: Device pointer 75 * @desc: SoC description for this instance 76 * @version: SCMI revision information containing protocol version, 77 * implementation version and (sub-)vendor identification. 78 * @handle: Instance of SCMI handle to send to clients 79 * @tx_minfo: Universal Transmit Message management info 80 * @rx_minfo: Universal Receive Message management info 81 * @tx_idr: IDR object to map protocol id to Tx channel info pointer 82 * @rx_idr: IDR object to map protocol id to Rx channel info pointer 83 * @protocols_imp: List of protocols implemented, currently maximum of 84 * MAX_PROTOCOLS_IMP elements allocated by the base protocol 85 * @node: List head 86 * @users: Number of users of this instance 87 */ 88 struct scmi_info { 89 struct device *dev; 90 const struct scmi_desc *desc; 91 struct scmi_revision_info version; 92 struct scmi_handle handle; 93 struct scmi_xfers_info tx_minfo; 94 struct scmi_xfers_info rx_minfo; 95 struct idr tx_idr; 96 struct idr rx_idr; 97 u8 *protocols_imp; 98 struct list_head node; 99 int users; 100 }; 101 102 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle) 103 104 static const int scmi_linux_errmap[] = { 105 /* better than switch case as long as return value is continuous */ 106 0, /* SCMI_SUCCESS */ 107 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */ 108 -EINVAL, /* SCMI_ERR_PARAM */ 109 -EACCES, /* SCMI_ERR_ACCESS */ 110 -ENOENT, /* SCMI_ERR_ENTRY */ 111 -ERANGE, /* SCMI_ERR_RANGE */ 112 -EBUSY, /* SCMI_ERR_BUSY */ 113 -ECOMM, /* SCMI_ERR_COMMS */ 114 -EIO, /* SCMI_ERR_GENERIC */ 115 -EREMOTEIO, /* SCMI_ERR_HARDWARE */ 116 -EPROTO, /* SCMI_ERR_PROTOCOL */ 117 }; 118 119 static inline int scmi_to_linux_errno(int errno) 120 { 121 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX) 122 return scmi_linux_errmap[-errno]; 123 return -EIO; 124 } 125 126 /** 127 * scmi_dump_header_dbg() - Helper to dump a message header. 128 * 129 * @dev: Device pointer corresponding to the SCMI entity 130 * @hdr: pointer to header. 131 */ 132 static inline void scmi_dump_header_dbg(struct device *dev, 133 struct scmi_msg_hdr *hdr) 134 { 135 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n", 136 hdr->id, hdr->seq, hdr->protocol_id); 137 } 138 139 /** 140 * scmi_xfer_get() - Allocate one message 141 * 142 * @handle: Pointer to SCMI entity handle 143 * @minfo: Pointer to Tx/Rx Message management info based on channel type 144 * 145 * Helper function which is used by various message functions that are 146 * exposed to clients of this driver for allocating a message traffic event. 147 * 148 * This function can sleep depending on pending requests already in the system 149 * for the SCMI entity. Further, this also holds a spinlock to maintain 150 * integrity of internal data structures. 151 * 152 * Return: 0 if all went fine, else corresponding error. 153 */ 154 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle, 155 struct scmi_xfers_info *minfo) 156 { 157 u16 xfer_id; 158 struct scmi_xfer *xfer; 159 unsigned long flags, bit_pos; 160 struct scmi_info *info = handle_to_scmi_info(handle); 161 162 /* Keep the locked section as small as possible */ 163 spin_lock_irqsave(&minfo->xfer_lock, flags); 164 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table, 165 info->desc->max_msg); 166 if (bit_pos == info->desc->max_msg) { 167 spin_unlock_irqrestore(&minfo->xfer_lock, flags); 168 return ERR_PTR(-ENOMEM); 169 } 170 set_bit(bit_pos, minfo->xfer_alloc_table); 171 spin_unlock_irqrestore(&minfo->xfer_lock, flags); 172 173 xfer_id = bit_pos; 174 175 xfer = &minfo->xfer_block[xfer_id]; 176 xfer->hdr.seq = xfer_id; 177 reinit_completion(&xfer->done); 178 xfer->transfer_id = atomic_inc_return(&transfer_last_id); 179 180 return xfer; 181 } 182 183 /** 184 * __scmi_xfer_put() - Release a message 185 * 186 * @minfo: Pointer to Tx/Rx Message management info based on channel type 187 * @xfer: message that was reserved by scmi_xfer_get 188 * 189 * This holds a spinlock to maintain integrity of internal data structures. 190 */ 191 static void 192 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer) 193 { 194 unsigned long flags; 195 196 /* 197 * Keep the locked section as small as possible 198 * NOTE: we might escape with smp_mb and no lock here.. 199 * but just be conservative and symmetric. 200 */ 201 spin_lock_irqsave(&minfo->xfer_lock, flags); 202 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table); 203 spin_unlock_irqrestore(&minfo->xfer_lock, flags); 204 } 205 206 static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr) 207 { 208 struct scmi_xfer *xfer; 209 struct device *dev = cinfo->dev; 210 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); 211 struct scmi_xfers_info *minfo = &info->rx_minfo; 212 ktime_t ts; 213 214 ts = ktime_get_boottime(); 215 xfer = scmi_xfer_get(cinfo->handle, minfo); 216 if (IS_ERR(xfer)) { 217 dev_err(dev, "failed to get free message slot (%ld)\n", 218 PTR_ERR(xfer)); 219 info->desc->ops->clear_channel(cinfo); 220 return; 221 } 222 223 unpack_scmi_header(msg_hdr, &xfer->hdr); 224 scmi_dump_header_dbg(dev, &xfer->hdr); 225 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size, 226 xfer); 227 scmi_notify(cinfo->handle, xfer->hdr.protocol_id, 228 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts); 229 230 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, 231 xfer->hdr.protocol_id, xfer->hdr.seq, 232 MSG_TYPE_NOTIFICATION); 233 234 __scmi_xfer_put(minfo, xfer); 235 236 info->desc->ops->clear_channel(cinfo); 237 } 238 239 static void scmi_handle_response(struct scmi_chan_info *cinfo, 240 u16 xfer_id, u8 msg_type) 241 { 242 struct scmi_xfer *xfer; 243 struct device *dev = cinfo->dev; 244 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); 245 struct scmi_xfers_info *minfo = &info->tx_minfo; 246 247 /* Are we even expecting this? */ 248 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) { 249 dev_err(dev, "message for %d is not expected!\n", xfer_id); 250 info->desc->ops->clear_channel(cinfo); 251 return; 252 } 253 254 xfer = &minfo->xfer_block[xfer_id]; 255 /* 256 * Even if a response was indeed expected on this slot at this point, 257 * a buggy platform could wrongly reply feeding us an unexpected 258 * delayed response we're not prepared to handle: bail-out safely 259 * blaming firmware. 260 */ 261 if (unlikely(msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done)) { 262 dev_err(dev, 263 "Delayed Response for %d not expected! Buggy F/W ?\n", 264 xfer_id); 265 info->desc->ops->clear_channel(cinfo); 266 /* It was unexpected, so nobody will clear the xfer if not us */ 267 __scmi_xfer_put(minfo, xfer); 268 return; 269 } 270 271 scmi_dump_header_dbg(dev, &xfer->hdr); 272 273 info->desc->ops->fetch_response(cinfo, xfer); 274 275 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, 276 xfer->hdr.protocol_id, xfer->hdr.seq, 277 msg_type); 278 279 if (msg_type == MSG_TYPE_DELAYED_RESP) { 280 info->desc->ops->clear_channel(cinfo); 281 complete(xfer->async_done); 282 } else { 283 complete(&xfer->done); 284 } 285 } 286 287 /** 288 * scmi_rx_callback() - callback for receiving messages 289 * 290 * @cinfo: SCMI channel info 291 * @msg_hdr: Message header 292 * 293 * Processes one received message to appropriate transfer information and 294 * signals completion of the transfer. 295 * 296 * NOTE: This function will be invoked in IRQ context, hence should be 297 * as optimal as possible. 298 */ 299 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr) 300 { 301 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr); 302 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr); 303 304 switch (msg_type) { 305 case MSG_TYPE_NOTIFICATION: 306 scmi_handle_notification(cinfo, msg_hdr); 307 break; 308 case MSG_TYPE_COMMAND: 309 case MSG_TYPE_DELAYED_RESP: 310 scmi_handle_response(cinfo, xfer_id, msg_type); 311 break; 312 default: 313 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type); 314 break; 315 } 316 } 317 318 /** 319 * scmi_xfer_put() - Release a transmit message 320 * 321 * @handle: Pointer to SCMI entity handle 322 * @xfer: message that was reserved by scmi_xfer_get 323 */ 324 void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer) 325 { 326 struct scmi_info *info = handle_to_scmi_info(handle); 327 328 __scmi_xfer_put(&info->tx_minfo, xfer); 329 } 330 331 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC) 332 333 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo, 334 struct scmi_xfer *xfer, ktime_t stop) 335 { 336 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); 337 338 return info->desc->ops->poll_done(cinfo, xfer) || 339 ktime_after(ktime_get(), stop); 340 } 341 342 /** 343 * scmi_do_xfer() - Do one transfer 344 * 345 * @handle: Pointer to SCMI entity handle 346 * @xfer: Transfer to initiate and wait for response 347 * 348 * Return: -ETIMEDOUT in case of no response, if transmit error, 349 * return corresponding error, else if all goes well, 350 * return 0. 351 */ 352 int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer) 353 { 354 int ret; 355 int timeout; 356 struct scmi_info *info = handle_to_scmi_info(handle); 357 struct device *dev = info->dev; 358 struct scmi_chan_info *cinfo; 359 360 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id); 361 if (unlikely(!cinfo)) 362 return -EINVAL; 363 364 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id, 365 xfer->hdr.protocol_id, xfer->hdr.seq, 366 xfer->hdr.poll_completion); 367 368 ret = info->desc->ops->send_message(cinfo, xfer); 369 if (ret < 0) { 370 dev_dbg(dev, "Failed to send message %d\n", ret); 371 return ret; 372 } 373 374 if (xfer->hdr.poll_completion) { 375 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS); 376 377 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop)); 378 379 if (ktime_before(ktime_get(), stop)) 380 info->desc->ops->fetch_response(cinfo, xfer); 381 else 382 ret = -ETIMEDOUT; 383 } else { 384 /* And we wait for the response. */ 385 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms); 386 if (!wait_for_completion_timeout(&xfer->done, timeout)) { 387 dev_err(dev, "timed out in resp(caller: %pS)\n", 388 (void *)_RET_IP_); 389 ret = -ETIMEDOUT; 390 } 391 } 392 393 if (!ret && xfer->hdr.status) 394 ret = scmi_to_linux_errno(xfer->hdr.status); 395 396 if (info->desc->ops->mark_txdone) 397 info->desc->ops->mark_txdone(cinfo, ret); 398 399 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id, 400 xfer->hdr.protocol_id, xfer->hdr.seq, ret); 401 402 return ret; 403 } 404 405 void scmi_reset_rx_to_maxsz(const struct scmi_handle *handle, 406 struct scmi_xfer *xfer) 407 { 408 struct scmi_info *info = handle_to_scmi_info(handle); 409 410 xfer->rx.len = info->desc->max_msg_size; 411 } 412 413 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC) 414 415 /** 416 * scmi_do_xfer_with_response() - Do one transfer and wait until the delayed 417 * response is received 418 * 419 * @handle: Pointer to SCMI entity handle 420 * @xfer: Transfer to initiate and wait for response 421 * 422 * Return: -ETIMEDOUT in case of no delayed response, if transmit error, 423 * return corresponding error, else if all goes well, return 0. 424 */ 425 int scmi_do_xfer_with_response(const struct scmi_handle *handle, 426 struct scmi_xfer *xfer) 427 { 428 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT); 429 DECLARE_COMPLETION_ONSTACK(async_response); 430 431 xfer->async_done = &async_response; 432 433 ret = scmi_do_xfer(handle, xfer); 434 if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout)) 435 ret = -ETIMEDOUT; 436 437 xfer->async_done = NULL; 438 return ret; 439 } 440 441 /** 442 * scmi_xfer_get_init() - Allocate and initialise one message for transmit 443 * 444 * @handle: Pointer to SCMI entity handle 445 * @msg_id: Message identifier 446 * @prot_id: Protocol identifier for the message 447 * @tx_size: transmit message size 448 * @rx_size: receive message size 449 * @p: pointer to the allocated and initialised message 450 * 451 * This function allocates the message using @scmi_xfer_get and 452 * initialise the header. 453 * 454 * Return: 0 if all went fine with @p pointing to message, else 455 * corresponding error. 456 */ 457 int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id, 458 size_t tx_size, size_t rx_size, struct scmi_xfer **p) 459 { 460 int ret; 461 struct scmi_xfer *xfer; 462 struct scmi_info *info = handle_to_scmi_info(handle); 463 struct scmi_xfers_info *minfo = &info->tx_minfo; 464 struct device *dev = info->dev; 465 466 /* Ensure we have sane transfer sizes */ 467 if (rx_size > info->desc->max_msg_size || 468 tx_size > info->desc->max_msg_size) 469 return -ERANGE; 470 471 xfer = scmi_xfer_get(handle, minfo); 472 if (IS_ERR(xfer)) { 473 ret = PTR_ERR(xfer); 474 dev_err(dev, "failed to get free message slot(%d)\n", ret); 475 return ret; 476 } 477 478 xfer->tx.len = tx_size; 479 xfer->rx.len = rx_size ? : info->desc->max_msg_size; 480 xfer->hdr.id = msg_id; 481 xfer->hdr.protocol_id = prot_id; 482 xfer->hdr.poll_completion = false; 483 484 *p = xfer; 485 486 return 0; 487 } 488 489 /** 490 * scmi_version_get() - command to get the revision of the SCMI entity 491 * 492 * @handle: Pointer to SCMI entity handle 493 * @protocol: Protocol identifier for the message 494 * @version: Holds returned version of protocol. 495 * 496 * Updates the SCMI information in the internal data structure. 497 * 498 * Return: 0 if all went fine, else return appropriate error. 499 */ 500 int scmi_version_get(const struct scmi_handle *handle, u8 protocol, 501 u32 *version) 502 { 503 int ret; 504 __le32 *rev_info; 505 struct scmi_xfer *t; 506 507 ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0, 508 sizeof(*version), &t); 509 if (ret) 510 return ret; 511 512 ret = scmi_do_xfer(handle, t); 513 if (!ret) { 514 rev_info = t->rx.buf; 515 *version = le32_to_cpu(*rev_info); 516 } 517 518 scmi_xfer_put(handle, t); 519 return ret; 520 } 521 522 void scmi_setup_protocol_implemented(const struct scmi_handle *handle, 523 u8 *prot_imp) 524 { 525 struct scmi_info *info = handle_to_scmi_info(handle); 526 527 info->protocols_imp = prot_imp; 528 } 529 530 static bool 531 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id) 532 { 533 int i; 534 struct scmi_info *info = handle_to_scmi_info(handle); 535 536 if (!info->protocols_imp) 537 return false; 538 539 for (i = 0; i < MAX_PROTOCOLS_IMP; i++) 540 if (info->protocols_imp[i] == prot_id) 541 return true; 542 return false; 543 } 544 545 /** 546 * scmi_handle_get() - Get the SCMI handle for a device 547 * 548 * @dev: pointer to device for which we want SCMI handle 549 * 550 * NOTE: The function does not track individual clients of the framework 551 * and is expected to be maintained by caller of SCMI protocol library. 552 * scmi_handle_put must be balanced with successful scmi_handle_get 553 * 554 * Return: pointer to handle if successful, NULL on error 555 */ 556 struct scmi_handle *scmi_handle_get(struct device *dev) 557 { 558 struct list_head *p; 559 struct scmi_info *info; 560 struct scmi_handle *handle = NULL; 561 562 mutex_lock(&scmi_list_mutex); 563 list_for_each(p, &scmi_list) { 564 info = list_entry(p, struct scmi_info, node); 565 if (dev->parent == info->dev) { 566 handle = &info->handle; 567 info->users++; 568 break; 569 } 570 } 571 mutex_unlock(&scmi_list_mutex); 572 573 return handle; 574 } 575 576 /** 577 * scmi_handle_put() - Release the handle acquired by scmi_handle_get 578 * 579 * @handle: handle acquired by scmi_handle_get 580 * 581 * NOTE: The function does not track individual clients of the framework 582 * and is expected to be maintained by caller of SCMI protocol library. 583 * scmi_handle_put must be balanced with successful scmi_handle_get 584 * 585 * Return: 0 is successfully released 586 * if null was passed, it returns -EINVAL; 587 */ 588 int scmi_handle_put(const struct scmi_handle *handle) 589 { 590 struct scmi_info *info; 591 592 if (!handle) 593 return -EINVAL; 594 595 info = handle_to_scmi_info(handle); 596 mutex_lock(&scmi_list_mutex); 597 if (!WARN_ON(!info->users)) 598 info->users--; 599 mutex_unlock(&scmi_list_mutex); 600 601 return 0; 602 } 603 604 static int __scmi_xfer_info_init(struct scmi_info *sinfo, 605 struct scmi_xfers_info *info) 606 { 607 int i; 608 struct scmi_xfer *xfer; 609 struct device *dev = sinfo->dev; 610 const struct scmi_desc *desc = sinfo->desc; 611 612 /* Pre-allocated messages, no more than what hdr.seq can support */ 613 if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) { 614 dev_err(dev, "Maximum message of %d exceeds supported %ld\n", 615 desc->max_msg, MSG_TOKEN_MAX); 616 return -EINVAL; 617 } 618 619 info->xfer_block = devm_kcalloc(dev, desc->max_msg, 620 sizeof(*info->xfer_block), GFP_KERNEL); 621 if (!info->xfer_block) 622 return -ENOMEM; 623 624 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg), 625 sizeof(long), GFP_KERNEL); 626 if (!info->xfer_alloc_table) 627 return -ENOMEM; 628 629 /* Pre-initialize the buffer pointer to pre-allocated buffers */ 630 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) { 631 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size, 632 GFP_KERNEL); 633 if (!xfer->rx.buf) 634 return -ENOMEM; 635 636 xfer->tx.buf = xfer->rx.buf; 637 init_completion(&xfer->done); 638 } 639 640 spin_lock_init(&info->xfer_lock); 641 642 return 0; 643 } 644 645 static int scmi_xfer_info_init(struct scmi_info *sinfo) 646 { 647 int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo); 648 649 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE)) 650 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo); 651 652 return ret; 653 } 654 655 static int scmi_chan_setup(struct scmi_info *info, struct device *dev, 656 int prot_id, bool tx) 657 { 658 int ret, idx; 659 struct scmi_chan_info *cinfo; 660 struct idr *idr; 661 662 /* Transmit channel is first entry i.e. index 0 */ 663 idx = tx ? 0 : 1; 664 idr = tx ? &info->tx_idr : &info->rx_idr; 665 666 /* check if already allocated, used for multiple device per protocol */ 667 cinfo = idr_find(idr, prot_id); 668 if (cinfo) 669 return 0; 670 671 if (!info->desc->ops->chan_available(dev, idx)) { 672 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE); 673 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */ 674 return -EINVAL; 675 goto idr_alloc; 676 } 677 678 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL); 679 if (!cinfo) 680 return -ENOMEM; 681 682 cinfo->dev = dev; 683 684 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx); 685 if (ret) 686 return ret; 687 688 idr_alloc: 689 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL); 690 if (ret != prot_id) { 691 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret); 692 return ret; 693 } 694 695 cinfo->handle = &info->handle; 696 return 0; 697 } 698 699 static inline int 700 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id) 701 { 702 int ret = scmi_chan_setup(info, dev, prot_id, true); 703 704 if (!ret) /* Rx is optional, hence no error check */ 705 scmi_chan_setup(info, dev, prot_id, false); 706 707 return ret; 708 } 709 710 static inline void 711 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info, 712 int prot_id, const char *name) 713 { 714 struct scmi_device *sdev; 715 716 sdev = scmi_device_create(np, info->dev, prot_id, name); 717 if (!sdev) { 718 dev_err(info->dev, "failed to create %d protocol device\n", 719 prot_id); 720 return; 721 } 722 723 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) { 724 dev_err(&sdev->dev, "failed to setup transport\n"); 725 scmi_device_destroy(sdev); 726 return; 727 } 728 729 /* setup handle now as the transport is ready */ 730 scmi_set_handle(sdev); 731 } 732 733 #define MAX_SCMI_DEV_PER_PROTOCOL 2 734 struct scmi_prot_devnames { 735 int protocol_id; 736 char *names[MAX_SCMI_DEV_PER_PROTOCOL]; 737 }; 738 739 static struct scmi_prot_devnames devnames[] = { 740 { SCMI_PROTOCOL_POWER, { "genpd" },}, 741 { SCMI_PROTOCOL_SYSTEM, { "syspower" },}, 742 { SCMI_PROTOCOL_PERF, { "cpufreq" },}, 743 { SCMI_PROTOCOL_CLOCK, { "clocks" },}, 744 { SCMI_PROTOCOL_SENSOR, { "hwmon" },}, 745 { SCMI_PROTOCOL_RESET, { "reset" },}, 746 { SCMI_PROTOCOL_VOLTAGE, { "regulator" },}, 747 }; 748 749 static inline void 750 scmi_create_protocol_devices(struct device_node *np, struct scmi_info *info, 751 int prot_id) 752 { 753 int loop, cnt; 754 755 for (loop = 0; loop < ARRAY_SIZE(devnames); loop++) { 756 if (devnames[loop].protocol_id != prot_id) 757 continue; 758 759 for (cnt = 0; cnt < ARRAY_SIZE(devnames[loop].names); cnt++) { 760 const char *name = devnames[loop].names[cnt]; 761 762 if (name) 763 scmi_create_protocol_device(np, info, prot_id, 764 name); 765 } 766 } 767 } 768 769 static int scmi_probe(struct platform_device *pdev) 770 { 771 int ret; 772 struct scmi_handle *handle; 773 const struct scmi_desc *desc; 774 struct scmi_info *info; 775 struct device *dev = &pdev->dev; 776 struct device_node *child, *np = dev->of_node; 777 778 desc = of_device_get_match_data(dev); 779 if (!desc) 780 return -EINVAL; 781 782 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 783 if (!info) 784 return -ENOMEM; 785 786 info->dev = dev; 787 info->desc = desc; 788 INIT_LIST_HEAD(&info->node); 789 790 platform_set_drvdata(pdev, info); 791 idr_init(&info->tx_idr); 792 idr_init(&info->rx_idr); 793 794 handle = &info->handle; 795 handle->dev = info->dev; 796 handle->version = &info->version; 797 798 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE); 799 if (ret) 800 return ret; 801 802 ret = scmi_xfer_info_init(info); 803 if (ret) 804 return ret; 805 806 if (scmi_notification_init(handle)) 807 dev_err(dev, "SCMI Notifications NOT available.\n"); 808 809 ret = scmi_base_protocol_init(handle); 810 if (ret) { 811 dev_err(dev, "unable to communicate with SCMI(%d)\n", ret); 812 return ret; 813 } 814 815 mutex_lock(&scmi_list_mutex); 816 list_add_tail(&info->node, &scmi_list); 817 mutex_unlock(&scmi_list_mutex); 818 819 for_each_available_child_of_node(np, child) { 820 u32 prot_id; 821 822 if (of_property_read_u32(child, "reg", &prot_id)) 823 continue; 824 825 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id)) 826 dev_err(dev, "Out of range protocol %d\n", prot_id); 827 828 if (!scmi_is_protocol_implemented(handle, prot_id)) { 829 dev_err(dev, "SCMI protocol %d not implemented\n", 830 prot_id); 831 continue; 832 } 833 834 scmi_create_protocol_devices(child, info, prot_id); 835 } 836 837 return 0; 838 } 839 840 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id) 841 { 842 idr_remove(idr, id); 843 } 844 845 static int scmi_remove(struct platform_device *pdev) 846 { 847 int ret = 0; 848 struct scmi_info *info = platform_get_drvdata(pdev); 849 struct idr *idr = &info->tx_idr; 850 851 scmi_notification_exit(&info->handle); 852 853 mutex_lock(&scmi_list_mutex); 854 if (info->users) 855 ret = -EBUSY; 856 else 857 list_del(&info->node); 858 mutex_unlock(&scmi_list_mutex); 859 860 if (ret) 861 return ret; 862 863 /* Safe to free channels since no more users */ 864 ret = idr_for_each(idr, info->desc->ops->chan_free, idr); 865 idr_destroy(&info->tx_idr); 866 867 idr = &info->rx_idr; 868 ret = idr_for_each(idr, info->desc->ops->chan_free, idr); 869 idr_destroy(&info->rx_idr); 870 871 return ret; 872 } 873 874 static ssize_t protocol_version_show(struct device *dev, 875 struct device_attribute *attr, char *buf) 876 { 877 struct scmi_info *info = dev_get_drvdata(dev); 878 879 return sprintf(buf, "%u.%u\n", info->version.major_ver, 880 info->version.minor_ver); 881 } 882 static DEVICE_ATTR_RO(protocol_version); 883 884 static ssize_t firmware_version_show(struct device *dev, 885 struct device_attribute *attr, char *buf) 886 { 887 struct scmi_info *info = dev_get_drvdata(dev); 888 889 return sprintf(buf, "0x%x\n", info->version.impl_ver); 890 } 891 static DEVICE_ATTR_RO(firmware_version); 892 893 static ssize_t vendor_id_show(struct device *dev, 894 struct device_attribute *attr, char *buf) 895 { 896 struct scmi_info *info = dev_get_drvdata(dev); 897 898 return sprintf(buf, "%s\n", info->version.vendor_id); 899 } 900 static DEVICE_ATTR_RO(vendor_id); 901 902 static ssize_t sub_vendor_id_show(struct device *dev, 903 struct device_attribute *attr, char *buf) 904 { 905 struct scmi_info *info = dev_get_drvdata(dev); 906 907 return sprintf(buf, "%s\n", info->version.sub_vendor_id); 908 } 909 static DEVICE_ATTR_RO(sub_vendor_id); 910 911 static struct attribute *versions_attrs[] = { 912 &dev_attr_firmware_version.attr, 913 &dev_attr_protocol_version.attr, 914 &dev_attr_vendor_id.attr, 915 &dev_attr_sub_vendor_id.attr, 916 NULL, 917 }; 918 ATTRIBUTE_GROUPS(versions); 919 920 /* Each compatible listed below must have descriptor associated with it */ 921 static const struct of_device_id scmi_of_match[] = { 922 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc }, 923 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY 924 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc}, 925 #endif 926 { /* Sentinel */ }, 927 }; 928 929 MODULE_DEVICE_TABLE(of, scmi_of_match); 930 931 static struct platform_driver scmi_driver = { 932 .driver = { 933 .name = "arm-scmi", 934 .of_match_table = scmi_of_match, 935 .dev_groups = versions_groups, 936 }, 937 .probe = scmi_probe, 938 .remove = scmi_remove, 939 }; 940 941 static int __init scmi_driver_init(void) 942 { 943 scmi_bus_init(); 944 945 scmi_clock_register(); 946 scmi_perf_register(); 947 scmi_power_register(); 948 scmi_reset_register(); 949 scmi_sensors_register(); 950 scmi_voltage_register(); 951 scmi_system_register(); 952 953 return platform_driver_register(&scmi_driver); 954 } 955 subsys_initcall(scmi_driver_init); 956 957 static void __exit scmi_driver_exit(void) 958 { 959 scmi_bus_exit(); 960 961 scmi_clock_unregister(); 962 scmi_perf_unregister(); 963 scmi_power_unregister(); 964 scmi_reset_unregister(); 965 scmi_sensors_unregister(); 966 scmi_voltage_unregister(); 967 scmi_system_unregister(); 968 969 platform_driver_unregister(&scmi_driver); 970 } 971 module_exit(scmi_driver_exit); 972 973 MODULE_ALIAS("platform: arm-scmi"); 974 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); 975 MODULE_DESCRIPTION("ARM SCMI protocol driver"); 976 MODULE_LICENSE("GPL v2"); 977