^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * DMABUF Heaps Allocation Infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2011 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2019 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Simon Xue <xxm@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #ifndef _RK_DMA_HEAPS_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define _RK_DMA_HEAPS_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/dma-buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/rk-dma-heap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #if defined(CONFIG_DMABUF_RK_HEAPS_DEBUG_PRINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define dma_heap_print(fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define dma_heap_print(fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) no_printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define RK_DMA_HEAP_NAME_LEN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct rk_vmap_pfn_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long pfn; /* first pfn of contiguous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) pgprot_t prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * struct rk_dma_heap_ops - ops to operate on a given heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @allocate: allocate dmabuf and return struct dma_buf ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @get_pool_size: if heap maintains memory pools, get pool size in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * allocate returns dmabuf on success, ERR_PTR(-errno) on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct rk_dma_heap_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct dma_buf *(*allocate)(struct rk_dma_heap *heap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long fd_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long heap_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct page *(*alloc_contig_pages)(struct rk_dma_heap *heap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size_t len, const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void (*free_contig_pages)(struct rk_dma_heap *heap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct page *pages, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) long (*get_pool_size)(struct rk_dma_heap *heap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * struct rk_dma_heap_export_info - information needed to export a new dmabuf heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @name: used for debugging/device-node name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @ops: ops struct for this heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @priv: heap exporter private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Information needed to export a new dmabuf heap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct rk_dma_heap_export_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) const struct rk_dma_heap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) bool support_cma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * struct rk_dma_heap - represents a dmabuf heap in the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * @name: used for debugging/device-node name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @ops: ops struct for this heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @heap_devt heap device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @list list head connecting to list of heaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @heap_cdev heap char device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @heap_dev heap device struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Represents a heap of memory from which buffers can be made.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct rk_dma_heap {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) const struct rk_dma_heap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) dev_t heap_devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct list_head dmabuf_list; /* dmabuf attach to this node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct mutex dmabuf_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct list_head contig_list; /* contig buffer attach to this node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct mutex contig_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct cdev heap_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct kref refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct device *heap_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) bool support_cma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct seq_file *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct proc_dir_entry *procfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned long total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct rk_dma_heap_dmabuf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct dma_buf *dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) const char *orig_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) phys_addr_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) phys_addr_t end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct rk_dma_heap_contig_buf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) const char *orig_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) phys_addr_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) phys_addr_t end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * rk_dma_heap_get_drvdata() - get per-heap driver data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @heap: DMA-Heap to retrieve private data for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * The per-heap data for the heap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void *rk_dma_heap_get_drvdata(struct rk_dma_heap *heap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * rk_dma_heap_get_dev() - get device struct for the heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * @heap: DMA-Heap to retrieve device struct from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * The device struct for the heap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct device *rk_dma_heap_get_dev(struct rk_dma_heap *heap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * rk_dma_heap_get_name() - get heap name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @heap: DMA-Heap to retrieve private data for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * The char* for the heap name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) const char *rk_dma_heap_get_name(struct rk_dma_heap *heap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * rk_dma_heap_add - adds a heap to dmabuf heaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @exp_info: information needed to register this heap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct rk_dma_heap *rk_dma_heap_add(const struct rk_dma_heap_export_info *exp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * rk_dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * @heap: heap pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void rk_dma_heap_put(struct rk_dma_heap *heap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * rk_vmap_contig_pfn - Map contiguous pfn to vm area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @pfn: indicate the first pfn of contig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @count: count of pfns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @prot: for mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void *rk_vmap_contig_pfn(unsigned long pfn, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pgprot_t prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * rk_dma_heap_total_inc - Increase total buffer size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @heap: dma_heap to increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * @len: length to increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void rk_dma_heap_total_inc(struct rk_dma_heap *heap, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * rk_dma_heap_total_dec - Decrease total buffer size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * @heap: dma_heap to decrease
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * @len: length to decrease
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void rk_dma_heap_total_dec(struct rk_dma_heap *heap, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * rk_dma_heap_get_cma - get cma structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct cma *rk_dma_heap_get_cma(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #endif /* _DMA_HEAPS_H */