xref: /linux/drivers/soc/qcom/rpmh-internal.h (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1658628e7SLina Iyer /* SPDX-License-Identifier: GPL-2.0 */
2658628e7SLina Iyer /*
3658628e7SLina Iyer  * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
4658628e7SLina Iyer  */
5658628e7SLina Iyer 
6658628e7SLina Iyer 
7658628e7SLina Iyer #ifndef __RPM_INTERNAL_H__
8658628e7SLina Iyer #define __RPM_INTERNAL_H__
9658628e7SLina Iyer 
10658628e7SLina Iyer #include <linux/bitmap.h>
112bc20f3cSStephen Boyd #include <linux/wait.h>
12658628e7SLina Iyer #include <soc/qcom/tcs.h>
13658628e7SLina Iyer 
14658628e7SLina Iyer #define TCS_TYPE_NR			4
15658628e7SLina Iyer #define MAX_CMDS_PER_TCS		16
16658628e7SLina Iyer #define MAX_TCS_PER_TYPE		3
17658628e7SLina Iyer #define MAX_TCS_NR			(MAX_TCS_PER_TYPE * TCS_TYPE_NR)
18fa460e45SLina Iyer #define MAX_TCS_SLOTS			(MAX_CMDS_PER_TCS * MAX_TCS_PER_TYPE)
19658628e7SLina Iyer 
20658628e7SLina Iyer struct rsc_drv;
21658628e7SLina Iyer 
22658628e7SLina Iyer /**
23658628e7SLina Iyer  * struct tcs_group: group of Trigger Command Sets (TCS) to send state requests
24658628e7SLina Iyer  * to the controller
25658628e7SLina Iyer  *
26e40b0c16SDouglas Anderson  * @drv:       The controller.
27e40b0c16SDouglas Anderson  * @type:      Type of the TCS in this group - active, sleep, wake.
28e40b0c16SDouglas Anderson  * @mask:      Mask of the TCSes relative to all the TCSes in the RSC.
29e40b0c16SDouglas Anderson  * @offset:    Start of the TCS group relative to the TCSes in the RSC.
30e40b0c16SDouglas Anderson  * @num_tcs:   Number of TCSes in this type.
31e40b0c16SDouglas Anderson  * @ncpt:      Number of commands in each TCS.
32e40b0c16SDouglas Anderson  * @req:       Requests that are sent from the TCS; only used for ACTIVE_ONLY
33e40b0c16SDouglas Anderson  *             transfers (could be on a wake/sleep TCS if we are borrowing for
34e40b0c16SDouglas Anderson  *             an ACTIVE_ONLY transfer).
35e40b0c16SDouglas Anderson  *             Start: grab drv->lock, set req, set tcs_in_use, drop drv->lock,
36e40b0c16SDouglas Anderson  *                    trigger
37e40b0c16SDouglas Anderson  *             End: get irq, access req,
38e40b0c16SDouglas Anderson  *                  grab drv->lock, clear tcs_in_use, drop drv->lock
39e40b0c16SDouglas Anderson  * @slots:     Indicates which of @cmd_addr are occupied; only used for
40e40b0c16SDouglas Anderson  *             SLEEP / WAKE TCSs.  Things are tightly packed in the
41e40b0c16SDouglas Anderson  *             case that (ncpt < MAX_CMDS_PER_TCS).  That is if ncpt = 2 and
42e40b0c16SDouglas Anderson  *             MAX_CMDS_PER_TCS = 16 then bit[2] = the first bit in 2nd TCS.
43658628e7SLina Iyer  */
44658628e7SLina Iyer struct tcs_group {
45658628e7SLina Iyer 	struct rsc_drv *drv;
46658628e7SLina Iyer 	int type;
47658628e7SLina Iyer 	u32 mask;
48658628e7SLina Iyer 	u32 offset;
49658628e7SLina Iyer 	int num_tcs;
50658628e7SLina Iyer 	int ncpt;
51658628e7SLina Iyer 	const struct tcs_request *req[MAX_TCS_PER_TYPE];
52fa460e45SLina Iyer 	DECLARE_BITMAP(slots, MAX_TCS_SLOTS);
53658628e7SLina Iyer };
54658628e7SLina Iyer 
55658628e7SLina Iyer /**
56c1038456SLina Iyer  * struct rpmh_request: the message to be sent to rpmh-rsc
57c1038456SLina Iyer  *
58c1038456SLina Iyer  * @msg: the request
59c1038456SLina Iyer  * @cmd: the payload that will be part of the @msg
60c1038456SLina Iyer  * @completion: triggered when request is done
61c1038456SLina Iyer  * @dev: the device making the request
62564b5e24SLina Iyer  * @needs_free: check to free dynamically allocated request object
63c1038456SLina Iyer  */
64c1038456SLina Iyer struct rpmh_request {
65c1038456SLina Iyer 	struct tcs_request msg;
66c1038456SLina Iyer 	struct tcs_cmd cmd[MAX_RPMH_PAYLOAD];
67c1038456SLina Iyer 	struct completion *completion;
68c1038456SLina Iyer 	const struct device *dev;
69564b5e24SLina Iyer 	bool needs_free;
70c1038456SLina Iyer };
71c1038456SLina Iyer 
72c1038456SLina Iyer /**
73c1038456SLina Iyer  * struct rpmh_ctrlr: our representation of the controller
74c1038456SLina Iyer  *
75600513dfSLina Iyer  * @cache: the list of cached requests
76600513dfSLina Iyer  * @cache_lock: synchronize access to the cache data
77600513dfSLina Iyer  * @dirty: was the cache updated since flush
78c8790cb6SLina Iyer  * @batch_cache: Cache sleep and wake requests sent as batch
79c1038456SLina Iyer  */
80c1038456SLina Iyer struct rpmh_ctrlr {
81600513dfSLina Iyer 	struct list_head cache;
82600513dfSLina Iyer 	spinlock_t cache_lock;
83600513dfSLina Iyer 	bool dirty;
84c8790cb6SLina Iyer 	struct list_head batch_cache;
85c1038456SLina Iyer };
86c1038456SLina Iyer 
8740482e4fSAbel Vesa struct rsc_ver {
8840482e4fSAbel Vesa 	u32 major;
8940482e4fSAbel Vesa 	u32 minor;
9040482e4fSAbel Vesa };
9140482e4fSAbel Vesa 
92c1038456SLina Iyer /**
93658628e7SLina Iyer  * struct rsc_drv: the Direct Resource Voter (DRV) of the
94658628e7SLina Iyer  * Resource State Coordinator controller (RSC)
95658628e7SLina Iyer  *
96e40b0c16SDouglas Anderson  * @name:               Controller identifier.
97ab33c8f3SMaulik Shah  * @base:               Start address of the DRV registers in this controller.
98e40b0c16SDouglas Anderson  * @tcs_base:           Start address of the TCS registers in this controller.
99e40b0c16SDouglas Anderson  * @id:                 Instance id in the controller (Direct Resource Voter).
100e40b0c16SDouglas Anderson  * @num_tcs:            Number of TCSes in this DRV.
101e40b0c16SDouglas Anderson  * @rsc_pm:             CPU PM notifier for controller.
102e40b0c16SDouglas Anderson  *                      Used when solver mode is not present.
103d2a8cfc6SDouglas Anderson  * @cpus_in_pm:         Number of CPUs not in idle power collapse.
10425092e61SLina Iyer  *                      Used when solver mode and "power-domains" is not present.
10525092e61SLina Iyer  * @genpd_nb:           PM Domain notifier for cluster genpd notifications.
106e40b0c16SDouglas Anderson  * @tcs:                TCS groups.
107e40b0c16SDouglas Anderson  * @tcs_in_use:         S/W state of the TCS; only set for ACTIVE_ONLY
108e40b0c16SDouglas Anderson  *                      transfers, but might show a sleep/wake TCS in use if
109e40b0c16SDouglas Anderson  *                      it was borrowed for an active_only transfer.  You
110555701a4SDouglas Anderson  *                      must hold the lock in this struct (AKA drv->lock) in
111555701a4SDouglas Anderson  *                      order to update this.
112d2a8cfc6SDouglas Anderson  * @lock:               Synchronize state of the controller.  If RPMH's cache
113d2a8cfc6SDouglas Anderson  *                      lock will also be held, the order is: drv->lock then
114d2a8cfc6SDouglas Anderson  *                      cache_lock.
1152bc20f3cSStephen Boyd  * @tcs_wait:           Wait queue used to wait for @tcs_in_use to free up a
1162bc20f3cSStephen Boyd  *                      slot
117985427f9SMaulik Shah  * @client:             Handle to the DRV's client.
118cccbe3e5SMaulik Shah  * @dev:                RSC device.
119658628e7SLina Iyer  */
120658628e7SLina Iyer struct rsc_drv {
121658628e7SLina Iyer 	const char *name;
122ab33c8f3SMaulik Shah 	void __iomem *base;
123658628e7SLina Iyer 	void __iomem *tcs_base;
124658628e7SLina Iyer 	int id;
125658628e7SLina Iyer 	int num_tcs;
126985427f9SMaulik Shah 	struct notifier_block rsc_pm;
12725092e61SLina Iyer 	struct notifier_block genpd_nb;
128d2a8cfc6SDouglas Anderson 	atomic_t cpus_in_pm;
129658628e7SLina Iyer 	struct tcs_group tcs[TCS_TYPE_NR];
130658628e7SLina Iyer 	DECLARE_BITMAP(tcs_in_use, MAX_TCS_NR);
131658628e7SLina Iyer 	spinlock_t lock;
1322bc20f3cSStephen Boyd 	wait_queue_head_t tcs_wait;
133c1038456SLina Iyer 	struct rpmh_ctrlr client;
134cccbe3e5SMaulik Shah 	struct device *dev;
13540482e4fSAbel Vesa 	struct rsc_ver ver;
13640482e4fSAbel Vesa 	u32 *regs;
137658628e7SLina Iyer };
138658628e7SLina Iyer 
139658628e7SLina Iyer int rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg);
140fa460e45SLina Iyer int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv,
141fa460e45SLina Iyer 			     const struct tcs_request *msg);
142881808d0SDouglas Anderson void rpmh_rsc_invalidate(struct rsc_drv *drv);
143cccbe3e5SMaulik Shah void rpmh_rsc_write_next_wakeup(struct rsc_drv *drv);
144658628e7SLina Iyer 
145*323dc2dcSAbel Vesa void rpmh_tx_done(const struct tcs_request *msg);
146d5e20507SMaulik Shah int rpmh_flush(struct rpmh_ctrlr *ctrlr);
147c1038456SLina Iyer 
148658628e7SLina Iyer #endif /* __RPM_INTERNAL_H__ */
149