xref: /linux/drivers/soc/qcom/rpmh-internal.h (revision b2370c0f8960dbe9bc0b5169944c58239964a738)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
4  */
5 
6 
7 #ifndef __RPM_INTERNAL_H__
8 #define __RPM_INTERNAL_H__
9 
10 #include <linux/bitmap.h>
11 #include <linux/wait.h>
12 #include <soc/qcom/tcs.h>
13 
14 #define TCS_TYPE_NR			4
15 #define MAX_CMDS_PER_TCS		16
16 #define MAX_TCS_PER_TYPE		3
17 #define MAX_TCS_NR			(MAX_TCS_PER_TYPE * TCS_TYPE_NR)
18 #define MAX_TCS_SLOTS			(MAX_CMDS_PER_TCS * MAX_TCS_PER_TYPE)
19 
20 struct rsc_drv;
21 
22 /**
23  * struct tcs_group - group of Trigger Command Sets (TCS) to send state requests
24  * to the controller
25  *
26  * @drv:       The controller.
27  * @type:      Type of the TCS in this group - active, sleep, wake.
28  * @mask:      Mask of the TCSes relative to all the TCSes in the RSC.
29  * @offset:    Start of the TCS group relative to the TCSes in the RSC.
30  * @num_tcs:   Number of TCSes in this type.
31  * @ncpt:      Number of commands in each TCS.
32  * @req:       Requests that are sent from the TCS; only used for ACTIVE_ONLY
33  *             transfers (could be on a wake/sleep TCS if we are borrowing for
34  *             an ACTIVE_ONLY transfer).
35  *             Start: grab drv->lock, set req, set tcs_in_use, drop drv->lock,
36  *                    trigger
37  *             End: get irq, access req,
38  *                  grab drv->lock, clear tcs_in_use, drop drv->lock
39  * @slots:     Indicates which of @cmd_addr are occupied; only used for
40  *             SLEEP / WAKE TCSs.  Things are tightly packed in the
41  *             case that (ncpt < MAX_CMDS_PER_TCS).  That is if ncpt = 2 and
42  *             MAX_CMDS_PER_TCS = 16 then bit[2] = the first bit in 2nd TCS.
43  */
44 struct tcs_group {
45 	struct rsc_drv *drv;
46 	int type;
47 	u32 mask;
48 	u32 offset;
49 	int num_tcs;
50 	int ncpt;
51 	const struct tcs_request *req[MAX_TCS_PER_TYPE];
52 	DECLARE_BITMAP(slots, MAX_TCS_SLOTS);
53 };
54 
55 /**
56  * struct rpmh_request - the message to be sent to rpmh-rsc
57  *
58  * @msg: the request
59  * @cmd: the payload that will be part of the @msg
60  * @completion: triggered when request is done
61  * @dev: the device making the request
62  * @needs_free: check to free dynamically allocated request object
63  */
64 struct rpmh_request {
65 	struct tcs_request msg;
66 	struct tcs_cmd cmd[MAX_RPMH_PAYLOAD];
67 	struct completion *completion;
68 	const struct device *dev;
69 	bool needs_free;
70 };
71 
72 /**
73  * struct rpmh_ctrlr - our representation of the controller
74  *
75  * @cache: the list of cached requests
76  * @cache_lock: synchronize access to the cache data
77  * @dirty: was the cache updated since flush
78  * @batch_cache: Cache sleep and wake requests sent as batch
79  */
80 struct rpmh_ctrlr {
81 	struct list_head cache;
82 	spinlock_t cache_lock;
83 	bool dirty;
84 	struct list_head batch_cache;
85 };
86 
87 /**
88  * struct rsc_ver - Hardware version of the RSC controller.
89  *
90  * @major: Major version number.
91  * @minor: Minor version number.
92  */
93 struct rsc_ver {
94 	u32 major;
95 	u32 minor;
96 };
97 
98 /**
99  * struct rsc_drv - the Direct Resource Voter (DRV) of the
100  * Resource State Coordinator controller (RSC)
101  *
102  * @name:               Controller identifier.
103  * @base:               Start address of the DRV registers in this controller.
104  * @tcs_base:           Start address of the TCS registers in this controller.
105  * @id:                 Instance id in the controller (Direct Resource Voter).
106  * @num_tcs:            Number of TCSes in this DRV.
107  * @rsc_pm:             CPU PM notifier for controller.
108  *                      Used when solver mode is not present.
109  * @cpus_in_pm:         Number of CPUs not in idle power collapse.
110  *                      Used when solver mode and "power-domains" is not present.
111  * @genpd_nb:           PM Domain notifier for cluster genpd notifications.
112  * @tcs:                TCS groups.
113  * @tcs_in_use:         S/W state of the TCS; only set for ACTIVE_ONLY
114  *                      transfers, but might show a sleep/wake TCS in use if
115  *                      it was borrowed for an active_only transfer.  You
116  *                      must hold the lock in this struct (AKA drv->lock) in
117  *                      order to update this.
118  * @lock:               Synchronize state of the controller.  If RPMH's cache
119  *                      lock will also be held, the order is: drv->lock then
120  *                      cache_lock.
121  * @tcs_wait:           Wait queue used to wait for @tcs_in_use to free up a
122  *                      slot
123  * @client:             Handle to the DRV's client.
124  * @dev:                RSC device.
125  * @ver:                HW version of the RSC controller.
126  * @regs:               Pointer to the register offset table for this version.
127  */
128 struct rsc_drv {
129 	const char *name;
130 	void __iomem *base;
131 	void __iomem *tcs_base;
132 	int id;
133 	int num_tcs;
134 	struct notifier_block rsc_pm;
135 	struct notifier_block genpd_nb;
136 	atomic_t cpus_in_pm;
137 	struct tcs_group tcs[TCS_TYPE_NR];
138 	DECLARE_BITMAP(tcs_in_use, MAX_TCS_NR);
139 	spinlock_t lock;
140 	wait_queue_head_t tcs_wait;
141 	struct rpmh_ctrlr client;
142 	struct device *dev;
143 	struct rsc_ver ver;
144 	u32 *regs;
145 };
146 
147 int rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg);
148 int rpmh_rsc_write_ctrl_data(struct rsc_drv *drv,
149 			     const struct tcs_request *msg);
150 void rpmh_rsc_invalidate(struct rsc_drv *drv);
151 void rpmh_rsc_write_next_wakeup(struct rsc_drv *drv);
152 
153 void rpmh_tx_done(const struct tcs_request *msg);
154 int rpmh_flush(struct rpmh_ctrlr *ctrlr);
155 
156 #endif /* __RPM_INTERNAL_H__ */
157