1 /*
2  *
3  * (C) COPYRIGHT 2010-2017 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15 
16 
17 
18 
19 
20 /**
21  * @file mali_kbase_mem.h
22  * Base kernel memory APIs
23  */
24 
25 #ifndef _KBASE_MEM_H_
26 #define _KBASE_MEM_H_
27 
28 #ifndef _KBASE_H_
29 #error "Don't include this file directly, use mali_kbase.h instead"
30 #endif
31 
32 #include <linux/kref.h>
33 #ifdef CONFIG_KDS
34 #include <linux/kds.h>
35 #endif				/* CONFIG_KDS */
36 #ifdef CONFIG_UMP
37 #include <linux/ump.h>
38 #endif				/* CONFIG_UMP */
39 #include "mali_base_kernel.h"
40 #include <mali_kbase_hw.h>
41 #include "mali_kbase_pm.h"
42 #include "mali_kbase_defs.h"
43 #if defined(CONFIG_MALI_GATOR_SUPPORT)
44 #include "mali_kbase_gator.h"
45 #endif
46 /* Required for kbase_mem_evictable_unmake */
47 #include "mali_kbase_mem_linux.h"
48 
49 /* Part of the workaround for uTLB invalid pages is to ensure we grow/shrink tmem by 4 pages at a time */
50 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316 (2)	/* round to 4 pages */
51 
52 /* Part of the workaround for PRLAM-9630 requires us to grow/shrink memory by 8 pages.
53 The MMU reads in 8 page table entries from memory at a time, if we have more than one page fault within the same 8 pages and
54 page tables are updated accordingly, the MMU does not re-read the page table entries from memory for the subsequent page table
55 updates and generates duplicate page faults as the page table information used by the MMU is not valid.   */
56 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630 (3)	/* round to 8 pages */
57 
58 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2 (0)	/* round to 1 page */
59 
60 /* This must always be a power of 2 */
61 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2)
62 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_8316 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316)
63 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_9630 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630)
64 /**
65  * A CPU mapping
66  */
67 struct kbase_cpu_mapping {
68 	struct   list_head mappings_list;
69 	struct   kbase_mem_phy_alloc *alloc;
70 	struct   kbase_context *kctx;
71 	struct   kbase_va_region *region;
72 	int      count;
73 	int      free_on_close;
74 };
75 
76 enum kbase_memory_type {
77 	KBASE_MEM_TYPE_NATIVE,
78 	KBASE_MEM_TYPE_IMPORTED_UMP,
79 	KBASE_MEM_TYPE_IMPORTED_UMM,
80 	KBASE_MEM_TYPE_IMPORTED_USER_BUF,
81 	KBASE_MEM_TYPE_ALIAS,
82 	KBASE_MEM_TYPE_TB,
83 	KBASE_MEM_TYPE_RAW
84 };
85 
86 /* internal structure, mirroring base_mem_aliasing_info,
87  * but with alloc instead of a gpu va (handle) */
88 struct kbase_aliased {
89 	struct kbase_mem_phy_alloc *alloc; /* NULL for special, non-NULL for native */
90 	u64 offset; /* in pages */
91 	u64 length; /* in pages */
92 };
93 
94 /**
95  * @brief Physical pages tracking object properties
96   */
97 #define KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED  (1ul << 0)
98 #define KBASE_MEM_PHY_ALLOC_LARGE            (1ul << 1)
99 
100 /* physical pages tracking object.
101  * Set up to track N pages.
102  * N not stored here, the creator holds that info.
103  * This object only tracks how many elements are actually valid (present).
104  * Changing of nents or *pages should only happen if the kbase_mem_phy_alloc is not
105  * shared with another region or client. CPU mappings are OK to exist when changing, as
106  * long as the tracked mappings objects are updated as part of the change.
107  */
108 struct kbase_mem_phy_alloc {
109 	struct kref           kref; /* number of users of this alloc */
110 	atomic_t              gpu_mappings;
111 	size_t                nents; /* 0..N */
112 	phys_addr_t           *pages; /* N elements, only 0..nents are valid */
113 
114 	/* kbase_cpu_mappings */
115 	struct list_head      mappings;
116 
117 	/* Node used to store this allocation on the eviction list */
118 	struct list_head      evict_node;
119 	/* Physical backing size when the pages where evicted */
120 	size_t                evicted;
121 	/*
122 	 * Back reference to the region structure which created this
123 	 * allocation, or NULL if it has been freed.
124 	 */
125 	struct kbase_va_region *reg;
126 
127 	/* type of buffer */
128 	enum kbase_memory_type type;
129 
130 	unsigned long properties;
131 
132 	/* member in union valid based on @a type */
133 	union {
134 #ifdef CONFIG_UMP
135 		ump_dd_handle ump_handle;
136 #endif /* CONFIG_UMP */
137 #if defined(CONFIG_DMA_SHARED_BUFFER)
138 		struct {
139 			struct dma_buf *dma_buf;
140 			struct dma_buf_attachment *dma_attachment;
141 			unsigned int current_mapping_usage_count;
142 			struct sg_table *sgt;
143 		} umm;
144 #endif /* defined(CONFIG_DMA_SHARED_BUFFER) */
145 		struct {
146 			u64 stride;
147 			size_t nents;
148 			struct kbase_aliased *aliased;
149 		} alias;
150 		/* Used by type = (KBASE_MEM_TYPE_NATIVE, KBASE_MEM_TYPE_TB) */
151 		struct kbase_context *kctx;
152 		struct kbase_alloc_import_user_buf {
153 			unsigned long address;
154 			unsigned long size;
155 			unsigned long nr_pages;
156 			struct page **pages;
157 			/* top bit (1<<31) of current_mapping_usage_count
158 			 * specifies that this import was pinned on import
159 			 * See PINNED_ON_IMPORT
160 			 */
161 			u32 current_mapping_usage_count;
162 			struct mm_struct *mm;
163 			dma_addr_t *dma_addrs;
164 		} user_buf;
165 	} imported;
166 };
167 
168 /* The top bit of kbase_alloc_import_user_buf::current_mapping_usage_count is
169  * used to signify that a buffer was pinned when it was imported. Since the
170  * reference count is limited by the number of atoms that can be submitted at
171  * once there should be no danger of overflowing into this bit.
172  * Stealing the top bit also has the benefit that
173  * current_mapping_usage_count != 0 if and only if the buffer is mapped.
174  */
175 #define PINNED_ON_IMPORT	(1<<31)
176 
kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc *alloc)177 static inline void kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc *alloc)
178 {
179 	KBASE_DEBUG_ASSERT(alloc);
180 	/* we only track mappings of NATIVE buffers */
181 	if (alloc->type == KBASE_MEM_TYPE_NATIVE)
182 		atomic_inc(&alloc->gpu_mappings);
183 }
184 
kbase_mem_phy_alloc_gpu_unmapped(struct kbase_mem_phy_alloc *alloc)185 static inline void kbase_mem_phy_alloc_gpu_unmapped(struct kbase_mem_phy_alloc *alloc)
186 {
187 	KBASE_DEBUG_ASSERT(alloc);
188 	/* we only track mappings of NATIVE buffers */
189 	if (alloc->type == KBASE_MEM_TYPE_NATIVE)
190 		if (0 > atomic_dec_return(&alloc->gpu_mappings)) {
191 			pr_err("Mismatched %s:\n", __func__);
192 			dump_stack();
193 		}
194 }
195 
196 void kbase_mem_kref_free(struct kref *kref);
197 
198 int kbase_mem_init(struct kbase_device *kbdev);
199 void kbase_mem_halt(struct kbase_device *kbdev);
200 void kbase_mem_term(struct kbase_device *kbdev);
201 
kbase_mem_phy_alloc_get(struct kbase_mem_phy_alloc *alloc)202 static inline struct kbase_mem_phy_alloc *kbase_mem_phy_alloc_get(struct kbase_mem_phy_alloc *alloc)
203 {
204 	kref_get(&alloc->kref);
205 	return alloc;
206 }
207 
kbase_mem_phy_alloc_put(struct kbase_mem_phy_alloc *alloc)208 static inline struct kbase_mem_phy_alloc *kbase_mem_phy_alloc_put(struct kbase_mem_phy_alloc *alloc)
209 {
210 	kref_put(&alloc->kref, kbase_mem_kref_free);
211 	return NULL;
212 }
213 
214 /**
215  * A GPU memory region, and attributes for CPU mappings.
216  */
217 struct kbase_va_region {
218 	struct rb_node rblink;
219 	struct list_head link;
220 
221 	struct kbase_context *kctx;	/* Backlink to base context */
222 
223 	u64 start_pfn;		/* The PFN in GPU space */
224 	size_t nr_pages;
225 
226 /* Free region */
227 #define KBASE_REG_FREE              (1ul << 0)
228 /* CPU write access */
229 #define KBASE_REG_CPU_WR            (1ul << 1)
230 /* GPU write access */
231 #define KBASE_REG_GPU_WR            (1ul << 2)
232 /* No eXecute flag */
233 #define KBASE_REG_GPU_NX            (1ul << 3)
234 /* Is CPU cached? */
235 #define KBASE_REG_CPU_CACHED        (1ul << 4)
236 /* Is GPU cached? */
237 #define KBASE_REG_GPU_CACHED        (1ul << 5)
238 
239 #define KBASE_REG_GROWABLE          (1ul << 6)
240 /* Can grow on pf? */
241 #define KBASE_REG_PF_GROW           (1ul << 7)
242 
243 /* VA managed by us */
244 #define KBASE_REG_CUSTOM_VA         (1ul << 8)
245 
246 /* inner shareable coherency */
247 #define KBASE_REG_SHARE_IN          (1ul << 9)
248 /* inner & outer shareable coherency */
249 #define KBASE_REG_SHARE_BOTH        (1ul << 10)
250 
251 /* Space for 4 different zones */
252 #define KBASE_REG_ZONE_MASK         (3ul << 11)
253 #define KBASE_REG_ZONE(x)           (((x) & 3) << 11)
254 
255 /* GPU read access */
256 #define KBASE_REG_GPU_RD            (1ul<<13)
257 /* CPU read access */
258 #define KBASE_REG_CPU_RD            (1ul<<14)
259 
260 /* Index of chosen MEMATTR for this region (0..7) */
261 #define KBASE_REG_MEMATTR_MASK      (7ul << 16)
262 #define KBASE_REG_MEMATTR_INDEX(x)  (((x) & 7) << 16)
263 #define KBASE_REG_MEMATTR_VALUE(x)  (((x) & KBASE_REG_MEMATTR_MASK) >> 16)
264 
265 #define KBASE_REG_SECURE            (1ul << 19)
266 
267 #define KBASE_REG_DONT_NEED         (1ul << 20)
268 
269 /* Imported buffer is padded? */
270 #define KBASE_REG_IMPORT_PAD        (1ul << 21)
271 
272 #define KBASE_REG_ZONE_SAME_VA      KBASE_REG_ZONE(0)
273 
274 /* only used with 32-bit clients */
275 /*
276  * On a 32bit platform, custom VA should be wired from (4GB + shader region)
277  * to the VA limit of the GPU. Unfortunately, the Linux mmap() interface
278  * limits us to 2^32 pages (2^44 bytes, see mmap64 man page for reference).
279  * So we put the default limit to the maximum possible on Linux and shrink
280  * it down, if required by the GPU, during initialization.
281  */
282 
283 /*
284  * Dedicated 16MB region for shader code:
285  * VA range 0x101000000-0x102000000
286  */
287 #define KBASE_REG_ZONE_EXEC         KBASE_REG_ZONE(1)
288 #define KBASE_REG_ZONE_EXEC_BASE    (0x101000000ULL >> PAGE_SHIFT)
289 #define KBASE_REG_ZONE_EXEC_SIZE    ((16ULL * 1024 * 1024) >> PAGE_SHIFT)
290 
291 #define KBASE_REG_ZONE_CUSTOM_VA         KBASE_REG_ZONE(2)
292 #define KBASE_REG_ZONE_CUSTOM_VA_BASE    (KBASE_REG_ZONE_EXEC_BASE + KBASE_REG_ZONE_EXEC_SIZE) /* Starting after KBASE_REG_ZONE_EXEC */
293 #define KBASE_REG_ZONE_CUSTOM_VA_SIZE    (((1ULL << 44) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE)
294 /* end 32-bit clients only */
295 
296 	unsigned long flags;
297 
298 	size_t extent; /* nr of pages alloc'd on PF */
299 
300 	struct kbase_mem_phy_alloc *cpu_alloc; /* the one alloc object we mmap to the CPU when mapping this region */
301 	struct kbase_mem_phy_alloc *gpu_alloc; /* the one alloc object we mmap to the GPU when mapping this region */
302 
303 	/* non-NULL if this memory object is a kds_resource */
304 	struct kds_resource *kds_res;
305 
306 	/* List head used to store the region in the JIT allocation pool */
307 	struct list_head jit_node;
308 };
309 
310 /* Common functions */
kbase_get_cpu_phy_pages(struct kbase_va_region *reg)311 static inline phys_addr_t *kbase_get_cpu_phy_pages(struct kbase_va_region *reg)
312 {
313 	KBASE_DEBUG_ASSERT(reg);
314 	KBASE_DEBUG_ASSERT(reg->cpu_alloc);
315 	KBASE_DEBUG_ASSERT(reg->gpu_alloc);
316 	KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
317 
318 	return reg->cpu_alloc->pages;
319 }
320 
kbase_get_gpu_phy_pages(struct kbase_va_region *reg)321 static inline phys_addr_t *kbase_get_gpu_phy_pages(struct kbase_va_region *reg)
322 {
323 	KBASE_DEBUG_ASSERT(reg);
324 	KBASE_DEBUG_ASSERT(reg->cpu_alloc);
325 	KBASE_DEBUG_ASSERT(reg->gpu_alloc);
326 	KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
327 
328 	return reg->gpu_alloc->pages;
329 }
330 
kbase_reg_current_backed_size(struct kbase_va_region *reg)331 static inline size_t kbase_reg_current_backed_size(struct kbase_va_region *reg)
332 {
333 	KBASE_DEBUG_ASSERT(reg);
334 	/* if no alloc object the backed size naturally is 0 */
335 	if (!reg->cpu_alloc)
336 		return 0;
337 
338 	KBASE_DEBUG_ASSERT(reg->cpu_alloc);
339 	KBASE_DEBUG_ASSERT(reg->gpu_alloc);
340 	KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
341 
342 	return reg->cpu_alloc->nents;
343 }
344 
345 #define KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD ((size_t)(4*1024)) /* size above which vmalloc is used over kmalloc */
346 
kbase_alloc_create(size_t nr_pages, enum kbase_memory_type type)347 static inline struct kbase_mem_phy_alloc *kbase_alloc_create(size_t nr_pages, enum kbase_memory_type type)
348 {
349 	struct kbase_mem_phy_alloc *alloc;
350 	size_t alloc_size = sizeof(*alloc) + sizeof(*alloc->pages) * nr_pages;
351 	size_t per_page_size = sizeof(*alloc->pages);
352 
353 	/* Imported pages may have page private data already in use */
354 	if (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF) {
355 		alloc_size += nr_pages *
356 				sizeof(*alloc->imported.user_buf.dma_addrs);
357 		per_page_size += sizeof(*alloc->imported.user_buf.dma_addrs);
358 	}
359 
360 	/*
361 	 * Prevent nr_pages*per_page_size + sizeof(*alloc) from
362 	 * wrapping around.
363 	 */
364 	if (nr_pages > ((((size_t) -1) - sizeof(*alloc))
365 			/ per_page_size))
366 		return ERR_PTR(-ENOMEM);
367 
368 	/* Allocate based on the size to reduce internal fragmentation of vmem */
369 	if (alloc_size > KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD)
370 		alloc = vzalloc(alloc_size);
371 	else
372 		alloc = kzalloc(alloc_size, GFP_KERNEL);
373 
374 	if (!alloc)
375 		return ERR_PTR(-ENOMEM);
376 
377 	/* Store allocation method */
378 	if (alloc_size > KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD)
379 		alloc->properties |= KBASE_MEM_PHY_ALLOC_LARGE;
380 
381 	kref_init(&alloc->kref);
382 	atomic_set(&alloc->gpu_mappings, 0);
383 	alloc->nents = 0;
384 	alloc->pages = (void *)(alloc + 1);
385 	INIT_LIST_HEAD(&alloc->mappings);
386 	alloc->type = type;
387 
388 	if (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF)
389 		alloc->imported.user_buf.dma_addrs =
390 				(void *) (alloc->pages + nr_pages);
391 
392 	return alloc;
393 }
394 
kbase_reg_prepare_native(struct kbase_va_region *reg, struct kbase_context *kctx)395 static inline int kbase_reg_prepare_native(struct kbase_va_region *reg,
396 		struct kbase_context *kctx)
397 {
398 	KBASE_DEBUG_ASSERT(reg);
399 	KBASE_DEBUG_ASSERT(!reg->cpu_alloc);
400 	KBASE_DEBUG_ASSERT(!reg->gpu_alloc);
401 	KBASE_DEBUG_ASSERT(reg->flags & KBASE_REG_FREE);
402 
403 	reg->cpu_alloc = kbase_alloc_create(reg->nr_pages,
404 			KBASE_MEM_TYPE_NATIVE);
405 	if (IS_ERR(reg->cpu_alloc))
406 		return PTR_ERR(reg->cpu_alloc);
407 	else if (!reg->cpu_alloc)
408 		return -ENOMEM;
409 	reg->cpu_alloc->imported.kctx = kctx;
410 	INIT_LIST_HEAD(&reg->cpu_alloc->evict_node);
411 	if (kbase_ctx_flag(kctx, KCTX_INFINITE_CACHE)
412 	    && (reg->flags & KBASE_REG_CPU_CACHED)) {
413 		reg->gpu_alloc = kbase_alloc_create(reg->nr_pages,
414 				KBASE_MEM_TYPE_NATIVE);
415 		reg->gpu_alloc->imported.kctx = kctx;
416 		INIT_LIST_HEAD(&reg->gpu_alloc->evict_node);
417 	} else {
418 		reg->gpu_alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
419 	}
420 
421 	INIT_LIST_HEAD(&reg->jit_node);
422 	reg->flags &= ~KBASE_REG_FREE;
423 	return 0;
424 }
425 
kbase_atomic_add_pages(int num_pages, atomic_t *used_pages)426 static inline int kbase_atomic_add_pages(int num_pages, atomic_t *used_pages)
427 {
428 	int new_val = atomic_add_return(num_pages, used_pages);
429 #if defined(CONFIG_MALI_GATOR_SUPPORT)
430 	kbase_trace_mali_total_alloc_pages_change((long long int)new_val);
431 #endif
432 	return new_val;
433 }
434 
kbase_atomic_sub_pages(int num_pages, atomic_t *used_pages)435 static inline int kbase_atomic_sub_pages(int num_pages, atomic_t *used_pages)
436 {
437 	int new_val = atomic_sub_return(num_pages, used_pages);
438 #if defined(CONFIG_MALI_GATOR_SUPPORT)
439 	kbase_trace_mali_total_alloc_pages_change((long long int)new_val);
440 #endif
441 	return new_val;
442 }
443 
444 /*
445  * Max size for kbdev memory pool (in pages)
446  */
447 #define KBASE_MEM_POOL_MAX_SIZE_KBDEV (SZ_64M >> PAGE_SHIFT)
448 
449 /*
450  * Max size for kctx memory pool (in pages)
451  */
452 #define KBASE_MEM_POOL_MAX_SIZE_KCTX  (SZ_64M >> PAGE_SHIFT)
453 
454 /**
455  * kbase_mem_pool_init - Create a memory pool for a kbase device
456  * @pool:      Memory pool to initialize
457  * @max_size:  Maximum number of free pages the pool can hold
458  * @kbdev:     Kbase device where memory is used
459  * @next_pool: Pointer to the next pool or NULL.
460  *
461  * Allocations from @pool are in whole pages. Each @pool has a free list where
462  * pages can be quickly allocated from. The free list is initially empty and
463  * filled whenever pages are freed back to the pool. The number of free pages
464  * in the pool will in general not exceed @max_size, but the pool may in
465  * certain corner cases grow above @max_size.
466  *
467  * If @next_pool is not NULL, we will allocate from @next_pool before going to
468  * the kernel allocator. Similarily pages can spill over to @next_pool when
469  * @pool is full. Pages are zeroed before they spill over to another pool, to
470  * prevent leaking information between applications.
471  *
472  * A shrinker is registered so that Linux mm can reclaim pages from the pool as
473  * needed.
474  *
475  * Return: 0 on success, negative -errno on error
476  */
477 int kbase_mem_pool_init(struct kbase_mem_pool *pool,
478 		size_t max_size,
479 		struct kbase_device *kbdev,
480 		struct kbase_mem_pool *next_pool);
481 
482 /**
483  * kbase_mem_pool_term - Destroy a memory pool
484  * @pool:  Memory pool to destroy
485  *
486  * Pages in the pool will spill over to @next_pool (if available) or freed to
487  * the kernel.
488  */
489 void kbase_mem_pool_term(struct kbase_mem_pool *pool);
490 
491 /**
492  * kbase_mem_pool_alloc - Allocate a page from memory pool
493  * @pool:  Memory pool to allocate from
494  *
495  * Allocations from the pool are made as follows:
496  * 1. If there are free pages in the pool, allocate a page from @pool.
497  * 2. Otherwise, if @next_pool is not NULL and has free pages, allocate a page
498  *    from @next_pool.
499  * 3. Return NULL if no memory in the pool
500  *
501  * Return: Pointer to allocated page, or NULL if allocation failed.
502  */
503 struct page *kbase_mem_pool_alloc(struct kbase_mem_pool *pool);
504 
505 /**
506  * kbase_mem_pool_free - Free a page to memory pool
507  * @pool:  Memory pool where page should be freed
508  * @page:  Page to free to the pool
509  * @dirty: Whether some of the page may be dirty in the cache.
510  *
511  * Pages are freed to the pool as follows:
512  * 1. If @pool is not full, add @page to @pool.
513  * 2. Otherwise, if @next_pool is not NULL and not full, add @page to
514  *    @next_pool.
515  * 3. Finally, free @page to the kernel.
516  */
517 void kbase_mem_pool_free(struct kbase_mem_pool *pool, struct page *page,
518 		bool dirty);
519 
520 /**
521  * kbase_mem_pool_alloc_pages - Allocate pages from memory pool
522  * @pool:     Memory pool to allocate from
523  * @nr_pages: Number of pages to allocate
524  * @pages:    Pointer to array where the physical address of the allocated
525  *            pages will be stored.
526  *
527  * Like kbase_mem_pool_alloc() but optimized for allocating many pages.
528  *
529  * Return: 0 on success, negative -errno on error
530  */
531 int kbase_mem_pool_alloc_pages(struct kbase_mem_pool *pool, size_t nr_pages,
532 		phys_addr_t *pages);
533 
534 /**
535  * kbase_mem_pool_free_pages - Free pages to memory pool
536  * @pool:     Memory pool where pages should be freed
537  * @nr_pages: Number of pages to free
538  * @pages:    Pointer to array holding the physical addresses of the pages to
539  *            free.
540  * @dirty:    Whether any pages may be dirty in the cache.
541  * @reclaimed: Whether the pages where reclaimable and thus should bypass
542  *             the pool and go straight to the kernel.
543  *
544  * Like kbase_mem_pool_free() but optimized for freeing many pages.
545  */
546 void kbase_mem_pool_free_pages(struct kbase_mem_pool *pool, size_t nr_pages,
547 		phys_addr_t *pages, bool dirty, bool reclaimed);
548 
549 /**
550  * kbase_mem_pool_size - Get number of free pages in memory pool
551  * @pool:  Memory pool to inspect
552  *
553  * Note: the size of the pool may in certain corner cases exceed @max_size!
554  *
555  * Return: Number of free pages in the pool
556  */
kbase_mem_pool_size(struct kbase_mem_pool *pool)557 static inline size_t kbase_mem_pool_size(struct kbase_mem_pool *pool)
558 {
559 	return READ_ONCE(pool->cur_size);
560 }
561 
562 /**
563  * kbase_mem_pool_max_size - Get maximum number of free pages in memory pool
564  * @pool:  Memory pool to inspect
565  *
566  * Return: Maximum number of free pages in the pool
567  */
kbase_mem_pool_max_size(struct kbase_mem_pool *pool)568 static inline size_t kbase_mem_pool_max_size(struct kbase_mem_pool *pool)
569 {
570 	return pool->max_size;
571 }
572 
573 
574 /**
575  * kbase_mem_pool_set_max_size - Set maximum number of free pages in memory pool
576  * @pool:     Memory pool to inspect
577  * @max_size: Maximum number of free pages the pool can hold
578  *
579  * If @max_size is reduced, the pool will be shrunk to adhere to the new limit.
580  * For details see kbase_mem_pool_shrink().
581  */
582 void kbase_mem_pool_set_max_size(struct kbase_mem_pool *pool, size_t max_size);
583 
584 /**
585  * kbase_mem_pool_grow - Grow the pool
586  * @pool:       Memory pool to grow
587  * @nr_to_grow: Number of pages to add to the pool
588  *
589  * Adds @nr_to_grow pages to the pool. Note that this may cause the pool to
590  * become larger than the maximum size specified.
591  *
592  * Returns: 0 on success, -ENOMEM if unable to allocate sufficent pages
593  */
594 int kbase_mem_pool_grow(struct kbase_mem_pool *pool, size_t nr_to_grow);
595 
596 /**
597  * kbase_mem_pool_trim - Grow or shrink the pool to a new size
598  * @pool:     Memory pool to trim
599  * @new_size: New number of pages in the pool
600  *
601  * If @new_size > @cur_size, fill the pool with new pages from the kernel, but
602  * not above the max_size for the pool.
603  * If @new_size < @cur_size, shrink the pool by freeing pages to the kernel.
604  */
605 void kbase_mem_pool_trim(struct kbase_mem_pool *pool, size_t new_size);
606 
607 /*
608  * kbase_mem_alloc_page - Allocate a new page for a device
609  * @kbdev: The kbase device
610  *
611  * Most uses should use kbase_mem_pool_alloc to allocate a page. However that
612  * function can fail in the event the pool is empty.
613  *
614  * Return: A new page or NULL if no memory
615  */
616 struct page *kbase_mem_alloc_page(struct kbase_device *kbdev);
617 
618 int kbase_region_tracker_init(struct kbase_context *kctx);
619 int kbase_region_tracker_init_jit(struct kbase_context *kctx, u64 jit_va_pages);
620 void kbase_region_tracker_term(struct kbase_context *kctx);
621 
622 struct kbase_va_region *kbase_region_tracker_find_region_enclosing_address(struct kbase_context *kctx, u64 gpu_addr);
623 
624 /**
625  * @brief Check that a pointer is actually a valid region.
626  *
627  * Must be called with context lock held.
628  */
629 struct kbase_va_region *kbase_region_tracker_find_region_base_address(struct kbase_context *kctx, u64 gpu_addr);
630 
631 struct kbase_va_region *kbase_alloc_free_region(struct kbase_context *kctx, u64 start_pfn, size_t nr_pages, int zone);
632 void kbase_free_alloced_region(struct kbase_va_region *reg);
633 int kbase_add_va_region(struct kbase_context *kctx, struct kbase_va_region *reg, u64 addr, size_t nr_pages, size_t align);
634 
635 bool kbase_check_alloc_flags(unsigned long flags);
636 bool kbase_check_import_flags(unsigned long flags);
637 
638 /**
639  * kbase_update_region_flags - Convert user space flags to kernel region flags
640  *
641  * @kctx:  kbase context
642  * @reg:   The region to update the flags on
643  * @flags: The flags passed from user space
644  *
645  * The user space flag BASE_MEM_COHERENT_SYSTEM_REQUIRED will be rejected and
646  * this function will fail if the system does not support system coherency.
647  *
648  * Return: 0 if successful, -EINVAL if the flags are not supported
649  */
650 int kbase_update_region_flags(struct kbase_context *kctx,
651 		struct kbase_va_region *reg, unsigned long flags);
652 
653 void kbase_gpu_vm_lock(struct kbase_context *kctx);
654 void kbase_gpu_vm_unlock(struct kbase_context *kctx);
655 
656 int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size);
657 
658 int kbase_mmu_init(struct kbase_context *kctx);
659 void kbase_mmu_term(struct kbase_context *kctx);
660 
661 phys_addr_t kbase_mmu_alloc_pgd(struct kbase_context *kctx);
662 void kbase_mmu_free_pgd(struct kbase_context *kctx);
663 int kbase_mmu_insert_pages_no_flush(struct kbase_context *kctx, u64 vpfn,
664 				  phys_addr_t *phys, size_t nr,
665 				  unsigned long flags);
666 int kbase_mmu_insert_pages(struct kbase_context *kctx, u64 vpfn,
667 				  phys_addr_t *phys, size_t nr,
668 				  unsigned long flags);
669 int kbase_mmu_insert_single_page(struct kbase_context *kctx, u64 vpfn,
670 					phys_addr_t phys, size_t nr,
671 					unsigned long flags);
672 
673 int kbase_mmu_teardown_pages(struct kbase_context *kctx, u64 vpfn, size_t nr);
674 int kbase_mmu_update_pages(struct kbase_context *kctx, u64 vpfn, phys_addr_t *phys, size_t nr, unsigned long flags);
675 
676 /**
677  * @brief Register region and map it on the GPU.
678  *
679  * Call kbase_add_va_region() and map the region on the GPU.
680  */
681 int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg, u64 addr, size_t nr_pages, size_t align);
682 
683 /**
684  * @brief Remove the region from the GPU and unregister it.
685  *
686  * Must be called with context lock held.
687  */
688 int kbase_gpu_munmap(struct kbase_context *kctx, struct kbase_va_region *reg);
689 
690 /**
691  * The caller has the following locking conditions:
692  * - It must hold kbase_device->mmu_hw_mutex
693  * - It must hold the hwaccess_lock
694  */
695 void kbase_mmu_update(struct kbase_context *kctx);
696 
697 /**
698  * kbase_mmu_disable() - Disable the MMU for a previously active kbase context.
699  * @kctx:	Kbase context
700  *
701  * Disable and perform the required cache maintenance to remove the all
702  * data from provided kbase context from the GPU caches.
703  *
704  * The caller has the following locking conditions:
705  * - It must hold kbase_device->mmu_hw_mutex
706  * - It must hold the hwaccess_lock
707  */
708 void kbase_mmu_disable(struct kbase_context *kctx);
709 
710 /**
711  * kbase_mmu_disable_as() - Set the MMU to unmapped mode for the specified
712  * address space.
713  * @kbdev:	Kbase device
714  * @as_nr:	The address space number to set to unmapped.
715  *
716  * This function must only be called during reset/power-up and it used to
717  * ensure the registers are in a known state.
718  *
719  * The caller must hold kbdev->mmu_hw_mutex.
720  */
721 void kbase_mmu_disable_as(struct kbase_device *kbdev, int as_nr);
722 
723 void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat);
724 
725 /** Dump the MMU tables to a buffer
726  *
727  * This function allocates a buffer (of @c nr_pages pages) to hold a dump of the MMU tables and fills it. If the
728  * buffer is too small then the return value will be NULL.
729  *
730  * The GPU vm lock must be held when calling this function.
731  *
732  * The buffer returned should be freed with @ref vfree when it is no longer required.
733  *
734  * @param[in]   kctx        The kbase context to dump
735  * @param[in]   nr_pages    The number of pages to allocate for the buffer.
736  *
737  * @return The address of the buffer containing the MMU dump or NULL on error (including if the @c nr_pages is too
738  * small)
739  */
740 void *kbase_mmu_dump(struct kbase_context *kctx, int nr_pages);
741 
742 /**
743  * kbase_sync_now - Perform cache maintenance on a memory region
744  *
745  * @kctx: The kbase context of the region
746  * @sset: A syncset structure describing the region and direction of the
747  *        synchronisation required
748  *
749  * Return: 0 on success or error code
750  */
751 int kbase_sync_now(struct kbase_context *kctx, struct basep_syncset *sset);
752 void kbase_sync_single(struct kbase_context *kctx, phys_addr_t cpu_pa,
753 		phys_addr_t gpu_pa, off_t offset, size_t size,
754 		enum kbase_sync_type sync_fn);
755 void kbase_pre_job_sync(struct kbase_context *kctx, struct base_syncset *syncsets, size_t nr);
756 void kbase_post_job_sync(struct kbase_context *kctx, struct base_syncset *syncsets, size_t nr);
757 
758 /* OS specific functions */
759 int kbase_mem_free(struct kbase_context *kctx, u64 gpu_addr);
760 int kbase_mem_free_region(struct kbase_context *kctx, struct kbase_va_region *reg);
761 void kbase_os_mem_map_lock(struct kbase_context *kctx);
762 void kbase_os_mem_map_unlock(struct kbase_context *kctx);
763 
764 /**
765  * @brief Update the memory allocation counters for the current process
766  *
767  * OS specific call to updates the current memory allocation counters for the current process with
768  * the supplied delta.
769  *
770  * @param[in] kctx  The kbase context
771  * @param[in] pages The desired delta to apply to the memory usage counters.
772  */
773 
774 void kbasep_os_process_page_usage_update(struct kbase_context *kctx, int pages);
775 
776 /**
777  * @brief Add to the memory allocation counters for the current process
778  *
779  * OS specific call to add to the current memory allocation counters for the current process by
780  * the supplied amount.
781  *
782  * @param[in] kctx  The kernel base context used for the allocation.
783  * @param[in] pages The desired delta to apply to the memory usage counters.
784  */
785 
kbase_process_page_usage_inc(struct kbase_context *kctx, int pages)786 static inline void kbase_process_page_usage_inc(struct kbase_context *kctx, int pages)
787 {
788 	kbasep_os_process_page_usage_update(kctx, pages);
789 }
790 
791 /**
792  * @brief Subtract from the memory allocation counters for the current process
793  *
794  * OS specific call to subtract from the current memory allocation counters for the current process by
795  * the supplied amount.
796  *
797  * @param[in] kctx  The kernel base context used for the allocation.
798  * @param[in] pages The desired delta to apply to the memory usage counters.
799  */
800 
kbase_process_page_usage_dec(struct kbase_context *kctx, int pages)801 static inline void kbase_process_page_usage_dec(struct kbase_context *kctx, int pages)
802 {
803 	kbasep_os_process_page_usage_update(kctx, 0 - pages);
804 }
805 
806 /**
807  * kbasep_find_enclosing_cpu_mapping_offset() - Find the offset of the CPU
808  * mapping of a memory allocation containing a given address range
809  *
810  * Searches for a CPU mapping of any part of any region that fully encloses the
811  * CPU virtual address range specified by @uaddr and @size. Returns a failure
812  * indication if only part of the address range lies within a CPU mapping.
813  *
814  * @kctx:      The kernel base context used for the allocation.
815  * @uaddr:     Start of the CPU virtual address range.
816  * @size:      Size of the CPU virtual address range (in bytes).
817  * @offset:    The offset from the start of the allocation to the specified CPU
818  *             virtual address.
819  *
820  * Return: 0 if offset was obtained successfully. Error code otherwise.
821  */
822 int kbasep_find_enclosing_cpu_mapping_offset(
823 		struct kbase_context *kctx,
824 		unsigned long uaddr, size_t size, u64 *offset);
825 
826 enum hrtimer_restart kbasep_as_poke_timer_callback(struct hrtimer *timer);
827 void kbase_as_poking_timer_retain_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom);
828 void kbase_as_poking_timer_release_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom);
829 
830 /**
831 * @brief Allocates physical pages.
832 *
833 * Allocates \a nr_pages_requested and updates the alloc object.
834 *
835 * @param[in] alloc allocation object to add pages to
836 * @param[in] nr_pages_requested number of physical pages to allocate
837 *
838 * @return 0 if all pages have been successfully allocated. Error code otherwise
839 */
840 int kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc *alloc, size_t nr_pages_requested);
841 
842 /**
843 * @brief Free physical pages.
844 *
845 * Frees \a nr_pages and updates the alloc object.
846 *
847 * @param[in] alloc allocation object to free pages from
848 * @param[in] nr_pages_to_free number of physical pages to free
849 */
850 int kbase_free_phy_pages_helper(struct kbase_mem_phy_alloc *alloc, size_t nr_pages_to_free);
851 
kbase_set_dma_addr(struct page *p, dma_addr_t dma_addr)852 static inline void kbase_set_dma_addr(struct page *p, dma_addr_t dma_addr)
853 {
854 	SetPagePrivate(p);
855 	if (sizeof(dma_addr_t) > sizeof(p->private)) {
856 		/* on 32-bit ARM with LPAE dma_addr_t becomes larger, but the
857 		 * private field stays the same. So we have to be clever and
858 		 * use the fact that we only store DMA addresses of whole pages,
859 		 * so the low bits should be zero */
860 		KBASE_DEBUG_ASSERT(!(dma_addr & (PAGE_SIZE - 1)));
861 		set_page_private(p, dma_addr >> PAGE_SHIFT);
862 	} else {
863 		set_page_private(p, dma_addr);
864 	}
865 }
866 
kbase_dma_addr(struct page *p)867 static inline dma_addr_t kbase_dma_addr(struct page *p)
868 {
869 	if (sizeof(dma_addr_t) > sizeof(p->private))
870 		return ((dma_addr_t)page_private(p)) << PAGE_SHIFT;
871 
872 	return (dma_addr_t)page_private(p);
873 }
874 
kbase_clear_dma_addr(struct page *p)875 static inline void kbase_clear_dma_addr(struct page *p)
876 {
877 	ClearPagePrivate(p);
878 }
879 
880 /**
881 * @brief Process a bus or page fault.
882 *
883 * This function will process a fault on a specific address space
884 *
885 * @param[in] kbdev   The @ref kbase_device the fault happened on
886 * @param[in] kctx    The @ref kbase_context for the faulting address space if
887 *                    one was found.
888 * @param[in] as      The address space that has the fault
889 */
890 void kbase_mmu_interrupt_process(struct kbase_device *kbdev,
891 		struct kbase_context *kctx, struct kbase_as *as);
892 
893 /**
894  * @brief Process a page fault.
895  *
896  * @param[in] data  work_struct passed by queue_work()
897  */
898 void page_fault_worker(struct work_struct *data);
899 
900 /**
901  * @brief Process a bus fault.
902  *
903  * @param[in] data  work_struct passed by queue_work()
904  */
905 void bus_fault_worker(struct work_struct *data);
906 
907 /**
908  * @brief Flush MMU workqueues.
909  *
910  * This function will cause any outstanding page or bus faults to be processed.
911  * It should be called prior to powering off the GPU.
912  *
913  * @param[in] kbdev   Device pointer
914  */
915 void kbase_flush_mmu_wqs(struct kbase_device *kbdev);
916 
917 /**
918  * kbase_sync_single_for_device - update physical memory and give GPU ownership
919  * @kbdev: Device pointer
920  * @handle: DMA address of region
921  * @size: Size of region to sync
922  * @dir:  DMA data direction
923  */
924 
925 void kbase_sync_single_for_device(struct kbase_device *kbdev, dma_addr_t handle,
926 		size_t size, enum dma_data_direction dir);
927 
928 /**
929  * kbase_sync_single_for_cpu - update physical memory and give CPU ownership
930  * @kbdev: Device pointer
931  * @handle: DMA address of region
932  * @size: Size of region to sync
933  * @dir:  DMA data direction
934  */
935 
936 void kbase_sync_single_for_cpu(struct kbase_device *kbdev, dma_addr_t handle,
937 		size_t size, enum dma_data_direction dir);
938 
939 #ifdef CONFIG_DEBUG_FS
940 /**
941  * kbase_jit_debugfs_init - Add per context debugfs entry for JIT.
942  * @kctx: kbase context
943  */
944 void kbase_jit_debugfs_init(struct kbase_context *kctx);
945 #endif /* CONFIG_DEBUG_FS */
946 
947 /**
948  * kbase_jit_init - Initialize the JIT memory pool management
949  * @kctx: kbase context
950  *
951  * Returns zero on success or negative error number on failure.
952  */
953 int kbase_jit_init(struct kbase_context *kctx);
954 
955 /**
956  * kbase_jit_allocate - Allocate JIT memory
957  * @kctx: kbase context
958  * @info: JIT allocation information
959  *
960  * Return: JIT allocation on success or NULL on failure.
961  */
962 struct kbase_va_region *kbase_jit_allocate(struct kbase_context *kctx,
963 		struct base_jit_alloc_info *info);
964 
965 /**
966  * kbase_jit_free - Free a JIT allocation
967  * @kctx: kbase context
968  * @reg: JIT allocation
969  *
970  * Frees a JIT allocation and places it into the free pool for later reuse.
971  */
972 void kbase_jit_free(struct kbase_context *kctx, struct kbase_va_region *reg);
973 
974 /**
975  * kbase_jit_backing_lost - Inform JIT that an allocation has lost backing
976  * @reg: JIT allocation
977  */
978 void kbase_jit_backing_lost(struct kbase_va_region *reg);
979 
980 /**
981  * kbase_jit_evict - Evict a JIT allocation from the pool
982  * @kctx: kbase context
983  *
984  * Evict the least recently used JIT allocation from the pool. This can be
985  * required if normal VA allocations are failing due to VA exhaustion.
986  *
987  * Return: True if a JIT allocation was freed, false otherwise.
988  */
989 bool kbase_jit_evict(struct kbase_context *kctx);
990 
991 /**
992  * kbase_jit_term - Terminate the JIT memory pool management
993  * @kctx: kbase context
994  */
995 void kbase_jit_term(struct kbase_context *kctx);
996 
997 /**
998  * kbase_map_external_resource - Map an external resource to the GPU.
999  * @kctx:              kbase context.
1000  * @reg:               The region to map.
1001  * @locked_mm:         The mm_struct which has been locked for this operation.
1002  * @kds_res_count:     The number of KDS resources.
1003  * @kds_resources:     Array of KDS resources.
1004  * @kds_access_bitmap: Access bitmap for KDS.
1005  * @exclusive:         If the KDS resource requires exclusive access.
1006  *
1007  * Return: The physical allocation which backs the region on success or NULL
1008  * on failure.
1009  */
1010 struct kbase_mem_phy_alloc *kbase_map_external_resource(
1011 		struct kbase_context *kctx, struct kbase_va_region *reg,
1012 		struct mm_struct *locked_mm
1013 #ifdef CONFIG_KDS
1014 		, u32 *kds_res_count, struct kds_resource **kds_resources,
1015 		unsigned long *kds_access_bitmap, bool exclusive
1016 #endif
1017 		);
1018 
1019 /**
1020  * kbase_unmap_external_resource - Unmap an external resource from the GPU.
1021  * @kctx:  kbase context.
1022  * @reg:   The region to unmap or NULL if it has already been released.
1023  * @alloc: The physical allocation being unmapped.
1024  */
1025 void kbase_unmap_external_resource(struct kbase_context *kctx,
1026 		struct kbase_va_region *reg, struct kbase_mem_phy_alloc *alloc);
1027 
1028 /**
1029  * kbase_sticky_resource_init - Initialize sticky resource management.
1030  * @kctx: kbase context
1031  *
1032  * Returns zero on success or negative error number on failure.
1033  */
1034 int kbase_sticky_resource_init(struct kbase_context *kctx);
1035 
1036 /**
1037  * kbase_sticky_resource_acquire - Acquire a reference on a sticky resource.
1038  * @kctx:     kbase context.
1039  * @gpu_addr: The GPU address of the external resource.
1040  *
1041  * Return: The metadata object which represents the binding between the
1042  * external resource and the kbase context on success or NULL on failure.
1043  */
1044 struct kbase_ctx_ext_res_meta *kbase_sticky_resource_acquire(
1045 		struct kbase_context *kctx, u64 gpu_addr);
1046 
1047 /**
1048  * kbase_sticky_resource_release - Release a reference on a sticky resource.
1049  * @kctx:     kbase context.
1050  * @meta:     Binding metadata.
1051  * @gpu_addr: GPU address of the external resource.
1052  *
1053  * If meta is NULL then gpu_addr will be used to scan the metadata list and
1054  * find the matching metadata (if any), otherwise the provided meta will be
1055  * used and gpu_addr will be ignored.
1056  *
1057  * Return: True if the release found the metadata and the reference was dropped.
1058  */
1059 bool kbase_sticky_resource_release(struct kbase_context *kctx,
1060 		struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr);
1061 
1062 /**
1063  * kbase_sticky_resource_term - Terminate sticky resource management.
1064  * @kctx: kbase context
1065  */
1066 void kbase_sticky_resource_term(struct kbase_context *kctx);
1067 
1068 #endif				/* _KBASE_MEM_H_ */
1069