From dde8daabfc4e2306424326d28b89f71365cabbf4 Mon Sep 17 00:00:00 2001 From: Jianqun Xu Date: Mon, 27 Dec 2021 14:53:33 +0800 Subject: [PATCH] dma-buf: dump each dmabuf info Loop into each dmabuf and dump its info, including name, exp_name, size and physical address range from the scatterlist table(sgt). Since the sgt of dmabuf only exist after device attach/map, but some dmabufs have not be attach/map by device yet. This patch creates a fake platform device to do attach/map for a dmabuf who has a empty attachment list, and unmap/detach after all. Tested on RV1106 evb: cat /proc/rk_dmabuf NAME EXPORT SIZE:KiB SGLIST vmpi rk-dma-heap-cma 4 KiB 0: 0x04548000..0x04548fff vmpi rk-dma-heap-cma 4 KiB 0: 0x04547000..0x04547fff vmpi rk-dma-heap-cma 5584 KiB 0: 0x05400000..0x05973fff vmpi rk-dma-heap-cma 920 KiB 0: 0x05300000..0x053e5fff vmpi rk-dma-heap-cma 232 KiB 0: 0x045c0000..0x045f9fff vmpi rk-dma-heap-cma 4 KiB 0: 0x04546000..0x04546fff vmpi rk-dma-heap-cma 5584 KiB 0: 0x04d00000..0x05273fff vmpi rk-dma-heap-cma 920 KiB 0: 0x04c00000..0x04ce5fff vmpi rk-dma-heap-cma 232 KiB 0: 0x04580000..0x045b9fff vmpi rk-dma-heap-cma 5400 KiB 0: 0x04600000..0x04b45fff vmpi rk-dma-heap-cma 5400 KiB 0: 0x04000000..0x04545fff (null) videobuf2_vmalloc 12 KiB (null) videobuf2_vmalloc 12 KiB (null) videobuf2_vmalloc 12 KiB (null) videobuf2_vmalloc 12 KiB Signed-off-by: Jianqun Xu Change-Id: Icf46e9d585847ef6dafbddcd24e589046e3ff015 --- drivers/dma-buf/Kconfig | 11 ++++ drivers/dma-buf/Makefile | 1 + drivers/dma-buf/dmabuf_procfs.c | 97 +++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 drivers/dma-buf/dmabuf_procfs.c diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig index e375dde37161..c293e30a3de1 100644 --- a/drivers/dma-buf/Kconfig +++ b/drivers/dma-buf/Kconfig @@ -9,6 +9,17 @@ config DMABUF_CACHE This option support to store attachments in a list and destroy them by set to a callback list in the dtor of dma-buf. +config DMABUF_PROCFS + tristate "DMABUF procfs support" + depends on DMA_SHARED_BUFFER + depends on PROC_FS + depends on NO_GKI + help + Turns on this to create a procfs debug interface for dma-buf, support + get information from db_list by get_each_dmabuf. + + If unsure, say "N". + config SYNC_FILE bool "Explicit Synchronization Framework" default n diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile index 66c2dc4b6881..b89f3309de80 100644 --- a/drivers/dma-buf/Makefile +++ b/drivers/dma-buf/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_SW_SYNC_DEBUG) += sync_debug.o obj-$(CONFIG_UDMABUF) += udmabuf.o obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o obj-$(CONFIG_DMABUF_HEAPS_ROCKCHIP) += rk_heaps/ +obj-$(CONFIG_DMABUF_PROCFS) += dmabuf_procfs.o dmabuf_selftests-y := \ selftest.o \ diff --git a/drivers/dma-buf/dmabuf_procfs.c b/drivers/dma-buf/dmabuf_procfs.c new file mode 100644 index 000000000000..29b62bfcafbe --- /dev/null +++ b/drivers/dma-buf/dmabuf_procfs.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2022 Rockchip Electronics Co. Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define K(size) ((unsigned long)((size) >> 10)) +static struct device *dmabuf_dev; + +static void rk_dmabuf_dump_sgt(struct dma_buf *dmabuf, void *priv) +{ + struct seq_file *s = priv; + struct scatterlist *sg; + struct dma_buf_attachment *a; + phys_addr_t end; + int i; + + list_for_each_entry(a, &dmabuf->attachments, node) { + if (!a->sgt) + continue; + for_each_sgtable_sg(a->sgt, sg, i) { + end = sg->dma_address + sg->length - 1; + seq_printf(s, "%4d: ", i); + seq_printf(s, "%pa..%pa\t", &sg->dma_address, &end); + } + break; + } +} + +static int rk_dmabuf_cb(const struct dma_buf *dmabuf, void *private) +{ + struct seq_file *s = private; + struct sg_table *sgt; + struct dma_buf_attachment *a; + struct dma_buf *db = (struct dma_buf *)dmabuf; + bool need_map = list_empty(&db->attachments); + + seq_printf(s, "%s\t%32s %10lu KiB", db->name, db->exp_name, K(db->size)); + + if (need_map) { + a = dma_buf_attach(db, dmabuf_dev); + if (IS_ERR_OR_NULL(a)) + return PTR_ERR(a); + + sgt = dma_buf_map_attachment(a, 0); + if (IS_ERR_OR_NULL(sgt)) + return PTR_ERR(sgt); + } + + rk_dmabuf_dump_sgt(db, s); + + if (need_map) { + dma_buf_unmap_attachment(a, sgt, 0); + dma_buf_detach(db, a); + } + + seq_puts(s, "\n"); + + return 0; +} + +static int rk_dmabuf_show(struct seq_file *s, void *v) +{ + seq_printf(s, "%s\t%32s %14s %8s\n\n", "NAME", "EXPORT", "SIZE:KiB", "SGLIST"); + + return get_each_dmabuf(rk_dmabuf_cb, s); +} + +static int __init rk_dmabuf_init(void) +{ + struct platform_device *pdev; + struct platform_device_info dev_info = { + .name = "dmabuf", + .id = PLATFORM_DEVID_AUTO, + .dma_mask = DMA_BIT_MASK(32), + }; + + pdev = platform_device_register_full(&dev_info); + dmabuf_dev = pdev ? &pdev->dev : NULL; + + proc_create_single("rk_dmabuf", 0, NULL, rk_dmabuf_show); + + return 0; +} +late_initcall_sync(rk_dmabuf_init); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Jianqun Xu "); +MODULE_DESCRIPTION("ROCKCHIP DMABUF Driver"); +MODULE_ALIAS("platform:rk-dmabuf");