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