diff options
Diffstat (limited to 'drivers/cenalloc/cenalloc_priv.h')
-rw-r--r-- | drivers/cenalloc/cenalloc_priv.h | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/drivers/cenalloc/cenalloc_priv.h b/drivers/cenalloc/cenalloc_priv.h new file mode 100644 index 000000000000..31f5e5941e0c --- /dev/null +++ b/drivers/cenalloc/cenalloc_priv.h @@ -0,0 +1,188 @@ +/* + * Private header file for allocator helper framework for constraints-aware + * dma-buf backing storage allocation. + * + * Copyright(C) 2014 Linaro Limited. All rights reserved. + * Author: Sumit Semwal <sumit.semwal@linaro.org> + * + * Structure for management of clients, buffers etc heavily derived from + * Android's ION framework. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CENALLOC_PRIV_H_ +#define CENALLOC_PRIV_H_ + +#include <linux/device.h> +#include <linux/dma-direction.h> +#include <linux/kref.h> +#include <linux/mm_types.h> +#include <linux/mutex.h> +#include <linux/rbtree.h> +#include <linux/sched.h> +#include <linux/types.h> + +#include "cenalloc.h" + +/** + * TODO: Find a better way to generate and manage allocator IDs + * For now, define a couple of allocator types + * + * @CENALLOC_ALLOCATOR_SYSTEM: eg memory allocated via vmalloc + * @CENALLOC_ALLOCATOR_SYSTEM_CONTIG: eg memory allocated via kmalloc + * @CENALLOC_NUM_ALLOCATORS: helper for iterating over Allocators + * a bit mask is used to identify the + * allocators, so only 32 total allocator + * types are supported + */ +#define CENALLOC_ALLOCATOR_SYSTEM 0x1 +#define CENALLOC_ALLOCATOR_SYSTEM_CONTIG (CENALLOC_ALLOCATOR_SYSTEM + 1) + +#define CENALLOC_NUM_ALLOCATORS 32 + +#define CENALLOC_ALLOCATOR_SYSTEM_MASK (1 << CENALLOC_ALLOCATOR_SYSTEM) +#define CENALLOC_ALLOCATOR_SYSTEM_CONTIG_MASK \ + (1 << CENALLOC_ALLOCATOR_SYSTEM_CONTIG) + + +struct cenalloc_device; +/** + * struct cenalloc_buffer - metadata for a particular buffer + * @node: node in the cenalloc_device buffers tree + * @dev: back pointer to the cenalloc_device + * @allocator: back pointer to the allocator the buffer came from + * @flags: buffer specific flags + * @private_flags: internal buffer specific flags + * @size: size of the buffer + * @priv_virt: private data to the buffer representable as + * a void * + * @lock: protects the buffers cnt fields + * @pages: flat array of pages in the buffer -- used by fault + * handler and only valid for buffers that are faulted in + * @vmas: list of vma's mapping this buffer + * @sg_table: the sg table for the buffer if dmap_cnt is not zero + * @handle_count: count of handles referencing this buffer +*/ +struct cenalloc_buffer { + struct rb_node node; + struct cenalloc_device *dev; + struct cenalloc_allocator *allocator; + unsigned long flags; + unsigned long align; + unsigned long private_flags; + + size_t size; + + struct kref ref; + unsigned int kmap_cnt; + struct mutex lock; + void *vaddr; + struct page **pages; + struct list_head vmas; + + struct sg_table *sg_table; + struct dma_buf *dmabuf; +}; + +/** + * struct cenalloc_allocator_ops - ops to operate on a given allocator + * @allocate: allocate memory + * @free: free memory + * @phys: get physical address of a buffer (only define on + * physically contiguous allocators) + * @map_dma: map the memory for dma to a scatterlist + * @unmap_dma: unmap the memory for dma + * @map_kernel: map memory to the kernel + * @unmap_kernel: unmap memory to the kernel + * @map_user: map memory to userspace + * @sync_for_device: sync the memory for device + * + * allocate, phys, and map_user return 0 on success, -errno on error. + * map_dma and map_kernel return pointer on success, ERR_PTR on + * error. + */ +struct cenalloc_allocator_ops { + int (*allocate)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer, unsigned long len, + unsigned long align, unsigned long flags); + void (*free)(struct cenalloc_buffer *buffer); + int (*phys)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer, + phys_addr_t *addr, size_t *len); + struct sg_table * (*map_dma)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer); + void (*unmap_dma)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer); + void * (*map_kernel)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer); + void (*unmap_kernel)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer); + int (*map_user)(struct cenalloc_allocator *mapper, + struct cenalloc_buffer *buffer, + struct vm_area_struct *vma); + + void (*sync_for_device)(struct cenalloc_allocator *allocator, + struct cenalloc_buffer *buffer, + struct device *dev, + enum dma_data_direction dir); +}; + +/** + * struct cenalloc_allocator - represents a allocator in the system + * @node: rb node to put the allocator on the device's tree of + * allocators + * @dev: back pointer to the cenalloc_device + * @type: type of allocator + * @ops: ops struct as above + * @flags: flags + * @id: id of allocator, also indicates priority of this + * allocator when allocating. These MUST be unique. + * @name: used for debugging + * @debug_show: called when allocator debug file is read to add any + * allocator specific debug info to output + * + * Represents a pool of memory from which buffers can be allocated. In some + * systems the only allocator is regular system memory allocated via vmalloc. + * On others, some blocks might require large physically contiguous buffers + * that are allocated from a specially reserved allocator. + */ +struct cenalloc_allocator { + struct plist_node node; + struct cenalloc_device *dev; + unsigned long type; + struct cenalloc_allocator_ops *ops; + struct vm_operations_struct *vma_ops; + unsigned long flags; + unsigned int id; + const char *name; + + int (*debug_show)(struct cenalloc_allocator *allocator, + struct seq_file *, void *); +}; + +/** + * cenalloc_device_add_allocator - adds an allocator to the cenalloc device + * @dev: the device + * @allocator: the allocator to add + */ +void cenalloc_device_add_allocator(struct cenalloc_device *dev, + struct cenalloc_allocator *allocator); + +/** + * TODO: add some helpers for common allocator operations on buffers; + * some candidates might be: + * cenalloc_allocator_{map_kernel, unmap_kernel, map_user} + * some vma_* management operations + */ + +#endif /* CENALLOC_PRIV_H_ */ |