The MSM DSI command DMA engine natively supports chaining multiple
packets in a single DMA buffer by setting BIT(7) ("Last packet") only on
the final packet of the transfer.  Previously, each mipi_dsi_msg was
dispatched as its own DMA transfer, meaning every command in video mode
paid a full dsi_wait4video_eng_busy() cycle (~33 ms at 60 Hz) on DSI 6G
controllers without hardware DMA scheduling.

For panels whose brightness updates require multiple DCS commands (such
as Samsung S6E88A0 AMOLED panels sending test-key, AOR, ELVSS, gamma,
latch, and test-key-off = 6 commands, 80 bytes total), a single
brightness change took ~200-230 ms (~4.5 updates/sec).

Implement struct mipi_dsi_host_ops.transfer_batch() by packing
contiguous write-only messages that share the same LPM flag into a
single CMD DMA buffer (splitting cleanly if a chunk exceeds the BLLP or
TX buffer limit, and falling back to single-message transfer for reads
or empty messages).  Tested on Samsung Galaxy A3 (SM-A300F, MSM8916,
DSI 6G v1.3): backlight updates drop from ~200 ms to ~35 ms (~6x faster)
with all 6 commands sent atomically in a single VBLANK.

Signed-off-by: Vladimir Bely <[email protected]>
---
 drivers/gpu/drm/msm/dsi/dsi.h         |   4 +
 drivers/gpu/drm/msm/dsi/dsi_host.c    | 187 +++++++++++++++++++++++---
 drivers/gpu/drm/msm/dsi/dsi_manager.c |  40 ++++++
 3 files changed, 214 insertions(+), 17 deletions(-)

diff --git a/drivers/gpu/drm/msm/dsi/dsi.h b/drivers/gpu/drm/msm/dsi/dsi.h
index 93c028a12..e7b3c66e7 100644
--- a/drivers/gpu/drm/msm/dsi/dsi.h
+++ b/drivers/gpu/drm/msm/dsi/dsi.h
@@ -51,6 +51,8 @@ struct msm_dsi {
 int msm_dsi_manager_connector_init(struct msm_dsi *msm_dsi,
                                   struct drm_encoder *encoder);
 int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg);
+int msm_dsi_manager_cmd_xfer_batch(int id, const struct mipi_dsi_msg *msgs,
+                                  size_t count);
 bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 dma_base, u32 len);
 int msm_dsi_manager_register(struct msm_dsi *msm_dsi);
 void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi);
@@ -64,6 +66,8 @@ void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
                                        const struct mipi_dsi_msg *msg);
 int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
                                        const struct mipi_dsi_msg *msg);
+int msm_dsi_host_cmd_tx_batch(struct mipi_dsi_host *host,
+                             const struct mipi_dsi_msg *msgs, size_t count);
 int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
                                        const struct mipi_dsi_msg *msg);
 void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host,
diff --git a/drivers/gpu/drm/msm/dsi/dsi_host.c 
b/drivers/gpu/drm/msm/dsi/dsi_host.c
index 78f0fd259..6e1630edc 100644
--- a/drivers/gpu/drm/msm/dsi/dsi_host.c
+++ b/drivers/gpu/drm/msm/dsi/dsi_host.c
@@ -1321,6 +1321,32 @@ void dsi_tx_buf_put_6g(struct msm_dsi_host *msm_host)
        msm_gem_put_vaddr(msm_host->tx_gem_obj);
 }
 
+static int dsi_cmd_dma_pack(u8 *data, const struct mipi_dsi_packet *packet,
+                           const struct mipi_dsi_msg *msg, bool last)
+{
+       int len = (packet->size + 3) & (~0x3);
+
+       /* MSM specific command format in memory */
+       data[0] = packet->header[1];
+       data[1] = packet->header[2];
+       data[2] = packet->header[0];
+       data[3] = last ? BIT(7) : 0; /* Last packet */
+       if (mipi_dsi_packet_format_is_long(msg->type))
+               data[3] |= BIT(6);
+       if (msg->rx_buf && msg->rx_len)
+               data[3] |= BIT(5);
+
+       /* Long packet */
+       if (packet->payload && packet->payload_length)
+               memcpy(data + 4, packet->payload, packet->payload_length);
+
+       /* Append 0xff to the end */
+       if (packet->size < len)
+               memset(data + packet->size, 0xff, len - packet->size);
+
+       return len;
+}
+
 /*
  * prepare cmd buffer to be txed
  */
