0001
0002
0003
0004
0005
0006 #include <linux/slab.h>
0007 #include <linux/pm_runtime.h>
0008 #include "slimbus.h"
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
0025 {
0026 struct slim_msg_txn *txn;
0027 struct slim_val_inf *msg;
0028 unsigned long flags;
0029
0030 spin_lock_irqsave(&ctrl->txn_lock, flags);
0031 txn = idr_find(&ctrl->tid_idr, tid);
0032 spin_unlock_irqrestore(&ctrl->txn_lock, flags);
0033
0034 if (txn == NULL)
0035 return;
0036
0037 msg = txn->msg;
0038 if (msg == NULL || msg->rbuf == NULL) {
0039 dev_err(ctrl->dev, "Got response to invalid TID:%d, len:%d\n",
0040 tid, len);
0041 return;
0042 }
0043
0044 slim_free_txn_tid(ctrl, txn);
0045 memcpy(msg->rbuf, reply, len);
0046 if (txn->comp)
0047 complete(txn->comp);
0048
0049
0050 pm_runtime_mark_last_busy(ctrl->dev);
0051 pm_runtime_put_autosuspend(ctrl->dev);
0052 }
0053 EXPORT_SYMBOL_GPL(slim_msg_response);
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 int slim_alloc_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn)
0064 {
0065 unsigned long flags;
0066 int ret = 0;
0067
0068 spin_lock_irqsave(&ctrl->txn_lock, flags);
0069 ret = idr_alloc_cyclic(&ctrl->tid_idr, txn, 1,
0070 SLIM_MAX_TIDS, GFP_ATOMIC);
0071 if (ret < 0) {
0072 spin_unlock_irqrestore(&ctrl->txn_lock, flags);
0073 return ret;
0074 }
0075 txn->tid = ret;
0076 spin_unlock_irqrestore(&ctrl->txn_lock, flags);
0077 return 0;
0078 }
0079 EXPORT_SYMBOL_GPL(slim_alloc_txn_tid);
0080
0081
0082
0083
0084
0085
0086
0087 void slim_free_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn)
0088 {
0089 unsigned long flags;
0090
0091 spin_lock_irqsave(&ctrl->txn_lock, flags);
0092 idr_remove(&ctrl->tid_idr, txn->tid);
0093 spin_unlock_irqrestore(&ctrl->txn_lock, flags);
0094 }
0095 EXPORT_SYMBOL_GPL(slim_free_txn_tid);
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 int slim_do_transfer(struct slim_controller *ctrl, struct slim_msg_txn *txn)
0111 {
0112 DECLARE_COMPLETION_ONSTACK(done);
0113 bool need_tid = false, clk_pause_msg = false;
0114 int ret, timeout;
0115
0116
0117
0118
0119
0120 if (ctrl->sched.clk_state == SLIM_CLK_ENTERING_PAUSE &&
0121 (txn->mt == SLIM_MSG_MT_CORE &&
0122 txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
0123 txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
0124 clk_pause_msg = true;
0125
0126 if (!clk_pause_msg) {
0127 ret = pm_runtime_get_sync(ctrl->dev);
0128 if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
0129 dev_err(ctrl->dev, "ctrl wrong state:%d, ret:%d\n",
0130 ctrl->sched.clk_state, ret);
0131 goto slim_xfer_err;
0132 }
0133 }
0134
0135 txn->tid = 0;
0136 need_tid = slim_tid_txn(txn->mt, txn->mc);
0137
0138 if (need_tid) {
0139 ret = slim_alloc_txn_tid(ctrl, txn);
0140 if (ret)
0141 return ret;
0142
0143 if (!txn->msg->comp)
0144 txn->comp = &done;
0145 else
0146 txn->comp = txn->comp;
0147 }
0148
0149 ret = ctrl->xfer_msg(ctrl, txn);
0150
0151 if (!ret && need_tid && !txn->msg->comp) {
0152 unsigned long ms = txn->rl + HZ;
0153
0154 timeout = wait_for_completion_timeout(txn->comp,
0155 msecs_to_jiffies(ms));
0156 if (!timeout) {
0157 ret = -ETIMEDOUT;
0158 slim_free_txn_tid(ctrl, txn);
0159 }
0160 }
0161
0162 if (ret)
0163 dev_err(ctrl->dev, "Tx:MT:0x%x, MC:0x%x, LA:0x%x failed:%d\n",
0164 txn->mt, txn->mc, txn->la, ret);
0165
0166 slim_xfer_err:
0167 if (!clk_pause_msg && (txn->tid == 0 || ret == -ETIMEDOUT)) {
0168
0169
0170
0171
0172 pm_runtime_mark_last_busy(ctrl->dev);
0173 pm_runtime_put_autosuspend(ctrl->dev);
0174 }
0175 return ret;
0176 }
0177 EXPORT_SYMBOL_GPL(slim_do_transfer);
0178
0179 static int slim_val_inf_sanity(struct slim_controller *ctrl,
0180 struct slim_val_inf *msg, u8 mc)
0181 {
0182 if (!msg || msg->num_bytes > 16 ||
0183 (msg->start_offset + msg->num_bytes) > 0xC00)
0184 goto reterr;
0185 switch (mc) {
0186 case SLIM_MSG_MC_REQUEST_VALUE:
0187 case SLIM_MSG_MC_REQUEST_INFORMATION:
0188 if (msg->rbuf != NULL)
0189 return 0;
0190 break;
0191
0192 case SLIM_MSG_MC_CHANGE_VALUE:
0193 case SLIM_MSG_MC_CLEAR_INFORMATION:
0194 if (msg->wbuf != NULL)
0195 return 0;
0196 break;
0197
0198 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
0199 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
0200 if (msg->rbuf != NULL && msg->wbuf != NULL)
0201 return 0;
0202 break;
0203 }
0204 reterr:
0205 if (msg)
0206 dev_err(ctrl->dev, "Sanity check failed:msg:offset:0x%x, mc:%d\n",
0207 msg->start_offset, mc);
0208 return -EINVAL;
0209 }
0210
0211 static u16 slim_slicesize(int code)
0212 {
0213 static const u8 sizetocode[16] = {
0214 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7
0215 };
0216
0217 code = clamp(code, 1, (int)ARRAY_SIZE(sizetocode));
0218
0219 return sizetocode[code - 1];
0220 }
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 int slim_xfer_msg(struct slim_device *sbdev, struct slim_val_inf *msg,
0234 u8 mc)
0235 {
0236 DEFINE_SLIM_LDEST_TXN(txn_stack, mc, 6, sbdev->laddr, msg);
0237 struct slim_msg_txn *txn = &txn_stack;
0238 struct slim_controller *ctrl = sbdev->ctrl;
0239 int ret;
0240 u16 sl;
0241
0242 if (!ctrl)
0243 return -EINVAL;
0244
0245 ret = slim_val_inf_sanity(ctrl, msg, mc);
0246 if (ret)
0247 return ret;
0248
0249 sl = slim_slicesize(msg->num_bytes);
0250
0251 dev_dbg(ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
0252 msg->start_offset, msg->num_bytes, mc, sl);
0253
0254 txn->ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
0255
0256 switch (mc) {
0257 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
0258 case SLIM_MSG_MC_CHANGE_VALUE:
0259 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
0260 case SLIM_MSG_MC_CLEAR_INFORMATION:
0261 txn->rl += msg->num_bytes;
0262 break;
0263 default:
0264 break;
0265 }
0266
0267 if (slim_tid_txn(txn->mt, txn->mc))
0268 txn->rl++;
0269
0270 return slim_do_transfer(ctrl, txn);
0271 }
0272 EXPORT_SYMBOL_GPL(slim_xfer_msg);
0273
0274 static void slim_fill_msg(struct slim_val_inf *msg, u32 addr,
0275 size_t count, u8 *rbuf, u8 *wbuf)
0276 {
0277 msg->start_offset = addr;
0278 msg->num_bytes = count;
0279 msg->rbuf = rbuf;
0280 msg->wbuf = wbuf;
0281 msg->comp = NULL;
0282 }
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 int slim_read(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
0297 {
0298 struct slim_val_inf msg;
0299
0300 slim_fill_msg(&msg, addr, count, val, NULL);
0301
0302 return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_REQUEST_VALUE);
0303 }
0304 EXPORT_SYMBOL_GPL(slim_read);
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314 int slim_readb(struct slim_device *sdev, u32 addr)
0315 {
0316 int ret;
0317 u8 buf;
0318
0319 ret = slim_read(sdev, addr, 1, &buf);
0320 if (ret < 0)
0321 return ret;
0322 else
0323 return buf;
0324 }
0325 EXPORT_SYMBOL_GPL(slim_readb);
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339 int slim_write(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
0340 {
0341 struct slim_val_inf msg;
0342
0343 slim_fill_msg(&msg, addr, count, NULL, val);
0344
0345 return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_CHANGE_VALUE);
0346 }
0347 EXPORT_SYMBOL_GPL(slim_write);
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361 int slim_writeb(struct slim_device *sdev, u32 addr, u8 value)
0362 {
0363 return slim_write(sdev, addr, 1, &value);
0364 }
0365 EXPORT_SYMBOL_GPL(slim_writeb);