1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3 *
4 * (C) COPYRIGHT 2010-2021 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 /**
23 * DOC: Base kernel memory APIs
24 */
25
26 #ifndef _KBASE_MEM_H_
27 #define _KBASE_MEM_H_
28
29 #ifndef _KBASE_H_
30 #error "Don't include this file directly, use mali_kbase.h instead"
31 #endif
32
33 #include <linux/kref.h>
34 #include <uapi/gpu/arm/bifrost/mali_base_kernel.h>
35 #include <mali_kbase_hw.h>
36 #include "mali_kbase_pm.h"
37 #include "mali_kbase_defs.h"
38 /* Required for kbase_mem_evictable_unmake */
39 #include "mali_kbase_mem_linux.h"
40
41 static inline void kbase_process_page_usage_inc(struct kbase_context *kctx,
42 int pages);
43
44 /* Part of the workaround for uTLB invalid pages is to ensure we grow/shrink tmem by 4 pages at a time */
45 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316 (2) /* round to 4 pages */
46
47 /* Part of the workaround for PRLAM-9630 requires us to grow/shrink memory by
48 * 8 pages. The MMU reads in 8 page table entries from memory at a time, if we
49 * have more than one page fault within the same 8 pages and page tables are
50 * updated accordingly, the MMU does not re-read the page table entries from
51 * memory for the subsequent page table updates and generates duplicate page
52 * faults as the page table information used by the MMU is not valid.
53 */
54 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630 (3) /* round to 8 pages */
55
56 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2 (0) /* round to 1 page */
57
58 /* This must always be a power of 2 */
59 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2)
60 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_8316 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316)
61 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_9630 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630)
62
63 /*
64 * A CPU mapping
65 */
66 struct kbase_cpu_mapping {
67 struct list_head mappings_list;
68 struct kbase_mem_phy_alloc *alloc;
69 struct kbase_context *kctx;
70 struct kbase_va_region *region;
71 int count;
72 int free_on_close;
73 };
74
75 enum kbase_memory_type {
76 KBASE_MEM_TYPE_NATIVE,
77 KBASE_MEM_TYPE_IMPORTED_UMM,
78 KBASE_MEM_TYPE_IMPORTED_USER_BUF,
79 KBASE_MEM_TYPE_ALIAS,
80 KBASE_MEM_TYPE_RAW
81 };
82
83 /* internal structure, mirroring base_mem_aliasing_info,
84 * but with alloc instead of a gpu va (handle)
85 */
86 struct kbase_aliased {
87 struct kbase_mem_phy_alloc *alloc; /* NULL for special, non-NULL for native */
88 u64 offset; /* in pages */
89 u64 length; /* in pages */
90 };
91
92 /* Physical pages tracking object properties */
93 #define KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED (1u << 0)
94 #define KBASE_MEM_PHY_ALLOC_LARGE (1u << 1)
95
96 /* struct kbase_mem_phy_alloc - Physical pages tracking object.
97 *
98 * Set up to track N pages.
99 * N not stored here, the creator holds that info.
100 * This object only tracks how many elements are actually valid (present).
101 * Changing of nents or *pages should only happen if the kbase_mem_phy_alloc
102 * is not shared with another region or client. CPU mappings are OK to
103 * exist when changing, as long as the tracked mappings objects are
104 * updated as part of the change.
105 *
106 * @kref: number of users of this alloc
107 * @gpu_mappings: count number of times mapped on the GPU. Indicates the number
108 * of references there are to the physical pages from different
109 * GPU VA regions.
110 * @kernel_mappings: count number of times mapped on the CPU, specifically in
111 * the kernel. Indicates the number of references there are
112 * to the physical pages to prevent flag changes or shrink
113 * while maps are still held.
114 * @nents: 0..N
115 * @pages: N elements, only 0..nents are valid
116 * @mappings: List of CPU mappings of this physical memory allocation.
117 * @evict_node: Node used to store this allocation on the eviction list
118 * @evicted: Physical backing size when the pages where evicted
119 * @reg: Back reference to the region structure which created this
120 * allocation, or NULL if it has been freed.
121 * @type: type of buffer
122 * @permanent_map: Kernel side mapping of the alloc, shall never be
123 * referred directly. kbase_phy_alloc_mapping_get() &
124 * kbase_phy_alloc_mapping_put() pair should be used
125 * around access to the kernel-side CPU mapping so that
126 * mapping doesn't disappear whilst it is being accessed.
127 * @properties: Bitmask of properties, e.g. KBASE_MEM_PHY_ALLOC_LARGE.
128 * @group_id: A memory group ID to be passed to a platform-specific
129 * memory group manager, if present.
130 * Valid range is 0..(MEMORY_GROUP_MANAGER_NR_GROUPS-1).
131 * @imported: member in union valid based on @a type
132 */
133 struct kbase_mem_phy_alloc {
134 struct kref kref;
135 atomic_t gpu_mappings;
136 atomic_t kernel_mappings;
137 size_t nents;
138 struct tagged_addr *pages;
139 struct list_head mappings;
140 struct list_head evict_node;
141 size_t evicted;
142 struct kbase_va_region *reg;
143 enum kbase_memory_type type;
144 struct kbase_vmap_struct *permanent_map;
145 u8 properties;
146 u8 group_id;
147
148 union {
149 struct {
150 struct kbase_context *kctx;
151 struct dma_buf *dma_buf;
152 struct dma_buf_attachment *dma_attachment;
153 unsigned int current_mapping_usage_count;
154 struct sg_table *sgt;
155 bool need_sync;
156 } umm;
157 struct {
158 u64 stride;
159 size_t nents;
160 struct kbase_aliased *aliased;
161 } alias;
162 struct {
163 struct kbase_context *kctx;
164 /* Number of pages in this structure, including *pages.
165 * Used for kernel memory tracking.
166 */
167 size_t nr_struct_pages;
168 } native;
169 struct kbase_alloc_import_user_buf {
170 unsigned long address;
171 unsigned long size;
172 unsigned long nr_pages;
173 struct page **pages;
174 /* top bit (1<<31) of current_mapping_usage_count
175 * specifies that this import was pinned on import
176 * See PINNED_ON_IMPORT
177 */
178 u32 current_mapping_usage_count;
179 struct mm_struct *mm;
180 dma_addr_t *dma_addrs;
181 } user_buf;
182 } imported;
183 };
184
185 /* The top bit of kbase_alloc_import_user_buf::current_mapping_usage_count is
186 * used to signify that a buffer was pinned when it was imported. Since the
187 * reference count is limited by the number of atoms that can be submitted at
188 * once there should be no danger of overflowing into this bit.
189 * Stealing the top bit also has the benefit that
190 * current_mapping_usage_count != 0 if and only if the buffer is mapped.
191 */
192 #define PINNED_ON_IMPORT (1<<31)
193
194 /**
195 * enum kbase_jit_report_flags - Flags for just-in-time memory allocation
196 * pressure limit functions
197 * @KBASE_JIT_REPORT_ON_ALLOC_OR_FREE: Notifying about an update happening due
198 * to a just-in-time memory allocation or free
199 *
200 * Used to control flow within pressure limit related functions, or to provide
201 * extra debugging information
202 */
203 enum kbase_jit_report_flags {
204 KBASE_JIT_REPORT_ON_ALLOC_OR_FREE = (1u << 0)
205 };
206
kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc * alloc)207 static inline void kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc *alloc)
208 {
209 KBASE_DEBUG_ASSERT(alloc);
210 /* we only track mappings of NATIVE buffers */
211 if (alloc->type == KBASE_MEM_TYPE_NATIVE)
212 atomic_inc(&alloc->gpu_mappings);
213 }
214
kbase_mem_phy_alloc_gpu_unmapped(struct kbase_mem_phy_alloc * alloc)215 static inline void kbase_mem_phy_alloc_gpu_unmapped(struct kbase_mem_phy_alloc *alloc)
216 {
217 KBASE_DEBUG_ASSERT(alloc);
218 /* we only track mappings of NATIVE buffers */
219 if (alloc->type == KBASE_MEM_TYPE_NATIVE)
220 if (atomic_dec_return(&alloc->gpu_mappings) < 0) {
221 pr_err("Mismatched %s:\n", __func__);
222 dump_stack();
223 }
224 }
225
226 /**
227 * kbase_mem_phy_alloc_kernel_mapped - Increment kernel_mappings
228 * counter for a memory region to prevent commit and flag changes
229 *
230 * @alloc: Pointer to physical pages tracking object
231 */
232 static inline void
kbase_mem_phy_alloc_kernel_mapped(struct kbase_mem_phy_alloc * alloc)233 kbase_mem_phy_alloc_kernel_mapped(struct kbase_mem_phy_alloc *alloc)
234 {
235 atomic_inc(&alloc->kernel_mappings);
236 }
237
238 /**
239 * kbase_mem_phy_alloc_kernel_unmapped - Decrement kernel_mappings
240 * counter for a memory region to allow commit and flag changes
241 *
242 * @alloc: Pointer to physical pages tracking object
243 */
244 static inline void
kbase_mem_phy_alloc_kernel_unmapped(struct kbase_mem_phy_alloc * alloc)245 kbase_mem_phy_alloc_kernel_unmapped(struct kbase_mem_phy_alloc *alloc)
246 {
247 WARN_ON(atomic_dec_return(&alloc->kernel_mappings) < 0);
248 }
249
250 /**
251 * kbase_mem_is_imported - Indicate whether a memory type is imported
252 *
253 * @type: the memory type
254 *
255 * Return: true if the memory type is imported, false otherwise
256 */
kbase_mem_is_imported(enum kbase_memory_type type)257 static inline bool kbase_mem_is_imported(enum kbase_memory_type type)
258 {
259 return (type == KBASE_MEM_TYPE_IMPORTED_UMM) ||
260 (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF);
261 }
262
263 void kbase_mem_kref_free(struct kref *kref);
264
265 int kbase_mem_init(struct kbase_device *kbdev);
266 void kbase_mem_halt(struct kbase_device *kbdev);
267 void kbase_mem_term(struct kbase_device *kbdev);
268
kbase_mem_phy_alloc_get(struct kbase_mem_phy_alloc * alloc)269 static inline struct kbase_mem_phy_alloc *kbase_mem_phy_alloc_get(struct kbase_mem_phy_alloc *alloc)
270 {
271 kref_get(&alloc->kref);
272 return alloc;
273 }
274
kbase_mem_phy_alloc_put(struct kbase_mem_phy_alloc * alloc)275 static inline struct kbase_mem_phy_alloc *kbase_mem_phy_alloc_put(struct kbase_mem_phy_alloc *alloc)
276 {
277 kref_put(&alloc->kref, kbase_mem_kref_free);
278 return NULL;
279 }
280
281 /**
282 * struct kbase_va_region - A GPU memory region, and attributes for CPU mappings
283 *
284 * @rblink: Node in a red-black tree of memory regions within the same zone of
285 * the GPU's virtual address space.
286 * @link: Links to neighboring items in a list of growable memory regions
287 * that triggered incremental rendering by growing too much.
288 * @rbtree: Backlink to the red-black tree of memory regions.
289 * @start_pfn: The Page Frame Number in GPU virtual address space.
290 * @user_data: The address of GPU command queue when VA region represents
291 * a ring buffer.
292 * @nr_pages: The size of the region in pages.
293 * @initial_commit: Initial commit, for aligning the start address and
294 * correctly growing KBASE_REG_TILER_ALIGN_TOP regions.
295 * @threshold_pages: If non-zero and the amount of memory committed to a region
296 * that can grow on page fault exceeds this number of pages
297 * then the driver switches to incremental rendering.
298 * @flags: Flags
299 * @extension: Number of pages allocated on page fault.
300 * @cpu_alloc: The physical memory we mmap to the CPU when mapping this region.
301 * @gpu_alloc: The physical memory we mmap to the GPU when mapping this region.
302 * @jit_node: Links to neighboring regions in the just-in-time memory pool.
303 * @jit_usage_id: The last just-in-time memory usage ID for this region.
304 * @jit_bin_id: The just-in-time memory bin this region came from.
305 * @va_refcnt: Number of users of this region. Protected by reg_lock.
306 * @heap_info_gpu_addr: Pointer to an object in GPU memory defining an end of
307 * an allocated region
308 * The object can be one of:
309 * - u32 value defining the size of the region
310 * - u64 pointer first unused byte in the region
311 * The interpretation of the object depends on
312 * BASE_JIT_ALLOC_HEAP_INFO_IS_SIZE flag in
313 * jit_info_flags - if it is set, the heap info object
314 * should be interpreted as size.
315 * @used_pages: The current estimate of the number of pages used, which in
316 * normal use is either:
317 * - the initial estimate == va_pages
318 * - the actual pages used, as found by a JIT usage report
319 * Note that since the value is calculated from GPU memory after a
320 * JIT usage report, at any point in time it is allowed to take a
321 * random value that is no greater than va_pages (e.g. it may be
322 * greater than gpu_alloc->nents)
323 */
324 struct kbase_va_region {
325 struct rb_node rblink;
326 struct list_head link;
327 struct rb_root *rbtree;
328 u64 start_pfn;
329 void *user_data;
330 size_t nr_pages;
331 size_t initial_commit;
332 size_t threshold_pages;
333
334 /* Free region */
335 #define KBASE_REG_FREE (1ul << 0)
336 /* CPU write access */
337 #define KBASE_REG_CPU_WR (1ul << 1)
338 /* GPU write access */
339 #define KBASE_REG_GPU_WR (1ul << 2)
340 /* No eXecute flag */
341 #define KBASE_REG_GPU_NX (1ul << 3)
342 /* Is CPU cached? */
343 #define KBASE_REG_CPU_CACHED (1ul << 4)
344 /* Is GPU cached?
345 * Some components within the GPU might only be able to access memory that is
346 * GPU cacheable. Refer to the specific GPU implementation for more details.
347 */
348 #define KBASE_REG_GPU_CACHED (1ul << 5)
349
350 #define KBASE_REG_GROWABLE (1ul << 6)
351 /* Can grow on pf? */
352 #define KBASE_REG_PF_GROW (1ul << 7)
353
354 /* Allocation doesn't straddle the 4GB boundary in GPU virtual space */
355 #define KBASE_REG_GPU_VA_SAME_4GB_PAGE (1ul << 8)
356
357 /* inner shareable coherency */
358 #define KBASE_REG_SHARE_IN (1ul << 9)
359 /* inner & outer shareable coherency */
360 #define KBASE_REG_SHARE_BOTH (1ul << 10)
361
362 /* Space for 4 different zones */
363 #define KBASE_REG_ZONE_MASK ((KBASE_REG_ZONE_MAX - 1ul) << 11)
364 #define KBASE_REG_ZONE(x) (((x) & (KBASE_REG_ZONE_MAX - 1ul)) << 11)
365 #define KBASE_REG_ZONE_IDX(x) (((x) & KBASE_REG_ZONE_MASK) >> 11)
366
367 #if ((KBASE_REG_ZONE_MAX - 1) & 0x3) != (KBASE_REG_ZONE_MAX - 1)
368 #error KBASE_REG_ZONE_MAX too large for allocation of KBASE_REG_<...> bits
369 #endif
370
371 /* GPU read access */
372 #define KBASE_REG_GPU_RD (1ul<<13)
373 /* CPU read access */
374 #define KBASE_REG_CPU_RD (1ul<<14)
375
376 /* Index of chosen MEMATTR for this region (0..7) */
377 #define KBASE_REG_MEMATTR_MASK (7ul << 16)
378 #define KBASE_REG_MEMATTR_INDEX(x) (((x) & 7) << 16)
379 #define KBASE_REG_MEMATTR_VALUE(x) (((x) & KBASE_REG_MEMATTR_MASK) >> 16)
380
381 #define KBASE_REG_PROTECTED (1ul << 19)
382
383 #define KBASE_REG_DONT_NEED (1ul << 20)
384
385 /* Imported buffer is padded? */
386 #define KBASE_REG_IMPORT_PAD (1ul << 21)
387
388 #if MALI_USE_CSF
389 /* CSF event memory */
390 #define KBASE_REG_CSF_EVENT (1ul << 22)
391 #else
392 /* Bit 22 is reserved.
393 *
394 * Do not remove, use the next unreserved bit for new flags
395 */
396 #define KBASE_REG_RESERVED_BIT_22 (1ul << 22)
397 #endif
398
399 #if !MALI_USE_CSF
400 /* The top of the initial commit is aligned to extension pages.
401 * Extent must be a power of 2
402 */
403 #define KBASE_REG_TILER_ALIGN_TOP (1ul << 23)
404 #else
405 /* Bit 23 is reserved.
406 *
407 * Do not remove, use the next unreserved bit for new flags
408 */
409 #define KBASE_REG_RESERVED_BIT_23 (1ul << 23)
410 #endif /* !MALI_USE_CSF */
411
412 /* Whilst this flag is set the GPU allocation is not supposed to be freed by
413 * user space. The flag will remain set for the lifetime of JIT allocations.
414 */
415 #define KBASE_REG_NO_USER_FREE (1ul << 24)
416
417 /* Memory has permanent kernel side mapping */
418 #define KBASE_REG_PERMANENT_KERNEL_MAPPING (1ul << 25)
419
420 /* GPU VA region has been freed by the userspace, but still remains allocated
421 * due to the reference held by CPU mappings created on the GPU VA region.
422 *
423 * A region with this flag set has had kbase_gpu_munmap() called on it, but can
424 * still be looked-up in the region tracker as a non-free region. Hence must
425 * not create or update any more GPU mappings on such regions because they will
426 * not be unmapped when the region is finally destroyed.
427 *
428 * Since such regions are still present in the region tracker, new allocations
429 * attempted with BASE_MEM_SAME_VA might fail if their address intersects with
430 * a region with this flag set.
431 *
432 * In addition, this flag indicates the gpu_alloc member might no longer valid
433 * e.g. in infinite cache simulation.
434 */
435 #define KBASE_REG_VA_FREED (1ul << 26)
436
437 /* If set, the heap info address points to a u32 holding the used size in bytes;
438 * otherwise it points to a u64 holding the lowest address of unused memory.
439 */
440 #define KBASE_REG_HEAP_INFO_IS_SIZE (1ul << 27)
441
442 /* Allocation is actively used for JIT memory */
443 #define KBASE_REG_ACTIVE_JIT_ALLOC (1ul << 28)
444
445 #define KBASE_REG_ZONE_SAME_VA KBASE_REG_ZONE(0)
446
447 /* only used with 32-bit clients */
448 /*
449 * On a 32bit platform, custom VA should be wired from 4GB
450 * to the VA limit of the GPU. Unfortunately, the Linux mmap() interface
451 * limits us to 2^32 pages (2^44 bytes, see mmap64 man page for reference).
452 * So we put the default limit to the maximum possible on Linux and shrink
453 * it down, if required by the GPU, during initialization.
454 */
455
456 #define KBASE_REG_ZONE_CUSTOM_VA KBASE_REG_ZONE(1)
457 #define KBASE_REG_ZONE_CUSTOM_VA_BASE (0x100000000ULL >> PAGE_SHIFT)
458 #define KBASE_REG_ZONE_CUSTOM_VA_SIZE (((1ULL << 44) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE)
459 /* end 32-bit clients only */
460
461 /* The starting address and size of the GPU-executable zone are dynamic
462 * and depend on the platform and the number of pages requested by the
463 * user process, with an upper limit of 4 GB.
464 */
465 #define KBASE_REG_ZONE_EXEC_VA KBASE_REG_ZONE(2)
466 #define KBASE_REG_ZONE_EXEC_VA_MAX_PAGES ((1ULL << 32) >> PAGE_SHIFT) /* 4 GB */
467
468 #if MALI_USE_CSF
469 #define KBASE_REG_ZONE_MCU_SHARED KBASE_REG_ZONE(3)
470 #define KBASE_REG_ZONE_MCU_SHARED_BASE (0x04000000ULL >> PAGE_SHIFT)
471 #define KBASE_REG_ZONE_MCU_SHARED_SIZE (((0x08000000ULL) >> PAGE_SHIFT) - \
472 KBASE_REG_ZONE_MCU_SHARED_BASE)
473 #endif
474
475 unsigned long flags;
476 size_t extension;
477 struct kbase_mem_phy_alloc *cpu_alloc;
478 struct kbase_mem_phy_alloc *gpu_alloc;
479 struct list_head jit_node;
480 u16 jit_usage_id;
481 u8 jit_bin_id;
482
483 #if MALI_JIT_PRESSURE_LIMIT_BASE
484 /* Pointer to an object in GPU memory defining an end of an allocated
485 * region
486 *
487 * The object can be one of:
488 * - u32 value defining the size of the region
489 * - u64 pointer first unused byte in the region
490 *
491 * The interpretation of the object depends on
492 * BASE_JIT_ALLOC_HEAP_INFO_IS_SIZE flag in jit_info_flags - if it is
493 * set, the heap info object should be interpreted as size.
494 */
495 u64 heap_info_gpu_addr;
496
497 /* The current estimate of the number of pages used, which in normal
498 * use is either:
499 * - the initial estimate == va_pages
500 * - the actual pages used, as found by a JIT usage report
501 *
502 * Note that since the value is calculated from GPU memory after a JIT
503 * usage report, at any point in time it is allowed to take a random
504 * value that is no greater than va_pages (e.g. it may be greater than
505 * gpu_alloc->nents)
506 */
507 size_t used_pages;
508 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
509
510 int va_refcnt;
511 };
512
513 /**
514 * kbase_is_ctx_reg_zone - determine whether a KBASE_REG_ZONE_<...> is for a
515 * context or for a device
516 * @zone_bits: A KBASE_REG_ZONE_<...> to query
517 *
518 * Return: True if the zone for @zone_bits is a context zone, False otherwise
519 */
kbase_is_ctx_reg_zone(unsigned long zone_bits)520 static inline bool kbase_is_ctx_reg_zone(unsigned long zone_bits)
521 {
522 WARN_ON((zone_bits & KBASE_REG_ZONE_MASK) != zone_bits);
523 return (zone_bits == KBASE_REG_ZONE_SAME_VA ||
524 zone_bits == KBASE_REG_ZONE_CUSTOM_VA ||
525 zone_bits == KBASE_REG_ZONE_EXEC_VA);
526 }
527
528 /* Special marker for failed JIT allocations that still must be marked as
529 * in-use
530 */
531 #define KBASE_RESERVED_REG_JIT_ALLOC ((struct kbase_va_region *)-1)
532
kbase_is_region_free(struct kbase_va_region * reg)533 static inline bool kbase_is_region_free(struct kbase_va_region *reg)
534 {
535 return (!reg || reg->flags & KBASE_REG_FREE);
536 }
537
kbase_is_region_invalid(struct kbase_va_region * reg)538 static inline bool kbase_is_region_invalid(struct kbase_va_region *reg)
539 {
540 return (!reg || reg->flags & KBASE_REG_VA_FREED);
541 }
542
kbase_is_region_invalid_or_free(struct kbase_va_region * reg)543 static inline bool kbase_is_region_invalid_or_free(struct kbase_va_region *reg)
544 {
545 /* Possibly not all functions that find regions would be using this
546 * helper, so they need to be checked when maintaining this function.
547 */
548 return (kbase_is_region_invalid(reg) || kbase_is_region_free(reg));
549 }
550
551 void kbase_remove_va_region(struct kbase_device *kbdev,
552 struct kbase_va_region *reg);
kbase_region_refcnt_free(struct kbase_device * kbdev,struct kbase_va_region * reg)553 static inline void kbase_region_refcnt_free(struct kbase_device *kbdev,
554 struct kbase_va_region *reg)
555 {
556 /* If region was mapped then remove va region*/
557 if (reg->start_pfn)
558 kbase_remove_va_region(kbdev, reg);
559
560 /* To detect use-after-free in debug builds */
561 KBASE_DEBUG_CODE(reg->flags |= KBASE_REG_FREE);
562 kfree(reg);
563 }
564
kbase_va_region_alloc_get(struct kbase_context * kctx,struct kbase_va_region * region)565 static inline struct kbase_va_region *kbase_va_region_alloc_get(
566 struct kbase_context *kctx, struct kbase_va_region *region)
567 {
568 lockdep_assert_held(&kctx->reg_lock);
569
570 WARN_ON(!region->va_refcnt);
571
572 /* non-atomic as kctx->reg_lock is held */
573 dev_dbg(kctx->kbdev->dev, "va_refcnt %d before get %pK\n",
574 region->va_refcnt, (void *)region);
575 region->va_refcnt++;
576
577 return region;
578 }
579
kbase_va_region_alloc_put(struct kbase_context * kctx,struct kbase_va_region * region)580 static inline struct kbase_va_region *kbase_va_region_alloc_put(
581 struct kbase_context *kctx, struct kbase_va_region *region)
582 {
583 lockdep_assert_held(&kctx->reg_lock);
584
585 WARN_ON(region->va_refcnt <= 0);
586 WARN_ON(region->flags & KBASE_REG_FREE);
587
588 /* non-atomic as kctx->reg_lock is held */
589 region->va_refcnt--;
590 dev_dbg(kctx->kbdev->dev, "va_refcnt %d after put %pK\n",
591 region->va_refcnt, (void *)region);
592 if (!region->va_refcnt)
593 kbase_region_refcnt_free(kctx->kbdev, region);
594
595 return NULL;
596 }
597
598 /* Common functions */
kbase_get_cpu_phy_pages(struct kbase_va_region * reg)599 static inline struct tagged_addr *kbase_get_cpu_phy_pages(
600 struct kbase_va_region *reg)
601 {
602 KBASE_DEBUG_ASSERT(reg);
603 KBASE_DEBUG_ASSERT(reg->cpu_alloc);
604 KBASE_DEBUG_ASSERT(reg->gpu_alloc);
605 KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
606
607 return reg->cpu_alloc->pages;
608 }
609
kbase_get_gpu_phy_pages(struct kbase_va_region * reg)610 static inline struct tagged_addr *kbase_get_gpu_phy_pages(
611 struct kbase_va_region *reg)
612 {
613 KBASE_DEBUG_ASSERT(reg);
614 KBASE_DEBUG_ASSERT(reg->cpu_alloc);
615 KBASE_DEBUG_ASSERT(reg->gpu_alloc);
616 KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
617
618 return reg->gpu_alloc->pages;
619 }
620
kbase_reg_current_backed_size(struct kbase_va_region * reg)621 static inline size_t kbase_reg_current_backed_size(struct kbase_va_region *reg)
622 {
623 KBASE_DEBUG_ASSERT(reg);
624 /* if no alloc object the backed size naturally is 0 */
625 if (!reg->cpu_alloc)
626 return 0;
627
628 KBASE_DEBUG_ASSERT(reg->cpu_alloc);
629 KBASE_DEBUG_ASSERT(reg->gpu_alloc);
630 KBASE_DEBUG_ASSERT(reg->cpu_alloc->nents == reg->gpu_alloc->nents);
631
632 return reg->cpu_alloc->nents;
633 }
634
635 #define KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD ((size_t)(4*1024)) /* size above which vmalloc is used over kmalloc */
636
kbase_alloc_create(struct kbase_context * kctx,size_t nr_pages,enum kbase_memory_type type,int group_id)637 static inline struct kbase_mem_phy_alloc *kbase_alloc_create(
638 struct kbase_context *kctx, size_t nr_pages,
639 enum kbase_memory_type type, int group_id)
640 {
641 struct kbase_mem_phy_alloc *alloc;
642 size_t alloc_size = sizeof(*alloc) + sizeof(*alloc->pages) * nr_pages;
643 size_t per_page_size = sizeof(*alloc->pages);
644
645 /* Imported pages may have page private data already in use */
646 if (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF) {
647 alloc_size += nr_pages *
648 sizeof(*alloc->imported.user_buf.dma_addrs);
649 per_page_size += sizeof(*alloc->imported.user_buf.dma_addrs);
650 }
651
652 /*
653 * Prevent nr_pages*per_page_size + sizeof(*alloc) from
654 * wrapping around.
655 */
656 if (nr_pages > ((((size_t) -1) - sizeof(*alloc))
657 / per_page_size))
658 return ERR_PTR(-ENOMEM);
659
660 /* Allocate based on the size to reduce internal fragmentation of vmem */
661 if (alloc_size > KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD)
662 alloc = vzalloc(alloc_size);
663 else
664 alloc = kzalloc(alloc_size, GFP_KERNEL);
665
666 if (!alloc)
667 return ERR_PTR(-ENOMEM);
668
669 if (type == KBASE_MEM_TYPE_NATIVE) {
670 alloc->imported.native.nr_struct_pages =
671 (alloc_size + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
672 kbase_process_page_usage_inc(kctx,
673 alloc->imported.native.nr_struct_pages);
674 }
675
676 /* Store allocation method */
677 if (alloc_size > KBASE_MEM_PHY_ALLOC_LARGE_THRESHOLD)
678 alloc->properties |= KBASE_MEM_PHY_ALLOC_LARGE;
679
680 kref_init(&alloc->kref);
681 atomic_set(&alloc->gpu_mappings, 0);
682 atomic_set(&alloc->kernel_mappings, 0);
683 alloc->nents = 0;
684 alloc->pages = (void *)(alloc + 1);
685 INIT_LIST_HEAD(&alloc->mappings);
686 alloc->type = type;
687 alloc->group_id = group_id;
688
689 if (type == KBASE_MEM_TYPE_IMPORTED_USER_BUF)
690 alloc->imported.user_buf.dma_addrs =
691 (void *) (alloc->pages + nr_pages);
692
693 return alloc;
694 }
695
kbase_reg_prepare_native(struct kbase_va_region * reg,struct kbase_context * kctx,int group_id)696 static inline int kbase_reg_prepare_native(struct kbase_va_region *reg,
697 struct kbase_context *kctx, int group_id)
698 {
699 KBASE_DEBUG_ASSERT(reg);
700 KBASE_DEBUG_ASSERT(!reg->cpu_alloc);
701 KBASE_DEBUG_ASSERT(!reg->gpu_alloc);
702 KBASE_DEBUG_ASSERT(reg->flags & KBASE_REG_FREE);
703
704 reg->cpu_alloc = kbase_alloc_create(kctx, reg->nr_pages,
705 KBASE_MEM_TYPE_NATIVE, group_id);
706 if (IS_ERR(reg->cpu_alloc))
707 return PTR_ERR(reg->cpu_alloc);
708 else if (!reg->cpu_alloc)
709 return -ENOMEM;
710
711 reg->cpu_alloc->imported.native.kctx = kctx;
712 if (kbase_ctx_flag(kctx, KCTX_INFINITE_CACHE)
713 && (reg->flags & KBASE_REG_CPU_CACHED)) {
714 reg->gpu_alloc = kbase_alloc_create(kctx, reg->nr_pages,
715 KBASE_MEM_TYPE_NATIVE, group_id);
716 if (IS_ERR_OR_NULL(reg->gpu_alloc)) {
717 kbase_mem_phy_alloc_put(reg->cpu_alloc);
718 return -ENOMEM;
719 }
720 reg->gpu_alloc->imported.native.kctx = kctx;
721 } else {
722 reg->gpu_alloc = kbase_mem_phy_alloc_get(reg->cpu_alloc);
723 }
724
725 mutex_lock(&kctx->jit_evict_lock);
726 INIT_LIST_HEAD(®->cpu_alloc->evict_node);
727 INIT_LIST_HEAD(®->gpu_alloc->evict_node);
728 mutex_unlock(&kctx->jit_evict_lock);
729
730 reg->flags &= ~KBASE_REG_FREE;
731
732 return 0;
733 }
734
735 /*
736 * Max size for kbdev memory pool (in pages)
737 */
738 #define KBASE_MEM_POOL_MAX_SIZE_KBDEV (SZ_64M >> PAGE_SHIFT)
739
740 /*
741 * Max size for kctx memory pool (in pages)
742 */
743 #define KBASE_MEM_POOL_MAX_SIZE_KCTX (SZ_64M >> PAGE_SHIFT)
744
745 /*
746 * The order required for a 2MB page allocation (2^order * 4KB = 2MB)
747 */
748 #define KBASE_MEM_POOL_2MB_PAGE_TABLE_ORDER 9
749
750 /*
751 * The order required for a 4KB page allocation
752 */
753 #define KBASE_MEM_POOL_4KB_PAGE_TABLE_ORDER 0
754
755 /**
756 * kbase_mem_pool_config_set_max_size - Set maximum number of free pages in
757 * initial configuration of a memory pool
758 *
759 * @config: Initial configuration for a physical memory pool
760 * @max_size: Maximum number of free pages that a pool created from
761 * @config can hold
762 */
kbase_mem_pool_config_set_max_size(struct kbase_mem_pool_config * const config,size_t const max_size)763 static inline void kbase_mem_pool_config_set_max_size(
764 struct kbase_mem_pool_config *const config, size_t const max_size)
765 {
766 WRITE_ONCE(config->max_size, max_size);
767 }
768
769 /**
770 * kbase_mem_pool_config_get_max_size - Get maximum number of free pages from
771 * initial configuration of a memory pool
772 *
773 * @config: Initial configuration for a physical memory pool
774 *
775 * Return: Maximum number of free pages that a pool created from @config
776 * can hold
777 */
kbase_mem_pool_config_get_max_size(const struct kbase_mem_pool_config * const config)778 static inline size_t kbase_mem_pool_config_get_max_size(
779 const struct kbase_mem_pool_config *const config)
780 {
781 return READ_ONCE(config->max_size);
782 }
783
784 /**
785 * kbase_mem_pool_init - Create a memory pool for a kbase device
786 * @pool: Memory pool to initialize
787 * @config: Initial configuration for the memory pool
788 * @order: Page order for physical page size (order=0=>4kB, order=9=>2MB)
789 * @group_id: A memory group ID to be passed to a platform-specific
790 * memory group manager, if present.
791 * Valid range is 0..(MEMORY_GROUP_MANAGER_NR_GROUPS-1).
792 * @kbdev: Kbase device where memory is used
793 * @next_pool: Pointer to the next pool or NULL.
794 *
795 * Allocations from @pool are in whole pages. Each @pool has a free list where
796 * pages can be quickly allocated from. The free list is initially empty and
797 * filled whenever pages are freed back to the pool. The number of free pages
798 * in the pool will in general not exceed @max_size, but the pool may in
799 * certain corner cases grow above @max_size.
800 *
801 * If @next_pool is not NULL, we will allocate from @next_pool before going to
802 * the memory group manager. Similarly pages can spill over to @next_pool when
803 * @pool is full. Pages are zeroed before they spill over to another pool, to
804 * prevent leaking information between applications.
805 *
806 * A shrinker is registered so that Linux mm can reclaim pages from the pool as
807 * needed.
808 *
809 * Return: 0 on success, negative -errno on error
810 */
811 int kbase_mem_pool_init(struct kbase_mem_pool *pool,
812 const struct kbase_mem_pool_config *config,
813 unsigned int order,
814 int group_id,
815 struct kbase_device *kbdev,
816 struct kbase_mem_pool *next_pool);
817
818 /**
819 * kbase_mem_pool_term - Destroy a memory pool
820 * @pool: Memory pool to destroy
821 *
822 * Pages in the pool will spill over to @next_pool (if available) or freed to
823 * the kernel.
824 */
825 void kbase_mem_pool_term(struct kbase_mem_pool *pool);
826
827 /**
828 * kbase_mem_pool_alloc - Allocate a page from memory pool
829 * @pool: Memory pool to allocate from
830 *
831 * Allocations from the pool are made as follows:
832 * 1. If there are free pages in the pool, allocate a page from @pool.
833 * 2. Otherwise, if @next_pool is not NULL and has free pages, allocate a page
834 * from @next_pool.
835 * 3. Return NULL if no memory in the pool
836 *
837 * Return: Pointer to allocated page, or NULL if allocation failed.
838 *
839 * Note : This function should not be used if the pool lock is held. Use
840 * kbase_mem_pool_alloc_locked() instead.
841 */
842 struct page *kbase_mem_pool_alloc(struct kbase_mem_pool *pool);
843
844 /**
845 * kbase_mem_pool_alloc_locked - Allocate a page from memory pool
846 * @pool: Memory pool to allocate from
847 *
848 * If there are free pages in the pool, this function allocates a page from
849 * @pool. This function does not use @next_pool.
850 *
851 * Return: Pointer to allocated page, or NULL if allocation failed.
852 *
853 * Note : Caller must hold the pool lock.
854 */
855 struct page *kbase_mem_pool_alloc_locked(struct kbase_mem_pool *pool);
856
857 /**
858 * kbase_mem_pool_free - Free a page to memory pool
859 * @pool: Memory pool where page should be freed
860 * @page: Page to free to the pool
861 * @dirty: Whether some of the page may be dirty in the cache.
862 *
863 * Pages are freed to the pool as follows:
864 * 1. If @pool is not full, add @page to @pool.
865 * 2. Otherwise, if @next_pool is not NULL and not full, add @page to
866 * @next_pool.
867 * 3. Finally, free @page to the kernel.
868 *
869 * Note : This function should not be used if the pool lock is held. Use
870 * kbase_mem_pool_free_locked() instead.
871 */
872 void kbase_mem_pool_free(struct kbase_mem_pool *pool, struct page *page,
873 bool dirty);
874
875 /**
876 * kbase_mem_pool_free_locked - Free a page to memory pool
877 * @pool: Memory pool where page should be freed
878 * @p: Page to free to the pool
879 * @dirty: Whether some of the page may be dirty in the cache.
880 *
881 * If @pool is not full, this function adds @page to @pool. Otherwise, @page is
882 * freed to the kernel. This function does not use @next_pool.
883 *
884 * Note : Caller must hold the pool lock.
885 */
886 void kbase_mem_pool_free_locked(struct kbase_mem_pool *pool, struct page *p,
887 bool dirty);
888
889 /**
890 * kbase_mem_pool_alloc_pages - Allocate pages from memory pool
891 * @pool: Memory pool to allocate from
892 * @nr_4k_pages: Number of pages to allocate
893 * @pages: Pointer to array where the physical address of the allocated
894 * pages will be stored.
895 * @partial_allowed: If fewer pages allocated is allowed
896 *
897 * Like kbase_mem_pool_alloc() but optimized for allocating many pages.
898 *
899 * Return:
900 * On success number of pages allocated (could be less than nr_pages if
901 * partial_allowed).
902 * On error an error code.
903 *
904 * Note : This function should not be used if the pool lock is held. Use
905 * kbase_mem_pool_alloc_pages_locked() instead.
906 *
907 * The caller must not hold vm_lock, as this could cause a deadlock if
908 * the kernel OoM killer runs. If the caller must allocate pages while holding
909 * this lock, it should use kbase_mem_pool_alloc_pages_locked() instead.
910 */
911 int kbase_mem_pool_alloc_pages(struct kbase_mem_pool *pool, size_t nr_4k_pages,
912 struct tagged_addr *pages, bool partial_allowed);
913
914 /**
915 * kbase_mem_pool_alloc_pages_locked - Allocate pages from memory pool
916 * @pool: Memory pool to allocate from
917 * @nr_4k_pages: Number of pages to allocate
918 * @pages: Pointer to array where the physical address of the allocated
919 * pages will be stored.
920 *
921 * Like kbase_mem_pool_alloc() but optimized for allocating many pages. This
922 * version does not allocate new pages from the kernel, and therefore will never
923 * trigger the OoM killer. Therefore, it can be run while the vm_lock is held.
924 *
925 * As new pages can not be allocated, the caller must ensure there are
926 * sufficient pages in the pool. Usage of this function should look like :
927 *
928 * kbase_gpu_vm_lock(kctx);
929 * kbase_mem_pool_lock(pool)
930 * while (kbase_mem_pool_size(pool) < pages_required) {
931 * kbase_mem_pool_unlock(pool)
932 * kbase_gpu_vm_unlock(kctx);
933 * kbase_mem_pool_grow(pool)
934 * kbase_gpu_vm_lock(kctx);
935 * kbase_mem_pool_lock(pool)
936 * }
937 * kbase_mem_pool_alloc_pages_locked(pool)
938 * kbase_mem_pool_unlock(pool)
939 * Perform other processing that requires vm_lock...
940 * kbase_gpu_vm_unlock(kctx);
941 *
942 * This ensures that the pool can be grown to the required size and that the
943 * allocation can complete without another thread using the newly grown pages.
944 *
945 * Return:
946 * On success number of pages allocated.
947 * On error an error code.
948 *
949 * Note : Caller must hold the pool lock.
950 */
951 int kbase_mem_pool_alloc_pages_locked(struct kbase_mem_pool *pool,
952 size_t nr_4k_pages, struct tagged_addr *pages);
953
954 /**
955 * kbase_mem_pool_free_pages - Free pages to memory pool
956 * @pool: Memory pool where pages should be freed
957 * @nr_pages: Number of pages to free
958 * @pages: Pointer to array holding the physical addresses of the pages to
959 * free.
960 * @dirty: Whether any pages may be dirty in the cache.
961 * @reclaimed: Whether the pages where reclaimable and thus should bypass
962 * the pool and go straight to the kernel.
963 *
964 * Like kbase_mem_pool_free() but optimized for freeing many pages.
965 */
966 void kbase_mem_pool_free_pages(struct kbase_mem_pool *pool, size_t nr_pages,
967 struct tagged_addr *pages, bool dirty, bool reclaimed);
968
969 /**
970 * kbase_mem_pool_free_pages_locked - Free pages to memory pool
971 * @pool: Memory pool where pages should be freed
972 * @nr_pages: Number of pages to free
973 * @pages: Pointer to array holding the physical addresses of the pages to
974 * free.
975 * @dirty: Whether any pages may be dirty in the cache.
976 * @reclaimed: Whether the pages where reclaimable and thus should bypass
977 * the pool and go straight to the kernel.
978 *
979 * Like kbase_mem_pool_free() but optimized for freeing many pages.
980 */
981 void kbase_mem_pool_free_pages_locked(struct kbase_mem_pool *pool,
982 size_t nr_pages, struct tagged_addr *pages, bool dirty,
983 bool reclaimed);
984
985 /**
986 * kbase_mem_pool_size - Get number of free pages in memory pool
987 * @pool: Memory pool to inspect
988 *
989 * Note: the size of the pool may in certain corner cases exceed @max_size!
990 *
991 * Return: Number of free pages in the pool
992 */
kbase_mem_pool_size(struct kbase_mem_pool * pool)993 static inline size_t kbase_mem_pool_size(struct kbase_mem_pool *pool)
994 {
995 return READ_ONCE(pool->cur_size);
996 }
997
998 /**
999 * kbase_mem_pool_max_size - Get maximum number of free pages in memory pool
1000 * @pool: Memory pool to inspect
1001 *
1002 * Return: Maximum number of free pages in the pool
1003 */
kbase_mem_pool_max_size(struct kbase_mem_pool * pool)1004 static inline size_t kbase_mem_pool_max_size(struct kbase_mem_pool *pool)
1005 {
1006 return pool->max_size;
1007 }
1008
1009
1010 /**
1011 * kbase_mem_pool_set_max_size - Set maximum number of free pages in memory pool
1012 * @pool: Memory pool to inspect
1013 * @max_size: Maximum number of free pages the pool can hold
1014 *
1015 * If @max_size is reduced, the pool will be shrunk to adhere to the new limit.
1016 * For details see kbase_mem_pool_shrink().
1017 */
1018 void kbase_mem_pool_set_max_size(struct kbase_mem_pool *pool, size_t max_size);
1019
1020 /**
1021 * kbase_mem_pool_grow - Grow the pool
1022 * @pool: Memory pool to grow
1023 * @nr_to_grow: Number of pages to add to the pool
1024 *
1025 * Adds @nr_to_grow pages to the pool. Note that this may cause the pool to
1026 * become larger than the maximum size specified.
1027 *
1028 * Returns: 0 on success, -ENOMEM if unable to allocate sufficent pages
1029 */
1030 int kbase_mem_pool_grow(struct kbase_mem_pool *pool, size_t nr_to_grow);
1031
1032 /**
1033 * kbase_mem_pool_trim - Grow or shrink the pool to a new size
1034 * @pool: Memory pool to trim
1035 * @new_size: New number of pages in the pool
1036 *
1037 * If @new_size > @cur_size, fill the pool with new pages from the kernel, but
1038 * not above the max_size for the pool.
1039 * If @new_size < @cur_size, shrink the pool by freeing pages to the kernel.
1040 */
1041 void kbase_mem_pool_trim(struct kbase_mem_pool *pool, size_t new_size);
1042
1043 /**
1044 * kbase_mem_pool_mark_dying - Mark that this pool is dying
1045 * @pool: Memory pool
1046 *
1047 * This will cause any ongoing allocation operations (eg growing on page fault)
1048 * to be terminated.
1049 */
1050 void kbase_mem_pool_mark_dying(struct kbase_mem_pool *pool);
1051
1052 /**
1053 * kbase_mem_alloc_page - Allocate a new page for a device
1054 * @pool: Memory pool to allocate a page from
1055 *
1056 * Most uses should use kbase_mem_pool_alloc to allocate a page. However that
1057 * function can fail in the event the pool is empty.
1058 *
1059 * Return: A new page or NULL if no memory
1060 */
1061 struct page *kbase_mem_alloc_page(struct kbase_mem_pool *pool);
1062
1063 /**
1064 * kbase_region_tracker_init - Initialize the region tracker data structure
1065 * @kctx: kbase context
1066 *
1067 * Return: 0 if success, negative error code otherwise.
1068 */
1069 int kbase_region_tracker_init(struct kbase_context *kctx);
1070
1071 /**
1072 * kbase_region_tracker_init_jit - Initialize the just-in-time memory
1073 * allocation region
1074 * @kctx: Kbase context.
1075 * @jit_va_pages: Size of the JIT region in pages.
1076 * @max_allocations: Maximum number of allocations allowed for the JIT region.
1077 * Valid range is 0..%BASE_JIT_ALLOC_COUNT.
1078 * @trim_level: Trim level for the JIT region.
1079 * Valid range is 0..%BASE_JIT_MAX_TRIM_LEVEL.
1080 * @group_id: The physical group ID from which to allocate JIT memory.
1081 * Valid range is 0..(%MEMORY_GROUP_MANAGER_NR_GROUPS-1).
1082 * @phys_pages_limit: Maximum number of physical pages to use to back the JIT
1083 * region. Must not exceed @jit_va_pages.
1084 *
1085 * Return: 0 if success, negative error code otherwise.
1086 */
1087 int kbase_region_tracker_init_jit(struct kbase_context *kctx, u64 jit_va_pages,
1088 int max_allocations, int trim_level, int group_id,
1089 u64 phys_pages_limit);
1090
1091 /**
1092 * kbase_region_tracker_init_exec - Initialize the GPU-executable memory region
1093 * @kctx: kbase context
1094 * @exec_va_pages: Size of the JIT region in pages.
1095 * It must not be greater than 4 GB.
1096 *
1097 * Return: 0 if success, negative error code otherwise.
1098 */
1099 int kbase_region_tracker_init_exec(struct kbase_context *kctx, u64 exec_va_pages);
1100
1101 /**
1102 * kbase_region_tracker_term - Terminate the JIT region
1103 * @kctx: kbase context
1104 */
1105 void kbase_region_tracker_term(struct kbase_context *kctx);
1106
1107 /**
1108 * kbase_region_tracker_term_rbtree - Free memory for a region tracker
1109 *
1110 * This will free all the regions within the region tracker
1111 *
1112 * @rbtree: Region tracker tree root
1113 */
1114 void kbase_region_tracker_term_rbtree(struct rb_root *rbtree);
1115
1116 struct kbase_va_region *kbase_region_tracker_find_region_enclosing_address(
1117 struct kbase_context *kctx, u64 gpu_addr);
1118 struct kbase_va_region *kbase_find_region_enclosing_address(
1119 struct rb_root *rbtree, u64 gpu_addr);
1120
1121 /**
1122 * Check that a pointer is actually a valid region.
1123 * @kctx: kbase context containing the region
1124 * @gpu_addr: pointer to check
1125 *
1126 * Must be called with context lock held.
1127 */
1128 struct kbase_va_region *kbase_region_tracker_find_region_base_address(
1129 struct kbase_context *kctx, u64 gpu_addr);
1130 struct kbase_va_region *kbase_find_region_base_address(struct rb_root *rbtree,
1131 u64 gpu_addr);
1132
1133 struct kbase_va_region *kbase_alloc_free_region(struct rb_root *rbtree,
1134 u64 start_pfn, size_t nr_pages, int zone);
1135 void kbase_free_alloced_region(struct kbase_va_region *reg);
1136 int kbase_add_va_region(struct kbase_context *kctx, struct kbase_va_region *reg,
1137 u64 addr, size_t nr_pages, size_t align);
1138 int kbase_add_va_region_rbtree(struct kbase_device *kbdev,
1139 struct kbase_va_region *reg, u64 addr, size_t nr_pages,
1140 size_t align);
1141
1142 bool kbase_check_alloc_flags(unsigned long flags);
1143 bool kbase_check_import_flags(unsigned long flags);
1144
1145 /**
1146 * kbase_check_alloc_sizes - check user space sizes parameters for an
1147 * allocation
1148 *
1149 * @kctx: kbase context
1150 * @flags: The flags passed from user space
1151 * @va_pages: The size of the requested region, in pages.
1152 * @commit_pages: Number of pages to commit initially.
1153 * @extension: Number of pages to grow by on GPU page fault and/or alignment
1154 * (depending on flags)
1155 *
1156 * Makes checks on the size parameters passed in from user space for a memory
1157 * allocation call, with respect to the flags requested.
1158 *
1159 * Return: 0 if sizes are valid for these flags, negative error code otherwise
1160 */
1161 int kbase_check_alloc_sizes(struct kbase_context *kctx, unsigned long flags,
1162 u64 va_pages, u64 commit_pages, u64 extension);
1163
1164 /**
1165 * kbase_update_region_flags - Convert user space flags to kernel region flags
1166 *
1167 * @kctx: kbase context
1168 * @reg: The region to update the flags on
1169 * @flags: The flags passed from user space
1170 *
1171 * The user space flag BASE_MEM_COHERENT_SYSTEM_REQUIRED will be rejected and
1172 * this function will fail if the system does not support system coherency.
1173 *
1174 * Return: 0 if successful, -EINVAL if the flags are not supported
1175 */
1176 int kbase_update_region_flags(struct kbase_context *kctx,
1177 struct kbase_va_region *reg, unsigned long flags);
1178
1179 void kbase_gpu_vm_lock(struct kbase_context *kctx);
1180 void kbase_gpu_vm_unlock(struct kbase_context *kctx);
1181
1182 int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size);
1183
1184 /**
1185 * Register region and map it on the GPU.
1186 * @kctx: kbase context containing the region
1187 * @reg: the region to add
1188 * @addr: the address to insert the region at
1189 * @nr_pages: the number of pages in the region
1190 * @align: the minimum alignment in pages
1191 * @mmu_sync_info: Indicates whether this call is synchronous wrt MMU ops.
1192 *
1193 * Call kbase_add_va_region() and map the region on the GPU.
1194 */
1195 int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg,
1196 u64 addr, size_t nr_pages, size_t align,
1197 enum kbase_caller_mmu_sync_info mmu_sync_info);
1198
1199 /**
1200 * Remove the region from the GPU and unregister it.
1201 * @kctx: KBase context
1202 * @reg: The region to remove
1203 *
1204 * Must be called with context lock held.
1205 */
1206 int kbase_gpu_munmap(struct kbase_context *kctx, struct kbase_va_region *reg);
1207
1208 /**
1209 * kbase_mmu_update - Configure an address space on the GPU to the specified
1210 * MMU tables
1211 *
1212 * The caller has the following locking conditions:
1213 * - It must hold kbase_device->mmu_hw_mutex
1214 * - It must hold the hwaccess_lock
1215 *
1216 * @kbdev: Kbase device structure
1217 * @mmut: The set of MMU tables to be configured on the address space
1218 * @as_nr: The address space to be configured
1219 */
1220 void kbase_mmu_update(struct kbase_device *kbdev, struct kbase_mmu_table *mmut,
1221 int as_nr);
1222
1223 /**
1224 * kbase_mmu_disable() - Disable the MMU for a previously active kbase context.
1225 * @kctx: Kbase context
1226 *
1227 * Disable and perform the required cache maintenance to remove the all
1228 * data from provided kbase context from the GPU caches.
1229 *
1230 * The caller has the following locking conditions:
1231 * - It must hold kbase_device->mmu_hw_mutex
1232 * - It must hold the hwaccess_lock
1233 */
1234 void kbase_mmu_disable(struct kbase_context *kctx);
1235
1236 /**
1237 * kbase_mmu_disable_as() - Set the MMU to unmapped mode for the specified
1238 * address space.
1239 * @kbdev: Kbase device
1240 * @as_nr: The address space number to set to unmapped.
1241 *
1242 * This function must only be called during reset/power-up and it used to
1243 * ensure the registers are in a known state.
1244 *
1245 * The caller must hold kbdev->mmu_hw_mutex.
1246 */
1247 void kbase_mmu_disable_as(struct kbase_device *kbdev, int as_nr);
1248
1249 void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat);
1250
1251 /**
1252 * kbase_mmu_dump() - Dump the MMU tables to a buffer.
1253 *
1254 * This function allocates a buffer (of @c nr_pages pages) to hold a dump
1255 * of the MMU tables and fills it. If the buffer is too small
1256 * then the return value will be NULL.
1257 *
1258 * The GPU vm lock must be held when calling this function.
1259 *
1260 * The buffer returned should be freed with @ref vfree when it is no longer
1261 * required.
1262 *
1263 * @kctx: The kbase context to dump
1264 * @nr_pages: The number of pages to allocate for the buffer.
1265 *
1266 * Return: The address of the buffer containing the MMU dump or NULL on error
1267 * (including if the @c nr_pages is too small)
1268 */
1269 void *kbase_mmu_dump(struct kbase_context *kctx, int nr_pages);
1270
1271 /**
1272 * kbase_sync_now - Perform cache maintenance on a memory region
1273 *
1274 * @kctx: The kbase context of the region
1275 * @sset: A syncset structure describing the region and direction of the
1276 * synchronisation required
1277 *
1278 * Return: 0 on success or error code
1279 */
1280 int kbase_sync_now(struct kbase_context *kctx, struct basep_syncset *sset);
1281 void kbase_sync_single(struct kbase_context *kctx, struct tagged_addr cpu_pa,
1282 struct tagged_addr gpu_pa, off_t offset, size_t size,
1283 enum kbase_sync_type sync_fn);
1284
1285 /* OS specific functions */
1286 int kbase_mem_free(struct kbase_context *kctx, u64 gpu_addr);
1287 int kbase_mem_free_region(struct kbase_context *kctx, struct kbase_va_region *reg);
1288 void kbase_os_mem_map_lock(struct kbase_context *kctx);
1289 void kbase_os_mem_map_unlock(struct kbase_context *kctx);
1290
1291 /**
1292 * kbasep_os_process_page_usage_update() - Update the memory allocation
1293 * counters for the current process.
1294 *
1295 * OS specific call to updates the current memory allocation counters
1296 * for the current process with the supplied delta.
1297 *
1298 * @kctx: The kbase context
1299 * @pages: The desired delta to apply to the memory usage counters.
1300 */
1301
1302 void kbasep_os_process_page_usage_update(struct kbase_context *kctx, int pages);
1303
1304 /**
1305 * kbase_process_page_usage_inc() - Add to the memory allocation counters for
1306 * the current process
1307 *
1308 * OS specific call to add to the current memory allocation counters for
1309 * the current process by the supplied amount.
1310 *
1311 * @kctx: The kernel base context used for the allocation.
1312 * @pages: The desired delta to apply to the memory usage counters.
1313 */
1314
kbase_process_page_usage_inc(struct kbase_context * kctx,int pages)1315 static inline void kbase_process_page_usage_inc(struct kbase_context *kctx, int pages)
1316 {
1317 kbasep_os_process_page_usage_update(kctx, pages);
1318 }
1319
1320 /**
1321 * kbase_process_page_usage_dec() - Subtract from the memory allocation
1322 * counters for the current process.
1323 *
1324 * OS specific call to subtract from the current memory allocation counters
1325 * for the current process by the supplied amount.
1326 *
1327 * @kctx: The kernel base context used for the allocation.
1328 * @pages: The desired delta to apply to the memory usage counters.
1329 */
1330
kbase_process_page_usage_dec(struct kbase_context * kctx,int pages)1331 static inline void kbase_process_page_usage_dec(struct kbase_context *kctx, int pages)
1332 {
1333 kbasep_os_process_page_usage_update(kctx, 0 - pages);
1334 }
1335
1336 /**
1337 * kbasep_find_enclosing_cpu_mapping_offset() - Find the offset of the CPU
1338 * mapping of a memory allocation containing a given address range
1339 *
1340 * Searches for a CPU mapping of any part of any region that fully encloses the
1341 * CPU virtual address range specified by @uaddr and @size. Returns a failure
1342 * indication if only part of the address range lies within a CPU mapping.
1343 *
1344 * @kctx: The kernel base context used for the allocation.
1345 * @uaddr: Start of the CPU virtual address range.
1346 * @size: Size of the CPU virtual address range (in bytes).
1347 * @offset: The offset from the start of the allocation to the specified CPU
1348 * virtual address.
1349 *
1350 * Return: 0 if offset was obtained successfully. Error code otherwise.
1351 */
1352 int kbasep_find_enclosing_cpu_mapping_offset(
1353 struct kbase_context *kctx,
1354 unsigned long uaddr, size_t size, u64 *offset);
1355
1356 /**
1357 * kbasep_find_enclosing_gpu_mapping_start_and_offset() - Find the address of
1358 * the start of GPU virtual memory region which encloses @gpu_addr for the
1359 * @size length in bytes
1360 *
1361 * Searches for the memory region in GPU virtual memory space which contains
1362 * the region defined by the @gpu_addr and @size, where @gpu_addr is the
1363 * beginning and @size the length in bytes of the provided region. If found,
1364 * the location of the start address of the GPU virtual memory region is
1365 * passed in @start pointer and the location of the offset of the region into
1366 * the GPU virtual memory region is passed in @offset pointer.
1367 *
1368 * @kctx: The kernel base context within which the memory is searched.
1369 * @gpu_addr: GPU virtual address for which the region is sought; defines
1370 * the beginning of the provided region.
1371 * @size: The length (in bytes) of the provided region for which the
1372 * GPU virtual memory region is sought.
1373 * @start: Pointer to the location where the address of the start of
1374 * the found GPU virtual memory region is.
1375 * @offset: Pointer to the location where the offset of @gpu_addr into
1376 * the found GPU virtual memory region is.
1377 */
1378 int kbasep_find_enclosing_gpu_mapping_start_and_offset(
1379 struct kbase_context *kctx,
1380 u64 gpu_addr, size_t size, u64 *start, u64 *offset);
1381
1382 /**
1383 * kbase_alloc_phy_pages_helper - Allocates physical pages.
1384 * @alloc: allocation object to add pages to
1385 * @nr_pages_requested: number of physical pages to allocate
1386 *
1387 * Allocates \a nr_pages_requested and updates the alloc object.
1388 *
1389 * Return: 0 if all pages have been successfully allocated. Error code otherwise
1390 *
1391 * Note : The caller must not hold vm_lock, as this could cause a deadlock if
1392 * the kernel OoM killer runs. If the caller must allocate pages while holding
1393 * this lock, it should use kbase_mem_pool_alloc_pages_locked() instead.
1394 *
1395 * This function cannot be used from interrupt context
1396 */
1397 int kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc *alloc,
1398 size_t nr_pages_requested);
1399
1400 /**
1401 * kbase_alloc_phy_pages_helper_locked - Allocates physical pages.
1402 * @alloc: allocation object to add pages to
1403 * @pool: Memory pool to allocate from
1404 * @nr_pages_requested: number of physical pages to allocate
1405 * @prealloc_sa: Information about the partial allocation if the amount
1406 * of memory requested is not a multiple of 2MB. One
1407 * instance of struct kbase_sub_alloc must be allocated by
1408 * the caller iff CONFIG_MALI_2MB_ALLOC is enabled.
1409 *
1410 * Allocates \a nr_pages_requested and updates the alloc object. This function
1411 * does not allocate new pages from the kernel, and therefore will never trigger
1412 * the OoM killer. Therefore, it can be run while the vm_lock is held.
1413 *
1414 * As new pages can not be allocated, the caller must ensure there are
1415 * sufficient pages in the pool. Usage of this function should look like :
1416 *
1417 * kbase_gpu_vm_lock(kctx);
1418 * kbase_mem_pool_lock(pool)
1419 * while (kbase_mem_pool_size(pool) < pages_required) {
1420 * kbase_mem_pool_unlock(pool)
1421 * kbase_gpu_vm_unlock(kctx);
1422 * kbase_mem_pool_grow(pool)
1423 * kbase_gpu_vm_lock(kctx);
1424 * kbase_mem_pool_lock(pool)
1425 * }
1426 * kbase_alloc_phy_pages_helper_locked(pool)
1427 * kbase_mem_pool_unlock(pool)
1428 * Perform other processing that requires vm_lock...
1429 * kbase_gpu_vm_unlock(kctx);
1430 *
1431 * This ensures that the pool can be grown to the required size and that the
1432 * allocation can complete without another thread using the newly grown pages.
1433 *
1434 * If CONFIG_MALI_2MB_ALLOC is defined and the allocation is >= 2MB, then
1435 * @pool must be alloc->imported.native.kctx->lp_mem_pool. Otherwise it must be
1436 * alloc->imported.native.kctx->mem_pool.
1437 * @prealloc_sa is used to manage the non-2MB sub-allocation. It has to be
1438 * pre-allocated because we must not sleep (due to the usage of kmalloc())
1439 * whilst holding pool->pool_lock.
1440 * @prealloc_sa shall be set to NULL if it has been consumed by this function
1441 * to indicate that the caller must not free it.
1442 *
1443 * Return: Pointer to array of allocated pages. NULL on failure.
1444 *
1445 * Note : Caller must hold pool->pool_lock
1446 */
1447 struct tagged_addr *kbase_alloc_phy_pages_helper_locked(
1448 struct kbase_mem_phy_alloc *alloc, struct kbase_mem_pool *pool,
1449 size_t nr_pages_requested,
1450 struct kbase_sub_alloc **prealloc_sa);
1451
1452 /**
1453 * kbase_free_phy_pages_helper() - Free physical pages.
1454 *
1455 * Frees \a nr_pages and updates the alloc object.
1456 *
1457 * @alloc: allocation object to free pages from
1458 * @nr_pages_to_free: number of physical pages to free
1459 *
1460 * Return: 0 on success, otherwise a negative error code
1461 */
1462 int kbase_free_phy_pages_helper(struct kbase_mem_phy_alloc *alloc, size_t nr_pages_to_free);
1463
1464 /**
1465 * kbase_free_phy_pages_helper_locked - Free pages allocated with
1466 * kbase_alloc_phy_pages_helper_locked()
1467 * @alloc: Allocation object to free pages from
1468 * @pool: Memory pool to return freed pages to
1469 * @pages: Pages allocated by kbase_alloc_phy_pages_helper_locked()
1470 * @nr_pages_to_free: Number of physical pages to free
1471 *
1472 * This function atomically frees pages allocated with
1473 * kbase_alloc_phy_pages_helper_locked(). @pages is the pointer to the page
1474 * array that is returned by that function. @pool must be the pool that the
1475 * pages were originally allocated from.
1476 *
1477 * If the mem_pool has been unlocked since the allocation then
1478 * kbase_free_phy_pages_helper() should be used instead.
1479 */
1480 void kbase_free_phy_pages_helper_locked(struct kbase_mem_phy_alloc *alloc,
1481 struct kbase_mem_pool *pool, struct tagged_addr *pages,
1482 size_t nr_pages_to_free);
1483
kbase_set_dma_addr(struct page * p,dma_addr_t dma_addr)1484 static inline void kbase_set_dma_addr(struct page *p, dma_addr_t dma_addr)
1485 {
1486 SetPagePrivate(p);
1487 if (sizeof(dma_addr_t) > sizeof(p->private)) {
1488 /* on 32-bit ARM with LPAE dma_addr_t becomes larger, but the
1489 * private field stays the same. So we have to be clever and
1490 * use the fact that we only store DMA addresses of whole pages,
1491 * so the low bits should be zero
1492 */
1493 KBASE_DEBUG_ASSERT(!(dma_addr & (PAGE_SIZE - 1)));
1494 set_page_private(p, dma_addr >> PAGE_SHIFT);
1495 } else {
1496 set_page_private(p, dma_addr);
1497 }
1498 }
1499
kbase_dma_addr(struct page * p)1500 static inline dma_addr_t kbase_dma_addr(struct page *p)
1501 {
1502 if (sizeof(dma_addr_t) > sizeof(p->private))
1503 return ((dma_addr_t)page_private(p)) << PAGE_SHIFT;
1504
1505 return (dma_addr_t)page_private(p);
1506 }
1507
kbase_clear_dma_addr(struct page * p)1508 static inline void kbase_clear_dma_addr(struct page *p)
1509 {
1510 ClearPagePrivate(p);
1511 }
1512
1513 /**
1514 * kbase_flush_mmu_wqs() - Flush MMU workqueues.
1515 * @kbdev: Device pointer.
1516 *
1517 * This function will cause any outstanding page or bus faults to be processed.
1518 * It should be called prior to powering off the GPU.
1519 */
1520 void kbase_flush_mmu_wqs(struct kbase_device *kbdev);
1521
1522 /**
1523 * kbase_sync_single_for_device - update physical memory and give GPU ownership
1524 * @kbdev: Device pointer
1525 * @handle: DMA address of region
1526 * @size: Size of region to sync
1527 * @dir: DMA data direction
1528 */
1529
1530 void kbase_sync_single_for_device(struct kbase_device *kbdev, dma_addr_t handle,
1531 size_t size, enum dma_data_direction dir);
1532
1533 /**
1534 * kbase_sync_single_for_cpu - update physical memory and give CPU ownership
1535 * @kbdev: Device pointer
1536 * @handle: DMA address of region
1537 * @size: Size of region to sync
1538 * @dir: DMA data direction
1539 */
1540
1541 void kbase_sync_single_for_cpu(struct kbase_device *kbdev, dma_addr_t handle,
1542 size_t size, enum dma_data_direction dir);
1543
1544 #if IS_ENABLED(CONFIG_DEBUG_FS)
1545 /**
1546 * kbase_jit_debugfs_init - Add per context debugfs entry for JIT.
1547 * @kctx: kbase context
1548 */
1549 void kbase_jit_debugfs_init(struct kbase_context *kctx);
1550 #endif /* CONFIG_DEBUG_FS */
1551
1552 /**
1553 * kbase_jit_init - Initialize the JIT memory pool management
1554 * @kctx: kbase context
1555 *
1556 * Returns zero on success or negative error number on failure.
1557 */
1558 int kbase_jit_init(struct kbase_context *kctx);
1559
1560 /**
1561 * kbase_jit_allocate - Allocate JIT memory
1562 * @kctx: kbase context
1563 * @info: JIT allocation information
1564 * @ignore_pressure_limit: Whether the JIT memory pressure limit is ignored
1565 *
1566 * Return: JIT allocation on success or NULL on failure.
1567 */
1568 struct kbase_va_region *kbase_jit_allocate(struct kbase_context *kctx,
1569 const struct base_jit_alloc_info *info,
1570 bool ignore_pressure_limit);
1571
1572 /**
1573 * kbase_jit_free - Free a JIT allocation
1574 * @kctx: kbase context
1575 * @reg: JIT allocation
1576 *
1577 * Frees a JIT allocation and places it into the free pool for later reuse.
1578 */
1579 void kbase_jit_free(struct kbase_context *kctx, struct kbase_va_region *reg);
1580
1581 /**
1582 * kbase_jit_backing_lost - Inform JIT that an allocation has lost backing
1583 * @reg: JIT allocation
1584 */
1585 void kbase_jit_backing_lost(struct kbase_va_region *reg);
1586
1587 /**
1588 * kbase_jit_evict - Evict a JIT allocation from the pool
1589 * @kctx: kbase context
1590 *
1591 * Evict the least recently used JIT allocation from the pool. This can be
1592 * required if normal VA allocations are failing due to VA exhaustion.
1593 *
1594 * Return: True if a JIT allocation was freed, false otherwise.
1595 */
1596 bool kbase_jit_evict(struct kbase_context *kctx);
1597
1598 /**
1599 * kbase_jit_term - Terminate the JIT memory pool management
1600 * @kctx: kbase context
1601 */
1602 void kbase_jit_term(struct kbase_context *kctx);
1603
1604 #if MALI_JIT_PRESSURE_LIMIT_BASE
1605 /**
1606 * kbase_trace_jit_report_gpu_mem_trace_enabled - variant of
1607 * kbase_trace_jit_report_gpu_mem() that should only be called once the
1608 * corresponding tracepoint is verified to be enabled
1609 * @kctx: kbase context
1610 * @reg: Just-in-time memory region to trace
1611 * @flags: combination of values from enum kbase_jit_report_flags
1612 */
1613 void kbase_trace_jit_report_gpu_mem_trace_enabled(struct kbase_context *kctx,
1614 struct kbase_va_region *reg, unsigned int flags);
1615 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1616
1617 /**
1618 * kbase_trace_jit_report_gpu_mem - Trace information about the GPU memory used
1619 * to make a JIT report
1620 * @kctx: kbase context
1621 * @reg: Just-in-time memory region to trace
1622 * @flags: combination of values from enum kbase_jit_report_flags
1623 *
1624 * Information is traced using the trace_mali_jit_report_gpu_mem() tracepoint.
1625 *
1626 * In case that tracepoint is not enabled, this function should have the same
1627 * low overheads as a tracepoint itself (i.e. use of 'jump labels' to avoid
1628 * conditional branches)
1629 *
1630 * This can take the reg_lock on @kctx, do not use in places where this lock is
1631 * already held.
1632 *
1633 * Note: this has to be a macro because at this stage the tracepoints have not
1634 * been included. Also gives no opportunity for the compiler to mess up
1635 * inlining it.
1636 */
1637 #if MALI_JIT_PRESSURE_LIMIT_BASE
1638 #define kbase_trace_jit_report_gpu_mem(kctx, reg, flags) \
1639 do { \
1640 if (trace_mali_jit_report_gpu_mem_enabled()) \
1641 kbase_trace_jit_report_gpu_mem_trace_enabled( \
1642 (kctx), (reg), (flags)); \
1643 } while (0)
1644 #else
1645 #define kbase_trace_jit_report_gpu_mem(kctx, reg, flags) \
1646 CSTD_NOP(kctx, reg, flags)
1647 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1648
1649 #if MALI_JIT_PRESSURE_LIMIT_BASE
1650 /**
1651 * kbase_jit_report_update_pressure - safely update the JIT physical page
1652 * pressure and JIT region's estimate of used_pages
1653 * @kctx: kbase context, to update the current physical pressure
1654 * @reg: Just-in-time memory region to update with @new_used_pages
1655 * @new_used_pages: new value of number of pages used in the JIT region
1656 * @flags: combination of values from enum kbase_jit_report_flags
1657 *
1658 * Takes care of:
1659 * - correctly updating the pressure given the current reg->used_pages and
1660 * new_used_pages
1661 * - then updating the %kbase_va_region used_pages member
1662 *
1663 * Precondition:
1664 * - new_used_pages <= reg->nr_pages
1665 */
1666 void kbase_jit_report_update_pressure(struct kbase_context *kctx,
1667 struct kbase_va_region *reg, u64 new_used_pages,
1668 unsigned int flags);
1669
1670 /**
1671 * jit_trim_necessary_pages() - calculate and trim the least pages possible to
1672 * satisfy a new JIT allocation
1673 *
1674 * @kctx: Pointer to the kbase context
1675 * @needed_pages: Number of JIT physical pages by which trimming is requested.
1676 * The actual number of pages trimmed could differ.
1677 *
1678 * Before allocating a new just-in-time memory region or reusing a previous
1679 * one, ensure that the total JIT physical page usage also will not exceed the
1680 * pressure limit.
1681 *
1682 * If there are no reported-on allocations, then we already guarantee this will
1683 * be the case - because our current pressure then only comes from the va_pages
1684 * of each JIT region, hence JIT physical page usage is guaranteed to be
1685 * bounded by this.
1686 *
1687 * However as soon as JIT allocations become "reported on", the pressure is
1688 * lowered to allow new JIT regions to be allocated. It is after such a point
1689 * that the total JIT physical page usage could (either now or in the future on
1690 * a grow-on-GPU-page-fault) exceed the pressure limit, but only on newly
1691 * allocated JIT regions. Hence, trim any "reported on" regions.
1692 *
1693 * Any pages freed will go into the pool and be allocated from there in
1694 * kbase_mem_alloc().
1695 */
1696 void kbase_jit_trim_necessary_pages(struct kbase_context *kctx,
1697 size_t needed_pages);
1698
1699 /*
1700 * Same as kbase_jit_request_phys_increase(), except that Caller is supposed
1701 * to take jit_evict_lock also on @kctx before calling this function.
1702 */
1703 static inline void
kbase_jit_request_phys_increase_locked(struct kbase_context * kctx,size_t needed_pages)1704 kbase_jit_request_phys_increase_locked(struct kbase_context *kctx,
1705 size_t needed_pages)
1706 {
1707 #if !MALI_USE_CSF
1708 lockdep_assert_held(&kctx->jctx.lock);
1709 #endif /* !MALI_USE_CSF */
1710 lockdep_assert_held(&kctx->reg_lock);
1711 lockdep_assert_held(&kctx->jit_evict_lock);
1712
1713 kctx->jit_phys_pages_to_be_allocated += needed_pages;
1714
1715 kbase_jit_trim_necessary_pages(kctx,
1716 kctx->jit_phys_pages_to_be_allocated);
1717 }
1718
1719 /**
1720 * kbase_jit_request_phys_increase() - Increment the backing pages count and do
1721 * the required trimming before allocating pages for a JIT allocation.
1722 *
1723 * @kctx: Pointer to the kbase context
1724 * @needed_pages: Number of pages to be allocated for the JIT allocation.
1725 *
1726 * This function needs to be called before allocating backing pages for a
1727 * just-in-time memory region. The backing pages are currently allocated when,
1728 *
1729 * - A new JIT region is created.
1730 * - An old JIT region is reused from the cached pool.
1731 * - GPU page fault occurs for the active JIT region.
1732 * - Backing is grown for the JIT region through the commit ioctl.
1733 *
1734 * This function would ensure that the total JIT physical page usage does not
1735 * exceed the pressure limit even when the backing pages get allocated
1736 * simultaneously for multiple JIT allocations from different threads.
1737 *
1738 * There should be a matching call to kbase_jit_done_phys_increase(), after
1739 * the pages have been allocated and accounted against the active JIT
1740 * allocation.
1741 *
1742 * Caller is supposed to take reg_lock on @kctx before calling this function.
1743 */
kbase_jit_request_phys_increase(struct kbase_context * kctx,size_t needed_pages)1744 static inline void kbase_jit_request_phys_increase(struct kbase_context *kctx,
1745 size_t needed_pages)
1746 {
1747 #if !MALI_USE_CSF
1748 lockdep_assert_held(&kctx->jctx.lock);
1749 #endif /* !MALI_USE_CSF */
1750 lockdep_assert_held(&kctx->reg_lock);
1751
1752 mutex_lock(&kctx->jit_evict_lock);
1753 kbase_jit_request_phys_increase_locked(kctx, needed_pages);
1754 mutex_unlock(&kctx->jit_evict_lock);
1755 }
1756
1757 /**
1758 * kbase_jit_done_phys_increase() - Decrement the backing pages count after the
1759 * allocation of pages for a JIT allocation.
1760 *
1761 * @kctx: Pointer to the kbase context
1762 * @needed_pages: Number of pages that were allocated for the JIT allocation.
1763 *
1764 * This function should be called after backing pages have been allocated and
1765 * accounted against the active JIT allocation.
1766 * The call should be made when the following have been satisfied:
1767 * when the allocation is on the jit_active_head.
1768 * when additional needed_pages have been allocated.
1769 * kctx->reg_lock was held during the above and has not yet been unlocked.
1770 * Failure to call this function before unlocking the kctx->reg_lock when
1771 * either the above have changed may result in over-accounting the memory.
1772 * This ensures kbase_jit_trim_necessary_pages() gets a consistent count of
1773 * the memory.
1774 *
1775 * A matching call to kbase_jit_request_phys_increase() should have been made,
1776 * before the allocation of backing pages.
1777 *
1778 * Caller is supposed to take reg_lock on @kctx before calling this function.
1779 */
kbase_jit_done_phys_increase(struct kbase_context * kctx,size_t needed_pages)1780 static inline void kbase_jit_done_phys_increase(struct kbase_context *kctx,
1781 size_t needed_pages)
1782 {
1783 lockdep_assert_held(&kctx->reg_lock);
1784
1785 WARN_ON(kctx->jit_phys_pages_to_be_allocated < needed_pages);
1786
1787 kctx->jit_phys_pages_to_be_allocated -= needed_pages;
1788 }
1789 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1790
1791 /**
1792 * kbase_has_exec_va_zone - EXEC_VA zone predicate
1793 *
1794 * Determine whether an EXEC_VA zone has been created for the GPU address space
1795 * of the given kbase context.
1796 *
1797 * @kctx: kbase context
1798 *
1799 * Return: True if the kbase context has an EXEC_VA zone.
1800 */
1801 bool kbase_has_exec_va_zone(struct kbase_context *kctx);
1802
1803 /**
1804 * kbase_map_external_resource - Map an external resource to the GPU.
1805 * @kctx: kbase context.
1806 * @reg: The region to map.
1807 * @locked_mm: The mm_struct which has been locked for this operation.
1808 *
1809 * Return: The physical allocation which backs the region on success or NULL
1810 * on failure.
1811 */
1812 struct kbase_mem_phy_alloc *kbase_map_external_resource(
1813 struct kbase_context *kctx, struct kbase_va_region *reg,
1814 struct mm_struct *locked_mm);
1815
1816 /**
1817 * kbase_unmap_external_resource - Unmap an external resource from the GPU.
1818 * @kctx: kbase context.
1819 * @reg: The region to unmap or NULL if it has already been released.
1820 * @alloc: The physical allocation being unmapped.
1821 */
1822 void kbase_unmap_external_resource(struct kbase_context *kctx,
1823 struct kbase_va_region *reg, struct kbase_mem_phy_alloc *alloc);
1824
1825 /**
1826 * kbase_unpin_user_buf_page - Unpin a page of a user buffer.
1827 * @page: page to unpin
1828 */
1829 void kbase_unpin_user_buf_page(struct page *page);
1830
1831 /**
1832 * kbase_jd_user_buf_pin_pages - Pin the pages of a user buffer.
1833 * @kctx: kbase context.
1834 * @reg: The region associated with the imported user buffer.
1835 *
1836 * To successfully pin the pages for a user buffer the current mm_struct must
1837 * be the same as the mm_struct of the user buffer. After successfully pinning
1838 * the pages further calls to this function succeed without doing work.
1839 *
1840 * Return: zero on success or negative number on failure.
1841 */
1842 int kbase_jd_user_buf_pin_pages(struct kbase_context *kctx,
1843 struct kbase_va_region *reg);
1844
1845 /**
1846 * kbase_sticky_resource_init - Initialize sticky resource management.
1847 * @kctx: kbase context
1848 *
1849 * Returns zero on success or negative error number on failure.
1850 */
1851 int kbase_sticky_resource_init(struct kbase_context *kctx);
1852
1853 /**
1854 * kbase_sticky_resource_acquire - Acquire a reference on a sticky resource.
1855 * @kctx: kbase context.
1856 * @gpu_addr: The GPU address of the external resource.
1857 *
1858 * Return: The metadata object which represents the binding between the
1859 * external resource and the kbase context on success or NULL on failure.
1860 */
1861 struct kbase_ctx_ext_res_meta *kbase_sticky_resource_acquire(
1862 struct kbase_context *kctx, u64 gpu_addr);
1863
1864 /**
1865 * kbase_sticky_resource_release - Release a reference on a sticky resource.
1866 * @kctx: kbase context.
1867 * @meta: Binding metadata.
1868 * @gpu_addr: GPU address of the external resource.
1869 *
1870 * If meta is NULL then gpu_addr will be used to scan the metadata list and
1871 * find the matching metadata (if any), otherwise the provided meta will be
1872 * used and gpu_addr will be ignored.
1873 *
1874 * Return: True if the release found the metadata and the reference was dropped.
1875 */
1876 bool kbase_sticky_resource_release(struct kbase_context *kctx,
1877 struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr);
1878
1879 /**
1880 * kbase_sticky_resource_release_force - Release a sticky resource.
1881 * @kctx: kbase context.
1882 * @meta: Binding metadata.
1883 * @gpu_addr: GPU address of the external resource.
1884 *
1885 * If meta is NULL then gpu_addr will be used to scan the metadata list and
1886 * find the matching metadata (if any), otherwise the provided meta will be
1887 * used and gpu_addr will be ignored.
1888 *
1889 * Return: True if the release found the metadata and the resource was
1890 * released.
1891 */
1892 bool kbase_sticky_resource_release_force(struct kbase_context *kctx,
1893 struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr);
1894
1895 /**
1896 * kbase_sticky_resource_term - Terminate sticky resource management.
1897 * @kctx: kbase context
1898 */
1899 void kbase_sticky_resource_term(struct kbase_context *kctx);
1900
1901 /**
1902 * kbase_mem_pool_lock - Lock a memory pool
1903 * @pool: Memory pool to lock
1904 */
kbase_mem_pool_lock(struct kbase_mem_pool * pool)1905 static inline void kbase_mem_pool_lock(struct kbase_mem_pool *pool)
1906 {
1907 spin_lock(&pool->pool_lock);
1908 }
1909
1910 /**
1911 * kbase_mem_pool_lock - Release a memory pool
1912 * @pool: Memory pool to lock
1913 */
kbase_mem_pool_unlock(struct kbase_mem_pool * pool)1914 static inline void kbase_mem_pool_unlock(struct kbase_mem_pool *pool)
1915 {
1916 spin_unlock(&pool->pool_lock);
1917 }
1918
1919 /**
1920 * kbase_mem_evictable_mark_reclaim - Mark the pages as reclaimable.
1921 * @alloc: The physical allocation
1922 */
1923 void kbase_mem_evictable_mark_reclaim(struct kbase_mem_phy_alloc *alloc);
1924
1925 #if MALI_USE_CSF
1926 /**
1927 * kbase_link_event_mem_page - Add the new event memory region to the per
1928 * context list of event pages.
1929 * @kctx: Pointer to kbase context
1930 * @reg: Pointer to the region allocated for event memory.
1931 *
1932 * The region being linked shouldn't have been marked as free and should
1933 * have KBASE_REG_CSF_EVENT flag set for it.
1934 */
kbase_link_event_mem_page(struct kbase_context * kctx,struct kbase_va_region * reg)1935 static inline void kbase_link_event_mem_page(struct kbase_context *kctx,
1936 struct kbase_va_region *reg)
1937 {
1938 lockdep_assert_held(&kctx->reg_lock);
1939
1940 WARN_ON(reg->flags & KBASE_REG_FREE);
1941 WARN_ON(!(reg->flags & KBASE_REG_CSF_EVENT));
1942
1943 list_add(®->link, &kctx->csf.event_pages_head);
1944 }
1945
1946 /**
1947 * kbase_unlink_event_mem_page - Remove the event memory region from the per
1948 * context list of event pages.
1949 * @kctx: Pointer to kbase context
1950 * @reg: Pointer to the region allocated for event memory.
1951 *
1952 * The region being un-linked shouldn't have been marked as free and should
1953 * have KBASE_REG_CSF_EVENT flag set for it.
1954 */
kbase_unlink_event_mem_page(struct kbase_context * kctx,struct kbase_va_region * reg)1955 static inline void kbase_unlink_event_mem_page(struct kbase_context *kctx,
1956 struct kbase_va_region *reg)
1957 {
1958 lockdep_assert_held(&kctx->reg_lock);
1959
1960 WARN_ON(reg->flags & KBASE_REG_FREE);
1961 WARN_ON(!(reg->flags & KBASE_REG_CSF_EVENT));
1962
1963 list_del(®->link);
1964 }
1965
1966 /**
1967 * kbase_mcu_shared_interface_region_tracker_init - Initialize the rb tree to
1968 * manage the shared interface segment of MCU firmware address space.
1969 * @kbdev: Pointer to the kbase device
1970 *
1971 * Returns zero on success or negative error number on failure.
1972 */
1973 int kbase_mcu_shared_interface_region_tracker_init(struct kbase_device *kbdev);
1974
1975 /**
1976 * kbase_mcu_shared_interface_region_tracker_term - Teardown the rb tree
1977 * managing the shared interface segment of MCU firmware address space.
1978 * @kbdev: Pointer to the kbase device
1979 */
1980 void kbase_mcu_shared_interface_region_tracker_term(struct kbase_device *kbdev);
1981 #endif
1982
1983 /**
1984 * kbase_mem_umm_map - Map dma-buf
1985 * @kctx: Pointer to the kbase context
1986 * @reg: Pointer to the region of the imported dma-buf to map
1987 *
1988 * Map a dma-buf on the GPU. The mappings are reference counted.
1989 *
1990 * Returns 0 on success, or a negative error code.
1991 */
1992 int kbase_mem_umm_map(struct kbase_context *kctx,
1993 struct kbase_va_region *reg);
1994
1995 /**
1996 * kbase_mem_umm_unmap - Unmap dma-buf
1997 * @kctx: Pointer to the kbase context
1998 * @reg: Pointer to the region of the imported dma-buf to unmap
1999 * @alloc: Pointer to the alloc to release
2000 *
2001 * Unmap a dma-buf from the GPU. The mappings are reference counted.
2002 *
2003 * @reg must be the original region with GPU mapping of @alloc; or NULL. If
2004 * @reg is NULL, or doesn't match @alloc, the GPU page table entries matching
2005 * @reg will not be updated.
2006 *
2007 * @alloc must be a valid physical allocation of type
2008 * KBASE_MEM_TYPE_IMPORTED_UMM that was previously mapped by
2009 * kbase_mem_umm_map(). The dma-buf attachment referenced by @alloc will
2010 * release it's mapping reference, and if the refcount reaches 0, also be be
2011 * unmapped, regardless of the value of @reg.
2012 */
2013 void kbase_mem_umm_unmap(struct kbase_context *kctx,
2014 struct kbase_va_region *reg, struct kbase_mem_phy_alloc *alloc);
2015
2016 /**
2017 * kbase_mem_do_sync_imported - Sync caches for imported memory
2018 * @kctx: Pointer to the kbase context
2019 * @reg: Pointer to the region with imported memory to sync
2020 * @sync_fn: The type of sync operation to perform
2021 *
2022 * Sync CPU caches for supported (currently only dma-buf (UMM)) memory.
2023 * Attempting to sync unsupported imported memory types will result in an error
2024 * code, -EINVAL.
2025 *
2026 * Return: 0 on success, or a negative error code.
2027 */
2028 int kbase_mem_do_sync_imported(struct kbase_context *kctx,
2029 struct kbase_va_region *reg, enum kbase_sync_type sync_fn);
2030
2031 /**
2032 * kbase_mem_copy_to_pinned_user_pages - Memcpy from source input page to
2033 * an unaligned address at a given offset from the start of a target page.
2034 *
2035 * @dest_pages: Pointer to the array of pages to which the content is
2036 * to be copied from the provided @src_page.
2037 * @src_page: Pointer to the page which correspond to the source page
2038 * from which the copying will take place.
2039 * @to_copy: Total number of bytes pending to be copied from
2040 * @src_page to @target_page_nr within @dest_pages.
2041 * This will get decremented by number of bytes we
2042 * managed to copy from source page to target pages.
2043 * @nr_pages: Total number of pages present in @dest_pages.
2044 * @target_page_nr: Target page number to which @src_page needs to be
2045 * copied. This will get incremented by one if
2046 * we are successful in copying from source page.
2047 * @offset: Offset in bytes into the target pages from which the
2048 * copying is to be performed.
2049 *
2050 * Return: 0 on success, or a negative error code.
2051 */
2052 int kbase_mem_copy_to_pinned_user_pages(struct page **dest_pages,
2053 void *src_page, size_t *to_copy, unsigned int nr_pages,
2054 unsigned int *target_page_nr, size_t offset);
2055
2056 /**
2057 * kbase_reg_zone_end_pfn - return the end Page Frame Number of @zone
2058 * @zone: zone to query
2059 *
2060 * Return: The end of the zone corresponding to @zone
2061 */
kbase_reg_zone_end_pfn(struct kbase_reg_zone * zone)2062 static inline u64 kbase_reg_zone_end_pfn(struct kbase_reg_zone *zone)
2063 {
2064 return zone->base_pfn + zone->va_size_pages;
2065 }
2066
2067 /**
2068 * kbase_ctx_reg_zone_init - initialize a zone in @kctx
2069 * @kctx: Pointer to kbase context
2070 * @zone_bits: A KBASE_REG_ZONE_<...> to initialize
2071 * @base_pfn: Page Frame Number in GPU virtual address space for the start of
2072 * the Zone
2073 * @va_size_pages: Size of the Zone in pages
2074 */
kbase_ctx_reg_zone_init(struct kbase_context * kctx,unsigned long zone_bits,u64 base_pfn,u64 va_size_pages)2075 static inline void kbase_ctx_reg_zone_init(struct kbase_context *kctx,
2076 unsigned long zone_bits,
2077 u64 base_pfn, u64 va_size_pages)
2078 {
2079 struct kbase_reg_zone *zone;
2080
2081 lockdep_assert_held(&kctx->reg_lock);
2082 WARN_ON(!kbase_is_ctx_reg_zone(zone_bits));
2083
2084 zone = &kctx->reg_zone[KBASE_REG_ZONE_IDX(zone_bits)];
2085 *zone = (struct kbase_reg_zone){
2086 .base_pfn = base_pfn, .va_size_pages = va_size_pages,
2087 };
2088 }
2089
2090 /**
2091 * kbase_ctx_reg_zone_get_nolock - get a zone from @kctx where the caller does
2092 * not have @kctx 's region lock
2093 * @kctx: Pointer to kbase context
2094 * @zone_bits: A KBASE_REG_ZONE_<...> to retrieve
2095 *
2096 * This should only be used in performance-critical paths where the code is
2097 * resilient to a race with the zone changing.
2098 *
2099 * Return: The zone corresponding to @zone_bits
2100 */
2101 static inline struct kbase_reg_zone *
kbase_ctx_reg_zone_get_nolock(struct kbase_context * kctx,unsigned long zone_bits)2102 kbase_ctx_reg_zone_get_nolock(struct kbase_context *kctx,
2103 unsigned long zone_bits)
2104 {
2105 WARN_ON(!kbase_is_ctx_reg_zone(zone_bits));
2106
2107 return &kctx->reg_zone[KBASE_REG_ZONE_IDX(zone_bits)];
2108 }
2109
2110 /**
2111 * kbase_ctx_reg_zone_get - get a zone from @kctx
2112 * @kctx: Pointer to kbase context
2113 * @zone_bits: A KBASE_REG_ZONE_<...> to retrieve
2114 *
2115 * The get is not refcounted - there is no corresponding 'put' operation
2116 *
2117 * Return: The zone corresponding to @zone_bits
2118 */
2119 static inline struct kbase_reg_zone *
kbase_ctx_reg_zone_get(struct kbase_context * kctx,unsigned long zone_bits)2120 kbase_ctx_reg_zone_get(struct kbase_context *kctx, unsigned long zone_bits)
2121 {
2122 lockdep_assert_held(&kctx->reg_lock);
2123 WARN_ON(!kbase_is_ctx_reg_zone(zone_bits));
2124
2125 return &kctx->reg_zone[KBASE_REG_ZONE_IDX(zone_bits)];
2126 }
2127
2128 /**
2129 * kbase_mem_allow_alloc - Check if allocation of GPU memory is allowed
2130 * @kctx: Pointer to kbase context
2131 *
2132 * Don't allow the allocation of GPU memory until user space has set up the
2133 * tracking page (which sets kctx->process_mm) or if the ioctl has been issued
2134 * from the forked child process using the mali device file fd inherited from
2135 * the parent process.
2136 */
kbase_mem_allow_alloc(struct kbase_context * kctx)2137 static inline bool kbase_mem_allow_alloc(struct kbase_context *kctx)
2138 {
2139 bool allow_alloc = true;
2140
2141 rcu_read_lock();
2142 allow_alloc = (rcu_dereference(kctx->process_mm) == current->mm);
2143 rcu_read_unlock();
2144
2145 return allow_alloc;
2146 }
2147
2148 /**
2149 * kbase_mem_group_id_get - Get group ID from flags
2150 * @flags: Flags to pass to base_mem_alloc
2151 *
2152 * This inline function extracts the encoded group ID from flags
2153 * and converts it into numeric value (0~15).
2154 *
2155 * Return: group ID(0~15) extracted from the parameter
2156 */
kbase_mem_group_id_get(base_mem_alloc_flags flags)2157 static inline int kbase_mem_group_id_get(base_mem_alloc_flags flags)
2158 {
2159 KBASE_DEBUG_ASSERT((flags & ~BASE_MEM_FLAGS_INPUT_MASK) == 0);
2160 return (int)BASE_MEM_GROUP_ID_GET(flags);
2161 }
2162
2163 /**
2164 * kbase_mem_group_id_set - Set group ID into base_mem_alloc_flags
2165 * @id: group ID(0~15) you want to encode
2166 *
2167 * This inline function encodes specific group ID into base_mem_alloc_flags.
2168 * Parameter 'id' should lie in-between 0 to 15.
2169 *
2170 * Return: base_mem_alloc_flags with the group ID (id) encoded
2171 *
2172 * The return value can be combined with other flags against base_mem_alloc
2173 * to identify a specific memory group.
2174 */
kbase_mem_group_id_set(int id)2175 static inline base_mem_alloc_flags kbase_mem_group_id_set(int id)
2176 {
2177 return BASE_MEM_GROUP_ID_SET(id);
2178 }
2179 #endif /* _KBASE_MEM_H_ */
2180