1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. 4 */ 5 6 #define pr_fmt(fmt) "%s " fmt, KBUILD_MODNAME 7 8 #include <linux/atomic.h> 9 #include <linux/cpu_pm.h> 10 #include <linux/delay.h> 11 #include <linux/interrupt.h> 12 #include <linux/io.h> 13 #include <linux/iopoll.h> 14 #include <linux/kernel.h> 15 #include <linux/list.h> 16 #include <linux/of.h> 17 #include <linux/of_irq.h> 18 #include <linux/of_platform.h> 19 #include <linux/platform_device.h> 20 #include <linux/slab.h> 21 #include <linux/spinlock.h> 22 23 #include <soc/qcom/cmd-db.h> 24 #include <soc/qcom/tcs.h> 25 #include <dt-bindings/soc/qcom,rpmh-rsc.h> 26 27 #include "rpmh-internal.h" 28 29 #define CREATE_TRACE_POINTS 30 #include "trace-rpmh.h" 31 32 #define RSC_DRV_TCS_OFFSET 672 33 #define RSC_DRV_CMD_OFFSET 20 34 35 /* DRV HW Solver Configuration Information Register */ 36 #define DRV_SOLVER_CONFIG 0x04 37 #define DRV_HW_SOLVER_MASK 1 38 #define DRV_HW_SOLVER_SHIFT 24 39 40 /* DRV TCS Configuration Information Register */ 41 #define DRV_PRNT_CHLD_CONFIG 0x0C 42 #define DRV_NUM_TCS_MASK 0x3F 43 #define DRV_NUM_TCS_SHIFT 6 44 #define DRV_NCPT_MASK 0x1F 45 #define DRV_NCPT_SHIFT 27 46 47 /* Offsets for common TCS Registers, one bit per TCS */ 48 #define RSC_DRV_IRQ_ENABLE 0x00 49 #define RSC_DRV_IRQ_STATUS 0x04 50 #define RSC_DRV_IRQ_CLEAR 0x08 /* w/o; write 1 to clear */ 51 52 /* 53 * Offsets for per TCS Registers. 54 * 55 * TCSes start at 0x10 from tcs_base and are stored one after another. 56 * Multiply tcs_id by RSC_DRV_TCS_OFFSET to find a given TCS and add one 57 * of the below to find a register. 58 */ 59 #define RSC_DRV_CMD_WAIT_FOR_CMPL 0x10 /* 1 bit per command */ 60 #define RSC_DRV_CONTROL 0x14 61 #define RSC_DRV_STATUS 0x18 /* zero if tcs is busy */ 62 #define RSC_DRV_CMD_ENABLE 0x1C /* 1 bit per command */ 63 64 /* 65 * Offsets for per command in a TCS. 66 * 67 * Commands (up to 16) start at 0x30 in a TCS; multiply command index 68 * by RSC_DRV_CMD_OFFSET and add one of the below to find a register. 69 */ 70 #define RSC_DRV_CMD_MSGID 0x30 71 #define RSC_DRV_CMD_ADDR 0x34 72 #define RSC_DRV_CMD_DATA 0x38 73 #define RSC_DRV_CMD_STATUS 0x3C 74 #define RSC_DRV_CMD_RESP_DATA 0x40 75 76 #define TCS_AMC_MODE_ENABLE BIT(16) 77 #define TCS_AMC_MODE_TRIGGER BIT(24) 78 79 /* TCS CMD register bit mask */ 80 #define CMD_MSGID_LEN 8 81 #define CMD_MSGID_RESP_REQ BIT(8) 82 #define CMD_MSGID_WRITE BIT(16) 83 #define CMD_STATUS_ISSUED BIT(8) 84 #define CMD_STATUS_COMPL BIT(16) 85 86 /* 87 * Here's a high level overview of how all the registers in RPMH work 88 * together: 89 * 90 * - The main rpmh-rsc address is the base of a register space that can 91 * be used to find overall configuration of the hardware 92 * (DRV_PRNT_CHLD_CONFIG). Also found within the rpmh-rsc register 93 * space are all the TCS blocks. The offset of the TCS blocks is 94 * specified in the device tree by "qcom,tcs-offset" and used to 95 * compute tcs_base. 96 * - TCS blocks come one after another. Type, count, and order are 97 * specified by the device tree as "qcom,tcs-config". 98 * - Each TCS block has some registers, then space for up to 16 commands. 99 * Note that though address space is reserved for 16 commands, fewer 100 * might be present. See ncpt (num cmds per TCS). 101 * 102 * Here's a picture: 103 * 104 * +---------------------------------------------------+ 105 * |RSC | 106 * | ctrl | 107 * | | 108 * | Drvs: | 109 * | +-----------------------------------------------+ | 110 * | |DRV0 | | 111 * | | ctrl/config | | 112 * | | IRQ | | 113 * | | | | 114 * | | TCSes: | | 115 * | | +------------------------------------------+ | | 116 * | | |TCS0 | | | | | | | | | | | | | | | 117 * | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15| | | 118 * | | | | | | | | | | | | | | | | | | 119 * | | +------------------------------------------+ | | 120 * | | +------------------------------------------+ | | 121 * | | |TCS1 | | | | | | | | | | | | | | | 122 * | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15| | | 123 * | | | | | | | | | | | | | | | | | | 124 * | | +------------------------------------------+ | | 125 * | | +------------------------------------------+ | | 126 * | | |TCS2 | | | | | | | | | | | | | | | 127 * | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15| | | 128 * | | | | | | | | | | | | | | | | | | 129 * | | +------------------------------------------+ | | 130 * | | ...... | | 131 * | +-----------------------------------------------+ | 132 * | +-----------------------------------------------+ | 133 * | |DRV1 | | 134 * | | (same as DRV0) | | 135 * | +-----------------------------------------------+ | 136 * | ...... | 137 * +---------------------------------------------------+ 138 */ 139 140 static inline void __iomem * 141 tcs_reg_addr(const struct rsc_drv *drv, int reg, int tcs_id) 142 { 143 return drv->tcs_base + RSC_DRV_TCS_OFFSET * tcs_id + reg; 144 } 145 146 static inline void __iomem * 147 tcs_cmd_addr(const struct rsc_drv *drv, int reg, int tcs_id, int cmd_id) 148 { 149 return tcs_reg_addr(drv, reg, tcs_id) + RSC_DRV_CMD_OFFSET * cmd_id; 150 } 151 152 static u32 read_tcs_cmd(const struct rsc_drv *drv, int reg, int tcs_id, 153 int cmd_id) 154 { 155 return readl_relaxed(tcs_cmd_addr(drv, reg, tcs_id, cmd_id)); 156 } 157 158 static u32 read_tcs_reg(const struct rsc_drv *drv, int reg, int tcs_id) 159 { 160 return readl_relaxed(tcs_reg_addr(drv, reg, tcs_id)); 161 } 162 163 static void write_tcs_cmd(const struct rsc_drv *drv, int reg, int tcs_id, 164 int cmd_id, u32 data) 165 { 166 writel_relaxed(data, tcs_cmd_addr(drv, reg, tcs_id, cmd_id)); 167 } 168 169 static void write_tcs_reg(const struct rsc_drv *drv, int reg, int tcs_id, 170 u32 data) 171 { 172 writel_relaxed(data, tcs_reg_addr(drv, reg, tcs_id)); 173 } 174 175 static void write_tcs_reg_sync(const struct rsc_drv *drv, int reg, int tcs_id, 176 u32 data) 177 { 178 u32 new_data; 179 180 writel(data, tcs_reg_addr(drv, reg, tcs_id)); 181 if (readl_poll_timeout_atomic(tcs_reg_addr(drv, reg, tcs_id), new_data, 182 new_data == data, 1, USEC_PER_SEC)) 183 pr_err("%s: error writing %#x to %d:%#x\n", drv->name, 184 data, tcs_id, reg); 185 } 186 187 /** 188 * tcs_is_free() - Return if a TCS is totally free. 189 * @drv: The RSC controller. 190 * @tcs_id: The global ID of this TCS. 191 * 192 * Returns true if nobody has claimed this TCS (by setting tcs_in_use). 193 * 194 * Context: Must be called with the drv->lock held. 195 * 196 * Return: true if the given TCS is free. 197 */ 198 static bool tcs_is_free(struct rsc_drv *drv, int tcs_id) 199 { 200 return !test_bit(tcs_id, drv->tcs_in_use); 201 } 202 203 /** 204 * tcs_invalidate() - Invalidate all TCSes of the given type (sleep or wake). 205 * @drv: The RSC controller. 206 * @type: SLEEP_TCS or WAKE_TCS 207 * 208 * This will clear the "slots" variable of the given tcs_group and also 209 * tell the hardware to forget about all entries. 210 * 211 * The caller must ensure that no other RPMH actions are happening when this 212 * function is called, since otherwise the device may immediately become 213 * used again even before this function exits. 214 */ 215 static void tcs_invalidate(struct rsc_drv *drv, int type) 216 { 217 int m; 218 struct tcs_group *tcs = &drv->tcs[type]; 219 220 /* Caller ensures nobody else is running so no lock */ 221 if (bitmap_empty(tcs->slots, MAX_TCS_SLOTS)) 222 return; 223 224 for (m = tcs->offset; m < tcs->offset + tcs->num_tcs; m++) { 225 write_tcs_reg_sync(drv, RSC_DRV_CMD_ENABLE, m, 0); 226 write_tcs_reg_sync(drv, RSC_DRV_CMD_WAIT_FOR_CMPL, m, 0); 227 } 228 bitmap_zero(tcs->slots, MAX_TCS_SLOTS); 229 } 230 231 /** 232 * rpmh_rsc_invalidate() - Invalidate sleep and wake TCSes. 233 * @drv: The RSC controller. 234 * 235 * The caller must ensure that no other RPMH actions are happening when this 236 * function is called, since otherwise the device may immediately become 237 * used again even before this function exits. 238 */ 239 void rpmh_rsc_invalidate(struct rsc_drv *drv) 240 { 241 tcs_invalidate(drv, SLEEP_TCS); 242 tcs_invalidate(drv, WAKE_TCS); 243 } 244 245 /** 246 * get_tcs_for_msg() - Get the tcs_group used to send the given message. 247 * @drv: The RSC controller. 248 * @msg: The message we want to send. 249 * 250 * This is normally pretty straightforward except if we are trying to send 251 * an ACTIVE_ONLY message but don't have any active_only TCSes. 252 * 253 * Return: A pointer to a tcs_group or an ERR_PTR. 254 */ 255 static struct tcs_group *get_tcs_for_msg(struct rsc_drv *drv, 256 const struct tcs_request *msg) 257 { 258 int type; 259 struct tcs_group *tcs; 260 261 switch (msg->state) { 262 case RPMH_ACTIVE_ONLY_STATE: 263 type = ACTIVE_TCS; 264 break; 265 case RPMH_WAKE_ONLY_STATE: 266 type = WAKE_TCS; 267 break; 268 case RPMH_SLEEP_STATE: 269 type = SLEEP_TCS; 270 break; 271 default: 272 return ERR_PTR(-EINVAL); 273 } 274 275 /* 276 * If we are making an active request on a RSC that does not have a 277 * dedicated TCS for active state use, then re-purpose a wake TCS to 278 * send active votes. This is safe because we ensure any active-only 279 * transfers have finished before we use it (maybe by running from 280 * the last CPU in PM code). 281 */ 282 tcs = &drv->tcs[type]; 283 if (msg->state == RPMH_ACTIVE_ONLY_STATE && !tcs->num_tcs) 284 tcs = &drv->tcs[WAKE_TCS]; 285 286 return tcs; 287 } 288 289 /** 290 * get_req_from_tcs() - Get a stashed request that was xfering on the given TCS. 291 * @drv: The RSC controller. 292 * @tcs_id: The global ID of this TCS. 293 * 294 * For ACTIVE_ONLY transfers we want to call back into the client when the 295 * transfer finishes. To do this we need the "request" that the client 296 * originally provided us. This function grabs the request that we stashed 297 * when we started the transfer. 298 * 299 * This only makes sense for ACTIVE_ONLY transfers since those are the only 300 * ones we track sending (the only ones we enable interrupts for and the only 301 * ones we call back to the client for). 302 * 303 * Return: The stashed request. 304 */ 305 static const struct tcs_request *get_req_from_tcs(struct rsc_drv *drv, 306 int tcs_id) 307 { 308 struct tcs_group *tcs; 309 int i; 310 311 for (i = 0; i < TCS_TYPE_NR; i++) { 312 tcs = &drv->tcs[i]; 313 if (tcs->mask & BIT(tcs_id)) 314 return tcs->req[tcs_id - tcs->offset]; 315 } 316 317 return NULL; 318 } 319 320 /** 321 * __tcs_set_trigger() - Start xfer on a TCS or unset trigger on a borrowed TCS 322 * @drv: The controller. 323 * @tcs_id: The global ID of this TCS. 324 * @trigger: If true then untrigger/retrigger. If false then just untrigger. 325 * 326 * In the normal case we only ever call with "trigger=true" to start a 327 * transfer. That will un-trigger/disable the TCS from the last transfer 328 * then trigger/enable for this transfer. 329 * 330 * If we borrowed a wake TCS for an active-only transfer we'll also call 331 * this function with "trigger=false" to just do the un-trigger/disable 332 * before using the TCS for wake purposes again. 333 * 334 * Note that the AP is only in charge of triggering active-only transfers. 335 * The AP never triggers sleep/wake values using this function. 336 */ 337 static void __tcs_set_trigger(struct rsc_drv *drv, int tcs_id, bool trigger) 338 { 339 u32 enable; 340 341 /* 342 * HW req: Clear the DRV_CONTROL and enable TCS again 343 * While clearing ensure that the AMC mode trigger is cleared 344 * and then the mode enable is cleared. 345 */ 346 enable = read_tcs_reg(drv, RSC_DRV_CONTROL, tcs_id); 347 enable &= ~TCS_AMC_MODE_TRIGGER; 348 write_tcs_reg_sync(drv, RSC_DRV_CONTROL, tcs_id, enable); 349 enable &= ~TCS_AMC_MODE_ENABLE; 350 write_tcs_reg_sync(drv, RSC_DRV_CONTROL, tcs_id, enable); 351 352 if (trigger) { 353 /* Enable the AMC mode on the TCS and then trigger the TCS */ 354 enable = TCS_AMC_MODE_ENABLE; 355 write_tcs_reg_sync(drv, RSC_DRV_CONTROL, tcs_id, enable); 356 enable |= TCS_AMC_MODE_TRIGGER; 357 write_tcs_reg_sync(drv, RSC_DRV_CONTROL, tcs_id, enable); 358 } 359 } 360 361 /** 362 * enable_tcs_irq() - Enable or disable interrupts on the given TCS. 363 * @drv: The controller. 364 * @tcs_id: The global ID of this TCS. 365 * @enable: If true then enable; if false then disable 366 * 367 * We only ever call this when we borrow a wake TCS for an active-only 368 * transfer. For active-only TCSes interrupts are always left enabled. 369 */ 370 static void enable_tcs_irq(struct rsc_drv *drv, int tcs_id, bool enable) 371 { 372 u32 data; 373 374 data = readl_relaxed(drv->tcs_base + RSC_DRV_IRQ_ENABLE); 375 if (enable) 376 data |= BIT(tcs_id); 377 else 378 data &= ~BIT(tcs_id); 379 writel_relaxed(data, drv->tcs_base + RSC_DRV_IRQ_ENABLE); 380 } 381 382 /** 383 * tcs_tx_done() - TX Done interrupt handler. 384 * @irq: The IRQ number (ignored). 385 * @p: Pointer to "struct rsc_drv". 386 * 387 * Called for ACTIVE_ONLY transfers (those are the only ones we enable the 388 * IRQ for) when a transfer is done. 389 * 390 * Return: IRQ_HANDLED 391 */ 392 static irqreturn_t tcs_tx_done(int irq, void *p) 393 { 394 struct rsc_drv *drv = p; 395 int i, j, err = 0; 396 unsigned long irq_status; 397 const struct tcs_request *req; 398 struct tcs_cmd *cmd; 399 400 irq_status = readl_relaxed(drv->tcs_base + RSC_DRV_IRQ_STATUS); 401 402 for_each_set_bit(i, &irq_status, BITS_PER_LONG) { 403 req = get_req_from_tcs(drv, i); 404 if (!req) { 405 WARN_ON(1); 406 goto skip; 407 } 408 409 err = 0; 410 for (j = 0; j < req->num_cmds; j++) { 411 u32 sts; 412 413 cmd = &req->cmds[j]; 414 sts = read_tcs_cmd(drv, RSC_DRV_CMD_STATUS, i, j); 415 if (!(sts & CMD_STATUS_ISSUED) || 416 ((req->wait_for_compl || cmd->wait) && 417 !(sts & CMD_STATUS_COMPL))) { 418 pr_err("Incomplete request: %s: addr=%#x data=%#x", 419 drv->name, cmd->addr, cmd->data); 420 err = -EIO; 421 } 422 } 423 424 trace_rpmh_tx_done(drv, i, req, err); 425 426 /* 427 * If wake tcs was re-purposed for sending active 428 * votes, clear AMC trigger & enable modes and 429 * disable interrupt for this TCS 430 */ 431 if (!drv->tcs[ACTIVE_TCS].num_tcs) 432 __tcs_set_trigger(drv, i, false); 433 skip: 434 /* Reclaim the TCS */ 435 write_tcs_reg(drv, RSC_DRV_CMD_ENABLE, i, 0); 436 write_tcs_reg(drv, RSC_DRV_CMD_WAIT_FOR_CMPL, i, 0); 437 writel_relaxed(BIT(i), drv->tcs_base + RSC_DRV_IRQ_CLEAR); 438 spin_lock(&drv->lock); 439 clear_bit(i, drv->tcs_in_use); 440 /* 441 * Disable interrupt for WAKE TCS to avoid being 442 * spammed with interrupts coming when the solver 443 * sends its wake votes. 444 */ 445 if (!drv->tcs[ACTIVE_TCS].num_tcs) 446 enable_tcs_irq(drv, i, false); 447 spin_unlock(&drv->lock); 448 if (req) 449 rpmh_tx_done(req, err); 450 } 451 452 return IRQ_HANDLED; 453 } 454 455 /** 456 * __tcs_buffer_write() - Write to TCS hardware from a request; don't trigger. 457 * @drv: The controller. 458 * @tcs_id: The global ID of this TCS. 459 * @cmd_id: The index within the TCS to start writing. 460 * @msg: The message we want to send, which will contain several addr/data 461 * pairs to program (but few enough that they all fit in one TCS). 462 * 463 * This is used for all types of transfers (active, sleep, and wake). 464 */ 465 static void __tcs_buffer_write(struct rsc_drv *drv, int tcs_id, int cmd_id, 466 const struct tcs_request *msg) 467 { 468 u32 msgid, cmd_msgid; 469 u32 cmd_enable = 0; 470 u32 cmd_complete; 471 struct tcs_cmd *cmd; 472 int i, j; 473 474 cmd_msgid = CMD_MSGID_LEN; 475 cmd_msgid |= msg->wait_for_compl ? CMD_MSGID_RESP_REQ : 0; 476 cmd_msgid |= CMD_MSGID_WRITE; 477 478 cmd_complete = read_tcs_reg(drv, RSC_DRV_CMD_WAIT_FOR_CMPL, tcs_id); 479 480 for (i = 0, j = cmd_id; i < msg->num_cmds; i++, j++) { 481 cmd = &msg->cmds[i]; 482 cmd_enable |= BIT(j); 483 cmd_complete |= cmd->wait << j; 484 msgid = cmd_msgid; 485 msgid |= cmd->wait ? CMD_MSGID_RESP_REQ : 0; 486 487 write_tcs_cmd(drv, RSC_DRV_CMD_MSGID, tcs_id, j, msgid); 488 write_tcs_cmd(drv, RSC_DRV_CMD_ADDR, tcs_id, j, cmd->addr); 489 write_tcs_cmd(drv, RSC_DRV_CMD_DATA, tcs_id, j, cmd->data); 490 trace_rpmh_send_msg_rcuidle(drv, tcs_id, j, msgid, cmd); 491 } 492 493 write_tcs_reg(drv, RSC_DRV_CMD_WAIT_FOR_CMPL, tcs_id, cmd_complete); 494 cmd_enable |= read_tcs_reg(drv, RSC_DRV_CMD_ENABLE, tcs_id); 495 write_tcs_reg(drv, RSC_DRV_CMD_ENABLE, tcs_id, cmd_enable); 496 } 497 498 /** 499 * check_for_req_inflight() - Look to see if conflicting cmds are in flight. 500 * @drv: The controller. 501 * @tcs: A pointer to the tcs_group used for ACTIVE_ONLY transfers. 502 * @msg: The message we want to send, which will contain several addr/data 503 * pairs to program (but few enough that they all fit in one TCS). 504 * 505 * This will walk through the TCSes in the group and check if any of them 506 * appear to be sending to addresses referenced in the message. If it finds 507 * one it'll return -EBUSY. 508 * 509 * Only for use for active-only transfers. 510 * 511 * Must be called with the drv->lock held since that protects tcs_in_use. 512 * 513 * Return: 0 if nothing in flight or -EBUSY if we should try again later. 514 * The caller must re-enable interrupts between tries since that's 515 * the only way tcs_is_free() will ever return true and the only way 516 * RSC_DRV_CMD_ENABLE will ever be cleared. 517 */ 518 static int check_for_req_inflight(struct rsc_drv *drv, struct tcs_group *tcs, 519 const struct tcs_request *msg) 520 { 521 unsigned long curr_enabled; 522 u32 addr; 523 int i, j, k; 524 int tcs_id = tcs->offset; 525 526 for (i = 0; i < tcs->num_tcs; i++, tcs_id++) { 527 if (tcs_is_free(drv, tcs_id)) 528 continue; 529 530 curr_enabled = read_tcs_reg(drv, RSC_DRV_CMD_ENABLE, tcs_id); 531 532 for_each_set_bit(j, &curr_enabled, MAX_CMDS_PER_TCS) { 533 addr = read_tcs_cmd(drv, RSC_DRV_CMD_ADDR, tcs_id, j); 534 for (k = 0; k < msg->num_cmds; k++) { 535 if (addr == msg->cmds[k].addr) 536 return -EBUSY; 537 } 538 } 539 } 540 541 return 0; 542 } 543 544 /** 545 * find_free_tcs() - Find free tcs in the given tcs_group; only for active. 546 * @tcs: A pointer to the active-only tcs_group (or the wake tcs_group if 547 * we borrowed it because there are zero active-only ones). 548 * 549 * Must be called with the drv->lock held since that protects tcs_in_use. 550 * 551 * Return: The first tcs that's free. 552 */ 553 static int find_free_tcs(struct tcs_group *tcs) 554 { 555 int i; 556 557 for (i = 0; i < tcs->num_tcs; i++) { 558 if (tcs_is_free(tcs->drv, tcs->offset + i)) 559 return tcs->offset + i; 560 } 561 562 return -EBUSY; 563 } 564 565 /** 566 * tcs_write() - Store messages into a TCS right now, or return -EBUSY. 567 * @drv: The controller. 568 * @msg: The data to be sent. 569 * 570 * Grabs a TCS for ACTIVE_ONLY transfers and writes the messages to it. 571 * 572 * If there are no free TCSes for ACTIVE_ONLY transfers or if a command for 573 * the same address is already transferring returns -EBUSY which means the 574 * client should retry shortly. 575 * 576 * Return: 0 on success, -EBUSY if client should retry, or an error. 577 * Client should have interrupts enabled for a bit before retrying. 578 */ 579 static int tcs_write(struct rsc_drv *drv, const struct tcs_request *msg) 580 { 581 struct tcs_group *tcs; 582 int tcs_id; 583 unsigned long flags; 584 int ret; 585 586 tcs = get_tcs_for_msg(drv, msg); 587 if (IS_ERR(tcs)) 588 return PTR_ERR(tcs); 589 590 spin_lock_irqsave(&drv->lock, flags); 591 /* 592 * The h/w does not like if we send a request to the same address, 593 * when one is already in-flight or being processed. 594 */ 595 ret = check_for_req_inflight(drv, tcs, msg); 596 if (ret) 597 goto unlock; 598 599 ret = find_free_tcs(tcs); 600 if (ret < 0) 601 goto unlock; 602 tcs_id = ret; 603 604 tcs->req[tcs_id - tcs->offset] = msg; 605 set_bit(tcs_id, drv->tcs_in_use); 606 if (msg->state == RPMH_ACTIVE_ONLY_STATE && tcs->type != ACTIVE_TCS) { 607 /* 608 * Clear previously programmed WAKE commands in selected 609 * repurposed TCS to avoid triggering them. tcs->slots will be 610 * cleaned from rpmh_flush() by invoking rpmh_rsc_invalidate() 611 */ 612 write_tcs_reg_sync(drv, RSC_DRV_CMD_ENABLE, tcs_id, 0); 613 write_tcs_reg_sync(drv, RSC_DRV_CMD_WAIT_FOR_CMPL, tcs_id, 0); 614 enable_tcs_irq(drv, tcs_id, true); 615 } 616 spin_unlock_irqrestore(&drv->lock, flags); 617 618 /* 619 * These two can be done after the lock is released because: 620 * - We marked "tcs_in_use" under lock. 621 * - Once "tcs_in_use" has been marked nobody else could be writing 622 * to these registers until the interrupt goes off. 623 * - The interrupt can't go off until we trigger w/ the last line 624 * of __tcs_set_trigger() below. 625 */ 626 __tcs_buffer_write(drv, tcs_id, 0, msg); 627 __tcs_set_trigger(drv, tcs_id, true); 628 629 return 0; 630 unlock: 631 spin_unlock_irqrestore(&drv->lock, flags); 632 return ret; 633 } 634 635 /** 636 * rpmh_rsc_send_data() - Write / trigger active-only message. 637 * @drv: The controller. 638 * @msg: The data to be sent. 639 * 640 * NOTES: 641 * - This is only used for "ACTIVE_ONLY" since the limitations of this 642 * function don't make sense for sleep/wake cases. 643 * - To do the transfer, we will grab a whole TCS for ourselves--we don't 644 * try to share. If there are none available we'll wait indefinitely 645 * for a free one. 646 * - This function will not wait for the commands to be finished, only for 647 * data to be programmed into the RPMh. See rpmh_tx_done() which will 648 * be called when the transfer is fully complete. 649 * - This function must be called with interrupts enabled. If the hardware 650 * is busy doing someone else's transfer we need that transfer to fully 651 * finish so that we can have the hardware, and to fully finish it needs 652 * the interrupt handler to run. If the interrupts is set to run on the 653 * active CPU this can never happen if interrupts are disabled. 654 * 655 * Return: 0 on success, -EINVAL on error. 656 */ 657 int rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg) 658 { 659 int ret; 660 661 do { 662 ret = tcs_write(drv, msg); 663 if (ret == -EBUSY) { 664 pr_info_ratelimited("TCS Busy, retrying RPMH message send: addr=%#x\n", 665 msg->cmds[0].addr); 666 udelay(10); 667 } 668 } while (ret == -EBUSY); 669 670 return ret; 671 } 672 673 /** 674 * find_slots() - Find a place to write the given message. 675 * @tcs: The tcs group to search. 676 * @msg: The message we want to find room for. 677 * @tcs_id: If we return 0 from the function, we return the global ID of the 678 * TCS to write to here. 679 * @cmd_id: If we return 0 from the function, we return the index of 680 * the command array of the returned TCS where the client should 681 * start writing the message. 682 * 683 * Only for use on sleep/wake TCSes since those are the only ones we maintain 684 * tcs->slots for. 685 * 686 * Return: -ENOMEM if there was no room, else 0. 687 */ 688 static int find_slots(struct tcs_group *tcs, const struct tcs_request *msg, 689 int *tcs_id, int *cmd_id) 690 { 691 int slot, offset; 692 int i = 0; 693 694 /* Do over, until we can fit the full payload in a single TCS */ 695 do { 696 slot = bitmap_find_next_zero_area(tcs->slots, MAX_TCS_SLOTS, 697 i, msg->num_cmds, 0); 698 if (slot >= tcs->num_tcs * tcs->ncpt) 699 return -ENOMEM; 700 i += tcs->ncpt; 701 } while (slot + msg->num_cmds - 1 >= i); 702 703 bitmap_set(tcs->slots, slot, msg->num_cmds); 704 705 offset = slot / tcs->ncpt; 706 *tcs_id = offset + tcs->offset; 707 *cmd_id = slot % tcs->ncpt; 708 709 return 0; 710 } 711 712 /** 713 * rpmh_rsc_write_ctrl_data() - Write request to controller but don't trigger. 714 * @drv: The controller. 715 * @msg: The data to be written to the controller. 716 * 717 * This should only be called for for sleep/wake state, never active-only 718 * state. 719 * 720 * The caller must ensure that no other RPMH actions are happening and the 721 * controller is idle when this function is called since it runs lockless. 722 * 723 * Return: 0 if no error; else -error. 724 */ 725 int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv, const struct tcs_request *msg) 726 { 727 struct tcs_group *tcs; 728 int tcs_id = 0, cmd_id = 0; 729 int ret; 730 731 tcs = get_tcs_for_msg(drv, msg); 732 if (IS_ERR(tcs)) 733 return PTR_ERR(tcs); 734 735 /* find the TCS id and the command in the TCS to write to */ 736 ret = find_slots(tcs, msg, &tcs_id, &cmd_id); 737 if (!ret) 738 __tcs_buffer_write(drv, tcs_id, cmd_id, msg); 739 740 return ret; 741 } 742 743 /** 744 * rpmh_rsc_ctrlr_is_busy() - Check if any of the AMCs are busy. 745 * @drv: The controller 746 * 747 * Checks if any of the AMCs are busy in handling ACTIVE sets. 748 * This is called from the last cpu powering down before flushing 749 * SLEEP and WAKE sets. If AMCs are busy, controller can not enter 750 * power collapse, so deny from the last cpu's pm notification. 751 * 752 * Context: Must be called with the drv->lock held. 753 * 754 * Return: 755 * * False - AMCs are idle 756 * * True - AMCs are busy 757 */ 758 static bool rpmh_rsc_ctrlr_is_busy(struct rsc_drv *drv) 759 { 760 int m; 761 struct tcs_group *tcs = &drv->tcs[ACTIVE_TCS]; 762 763 /* 764 * If we made an active request on a RSC that does not have a 765 * dedicated TCS for active state use, then re-purposed wake TCSes 766 * should be checked for not busy, because we used wake TCSes for 767 * active requests in this case. 768 */ 769 if (!tcs->num_tcs) 770 tcs = &drv->tcs[WAKE_TCS]; 771 772 for (m = tcs->offset; m < tcs->offset + tcs->num_tcs; m++) { 773 if (!tcs_is_free(drv, m)) 774 return true; 775 } 776 777 return false; 778 } 779 780 /** 781 * rpmh_rsc_cpu_pm_callback() - Check if any of the AMCs are busy. 782 * @nfb: Pointer to the notifier block in struct rsc_drv. 783 * @action: CPU_PM_ENTER, CPU_PM_ENTER_FAILED, or CPU_PM_EXIT. 784 * @v: Unused 785 * 786 * This function is given to cpu_pm_register_notifier so we can be informed 787 * about when CPUs go down. When all CPUs go down we know no more active 788 * transfers will be started so we write sleep/wake sets. This function gets 789 * called from cpuidle code paths and also at system suspend time. 790 * 791 * If its last CPU going down and AMCs are not busy then writes cached sleep 792 * and wake messages to TCSes. The firmware then takes care of triggering 793 * them when entering deepest low power modes. 794 * 795 * Return: See cpu_pm_register_notifier() 796 */ 797 static int rpmh_rsc_cpu_pm_callback(struct notifier_block *nfb, 798 unsigned long action, void *v) 799 { 800 struct rsc_drv *drv = container_of(nfb, struct rsc_drv, rsc_pm); 801 int ret = NOTIFY_OK; 802 int cpus_in_pm; 803 804 switch (action) { 805 case CPU_PM_ENTER: 806 cpus_in_pm = atomic_inc_return(&drv->cpus_in_pm); 807 /* 808 * NOTE: comments for num_online_cpus() point out that it's 809 * only a snapshot so we need to be careful. It should be OK 810 * for us to use, though. It's important for us not to miss 811 * if we're the last CPU going down so it would only be a 812 * problem if a CPU went offline right after we did the check 813 * AND that CPU was not idle AND that CPU was the last non-idle 814 * CPU. That can't happen. CPUs would have to come out of idle 815 * before the CPU could go offline. 816 */ 817 if (cpus_in_pm < num_online_cpus()) 818 return NOTIFY_OK; 819 break; 820 case CPU_PM_ENTER_FAILED: 821 case CPU_PM_EXIT: 822 atomic_dec(&drv->cpus_in_pm); 823 return NOTIFY_OK; 824 default: 825 return NOTIFY_DONE; 826 } 827 828 /* 829 * It's likely we're on the last CPU. Grab the drv->lock and write 830 * out the sleep/wake commands to RPMH hardware. Grabbing the lock 831 * means that if we race with another CPU coming up we are still 832 * guaranteed to be safe. If another CPU came up just after we checked 833 * and has grabbed the lock or started an active transfer then we'll 834 * notice we're busy and abort. If another CPU comes up after we start 835 * flushing it will be blocked from starting an active transfer until 836 * we're done flushing. If another CPU starts an active transfer after 837 * we release the lock we're still OK because we're no longer the last 838 * CPU. 839 */ 840 if (spin_trylock(&drv->lock)) { 841 if (rpmh_rsc_ctrlr_is_busy(drv) || rpmh_flush(&drv->client)) 842 ret = NOTIFY_BAD; 843 spin_unlock(&drv->lock); 844 } else { 845 /* Another CPU must be up */ 846 return NOTIFY_OK; 847 } 848 849 if (ret == NOTIFY_BAD) { 850 /* Double-check if we're here because someone else is up */ 851 if (cpus_in_pm < num_online_cpus()) 852 ret = NOTIFY_OK; 853 else 854 /* We won't be called w/ CPU_PM_ENTER_FAILED */ 855 atomic_dec(&drv->cpus_in_pm); 856 } 857 858 return ret; 859 } 860 861 static int rpmh_probe_tcs_config(struct platform_device *pdev, 862 struct rsc_drv *drv, void __iomem *base) 863 { 864 struct tcs_type_config { 865 u32 type; 866 u32 n; 867 } tcs_cfg[TCS_TYPE_NR] = { { 0 } }; 868 struct device_node *dn = pdev->dev.of_node; 869 u32 config, max_tcs, ncpt, offset; 870 int i, ret, n, st = 0; 871 struct tcs_group *tcs; 872 873 ret = of_property_read_u32(dn, "qcom,tcs-offset", &offset); 874 if (ret) 875 return ret; 876 drv->tcs_base = base + offset; 877 878 config = readl_relaxed(base + DRV_PRNT_CHLD_CONFIG); 879 880 max_tcs = config; 881 max_tcs &= DRV_NUM_TCS_MASK << (DRV_NUM_TCS_SHIFT * drv->id); 882 max_tcs = max_tcs >> (DRV_NUM_TCS_SHIFT * drv->id); 883 884 ncpt = config & (DRV_NCPT_MASK << DRV_NCPT_SHIFT); 885 ncpt = ncpt >> DRV_NCPT_SHIFT; 886 887 n = of_property_count_u32_elems(dn, "qcom,tcs-config"); 888 if (n != 2 * TCS_TYPE_NR) 889 return -EINVAL; 890 891 for (i = 0; i < TCS_TYPE_NR; i++) { 892 ret = of_property_read_u32_index(dn, "qcom,tcs-config", 893 i * 2, &tcs_cfg[i].type); 894 if (ret) 895 return ret; 896 if (tcs_cfg[i].type >= TCS_TYPE_NR) 897 return -EINVAL; 898 899 ret = of_property_read_u32_index(dn, "qcom,tcs-config", 900 i * 2 + 1, &tcs_cfg[i].n); 901 if (ret) 902 return ret; 903 if (tcs_cfg[i].n > MAX_TCS_PER_TYPE) 904 return -EINVAL; 905 } 906 907 for (i = 0; i < TCS_TYPE_NR; i++) { 908 tcs = &drv->tcs[tcs_cfg[i].type]; 909 if (tcs->drv) 910 return -EINVAL; 911 tcs->drv = drv; 912 tcs->type = tcs_cfg[i].type; 913 tcs->num_tcs = tcs_cfg[i].n; 914 tcs->ncpt = ncpt; 915 916 if (!tcs->num_tcs || tcs->type == CONTROL_TCS) 917 continue; 918 919 if (st + tcs->num_tcs > max_tcs || 920 st + tcs->num_tcs >= BITS_PER_BYTE * sizeof(tcs->mask)) 921 return -EINVAL; 922 923 tcs->mask = ((1 << tcs->num_tcs) - 1) << st; 924 tcs->offset = st; 925 st += tcs->num_tcs; 926 } 927 928 drv->num_tcs = st; 929 930 return 0; 931 } 932 933 static int rpmh_rsc_probe(struct platform_device *pdev) 934 { 935 struct device_node *dn = pdev->dev.of_node; 936 struct rsc_drv *drv; 937 struct resource *res; 938 char drv_id[10] = {0}; 939 int ret, irq; 940 u32 solver_config; 941 void __iomem *base; 942 943 /* 944 * Even though RPMh doesn't directly use cmd-db, all of its children 945 * do. To avoid adding this check to our children we'll do it now. 946 */ 947 ret = cmd_db_ready(); 948 if (ret) { 949 if (ret != -EPROBE_DEFER) 950 dev_err(&pdev->dev, "Command DB not available (%d)\n", 951 ret); 952 return ret; 953 } 954 955 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); 956 if (!drv) 957 return -ENOMEM; 958 959 ret = of_property_read_u32(dn, "qcom,drv-id", &drv->id); 960 if (ret) 961 return ret; 962 963 drv->name = of_get_property(dn, "label", NULL); 964 if (!drv->name) 965 drv->name = dev_name(&pdev->dev); 966 967 snprintf(drv_id, ARRAY_SIZE(drv_id), "drv-%d", drv->id); 968 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, drv_id); 969 base = devm_ioremap_resource(&pdev->dev, res); 970 if (IS_ERR(base)) 971 return PTR_ERR(base); 972 973 ret = rpmh_probe_tcs_config(pdev, drv, base); 974 if (ret) 975 return ret; 976 977 spin_lock_init(&drv->lock); 978 bitmap_zero(drv->tcs_in_use, MAX_TCS_NR); 979 980 irq = platform_get_irq(pdev, drv->id); 981 if (irq < 0) 982 return irq; 983 984 ret = devm_request_irq(&pdev->dev, irq, tcs_tx_done, 985 IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND, 986 drv->name, drv); 987 if (ret) 988 return ret; 989 990 /* 991 * CPU PM notification are not required for controllers that support 992 * 'HW solver' mode where they can be in autonomous mode executing low 993 * power mode to power down. 994 */ 995 solver_config = readl_relaxed(base + DRV_SOLVER_CONFIG); 996 solver_config &= DRV_HW_SOLVER_MASK << DRV_HW_SOLVER_SHIFT; 997 solver_config = solver_config >> DRV_HW_SOLVER_SHIFT; 998 if (!solver_config) { 999 drv->rsc_pm.notifier_call = rpmh_rsc_cpu_pm_callback; 1000 cpu_pm_register_notifier(&drv->rsc_pm); 1001 } 1002 1003 /* Enable the active TCS to send requests immediately */ 1004 writel_relaxed(drv->tcs[ACTIVE_TCS].mask, 1005 drv->tcs_base + RSC_DRV_IRQ_ENABLE); 1006 1007 spin_lock_init(&drv->client.cache_lock); 1008 INIT_LIST_HEAD(&drv->client.cache); 1009 INIT_LIST_HEAD(&drv->client.batch_cache); 1010 1011 dev_set_drvdata(&pdev->dev, drv); 1012 1013 return devm_of_platform_populate(&pdev->dev); 1014 } 1015 1016 static const struct of_device_id rpmh_drv_match[] = { 1017 { .compatible = "qcom,rpmh-rsc", }, 1018 { } 1019 }; 1020 1021 static struct platform_driver rpmh_driver = { 1022 .probe = rpmh_rsc_probe, 1023 .driver = { 1024 .name = "rpmh", 1025 .of_match_table = rpmh_drv_match, 1026 }, 1027 }; 1028 1029 static int __init rpmh_driver_init(void) 1030 { 1031 return platform_driver_register(&rpmh_driver); 1032 } 1033 arch_initcall(rpmh_driver_init); 1034