Merge tag 'lsk-v4.4-17.03-android' of git://git.linaro.org/kernel/linux-linaro-stable.git
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / linux / mali_memory_types.h
1 /*
2  * Copyright (C) 2013-2015 ARM Limited. All rights reserved.
3  * 
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  * 
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 #ifndef __MALI_MEMORY_TYPES_H__
12 #define __MALI_MEMORY_TYPES_H__
13
14 #include <linux/mm.h>
15
16 #if defined(CONFIG_MALI400_UMP)
17 #include "ump_kernel_interface.h"
18 #endif
19
20 typedef u32 mali_address_t;
21
22 typedef enum mali_mem_type {
23         MALI_MEM_OS,
24         MALI_MEM_EXTERNAL,
25         MALI_MEM_SWAP,
26         MALI_MEM_DMA_BUF,
27         MALI_MEM_UMP,
28         MALI_MEM_BLOCK,
29         MALI_MEM_COW,
30         MALI_MEM_TYPE_MAX,
31 } mali_mem_type;
32
33 typedef struct mali_block_item {
34         /* for block type, the block_phy is alway page size align
35         * so use low 12bit used for ref_cout.
36         */
37         unsigned long phy_addr;
38 } mali_block_item;
39
40 /**
41  * idx is used to locate the given page in the address space of swap file.
42  * ref_count is used to mark how many memory backends are using this item.
43  */
44 typedef struct mali_swap_item {
45         u32 idx;
46         atomic_t ref_count;
47         struct page *page;
48         dma_addr_t dma_addr;
49 } mali_swap_item;
50
51 typedef enum mali_page_node_type {
52         MALI_PAGE_NODE_OS,
53         MALI_PAGE_NODE_BLOCK,
54         MALI_PAGE_NODE_SWAP,
55 } mali_page_node_type;
56
57 typedef struct mali_page_node {
58         struct list_head list;
59         union {
60                 struct page *page;
61                 mali_block_item *blk_it; /*pointer to block item*/
62                 mali_swap_item *swap_it;
63         };
64
65         u32 type;
66 } mali_page_node;
67
68 typedef struct mali_mem_os_mem {
69         struct list_head pages;
70         u32 count;
71 } mali_mem_os_mem;
72
73 typedef struct mali_mem_dma_buf {
74 #if defined(CONFIG_DMA_SHARED_BUFFER)
75         struct mali_dma_buf_attachment *attachment;
76 #endif
77 } mali_mem_dma_buf;
78
79 typedef struct mali_mem_external {
80         dma_addr_t phys;
81         u32 size;
82 } mali_mem_external;
83
84 typedef struct mali_mem_ump {
85 #if defined(CONFIG_MALI400_UMP)
86         ump_dd_handle handle;
87 #endif
88 } mali_mem_ump;
89
90 typedef struct block_allocator_allocation {
91         /* The list will be released in reverse order */
92         struct block_info *last_allocated;
93         u32 mapping_length;
94         struct block_allocator *info;
95 } block_allocator_allocation;
96
97 typedef struct mali_mem_block_mem {
98         struct list_head pfns;
99         u32 count;
100 } mali_mem_block_mem;
101
102 typedef struct mali_mem_virt_mali_mapping {
103         mali_address_t addr; /* Virtual Mali address */
104         u32 properties;      /* MMU Permissions + cache, must match MMU HW */
105 } mali_mem_virt_mali_mapping;
106
107 typedef struct mali_mem_virt_cpu_mapping {
108         void __user *addr;
109         struct vm_area_struct *vma;
110 } mali_mem_virt_cpu_mapping;
111
112 #define MALI_MEM_ALLOCATION_VALID_MAGIC 0xdeda110c
113 #define MALI_MEM_ALLOCATION_FREED_MAGIC 0x10101010
114
115 typedef struct mali_mm_node {
116         /* MALI GPU vaddr start, use u32 for mmu only support 32bit address*/
117         uint32_t start; /* GPU vaddr */
118         uint32_t size;  /* GPU allocation virtual size */
119         unsigned allocated : 1;
120 } mali_mm_node;
121
122 typedef struct mali_vma_node {
123         struct mali_mm_node vm_node;
124         struct rb_node vm_rb;
125 } mali_vma_node;
126
127
128 typedef struct mali_mem_allocation {
129         MALI_DEBUG_CODE(u32 magic);
130         mali_mem_type type;                /**< Type of memory */
131         u32 flags;                         /**< Flags for this allocation */
132
133         struct mali_session_data *session; /**< Pointer to session that owns the allocation */
134
135         mali_mem_virt_cpu_mapping cpu_mapping; /**< CPU mapping */
136         mali_mem_virt_mali_mapping mali_mapping; /**< Mali mapping */
137
138         /* add for new memory system */
139         struct mali_vma_node mali_vma_node;
140         u32 vsize; /* virtual size*/
141         u32 psize; /* physical backend memory size*/
142         struct list_head list;
143         s32 backend_handle; /* idr for mem_backend */
144         _mali_osk_atomic_t mem_alloc_refcount;
145 } mali_mem_allocation;
146
147 struct mali_mem_os_allocator {
148         spinlock_t pool_lock;
149         struct list_head pool_pages;
150         size_t pool_count;
151
152         atomic_t allocated_pages;
153         size_t allocation_limit;
154
155         struct shrinker shrinker;
156         struct delayed_work timed_shrinker;
157         struct workqueue_struct *wq;
158 };
159
160 /* COW backend memory type */
161 typedef struct mali_mem_cow {
162         struct list_head pages;  /**< all pages for this cow backend allocation,
163                                                                 including new allocated pages for modified range*/
164         u32 count;               /**< number of pages */
165         s32 change_pages_nr;
166 } mali_mem_cow;
167
168 typedef struct mali_mem_swap {
169         struct list_head pages;
170         u32 count;
171 } mali_mem_swap;
172
173 #define MALI_MEM_BACKEND_FLAG_COWED                   (0x1)  /* COW has happen on this backend */
174 #define MALI_MEM_BACKEND_FLAG_COW_CPU_NO_WRITE        (0x2)  /* This is an COW backend, mapped as not allowed cpu to write */
175 #define MALI_MEM_BACKEND_FLAG_SWAP_COWED              (0x4)  /* Mark the given backend is cowed from swappable memory. */
176 /* Mark this backend is not swapped_in in MALI driver, and before using it,
177  * we should swap it in and set up corresponding page table. */
178 #define MALI_MEM_BACKEND_FLAG_UNSWAPPED_IN            (0x8)
179 #define MALI_MEM_BACKEND_FLAG_NOT_BINDED              (0x1 << 5) /* this backend it not back with physical memory, used for defer bind */
180 #define MALI_MEM_BACKEND_FLAG_BINDED              (0x1 << 6) /* this backend it back with physical memory, used for defer bind */
181
182 typedef struct mali_mem_backend {
183         mali_mem_type type;                /**< Type of backend memory */
184         u32 flags;                         /**< Flags for this allocation */
185         u32 size;
186         /* Union selected by type. */
187         union {
188                 mali_mem_os_mem os_mem;       /**< MALI_MEM_OS */
189                 mali_mem_external ext_mem;    /**< MALI_MEM_EXTERNAL */
190                 mali_mem_dma_buf dma_buf;     /**< MALI_MEM_DMA_BUF */
191                 mali_mem_ump ump_mem;         /**< MALI_MEM_UMP */
192                 mali_mem_block_mem block_mem; /**< MALI_MEM_BLOCK */
193                 mali_mem_cow cow_mem;
194                 mali_mem_swap swap_mem;
195         };
196         mali_mem_allocation *mali_allocation;
197         struct mutex mutex;
198         mali_mem_type cow_type;
199
200         struct list_head list;           /**< Used to link swappable memory backend to the global swappable list */
201         int using_count;                 /**< Mark how many PP jobs are using this memory backend */
202         u32 start_idx;                   /**< If the correspondign vma of this backend is linear, this value will be used to set vma->vm_pgoff */
203 } mali_mem_backend;
204
205 #define MALI_MEM_FLAG_MALI_GUARD_PAGE (_MALI_MAP_EXTERNAL_MAP_GUARD_PAGE)
206 #define MALI_MEM_FLAG_DONT_CPU_MAP    (1 << 1)
207 #define MALI_MEM_FLAG_CAN_RESIZE  (_MALI_MEMORY_ALLOCATE_RESIZEABLE)
208 #endif /* __MALI_MEMORY_TYPES__ */