xref: /openbmc/linux/drivers/slimbus/messaging.c (revision ab9b3de70199f7479e251c30128b30b5559c97b7)
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  */
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 	if (txn == NULL) {
33 		spin_unlock_irqrestore(&ctrl->txn_lock, flags);
34 		return;
35 	}
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 	idr_remove(&ctrl->tid_idr, tid);
45 	spin_unlock_irqrestore(&ctrl->txn_lock, flags);
46 
47 	memcpy(msg->rbuf, reply, len);
48 	if (txn->comp)
49 		complete(txn->comp);
50 
51 	/* Remove runtime-pm vote now that response was received for TID txn */
52 	pm_runtime_mark_last_busy(ctrl->dev);
53 	pm_runtime_put_autosuspend(ctrl->dev);
54 }
55 EXPORT_SYMBOL_GPL(slim_msg_response);
56 
57 /**
58  * slim_do_transfer() - Process a SLIMbus-messaging transaction
59  *
60  * @ctrl: Controller handle
61  * @txn: Transaction to be sent over SLIMbus
62  *
63  * Called by controller to transmit messaging transactions not dealing with
64  * Interface/Value elements. (e.g. transmittting a message to assign logical
65  * address to a slave device
66  *
67  * Return: -ETIMEDOUT: If transmission of this message timed out
68  *	(e.g. due to bus lines not being clocked or driven by controller)
69  */
70 int slim_do_transfer(struct slim_controller *ctrl, struct slim_msg_txn *txn)
71 {
72 	DECLARE_COMPLETION_ONSTACK(done);
73 	bool need_tid = false, clk_pause_msg = false;
74 	unsigned long flags;
75 	int ret, tid, timeout;
76 
77 	/*
78 	 * do not vote for runtime-PM if the transactions are part of clock
79 	 * pause sequence
80 	 */
81 	if (ctrl->sched.clk_state == SLIM_CLK_ENTERING_PAUSE &&
82 		(txn->mt == SLIM_MSG_MT_CORE &&
83 		 txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
84 		 txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
85 		clk_pause_msg = true;
86 
87 	if (!clk_pause_msg) {
88 		ret = pm_runtime_get_sync(ctrl->dev);
89 		if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
90 			dev_err(ctrl->dev, "ctrl wrong state:%d, ret:%d\n",
91 				ctrl->sched.clk_state, ret);
92 			goto slim_xfer_err;
93 		}
94 	}
95 
96 	need_tid = slim_tid_txn(txn->mt, txn->mc);
97 
98 	if (need_tid) {
99 		spin_lock_irqsave(&ctrl->txn_lock, flags);
100 		tid = idr_alloc(&ctrl->tid_idr, txn, 0,
101 				SLIM_MAX_TIDS, GFP_ATOMIC);
102 		txn->tid = tid;
103 
104 		if (!txn->msg->comp)
105 			txn->comp = &done;
106 		else
107 			txn->comp = txn->comp;
108 
109 		spin_unlock_irqrestore(&ctrl->txn_lock, flags);
110 
111 		if (tid < 0)
112 			return tid;
113 	}
114 
115 	ret = ctrl->xfer_msg(ctrl, txn);
116 
117 	if (ret && need_tid && !txn->msg->comp) {
118 		unsigned long ms = txn->rl + HZ;
119 
120 		timeout = wait_for_completion_timeout(txn->comp,
121 						      msecs_to_jiffies(ms));
122 		if (!timeout) {
123 			ret = -ETIMEDOUT;
124 			spin_lock_irqsave(&ctrl->txn_lock, flags);
125 			idr_remove(&ctrl->tid_idr, tid);
126 			spin_unlock_irqrestore(&ctrl->txn_lock, flags);
127 		}
128 	}
129 
130 	if (ret)
131 		dev_err(ctrl->dev, "Tx:MT:0x%x, MC:0x%x, LA:0x%x failed:%d\n",
132 			txn->mt, txn->mc, txn->la, ret);
133 
134 slim_xfer_err:
135 	if (!clk_pause_msg && (!need_tid  || ret == -ETIMEDOUT)) {
136 		/*
137 		 * remove runtime-pm vote if this was TX only, or
138 		 * if there was error during this transaction
139 		 */
140 		pm_runtime_mark_last_busy(ctrl->dev);
141 		pm_runtime_mark_last_busy(ctrl->dev);
142 	}
143 	return ret;
144 }
145 EXPORT_SYMBOL_GPL(slim_do_transfer);
146 
147 static int slim_val_inf_sanity(struct slim_controller *ctrl,
148 			       struct slim_val_inf *msg, u8 mc)
149 {
150 	if (!msg || msg->num_bytes > 16 ||
151 	    (msg->start_offset + msg->num_bytes) > 0xC00)
152 		goto reterr;
153 	switch (mc) {
154 	case SLIM_MSG_MC_REQUEST_VALUE:
155 	case SLIM_MSG_MC_REQUEST_INFORMATION:
156 		if (msg->rbuf != NULL)
157 			return 0;
158 		break;
159 
160 	case SLIM_MSG_MC_CHANGE_VALUE:
161 	case SLIM_MSG_MC_CLEAR_INFORMATION:
162 		if (msg->wbuf != NULL)
163 			return 0;
164 		break;
165 
166 	case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
167 	case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
168 		if (msg->rbuf != NULL && msg->wbuf != NULL)
169 			return 0;
170 		break;
171 	}
172 reterr:
173 	if (msg)
174 		dev_err(ctrl->dev, "Sanity check failed:msg:offset:0x%x, mc:%d\n",
175 			msg->start_offset, mc);
176 	return -EINVAL;
177 }
178 
179 static u16 slim_slicesize(int code)
180 {
181 	static const u8 sizetocode[16] = {
182 		0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7
183 	};
184 
185 	clamp(code, 1, (int)ARRAY_SIZE(sizetocode));
186 
187 	return sizetocode[code - 1];
188 }
189 
190 /**
191  * slim_xfer_msg() - Transfer a value info message on slim device
192  *
193  * @sbdev: slim device to which this msg has to be transfered
194  * @msg: value info message pointer
195  * @mc: message code of the message
196  *
197  * Called by drivers which want to transfer a vlaue or info elements.
198  *
199  * Return: -ETIMEDOUT: If transmission of this message timed out
200  */
201 int slim_xfer_msg(struct slim_device *sbdev, struct slim_val_inf *msg,
202 		  u8 mc)
203 {
204 	DEFINE_SLIM_LDEST_TXN(txn_stack, mc, 6, sbdev->laddr, msg);
205 	struct slim_msg_txn *txn = &txn_stack;
206 	struct slim_controller *ctrl = sbdev->ctrl;
207 	int ret;
208 	u16 sl;
209 
210 	if (!ctrl)
211 		return -EINVAL;
212 
213 	ret = slim_val_inf_sanity(ctrl, msg, mc);
214 	if (ret)
215 		return ret;
216 
217 	sl = slim_slicesize(msg->num_bytes);
218 
219 	dev_dbg(ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
220 		msg->start_offset, msg->num_bytes, mc, sl);
221 
222 	txn->ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
223 
224 	switch (mc) {
225 	case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
226 	case SLIM_MSG_MC_CHANGE_VALUE:
227 	case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
228 	case SLIM_MSG_MC_CLEAR_INFORMATION:
229 		txn->rl += msg->num_bytes;
230 	default:
231 		break;
232 	}
233 
234 	if (slim_tid_txn(txn->mt, txn->mc))
235 		txn->rl++;
236 
237 	return slim_do_transfer(ctrl, txn);
238 }
239 EXPORT_SYMBOL_GPL(slim_xfer_msg);
240 
241 static void slim_fill_msg(struct slim_val_inf *msg, u32 addr,
242 			 size_t count, u8 *rbuf, u8 *wbuf)
243 {
244 	msg->start_offset = addr;
245 	msg->num_bytes = count;
246 	msg->rbuf = rbuf;
247 	msg->wbuf = wbuf;
248 }
249 
250 /**
251  * slim_read() - Read SLIMbus value element
252  *
253  * @sdev: client handle.
254  * @addr:  address of value element to read.
255  * @count: number of bytes to read. Maximum bytes allowed are 16.
256  * @val: will return what the value element value was
257  *
258  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
259  * this message timed out (e.g. due to bus lines not being clocked
260  * or driven by controller)
261  */
262 int slim_read(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
263 {
264 	struct slim_val_inf msg;
265 
266 	slim_fill_msg(&msg, addr, count, val, NULL);
267 
268 	return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_REQUEST_VALUE);
269 }
270 EXPORT_SYMBOL_GPL(slim_read);
271 
272 /**
273  * slim_readb() - Read byte from SLIMbus value element
274  *
275  * @sdev: client handle.
276  * @addr:  address in the value element to read.
277  *
278  * Return: byte value of value element.
279  */
280 int slim_readb(struct slim_device *sdev, u32 addr)
281 {
282 	int ret;
283 	u8 buf;
284 
285 	ret = slim_read(sdev, addr, 1, &buf);
286 	if (ret < 0)
287 		return ret;
288 	else
289 		return buf;
290 }
291 EXPORT_SYMBOL_GPL(slim_readb);
292 
293 /**
294  * slim_write() - Write SLIMbus value element
295  *
296  * @sdev: client handle.
297  * @addr:  address in the value element to write.
298  * @count: number of bytes to write. Maximum bytes allowed are 16.
299  * @val: value to write to value element
300  *
301  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
302  * this message timed out (e.g. due to bus lines not being clocked
303  * or driven by controller)
304  */
305 int slim_write(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
306 {
307 	struct slim_val_inf msg;
308 
309 	slim_fill_msg(&msg, addr, count,  val, NULL);
310 
311 	return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_CHANGE_VALUE);
312 }
313 EXPORT_SYMBOL_GPL(slim_write);
314 
315 /**
316  * slim_writeb() - Write byte to SLIMbus value element
317  *
318  * @sdev: client handle.
319  * @addr:  address of value element to write.
320  * @value: value to write to value element
321  *
322  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
323  * this message timed out (e.g. due to bus lines not being clocked
324  * or driven by controller)
325  *
326  */
327 int slim_writeb(struct slim_device *sdev, u32 addr, u8 value)
328 {
329 	return slim_write(sdev, addr, 1, &value);
330 }
331 EXPORT_SYMBOL_GPL(slim_writeb);
332