@@ -1352,23 +1378,7 @@ static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
                return ret;
        }
 
-       /* MSM specific command format in memory */
-       data[0] = packet.header[1];
-       data[1] = packet.header[2];
-       data[2] = packet.header[0];
-       data[3] = BIT(7); /* Last packet */
-       if (mipi_dsi_packet_format_is_long(msg->type))
-               data[3] |= BIT(6);
-       if (msg->rx_buf && msg->rx_len)
-               data[3] |= BIT(5);
-
-       /* Long packet */
-       if (packet.payload && packet.payload_length)
-               memcpy(data + 4, packet.payload, packet.payload_length);
-
-       /* Append 0xff to the end */
-       if (packet.size < len)
-               memset(data + packet.size, 0xff, len - packet.size);
+       dsi_cmd_dma_pack(data, &packet, msg, true);
 
        if (cfg_hnd->ops->tx_buf_put)
                cfg_hnd->ops->tx_buf_put(msm_host);
@@ -1568,6 +1578,90 @@ static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
        return len;
 }
 
+static int dsi_cmds2buf_tx_batch(struct msm_dsi_host *msm_host,
+                                const struct mipi_dsi_msg *msgs, size_t count)
+{
+       const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
+       int max_len = msm_host->tx_size;
+       int offset = 0, last_offset = 0;
+       u8 *data = NULL;
+       size_t i;
+       int ret;
+
+       if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && msm_host->mode) {
+               int bllp_len = msm_host->mode->hdisplay *
+                       mipi_dsi_pixel_format_to_bpp(msm_host->format) / 8;
+
+               max_len = min(max_len, bllp_len);
+       }
+
+       for (i = 0; i < count; i++) {
+               struct mipi_dsi_packet packet;
+               int pkt_len;
+
+               ret = mipi_dsi_create_packet(&packet, &msgs[i]);
+               if (ret) {
+                       pr_err("%s: create packet failed, %d\n", __func__, ret);
+                       goto err_put;
+               }
+               pkt_len = (packet.size + 3) & (~0x3);
+               if (pkt_len > max_len) {
+                       pr_err("%s: cmd cannot fit into transfer buffer, 
len=%d\n",
+                              __func__, pkt_len);
+                       ret = -EINVAL;
+                       goto err_put;
+               }
+
+               if (offset > 0 && offset + pkt_len > max_len) {
+                       data[last_offset + 3] |= BIT(7); /* Last packet */
+                       if (cfg_hnd->ops->tx_buf_put)
+                               cfg_hnd->ops->tx_buf_put(msm_host);
+                       data = NULL;
+
+                       ret = dsi_cmd_dma_tx(msm_host, offset);
+                       if (ret < 0)
+                               return ret;
+                       if (ret < offset)
+                               return -EIO;
+                       offset = 0;
+               }
+
+               if (!data) {
+                       data = cfg_hnd->ops->tx_buf_get(msm_host);
+                       if (IS_ERR(data)) {
+                               ret = PTR_ERR(data);
+                               pr_err("%s: get vaddr failed, %d\n", __func__, 
ret);
+                               return ret;
+                       }
+               }
+
+               dsi_cmd_dma_pack(data + offset, &packet, &msgs[i], false);
+               last_offset = offset;
+               offset += pkt_len;
+       }
+
+       if (offset > 0) {
+               data[last_offset + 3] |= BIT(7); /* Last packet */
+               if (cfg_hnd->ops->tx_buf_put)
+                       cfg_hnd->ops->tx_buf_put(msm_host);
+
+               ret = dsi_cmd_dma_tx(msm_host, offset);
+               if (ret < 0)
+                       return ret;
+               if (ret < offset)
+                       return -EIO;
+       } else if (data && cfg_hnd->ops->tx_buf_put) {
+               cfg_hnd->ops->tx_buf_put(msm_host);
+       }
+
+       return 0;
+
+err_put:
+       if (data && cfg_hnd->ops->tx_buf_put)
+               cfg_hnd->ops->tx_buf_put(msm_host);
+       return ret;
+}
+
 static void dsi_err_worker(struct work_struct *work)
 {
        struct msm_dsi_host *msm_host =
@@ -1789,10 +1883,61 @@ static ssize_t dsi_host_transfer(struct mipi_dsi_host 
*host,
        return ret;
 }
 
+static int dsi_host_transfer_batch(struct mipi_dsi_host *host,
+                                  const struct mipi_dsi_msg *msgs,
+                                  size_t count)
+{
+       struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
+       size_t i = 0;
+       int ret = 0;
+
+       if (!msgs || !msm_host->power_on)
+               return -EINVAL;
+
+       if (!count)
+               return 0;
+
+       mutex_lock(&msm_host->cmd_mutex);
+       while (i < count) {
+               size_t j;
+               u16 lpm;
+
+               if ((msgs[i].rx_buf && msgs[i].rx_len) ||
+                   !msgs[i].tx_buf || !msgs[i].tx_len) {
+                       ret = msm_dsi_manager_cmd_xfer(msm_host->id, &msgs[i]);
+                       i++;
+               } else {
+                       lpm = msgs[i].flags & MIPI_DSI_MSG_USE_LPM;
+                       j = i + 1;
+                       while (j < count &&
+                              !(msgs[j].rx_buf && msgs[j].rx_len) &&
+                              msgs[j].tx_buf && msgs[j].tx_len &&
+                              (msgs[j].flags & MIPI_DSI_MSG_USE_LPM) == lpm)
+                               j++;
+
+                       if (j - i == 1)
+                               ret = msm_dsi_manager_cmd_xfer(msm_host->id,
+                                                              &msgs[i]);
+                       else
+                               ret = 
msm_dsi_manager_cmd_xfer_batch(msm_host->id,
+                                                                    &msgs[i],
+                                                                    j - i);
+                       i = j;
+               }
+
+               if (ret < 0)
+                       break;
+       }
+       mutex_unlock(&msm_host->cmd_mutex);
+
+       return ret < 0 ? ret : 0;
+}
+
 static const struct mipi_dsi_host_ops dsi_host_ops = {
        .attach = dsi_host_attach,
        .detach = dsi_host_detach,
        .transfer = dsi_host_transfer,
+       .transfer_batch = dsi_host_transfer_batch,
 };
 
 /*
@@ -2225,6 +2370,14 @@ int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
        return dsi_cmds2buf_tx(msm_host, msg);
 }
 
+int msm_dsi_host_cmd_tx_batch(struct mipi_dsi_host *host,
+                             const struct mipi_dsi_msg *msgs, size_t count)
+{
+       struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
+
+       return dsi_cmds2buf_tx_batch(msm_host, msgs, count);
+}
+
 int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
                                const struct mipi_dsi_msg *msg)
 {
diff --git a/drivers/gpu/drm/msm/dsi/dsi_manager.c 
b/drivers/gpu/drm/msm/dsi/dsi_manager.c
index 531d4ff78..45c077fd9 100644
--- a/drivers/gpu/drm/msm/dsi/dsi_manager.c
+++ b/drivers/gpu/drm/msm/dsi/dsi_manager.c
@@ -539,6 +539,46 @@ int msm_dsi_manager_cmd_xfer(int id, const struct 
mipi_dsi_msg *msg)
        return ret;
 }
 
+int msm_dsi_manager_cmd_xfer_batch(int id, const struct mipi_dsi_msg *msgs,
+                                  size_t count)
+{
+       struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
+       struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
+       struct mipi_dsi_host *host = msm_dsi->host;
+       bool need_sync = IS_SYNC_NEEDED();
+       int ret;
+
+       if (!msgs || !count)
+               return 0;
+
+       if (need_sync && id == DSI_0)
+               return 0;
+
+       if (need_sync && msm_dsi0) {
+               ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, &msgs[0]);
+               if (ret) {
+                       pr_err("%s: failed to prepare non-trigger host, %d\n",
+                              __func__, ret);
+                       return ret;
+               }
+       }
+       ret = msm_dsi_host_xfer_prepare(host, &msgs[0]);
+       if (ret) {
+               pr_err("%s: failed to prepare host, %d\n", __func__, ret);
+               goto restore_host0;
+       }
+
+       ret = msm_dsi_host_cmd_tx_batch(host, msgs, count);
+
+       msm_dsi_host_xfer_restore(host, &msgs[0]);
+
+restore_host0:
+       if (need_sync && msm_dsi0)
+               msm_dsi_host_xfer_restore(msm_dsi0->host, &msgs[0]);
+
+       return ret;
+}
+
 bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 dma_base, u32 len)
 {
        struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
-- 
2.43.0

Reply via email to