1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2017, The Linux Foundation
6 #include <linux/slab.h>
7 #include <linux/pm_runtime.h>
11 * slim_msg_response() - Deliver Message response received from a device to the
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.
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
24 void slim_msg_response(struct slim_controller
*ctrl
, u8
*reply
, u8 tid
, u8 len
)
26 struct slim_msg_txn
*txn
;
27 struct slim_val_inf
*msg
;
30 spin_lock_irqsave(&ctrl
->txn_lock
, flags
);
31 txn
= idr_find(&ctrl
->tid_idr
, tid
);
33 spin_unlock_irqrestore(&ctrl
->txn_lock
, flags
);
38 if (msg
== NULL
|| msg
->rbuf
== NULL
) {
39 dev_err(ctrl
->dev
, "Got response to invalid TID:%d, len:%d\n",
41 spin_unlock_irqrestore(&ctrl
->txn_lock
, flags
);
45 idr_remove(&ctrl
->tid_idr
, tid
);
46 spin_unlock_irqrestore(&ctrl
->txn_lock
, flags
);
48 memcpy(msg
->rbuf
, reply
, len
);
52 /* Remove runtime-pm vote now that response was received for TID txn */
53 pm_runtime_mark_last_busy(ctrl
->dev
);
54 pm_runtime_put_autosuspend(ctrl
->dev
);
56 EXPORT_SYMBOL_GPL(slim_msg_response
);
59 * slim_do_transfer() - Process a SLIMbus-messaging transaction
61 * @ctrl: Controller handle
62 * @txn: Transaction to be sent over SLIMbus
64 * Called by controller to transmit messaging transactions not dealing with
65 * Interface/Value elements. (e.g. transmittting a message to assign logical
66 * address to a slave device
68 * Return: -ETIMEDOUT: If transmission of this message timed out
69 * (e.g. due to bus lines not being clocked or driven by controller)
71 int slim_do_transfer(struct slim_controller
*ctrl
, struct slim_msg_txn
*txn
)
73 DECLARE_COMPLETION_ONSTACK(done
);
74 bool need_tid
= false, clk_pause_msg
= false;
76 int ret
, tid
, timeout
;
79 * do not vote for runtime-PM if the transactions are part of clock
82 if (ctrl
->sched
.clk_state
== SLIM_CLK_ENTERING_PAUSE
&&
83 (txn
->mt
== SLIM_MSG_MT_CORE
&&
84 txn
->mc
>= SLIM_MSG_MC_BEGIN_RECONFIGURATION
&&
85 txn
->mc
<= SLIM_MSG_MC_RECONFIGURE_NOW
))
89 ret
= pm_runtime_get_sync(ctrl
->dev
);
90 if (ctrl
->sched
.clk_state
!= SLIM_CLK_ACTIVE
) {
91 dev_err(ctrl
->dev
, "ctrl wrong state:%d, ret:%d\n",
92 ctrl
->sched
.clk_state
, ret
);
97 need_tid
= slim_tid_txn(txn
->mt
, txn
->mc
);
100 spin_lock_irqsave(&ctrl
->txn_lock
, flags
);
101 tid
= idr_alloc(&ctrl
->tid_idr
, txn
, 0,
102 SLIM_MAX_TIDS
, GFP_ATOMIC
);
108 txn
->comp
= txn
->comp
;
110 spin_unlock_irqrestore(&ctrl
->txn_lock
, flags
);
116 ret
= ctrl
->xfer_msg(ctrl
, txn
);
118 if (ret
&& need_tid
&& !txn
->msg
->comp
) {
119 unsigned long ms
= txn
->rl
+ HZ
;
121 timeout
= wait_for_completion_timeout(txn
->comp
,
122 msecs_to_jiffies(ms
));
125 spin_lock_irqsave(&ctrl
->txn_lock
, flags
);
126 idr_remove(&ctrl
->tid_idr
, tid
);
127 spin_unlock_irqrestore(&ctrl
->txn_lock
, flags
);
132 dev_err(ctrl
->dev
, "Tx:MT:0x%x, MC:0x%x, LA:0x%x failed:%d\n",
133 txn
->mt
, txn
->mc
, txn
->la
, ret
);
136 if (!clk_pause_msg
&& (!need_tid
|| ret
== -ETIMEDOUT
)) {
138 * remove runtime-pm vote if this was TX only, or
139 * if there was error during this transaction
141 pm_runtime_mark_last_busy(ctrl
->dev
);
142 pm_runtime_mark_last_busy(ctrl
->dev
);
146 EXPORT_SYMBOL_GPL(slim_do_transfer
);
148 static int slim_val_inf_sanity(struct slim_controller
*ctrl
,
149 struct slim_val_inf
*msg
, u8 mc
)
151 if (!msg
|| msg
->num_bytes
> 16 ||
152 (msg
->start_offset
+ msg
->num_bytes
) > 0xC00)
155 case SLIM_MSG_MC_REQUEST_VALUE
:
156 case SLIM_MSG_MC_REQUEST_INFORMATION
:
157 if (msg
->rbuf
!= NULL
)
161 case SLIM_MSG_MC_CHANGE_VALUE
:
162 case SLIM_MSG_MC_CLEAR_INFORMATION
:
163 if (msg
->wbuf
!= NULL
)
167 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE
:
168 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION
:
169 if (msg
->rbuf
!= NULL
&& msg
->wbuf
!= NULL
)
175 dev_err(ctrl
->dev
, "Sanity check failed:msg:offset:0x%x, mc:%d\n",
176 msg
->start_offset
, mc
);
180 static u16
slim_slicesize(int code
)
182 static const u8 sizetocode
[16] = {
183 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7
186 code
= clamp(code
, 1, (int)ARRAY_SIZE(sizetocode
));
188 return sizetocode
[code
- 1];
192 * slim_xfer_msg() - Transfer a value info message on slim device
194 * @sbdev: slim device to which this msg has to be transfered
195 * @msg: value info message pointer
196 * @mc: message code of the message
198 * Called by drivers which want to transfer a vlaue or info elements.
200 * Return: -ETIMEDOUT: If transmission of this message timed out
202 int slim_xfer_msg(struct slim_device
*sbdev
, struct slim_val_inf
*msg
,
205 DEFINE_SLIM_LDEST_TXN(txn_stack
, mc
, 6, sbdev
->laddr
, msg
);
206 struct slim_msg_txn
*txn
= &txn_stack
;
207 struct slim_controller
*ctrl
= sbdev
->ctrl
;
214 ret
= slim_val_inf_sanity(ctrl
, msg
, mc
);
218 sl
= slim_slicesize(msg
->num_bytes
);
220 dev_dbg(ctrl
->dev
, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
221 msg
->start_offset
, msg
->num_bytes
, mc
, sl
);
223 txn
->ec
= ((sl
| (1 << 3)) | ((msg
->start_offset
& 0xFFF) << 4));
226 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE
:
227 case SLIM_MSG_MC_CHANGE_VALUE
:
228 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION
:
229 case SLIM_MSG_MC_CLEAR_INFORMATION
:
230 txn
->rl
+= msg
->num_bytes
;
235 if (slim_tid_txn(txn
->mt
, txn
->mc
))
238 return slim_do_transfer(ctrl
, txn
);
240 EXPORT_SYMBOL_GPL(slim_xfer_msg
);
242 static void slim_fill_msg(struct slim_val_inf
*msg
, u32 addr
,
243 size_t count
, u8
*rbuf
, u8
*wbuf
)
245 msg
->start_offset
= addr
;
246 msg
->num_bytes
= count
;
252 * slim_read() - Read SLIMbus value element
254 * @sdev: client handle.
255 * @addr: address of value element to read.
256 * @count: number of bytes to read. Maximum bytes allowed are 16.
257 * @val: will return what the value element value was
259 * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
260 * this message timed out (e.g. due to bus lines not being clocked
261 * or driven by controller)
263 int slim_read(struct slim_device
*sdev
, u32 addr
, size_t count
, u8
*val
)
265 struct slim_val_inf msg
;
267 slim_fill_msg(&msg
, addr
, count
, val
, NULL
);
269 return slim_xfer_msg(sdev
, &msg
, SLIM_MSG_MC_REQUEST_VALUE
);
271 EXPORT_SYMBOL_GPL(slim_read
);
274 * slim_readb() - Read byte from SLIMbus value element
276 * @sdev: client handle.
277 * @addr: address in the value element to read.
279 * Return: byte value of value element.
281 int slim_readb(struct slim_device
*sdev
, u32 addr
)
286 ret
= slim_read(sdev
, addr
, 1, &buf
);
292 EXPORT_SYMBOL_GPL(slim_readb
);
295 * slim_write() - Write SLIMbus value element
297 * @sdev: client handle.
298 * @addr: address in the value element to write.
299 * @count: number of bytes to write. Maximum bytes allowed are 16.
300 * @val: value to write to value element
302 * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
303 * this message timed out (e.g. due to bus lines not being clocked
304 * or driven by controller)
306 int slim_write(struct slim_device
*sdev
, u32 addr
, size_t count
, u8
*val
)
308 struct slim_val_inf msg
;
310 slim_fill_msg(&msg
, addr
, count
, val
, NULL
);
312 return slim_xfer_msg(sdev
, &msg
, SLIM_MSG_MC_CHANGE_VALUE
);
314 EXPORT_SYMBOL_GPL(slim_write
);
317 * slim_writeb() - Write byte to SLIMbus value element
319 * @sdev: client handle.
320 * @addr: address of value element to write.
321 * @value: value to write to value element
323 * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
324 * this message timed out (e.g. due to bus lines not being clocked
325 * or driven by controller)
328 int slim_writeb(struct slim_device
*sdev
, u32 addr
, u8 value
)
330 return slim_write(sdev
, addr
, 1, &value
);
332 EXPORT_SYMBOL_GPL(slim_writeb
);