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