Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drivers: qcom: rpmh-rsc: write sleep/wake requests to TCS

Sleep and wake requests are sent when the application processor
subsystem of the SoC is entering deep sleep states like in suspend.
These requests help lower the system power requirements when the
resources are not in use.

Sleep and wake requests are written to the TCS slots but are not
triggered at the time of writing. The TCS are triggered by the firmware
after the last of the CPUs has executed its WFI. Since these requests
may come in different batches of requests, it is the job of this
controller driver to find and arrange the requests into the available
TCSes.

Signed-off-by: Lina Iyer <ilina@codeaurora.org>
Signed-off-by: Raju P.L.S.S.S.N <rplsssn@codeaurora.org>
Reviewed-by: Evan Green <evgreen@chromium.org>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
Signed-off-by: Andy Gross <andy.gross@linaro.org>

authored by

Lina Iyer and committed by
Andy Gross
fa460e45 c1038456

+129
+8
drivers/soc/qcom/rpmh-internal.h
··· 14 14 #define MAX_CMDS_PER_TCS 16 15 15 #define MAX_TCS_PER_TYPE 3 16 16 #define MAX_TCS_NR (MAX_TCS_PER_TYPE * TCS_TYPE_NR) 17 + #define MAX_TCS_SLOTS (MAX_CMDS_PER_TCS * MAX_TCS_PER_TYPE) 17 18 18 19 struct rsc_drv; 19 20 ··· 30 29 * @ncpt: number of commands in each TCS 31 30 * @lock: lock for synchronizing this TCS writes 32 31 * @req: requests that are sent from the TCS 32 + * @cmd_cache: flattened cache of cmds in sleep/wake TCS 33 + * @slots: indicates which of @cmd_addr are occupied 33 34 */ 34 35 struct tcs_group { 35 36 struct rsc_drv *drv; ··· 42 39 int ncpt; 43 40 spinlock_t lock; 44 41 const struct tcs_request *req[MAX_TCS_PER_TYPE]; 42 + u32 *cmd_cache; 43 + DECLARE_BITMAP(slots, MAX_TCS_SLOTS); 45 44 }; 46 45 47 46 /** ··· 97 92 }; 98 93 99 94 int rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg); 95 + int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv, 96 + const struct tcs_request *msg); 97 + int rpmh_rsc_invalidate(struct rsc_drv *drv); 100 98 101 99 void rpmh_tx_done(const struct tcs_request *msg, int r); 102 100
+121
drivers/soc/qcom/rpmh-rsc.c
··· 110 110 case RPMH_ACTIVE_ONLY_STATE: 111 111 type = ACTIVE_TCS; 112 112 break; 113 + case RPMH_WAKE_ONLY_STATE: 114 + type = WAKE_TCS; 115 + break; 116 + case RPMH_SLEEP_STATE: 117 + type = SLEEP_TCS; 118 + break; 113 119 default: 114 120 return ERR_PTR(-EINVAL); 115 121 } ··· 355 349 return ret; 356 350 } 357 351 352 + static int find_match(const struct tcs_group *tcs, const struct tcs_cmd *cmd, 353 + int len) 354 + { 355 + int i, j; 356 + 357 + /* Check for already cached commands */ 358 + for_each_set_bit(i, tcs->slots, MAX_TCS_SLOTS) { 359 + if (tcs->cmd_cache[i] != cmd[0].addr) 360 + continue; 361 + if (i + len >= tcs->num_tcs * tcs->ncpt) 362 + goto seq_err; 363 + for (j = 0; j < len; j++) { 364 + if (tcs->cmd_cache[i + j] != cmd[j].addr) 365 + goto seq_err; 366 + } 367 + return i; 368 + } 369 + 370 + return -ENODATA; 371 + 372 + seq_err: 373 + WARN(1, "Message does not match previous sequence.\n"); 374 + return -EINVAL; 375 + } 376 + 377 + static int find_slots(struct tcs_group *tcs, const struct tcs_request *msg, 378 + int *tcs_id, int *cmd_id) 379 + { 380 + int slot, offset; 381 + int i = 0; 382 + 383 + /* Find if we already have the msg in our TCS */ 384 + slot = find_match(tcs, msg->cmds, msg->num_cmds); 385 + if (slot >= 0) 386 + goto copy_data; 387 + 388 + /* Do over, until we can fit the full payload in a TCS */ 389 + do { 390 + slot = bitmap_find_next_zero_area(tcs->slots, MAX_TCS_SLOTS, 391 + i, msg->num_cmds, 0); 392 + if (slot == tcs->num_tcs * tcs->ncpt) 393 + return -ENOMEM; 394 + i += tcs->ncpt; 395 + } while (slot + msg->num_cmds - 1 >= i); 396 + 397 + copy_data: 398 + bitmap_set(tcs->slots, slot, msg->num_cmds); 399 + /* Copy the addresses of the resources over to the slots */ 400 + for (i = 0; i < msg->num_cmds; i++) 401 + tcs->cmd_cache[slot + i] = msg->cmds[i].addr; 402 + 403 + offset = slot / tcs->ncpt; 404 + *tcs_id = offset + tcs->offset; 405 + *cmd_id = slot % tcs->ncpt; 406 + 407 + return 0; 408 + } 409 + 410 + static int tcs_ctrl_write(struct rsc_drv *drv, const struct tcs_request *msg) 411 + { 412 + struct tcs_group *tcs; 413 + int tcs_id = 0, cmd_id = 0; 414 + unsigned long flags; 415 + int ret; 416 + 417 + tcs = get_tcs_for_msg(drv, msg); 418 + if (IS_ERR(tcs)) 419 + return PTR_ERR(tcs); 420 + 421 + spin_lock_irqsave(&tcs->lock, flags); 422 + /* find the TCS id and the command in the TCS to write to */ 423 + ret = find_slots(tcs, msg, &tcs_id, &cmd_id); 424 + if (!ret) 425 + __tcs_buffer_write(drv, tcs_id, cmd_id, msg); 426 + spin_unlock_irqrestore(&tcs->lock, flags); 427 + 428 + return ret; 429 + } 430 + 431 + /** 432 + * rpmh_rsc_write_ctrl_data: Write request to the controller 433 + * 434 + * @drv: the controller 435 + * @msg: the data to be written to the controller 436 + * 437 + * There is no response returned for writing the request to the controller. 438 + */ 439 + int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv, const struct tcs_request *msg) 440 + { 441 + if (!msg || !msg->cmds || !msg->num_cmds || 442 + msg->num_cmds > MAX_RPMH_PAYLOAD) { 443 + pr_err("Payload error\n"); 444 + return -EINVAL; 445 + } 446 + 447 + /* Data sent to this API will not be sent immediately */ 448 + if (msg->state == RPMH_ACTIVE_ONLY_STATE) 449 + return -EINVAL; 450 + 451 + return tcs_ctrl_write(drv, msg); 452 + } 453 + 358 454 static int rpmh_probe_tcs_config(struct platform_device *pdev, 359 455 struct rsc_drv *drv) 360 456 { ··· 532 424 tcs->mask = ((1 << tcs->num_tcs) - 1) << st; 533 425 tcs->offset = st; 534 426 st += tcs->num_tcs; 427 + 428 + /* 429 + * Allocate memory to cache sleep and wake requests to 430 + * avoid reading TCS register memory. 431 + */ 432 + if (tcs->type == ACTIVE_TCS) 433 + continue; 434 + 435 + tcs->cmd_cache = devm_kcalloc(&pdev->dev, 436 + tcs->num_tcs * ncpt, sizeof(u32), 437 + GFP_KERNEL); 438 + if (!tcs->cmd_cache) 439 + return -ENOMEM; 535 440 } 536 441 537 442 drv->num_tcs = st;