1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2017, The Linux Foundation
4  */
5 
6 #include <linux/slab.h>
7 #include <linux/pm_runtime.h>
8 #include "slimbus.h"
9 
10 /**
11  * slim_msg_response() - Deliver Message response received from a device to the
12  *			framework.
13  *
14  * @ctrl: Controller handle
15  * @reply: Reply received from the device
16  * @len: Length of the reply
17  * @tid: Transaction ID received with which framework can associate reply.
18  *
19  * Called by controller to inform framework about the response received.
20  * This helps in making the API asynchronous, and controller-driver doesn't need
21  * to manage 1 more table other than the one managed by framework mapping TID
22  * with buffers
23  */
slim_msg_response(struct slim_controller * ctrl,u8 * reply,u8 tid,u8 len)24 void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
25 {
26 	struct slim_msg_txn *txn;
27 	struct slim_val_inf *msg;
28 	unsigned long flags;
29 
30 	spin_lock_irqsave(&ctrl->txn_lock, flags);
31 	txn = idr_find(&ctrl->tid_idr, tid);
32 	spin_unlock_irqrestore(&ctrl->txn_lock, flags);
33 
34 	if (txn == NULL)
35 		return;
36 
37 	msg = txn->msg;
38 	if (msg == NULL || msg->rbuf == NULL) {
39 		dev_err(ctrl->dev, "Got response to invalid TID:%d, len:%d\n",
40 				tid, len);
41 		return;
42 	}
43 
44 	slim_free_txn_tid(ctrl, txn);
45 	memcpy(msg->rbuf, reply, len);
46 	if (txn->comp)
47 		complete(txn->comp);
48 
49 	/* Remove runtime-pm vote now that response was received for TID txn */
50 	pm_runtime_mark_last_busy(ctrl->dev);
51 	pm_runtime_put_autosuspend(ctrl->dev);
52 }
53 EXPORT_SYMBOL_GPL(slim_msg_response);
54 
55 /**
56  * slim_alloc_txn_tid() - Allocate a tid to txn
57  *
58  * @ctrl: Controller handle
59  * @txn: transaction to be allocated with tid.
60  *
61  * Return: zero on success with valid txn->tid and error code on failures.
62  */
slim_alloc_txn_tid(struct slim_controller * ctrl,struct slim_msg_txn * txn)63 int slim_alloc_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn)
64 {
65 	unsigned long flags;
66 	int ret = 0;
67 
68 	spin_lock_irqsave(&ctrl->txn_lock, flags);
69 	ret = idr_alloc_cyclic(&ctrl->tid_idr, txn, 1,
70 				SLIM_MAX_TIDS, GFP_ATOMIC);
71 	if (ret < 0) {
72 		spin_unlock_irqrestore(&ctrl->txn_lock, flags);
73 		return ret;
74 	}
75 	txn->tid = ret;
76 	spin_unlock_irqrestore(&ctrl->txn_lock, flags);
77 	return 0;
78 }
79 EXPORT_SYMBOL_GPL(slim_alloc_txn_tid);
80 
81 /**
82  * slim_free_txn_tid() - Free tid of txn
83  *
84  * @ctrl: Controller handle
85  * @txn: transaction whose tid should be freed
86  */
slim_free_txn_tid(struct slim_controller * ctrl,struct slim_msg_txn * txn)87 void slim_free_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn)
88 {
89 	unsigned long flags;
90 
91 	spin_lock_irqsave(&ctrl->txn_lock, flags);
92 	idr_remove(&ctrl->tid_idr, txn->tid);
93 	spin_unlock_irqrestore(&ctrl->txn_lock, flags);
94 }
95 EXPORT_SYMBOL_GPL(slim_free_txn_tid);
96 
97 /**
98  * slim_do_transfer() - Process a SLIMbus-messaging transaction
99  *
100  * @ctrl: Controller handle
101  * @txn: Transaction to be sent over SLIMbus
102  *
103  * Called by controller to transmit messaging transactions not dealing with
104  * Interface/Value elements. (e.g. transmitting a message to assign logical
105  * address to a slave device
106  *
107  * Return: -ETIMEDOUT: If transmission of this message timed out
108  *	(e.g. due to bus lines not being clocked or driven by controller)
109  */
slim_do_transfer(struct slim_controller * ctrl,struct slim_msg_txn * txn)110 int slim_do_transfer(struct slim_controller *ctrl, struct slim_msg_txn *txn)
111 {
112 	DECLARE_COMPLETION_ONSTACK(done);
113 	bool need_tid = false, clk_pause_msg = false;
114 	int ret;
115 	unsigned long time_left;
116 
117 	/*
118 	 * do not vote for runtime-PM if the transactions are part of clock
119 	 * pause sequence
120 	 */
121 	if (ctrl->sched.clk_state == SLIM_CLK_ENTERING_PAUSE &&
122 		(txn->mt == SLIM_MSG_MT_CORE &&
123 		 txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
124 		 txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
125 		clk_pause_msg = true;
126 
127 	if (!clk_pause_msg) {
128 		ret = pm_runtime_get_sync(ctrl->dev);
129 		if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
130 			dev_err(ctrl->dev, "ctrl wrong state:%d, ret:%d\n",
131 				ctrl->sched.clk_state, ret);
132 			goto slim_xfer_err;
133 		}
134 	}
135 	/* Initialize tid to invalid value */
136 	txn->tid = 0;
137 	need_tid = slim_tid_txn(txn->mt, txn->mc);
138 
139 	if (need_tid) {
140 		ret = slim_alloc_txn_tid(ctrl, txn);
141 		if (ret)
142 			return ret;
143 
144 		if (!txn->msg->comp)
145 			txn->comp = &done;
146 		else
147 			txn->comp = txn->comp;
148 	}
149 
150 	ret = ctrl->xfer_msg(ctrl, txn);
151 
152 	if (!ret && need_tid && !txn->msg->comp) {
153 		unsigned long ms = txn->rl + HZ;
154 
155 		time_left = wait_for_completion_timeout(txn->comp,
156 							msecs_to_jiffies(ms));
157 		if (!time_left) {
158 			ret = -ETIMEDOUT;
159 			slim_free_txn_tid(ctrl, txn);
160 		}
161 	}
162 
163 	if (ret)
164 		dev_err(ctrl->dev, "Tx:MT:0x%x, MC:0x%x, LA:0x%x failed:%d\n",
165 			txn->mt, txn->mc, txn->la, ret);
166 
167 slim_xfer_err:
168 	if (!clk_pause_msg && (txn->tid == 0  || ret == -ETIMEDOUT)) {
169 		/*
170 		 * remove runtime-pm vote if this was TX only, or
171 		 * if there was error during this transaction
172 		 */
173 		pm_runtime_mark_last_busy(ctrl->dev);
174 		pm_runtime_put_autosuspend(ctrl->dev);
175 	}
176 	return ret;
177 }
178 EXPORT_SYMBOL_GPL(slim_do_transfer);
179 
slim_val_inf_sanity(struct slim_controller * ctrl,struct slim_val_inf * msg,u8 mc)180 static int slim_val_inf_sanity(struct slim_controller *ctrl,
181 			       struct slim_val_inf *msg, u8 mc)
182 {
183 	if (!msg || msg->num_bytes > 16 ||
184 	    (msg->start_offset + msg->num_bytes) > 0xC00)
185 		goto reterr;
186 	switch (mc) {
187 	case SLIM_MSG_MC_REQUEST_VALUE:
188 	case SLIM_MSG_MC_REQUEST_INFORMATION:
189 		if (msg->rbuf != NULL)
190 			return 0;
191 		break;
192 
193 	case SLIM_MSG_MC_CHANGE_VALUE:
194 	case SLIM_MSG_MC_CLEAR_INFORMATION:
195 		if (msg->wbuf != NULL)
196 			return 0;
197 		break;
198 
199 	case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
200 	case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
201 		if (msg->rbuf != NULL && msg->wbuf != NULL)
202 			return 0;
203 		break;
204 	}
205 reterr:
206 	if (msg)
207 		dev_err(ctrl->dev, "Sanity check failed:msg:offset:0x%x, mc:%d\n",
208 			msg->start_offset, mc);
209 	return -EINVAL;
210 }
211 
slim_slicesize(int code)212 static u16 slim_slicesize(int code)
213 {
214 	static const u8 sizetocode[16] = {
215 		0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7
216 	};
217 
218 	code = clamp(code, 1, (int)ARRAY_SIZE(sizetocode));
219 
220 	return sizetocode[code - 1];
221 }
222 
223 /**
224  * slim_xfer_msg() - Transfer a value info message on slim device
225  *
226  * @sbdev: slim device to which this msg has to be transfered
227  * @msg: value info message pointer
228  * @mc: message code of the message
229  *
230  * Called by drivers which want to transfer a vlaue or info elements.
231  *
232  * Return: -ETIMEDOUT: If transmission of this message timed out
233  */
slim_xfer_msg(struct slim_device * sbdev,struct slim_val_inf * msg,u8 mc)234 int slim_xfer_msg(struct slim_device *sbdev, struct slim_val_inf *msg,
235 		  u8 mc)
236 {
237 	DEFINE_SLIM_LDEST_TXN(txn_stack, mc, 6, sbdev->laddr, msg);
238 	struct slim_msg_txn *txn = &txn_stack;
239 	struct slim_controller *ctrl = sbdev->ctrl;
240 	int ret;
241 	u16 sl;
242 
243 	if (!ctrl)
244 		return -EINVAL;
245 
246 	ret = slim_val_inf_sanity(ctrl, msg, mc);
247 	if (ret)
248 		return ret;
249 
250 	sl = slim_slicesize(msg->num_bytes);
251 
252 	dev_dbg(ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
253 		msg->start_offset, msg->num_bytes, mc, sl);
254 
255 	txn->ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
256 
257 	switch (mc) {
258 	case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
259 	case SLIM_MSG_MC_CHANGE_VALUE:
260 	case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
261 	case SLIM_MSG_MC_CLEAR_INFORMATION:
262 		txn->rl += msg->num_bytes;
263 		break;
264 	default:
265 		break;
266 	}
267 
268 	if (slim_tid_txn(txn->mt, txn->mc))
269 		txn->rl++;
270 
271 	return slim_do_transfer(ctrl, txn);
272 }
273 EXPORT_SYMBOL_GPL(slim_xfer_msg);
274 
slim_fill_msg(struct slim_val_inf * msg,u32 addr,size_t count,u8 * rbuf,u8 * wbuf)275 static void slim_fill_msg(struct slim_val_inf *msg, u32 addr,
276 			 size_t count, u8 *rbuf, u8 *wbuf)
277 {
278 	msg->start_offset = addr;
279 	msg->num_bytes = count;
280 	msg->rbuf = rbuf;
281 	msg->wbuf = wbuf;
282 	msg->comp = NULL;
283 }
284 
285 /**
286  * slim_read() - Read SLIMbus value element
287  *
288  * @sdev: client handle.
289  * @addr:  address of value element to read.
290  * @count: number of bytes to read. Maximum bytes allowed are 16.
291  * @val: will return what the value element value was
292  *
293  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
294  * this message timed out (e.g. due to bus lines not being clocked
295  * or driven by controller)
296  */
slim_read(struct slim_device * sdev,u32 addr,size_t count,u8 * val)297 int slim_read(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
298 {
299 	struct slim_val_inf msg;
300 
301 	slim_fill_msg(&msg, addr, count, val, NULL);
302 
303 	return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_REQUEST_VALUE);
304 }
305 EXPORT_SYMBOL_GPL(slim_read);
306 
307 /**
308  * slim_readb() - Read byte from SLIMbus value element
309  *
310  * @sdev: client handle.
311  * @addr:  address in the value element to read.
312  *
313  * Return: byte value of value element.
314  */
slim_readb(struct slim_device * sdev,u32 addr)315 int slim_readb(struct slim_device *sdev, u32 addr)
316 {
317 	int ret;
318 	u8 buf;
319 
320 	ret = slim_read(sdev, addr, 1, &buf);
321 	if (ret < 0)
322 		return ret;
323 	else
324 		return buf;
325 }
326 EXPORT_SYMBOL_GPL(slim_readb);
327 
328 /**
329  * slim_write() - Write SLIMbus value element
330  *
331  * @sdev: client handle.
332  * @addr:  address in the value element to write.
333  * @count: number of bytes to write. Maximum bytes allowed are 16.
334  * @val: value to write to value element
335  *
336  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
337  * this message timed out (e.g. due to bus lines not being clocked
338  * or driven by controller)
339  */
slim_write(struct slim_device * sdev,u32 addr,size_t count,u8 * val)340 int slim_write(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
341 {
342 	struct slim_val_inf msg;
343 
344 	slim_fill_msg(&msg, addr, count,  NULL, val);
345 
346 	return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_CHANGE_VALUE);
347 }
348 EXPORT_SYMBOL_GPL(slim_write);
349 
350 /**
351  * slim_writeb() - Write byte to SLIMbus value element
352  *
353  * @sdev: client handle.
354  * @addr:  address of value element to write.
355  * @value: value to write to value element
356  *
357  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
358  * this message timed out (e.g. due to bus lines not being clocked
359  * or driven by controller)
360  *
361  */
slim_writeb(struct slim_device * sdev,u32 addr,u8 value)362 int slim_writeb(struct slim_device *sdev, u32 addr, u8 value)
363 {
364 	return slim_write(sdev, addr, 1, &value);
365 }
366 EXPORT_SYMBOL_GPL(slim_writeb);
367