blob: e8f0e92c27dda707fe3647a515534ef2b333944e [file] [log] [blame]
Andrew F. Davisc02a81f2019-12-03 17:26:37 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * DMABUF Heaps Allocation Infrastructure
4 *
5 * Copyright (C) 2011 Google, Inc.
6 * Copyright (C) 2019 Linaro Ltd.
7 */
8
9#ifndef _DMA_HEAPS_H
10#define _DMA_HEAPS_H
11
12#include <linux/cdev.h>
13#include <linux/types.h>
14
15struct dma_heap;
16
17/**
18 * struct dma_heap_ops - ops to operate on a given heap
John Stultz349e8362020-08-14 22:42:09 +000019 * @allocate: allocate dmabuf and return struct dma_buf ptr
Hridya Valsaraju9419dd12021-01-31 20:38:27 -080020 * @get_pool_size: if heap maintains memory pools, get pool size in bytes
Andrew F. Davisc02a81f2019-12-03 17:26:37 +000021 *
John Stultz349e8362020-08-14 22:42:09 +000022 * allocate returns dmabuf on success, ERR_PTR(-errno) on error.
Andrew F. Davisc02a81f2019-12-03 17:26:37 +000023 */
24struct dma_heap_ops {
John Stultz349e8362020-08-14 22:42:09 +000025 struct dma_buf *(*allocate)(struct dma_heap *heap,
Andrew F. Davisc02a81f2019-12-03 17:26:37 +000026 unsigned long len,
27 unsigned long fd_flags,
28 unsigned long heap_flags);
Hridya Valsaraju9419dd12021-01-31 20:38:27 -080029 long (*get_pool_size)(struct dma_heap *heap);
Andrew F. Davisc02a81f2019-12-03 17:26:37 +000030};
31
32/**
33 * struct dma_heap_export_info - information needed to export a new dmabuf heap
34 * @name: used for debugging/device-node name
35 * @ops: ops struct for this heap
36 * @priv: heap exporter private data
37 *
38 * Information needed to export a new dmabuf heap.
39 */
40struct dma_heap_export_info {
41 const char *name;
42 const struct dma_heap_ops *ops;
43 void *priv;
44};
45
46/**
47 * dma_heap_get_drvdata() - get per-heap driver data
48 * @heap: DMA-Heap to retrieve private data for
49 *
50 * Returns:
51 * The per-heap data for the heap.
52 */
53void *dma_heap_get_drvdata(struct dma_heap *heap);
54
55/**
John Stultzc8bdd322020-05-07 20:08:13 +000056 * dma_heap_get_dev() - get device struct for the heap
57 * @heap: DMA-Heap to retrieve device struct from
58 *
59 * Returns:
60 * The device struct for the heap.
61 */
62struct device *dma_heap_get_dev(struct dma_heap *heap);
63
64/**
John Stultze09764e2021-02-06 05:28:51 +000065 * dma_heap_get_name() - get heap name
66 * @heap: DMA-Heap to retrieve private data for
67 *
68 * Returns:
69 * The char* for the heap name.
70 */
71const char *dma_heap_get_name(struct dma_heap *heap);
72
73/**
Andrew F. Davisc02a81f2019-12-03 17:26:37 +000074 * dma_heap_add - adds a heap to dmabuf heaps
75 * @exp_info: information needed to register this heap
76 */
77struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
78
John Stultzc700bdd2020-07-25 02:53:27 +000079/**
80 * dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it
81 * @heap: heap pointer
82 */
83void dma_heap_put(struct dma_heap *heap);
84
John Stultz8e1ec972020-05-05 18:40:52 +000085/**
86 * dma_heap_find - Returns the registered dma_heap with the specified name
87 * @name: Name of the heap to find
88 *
89 * NOTE: dma_heaps returned from this function MUST be released
90 * using dma_heap_put() when the user is done.
91 */
92struct dma_heap *dma_heap_find(const char *name);
93
94/**
95 * dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap
96 * @heap: dma_heap to allocate from
97 * @len: size to allocate
98 * @fd_flags: flags to set on returned dma-buf fd
99 * @heap_flags: flags to pass to the dma heap
100 *
101 * This is for internal dma-buf allocations only.
102 */
103struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
104 unsigned int fd_flags,
105 unsigned int heap_flags);
106
107/** dma_heap_buffer_free - Free dma_buf allocated by dma_heap_buffer_alloc
108 * @dma_buf: dma_buf to free
109 *
110 * This is really only a simple wrapper to dma_buf_put()
111 */
112void dma_heap_buffer_free(struct dma_buf *);
113
114/**
115 * dma_heap_bufferfd_alloc - Allocate dma-buf fd from a dma_heap
116 * @heap: dma_heap to allocate from
117 * @len: size to allocate
118 * @fd_flags: flags to set on returned dma-buf fd
119 * @heap_flags: flags to pass to the dma heap
120 */
121int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len,
122 unsigned int fd_flags,
123 unsigned int heap_flags);
Andrew F. Davisc02a81f2019-12-03 17:26:37 +0000124#endif /* _DMA_HEAPS_H */