Lines Matching +full:irq +full:- +full:can +full:- +full:wake
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
30 #include <soc/qcom/cmd-db.h>
32 #include <dt-bindings/soc/qcom,rpmh-rsc.h>
34 #include "rpmh-internal.h"
37 #include "trace-rpmh.h"
98 * - The main rpmh-rsc address is the base of a register space that can
100 * (DRV_PRNT_CHLD_CONFIG). Also found within the rpmh-rsc register
102 * specified in the device tree by "qcom,tcs-offset" and used to
104 * - TCS blocks come one after another. Type, count, and order are
105 * specified by the device tree as "qcom,tcs-config".
106 * - Each TCS block has some registers, then space for up to 16 commands.
112 * +---------------------------------------------------+
117 * | +-----------------------------------------------+ |
120 * | | IRQ | |
123 * | | +------------------------------------------+ | |
127 * | | +------------------------------------------+ | |
128 * | | +------------------------------------------+ | |
132 * | | +------------------------------------------+ | |
133 * | | +------------------------------------------+ | |
137 * | | +------------------------------------------+ | |
139 * | +-----------------------------------------------+ |
140 * | +-----------------------------------------------+ |
143 * | +-----------------------------------------------+ |
145 * +---------------------------------------------------+
197 return drv->tcs_base + drv->regs[RSC_DRV_TCS_OFFSET] * tcs_id + reg; in tcs_reg_addr()
203 return tcs_reg_addr(drv, reg, tcs_id) + drv->regs[RSC_DRV_CMD_OFFSET] * cmd_id; in tcs_cmd_addr()
245 pr_err("%s: error writing %#x to %d:%#x\n", drv->name, in write_tcs_reg_sync()
250 * tcs_invalidate() - Invalidate all TCSes of the given type (sleep or wake).
264 struct tcs_group *tcs = &drv->tcs[type]; in tcs_invalidate()
267 if (bitmap_empty(tcs->slots, MAX_TCS_SLOTS)) in tcs_invalidate()
270 for (m = tcs->offset; m < tcs->offset + tcs->num_tcs; m++) in tcs_invalidate()
271 write_tcs_reg_sync(drv, drv->regs[RSC_DRV_CMD_ENABLE], m, 0); in tcs_invalidate()
273 bitmap_zero(tcs->slots, MAX_TCS_SLOTS); in tcs_invalidate()
277 * rpmh_rsc_invalidate() - Invalidate sleep and wake TCSes.
291 * get_tcs_for_msg() - Get the tcs_group used to send the given message.
306 switch (msg->state) { in get_tcs_for_msg()
317 return ERR_PTR(-EINVAL); in get_tcs_for_msg()
322 * dedicated TCS for active state use, then re-purpose a wake TCS to in get_tcs_for_msg()
323 * send active votes. This is safe because we ensure any active-only in get_tcs_for_msg()
327 tcs = &drv->tcs[type]; in get_tcs_for_msg()
328 if (msg->state == RPMH_ACTIVE_ONLY_STATE && !tcs->num_tcs) in get_tcs_for_msg()
329 tcs = &drv->tcs[WAKE_TCS]; in get_tcs_for_msg()
335 * get_req_from_tcs() - Get a stashed request that was xfering on the given TCS.
357 tcs = &drv->tcs[i]; in get_req_from_tcs()
358 if (tcs->mask & BIT(tcs_id)) in get_req_from_tcs()
359 return tcs->req[tcs_id - tcs->offset]; in get_req_from_tcs()
366 * __tcs_set_trigger() - Start xfer on a TCS or unset trigger on a borrowed TCS
372 * transfer. That will un-trigger/disable the TCS from the last transfer
375 * If we borrowed a wake TCS for an active-only transfer we'll also call
376 * this function with "trigger=false" to just do the un-trigger/disable
377 * before using the TCS for wake purposes again.
379 * Note that the AP is only in charge of triggering active-only transfers.
380 * The AP never triggers sleep/wake values using this function.
385 u32 reg = drv->regs[RSC_DRV_CONTROL]; in __tcs_set_trigger()
408 * enable_tcs_irq() - Enable or disable interrupts on the given TCS.
413 * We only ever call this when we borrow a wake TCS for an active-only
414 * transfer. For active-only TCSes interrupts are always left enabled.
419 u32 reg = drv->regs[RSC_DRV_IRQ_ENABLE]; in enable_tcs_irq()
421 data = readl_relaxed(drv->tcs_base + reg); in enable_tcs_irq()
426 writel_relaxed(data, drv->tcs_base + reg); in enable_tcs_irq()
430 * tcs_tx_done() - TX Done interrupt handler.
431 * @irq: The IRQ number (ignored).
435 * IRQ for) when a transfer is done.
439 static irqreturn_t tcs_tx_done(int irq, void *p) in tcs_tx_done() argument
446 irq_status = readl_relaxed(drv->tcs_base + drv->regs[RSC_DRV_IRQ_STATUS]); in tcs_tx_done()
456 * If wake tcs was re-purposed for sending active in tcs_tx_done()
460 if (!drv->tcs[ACTIVE_TCS].num_tcs) in tcs_tx_done()
464 write_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], i, 0); in tcs_tx_done()
465 writel_relaxed(BIT(i), drv->tcs_base + drv->regs[RSC_DRV_IRQ_CLEAR]); in tcs_tx_done()
466 spin_lock(&drv->lock); in tcs_tx_done()
467 clear_bit(i, drv->tcs_in_use); in tcs_tx_done()
469 * Disable interrupt for WAKE TCS to avoid being in tcs_tx_done()
471 * sends its wake votes. in tcs_tx_done()
473 if (!drv->tcs[ACTIVE_TCS].num_tcs) in tcs_tx_done()
475 spin_unlock(&drv->lock); in tcs_tx_done()
476 wake_up(&drv->tcs_wait); in tcs_tx_done()
485 * __tcs_buffer_write() - Write to TCS hardware from a request; don't trigger.
492 * This is used for all types of transfers (active, sleep, and wake).
504 cmd_msgid |= msg->wait_for_compl ? CMD_MSGID_RESP_REQ : 0; in __tcs_buffer_write()
506 for (i = 0, j = cmd_id; i < msg->num_cmds; i++, j++) { in __tcs_buffer_write()
507 cmd = &msg->cmds[i]; in __tcs_buffer_write()
511 * Additionally, if the cmd->wait is set, make the command in __tcs_buffer_write()
512 * response reqd even if the overall request was fire-n-forget. in __tcs_buffer_write()
514 msgid |= cmd->wait ? CMD_MSGID_RESP_REQ : 0; in __tcs_buffer_write()
516 write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_MSGID], tcs_id, j, msgid); in __tcs_buffer_write()
517 write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_ADDR], tcs_id, j, cmd->addr); in __tcs_buffer_write()
518 write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_DATA], tcs_id, j, cmd->data); in __tcs_buffer_write()
519 trace_rpmh_send_msg(drv, tcs_id, msg->state, j, msgid, cmd); in __tcs_buffer_write()
522 cmd_enable |= read_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id); in __tcs_buffer_write()
523 write_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id, cmd_enable); in __tcs_buffer_write()
527 * check_for_req_inflight() - Look to see if conflicting cmds are in flight.
535 * one it'll return -EBUSY.
537 * Only for use for active-only transfers.
539 * Must be called with the drv->lock held since that protects tcs_in_use.
541 * Return: 0 if nothing in flight or -EBUSY if we should try again later.
542 * The caller must re-enable interrupts between tries since that's
552 int i = tcs->offset; in check_for_req_inflight()
554 for_each_set_bit_from(i, drv->tcs_in_use, tcs->offset + tcs->num_tcs) { in check_for_req_inflight()
555 curr_enabled = read_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], i); in check_for_req_inflight()
558 addr = read_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_ADDR], i, j); in check_for_req_inflight()
559 for (k = 0; k < msg->num_cmds; k++) { in check_for_req_inflight()
560 if (addr == msg->cmds[k].addr) in check_for_req_inflight()
561 return -EBUSY; in check_for_req_inflight()
570 * find_free_tcs() - Find free tcs in the given tcs_group; only for active.
571 * @tcs: A pointer to the active-only tcs_group (or the wake tcs_group if
572 * we borrowed it because there are zero active-only ones).
574 * Must be called with the drv->lock held since that protects tcs_in_use.
576 * Return: The first tcs that's free or -EBUSY if all in use.
580 const struct rsc_drv *drv = tcs->drv; in find_free_tcs()
582 unsigned long max = tcs->offset + tcs->num_tcs; in find_free_tcs()
584 i = find_next_zero_bit(drv->tcs_in_use, max, tcs->offset); in find_free_tcs()
586 return -EBUSY; in find_free_tcs()
592 * claim_tcs_for_req() - Claim a tcs in the given tcs_group; only for active.
600 * Context: Must be called with the drv->lock held since that protects
603 * Return: The id of the claimed tcs or -EBUSY if a matching msg is in flight
613 * when one is already in-flight or being processed. in claim_tcs_for_req()
623 * rpmh_rsc_send_data() - Write / trigger active-only message.
628 * - This is only used for "ACTIVE_ONLY" since the limitations of this
629 * function don't make sense for sleep/wake cases.
630 * - To do the transfer, we will grab a whole TCS for ourselves--we don't
633 * - This function will not wait for the commands to be finished, only for
636 * - This function must be called with interrupts enabled. If the hardware
638 * finish so that we can have the hardware, and to fully finish it needs
640 * active CPU this can never happen if interrupts are disabled.
642 * Return: 0 on success, -EINVAL on error.
654 spin_lock_irqsave(&drv->lock, flags); in rpmh_rsc_send_data()
657 wait_event_lock_irq(drv->tcs_wait, in rpmh_rsc_send_data()
659 drv->lock); in rpmh_rsc_send_data()
661 tcs->req[tcs_id - tcs->offset] = msg; in rpmh_rsc_send_data()
662 set_bit(tcs_id, drv->tcs_in_use); in rpmh_rsc_send_data()
663 if (msg->state == RPMH_ACTIVE_ONLY_STATE && tcs->type != ACTIVE_TCS) { in rpmh_rsc_send_data()
665 * Clear previously programmed WAKE commands in selected in rpmh_rsc_send_data()
666 * repurposed TCS to avoid triggering them. tcs->slots will be in rpmh_rsc_send_data()
669 write_tcs_reg_sync(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id, 0); in rpmh_rsc_send_data()
672 spin_unlock_irqrestore(&drv->lock, flags); in rpmh_rsc_send_data()
675 * These two can be done after the lock is released because: in rpmh_rsc_send_data()
676 * - We marked "tcs_in_use" under lock. in rpmh_rsc_send_data()
677 * - Once "tcs_in_use" has been marked nobody else could be writing in rpmh_rsc_send_data()
679 * - The interrupt can't go off until we trigger w/ the last line in rpmh_rsc_send_data()
689 * find_slots() - Find a place to write the given message.
698 * Only for use on sleep/wake TCSes since those are the only ones we maintain
699 * tcs->slots for.
701 * Return: -ENOMEM if there was no room, else 0.
709 /* Do over, until we can fit the full payload in a single TCS */ in find_slots()
711 slot = bitmap_find_next_zero_area(tcs->slots, MAX_TCS_SLOTS, in find_slots()
712 i, msg->num_cmds, 0); in find_slots()
713 if (slot >= tcs->num_tcs * tcs->ncpt) in find_slots()
714 return -ENOMEM; in find_slots()
715 i += tcs->ncpt; in find_slots()
716 } while (slot + msg->num_cmds - 1 >= i); in find_slots()
718 bitmap_set(tcs->slots, slot, msg->num_cmds); in find_slots()
720 offset = slot / tcs->ncpt; in find_slots()
721 *tcs_id = offset + tcs->offset; in find_slots()
722 *cmd_id = slot % tcs->ncpt; in find_slots()
728 * rpmh_rsc_write_ctrl_data() - Write request to controller but don't trigger.
732 * This should only be called for sleep/wake state, never active-only
738 * Return: 0 if no error; else -error.
759 * rpmh_rsc_ctrlr_is_busy() - Check if any of the AMCs are busy.
764 * SLEEP and WAKE sets. If AMCs are busy, controller can not enter
767 * Context: Must be called with the drv->lock held.
770 * * False - AMCs are idle
771 * * True - AMCs are busy
776 const struct tcs_group *tcs = &drv->tcs[ACTIVE_TCS]; in rpmh_rsc_ctrlr_is_busy()
781 * dedicated TCS for active state use, then re-purposed wake TCSes in rpmh_rsc_ctrlr_is_busy()
782 * should be checked for not busy, because we used wake TCSes for in rpmh_rsc_ctrlr_is_busy()
785 if (!tcs->num_tcs) in rpmh_rsc_ctrlr_is_busy()
786 tcs = &drv->tcs[WAKE_TCS]; in rpmh_rsc_ctrlr_is_busy()
788 max = tcs->offset + tcs->num_tcs; in rpmh_rsc_ctrlr_is_busy()
789 set = find_next_bit(drv->tcs_in_use, max, tcs->offset); in rpmh_rsc_ctrlr_is_busy()
795 * rpmh_rsc_write_next_wakeup() - Write next wakeup in CONTROL_TCS.
807 if (!drv->tcs[CONTROL_TCS].num_tcs || !drv->genpd_nb.notifier_call) in rpmh_rsc_write_next_wakeup()
815 wakeup = dev_pm_genpd_get_next_hrtimer(drv->dev); in rpmh_rsc_write_next_wakeup()
832 writel_relaxed(lo, drv->base + RSC_DRV_CTL_TCS_DATA_LO); in rpmh_rsc_write_next_wakeup()
833 writel_relaxed(hi, drv->base + RSC_DRV_CTL_TCS_DATA_HI); in rpmh_rsc_write_next_wakeup()
837 * rpmh_rsc_cpu_pm_callback() - Check if any of the AMCs are busy.
842 * This function is given to cpu_pm_register_notifier so we can be informed
844 * transfers will be started so we write sleep/wake sets. This function gets
848 * and wake messages to TCSes. The firmware then takes care of triggering
862 cpus_in_pm = atomic_inc_return(&drv->cpus_in_pm); in rpmh_rsc_cpu_pm_callback()
869 * AND that CPU was not idle AND that CPU was the last non-idle in rpmh_rsc_cpu_pm_callback()
870 * CPU. That can't happen. CPUs would have to come out of idle in rpmh_rsc_cpu_pm_callback()
878 atomic_dec(&drv->cpus_in_pm); in rpmh_rsc_cpu_pm_callback()
885 * It's likely we're on the last CPU. Grab the drv->lock and write in rpmh_rsc_cpu_pm_callback()
886 * out the sleep/wake commands to RPMH hardware. Grabbing the lock in rpmh_rsc_cpu_pm_callback()
896 if (spin_trylock(&drv->lock)) { in rpmh_rsc_cpu_pm_callback()
897 if (rpmh_rsc_ctrlr_is_busy(drv) || rpmh_flush(&drv->client)) in rpmh_rsc_cpu_pm_callback()
899 spin_unlock(&drv->lock); in rpmh_rsc_cpu_pm_callback()
906 /* Double-check if we're here because someone else is up */ in rpmh_rsc_cpu_pm_callback()
911 atomic_dec(&drv->cpus_in_pm); in rpmh_rsc_cpu_pm_callback()
918 * rpmh_rsc_pd_callback() - Check if any of the AMCs are busy.
923 * This function is given to dev_pm_genpd_add_notifier() so we can be informed
924 * about when cluster-pd is going down. When cluster go down we know no more active
925 * transfers will be started so we write sleep/wake sets. This function gets
928 * If AMCs are not busy then writes cached sleep and wake messages to TCSes.
932 * * NOTIFY_OK - success
933 * * NOTIFY_BAD - failure
942 (rpmh_rsc_ctrlr_is_busy(drv) || rpmh_flush(&drv->client))) in rpmh_rsc_pd_callback()
953 drv->genpd_nb.notifier_call = rpmh_rsc_pd_callback; in rpmh_rsc_pd_attach()
954 ret = dev_pm_genpd_add_notifier(dev, &drv->genpd_nb); in rpmh_rsc_pd_attach()
967 struct device_node *dn = pdev->dev.of_node; in rpmh_probe_tcs_config()
972 ret = of_property_read_u32(dn, "qcom,tcs-offset", &offset); in rpmh_probe_tcs_config()
975 drv->tcs_base = drv->base + offset; in rpmh_probe_tcs_config()
977 config = readl_relaxed(drv->base + drv->regs[DRV_PRNT_CHLD_CONFIG]); in rpmh_probe_tcs_config()
980 max_tcs &= DRV_NUM_TCS_MASK << (DRV_NUM_TCS_SHIFT * drv->id); in rpmh_probe_tcs_config()
981 max_tcs = max_tcs >> (DRV_NUM_TCS_SHIFT * drv->id); in rpmh_probe_tcs_config()
986 n = of_property_count_u32_elems(dn, "qcom,tcs-config"); in rpmh_probe_tcs_config()
988 return -EINVAL; in rpmh_probe_tcs_config()
991 ret = of_property_read_u32_index(dn, "qcom,tcs-config", in rpmh_probe_tcs_config()
996 return -EINVAL; in rpmh_probe_tcs_config()
998 ret = of_property_read_u32_index(dn, "qcom,tcs-config", in rpmh_probe_tcs_config()
1003 return -EINVAL; in rpmh_probe_tcs_config()
1007 tcs = &drv->tcs[tcs_cfg[i].type]; in rpmh_probe_tcs_config()
1008 if (tcs->drv) in rpmh_probe_tcs_config()
1009 return -EINVAL; in rpmh_probe_tcs_config()
1010 tcs->drv = drv; in rpmh_probe_tcs_config()
1011 tcs->type = tcs_cfg[i].type; in rpmh_probe_tcs_config()
1012 tcs->num_tcs = tcs_cfg[i].n; in rpmh_probe_tcs_config()
1013 tcs->ncpt = ncpt; in rpmh_probe_tcs_config()
1015 if (!tcs->num_tcs || tcs->type == CONTROL_TCS) in rpmh_probe_tcs_config()
1018 if (st + tcs->num_tcs > max_tcs || in rpmh_probe_tcs_config()
1019 st + tcs->num_tcs >= BITS_PER_BYTE * sizeof(tcs->mask)) in rpmh_probe_tcs_config()
1020 return -EINVAL; in rpmh_probe_tcs_config()
1022 tcs->mask = ((1 << tcs->num_tcs) - 1) << st; in rpmh_probe_tcs_config()
1023 tcs->offset = st; in rpmh_probe_tcs_config()
1024 st += tcs->num_tcs; in rpmh_probe_tcs_config()
1027 drv->num_tcs = st; in rpmh_probe_tcs_config()
1034 struct device_node *dn = pdev->dev.of_node; in rpmh_rsc_probe()
1037 int ret, irq; in rpmh_rsc_probe() local
1042 * Even though RPMh doesn't directly use cmd-db, all of its children in rpmh_rsc_probe()
1047 if (ret != -EPROBE_DEFER) in rpmh_rsc_probe()
1048 dev_err(&pdev->dev, "Command DB not available (%d)\n", in rpmh_rsc_probe()
1053 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); in rpmh_rsc_probe()
1055 return -ENOMEM; in rpmh_rsc_probe()
1057 ret = of_property_read_u32(dn, "qcom,drv-id", &drv->id); in rpmh_rsc_probe()
1061 drv->name = of_get_property(dn, "label", NULL); in rpmh_rsc_probe()
1062 if (!drv->name) in rpmh_rsc_probe()
1063 drv->name = dev_name(&pdev->dev); in rpmh_rsc_probe()
1065 snprintf(drv_id, ARRAY_SIZE(drv_id), "drv-%d", drv->id); in rpmh_rsc_probe()
1066 drv->base = devm_platform_ioremap_resource_byname(pdev, drv_id); in rpmh_rsc_probe()
1067 if (IS_ERR(drv->base)) in rpmh_rsc_probe()
1068 return PTR_ERR(drv->base); in rpmh_rsc_probe()
1070 rsc_id = readl_relaxed(drv->base + RSC_DRV_ID); in rpmh_rsc_probe()
1071 drv->ver.major = rsc_id & (MAJOR_VER_MASK << MAJOR_VER_SHIFT); in rpmh_rsc_probe()
1072 drv->ver.major >>= MAJOR_VER_SHIFT; in rpmh_rsc_probe()
1073 drv->ver.minor = rsc_id & (MINOR_VER_MASK << MINOR_VER_SHIFT); in rpmh_rsc_probe()
1074 drv->ver.minor >>= MINOR_VER_SHIFT; in rpmh_rsc_probe()
1076 if (drv->ver.major == 3) in rpmh_rsc_probe()
1077 drv->regs = rpmh_rsc_reg_offset_ver_3_0; in rpmh_rsc_probe()
1079 drv->regs = rpmh_rsc_reg_offset_ver_2_7; in rpmh_rsc_probe()
1085 spin_lock_init(&drv->lock); in rpmh_rsc_probe()
1086 init_waitqueue_head(&drv->tcs_wait); in rpmh_rsc_probe()
1087 bitmap_zero(drv->tcs_in_use, MAX_TCS_NR); in rpmh_rsc_probe()
1089 irq = platform_get_irq(pdev, drv->id); in rpmh_rsc_probe()
1090 if (irq < 0) in rpmh_rsc_probe()
1091 return irq; in rpmh_rsc_probe()
1093 ret = devm_request_irq(&pdev->dev, irq, tcs_tx_done, in rpmh_rsc_probe()
1095 drv->name, drv); in rpmh_rsc_probe()
1101 * 'HW solver' mode where they can be in autonomous mode executing low in rpmh_rsc_probe()
1104 solver_config = readl_relaxed(drv->base + drv->regs[DRV_SOLVER_CONFIG]); in rpmh_rsc_probe()
1108 if (pdev->dev.pm_domain) { in rpmh_rsc_probe()
1109 ret = rpmh_rsc_pd_attach(drv, &pdev->dev); in rpmh_rsc_probe()
1113 drv->rsc_pm.notifier_call = rpmh_rsc_cpu_pm_callback; in rpmh_rsc_probe()
1114 cpu_pm_register_notifier(&drv->rsc_pm); in rpmh_rsc_probe()
1119 writel_relaxed(drv->tcs[ACTIVE_TCS].mask, in rpmh_rsc_probe()
1120 drv->tcs_base + drv->regs[RSC_DRV_IRQ_ENABLE]); in rpmh_rsc_probe()
1122 spin_lock_init(&drv->client.cache_lock); in rpmh_rsc_probe()
1123 INIT_LIST_HEAD(&drv->client.cache); in rpmh_rsc_probe()
1124 INIT_LIST_HEAD(&drv->client.batch_cache); in rpmh_rsc_probe()
1126 dev_set_drvdata(&pdev->dev, drv); in rpmh_rsc_probe()
1127 drv->dev = &pdev->dev; in rpmh_rsc_probe()
1129 ret = devm_of_platform_populate(&pdev->dev); in rpmh_rsc_probe()
1130 if (ret && pdev->dev.pm_domain) { in rpmh_rsc_probe()
1131 dev_pm_genpd_remove_notifier(&pdev->dev); in rpmh_rsc_probe()
1132 pm_runtime_disable(&pdev->dev); in rpmh_rsc_probe()
1139 { .compatible = "qcom,rpmh-rsc", },