Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2011-2017, The Linux Foundation
0004  */
0005 
0006 #include <linux/slab.h>
0007 #include <linux/pm_runtime.h>
0008 #include "slimbus.h"
0009 
0010 /**
0011  * slim_msg_response() - Deliver Message response received from a device to the
0012  *          framework.
0013  *
0014  * @ctrl: Controller handle
0015  * @reply: Reply received from the device
0016  * @len: Length of the reply
0017  * @tid: Transaction ID received with which framework can associate reply.
0018  *
0019  * Called by controller to inform framework about the response received.
0020  * This helps in making the API asynchronous, and controller-driver doesn't need
0021  * to manage 1 more table other than the one managed by framework mapping TID
0022  * with buffers
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     /* Remove runtime-pm vote now that response was received for TID txn */
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  * slim_alloc_txn_tid() - Allocate a tid to txn
0057  *
0058  * @ctrl: Controller handle
0059  * @txn: transaction to be allocated with tid.
0060  *
0061  * Return: zero on success with valid txn->tid and error code on failures.
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  * slim_free_txn_tid() - Free tid of txn
0083  *
0084  * @ctrl: Controller handle
0085  * @txn: transaction whose tid should be freed
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  * slim_do_transfer() - Process a SLIMbus-messaging transaction
0099  *
0100  * @ctrl: Controller handle
0101  * @txn: Transaction to be sent over SLIMbus
0102  *
0103  * Called by controller to transmit messaging transactions not dealing with
0104  * Interface/Value elements. (e.g. transmitting a message to assign logical
0105  * address to a slave device
0106  *
0107  * Return: -ETIMEDOUT: If transmission of this message timed out
0108  *  (e.g. due to bus lines not being clocked or driven by controller)
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      * do not vote for runtime-PM if the transactions are part of clock
0118      * pause sequence
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     /* Initialize tid to invalid value */
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          * remove runtime-pm vote if this was TX only, or
0170          * if there was error during this transaction
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  * slim_xfer_msg() - Transfer a value info message on slim device
0224  *
0225  * @sbdev: slim device to which this msg has to be transfered
0226  * @msg: value info message pointer
0227  * @mc: message code of the message
0228  *
0229  * Called by drivers which want to transfer a vlaue or info elements.
0230  *
0231  * Return: -ETIMEDOUT: If transmission of this message timed out
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  * slim_read() - Read SLIMbus value element
0286  *
0287  * @sdev: client handle.
0288  * @addr:  address of value element to read.
0289  * @count: number of bytes to read. Maximum bytes allowed are 16.
0290  * @val: will return what the value element value was
0291  *
0292  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
0293  * this message timed out (e.g. due to bus lines not being clocked
0294  * or driven by controller)
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  * slim_readb() - Read byte from SLIMbus value element
0308  *
0309  * @sdev: client handle.
0310  * @addr:  address in the value element to read.
0311  *
0312  * Return: byte value of value element.
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  * slim_write() - Write SLIMbus value element
0329  *
0330  * @sdev: client handle.
0331  * @addr:  address in the value element to write.
0332  * @count: number of bytes to write. Maximum bytes allowed are 16.
0333  * @val: value to write to value element
0334  *
0335  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
0336  * this message timed out (e.g. due to bus lines not being clocked
0337  * or driven by controller)
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  * slim_writeb() - Write byte to SLIMbus value element
0351  *
0352  * @sdev: client handle.
0353  * @addr:  address of value element to write.
0354  * @value: value to write to value element
0355  *
0356  * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
0357  * this message timed out (e.g. due to bus lines not being clocked
0358  * or driven by controller)
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);