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/mailbox_client.h> 23 #include <linux/module.h> 24 #include <linux/of_address.h> 25 #include <linux/of_device.h> 26 #include <linux/processor.h> 27 #include <linux/semaphore.h> 28 #include <linux/slab.h> 29 30 #include "common.h" 31 32 #define MSG_ID_SHIFT 0 33 #define MSG_ID_MASK 0xff 34 #define MSG_TYPE_SHIFT 8 35 #define MSG_TYPE_MASK 0x3 36 #define MSG_PROTOCOL_ID_SHIFT 10 37 #define MSG_PROTOCOL_ID_MASK 0xff 38 #define MSG_TOKEN_ID_SHIFT 18 39 #define MSG_TOKEN_ID_MASK 0x3ff 40 #define MSG_XTRACT_TOKEN(header) \ 41 (((header) >> MSG_TOKEN_ID_SHIFT) & MSG_TOKEN_ID_MASK) 42 43 enum scmi_error_codes { 44 SCMI_SUCCESS = 0, /* Success */ 45 SCMI_ERR_SUPPORT = -1, /* Not supported */ 46 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */ 47 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */ 48 SCMI_ERR_ENTRY = -4, /* Not found */ 49 SCMI_ERR_RANGE = -5, /* Value out of range */ 50 SCMI_ERR_BUSY = -6, /* Device busy */ 51 SCMI_ERR_COMMS = -7, /* Communication Error */ 52 SCMI_ERR_GENERIC = -8, /* Generic Error */ 53 SCMI_ERR_HARDWARE = -9, /* Hardware Error */ 54 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */ 55 SCMI_ERR_MAX 56 }; 57 58 /* List of all SCMI devices active in system */ 59 static LIST_HEAD(scmi_list); 60 /* Protection for the entire list */ 61 static DEFINE_MUTEX(scmi_list_mutex); 62 63 /** 64 * struct scmi_xfers_info - Structure to manage transfer information 65 * 66 * @xfer_block: Preallocated Message array 67 * @xfer_alloc_table: Bitmap table for allocated messages. 68 * Index of this bitmap table is also used for message 69 * sequence identifier. 70 * @xfer_lock: Protection for message allocation 71 */ 72 struct scmi_xfers_info { 73 struct scmi_xfer *xfer_block; 74 unsigned long *xfer_alloc_table; 75 /* protect transfer allocation */ 76 spinlock_t xfer_lock; 77 }; 78 79 /** 80 * struct scmi_desc - Description of SoC integration 81 * 82 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) 83 * @max_msg: Maximum number of messages that can be pending 84 * simultaneously in the system 85 * @max_msg_size: Maximum size of data per message that can be handled. 86 */ 87 struct scmi_desc { 88 int max_rx_timeout_ms; 89 int max_msg; 90 int max_msg_size; 91 }; 92 93 /** 94 * struct scmi_chan_info - Structure representing a SCMI channel informfation 95 * 96 * @cl: Mailbox Client 97 * @chan: Transmit/Receive mailbox channel 98 * @payload: Transmit/Receive mailbox channel payload area 99 * @dev: Reference to device in the SCMI hierarchy corresponding to this 100 * channel 101 */ 102 struct scmi_chan_info { 103 struct mbox_client cl; 104 struct mbox_chan *chan; 105 void __iomem *payload; 106 struct device *dev; 107 struct scmi_handle *handle; 108 }; 109 110 /** 111 * struct scmi_info - Structure representing a SCMI instance 112 * 113 * @dev: Device pointer 114 * @desc: SoC description for this instance 115 * @handle: Instance of SCMI handle to send to clients 116 * @version: SCMI revision information containing protocol version, 117 * implementation version and (sub-)vendor identification. 118 * @minfo: Message info 119 * @tx_idr: IDR object to map protocol id to channel info pointer 120 * @protocols_imp: list of protocols implemented, currently maximum of 121 * MAX_PROTOCOLS_IMP elements allocated by the base protocol 122 * @node: list head 123 * @users: Number of users of this instance 124 */ 125 struct scmi_info { 126 struct device *dev; 127 const struct scmi_desc *desc; 128 struct scmi_revision_info version; 129 struct scmi_handle handle; 130 struct scmi_xfers_info minfo; 131 struct idr tx_idr; 132 u8 *protocols_imp; 133 struct list_head node; 134 int users; 135 }; 136 137 #define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl) 138 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle) 139 140 /* 141 * SCMI specification requires all parameters, message headers, return 142 * arguments or any protocol data to be expressed in little endian 143 * format only. 144 */ 145 struct scmi_shared_mem { 146 __le32 reserved; 147 __le32 channel_status; 148 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1) 149 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0) 150 __le32 reserved1[2]; 151 __le32 flags; 152 #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0) 153 __le32 length; 154 __le32 msg_header; 155 u8 msg_payload[0]; 156 }; 157 158 static const int scmi_linux_errmap[] = { 159 /* better than switch case as long as return value is continuous */ 160 0, /* SCMI_SUCCESS */ 161 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */ 162 -EINVAL, /* SCMI_ERR_PARAM */ 163 -EACCES, /* SCMI_ERR_ACCESS */ 164 -ENOENT, /* SCMI_ERR_ENTRY */ 165 -ERANGE, /* SCMI_ERR_RANGE */ 166 -EBUSY, /* SCMI_ERR_BUSY */ 167 -ECOMM, /* SCMI_ERR_COMMS */ 168 -EIO, /* SCMI_ERR_GENERIC */ 169 -EREMOTEIO, /* SCMI_ERR_HARDWARE */ 170 -EPROTO, /* SCMI_ERR_PROTOCOL */ 171 }; 172 173 static inline int scmi_to_linux_errno(int errno) 174 { 175 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX) 176 return scmi_linux_errmap[-errno]; 177 return -EIO; 178 } 179 180 /** 181 * scmi_dump_header_dbg() - Helper to dump a message header. 182 * 183 * @dev: Device pointer corresponding to the SCMI entity 184 * @hdr: pointer to header. 185 */ 186 static inline void scmi_dump_header_dbg(struct device *dev, 187 struct scmi_msg_hdr *hdr) 188 { 189 dev_dbg(dev, "Command ID: %x Sequence ID: %x Protocol: %x\n", 190 hdr->id, hdr->seq, hdr->protocol_id); 191 } 192 193 static void scmi_fetch_response(struct scmi_xfer *xfer, 194 struct scmi_shared_mem __iomem *mem) 195 { 196 xfer->hdr.status = ioread32(mem->msg_payload); 197 /* Skip the length of header and statues in payload area i.e 8 bytes*/ 198 xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8); 199 200 /* Take a copy to the rx buffer.. */ 201 memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len); 202 } 203 204 /** 205 * scmi_rx_callback() - mailbox client callback for receive messages 206 * 207 * @cl: client pointer 208 * @m: mailbox message 209 * 210 * Processes one received message to appropriate transfer information and 211 * signals completion of the transfer. 212 * 213 * NOTE: This function will be invoked in IRQ context, hence should be 214 * as optimal as possible. 215 */ 216 static void scmi_rx_callback(struct mbox_client *cl, void *m) 217 { 218 u16 xfer_id; 219 struct scmi_xfer *xfer; 220 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl); 221 struct device *dev = cinfo->dev; 222 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); 223 struct scmi_xfers_info *minfo = &info->minfo; 224 struct scmi_shared_mem __iomem *mem = cinfo->payload; 225 226 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header)); 227 228 /* 229 * Are we even expecting this? 230 */ 231 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) { 232 dev_err(dev, "message for %d is not expected!\n", xfer_id); 233 return; 234 } 235 236 xfer = &minfo->xfer_block[xfer_id]; 237 238 scmi_dump_header_dbg(dev, &xfer->hdr); 239 /* Is the message of valid length? */ 240 if (xfer->rx.len > info->desc->max_msg_size) { 241 dev_err(dev, "unable to handle %zu xfer(max %d)\n", 242 xfer->rx.len, info->desc->max_msg_size); 243 return; 244 } 245 246 scmi_fetch_response(xfer, mem); 247 complete(&xfer->done); 248 } 249 250 /** 251 * pack_scmi_header() - packs and returns 32-bit header 252 * 253 * @hdr: pointer to header containing all the information on message id, 254 * protocol id and sequence id. 255 */ 256 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr) 257 { 258 return ((hdr->id & MSG_ID_MASK) << MSG_ID_SHIFT) | 259 ((hdr->seq & MSG_TOKEN_ID_MASK) << MSG_TOKEN_ID_SHIFT) | 260 ((hdr->protocol_id & MSG_PROTOCOL_ID_MASK) << MSG_PROTOCOL_ID_SHIFT); 261 } 262 263 /** 264 * scmi_tx_prepare() - mailbox client callback to prepare for the transfer 265 * 266 * @cl: client pointer 267 * @m: mailbox message 268 * 269 * This function prepares the shared memory which contains the header and the 270 * payload. 271 */ 272 static void scmi_tx_prepare(struct mbox_client *cl, void *m) 273 { 274 struct scmi_xfer *t = m; 275 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl); 276 struct scmi_shared_mem __iomem *mem = cinfo->payload; 277 278 /* Mark channel busy + clear error */ 279 iowrite32(0x0, &mem->channel_status); 280 iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED, 281 &mem->flags); 282 iowrite32(sizeof(mem->msg_header) + t->tx.len, &mem->length); 283 iowrite32(pack_scmi_header(&t->hdr), &mem->msg_header); 284 if (t->tx.buf) 285 memcpy_toio(mem->msg_payload, t->tx.buf, t->tx.len); 286 } 287 288 /** 289 * scmi_one_xfer_get() - Allocate one message 290 * 291 * @handle: SCMI entity handle 292 * 293 * Helper function which is used by various command functions that are 294 * exposed to clients of this driver for allocating a message traffic event. 295 * 296 * This function can sleep depending on pending requests already in the system 297 * for the SCMI entity. Further, this also holds a spinlock to maintain 298 * integrity of internal data structures. 299 * 300 * Return: 0 if all went fine, else corresponding error. 301 */ 302 static struct scmi_xfer *scmi_one_xfer_get(const struct scmi_handle *handle) 303 { 304 u16 xfer_id; 305 struct scmi_xfer *xfer; 306 unsigned long flags, bit_pos; 307 struct scmi_info *info = handle_to_scmi_info(handle); 308 struct scmi_xfers_info *minfo = &info->minfo; 309 310 /* Keep the locked section as small as possible */ 311 spin_lock_irqsave(&minfo->xfer_lock, flags); 312 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table, 313 info->desc->max_msg); 314 if (bit_pos == info->desc->max_msg) { 315 spin_unlock_irqrestore(&minfo->xfer_lock, flags); 316 return ERR_PTR(-ENOMEM); 317 } 318 set_bit(bit_pos, minfo->xfer_alloc_table); 319 spin_unlock_irqrestore(&minfo->xfer_lock, flags); 320 321 xfer_id = bit_pos; 322 323 xfer = &minfo->xfer_block[xfer_id]; 324 xfer->hdr.seq = xfer_id; 325 reinit_completion(&xfer->done); 326 327 return xfer; 328 } 329 330 /** 331 * scmi_one_xfer_put() - Release a message 332 * 333 * @minfo: transfer info pointer 334 * @xfer: message that was reserved by scmi_one_xfer_get 335 * 336 * This holds a spinlock to maintain integrity of internal data structures. 337 */ 338 void scmi_one_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer) 339 { 340 unsigned long flags; 341 struct scmi_info *info = handle_to_scmi_info(handle); 342 struct scmi_xfers_info *minfo = &info->minfo; 343 344 /* 345 * Keep the locked section as small as possible 346 * NOTE: we might escape with smp_mb and no lock here.. 347 * but just be conservative and symmetric. 348 */ 349 spin_lock_irqsave(&minfo->xfer_lock, flags); 350 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table); 351 spin_unlock_irqrestore(&minfo->xfer_lock, flags); 352 } 353 354 static bool 355 scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer) 356 { 357 struct scmi_shared_mem __iomem *mem = cinfo->payload; 358 u16 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header)); 359 360 if (xfer->hdr.seq != xfer_id) 361 return false; 362 363 return ioread32(&mem->channel_status) & 364 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR | 365 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE); 366 } 367 368 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC) 369 370 static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info *cinfo, 371 struct scmi_xfer *xfer, ktime_t stop) 372 { 373 ktime_t __cur = ktime_get(); 374 375 return scmi_xfer_poll_done(cinfo, xfer) || ktime_after(__cur, stop); 376 } 377 378 /** 379 * scmi_do_xfer() - Do one transfer 380 * 381 * @info: Pointer to SCMI entity information 382 * @xfer: Transfer to initiate and wait for response 383 * 384 * Return: -ETIMEDOUT in case of no response, if transmit error, 385 * return corresponding error, else if all goes well, 386 * return 0. 387 */ 388 int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer) 389 { 390 int ret; 391 int timeout; 392 struct scmi_info *info = handle_to_scmi_info(handle); 393 struct device *dev = info->dev; 394 struct scmi_chan_info *cinfo; 395 396 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id); 397 if (unlikely(!cinfo)) 398 return -EINVAL; 399 400 ret = mbox_send_message(cinfo->chan, xfer); 401 if (ret < 0) { 402 dev_dbg(dev, "mbox send fail %d\n", ret); 403 return ret; 404 } 405 406 /* mbox_send_message returns non-negative value on success, so reset */ 407 ret = 0; 408 409 if (xfer->hdr.poll_completion) { 410 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS); 411 412 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop)); 413 414 if (ktime_before(ktime_get(), stop)) 415 scmi_fetch_response(xfer, cinfo->payload); 416 else 417 ret = -ETIMEDOUT; 418 } else { 419 /* And we wait for the response. */ 420 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms); 421 if (!wait_for_completion_timeout(&xfer->done, timeout)) { 422 dev_err(dev, "mbox timed out in resp(caller: %pS)\n", 423 (void *)_RET_IP_); 424 ret = -ETIMEDOUT; 425 } 426 } 427 428 if (!ret && xfer->hdr.status) 429 ret = scmi_to_linux_errno(xfer->hdr.status); 430 431 /* 432 * NOTE: we might prefer not to need the mailbox ticker to manage the 433 * transfer queueing since the protocol layer queues things by itself. 434 * Unfortunately, we have to kick the mailbox framework after we have 435 * received our message. 436 */ 437 mbox_client_txdone(cinfo->chan, ret); 438 439 return ret; 440 } 441 442 /** 443 * scmi_one_xfer_init() - Allocate and initialise one message 444 * 445 * @handle: SCMI entity handle 446 * @msg_id: Message identifier 447 * @msg_prot_id: Protocol identifier for the message 448 * @tx_size: transmit message size 449 * @rx_size: receive message size 450 * @p: pointer to the allocated and initialised message 451 * 452 * This function allocates the message using @scmi_one_xfer_get and 453 * initialise the header. 454 * 455 * Return: 0 if all went fine with @p pointing to message, else 456 * corresponding error. 457 */ 458 int scmi_one_xfer_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id, 459 size_t tx_size, size_t rx_size, struct scmi_xfer **p) 460 { 461 int ret; 462 struct scmi_xfer *xfer; 463 struct scmi_info *info = handle_to_scmi_info(handle); 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_one_xfer_get(handle); 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 return 0; 486 } 487 488 /** 489 * scmi_version_get() - command to get the revision of the SCMI entity 490 * 491 * @handle: Handle to SCMI entity information 492 * 493 * Updates the SCMI information in the internal data structure. 494 * 495 * Return: 0 if all went fine, else return appropriate error. 496 */ 497 int scmi_version_get(const struct scmi_handle *handle, u8 protocol, 498 u32 *version) 499 { 500 int ret; 501 __le32 *rev_info; 502 struct scmi_xfer *t; 503 504 ret = scmi_one_xfer_init(handle, PROTOCOL_VERSION, protocol, 0, 505 sizeof(*version), &t); 506 if (ret) 507 return ret; 508 509 ret = scmi_do_xfer(handle, t); 510 if (!ret) { 511 rev_info = t->rx.buf; 512 *version = le32_to_cpu(*rev_info); 513 } 514 515 scmi_one_xfer_put(handle, t); 516 return ret; 517 } 518 519 void scmi_setup_protocol_implemented(const struct scmi_handle *handle, 520 u8 *prot_imp) 521 { 522 struct scmi_info *info = handle_to_scmi_info(handle); 523 524 info->protocols_imp = prot_imp; 525 } 526 527 static bool 528 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id) 529 { 530 int i; 531 struct scmi_info *info = handle_to_scmi_info(handle); 532 533 if (!info->protocols_imp) 534 return false; 535 536 for (i = 0; i < MAX_PROTOCOLS_IMP; i++) 537 if (info->protocols_imp[i] == prot_id) 538 return true; 539 return false; 540 } 541 542 /** 543 * scmi_handle_get() - Get the SCMI handle for a device 544 * 545 * @dev: pointer to device for which we want SCMI handle 546 * 547 * NOTE: The function does not track individual clients of the framework 548 * and is expected to be maintained by caller of SCMI protocol library. 549 * scmi_handle_put must be balanced with successful scmi_handle_get 550 * 551 * Return: pointer to handle if successful, NULL on error 552 */ 553 struct scmi_handle *scmi_handle_get(struct device *dev) 554 { 555 struct list_head *p; 556 struct scmi_info *info; 557 struct scmi_handle *handle = NULL; 558 559 mutex_lock(&scmi_list_mutex); 560 list_for_each(p, &scmi_list) { 561 info = list_entry(p, struct scmi_info, node); 562 if (dev->parent == info->dev) { 563 handle = &info->handle; 564 info->users++; 565 break; 566 } 567 } 568 mutex_unlock(&scmi_list_mutex); 569 570 return handle; 571 } 572 573 /** 574 * scmi_handle_put() - Release the handle acquired by scmi_handle_get 575 * 576 * @handle: handle acquired by scmi_handle_get 577 * 578 * NOTE: The function does not track individual clients of the framework 579 * and is expected to be maintained by caller of SCMI protocol library. 580 * scmi_handle_put must be balanced with successful scmi_handle_get 581 * 582 * Return: 0 is successfully released 583 * if null was passed, it returns -EINVAL; 584 */ 585 int scmi_handle_put(const struct scmi_handle *handle) 586 { 587 struct scmi_info *info; 588 589 if (!handle) 590 return -EINVAL; 591 592 info = handle_to_scmi_info(handle); 593 mutex_lock(&scmi_list_mutex); 594 if (!WARN_ON(!info->users)) 595 info->users--; 596 mutex_unlock(&scmi_list_mutex); 597 598 return 0; 599 } 600 601 static const struct scmi_desc scmi_generic_desc = { 602 .max_rx_timeout_ms = 30, /* we may increase this if required */ 603 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */ 604 .max_msg_size = 128, 605 }; 606 607 /* Each compatible listed below must have descriptor associated with it */ 608 static const struct of_device_id scmi_of_match[] = { 609 { .compatible = "arm,scmi", .data = &scmi_generic_desc }, 610 { /* Sentinel */ }, 611 }; 612 613 MODULE_DEVICE_TABLE(of, scmi_of_match); 614 615 static int scmi_xfer_info_init(struct scmi_info *sinfo) 616 { 617 int i; 618 struct scmi_xfer *xfer; 619 struct device *dev = sinfo->dev; 620 const struct scmi_desc *desc = sinfo->desc; 621 struct scmi_xfers_info *info = &sinfo->minfo; 622 623 /* Pre-allocated messages, no more than what hdr.seq can support */ 624 if (WARN_ON(desc->max_msg >= (MSG_TOKEN_ID_MASK + 1))) { 625 dev_err(dev, "Maximum message of %d exceeds supported %d\n", 626 desc->max_msg, MSG_TOKEN_ID_MASK + 1); 627 return -EINVAL; 628 } 629 630 info->xfer_block = devm_kcalloc(dev, desc->max_msg, 631 sizeof(*info->xfer_block), GFP_KERNEL); 632 if (!info->xfer_block) 633 return -ENOMEM; 634 635 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg), 636 sizeof(long), GFP_KERNEL); 637 if (!info->xfer_alloc_table) 638 return -ENOMEM; 639 640 bitmap_zero(info->xfer_alloc_table, desc->max_msg); 641 642 /* Pre-initialize the buffer pointer to pre-allocated buffers */ 643 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) { 644 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size, 645 GFP_KERNEL); 646 if (!xfer->rx.buf) 647 return -ENOMEM; 648 649 xfer->tx.buf = xfer->rx.buf; 650 init_completion(&xfer->done); 651 } 652 653 spin_lock_init(&info->xfer_lock); 654 655 return 0; 656 } 657 658 static int scmi_mailbox_check(struct device_node *np) 659 { 660 struct of_phandle_args arg; 661 662 return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells", 0, &arg); 663 } 664 665 static int scmi_mbox_free_channel(int id, void *p, void *data) 666 { 667 struct scmi_chan_info *cinfo = p; 668 struct idr *idr = data; 669 670 if (!IS_ERR_OR_NULL(cinfo->chan)) { 671 mbox_free_channel(cinfo->chan); 672 cinfo->chan = NULL; 673 } 674 675 idr_remove(idr, id); 676 677 return 0; 678 } 679 680 static int scmi_remove(struct platform_device *pdev) 681 { 682 int ret = 0; 683 struct scmi_info *info = platform_get_drvdata(pdev); 684 struct idr *idr = &info->tx_idr; 685 686 mutex_lock(&scmi_list_mutex); 687 if (info->users) 688 ret = -EBUSY; 689 else 690 list_del(&info->node); 691 mutex_unlock(&scmi_list_mutex); 692 693 if (!ret) { 694 /* Safe to free channels since no more users */ 695 ret = idr_for_each(idr, scmi_mbox_free_channel, idr); 696 idr_destroy(&info->tx_idr); 697 } 698 699 return ret; 700 } 701 702 static inline int 703 scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev, int prot_id) 704 { 705 int ret; 706 struct resource res; 707 resource_size_t size; 708 struct device_node *shmem, *np = dev->of_node; 709 struct scmi_chan_info *cinfo; 710 struct mbox_client *cl; 711 712 if (scmi_mailbox_check(np)) { 713 cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE); 714 goto idr_alloc; 715 } 716 717 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL); 718 if (!cinfo) 719 return -ENOMEM; 720 721 cinfo->dev = dev; 722 723 cl = &cinfo->cl; 724 cl->dev = dev; 725 cl->rx_callback = scmi_rx_callback; 726 cl->tx_prepare = scmi_tx_prepare; 727 cl->tx_block = false; 728 cl->knows_txdone = true; 729 730 shmem = of_parse_phandle(np, "shmem", 0); 731 ret = of_address_to_resource(shmem, 0, &res); 732 of_node_put(shmem); 733 if (ret) { 734 dev_err(dev, "failed to get SCMI Tx payload mem resource\n"); 735 return ret; 736 } 737 738 size = resource_size(&res); 739 cinfo->payload = devm_ioremap(info->dev, res.start, size); 740 if (!cinfo->payload) { 741 dev_err(dev, "failed to ioremap SCMI Tx payload\n"); 742 return -EADDRNOTAVAIL; 743 } 744 745 /* Transmit channel is first entry i.e. index 0 */ 746 cinfo->chan = mbox_request_channel(cl, 0); 747 if (IS_ERR(cinfo->chan)) { 748 ret = PTR_ERR(cinfo->chan); 749 if (ret != -EPROBE_DEFER) 750 dev_err(dev, "failed to request SCMI Tx mailbox\n"); 751 return ret; 752 } 753 754 idr_alloc: 755 ret = idr_alloc(&info->tx_idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL); 756 if (ret != prot_id) { 757 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret); 758 return ret; 759 } 760 761 cinfo->handle = &info->handle; 762 return 0; 763 } 764 765 static inline void 766 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info, 767 int prot_id) 768 { 769 struct scmi_device *sdev; 770 771 sdev = scmi_device_create(np, info->dev, prot_id); 772 if (!sdev) { 773 dev_err(info->dev, "failed to create %d protocol device\n", 774 prot_id); 775 return; 776 } 777 778 if (scmi_mbox_chan_setup(info, &sdev->dev, prot_id)) { 779 dev_err(&sdev->dev, "failed to setup transport\n"); 780 scmi_device_destroy(sdev); 781 } 782 783 /* setup handle now as the transport is ready */ 784 scmi_set_handle(sdev); 785 } 786 787 static int scmi_probe(struct platform_device *pdev) 788 { 789 int ret; 790 struct scmi_handle *handle; 791 const struct scmi_desc *desc; 792 struct scmi_info *info; 793 struct device *dev = &pdev->dev; 794 struct device_node *child, *np = dev->of_node; 795 796 /* Only mailbox method supported, check for the presence of one */ 797 if (scmi_mailbox_check(np)) { 798 dev_err(dev, "no mailbox found in %pOF\n", np); 799 return -EINVAL; 800 } 801 802 desc = of_match_device(scmi_of_match, dev)->data; 803 804 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 805 if (!info) 806 return -ENOMEM; 807 808 info->dev = dev; 809 info->desc = desc; 810 INIT_LIST_HEAD(&info->node); 811 812 ret = scmi_xfer_info_init(info); 813 if (ret) 814 return ret; 815 816 platform_set_drvdata(pdev, info); 817 idr_init(&info->tx_idr); 818 819 handle = &info->handle; 820 handle->dev = info->dev; 821 handle->version = &info->version; 822 823 ret = scmi_mbox_chan_setup(info, dev, SCMI_PROTOCOL_BASE); 824 if (ret) 825 return ret; 826 827 ret = scmi_base_protocol_init(handle); 828 if (ret) { 829 dev_err(dev, "unable to communicate with SCMI(%d)\n", ret); 830 return ret; 831 } 832 833 mutex_lock(&scmi_list_mutex); 834 list_add_tail(&info->node, &scmi_list); 835 mutex_unlock(&scmi_list_mutex); 836 837 for_each_available_child_of_node(np, child) { 838 u32 prot_id; 839 840 if (of_property_read_u32(child, "reg", &prot_id)) 841 continue; 842 843 prot_id &= MSG_PROTOCOL_ID_MASK; 844 845 if (!scmi_is_protocol_implemented(handle, prot_id)) { 846 dev_err(dev, "SCMI protocol %d not implemented\n", 847 prot_id); 848 continue; 849 } 850 851 scmi_create_protocol_device(child, info, prot_id); 852 } 853 854 return 0; 855 } 856 857 static struct platform_driver scmi_driver = { 858 .driver = { 859 .name = "arm-scmi", 860 .of_match_table = scmi_of_match, 861 }, 862 .probe = scmi_probe, 863 .remove = scmi_remove, 864 }; 865 866 module_platform_driver(scmi_driver); 867 868 MODULE_ALIAS("platform: arm-scmi"); 869 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); 870 MODULE_DESCRIPTION("ARM SCMI protocol driver"); 871 MODULE_LICENSE("GPL v2"); 872