162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#define pr_fmt(fmt) "%s " fmt, KBUILD_MODNAME
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/atomic.h>
962306a36Sopenharmony_ci#include <linux/cpu_pm.h>
1062306a36Sopenharmony_ci#include <linux/delay.h>
1162306a36Sopenharmony_ci#include <linux/interrupt.h>
1262306a36Sopenharmony_ci#include <linux/io.h>
1362306a36Sopenharmony_ci#include <linux/iopoll.h>
1462306a36Sopenharmony_ci#include <linux/kernel.h>
1562306a36Sopenharmony_ci#include <linux/ktime.h>
1662306a36Sopenharmony_ci#include <linux/list.h>
1762306a36Sopenharmony_ci#include <linux/module.h>
1862306a36Sopenharmony_ci#include <linux/notifier.h>
1962306a36Sopenharmony_ci#include <linux/of.h>
2062306a36Sopenharmony_ci#include <linux/of_irq.h>
2162306a36Sopenharmony_ci#include <linux/of_platform.h>
2262306a36Sopenharmony_ci#include <linux/platform_device.h>
2362306a36Sopenharmony_ci#include <linux/pm_domain.h>
2462306a36Sopenharmony_ci#include <linux/pm_runtime.h>
2562306a36Sopenharmony_ci#include <linux/slab.h>
2662306a36Sopenharmony_ci#include <linux/spinlock.h>
2762306a36Sopenharmony_ci#include <linux/wait.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <clocksource/arm_arch_timer.h>
3062306a36Sopenharmony_ci#include <soc/qcom/cmd-db.h>
3162306a36Sopenharmony_ci#include <soc/qcom/tcs.h>
3262306a36Sopenharmony_ci#include <dt-bindings/soc/qcom,rpmh-rsc.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include "rpmh-internal.h"
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define CREATE_TRACE_POINTS
3762306a36Sopenharmony_ci#include "trace-rpmh.h"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define RSC_DRV_ID			0
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define MAJOR_VER_MASK			0xFF
4362306a36Sopenharmony_ci#define MAJOR_VER_SHIFT			16
4462306a36Sopenharmony_ci#define MINOR_VER_MASK			0xFF
4562306a36Sopenharmony_ci#define MINOR_VER_SHIFT			8
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cienum {
4862306a36Sopenharmony_ci	RSC_DRV_TCS_OFFSET,
4962306a36Sopenharmony_ci	RSC_DRV_CMD_OFFSET,
5062306a36Sopenharmony_ci	DRV_SOLVER_CONFIG,
5162306a36Sopenharmony_ci	DRV_PRNT_CHLD_CONFIG,
5262306a36Sopenharmony_ci	RSC_DRV_IRQ_ENABLE,
5362306a36Sopenharmony_ci	RSC_DRV_IRQ_STATUS,
5462306a36Sopenharmony_ci	RSC_DRV_IRQ_CLEAR,
5562306a36Sopenharmony_ci	RSC_DRV_CMD_WAIT_FOR_CMPL,
5662306a36Sopenharmony_ci	RSC_DRV_CONTROL,
5762306a36Sopenharmony_ci	RSC_DRV_STATUS,
5862306a36Sopenharmony_ci	RSC_DRV_CMD_ENABLE,
5962306a36Sopenharmony_ci	RSC_DRV_CMD_MSGID,
6062306a36Sopenharmony_ci	RSC_DRV_CMD_ADDR,
6162306a36Sopenharmony_ci	RSC_DRV_CMD_DATA,
6262306a36Sopenharmony_ci	RSC_DRV_CMD_STATUS,
6362306a36Sopenharmony_ci	RSC_DRV_CMD_RESP_DATA,
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/* DRV HW Solver Configuration Information Register */
6762306a36Sopenharmony_ci#define DRV_HW_SOLVER_MASK		1
6862306a36Sopenharmony_ci#define DRV_HW_SOLVER_SHIFT		24
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/* DRV TCS Configuration Information Register */
7162306a36Sopenharmony_ci#define DRV_NUM_TCS_MASK		0x3F
7262306a36Sopenharmony_ci#define DRV_NUM_TCS_SHIFT		6
7362306a36Sopenharmony_ci#define DRV_NCPT_MASK			0x1F
7462306a36Sopenharmony_ci#define DRV_NCPT_SHIFT			27
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/* Offsets for CONTROL TCS Registers */
7762306a36Sopenharmony_ci#define RSC_DRV_CTL_TCS_DATA_HI		0x38
7862306a36Sopenharmony_ci#define RSC_DRV_CTL_TCS_DATA_HI_MASK	0xFFFFFF
7962306a36Sopenharmony_ci#define RSC_DRV_CTL_TCS_DATA_HI_VALID	BIT(31)
8062306a36Sopenharmony_ci#define RSC_DRV_CTL_TCS_DATA_LO		0x40
8162306a36Sopenharmony_ci#define RSC_DRV_CTL_TCS_DATA_LO_MASK	0xFFFFFFFF
8262306a36Sopenharmony_ci#define RSC_DRV_CTL_TCS_DATA_SIZE	32
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#define TCS_AMC_MODE_ENABLE		BIT(16)
8562306a36Sopenharmony_ci#define TCS_AMC_MODE_TRIGGER		BIT(24)
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/* TCS CMD register bit mask */
8862306a36Sopenharmony_ci#define CMD_MSGID_LEN			8
8962306a36Sopenharmony_ci#define CMD_MSGID_RESP_REQ		BIT(8)
9062306a36Sopenharmony_ci#define CMD_MSGID_WRITE			BIT(16)
9162306a36Sopenharmony_ci#define CMD_STATUS_ISSUED		BIT(8)
9262306a36Sopenharmony_ci#define CMD_STATUS_COMPL		BIT(16)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * Here's a high level overview of how all the registers in RPMH work
9662306a36Sopenharmony_ci * together:
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * - The main rpmh-rsc address is the base of a register space that can
9962306a36Sopenharmony_ci *   be used to find overall configuration of the hardware
10062306a36Sopenharmony_ci *   (DRV_PRNT_CHLD_CONFIG). Also found within the rpmh-rsc register
10162306a36Sopenharmony_ci *   space are all the TCS blocks. The offset of the TCS blocks is
10262306a36Sopenharmony_ci *   specified in the device tree by "qcom,tcs-offset" and used to
10362306a36Sopenharmony_ci *   compute tcs_base.
10462306a36Sopenharmony_ci * - TCS blocks come one after another. Type, count, and order are
10562306a36Sopenharmony_ci *   specified by the device tree as "qcom,tcs-config".
10662306a36Sopenharmony_ci * - Each TCS block has some registers, then space for up to 16 commands.
10762306a36Sopenharmony_ci *   Note that though address space is reserved for 16 commands, fewer
10862306a36Sopenharmony_ci *   might be present. See ncpt (num cmds per TCS).
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * Here's a picture:
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci *  +---------------------------------------------------+
11362306a36Sopenharmony_ci *  |RSC                                                |
11462306a36Sopenharmony_ci *  | ctrl                                              |
11562306a36Sopenharmony_ci *  |                                                   |
11662306a36Sopenharmony_ci *  | Drvs:                                             |
11762306a36Sopenharmony_ci *  | +-----------------------------------------------+ |
11862306a36Sopenharmony_ci *  | |DRV0                                           | |
11962306a36Sopenharmony_ci *  | | ctrl/config                                   | |
12062306a36Sopenharmony_ci *  | | IRQ                                           | |
12162306a36Sopenharmony_ci *  | |                                               | |
12262306a36Sopenharmony_ci *  | | TCSes:                                        | |
12362306a36Sopenharmony_ci *  | | +------------------------------------------+  | |
12462306a36Sopenharmony_ci *  | | |TCS0  |  |  |  |  |  |  |  |  |  |  |  |  |  | |
12562306a36Sopenharmony_ci *  | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15|  | |
12662306a36Sopenharmony_ci *  | | |      |  |  |  |  |  |  |  |  |  |  |  |  |  | |
12762306a36Sopenharmony_ci *  | | +------------------------------------------+  | |
12862306a36Sopenharmony_ci *  | | +------------------------------------------+  | |
12962306a36Sopenharmony_ci *  | | |TCS1  |  |  |  |  |  |  |  |  |  |  |  |  |  | |
13062306a36Sopenharmony_ci *  | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15|  | |
13162306a36Sopenharmony_ci *  | | |      |  |  |  |  |  |  |  |  |  |  |  |  |  | |
13262306a36Sopenharmony_ci *  | | +------------------------------------------+  | |
13362306a36Sopenharmony_ci *  | | +------------------------------------------+  | |
13462306a36Sopenharmony_ci *  | | |TCS2  |  |  |  |  |  |  |  |  |  |  |  |  |  | |
13562306a36Sopenharmony_ci *  | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15|  | |
13662306a36Sopenharmony_ci *  | | |      |  |  |  |  |  |  |  |  |  |  |  |  |  | |
13762306a36Sopenharmony_ci *  | | +------------------------------------------+  | |
13862306a36Sopenharmony_ci *  | |                    ......                     | |
13962306a36Sopenharmony_ci *  | +-----------------------------------------------+ |
14062306a36Sopenharmony_ci *  | +-----------------------------------------------+ |
14162306a36Sopenharmony_ci *  | |DRV1                                           | |
14262306a36Sopenharmony_ci *  | | (same as DRV0)                                | |
14362306a36Sopenharmony_ci *  | +-----------------------------------------------+ |
14462306a36Sopenharmony_ci *  |                      ......                       |
14562306a36Sopenharmony_ci *  +---------------------------------------------------+
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define USECS_TO_CYCLES(time_usecs)			\
14962306a36Sopenharmony_ci	xloops_to_cycles((time_usecs) * 0x10C7UL)
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic inline unsigned long xloops_to_cycles(u64 xloops)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	return (xloops * loops_per_jiffy * HZ) >> 32;
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_cistatic u32 rpmh_rsc_reg_offset_ver_2_7[] = {
15762306a36Sopenharmony_ci	[RSC_DRV_TCS_OFFSET]		= 672,
15862306a36Sopenharmony_ci	[RSC_DRV_CMD_OFFSET]		= 20,
15962306a36Sopenharmony_ci	[DRV_SOLVER_CONFIG]		= 0x04,
16062306a36Sopenharmony_ci	[DRV_PRNT_CHLD_CONFIG]		= 0x0C,
16162306a36Sopenharmony_ci	[RSC_DRV_IRQ_ENABLE]		= 0x00,
16262306a36Sopenharmony_ci	[RSC_DRV_IRQ_STATUS]		= 0x04,
16362306a36Sopenharmony_ci	[RSC_DRV_IRQ_CLEAR]		= 0x08,
16462306a36Sopenharmony_ci	[RSC_DRV_CMD_WAIT_FOR_CMPL]	= 0x10,
16562306a36Sopenharmony_ci	[RSC_DRV_CONTROL]		= 0x14,
16662306a36Sopenharmony_ci	[RSC_DRV_STATUS]		= 0x18,
16762306a36Sopenharmony_ci	[RSC_DRV_CMD_ENABLE]		= 0x1C,
16862306a36Sopenharmony_ci	[RSC_DRV_CMD_MSGID]		= 0x30,
16962306a36Sopenharmony_ci	[RSC_DRV_CMD_ADDR]		= 0x34,
17062306a36Sopenharmony_ci	[RSC_DRV_CMD_DATA]		= 0x38,
17162306a36Sopenharmony_ci	[RSC_DRV_CMD_STATUS]		= 0x3C,
17262306a36Sopenharmony_ci	[RSC_DRV_CMD_RESP_DATA]		= 0x40,
17362306a36Sopenharmony_ci};
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic u32 rpmh_rsc_reg_offset_ver_3_0[] = {
17662306a36Sopenharmony_ci	[RSC_DRV_TCS_OFFSET]		= 672,
17762306a36Sopenharmony_ci	[RSC_DRV_CMD_OFFSET]		= 24,
17862306a36Sopenharmony_ci	[DRV_SOLVER_CONFIG]		= 0x04,
17962306a36Sopenharmony_ci	[DRV_PRNT_CHLD_CONFIG]		= 0x0C,
18062306a36Sopenharmony_ci	[RSC_DRV_IRQ_ENABLE]		= 0x00,
18162306a36Sopenharmony_ci	[RSC_DRV_IRQ_STATUS]		= 0x04,
18262306a36Sopenharmony_ci	[RSC_DRV_IRQ_CLEAR]		= 0x08,
18362306a36Sopenharmony_ci	[RSC_DRV_CMD_WAIT_FOR_CMPL]	= 0x20,
18462306a36Sopenharmony_ci	[RSC_DRV_CONTROL]		= 0x24,
18562306a36Sopenharmony_ci	[RSC_DRV_STATUS]		= 0x28,
18662306a36Sopenharmony_ci	[RSC_DRV_CMD_ENABLE]		= 0x2C,
18762306a36Sopenharmony_ci	[RSC_DRV_CMD_MSGID]		= 0x34,
18862306a36Sopenharmony_ci	[RSC_DRV_CMD_ADDR]		= 0x38,
18962306a36Sopenharmony_ci	[RSC_DRV_CMD_DATA]		= 0x3C,
19062306a36Sopenharmony_ci	[RSC_DRV_CMD_STATUS]		= 0x40,
19162306a36Sopenharmony_ci	[RSC_DRV_CMD_RESP_DATA]		= 0x44,
19262306a36Sopenharmony_ci};
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cistatic inline void __iomem *
19562306a36Sopenharmony_citcs_reg_addr(const struct rsc_drv *drv, int reg, int tcs_id)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	return drv->tcs_base + drv->regs[RSC_DRV_TCS_OFFSET] * tcs_id + reg;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic inline void __iomem *
20162306a36Sopenharmony_citcs_cmd_addr(const struct rsc_drv *drv, int reg, int tcs_id, int cmd_id)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	return tcs_reg_addr(drv, reg, tcs_id) + drv->regs[RSC_DRV_CMD_OFFSET] * cmd_id;
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic u32 read_tcs_cmd(const struct rsc_drv *drv, int reg, int tcs_id,
20762306a36Sopenharmony_ci			int cmd_id)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	return readl_relaxed(tcs_cmd_addr(drv, reg, tcs_id, cmd_id));
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic u32 read_tcs_reg(const struct rsc_drv *drv, int reg, int tcs_id)
21362306a36Sopenharmony_ci{
21462306a36Sopenharmony_ci	return readl_relaxed(tcs_reg_addr(drv, reg, tcs_id));
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic void write_tcs_cmd(const struct rsc_drv *drv, int reg, int tcs_id,
21862306a36Sopenharmony_ci			  int cmd_id, u32 data)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	writel_relaxed(data, tcs_cmd_addr(drv, reg, tcs_id, cmd_id));
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistatic void write_tcs_reg(const struct rsc_drv *drv, int reg, int tcs_id,
22462306a36Sopenharmony_ci			  u32 data)
22562306a36Sopenharmony_ci{
22662306a36Sopenharmony_ci	writel_relaxed(data, tcs_reg_addr(drv, reg, tcs_id));
22762306a36Sopenharmony_ci}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_cistatic void write_tcs_reg_sync(const struct rsc_drv *drv, int reg, int tcs_id,
23062306a36Sopenharmony_ci			       u32 data)
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci	int i;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	writel(data, tcs_reg_addr(drv, reg, tcs_id));
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	/*
23762306a36Sopenharmony_ci	 * Wait until we read back the same value.  Use a counter rather than
23862306a36Sopenharmony_ci	 * ktime for timeout since this may be called after timekeeping stops.
23962306a36Sopenharmony_ci	 */
24062306a36Sopenharmony_ci	for (i = 0; i < USEC_PER_SEC; i++) {
24162306a36Sopenharmony_ci		if (readl(tcs_reg_addr(drv, reg, tcs_id)) == data)
24262306a36Sopenharmony_ci			return;
24362306a36Sopenharmony_ci		udelay(1);
24462306a36Sopenharmony_ci	}
24562306a36Sopenharmony_ci	pr_err("%s: error writing %#x to %d:%#x\n", drv->name,
24662306a36Sopenharmony_ci	       data, tcs_id, reg);
24762306a36Sopenharmony_ci}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/**
25062306a36Sopenharmony_ci * tcs_invalidate() - Invalidate all TCSes of the given type (sleep or wake).
25162306a36Sopenharmony_ci * @drv:  The RSC controller.
25262306a36Sopenharmony_ci * @type: SLEEP_TCS or WAKE_TCS
25362306a36Sopenharmony_ci *
25462306a36Sopenharmony_ci * This will clear the "slots" variable of the given tcs_group and also
25562306a36Sopenharmony_ci * tell the hardware to forget about all entries.
25662306a36Sopenharmony_ci *
25762306a36Sopenharmony_ci * The caller must ensure that no other RPMH actions are happening when this
25862306a36Sopenharmony_ci * function is called, since otherwise the device may immediately become
25962306a36Sopenharmony_ci * used again even before this function exits.
26062306a36Sopenharmony_ci */
26162306a36Sopenharmony_cistatic void tcs_invalidate(struct rsc_drv *drv, int type)
26262306a36Sopenharmony_ci{
26362306a36Sopenharmony_ci	int m;
26462306a36Sopenharmony_ci	struct tcs_group *tcs = &drv->tcs[type];
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	/* Caller ensures nobody else is running so no lock */
26762306a36Sopenharmony_ci	if (bitmap_empty(tcs->slots, MAX_TCS_SLOTS))
26862306a36Sopenharmony_ci		return;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	for (m = tcs->offset; m < tcs->offset + tcs->num_tcs; m++)
27162306a36Sopenharmony_ci		write_tcs_reg_sync(drv, drv->regs[RSC_DRV_CMD_ENABLE], m, 0);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	bitmap_zero(tcs->slots, MAX_TCS_SLOTS);
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci/**
27762306a36Sopenharmony_ci * rpmh_rsc_invalidate() - Invalidate sleep and wake TCSes.
27862306a36Sopenharmony_ci * @drv: The RSC controller.
27962306a36Sopenharmony_ci *
28062306a36Sopenharmony_ci * The caller must ensure that no other RPMH actions are happening when this
28162306a36Sopenharmony_ci * function is called, since otherwise the device may immediately become
28262306a36Sopenharmony_ci * used again even before this function exits.
28362306a36Sopenharmony_ci */
28462306a36Sopenharmony_civoid rpmh_rsc_invalidate(struct rsc_drv *drv)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	tcs_invalidate(drv, SLEEP_TCS);
28762306a36Sopenharmony_ci	tcs_invalidate(drv, WAKE_TCS);
28862306a36Sopenharmony_ci}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/**
29162306a36Sopenharmony_ci * get_tcs_for_msg() - Get the tcs_group used to send the given message.
29262306a36Sopenharmony_ci * @drv: The RSC controller.
29362306a36Sopenharmony_ci * @msg: The message we want to send.
29462306a36Sopenharmony_ci *
29562306a36Sopenharmony_ci * This is normally pretty straightforward except if we are trying to send
29662306a36Sopenharmony_ci * an ACTIVE_ONLY message but don't have any active_only TCSes.
29762306a36Sopenharmony_ci *
29862306a36Sopenharmony_ci * Return: A pointer to a tcs_group or an ERR_PTR.
29962306a36Sopenharmony_ci */
30062306a36Sopenharmony_cistatic struct tcs_group *get_tcs_for_msg(struct rsc_drv *drv,
30162306a36Sopenharmony_ci					 const struct tcs_request *msg)
30262306a36Sopenharmony_ci{
30362306a36Sopenharmony_ci	int type;
30462306a36Sopenharmony_ci	struct tcs_group *tcs;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	switch (msg->state) {
30762306a36Sopenharmony_ci	case RPMH_ACTIVE_ONLY_STATE:
30862306a36Sopenharmony_ci		type = ACTIVE_TCS;
30962306a36Sopenharmony_ci		break;
31062306a36Sopenharmony_ci	case RPMH_WAKE_ONLY_STATE:
31162306a36Sopenharmony_ci		type = WAKE_TCS;
31262306a36Sopenharmony_ci		break;
31362306a36Sopenharmony_ci	case RPMH_SLEEP_STATE:
31462306a36Sopenharmony_ci		type = SLEEP_TCS;
31562306a36Sopenharmony_ci		break;
31662306a36Sopenharmony_ci	default:
31762306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
31862306a36Sopenharmony_ci	}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	/*
32162306a36Sopenharmony_ci	 * If we are making an active request on a RSC that does not have a
32262306a36Sopenharmony_ci	 * dedicated TCS for active state use, then re-purpose a wake TCS to
32362306a36Sopenharmony_ci	 * send active votes. This is safe because we ensure any active-only
32462306a36Sopenharmony_ci	 * transfers have finished before we use it (maybe by running from
32562306a36Sopenharmony_ci	 * the last CPU in PM code).
32662306a36Sopenharmony_ci	 */
32762306a36Sopenharmony_ci	tcs = &drv->tcs[type];
32862306a36Sopenharmony_ci	if (msg->state == RPMH_ACTIVE_ONLY_STATE && !tcs->num_tcs)
32962306a36Sopenharmony_ci		tcs = &drv->tcs[WAKE_TCS];
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	return tcs;
33262306a36Sopenharmony_ci}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci/**
33562306a36Sopenharmony_ci * get_req_from_tcs() - Get a stashed request that was xfering on the given TCS.
33662306a36Sopenharmony_ci * @drv:    The RSC controller.
33762306a36Sopenharmony_ci * @tcs_id: The global ID of this TCS.
33862306a36Sopenharmony_ci *
33962306a36Sopenharmony_ci * For ACTIVE_ONLY transfers we want to call back into the client when the
34062306a36Sopenharmony_ci * transfer finishes. To do this we need the "request" that the client
34162306a36Sopenharmony_ci * originally provided us. This function grabs the request that we stashed
34262306a36Sopenharmony_ci * when we started the transfer.
34362306a36Sopenharmony_ci *
34462306a36Sopenharmony_ci * This only makes sense for ACTIVE_ONLY transfers since those are the only
34562306a36Sopenharmony_ci * ones we track sending (the only ones we enable interrupts for and the only
34662306a36Sopenharmony_ci * ones we call back to the client for).
34762306a36Sopenharmony_ci *
34862306a36Sopenharmony_ci * Return: The stashed request.
34962306a36Sopenharmony_ci */
35062306a36Sopenharmony_cistatic const struct tcs_request *get_req_from_tcs(struct rsc_drv *drv,
35162306a36Sopenharmony_ci						  int tcs_id)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	struct tcs_group *tcs;
35462306a36Sopenharmony_ci	int i;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	for (i = 0; i < TCS_TYPE_NR; i++) {
35762306a36Sopenharmony_ci		tcs = &drv->tcs[i];
35862306a36Sopenharmony_ci		if (tcs->mask & BIT(tcs_id))
35962306a36Sopenharmony_ci			return tcs->req[tcs_id - tcs->offset];
36062306a36Sopenharmony_ci	}
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	return NULL;
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci/**
36662306a36Sopenharmony_ci * __tcs_set_trigger() - Start xfer on a TCS or unset trigger on a borrowed TCS
36762306a36Sopenharmony_ci * @drv:     The controller.
36862306a36Sopenharmony_ci * @tcs_id:  The global ID of this TCS.
36962306a36Sopenharmony_ci * @trigger: If true then untrigger/retrigger. If false then just untrigger.
37062306a36Sopenharmony_ci *
37162306a36Sopenharmony_ci * In the normal case we only ever call with "trigger=true" to start a
37262306a36Sopenharmony_ci * transfer. That will un-trigger/disable the TCS from the last transfer
37362306a36Sopenharmony_ci * then trigger/enable for this transfer.
37462306a36Sopenharmony_ci *
37562306a36Sopenharmony_ci * If we borrowed a wake TCS for an active-only transfer we'll also call
37662306a36Sopenharmony_ci * this function with "trigger=false" to just do the un-trigger/disable
37762306a36Sopenharmony_ci * before using the TCS for wake purposes again.
37862306a36Sopenharmony_ci *
37962306a36Sopenharmony_ci * Note that the AP is only in charge of triggering active-only transfers.
38062306a36Sopenharmony_ci * The AP never triggers sleep/wake values using this function.
38162306a36Sopenharmony_ci */
38262306a36Sopenharmony_cistatic void __tcs_set_trigger(struct rsc_drv *drv, int tcs_id, bool trigger)
38362306a36Sopenharmony_ci{
38462306a36Sopenharmony_ci	u32 enable;
38562306a36Sopenharmony_ci	u32 reg = drv->regs[RSC_DRV_CONTROL];
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	/*
38862306a36Sopenharmony_ci	 * HW req: Clear the DRV_CONTROL and enable TCS again
38962306a36Sopenharmony_ci	 * While clearing ensure that the AMC mode trigger is cleared
39062306a36Sopenharmony_ci	 * and then the mode enable is cleared.
39162306a36Sopenharmony_ci	 */
39262306a36Sopenharmony_ci	enable = read_tcs_reg(drv, reg, tcs_id);
39362306a36Sopenharmony_ci	enable &= ~TCS_AMC_MODE_TRIGGER;
39462306a36Sopenharmony_ci	write_tcs_reg_sync(drv, reg, tcs_id, enable);
39562306a36Sopenharmony_ci	enable &= ~TCS_AMC_MODE_ENABLE;
39662306a36Sopenharmony_ci	write_tcs_reg_sync(drv, reg, tcs_id, enable);
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	if (trigger) {
39962306a36Sopenharmony_ci		/* Enable the AMC mode on the TCS and then trigger the TCS */
40062306a36Sopenharmony_ci		enable = TCS_AMC_MODE_ENABLE;
40162306a36Sopenharmony_ci		write_tcs_reg_sync(drv, reg, tcs_id, enable);
40262306a36Sopenharmony_ci		enable |= TCS_AMC_MODE_TRIGGER;
40362306a36Sopenharmony_ci		write_tcs_reg(drv, reg, tcs_id, enable);
40462306a36Sopenharmony_ci	}
40562306a36Sopenharmony_ci}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci/**
40862306a36Sopenharmony_ci * enable_tcs_irq() - Enable or disable interrupts on the given TCS.
40962306a36Sopenharmony_ci * @drv:     The controller.
41062306a36Sopenharmony_ci * @tcs_id:  The global ID of this TCS.
41162306a36Sopenharmony_ci * @enable:  If true then enable; if false then disable
41262306a36Sopenharmony_ci *
41362306a36Sopenharmony_ci * We only ever call this when we borrow a wake TCS for an active-only
41462306a36Sopenharmony_ci * transfer. For active-only TCSes interrupts are always left enabled.
41562306a36Sopenharmony_ci */
41662306a36Sopenharmony_cistatic void enable_tcs_irq(struct rsc_drv *drv, int tcs_id, bool enable)
41762306a36Sopenharmony_ci{
41862306a36Sopenharmony_ci	u32 data;
41962306a36Sopenharmony_ci	u32 reg = drv->regs[RSC_DRV_IRQ_ENABLE];
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	data = readl_relaxed(drv->tcs_base + reg);
42262306a36Sopenharmony_ci	if (enable)
42362306a36Sopenharmony_ci		data |= BIT(tcs_id);
42462306a36Sopenharmony_ci	else
42562306a36Sopenharmony_ci		data &= ~BIT(tcs_id);
42662306a36Sopenharmony_ci	writel_relaxed(data, drv->tcs_base + reg);
42762306a36Sopenharmony_ci}
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci/**
43062306a36Sopenharmony_ci * tcs_tx_done() - TX Done interrupt handler.
43162306a36Sopenharmony_ci * @irq: The IRQ number (ignored).
43262306a36Sopenharmony_ci * @p:   Pointer to "struct rsc_drv".
43362306a36Sopenharmony_ci *
43462306a36Sopenharmony_ci * Called for ACTIVE_ONLY transfers (those are the only ones we enable the
43562306a36Sopenharmony_ci * IRQ for) when a transfer is done.
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci * Return: IRQ_HANDLED
43862306a36Sopenharmony_ci */
43962306a36Sopenharmony_cistatic irqreturn_t tcs_tx_done(int irq, void *p)
44062306a36Sopenharmony_ci{
44162306a36Sopenharmony_ci	struct rsc_drv *drv = p;
44262306a36Sopenharmony_ci	int i;
44362306a36Sopenharmony_ci	unsigned long irq_status;
44462306a36Sopenharmony_ci	const struct tcs_request *req;
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	irq_status = readl_relaxed(drv->tcs_base + drv->regs[RSC_DRV_IRQ_STATUS]);
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci	for_each_set_bit(i, &irq_status, BITS_PER_TYPE(u32)) {
44962306a36Sopenharmony_ci		req = get_req_from_tcs(drv, i);
45062306a36Sopenharmony_ci		if (WARN_ON(!req))
45162306a36Sopenharmony_ci			goto skip;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci		trace_rpmh_tx_done(drv, i, req);
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci		/*
45662306a36Sopenharmony_ci		 * If wake tcs was re-purposed for sending active
45762306a36Sopenharmony_ci		 * votes, clear AMC trigger & enable modes and
45862306a36Sopenharmony_ci		 * disable interrupt for this TCS
45962306a36Sopenharmony_ci		 */
46062306a36Sopenharmony_ci		if (!drv->tcs[ACTIVE_TCS].num_tcs)
46162306a36Sopenharmony_ci			__tcs_set_trigger(drv, i, false);
46262306a36Sopenharmony_ciskip:
46362306a36Sopenharmony_ci		/* Reclaim the TCS */
46462306a36Sopenharmony_ci		write_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], i, 0);
46562306a36Sopenharmony_ci		writel_relaxed(BIT(i), drv->tcs_base + drv->regs[RSC_DRV_IRQ_CLEAR]);
46662306a36Sopenharmony_ci		spin_lock(&drv->lock);
46762306a36Sopenharmony_ci		clear_bit(i, drv->tcs_in_use);
46862306a36Sopenharmony_ci		/*
46962306a36Sopenharmony_ci		 * Disable interrupt for WAKE TCS to avoid being
47062306a36Sopenharmony_ci		 * spammed with interrupts coming when the solver
47162306a36Sopenharmony_ci		 * sends its wake votes.
47262306a36Sopenharmony_ci		 */
47362306a36Sopenharmony_ci		if (!drv->tcs[ACTIVE_TCS].num_tcs)
47462306a36Sopenharmony_ci			enable_tcs_irq(drv, i, false);
47562306a36Sopenharmony_ci		spin_unlock(&drv->lock);
47662306a36Sopenharmony_ci		wake_up(&drv->tcs_wait);
47762306a36Sopenharmony_ci		if (req)
47862306a36Sopenharmony_ci			rpmh_tx_done(req);
47962306a36Sopenharmony_ci	}
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	return IRQ_HANDLED;
48262306a36Sopenharmony_ci}
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci/**
48562306a36Sopenharmony_ci * __tcs_buffer_write() - Write to TCS hardware from a request; don't trigger.
48662306a36Sopenharmony_ci * @drv:    The controller.
48762306a36Sopenharmony_ci * @tcs_id: The global ID of this TCS.
48862306a36Sopenharmony_ci * @cmd_id: The index within the TCS to start writing.
48962306a36Sopenharmony_ci * @msg:    The message we want to send, which will contain several addr/data
49062306a36Sopenharmony_ci *          pairs to program (but few enough that they all fit in one TCS).
49162306a36Sopenharmony_ci *
49262306a36Sopenharmony_ci * This is used for all types of transfers (active, sleep, and wake).
49362306a36Sopenharmony_ci */
49462306a36Sopenharmony_cistatic void __tcs_buffer_write(struct rsc_drv *drv, int tcs_id, int cmd_id,
49562306a36Sopenharmony_ci			       const struct tcs_request *msg)
49662306a36Sopenharmony_ci{
49762306a36Sopenharmony_ci	u32 msgid;
49862306a36Sopenharmony_ci	u32 cmd_msgid = CMD_MSGID_LEN | CMD_MSGID_WRITE;
49962306a36Sopenharmony_ci	u32 cmd_enable = 0;
50062306a36Sopenharmony_ci	struct tcs_cmd *cmd;
50162306a36Sopenharmony_ci	int i, j;
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	/* Convert all commands to RR when the request has wait_for_compl set */
50462306a36Sopenharmony_ci	cmd_msgid |= msg->wait_for_compl ? CMD_MSGID_RESP_REQ : 0;
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	for (i = 0, j = cmd_id; i < msg->num_cmds; i++, j++) {
50762306a36Sopenharmony_ci		cmd = &msg->cmds[i];
50862306a36Sopenharmony_ci		cmd_enable |= BIT(j);
50962306a36Sopenharmony_ci		msgid = cmd_msgid;
51062306a36Sopenharmony_ci		/*
51162306a36Sopenharmony_ci		 * Additionally, if the cmd->wait is set, make the command
51262306a36Sopenharmony_ci		 * response reqd even if the overall request was fire-n-forget.
51362306a36Sopenharmony_ci		 */
51462306a36Sopenharmony_ci		msgid |= cmd->wait ? CMD_MSGID_RESP_REQ : 0;
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci		write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_MSGID], tcs_id, j, msgid);
51762306a36Sopenharmony_ci		write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_ADDR], tcs_id, j, cmd->addr);
51862306a36Sopenharmony_ci		write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_DATA], tcs_id, j, cmd->data);
51962306a36Sopenharmony_ci		trace_rpmh_send_msg(drv, tcs_id, msg->state, j, msgid, cmd);
52062306a36Sopenharmony_ci	}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	cmd_enable |= read_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id);
52362306a36Sopenharmony_ci	write_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id, cmd_enable);
52462306a36Sopenharmony_ci}
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci/**
52762306a36Sopenharmony_ci * check_for_req_inflight() - Look to see if conflicting cmds are in flight.
52862306a36Sopenharmony_ci * @drv: The controller.
52962306a36Sopenharmony_ci * @tcs: A pointer to the tcs_group used for ACTIVE_ONLY transfers.
53062306a36Sopenharmony_ci * @msg: The message we want to send, which will contain several addr/data
53162306a36Sopenharmony_ci *       pairs to program (but few enough that they all fit in one TCS).
53262306a36Sopenharmony_ci *
53362306a36Sopenharmony_ci * This will walk through the TCSes in the group and check if any of them
53462306a36Sopenharmony_ci * appear to be sending to addresses referenced in the message. If it finds
53562306a36Sopenharmony_ci * one it'll return -EBUSY.
53662306a36Sopenharmony_ci *
53762306a36Sopenharmony_ci * Only for use for active-only transfers.
53862306a36Sopenharmony_ci *
53962306a36Sopenharmony_ci * Must be called with the drv->lock held since that protects tcs_in_use.
54062306a36Sopenharmony_ci *
54162306a36Sopenharmony_ci * Return: 0 if nothing in flight or -EBUSY if we should try again later.
54262306a36Sopenharmony_ci *         The caller must re-enable interrupts between tries since that's
54362306a36Sopenharmony_ci *         the only way tcs_in_use will ever be updated and the only way
54462306a36Sopenharmony_ci *         RSC_DRV_CMD_ENABLE will ever be cleared.
54562306a36Sopenharmony_ci */
54662306a36Sopenharmony_cistatic int check_for_req_inflight(struct rsc_drv *drv, struct tcs_group *tcs,
54762306a36Sopenharmony_ci				  const struct tcs_request *msg)
54862306a36Sopenharmony_ci{
54962306a36Sopenharmony_ci	unsigned long curr_enabled;
55062306a36Sopenharmony_ci	u32 addr;
55162306a36Sopenharmony_ci	int j, k;
55262306a36Sopenharmony_ci	int i = tcs->offset;
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	for_each_set_bit_from(i, drv->tcs_in_use, tcs->offset + tcs->num_tcs) {
55562306a36Sopenharmony_ci		curr_enabled = read_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], i);
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci		for_each_set_bit(j, &curr_enabled, MAX_CMDS_PER_TCS) {
55862306a36Sopenharmony_ci			addr = read_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_ADDR], i, j);
55962306a36Sopenharmony_ci			for (k = 0; k < msg->num_cmds; k++) {
56062306a36Sopenharmony_ci				if (addr == msg->cmds[k].addr)
56162306a36Sopenharmony_ci					return -EBUSY;
56262306a36Sopenharmony_ci			}
56362306a36Sopenharmony_ci		}
56462306a36Sopenharmony_ci	}
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci	return 0;
56762306a36Sopenharmony_ci}
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci/**
57062306a36Sopenharmony_ci * find_free_tcs() - Find free tcs in the given tcs_group; only for active.
57162306a36Sopenharmony_ci * @tcs: A pointer to the active-only tcs_group (or the wake tcs_group if
57262306a36Sopenharmony_ci *       we borrowed it because there are zero active-only ones).
57362306a36Sopenharmony_ci *
57462306a36Sopenharmony_ci * Must be called with the drv->lock held since that protects tcs_in_use.
57562306a36Sopenharmony_ci *
57662306a36Sopenharmony_ci * Return: The first tcs that's free or -EBUSY if all in use.
57762306a36Sopenharmony_ci */
57862306a36Sopenharmony_cistatic int find_free_tcs(struct tcs_group *tcs)
57962306a36Sopenharmony_ci{
58062306a36Sopenharmony_ci	const struct rsc_drv *drv = tcs->drv;
58162306a36Sopenharmony_ci	unsigned long i;
58262306a36Sopenharmony_ci	unsigned long max = tcs->offset + tcs->num_tcs;
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	i = find_next_zero_bit(drv->tcs_in_use, max, tcs->offset);
58562306a36Sopenharmony_ci	if (i >= max)
58662306a36Sopenharmony_ci		return -EBUSY;
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	return i;
58962306a36Sopenharmony_ci}
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci/**
59262306a36Sopenharmony_ci * claim_tcs_for_req() - Claim a tcs in the given tcs_group; only for active.
59362306a36Sopenharmony_ci * @drv: The controller.
59462306a36Sopenharmony_ci * @tcs: The tcs_group used for ACTIVE_ONLY transfers.
59562306a36Sopenharmony_ci * @msg: The data to be sent.
59662306a36Sopenharmony_ci *
59762306a36Sopenharmony_ci * Claims a tcs in the given tcs_group while making sure that no existing cmd
59862306a36Sopenharmony_ci * is in flight that would conflict with the one in @msg.
59962306a36Sopenharmony_ci *
60062306a36Sopenharmony_ci * Context: Must be called with the drv->lock held since that protects
60162306a36Sopenharmony_ci * tcs_in_use.
60262306a36Sopenharmony_ci *
60362306a36Sopenharmony_ci * Return: The id of the claimed tcs or -EBUSY if a matching msg is in flight
60462306a36Sopenharmony_ci * or the tcs_group is full.
60562306a36Sopenharmony_ci */
60662306a36Sopenharmony_cistatic int claim_tcs_for_req(struct rsc_drv *drv, struct tcs_group *tcs,
60762306a36Sopenharmony_ci			     const struct tcs_request *msg)
60862306a36Sopenharmony_ci{
60962306a36Sopenharmony_ci	int ret;
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci	/*
61262306a36Sopenharmony_ci	 * The h/w does not like if we send a request to the same address,
61362306a36Sopenharmony_ci	 * when one is already in-flight or being processed.
61462306a36Sopenharmony_ci	 */
61562306a36Sopenharmony_ci	ret = check_for_req_inflight(drv, tcs, msg);
61662306a36Sopenharmony_ci	if (ret)
61762306a36Sopenharmony_ci		return ret;
61862306a36Sopenharmony_ci
61962306a36Sopenharmony_ci	return find_free_tcs(tcs);
62062306a36Sopenharmony_ci}
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci/**
62362306a36Sopenharmony_ci * rpmh_rsc_send_data() - Write / trigger active-only message.
62462306a36Sopenharmony_ci * @drv: The controller.
62562306a36Sopenharmony_ci * @msg: The data to be sent.
62662306a36Sopenharmony_ci *
62762306a36Sopenharmony_ci * NOTES:
62862306a36Sopenharmony_ci * - This is only used for "ACTIVE_ONLY" since the limitations of this
62962306a36Sopenharmony_ci *   function don't make sense for sleep/wake cases.
63062306a36Sopenharmony_ci * - To do the transfer, we will grab a whole TCS for ourselves--we don't
63162306a36Sopenharmony_ci *   try to share. If there are none available we'll wait indefinitely
63262306a36Sopenharmony_ci *   for a free one.
63362306a36Sopenharmony_ci * - This function will not wait for the commands to be finished, only for
63462306a36Sopenharmony_ci *   data to be programmed into the RPMh. See rpmh_tx_done() which will
63562306a36Sopenharmony_ci *   be called when the transfer is fully complete.
63662306a36Sopenharmony_ci * - This function must be called with interrupts enabled. If the hardware
63762306a36Sopenharmony_ci *   is busy doing someone else's transfer we need that transfer to fully
63862306a36Sopenharmony_ci *   finish so that we can have the hardware, and to fully finish it needs
63962306a36Sopenharmony_ci *   the interrupt handler to run. If the interrupts is set to run on the
64062306a36Sopenharmony_ci *   active CPU this can never happen if interrupts are disabled.
64162306a36Sopenharmony_ci *
64262306a36Sopenharmony_ci * Return: 0 on success, -EINVAL on error.
64362306a36Sopenharmony_ci */
64462306a36Sopenharmony_ciint rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg)
64562306a36Sopenharmony_ci{
64662306a36Sopenharmony_ci	struct tcs_group *tcs;
64762306a36Sopenharmony_ci	int tcs_id;
64862306a36Sopenharmony_ci	unsigned long flags;
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci	tcs = get_tcs_for_msg(drv, msg);
65162306a36Sopenharmony_ci	if (IS_ERR(tcs))
65262306a36Sopenharmony_ci		return PTR_ERR(tcs);
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	spin_lock_irqsave(&drv->lock, flags);
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci	/* Wait forever for a free tcs. It better be there eventually! */
65762306a36Sopenharmony_ci	wait_event_lock_irq(drv->tcs_wait,
65862306a36Sopenharmony_ci			    (tcs_id = claim_tcs_for_req(drv, tcs, msg)) >= 0,
65962306a36Sopenharmony_ci			    drv->lock);
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci	tcs->req[tcs_id - tcs->offset] = msg;
66262306a36Sopenharmony_ci	set_bit(tcs_id, drv->tcs_in_use);
66362306a36Sopenharmony_ci	if (msg->state == RPMH_ACTIVE_ONLY_STATE && tcs->type != ACTIVE_TCS) {
66462306a36Sopenharmony_ci		/*
66562306a36Sopenharmony_ci		 * Clear previously programmed WAKE commands in selected
66662306a36Sopenharmony_ci		 * repurposed TCS to avoid triggering them. tcs->slots will be
66762306a36Sopenharmony_ci		 * cleaned from rpmh_flush() by invoking rpmh_rsc_invalidate()
66862306a36Sopenharmony_ci		 */
66962306a36Sopenharmony_ci		write_tcs_reg_sync(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id, 0);
67062306a36Sopenharmony_ci		enable_tcs_irq(drv, tcs_id, true);
67162306a36Sopenharmony_ci	}
67262306a36Sopenharmony_ci	spin_unlock_irqrestore(&drv->lock, flags);
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci	/*
67562306a36Sopenharmony_ci	 * These two can be done after the lock is released because:
67662306a36Sopenharmony_ci	 * - We marked "tcs_in_use" under lock.
67762306a36Sopenharmony_ci	 * - Once "tcs_in_use" has been marked nobody else could be writing
67862306a36Sopenharmony_ci	 *   to these registers until the interrupt goes off.
67962306a36Sopenharmony_ci	 * - The interrupt can't go off until we trigger w/ the last line
68062306a36Sopenharmony_ci	 *   of __tcs_set_trigger() below.
68162306a36Sopenharmony_ci	 */
68262306a36Sopenharmony_ci	__tcs_buffer_write(drv, tcs_id, 0, msg);
68362306a36Sopenharmony_ci	__tcs_set_trigger(drv, tcs_id, true);
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci	return 0;
68662306a36Sopenharmony_ci}
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ci/**
68962306a36Sopenharmony_ci * find_slots() - Find a place to write the given message.
69062306a36Sopenharmony_ci * @tcs:    The tcs group to search.
69162306a36Sopenharmony_ci * @msg:    The message we want to find room for.
69262306a36Sopenharmony_ci * @tcs_id: If we return 0 from the function, we return the global ID of the
69362306a36Sopenharmony_ci *          TCS to write to here.
69462306a36Sopenharmony_ci * @cmd_id: If we return 0 from the function, we return the index of
69562306a36Sopenharmony_ci *          the command array of the returned TCS where the client should
69662306a36Sopenharmony_ci *          start writing the message.
69762306a36Sopenharmony_ci *
69862306a36Sopenharmony_ci * Only for use on sleep/wake TCSes since those are the only ones we maintain
69962306a36Sopenharmony_ci * tcs->slots for.
70062306a36Sopenharmony_ci *
70162306a36Sopenharmony_ci * Return: -ENOMEM if there was no room, else 0.
70262306a36Sopenharmony_ci */
70362306a36Sopenharmony_cistatic int find_slots(struct tcs_group *tcs, const struct tcs_request *msg,
70462306a36Sopenharmony_ci		      int *tcs_id, int *cmd_id)
70562306a36Sopenharmony_ci{
70662306a36Sopenharmony_ci	int slot, offset;
70762306a36Sopenharmony_ci	int i = 0;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	/* Do over, until we can fit the full payload in a single TCS */
71062306a36Sopenharmony_ci	do {
71162306a36Sopenharmony_ci		slot = bitmap_find_next_zero_area(tcs->slots, MAX_TCS_SLOTS,
71262306a36Sopenharmony_ci						  i, msg->num_cmds, 0);
71362306a36Sopenharmony_ci		if (slot >= tcs->num_tcs * tcs->ncpt)
71462306a36Sopenharmony_ci			return -ENOMEM;
71562306a36Sopenharmony_ci		i += tcs->ncpt;
71662306a36Sopenharmony_ci	} while (slot + msg->num_cmds - 1 >= i);
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	bitmap_set(tcs->slots, slot, msg->num_cmds);
71962306a36Sopenharmony_ci
72062306a36Sopenharmony_ci	offset = slot / tcs->ncpt;
72162306a36Sopenharmony_ci	*tcs_id = offset + tcs->offset;
72262306a36Sopenharmony_ci	*cmd_id = slot % tcs->ncpt;
72362306a36Sopenharmony_ci
72462306a36Sopenharmony_ci	return 0;
72562306a36Sopenharmony_ci}
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci/**
72862306a36Sopenharmony_ci * rpmh_rsc_write_ctrl_data() - Write request to controller but don't trigger.
72962306a36Sopenharmony_ci * @drv: The controller.
73062306a36Sopenharmony_ci * @msg: The data to be written to the controller.
73162306a36Sopenharmony_ci *
73262306a36Sopenharmony_ci * This should only be called for sleep/wake state, never active-only
73362306a36Sopenharmony_ci * state.
73462306a36Sopenharmony_ci *
73562306a36Sopenharmony_ci * The caller must ensure that no other RPMH actions are happening and the
73662306a36Sopenharmony_ci * controller is idle when this function is called since it runs lockless.
73762306a36Sopenharmony_ci *
73862306a36Sopenharmony_ci * Return: 0 if no error; else -error.
73962306a36Sopenharmony_ci */
74062306a36Sopenharmony_ciint rpmh_rsc_write_ctrl_data(struct rsc_drv *drv, const struct tcs_request *msg)
74162306a36Sopenharmony_ci{
74262306a36Sopenharmony_ci	struct tcs_group *tcs;
74362306a36Sopenharmony_ci	int tcs_id = 0, cmd_id = 0;
74462306a36Sopenharmony_ci	int ret;
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_ci	tcs = get_tcs_for_msg(drv, msg);
74762306a36Sopenharmony_ci	if (IS_ERR(tcs))
74862306a36Sopenharmony_ci		return PTR_ERR(tcs);
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci	/* find the TCS id and the command in the TCS to write to */
75162306a36Sopenharmony_ci	ret = find_slots(tcs, msg, &tcs_id, &cmd_id);
75262306a36Sopenharmony_ci	if (!ret)
75362306a36Sopenharmony_ci		__tcs_buffer_write(drv, tcs_id, cmd_id, msg);
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci	return ret;
75662306a36Sopenharmony_ci}
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci/**
75962306a36Sopenharmony_ci * rpmh_rsc_ctrlr_is_busy() - Check if any of the AMCs are busy.
76062306a36Sopenharmony_ci * @drv: The controller
76162306a36Sopenharmony_ci *
76262306a36Sopenharmony_ci * Checks if any of the AMCs are busy in handling ACTIVE sets.
76362306a36Sopenharmony_ci * This is called from the last cpu powering down before flushing
76462306a36Sopenharmony_ci * SLEEP and WAKE sets. If AMCs are busy, controller can not enter
76562306a36Sopenharmony_ci * power collapse, so deny from the last cpu's pm notification.
76662306a36Sopenharmony_ci *
76762306a36Sopenharmony_ci * Context: Must be called with the drv->lock held.
76862306a36Sopenharmony_ci *
76962306a36Sopenharmony_ci * Return:
77062306a36Sopenharmony_ci * * False		- AMCs are idle
77162306a36Sopenharmony_ci * * True		- AMCs are busy
77262306a36Sopenharmony_ci */
77362306a36Sopenharmony_cistatic bool rpmh_rsc_ctrlr_is_busy(struct rsc_drv *drv)
77462306a36Sopenharmony_ci{
77562306a36Sopenharmony_ci	unsigned long set;
77662306a36Sopenharmony_ci	const struct tcs_group *tcs = &drv->tcs[ACTIVE_TCS];
77762306a36Sopenharmony_ci	unsigned long max;
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci	/*
78062306a36Sopenharmony_ci	 * If we made an active request on a RSC that does not have a
78162306a36Sopenharmony_ci	 * dedicated TCS for active state use, then re-purposed wake TCSes
78262306a36Sopenharmony_ci	 * should be checked for not busy, because we used wake TCSes for
78362306a36Sopenharmony_ci	 * active requests in this case.
78462306a36Sopenharmony_ci	 */
78562306a36Sopenharmony_ci	if (!tcs->num_tcs)
78662306a36Sopenharmony_ci		tcs = &drv->tcs[WAKE_TCS];
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_ci	max = tcs->offset + tcs->num_tcs;
78962306a36Sopenharmony_ci	set = find_next_bit(drv->tcs_in_use, max, tcs->offset);
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ci	return set < max;
79262306a36Sopenharmony_ci}
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci/**
79562306a36Sopenharmony_ci * rpmh_rsc_write_next_wakeup() - Write next wakeup in CONTROL_TCS.
79662306a36Sopenharmony_ci * @drv: The controller
79762306a36Sopenharmony_ci *
79862306a36Sopenharmony_ci * Writes maximum wakeup cycles when called from suspend.
79962306a36Sopenharmony_ci * Writes earliest hrtimer wakeup when called from idle.
80062306a36Sopenharmony_ci */
80162306a36Sopenharmony_civoid rpmh_rsc_write_next_wakeup(struct rsc_drv *drv)
80262306a36Sopenharmony_ci{
80362306a36Sopenharmony_ci	ktime_t now, wakeup;
80462306a36Sopenharmony_ci	u64 wakeup_us, wakeup_cycles = ~0;
80562306a36Sopenharmony_ci	u32 lo, hi;
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci	if (!drv->tcs[CONTROL_TCS].num_tcs || !drv->genpd_nb.notifier_call)
80862306a36Sopenharmony_ci		return;
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_ci	/* Set highest time when system (timekeeping) is suspended */
81162306a36Sopenharmony_ci	if (system_state == SYSTEM_SUSPEND)
81262306a36Sopenharmony_ci		goto exit;
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_ci	/* Find the earliest hrtimer wakeup from online cpus */
81562306a36Sopenharmony_ci	wakeup = dev_pm_genpd_get_next_hrtimer(drv->dev);
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci	/* Find the relative wakeup in kernel time scale */
81862306a36Sopenharmony_ci	now = ktime_get();
81962306a36Sopenharmony_ci	wakeup = ktime_sub(wakeup, now);
82062306a36Sopenharmony_ci	wakeup_us = ktime_to_us(wakeup);
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ci	/* Convert the wakeup to arch timer scale */
82362306a36Sopenharmony_ci	wakeup_cycles = USECS_TO_CYCLES(wakeup_us);
82462306a36Sopenharmony_ci	wakeup_cycles += arch_timer_read_counter();
82562306a36Sopenharmony_ci
82662306a36Sopenharmony_ciexit:
82762306a36Sopenharmony_ci	lo = wakeup_cycles & RSC_DRV_CTL_TCS_DATA_LO_MASK;
82862306a36Sopenharmony_ci	hi = wakeup_cycles >> RSC_DRV_CTL_TCS_DATA_SIZE;
82962306a36Sopenharmony_ci	hi &= RSC_DRV_CTL_TCS_DATA_HI_MASK;
83062306a36Sopenharmony_ci	hi |= RSC_DRV_CTL_TCS_DATA_HI_VALID;
83162306a36Sopenharmony_ci
83262306a36Sopenharmony_ci	writel_relaxed(lo, drv->base + RSC_DRV_CTL_TCS_DATA_LO);
83362306a36Sopenharmony_ci	writel_relaxed(hi, drv->base + RSC_DRV_CTL_TCS_DATA_HI);
83462306a36Sopenharmony_ci}
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci/**
83762306a36Sopenharmony_ci * rpmh_rsc_cpu_pm_callback() - Check if any of the AMCs are busy.
83862306a36Sopenharmony_ci * @nfb:    Pointer to the notifier block in struct rsc_drv.
83962306a36Sopenharmony_ci * @action: CPU_PM_ENTER, CPU_PM_ENTER_FAILED, or CPU_PM_EXIT.
84062306a36Sopenharmony_ci * @v:      Unused
84162306a36Sopenharmony_ci *
84262306a36Sopenharmony_ci * This function is given to cpu_pm_register_notifier so we can be informed
84362306a36Sopenharmony_ci * about when CPUs go down. When all CPUs go down we know no more active
84462306a36Sopenharmony_ci * transfers will be started so we write sleep/wake sets. This function gets
84562306a36Sopenharmony_ci * called from cpuidle code paths and also at system suspend time.
84662306a36Sopenharmony_ci *
84762306a36Sopenharmony_ci * If its last CPU going down and AMCs are not busy then writes cached sleep
84862306a36Sopenharmony_ci * and wake messages to TCSes. The firmware then takes care of triggering
84962306a36Sopenharmony_ci * them when entering deepest low power modes.
85062306a36Sopenharmony_ci *
85162306a36Sopenharmony_ci * Return: See cpu_pm_register_notifier()
85262306a36Sopenharmony_ci */
85362306a36Sopenharmony_cistatic int rpmh_rsc_cpu_pm_callback(struct notifier_block *nfb,
85462306a36Sopenharmony_ci				    unsigned long action, void *v)
85562306a36Sopenharmony_ci{
85662306a36Sopenharmony_ci	struct rsc_drv *drv = container_of(nfb, struct rsc_drv, rsc_pm);
85762306a36Sopenharmony_ci	int ret = NOTIFY_OK;
85862306a36Sopenharmony_ci	int cpus_in_pm;
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_ci	switch (action) {
86162306a36Sopenharmony_ci	case CPU_PM_ENTER:
86262306a36Sopenharmony_ci		cpus_in_pm = atomic_inc_return(&drv->cpus_in_pm);
86362306a36Sopenharmony_ci		/*
86462306a36Sopenharmony_ci		 * NOTE: comments for num_online_cpus() point out that it's
86562306a36Sopenharmony_ci		 * only a snapshot so we need to be careful. It should be OK
86662306a36Sopenharmony_ci		 * for us to use, though.  It's important for us not to miss
86762306a36Sopenharmony_ci		 * if we're the last CPU going down so it would only be a
86862306a36Sopenharmony_ci		 * problem if a CPU went offline right after we did the check
86962306a36Sopenharmony_ci		 * AND that CPU was not idle AND that CPU was the last non-idle
87062306a36Sopenharmony_ci		 * CPU. That can't happen. CPUs would have to come out of idle
87162306a36Sopenharmony_ci		 * before the CPU could go offline.
87262306a36Sopenharmony_ci		 */
87362306a36Sopenharmony_ci		if (cpus_in_pm < num_online_cpus())
87462306a36Sopenharmony_ci			return NOTIFY_OK;
87562306a36Sopenharmony_ci		break;
87662306a36Sopenharmony_ci	case CPU_PM_ENTER_FAILED:
87762306a36Sopenharmony_ci	case CPU_PM_EXIT:
87862306a36Sopenharmony_ci		atomic_dec(&drv->cpus_in_pm);
87962306a36Sopenharmony_ci		return NOTIFY_OK;
88062306a36Sopenharmony_ci	default:
88162306a36Sopenharmony_ci		return NOTIFY_DONE;
88262306a36Sopenharmony_ci	}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci	/*
88562306a36Sopenharmony_ci	 * It's likely we're on the last CPU. Grab the drv->lock and write
88662306a36Sopenharmony_ci	 * out the sleep/wake commands to RPMH hardware. Grabbing the lock
88762306a36Sopenharmony_ci	 * means that if we race with another CPU coming up we are still
88862306a36Sopenharmony_ci	 * guaranteed to be safe. If another CPU came up just after we checked
88962306a36Sopenharmony_ci	 * and has grabbed the lock or started an active transfer then we'll
89062306a36Sopenharmony_ci	 * notice we're busy and abort. If another CPU comes up after we start
89162306a36Sopenharmony_ci	 * flushing it will be blocked from starting an active transfer until
89262306a36Sopenharmony_ci	 * we're done flushing. If another CPU starts an active transfer after
89362306a36Sopenharmony_ci	 * we release the lock we're still OK because we're no longer the last
89462306a36Sopenharmony_ci	 * CPU.
89562306a36Sopenharmony_ci	 */
89662306a36Sopenharmony_ci	if (spin_trylock(&drv->lock)) {
89762306a36Sopenharmony_ci		if (rpmh_rsc_ctrlr_is_busy(drv) || rpmh_flush(&drv->client))
89862306a36Sopenharmony_ci			ret = NOTIFY_BAD;
89962306a36Sopenharmony_ci		spin_unlock(&drv->lock);
90062306a36Sopenharmony_ci	} else {
90162306a36Sopenharmony_ci		/* Another CPU must be up */
90262306a36Sopenharmony_ci		return NOTIFY_OK;
90362306a36Sopenharmony_ci	}
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci	if (ret == NOTIFY_BAD) {
90662306a36Sopenharmony_ci		/* Double-check if we're here because someone else is up */
90762306a36Sopenharmony_ci		if (cpus_in_pm < num_online_cpus())
90862306a36Sopenharmony_ci			ret = NOTIFY_OK;
90962306a36Sopenharmony_ci		else
91062306a36Sopenharmony_ci			/* We won't be called w/ CPU_PM_ENTER_FAILED */
91162306a36Sopenharmony_ci			atomic_dec(&drv->cpus_in_pm);
91262306a36Sopenharmony_ci	}
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci	return ret;
91562306a36Sopenharmony_ci}
91662306a36Sopenharmony_ci
91762306a36Sopenharmony_ci/**
91862306a36Sopenharmony_ci * rpmh_rsc_pd_callback() - Check if any of the AMCs are busy.
91962306a36Sopenharmony_ci * @nfb:    Pointer to the genpd notifier block in struct rsc_drv.
92062306a36Sopenharmony_ci * @action: GENPD_NOTIFY_PRE_OFF, GENPD_NOTIFY_OFF, GENPD_NOTIFY_PRE_ON or GENPD_NOTIFY_ON.
92162306a36Sopenharmony_ci * @v:      Unused
92262306a36Sopenharmony_ci *
92362306a36Sopenharmony_ci * This function is given to dev_pm_genpd_add_notifier() so we can be informed
92462306a36Sopenharmony_ci * about when cluster-pd is going down. When cluster go down we know no more active
92562306a36Sopenharmony_ci * transfers will be started so we write sleep/wake sets. This function gets
92662306a36Sopenharmony_ci * called from cpuidle code paths and also at system suspend time.
92762306a36Sopenharmony_ci *
92862306a36Sopenharmony_ci * If AMCs are not busy then writes cached sleep and wake messages to TCSes.
92962306a36Sopenharmony_ci * The firmware then takes care of triggering them when entering deepest low power modes.
93062306a36Sopenharmony_ci *
93162306a36Sopenharmony_ci * Return:
93262306a36Sopenharmony_ci * * NOTIFY_OK          - success
93362306a36Sopenharmony_ci * * NOTIFY_BAD         - failure
93462306a36Sopenharmony_ci */
93562306a36Sopenharmony_cistatic int rpmh_rsc_pd_callback(struct notifier_block *nfb,
93662306a36Sopenharmony_ci				unsigned long action, void *v)
93762306a36Sopenharmony_ci{
93862306a36Sopenharmony_ci	struct rsc_drv *drv = container_of(nfb, struct rsc_drv, genpd_nb);
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	/* We don't need to lock as genpd on/off are serialized */
94162306a36Sopenharmony_ci	if ((action == GENPD_NOTIFY_PRE_OFF) &&
94262306a36Sopenharmony_ci	    (rpmh_rsc_ctrlr_is_busy(drv) || rpmh_flush(&drv->client)))
94362306a36Sopenharmony_ci		return NOTIFY_BAD;
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_ci	return NOTIFY_OK;
94662306a36Sopenharmony_ci}
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_cistatic int rpmh_rsc_pd_attach(struct rsc_drv *drv, struct device *dev)
94962306a36Sopenharmony_ci{
95062306a36Sopenharmony_ci	int ret;
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ci	pm_runtime_enable(dev);
95362306a36Sopenharmony_ci	drv->genpd_nb.notifier_call = rpmh_rsc_pd_callback;
95462306a36Sopenharmony_ci	ret = dev_pm_genpd_add_notifier(dev, &drv->genpd_nb);
95562306a36Sopenharmony_ci	if (ret)
95662306a36Sopenharmony_ci		pm_runtime_disable(dev);
95762306a36Sopenharmony_ci
95862306a36Sopenharmony_ci	return ret;
95962306a36Sopenharmony_ci}
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_cistatic int rpmh_probe_tcs_config(struct platform_device *pdev, struct rsc_drv *drv)
96262306a36Sopenharmony_ci{
96362306a36Sopenharmony_ci	struct tcs_type_config {
96462306a36Sopenharmony_ci		u32 type;
96562306a36Sopenharmony_ci		u32 n;
96662306a36Sopenharmony_ci	} tcs_cfg[TCS_TYPE_NR] = { { 0 } };
96762306a36Sopenharmony_ci	struct device_node *dn = pdev->dev.of_node;
96862306a36Sopenharmony_ci	u32 config, max_tcs, ncpt, offset;
96962306a36Sopenharmony_ci	int i, ret, n, st = 0;
97062306a36Sopenharmony_ci	struct tcs_group *tcs;
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci	ret = of_property_read_u32(dn, "qcom,tcs-offset", &offset);
97362306a36Sopenharmony_ci	if (ret)
97462306a36Sopenharmony_ci		return ret;
97562306a36Sopenharmony_ci	drv->tcs_base = drv->base + offset;
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci	config = readl_relaxed(drv->base + drv->regs[DRV_PRNT_CHLD_CONFIG]);
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_ci	max_tcs = config;
98062306a36Sopenharmony_ci	max_tcs &= DRV_NUM_TCS_MASK << (DRV_NUM_TCS_SHIFT * drv->id);
98162306a36Sopenharmony_ci	max_tcs = max_tcs >> (DRV_NUM_TCS_SHIFT * drv->id);
98262306a36Sopenharmony_ci
98362306a36Sopenharmony_ci	ncpt = config & (DRV_NCPT_MASK << DRV_NCPT_SHIFT);
98462306a36Sopenharmony_ci	ncpt = ncpt >> DRV_NCPT_SHIFT;
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_ci	n = of_property_count_u32_elems(dn, "qcom,tcs-config");
98762306a36Sopenharmony_ci	if (n != 2 * TCS_TYPE_NR)
98862306a36Sopenharmony_ci		return -EINVAL;
98962306a36Sopenharmony_ci
99062306a36Sopenharmony_ci	for (i = 0; i < TCS_TYPE_NR; i++) {
99162306a36Sopenharmony_ci		ret = of_property_read_u32_index(dn, "qcom,tcs-config",
99262306a36Sopenharmony_ci						 i * 2, &tcs_cfg[i].type);
99362306a36Sopenharmony_ci		if (ret)
99462306a36Sopenharmony_ci			return ret;
99562306a36Sopenharmony_ci		if (tcs_cfg[i].type >= TCS_TYPE_NR)
99662306a36Sopenharmony_ci			return -EINVAL;
99762306a36Sopenharmony_ci
99862306a36Sopenharmony_ci		ret = of_property_read_u32_index(dn, "qcom,tcs-config",
99962306a36Sopenharmony_ci						 i * 2 + 1, &tcs_cfg[i].n);
100062306a36Sopenharmony_ci		if (ret)
100162306a36Sopenharmony_ci			return ret;
100262306a36Sopenharmony_ci		if (tcs_cfg[i].n > MAX_TCS_PER_TYPE)
100362306a36Sopenharmony_ci			return -EINVAL;
100462306a36Sopenharmony_ci	}
100562306a36Sopenharmony_ci
100662306a36Sopenharmony_ci	for (i = 0; i < TCS_TYPE_NR; i++) {
100762306a36Sopenharmony_ci		tcs = &drv->tcs[tcs_cfg[i].type];
100862306a36Sopenharmony_ci		if (tcs->drv)
100962306a36Sopenharmony_ci			return -EINVAL;
101062306a36Sopenharmony_ci		tcs->drv = drv;
101162306a36Sopenharmony_ci		tcs->type = tcs_cfg[i].type;
101262306a36Sopenharmony_ci		tcs->num_tcs = tcs_cfg[i].n;
101362306a36Sopenharmony_ci		tcs->ncpt = ncpt;
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci		if (!tcs->num_tcs || tcs->type == CONTROL_TCS)
101662306a36Sopenharmony_ci			continue;
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci		if (st + tcs->num_tcs > max_tcs ||
101962306a36Sopenharmony_ci		    st + tcs->num_tcs >= BITS_PER_BYTE * sizeof(tcs->mask))
102062306a36Sopenharmony_ci			return -EINVAL;
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_ci		tcs->mask = ((1 << tcs->num_tcs) - 1) << st;
102362306a36Sopenharmony_ci		tcs->offset = st;
102462306a36Sopenharmony_ci		st += tcs->num_tcs;
102562306a36Sopenharmony_ci	}
102662306a36Sopenharmony_ci
102762306a36Sopenharmony_ci	drv->num_tcs = st;
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_ci	return 0;
103062306a36Sopenharmony_ci}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_cistatic int rpmh_rsc_probe(struct platform_device *pdev)
103362306a36Sopenharmony_ci{
103462306a36Sopenharmony_ci	struct device_node *dn = pdev->dev.of_node;
103562306a36Sopenharmony_ci	struct rsc_drv *drv;
103662306a36Sopenharmony_ci	char drv_id[10] = {0};
103762306a36Sopenharmony_ci	int ret, irq;
103862306a36Sopenharmony_ci	u32 solver_config;
103962306a36Sopenharmony_ci	u32 rsc_id;
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	/*
104262306a36Sopenharmony_ci	 * Even though RPMh doesn't directly use cmd-db, all of its children
104362306a36Sopenharmony_ci	 * do. To avoid adding this check to our children we'll do it now.
104462306a36Sopenharmony_ci	 */
104562306a36Sopenharmony_ci	ret = cmd_db_ready();
104662306a36Sopenharmony_ci	if (ret) {
104762306a36Sopenharmony_ci		if (ret != -EPROBE_DEFER)
104862306a36Sopenharmony_ci			dev_err(&pdev->dev, "Command DB not available (%d)\n",
104962306a36Sopenharmony_ci									ret);
105062306a36Sopenharmony_ci		return ret;
105162306a36Sopenharmony_ci	}
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
105462306a36Sopenharmony_ci	if (!drv)
105562306a36Sopenharmony_ci		return -ENOMEM;
105662306a36Sopenharmony_ci
105762306a36Sopenharmony_ci	ret = of_property_read_u32(dn, "qcom,drv-id", &drv->id);
105862306a36Sopenharmony_ci	if (ret)
105962306a36Sopenharmony_ci		return ret;
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_ci	drv->name = of_get_property(dn, "label", NULL);
106262306a36Sopenharmony_ci	if (!drv->name)
106362306a36Sopenharmony_ci		drv->name = dev_name(&pdev->dev);
106462306a36Sopenharmony_ci
106562306a36Sopenharmony_ci	snprintf(drv_id, ARRAY_SIZE(drv_id), "drv-%d", drv->id);
106662306a36Sopenharmony_ci	drv->base = devm_platform_ioremap_resource_byname(pdev, drv_id);
106762306a36Sopenharmony_ci	if (IS_ERR(drv->base))
106862306a36Sopenharmony_ci		return PTR_ERR(drv->base);
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci	rsc_id = readl_relaxed(drv->base + RSC_DRV_ID);
107162306a36Sopenharmony_ci	drv->ver.major = rsc_id & (MAJOR_VER_MASK << MAJOR_VER_SHIFT);
107262306a36Sopenharmony_ci	drv->ver.major >>= MAJOR_VER_SHIFT;
107362306a36Sopenharmony_ci	drv->ver.minor = rsc_id & (MINOR_VER_MASK << MINOR_VER_SHIFT);
107462306a36Sopenharmony_ci	drv->ver.minor >>= MINOR_VER_SHIFT;
107562306a36Sopenharmony_ci
107662306a36Sopenharmony_ci	if (drv->ver.major == 3)
107762306a36Sopenharmony_ci		drv->regs = rpmh_rsc_reg_offset_ver_3_0;
107862306a36Sopenharmony_ci	else
107962306a36Sopenharmony_ci		drv->regs = rpmh_rsc_reg_offset_ver_2_7;
108062306a36Sopenharmony_ci
108162306a36Sopenharmony_ci	ret = rpmh_probe_tcs_config(pdev, drv);
108262306a36Sopenharmony_ci	if (ret)
108362306a36Sopenharmony_ci		return ret;
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_ci	spin_lock_init(&drv->lock);
108662306a36Sopenharmony_ci	init_waitqueue_head(&drv->tcs_wait);
108762306a36Sopenharmony_ci	bitmap_zero(drv->tcs_in_use, MAX_TCS_NR);
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci	irq = platform_get_irq(pdev, drv->id);
109062306a36Sopenharmony_ci	if (irq < 0)
109162306a36Sopenharmony_ci		return irq;
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, irq, tcs_tx_done,
109462306a36Sopenharmony_ci			       IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND,
109562306a36Sopenharmony_ci			       drv->name, drv);
109662306a36Sopenharmony_ci	if (ret)
109762306a36Sopenharmony_ci		return ret;
109862306a36Sopenharmony_ci
109962306a36Sopenharmony_ci	/*
110062306a36Sopenharmony_ci	 * CPU PM/genpd notification are not required for controllers that support
110162306a36Sopenharmony_ci	 * 'HW solver' mode where they can be in autonomous mode executing low
110262306a36Sopenharmony_ci	 * power mode to power down.
110362306a36Sopenharmony_ci	 */
110462306a36Sopenharmony_ci	solver_config = readl_relaxed(drv->base + drv->regs[DRV_SOLVER_CONFIG]);
110562306a36Sopenharmony_ci	solver_config &= DRV_HW_SOLVER_MASK << DRV_HW_SOLVER_SHIFT;
110662306a36Sopenharmony_ci	solver_config = solver_config >> DRV_HW_SOLVER_SHIFT;
110762306a36Sopenharmony_ci	if (!solver_config) {
110862306a36Sopenharmony_ci		if (pdev->dev.pm_domain) {
110962306a36Sopenharmony_ci			ret = rpmh_rsc_pd_attach(drv, &pdev->dev);
111062306a36Sopenharmony_ci			if (ret)
111162306a36Sopenharmony_ci				return ret;
111262306a36Sopenharmony_ci		} else {
111362306a36Sopenharmony_ci			drv->rsc_pm.notifier_call = rpmh_rsc_cpu_pm_callback;
111462306a36Sopenharmony_ci			cpu_pm_register_notifier(&drv->rsc_pm);
111562306a36Sopenharmony_ci		}
111662306a36Sopenharmony_ci	}
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci	/* Enable the active TCS to send requests immediately */
111962306a36Sopenharmony_ci	writel_relaxed(drv->tcs[ACTIVE_TCS].mask,
112062306a36Sopenharmony_ci		       drv->tcs_base + drv->regs[RSC_DRV_IRQ_ENABLE]);
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_ci	spin_lock_init(&drv->client.cache_lock);
112362306a36Sopenharmony_ci	INIT_LIST_HEAD(&drv->client.cache);
112462306a36Sopenharmony_ci	INIT_LIST_HEAD(&drv->client.batch_cache);
112562306a36Sopenharmony_ci
112662306a36Sopenharmony_ci	dev_set_drvdata(&pdev->dev, drv);
112762306a36Sopenharmony_ci	drv->dev = &pdev->dev;
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_ci	ret = devm_of_platform_populate(&pdev->dev);
113062306a36Sopenharmony_ci	if (ret && pdev->dev.pm_domain) {
113162306a36Sopenharmony_ci		dev_pm_genpd_remove_notifier(&pdev->dev);
113262306a36Sopenharmony_ci		pm_runtime_disable(&pdev->dev);
113362306a36Sopenharmony_ci	}
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_ci	return ret;
113662306a36Sopenharmony_ci}
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_cistatic const struct of_device_id rpmh_drv_match[] = {
113962306a36Sopenharmony_ci	{ .compatible = "qcom,rpmh-rsc", },
114062306a36Sopenharmony_ci	{ }
114162306a36Sopenharmony_ci};
114262306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, rpmh_drv_match);
114362306a36Sopenharmony_ci
114462306a36Sopenharmony_cistatic struct platform_driver rpmh_driver = {
114562306a36Sopenharmony_ci	.probe = rpmh_rsc_probe,
114662306a36Sopenharmony_ci	.driver = {
114762306a36Sopenharmony_ci		  .name = "rpmh",
114862306a36Sopenharmony_ci		  .of_match_table = rpmh_drv_match,
114962306a36Sopenharmony_ci		  .suppress_bind_attrs = true,
115062306a36Sopenharmony_ci	},
115162306a36Sopenharmony_ci};
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_cistatic int __init rpmh_driver_init(void)
115462306a36Sopenharmony_ci{
115562306a36Sopenharmony_ci	return platform_driver_register(&rpmh_driver);
115662306a36Sopenharmony_ci}
115762306a36Sopenharmony_ciarch_initcall(rpmh_driver_init);
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ciMODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Driver");
116062306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
1161