soc: mediatek: cmdq: Refine cmdq_pkt_create() and cmdq_pkt_destroy()
authorChun-Kuang Hu <chunkuang.hu@kernel.org>
Thu, 22 Feb 2024 15:41:15 +0000 (15:41 +0000)
committerAngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Tue, 23 Apr 2024 10:16:54 +0000 (12:16 +0200)
cmdq_pkt_create() and cmdq_pkt_destroy() is not suitable for
client drivers so each client driver has implement its own
function. This refinement would pass struct cmdq_pkt pointer into
cmdq_pkt_create(). In addition, client driver has the struct
cmdq_client information, so it's not necessary to store this
information in struct cmdq_pkt. After this refinement, client
drivers could use these helper funciton instead of implementing
its own version.

Signed-off-by: Chun-Kuang Hu <chunkuang.hu@kernel.org>
Link: https://lore.kernel.org/r/20240222154120.16959-8-chunkuang.hu@kernel.org
Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
drivers/soc/mediatek/mtk-cmdq-helper.c
include/linux/soc/mediatek/mtk-cmdq.h

index bbe41302210ab2e34cfb0b7dbbf8375a02344883..1a1f1b260a064299cefb09d06ac61fc5ba0c7d94 100644 (file)
@@ -106,22 +106,16 @@ void cmdq_mbox_destroy(struct cmdq_client *client)
 }
 EXPORT_SYMBOL(cmdq_mbox_destroy);
 
-struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size)
+int cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt, size_t size)
 {
-       struct cmdq_pkt *pkt;
        struct device *dev;
        dma_addr_t dma_addr;
 
-       pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
-       if (!pkt)
-               return ERR_PTR(-ENOMEM);
        pkt->va_base = kzalloc(size, GFP_KERNEL);
-       if (!pkt->va_base) {
-               kfree(pkt);
-               return ERR_PTR(-ENOMEM);
-       }
+       if (!pkt->va_base)
+               return -ENOMEM;
+
        pkt->buf_size = size;
-       pkt->cl = (void *)client;
 
        dev = client->chan->mbox->dev;
        dma_addr = dma_map_single(dev, pkt->va_base, pkt->buf_size,
@@ -129,24 +123,20 @@ struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size)
        if (dma_mapping_error(dev, dma_addr)) {
                dev_err(dev, "dma map failed, size=%u\n", (u32)(u64)size);
                kfree(pkt->va_base);
-               kfree(pkt);
-               return ERR_PTR(-ENOMEM);
+               return -ENOMEM;
        }
 
        pkt->pa_base = dma_addr;
 
-       return pkt;
+       return 0;
 }
 EXPORT_SYMBOL(cmdq_pkt_create);
 
-void cmdq_pkt_destroy(struct cmdq_pkt *pkt)
+void cmdq_pkt_destroy(struct cmdq_client *client, struct cmdq_pkt *pkt)
 {
-       struct cmdq_client *client = (struct cmdq_client *)pkt->cl;
-
        dma_unmap_single(client->chan->mbox->dev, pkt->pa_base, pkt->buf_size,
                         DMA_TO_DEVICE);
        kfree(pkt->va_base);
-       kfree(pkt);
 }
 EXPORT_SYMBOL(cmdq_pkt_destroy);
 
index e79dc8c4ad41ab41f642ca003438148fff670754..e5335fb22a3b46a5434c6697edde5e3c0a09fbb9 100644 (file)
@@ -62,17 +62,19 @@ void cmdq_mbox_destroy(struct cmdq_client *client);
 /**
  * cmdq_pkt_create() - create a CMDQ packet
  * @client:    the CMDQ mailbox client
+ * @pkt:       the CMDQ packet
  * @size:      required CMDQ buffer size
  *
- * Return: CMDQ packet pointer
+ * Return: 0 for success; else the error code is returned
  */
-struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size);
+int cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt, size_t size);
 
 /**
  * cmdq_pkt_destroy() - destroy the CMDQ packet
+ * @client:    the CMDQ mailbox client
  * @pkt:       the CMDQ packet
  */
-void cmdq_pkt_destroy(struct cmdq_pkt *pkt);
+void cmdq_pkt_destroy(struct cmdq_client *client, struct cmdq_pkt *pkt);
 
 /**
  * cmdq_pkt_write() - append write command to the CMDQ packet
@@ -318,12 +320,12 @@ static inline struct cmdq_client *cmdq_mbox_create(struct device *dev, int index
 
 static inline void cmdq_mbox_destroy(struct cmdq_client *client) { }
 
-static inline  struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size)
+static inline int cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt, size_t size)
 {
-       return ERR_PTR(-EINVAL);
+       return -EINVAL;
 }
 
-static inline void cmdq_pkt_destroy(struct cmdq_pkt *pkt) { }
+static inline void cmdq_pkt_destroy(struct cmdq_client *client, struct cmdq_pkt *pkt) { }
 
 static inline int cmdq_pkt_write(struct cmdq_pkt *pkt, u8 subsys, u16 offset, u32 value)
 {