Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/staging/android/ion/ion_priv.h |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #ifndef _ION_PRIV_H |
| 18 | #define _ION_PRIV_H |
| 19 | |
| 20 | #include <linux/kref.h> |
| 21 | #include <linux/mm_types.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/rbtree.h> |
| 24 | |
| 25 | #include "ion.h" |
| 26 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 27 | struct ion_buffer *ion_handle_buffer(struct ion_handle *handle); |
| 28 | |
| 29 | /** |
| 30 | * struct ion_buffer - metadata for a particular buffer |
| 31 | * @ref: refernce count |
| 32 | * @node: node in the ion_device buffers tree |
| 33 | * @dev: back pointer to the ion_device |
| 34 | * @heap: back pointer to the heap the buffer came from |
| 35 | * @flags: buffer specific flags |
| 36 | * @size: size of the buffer |
| 37 | * @priv_virt: private data to the buffer representable as |
| 38 | * a void * |
| 39 | * @priv_phys: private data to the buffer representable as |
| 40 | * an ion_phys_addr_t (and someday a phys_addr_t) |
| 41 | * @lock: protects the buffers cnt fields |
| 42 | * @kmap_cnt: number of times the buffer is mapped to the kernel |
| 43 | * @vaddr: the kenrel mapping if kmap_cnt is not zero |
| 44 | * @dmap_cnt: number of times the buffer is mapped for dma |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 45 | * @sg_table: the sg table for the buffer if dmap_cnt is not zero |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 46 | */ |
| 47 | struct ion_buffer { |
| 48 | struct kref ref; |
| 49 | struct rb_node node; |
| 50 | struct ion_device *dev; |
| 51 | struct ion_heap *heap; |
| 52 | unsigned long flags; |
| 53 | size_t size; |
| 54 | union { |
| 55 | void *priv_virt; |
| 56 | ion_phys_addr_t priv_phys; |
| 57 | }; |
| 58 | struct mutex lock; |
| 59 | int kmap_cnt; |
| 60 | void *vaddr; |
| 61 | int dmap_cnt; |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 62 | struct sg_table *sg_table; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame^] | 63 | unsigned long *dirty; |
| 64 | struct list_head vmas; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * struct ion_heap_ops - ops to operate on a given heap |
| 69 | * @allocate: allocate memory |
| 70 | * @free: free memory |
| 71 | * @phys get physical address of a buffer (only define on |
| 72 | * physically contiguous heaps) |
| 73 | * @map_dma map the memory for dma to a scatterlist |
| 74 | * @unmap_dma unmap the memory for dma |
| 75 | * @map_kernel map memory to the kernel |
| 76 | * @unmap_kernel unmap memory to the kernel |
| 77 | * @map_user map memory to userspace |
| 78 | */ |
| 79 | struct ion_heap_ops { |
| 80 | int (*allocate) (struct ion_heap *heap, |
| 81 | struct ion_buffer *buffer, unsigned long len, |
| 82 | unsigned long align, unsigned long flags); |
| 83 | void (*free) (struct ion_buffer *buffer); |
| 84 | int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, |
| 85 | ion_phys_addr_t *addr, size_t *len); |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 86 | struct sg_table *(*map_dma) (struct ion_heap *heap, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 87 | struct ion_buffer *buffer); |
| 88 | void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer); |
| 89 | void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); |
| 90 | void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); |
| 91 | int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer, |
| 92 | struct vm_area_struct *vma); |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * struct ion_heap - represents a heap in the system |
| 97 | * @node: rb node to put the heap on the device's tree of heaps |
| 98 | * @dev: back pointer to the ion_device |
| 99 | * @type: type of heap |
| 100 | * @ops: ops struct as above |
| 101 | * @id: id of heap, also indicates priority of this heap when |
| 102 | * allocating. These are specified by platform data and |
| 103 | * MUST be unique |
| 104 | * @name: used for debugging |
| 105 | * |
| 106 | * Represents a pool of memory from which buffers can be made. In some |
| 107 | * systems the only heap is regular system memory allocated via vmalloc. |
| 108 | * On others, some blocks might require large physically contiguous buffers |
| 109 | * that are allocated from a specially reserved heap. |
| 110 | */ |
| 111 | struct ion_heap { |
| 112 | struct rb_node node; |
| 113 | struct ion_device *dev; |
| 114 | enum ion_heap_type type; |
| 115 | struct ion_heap_ops *ops; |
| 116 | int id; |
| 117 | const char *name; |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * ion_device_create - allocates and returns an ion device |
| 122 | * @custom_ioctl: arch specific ioctl function if applicable |
| 123 | * |
| 124 | * returns a valid device or -PTR_ERR |
| 125 | */ |
| 126 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 127 | (struct ion_client *client, |
| 128 | unsigned int cmd, |
| 129 | unsigned long arg)); |
| 130 | |
| 131 | /** |
| 132 | * ion_device_destroy - free and device and it's resource |
| 133 | * @dev: the device |
| 134 | */ |
| 135 | void ion_device_destroy(struct ion_device *dev); |
| 136 | |
| 137 | /** |
| 138 | * ion_device_add_heap - adds a heap to the ion device |
| 139 | * @dev: the device |
| 140 | * @heap: the heap to add |
| 141 | */ |
| 142 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap); |
| 143 | |
| 144 | /** |
| 145 | * functions for creating and destroying the built in ion heaps. |
| 146 | * architectures can add their own custom architecture specific |
| 147 | * heaps as appropriate. |
| 148 | */ |
| 149 | |
| 150 | struct ion_heap *ion_heap_create(struct ion_platform_heap *); |
| 151 | void ion_heap_destroy(struct ion_heap *); |
| 152 | |
| 153 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *); |
| 154 | void ion_system_heap_destroy(struct ion_heap *); |
| 155 | |
| 156 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *); |
| 157 | void ion_system_contig_heap_destroy(struct ion_heap *); |
| 158 | |
| 159 | struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *); |
| 160 | void ion_carveout_heap_destroy(struct ion_heap *); |
| 161 | /** |
| 162 | * kernel api to allocate/free from carveout -- used when carveout is |
| 163 | * used to back an architecture specific custom heap |
| 164 | */ |
| 165 | ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size, |
| 166 | unsigned long align); |
| 167 | void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, |
| 168 | unsigned long size); |
| 169 | /** |
| 170 | * The carveout heap returns physical addresses, since 0 may be a valid |
| 171 | * physical address, this is used to indicate allocation failed |
| 172 | */ |
| 173 | #define ION_CARVEOUT_ALLOCATE_FAIL -1 |
| 174 | |
| 175 | #endif /* _ION_PRIV_H */ |