xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/mali_kbase_mem.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #include <linux/dma-buf.h>
26 #include <linux/kernel.h>
27 #include <linux/bug.h>
28 #include <linux/compat.h>
29 #include <linux/version.h>
30 #include <linux/log2.h>
31 #if IS_ENABLED(CONFIG_OF)
32 #include <linux/of_platform.h>
33 #endif
34 
35 #include <mali_kbase_config.h>
36 #include <mali_kbase.h>
37 #include <gpu/mali_kbase_gpu_regmap.h>
38 #include <mali_kbase_cache_policy.h>
39 #include <mali_kbase_hw.h>
40 #include <tl/mali_kbase_tracepoints.h>
41 #include <mali_kbase_native_mgm.h>
42 #include <mali_kbase_mem_pool_group.h>
43 #include <mmu/mali_kbase_mmu.h>
44 #include <mali_kbase_config_defaults.h>
45 #include <mali_kbase_trace_gpu_mem.h>
46 
47 #define VA_REGION_SLAB_NAME_PREFIX "va-region-slab-"
48 #define VA_REGION_SLAB_NAME_SIZE (DEVNAME_SIZE + sizeof(VA_REGION_SLAB_NAME_PREFIX) + 1)
49 
50 #if MALI_JIT_PRESSURE_LIMIT_BASE
51 
52 /*
53  * Alignment of objects allocated by the GPU inside a just-in-time memory
54  * region whose size is given by an end address
55  *
56  * This is the alignment of objects allocated by the GPU, but possibly not
57  * fully written to. When taken into account with
58  * KBASE_GPU_ALLOCATED_OBJECT_MAX_BYTES it gives the maximum number of bytes
59  * that the JIT memory report size can exceed the actual backed memory size.
60  */
61 #define KBASE_GPU_ALLOCATED_OBJECT_ALIGN_BYTES (128u)
62 
63 /*
64  * Maximum size of objects allocated by the GPU inside a just-in-time memory
65  * region whose size is given by an end address
66  *
67  * This is the maximum size of objects allocated by the GPU, but possibly not
68  * fully written to. When taken into account with
69  * KBASE_GPU_ALLOCATED_OBJECT_ALIGN_BYTES it gives the maximum number of bytes
70  * that the JIT memory report size can exceed the actual backed memory size.
71  */
72 #define KBASE_GPU_ALLOCATED_OBJECT_MAX_BYTES (512u)
73 
74 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
75 
76 /* Forward declarations */
77 static void free_partial_locked(struct kbase_context *kctx,
78 		struct kbase_mem_pool *pool, struct tagged_addr tp);
79 
kbase_get_num_cpu_va_bits(struct kbase_context * kctx)80 static size_t kbase_get_num_cpu_va_bits(struct kbase_context *kctx)
81 {
82 #if defined(CONFIG_ARM64)
83 	/* VA_BITS can be as high as 48 bits, but all bits are available for
84 	 * both user and kernel.
85 	 */
86 	size_t cpu_va_bits = VA_BITS;
87 #elif defined(CONFIG_X86_64)
88 	/* x86_64 can access 48 bits of VA, but the 48th is used to denote
89 	 * kernel (1) vs userspace (0), so the max here is 47.
90 	 */
91 	size_t cpu_va_bits = 47;
92 #elif defined(CONFIG_ARM) || defined(CONFIG_X86_32)
93 	size_t cpu_va_bits = sizeof(void *) * BITS_PER_BYTE;
94 #else
95 #error "Unknown CPU VA width for this architecture"
96 #endif
97 
98 	if (kbase_ctx_compat_mode(kctx))
99 		cpu_va_bits = 32;
100 
101 	return cpu_va_bits;
102 }
103 
104 /* This function finds out which RB tree the given pfn from the GPU VA belongs
105  * to based on the memory zone the pfn refers to
106  */
kbase_gpu_va_to_rbtree(struct kbase_context * kctx,u64 gpu_pfn)107 static struct rb_root *kbase_gpu_va_to_rbtree(struct kbase_context *kctx,
108 								    u64 gpu_pfn)
109 {
110 	struct rb_root *rbtree = NULL;
111 
112 	struct kbase_reg_zone *exec_va_zone = kbase_ctx_reg_zone_get(kctx, KBASE_REG_ZONE_EXEC_VA);
113 
114 #if MALI_USE_CSF
115 	struct kbase_reg_zone *fixed_va_zone =
116 		kbase_ctx_reg_zone_get(kctx, KBASE_REG_ZONE_FIXED_VA);
117 
118 	struct kbase_reg_zone *exec_fixed_va_zone =
119 		kbase_ctx_reg_zone_get(kctx, KBASE_REG_ZONE_EXEC_FIXED_VA);
120 
121 	if (gpu_pfn >= fixed_va_zone->base_pfn) {
122 		rbtree = &kctx->reg_rbtree_fixed;
123 		return rbtree;
124 	} else if (gpu_pfn >= exec_fixed_va_zone->base_pfn) {
125 		rbtree = &kctx->reg_rbtree_exec_fixed;
126 		return rbtree;
127 	}
128 #endif
129 	if (gpu_pfn >= exec_va_zone->base_pfn)
130 		rbtree = &kctx->reg_rbtree_exec;
131 	else {
132 		u64 same_va_end;
133 
134 		if (kbase_ctx_compat_mode(kctx)) {
135 			same_va_end = KBASE_REG_ZONE_CUSTOM_VA_BASE;
136 		} else {
137 			struct kbase_reg_zone *same_va_zone =
138 				kbase_ctx_reg_zone_get(kctx,
139 						       KBASE_REG_ZONE_SAME_VA);
140 			same_va_end = kbase_reg_zone_end_pfn(same_va_zone);
141 		}
142 
143 		if (gpu_pfn >= same_va_end)
144 			rbtree = &kctx->reg_rbtree_custom;
145 		else
146 			rbtree = &kctx->reg_rbtree_same;
147 	}
148 
149 	return rbtree;
150 }
151 
152 /* This function inserts a region into the tree. */
kbase_region_tracker_insert(struct kbase_va_region * new_reg)153 static void kbase_region_tracker_insert(struct kbase_va_region *new_reg)
154 {
155 	u64 start_pfn = new_reg->start_pfn;
156 	struct rb_node **link = NULL;
157 	struct rb_node *parent = NULL;
158 	struct rb_root *rbtree = NULL;
159 
160 	rbtree = new_reg->rbtree;
161 
162 	link = &(rbtree->rb_node);
163 	/* Find the right place in the tree using tree search */
164 	while (*link) {
165 		struct kbase_va_region *old_reg;
166 
167 		parent = *link;
168 		old_reg = rb_entry(parent, struct kbase_va_region, rblink);
169 
170 		/* RBTree requires no duplicate entries. */
171 		KBASE_DEBUG_ASSERT(old_reg->start_pfn != start_pfn);
172 
173 		if (old_reg->start_pfn > start_pfn)
174 			link = &(*link)->rb_left;
175 		else
176 			link = &(*link)->rb_right;
177 	}
178 
179 	/* Put the new node there, and rebalance tree */
180 	rb_link_node(&(new_reg->rblink), parent, link);
181 
182 	rb_insert_color(&(new_reg->rblink), rbtree);
183 }
184 
find_region_enclosing_range_rbtree(struct rb_root * rbtree,u64 start_pfn,size_t nr_pages)185 static struct kbase_va_region *find_region_enclosing_range_rbtree(
186 		struct rb_root *rbtree, u64 start_pfn, size_t nr_pages)
187 {
188 	struct rb_node *rbnode;
189 	struct kbase_va_region *reg;
190 	u64 end_pfn = start_pfn + nr_pages;
191 
192 	rbnode = rbtree->rb_node;
193 
194 	while (rbnode) {
195 		u64 tmp_start_pfn, tmp_end_pfn;
196 
197 		reg = rb_entry(rbnode, struct kbase_va_region, rblink);
198 		tmp_start_pfn = reg->start_pfn;
199 		tmp_end_pfn = reg->start_pfn + reg->nr_pages;
200 
201 		/* If start is lower than this, go left. */
202 		if (start_pfn < tmp_start_pfn)
203 			rbnode = rbnode->rb_left;
204 		/* If end is higher than this, then go right. */
205 		else if (end_pfn > tmp_end_pfn)
206 			rbnode = rbnode->rb_right;
207 		else	/* Enclosing */
208 			return reg;
209 	}
210 
211 	return NULL;
212 }
213 
kbase_find_region_enclosing_address(struct rb_root * rbtree,u64 gpu_addr)214 struct kbase_va_region *kbase_find_region_enclosing_address(
215 		struct rb_root *rbtree, u64 gpu_addr)
216 {
217 	u64 gpu_pfn = gpu_addr >> PAGE_SHIFT;
218 	struct rb_node *rbnode;
219 	struct kbase_va_region *reg;
220 
221 	rbnode = rbtree->rb_node;
222 
223 	while (rbnode) {
224 		u64 tmp_start_pfn, tmp_end_pfn;
225 
226 		reg = rb_entry(rbnode, struct kbase_va_region, rblink);
227 		tmp_start_pfn = reg->start_pfn;
228 		tmp_end_pfn = reg->start_pfn + reg->nr_pages;
229 
230 		/* If start is lower than this, go left. */
231 		if (gpu_pfn < tmp_start_pfn)
232 			rbnode = rbnode->rb_left;
233 		/* If end is higher than this, then go right. */
234 		else if (gpu_pfn >= tmp_end_pfn)
235 			rbnode = rbnode->rb_right;
236 		else	/* Enclosing */
237 			return reg;
238 	}
239 
240 	return NULL;
241 }
242 
243 /* Find region enclosing given address. */
kbase_region_tracker_find_region_enclosing_address(struct kbase_context * kctx,u64 gpu_addr)244 struct kbase_va_region *kbase_region_tracker_find_region_enclosing_address(
245 		struct kbase_context *kctx, u64 gpu_addr)
246 {
247 	u64 gpu_pfn = gpu_addr >> PAGE_SHIFT;
248 	struct rb_root *rbtree = NULL;
249 
250 	KBASE_DEBUG_ASSERT(kctx != NULL);
251 
252 	lockdep_assert_held(&kctx->reg_lock);
253 
254 	rbtree = kbase_gpu_va_to_rbtree(kctx, gpu_pfn);
255 
256 	return kbase_find_region_enclosing_address(rbtree, gpu_addr);
257 }
258 
259 KBASE_EXPORT_TEST_API(kbase_region_tracker_find_region_enclosing_address);
260 
kbase_find_region_base_address(struct rb_root * rbtree,u64 gpu_addr)261 struct kbase_va_region *kbase_find_region_base_address(
262 		struct rb_root *rbtree, u64 gpu_addr)
263 {
264 	u64 gpu_pfn = gpu_addr >> PAGE_SHIFT;
265 	struct rb_node *rbnode = NULL;
266 	struct kbase_va_region *reg = NULL;
267 
268 	rbnode = rbtree->rb_node;
269 
270 	while (rbnode) {
271 		reg = rb_entry(rbnode, struct kbase_va_region, rblink);
272 		if (reg->start_pfn > gpu_pfn)
273 			rbnode = rbnode->rb_left;
274 		else if (reg->start_pfn < gpu_pfn)
275 			rbnode = rbnode->rb_right;
276 		else
277 			return reg;
278 	}
279 
280 	return NULL;
281 }
282 
283 /* Find region with given base address */
kbase_region_tracker_find_region_base_address(struct kbase_context * kctx,u64 gpu_addr)284 struct kbase_va_region *kbase_region_tracker_find_region_base_address(
285 		struct kbase_context *kctx, u64 gpu_addr)
286 {
287 	u64 gpu_pfn = gpu_addr >> PAGE_SHIFT;
288 	struct rb_root *rbtree = NULL;
289 
290 	lockdep_assert_held(&kctx->reg_lock);
291 
292 	rbtree = kbase_gpu_va_to_rbtree(kctx, gpu_pfn);
293 
294 	return kbase_find_region_base_address(rbtree, gpu_addr);
295 }
296 
297 KBASE_EXPORT_TEST_API(kbase_region_tracker_find_region_base_address);
298 
299 /* Find region meeting given requirements */
kbase_region_tracker_find_region_meeting_reqs(struct kbase_va_region * reg_reqs,size_t nr_pages,size_t align_offset,size_t align_mask,u64 * out_start_pfn)300 static struct kbase_va_region *kbase_region_tracker_find_region_meeting_reqs(
301 		struct kbase_va_region *reg_reqs,
302 		size_t nr_pages, size_t align_offset, size_t align_mask,
303 		u64 *out_start_pfn)
304 {
305 	struct rb_node *rbnode = NULL;
306 	struct kbase_va_region *reg = NULL;
307 	struct rb_root *rbtree = NULL;
308 
309 	/* Note that this search is a linear search, as we do not have a target
310 	 * address in mind, so does not benefit from the rbtree search
311 	 */
312 	rbtree = reg_reqs->rbtree;
313 
314 	for (rbnode = rb_first(rbtree); rbnode; rbnode = rb_next(rbnode)) {
315 		reg = rb_entry(rbnode, struct kbase_va_region, rblink);
316 		if ((reg->nr_pages >= nr_pages) &&
317 				(reg->flags & KBASE_REG_FREE)) {
318 			/* Check alignment */
319 			u64 start_pfn = reg->start_pfn;
320 
321 			/* When align_offset == align, this sequence is
322 			 * equivalent to:
323 			 *   (start_pfn + align_mask) & ~(align_mask)
324 			 *
325 			 * Otherwise, it aligns to n*align + offset, for the
326 			 * lowest value n that makes this still >start_pfn
327 			 */
328 			start_pfn += align_mask;
329 			start_pfn -= (start_pfn - align_offset) & (align_mask);
330 
331 			if (!(reg_reqs->flags & KBASE_REG_GPU_NX)) {
332 				/* Can't end at 4GB boundary */
333 				if (0 == ((start_pfn + nr_pages) & BASE_MEM_PFN_MASK_4GB))
334 					start_pfn += align_offset;
335 
336 				/* Can't start at 4GB boundary */
337 				if (0 == (start_pfn & BASE_MEM_PFN_MASK_4GB))
338 					start_pfn += align_offset;
339 
340 				if (!((start_pfn + nr_pages) & BASE_MEM_PFN_MASK_4GB) ||
341 				    !(start_pfn & BASE_MEM_PFN_MASK_4GB))
342 					continue;
343 			} else if (reg_reqs->flags &
344 					KBASE_REG_GPU_VA_SAME_4GB_PAGE) {
345 				u64 end_pfn = start_pfn + nr_pages - 1;
346 
347 				if ((start_pfn & ~BASE_MEM_PFN_MASK_4GB) !=
348 				    (end_pfn & ~BASE_MEM_PFN_MASK_4GB))
349 					start_pfn = end_pfn & ~BASE_MEM_PFN_MASK_4GB;
350 			}
351 
352 			if ((start_pfn >= reg->start_pfn) &&
353 					(start_pfn <= (reg->start_pfn + reg->nr_pages - 1)) &&
354 					((start_pfn + nr_pages - 1) <= (reg->start_pfn + reg->nr_pages - 1))) {
355 				*out_start_pfn = start_pfn;
356 				return reg;
357 			}
358 		}
359 	}
360 
361 	return NULL;
362 }
363 
364 /**
365  * kbase_remove_va_region - Remove a region object from the global list.
366  *
367  * @kbdev: The kbase device
368  * @reg: Region object to remove
369  *
370  * The region reg is removed, possibly by merging with other free and
371  * compatible adjacent regions.  It must be called with the context
372  * region lock held. The associated memory is not released (see
373  * kbase_free_alloced_region). Internal use only.
374  */
kbase_remove_va_region(struct kbase_device * kbdev,struct kbase_va_region * reg)375 void kbase_remove_va_region(struct kbase_device *kbdev,
376 			    struct kbase_va_region *reg)
377 {
378 	struct rb_node *rbprev;
379 	struct kbase_va_region *prev = NULL;
380 	struct rb_node *rbnext;
381 	struct kbase_va_region *next = NULL;
382 	struct rb_root *reg_rbtree = NULL;
383 	struct kbase_va_region *orig_reg = reg;
384 
385 	int merged_front = 0;
386 	int merged_back = 0;
387 
388 	reg_rbtree = reg->rbtree;
389 
390 	if (WARN_ON(RB_EMPTY_ROOT(reg_rbtree)))
391 		return;
392 
393 	/* Try to merge with the previous block first */
394 	rbprev = rb_prev(&(reg->rblink));
395 	if (rbprev) {
396 		prev = rb_entry(rbprev, struct kbase_va_region, rblink);
397 		if (prev->flags & KBASE_REG_FREE) {
398 			/* We're compatible with the previous VMA, merge with
399 			 * it, handling any gaps for robustness.
400 			 */
401 			u64 prev_end_pfn = prev->start_pfn + prev->nr_pages;
402 
403 			WARN_ON((prev->flags & KBASE_REG_ZONE_MASK) !=
404 					    (reg->flags & KBASE_REG_ZONE_MASK));
405 			if (!WARN_ON(reg->start_pfn < prev_end_pfn))
406 				prev->nr_pages += reg->start_pfn - prev_end_pfn;
407 			prev->nr_pages += reg->nr_pages;
408 			rb_erase(&(reg->rblink), reg_rbtree);
409 			reg = prev;
410 			merged_front = 1;
411 		}
412 	}
413 
414 	/* Try to merge with the next block second */
415 	/* Note we do the lookup here as the tree may have been rebalanced. */
416 	rbnext = rb_next(&(reg->rblink));
417 	if (rbnext) {
418 		next = rb_entry(rbnext, struct kbase_va_region, rblink);
419 		if (next->flags & KBASE_REG_FREE) {
420 			/* We're compatible with the next VMA, merge with it,
421 			 * handling any gaps for robustness.
422 			 */
423 			u64 reg_end_pfn = reg->start_pfn + reg->nr_pages;
424 
425 			WARN_ON((next->flags & KBASE_REG_ZONE_MASK) !=
426 					    (reg->flags & KBASE_REG_ZONE_MASK));
427 			if (!WARN_ON(next->start_pfn < reg_end_pfn))
428 				next->nr_pages += next->start_pfn - reg_end_pfn;
429 			next->start_pfn = reg->start_pfn;
430 			next->nr_pages += reg->nr_pages;
431 			rb_erase(&(reg->rblink), reg_rbtree);
432 			merged_back = 1;
433 		}
434 	}
435 
436 	if (merged_front && merged_back) {
437 		/* We already merged with prev, free it */
438 		kfree(reg);
439 	} else if (!(merged_front || merged_back)) {
440 		/* If we failed to merge then we need to add a new block */
441 
442 		/*
443 		 * We didn't merge anything. Try to add a new free
444 		 * placeholder, and in any case, remove the original one.
445 		 */
446 		struct kbase_va_region *free_reg;
447 
448 		free_reg = kbase_alloc_free_region(kbdev, reg_rbtree, reg->start_pfn, reg->nr_pages,
449 						   reg->flags & KBASE_REG_ZONE_MASK);
450 		if (!free_reg) {
451 			/* In case of failure, we cannot allocate a replacement
452 			 * free region, so we will be left with a 'gap' in the
453 			 * region tracker's address range (though, the rbtree
454 			 * will itself still be correct after erasing
455 			 * 'reg').
456 			 *
457 			 * The gap will be rectified when an adjacent region is
458 			 * removed by one of the above merging paths. Other
459 			 * paths will gracefully fail to allocate if they try
460 			 * to allocate in the gap.
461 			 *
462 			 * There is nothing that the caller can do, since free
463 			 * paths must not fail. The existing 'reg' cannot be
464 			 * repurposed as the free region as callers must have
465 			 * freedom of use with it by virtue of it being owned
466 			 * by them, not the region tracker insert/remove code.
467 			 */
468 			dev_warn(
469 				kbdev->dev,
470 				"Could not alloc a replacement free region for 0x%.16llx..0x%.16llx",
471 				(unsigned long long)reg->start_pfn << PAGE_SHIFT,
472 				(unsigned long long)(reg->start_pfn + reg->nr_pages) << PAGE_SHIFT);
473 			rb_erase(&(reg->rblink), reg_rbtree);
474 
475 			goto out;
476 		}
477 		rb_replace_node(&(reg->rblink), &(free_reg->rblink), reg_rbtree);
478 	}
479 
480 	/* This operation is always safe because the function never frees
481 	 * the region. If the region has been merged to both front and back,
482 	 * then it's the previous region that is supposed to be freed.
483 	 */
484 	orig_reg->start_pfn = 0;
485 
486 out:
487 	return;
488 }
489 
490 KBASE_EXPORT_TEST_API(kbase_remove_va_region);
491 
492 /**
493  * kbase_insert_va_region_nolock - Insert a VA region to the list,
494  * replacing the existing one.
495  *
496  * @kbdev: The kbase device
497  * @new_reg: The new region to insert
498  * @at_reg: The region to replace
499  * @start_pfn: The Page Frame Number to insert at
500  * @nr_pages: The number of pages of the region
501  *
502  * Return: 0 on success, error code otherwise.
503  */
kbase_insert_va_region_nolock(struct kbase_device * kbdev,struct kbase_va_region * new_reg,struct kbase_va_region * at_reg,u64 start_pfn,size_t nr_pages)504 static int kbase_insert_va_region_nolock(struct kbase_device *kbdev,
505 					 struct kbase_va_region *new_reg,
506 					 struct kbase_va_region *at_reg, u64 start_pfn,
507 					 size_t nr_pages)
508 {
509 	struct rb_root *reg_rbtree = NULL;
510 	int err = 0;
511 
512 	reg_rbtree = at_reg->rbtree;
513 
514 	/* Must be a free region */
515 	KBASE_DEBUG_ASSERT((at_reg->flags & KBASE_REG_FREE) != 0);
516 	/* start_pfn should be contained within at_reg */
517 	KBASE_DEBUG_ASSERT((start_pfn >= at_reg->start_pfn) && (start_pfn < at_reg->start_pfn + at_reg->nr_pages));
518 	/* at least nr_pages from start_pfn should be contained within at_reg */
519 	KBASE_DEBUG_ASSERT(start_pfn + nr_pages <= at_reg->start_pfn + at_reg->nr_pages);
520 	/* having at_reg means the rb_tree should not be empty */
521 	if (WARN_ON(RB_EMPTY_ROOT(reg_rbtree)))
522 		return -ENOMEM;
523 
524 	new_reg->start_pfn = start_pfn;
525 	new_reg->nr_pages = nr_pages;
526 
527 	/* Regions are a whole use, so swap and delete old one. */
528 	if (at_reg->start_pfn == start_pfn && at_reg->nr_pages == nr_pages) {
529 		rb_replace_node(&(at_reg->rblink), &(new_reg->rblink),
530 								reg_rbtree);
531 		kfree(at_reg);
532 	}
533 	/* New region replaces the start of the old one, so insert before. */
534 	else if (at_reg->start_pfn == start_pfn) {
535 		at_reg->start_pfn += nr_pages;
536 		KBASE_DEBUG_ASSERT(at_reg->nr_pages >= nr_pages);
537 		at_reg->nr_pages -= nr_pages;
538 
539 		kbase_region_tracker_insert(new_reg);
540 	}
541 	/* New region replaces the end of the old one, so insert after. */
542 	else if ((at_reg->start_pfn + at_reg->nr_pages) == (start_pfn + nr_pages)) {
543 		at_reg->nr_pages -= nr_pages;
544 
545 		kbase_region_tracker_insert(new_reg);
546 	}
547 	/* New region splits the old one, so insert and create new */
548 	else {
549 		struct kbase_va_region *new_front_reg;
550 
551 		new_front_reg = kbase_alloc_free_region(kbdev, reg_rbtree, at_reg->start_pfn,
552 							start_pfn - at_reg->start_pfn,
553 							at_reg->flags & KBASE_REG_ZONE_MASK);
554 
555 		if (new_front_reg) {
556 			at_reg->nr_pages -= nr_pages + new_front_reg->nr_pages;
557 			at_reg->start_pfn = start_pfn + nr_pages;
558 
559 			kbase_region_tracker_insert(new_front_reg);
560 			kbase_region_tracker_insert(new_reg);
561 		} else {
562 			err = -ENOMEM;
563 		}
564 	}
565 
566 	return err;
567 }
568 
569 /**
570  * kbase_add_va_region - Add a VA region to the region list for a context.
571  *
572  * @kctx: kbase context containing the region
573  * @reg: the region to add
574  * @addr: the address to insert the region at
575  * @nr_pages: the number of pages in the region
576  * @align: the minimum alignment in pages
577  *
578  * Return: 0 on success, error code otherwise.
579  */
kbase_add_va_region(struct kbase_context * kctx,struct kbase_va_region * reg,u64 addr,size_t nr_pages,size_t align)580 int kbase_add_va_region(struct kbase_context *kctx,
581 		struct kbase_va_region *reg, u64 addr,
582 		size_t nr_pages, size_t align)
583 {
584 	int err = 0;
585 	struct kbase_device *kbdev = kctx->kbdev;
586 	int cpu_va_bits = kbase_get_num_cpu_va_bits(kctx);
587 	int gpu_pc_bits =
588 		kbdev->gpu_props.props.core_props.log2_program_counter_size;
589 
590 	KBASE_DEBUG_ASSERT(kctx != NULL);
591 	KBASE_DEBUG_ASSERT(reg != NULL);
592 
593 	lockdep_assert_held(&kctx->reg_lock);
594 
595 	/* The executable allocation from the SAME_VA zone should already have an
596 	 * appropriately aligned GPU VA chosen for it.
597 	 * Also, executable allocations from EXEC_VA don't need the special
598 	 * alignment.
599 	 */
600 #if MALI_USE_CSF
601 	/* The same is also true for the EXEC_FIXED_VA zone.
602 	 */
603 #endif
604 	if (!(reg->flags & KBASE_REG_GPU_NX) && !addr &&
605 #if MALI_USE_CSF
606 		((reg->flags & KBASE_REG_ZONE_MASK) != KBASE_REG_ZONE_EXEC_FIXED_VA) &&
607 #endif
608 	    ((reg->flags & KBASE_REG_ZONE_MASK) != KBASE_REG_ZONE_EXEC_VA)) {
609 		if (cpu_va_bits > gpu_pc_bits) {
610 			align = max(align, (size_t)((1ULL << gpu_pc_bits)
611 						>> PAGE_SHIFT));
612 		}
613 	}
614 
615 	do {
616 		err = kbase_add_va_region_rbtree(kbdev, reg, addr, nr_pages,
617 				align);
618 		if (err != -ENOMEM)
619 			break;
620 
621 		/*
622 		 * If the allocation is not from the same zone as JIT
623 		 * then don't retry, we're out of VA and there is
624 		 * nothing which can be done about it.
625 		 */
626 		if ((reg->flags & KBASE_REG_ZONE_MASK) !=
627 				KBASE_REG_ZONE_CUSTOM_VA)
628 			break;
629 	} while (kbase_jit_evict(kctx));
630 
631 	return err;
632 }
633 
634 KBASE_EXPORT_TEST_API(kbase_add_va_region);
635 
636 /**
637  * kbase_add_va_region_rbtree - Insert a region into its corresponding rbtree
638  *
639  * @kbdev: The kbase device
640  * @reg: The region to add
641  * @addr: The address to add the region at, or 0 to map at any available address
642  * @nr_pages: The size of the region in pages
643  * @align: The minimum alignment in pages
644  *
645  * Insert a region into the rbtree that was specified when the region was
646  * created. If addr is 0 a free area in the rbtree is used, otherwise the
647  * specified address is used.
648  *
649  * Return: 0 on success, error code otherwise.
650  */
kbase_add_va_region_rbtree(struct kbase_device * kbdev,struct kbase_va_region * reg,u64 addr,size_t nr_pages,size_t align)651 int kbase_add_va_region_rbtree(struct kbase_device *kbdev,
652 		struct kbase_va_region *reg,
653 		u64 addr, size_t nr_pages, size_t align)
654 {
655 	struct device *const dev = kbdev->dev;
656 	struct rb_root *rbtree = NULL;
657 	struct kbase_va_region *tmp;
658 	u64 gpu_pfn = addr >> PAGE_SHIFT;
659 	int err = 0;
660 
661 	rbtree = reg->rbtree;
662 
663 	if (!align)
664 		align = 1;
665 
666 	/* must be a power of 2 */
667 	KBASE_DEBUG_ASSERT(is_power_of_2(align));
668 	KBASE_DEBUG_ASSERT(nr_pages > 0);
669 
670 	/* Path 1: Map a specific address. Find the enclosing region,
671 	 * which *must* be free.
672 	 */
673 	if (gpu_pfn) {
674 		KBASE_DEBUG_ASSERT(!(gpu_pfn & (align - 1)));
675 
676 		tmp = find_region_enclosing_range_rbtree(rbtree, gpu_pfn,
677 				nr_pages);
678 		if (kbase_is_region_invalid(tmp)) {
679 			dev_warn(dev, "Enclosing region not found or invalid: 0x%08llx gpu_pfn, %zu nr_pages", gpu_pfn, nr_pages);
680 			err = -ENOMEM;
681 			goto exit;
682 		} else if (!kbase_is_region_free(tmp)) {
683 			dev_warn(dev, "!(tmp->flags & KBASE_REG_FREE): tmp->start_pfn=0x%llx tmp->flags=0x%lx tmp->nr_pages=0x%zx gpu_pfn=0x%llx nr_pages=0x%zx\n",
684 					tmp->start_pfn, tmp->flags,
685 					tmp->nr_pages, gpu_pfn, nr_pages);
686 			err = -ENOMEM;
687 			goto exit;
688 		}
689 
690 		err = kbase_insert_va_region_nolock(kbdev, reg, tmp, gpu_pfn, nr_pages);
691 		if (err) {
692 			dev_warn(dev, "Failed to insert va region");
693 			err = -ENOMEM;
694 		}
695 	} else {
696 		/* Path 2: Map any free address which meets the requirements. */
697 		u64 start_pfn;
698 		size_t align_offset = align;
699 		size_t align_mask = align - 1;
700 
701 #if !MALI_USE_CSF
702 		if ((reg->flags & KBASE_REG_TILER_ALIGN_TOP)) {
703 			WARN(align > 1, "%s with align %lx might not be honored for KBASE_REG_TILER_ALIGN_TOP memory",
704 					__func__,
705 					(unsigned long)align);
706 			align_mask = reg->extension - 1;
707 			align_offset = reg->extension - reg->initial_commit;
708 		}
709 #endif /* !MALI_USE_CSF */
710 
711 		tmp = kbase_region_tracker_find_region_meeting_reqs(reg,
712 				nr_pages, align_offset, align_mask,
713 				&start_pfn);
714 		if (tmp) {
715 			err = kbase_insert_va_region_nolock(kbdev, reg, tmp, start_pfn, nr_pages);
716 			if (unlikely(err)) {
717 				dev_warn(dev, "Failed to insert region: 0x%08llx start_pfn, %zu nr_pages",
718 					start_pfn, nr_pages);
719 			}
720 		} else {
721 			dev_dbg(dev, "Failed to find a suitable region: %zu nr_pages, %zu align_offset, %zu align_mask\n",
722 				nr_pages, align_offset, align_mask);
723 			err = -ENOMEM;
724 		}
725 	}
726 
727 exit:
728 	return err;
729 }
730 
731 /*
732  * @brief Initialize the internal region tracker data structure.
733  */
734 #if MALI_USE_CSF
kbase_region_tracker_ds_init(struct kbase_context * kctx,struct kbase_va_region * same_va_reg,struct kbase_va_region * custom_va_reg,struct kbase_va_region * exec_va_reg,struct kbase_va_region * exec_fixed_va_reg,struct kbase_va_region * fixed_va_reg)735 static void kbase_region_tracker_ds_init(struct kbase_context *kctx,
736 					 struct kbase_va_region *same_va_reg,
737 					 struct kbase_va_region *custom_va_reg,
738 					 struct kbase_va_region *exec_va_reg,
739 					 struct kbase_va_region *exec_fixed_va_reg,
740 					 struct kbase_va_region *fixed_va_reg)
741 {
742 	u64 last_zone_end_pfn;
743 
744 	kctx->reg_rbtree_same = RB_ROOT;
745 	kbase_region_tracker_insert(same_va_reg);
746 
747 	last_zone_end_pfn = same_va_reg->start_pfn + same_va_reg->nr_pages;
748 
749 	/* Although custom_va_reg doesn't always exist, initialize
750 	 * unconditionally because of the mem_view debugfs
751 	 * implementation which relies on it being empty.
752 	 */
753 	kctx->reg_rbtree_custom = RB_ROOT;
754 	kctx->reg_rbtree_exec = RB_ROOT;
755 
756 	if (custom_va_reg) {
757 		WARN_ON(custom_va_reg->start_pfn < last_zone_end_pfn);
758 		kbase_region_tracker_insert(custom_va_reg);
759 		last_zone_end_pfn = custom_va_reg->start_pfn + custom_va_reg->nr_pages;
760 	}
761 
762 	/* Initialize exec, fixed and exec_fixed. These are always
763 	 * initialized at this stage, if they will exist at all.
764 	 */
765 	kctx->reg_rbtree_fixed = RB_ROOT;
766 	kctx->reg_rbtree_exec_fixed = RB_ROOT;
767 
768 	if (exec_va_reg) {
769 		WARN_ON(exec_va_reg->start_pfn < last_zone_end_pfn);
770 		kbase_region_tracker_insert(exec_va_reg);
771 		last_zone_end_pfn = exec_va_reg->start_pfn + exec_va_reg->nr_pages;
772 	}
773 
774 	if (exec_fixed_va_reg) {
775 		WARN_ON(exec_fixed_va_reg->start_pfn < last_zone_end_pfn);
776 		kbase_region_tracker_insert(exec_fixed_va_reg);
777 		last_zone_end_pfn = exec_fixed_va_reg->start_pfn + exec_fixed_va_reg->nr_pages;
778 	}
779 
780 	if (fixed_va_reg) {
781 		WARN_ON(fixed_va_reg->start_pfn < last_zone_end_pfn);
782 		kbase_region_tracker_insert(fixed_va_reg);
783 		last_zone_end_pfn = fixed_va_reg->start_pfn + fixed_va_reg->nr_pages;
784 	}
785 }
786 #else
kbase_region_tracker_ds_init(struct kbase_context * kctx,struct kbase_va_region * same_va_reg,struct kbase_va_region * custom_va_reg)787 static void kbase_region_tracker_ds_init(struct kbase_context *kctx,
788 		struct kbase_va_region *same_va_reg,
789 		struct kbase_va_region *custom_va_reg)
790 {
791 	kctx->reg_rbtree_same = RB_ROOT;
792 	kbase_region_tracker_insert(same_va_reg);
793 
794 	/* Although custom_va_reg and exec_va_reg don't always exist,
795 	 * initialize unconditionally because of the mem_view debugfs
796 	 * implementation which relies on them being empty.
797 	 *
798 	 * The difference between the two is that the EXEC_VA region
799 	 * is never initialized at this stage.
800 	 */
801 	kctx->reg_rbtree_custom = RB_ROOT;
802 	kctx->reg_rbtree_exec = RB_ROOT;
803 
804 	if (custom_va_reg)
805 		kbase_region_tracker_insert(custom_va_reg);
806 }
807 #endif /* MALI_USE_CSF */
808 
kbase_reg_flags_to_kctx(struct kbase_va_region * reg)809 static struct kbase_context *kbase_reg_flags_to_kctx(struct kbase_va_region *reg)
810 {
811 	struct kbase_context *kctx = NULL;
812 	struct rb_root *rbtree = reg->rbtree;
813 
814 	switch (reg->flags & KBASE_REG_ZONE_MASK) {
815 	case KBASE_REG_ZONE_CUSTOM_VA:
816 		kctx = container_of(rbtree, struct kbase_context, reg_rbtree_custom);
817 		break;
818 	case KBASE_REG_ZONE_SAME_VA:
819 		kctx = container_of(rbtree, struct kbase_context, reg_rbtree_same);
820 		break;
821 	case KBASE_REG_ZONE_EXEC_VA:
822 		kctx = container_of(rbtree, struct kbase_context, reg_rbtree_exec);
823 		break;
824 #if MALI_USE_CSF
825 	case KBASE_REG_ZONE_EXEC_FIXED_VA:
826 		kctx = container_of(rbtree, struct kbase_context, reg_rbtree_exec_fixed);
827 		break;
828 	case KBASE_REG_ZONE_FIXED_VA:
829 		kctx = container_of(rbtree, struct kbase_context, reg_rbtree_fixed);
830 		break;
831 	case KBASE_REG_ZONE_MCU_SHARED:
832 		/* This is only expected to be called on driver unload. */
833 		break;
834 #endif
835 	default:
836 		WARN(1, "Unknown zone in region: flags=0x%lx\n", reg->flags);
837 		break;
838 	}
839 
840 	return kctx;
841 }
842 
kbase_region_tracker_erase_rbtree(struct rb_root * rbtree)843 static void kbase_region_tracker_erase_rbtree(struct rb_root *rbtree)
844 {
845 	struct rb_node *rbnode;
846 	struct kbase_va_region *reg;
847 
848 	do {
849 		rbnode = rb_first(rbtree);
850 		if (rbnode) {
851 			rb_erase(rbnode, rbtree);
852 			reg = rb_entry(rbnode, struct kbase_va_region, rblink);
853 			WARN_ON(kbase_refcount_read(&reg->va_refcnt) != 1);
854 			if (kbase_page_migration_enabled)
855 				kbase_gpu_munmap(kbase_reg_flags_to_kctx(reg), reg);
856 			/* Reset the start_pfn - as the rbtree is being
857 			 * destroyed and we've already erased this region, there
858 			 * is no further need to attempt to remove it.
859 			 * This won't affect the cleanup if the region was
860 			 * being used as a sticky resource as the cleanup
861 			 * related to sticky resources anyways need to be
862 			 * performed before the term of region tracker.
863 			 */
864 			reg->start_pfn = 0;
865 			kbase_free_alloced_region(reg);
866 		}
867 	} while (rbnode);
868 }
869 
kbase_region_tracker_term(struct kbase_context * kctx)870 void kbase_region_tracker_term(struct kbase_context *kctx)
871 {
872 	WARN(kctx->as_nr != KBASEP_AS_NR_INVALID,
873 	     "kctx-%d_%d must first be scheduled out to flush GPU caches+tlbs before erasing remaining regions",
874 	     kctx->tgid, kctx->id);
875 
876 	kbase_gpu_vm_lock(kctx);
877 	kbase_region_tracker_erase_rbtree(&kctx->reg_rbtree_same);
878 	kbase_region_tracker_erase_rbtree(&kctx->reg_rbtree_custom);
879 	kbase_region_tracker_erase_rbtree(&kctx->reg_rbtree_exec);
880 #if MALI_USE_CSF
881 	WARN_ON(!list_empty(&kctx->csf.event_pages_head));
882 	kbase_region_tracker_erase_rbtree(&kctx->reg_rbtree_exec_fixed);
883 	kbase_region_tracker_erase_rbtree(&kctx->reg_rbtree_fixed);
884 
885 #endif
886 	kbase_gpu_vm_unlock(kctx);
887 }
888 
kbase_region_tracker_term_rbtree(struct rb_root * rbtree)889 void kbase_region_tracker_term_rbtree(struct rb_root *rbtree)
890 {
891 	kbase_region_tracker_erase_rbtree(rbtree);
892 }
893 
kbase_get_same_va_bits(struct kbase_context * kctx)894 static size_t kbase_get_same_va_bits(struct kbase_context *kctx)
895 {
896 	return min_t(size_t, kbase_get_num_cpu_va_bits(kctx),
897 			kctx->kbdev->gpu_props.mmu.va_bits);
898 }
899 
kbase_region_tracker_init(struct kbase_context * kctx)900 int kbase_region_tracker_init(struct kbase_context *kctx)
901 {
902 	struct kbase_va_region *same_va_reg;
903 	struct kbase_va_region *custom_va_reg = NULL;
904 	size_t same_va_bits = kbase_get_same_va_bits(kctx);
905 	u64 custom_va_size = KBASE_REG_ZONE_CUSTOM_VA_SIZE;
906 	u64 gpu_va_bits = kctx->kbdev->gpu_props.mmu.va_bits;
907 	u64 gpu_va_limit = (1ULL << gpu_va_bits) >> PAGE_SHIFT;
908 	u64 same_va_pages;
909 	u64 same_va_base = 1u;
910 	int err;
911 #if MALI_USE_CSF
912 	struct kbase_va_region *exec_va_reg;
913 	struct kbase_va_region *exec_fixed_va_reg;
914 	struct kbase_va_region *fixed_va_reg;
915 
916 	u64 exec_va_base;
917 	u64 fixed_va_end;
918 	u64 exec_fixed_va_base;
919 	u64 fixed_va_base;
920 	u64 fixed_va_pages;
921 #endif
922 
923 	/* Take the lock as kbase_free_alloced_region requires it */
924 	kbase_gpu_vm_lock(kctx);
925 
926 	same_va_pages = (1ULL << (same_va_bits - PAGE_SHIFT)) - same_va_base;
927 
928 #if MALI_USE_CSF
929 	if ((same_va_base + same_va_pages) > KBASE_REG_ZONE_EXEC_VA_BASE_64) {
930 		/* Depending on how the kernel is configured, it's possible (eg on aarch64) for
931 		 * same_va_bits to reach 48 bits. Cap same_va_pages so that the same_va zone
932 		 * doesn't cross into the exec_va zone.
933 		 */
934 		same_va_pages = KBASE_REG_ZONE_EXEC_VA_BASE_64 - same_va_base;
935 	}
936 #endif
937 
938 	/* all have SAME_VA */
939 	same_va_reg = kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_same, same_va_base,
940 					      same_va_pages, KBASE_REG_ZONE_SAME_VA);
941 
942 	if (!same_va_reg) {
943 		err = -ENOMEM;
944 		goto fail_unlock;
945 	}
946 	kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_SAME_VA, same_va_base,
947 				same_va_pages);
948 
949 	if (kbase_ctx_compat_mode(kctx)) {
950 		if (gpu_va_limit <= KBASE_REG_ZONE_CUSTOM_VA_BASE) {
951 			err = -EINVAL;
952 			goto fail_free_same_va;
953 		}
954 		/* If the current size of TMEM is out of range of the
955 		 * virtual address space addressable by the MMU then
956 		 * we should shrink it to fit
957 		 */
958 		if ((KBASE_REG_ZONE_CUSTOM_VA_BASE + KBASE_REG_ZONE_CUSTOM_VA_SIZE) >= gpu_va_limit)
959 			custom_va_size = gpu_va_limit - KBASE_REG_ZONE_CUSTOM_VA_BASE;
960 
961 		custom_va_reg = kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_custom,
962 							KBASE_REG_ZONE_CUSTOM_VA_BASE,
963 							custom_va_size, KBASE_REG_ZONE_CUSTOM_VA);
964 
965 		if (!custom_va_reg) {
966 			err = -ENOMEM;
967 			goto fail_free_same_va;
968 		}
969 		kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_CUSTOM_VA,
970 					KBASE_REG_ZONE_CUSTOM_VA_BASE,
971 					custom_va_size);
972 	} else {
973 		custom_va_size = 0;
974 	}
975 
976 #if MALI_USE_CSF
977 	/* The position of EXEC_VA depends on whether the client is 32-bit or 64-bit. */
978 	exec_va_base = KBASE_REG_ZONE_EXEC_VA_BASE_64;
979 
980 	/* Similarly the end of the FIXED_VA zone also depends on whether the client
981 	 * is 32 or 64-bits.
982 	 */
983 	fixed_va_end = KBASE_REG_ZONE_FIXED_VA_END_64;
984 
985 	if (kbase_ctx_compat_mode(kctx)) {
986 		exec_va_base = KBASE_REG_ZONE_EXEC_VA_BASE_32;
987 		fixed_va_end = KBASE_REG_ZONE_FIXED_VA_END_32;
988 	}
989 
990 	kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_EXEC_VA, exec_va_base,
991 				KBASE_REG_ZONE_EXEC_VA_SIZE);
992 
993 	exec_va_reg = kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_exec, exec_va_base,
994 					      KBASE_REG_ZONE_EXEC_VA_SIZE, KBASE_REG_ZONE_EXEC_VA);
995 
996 	if (!exec_va_reg) {
997 		err = -ENOMEM;
998 		goto fail_free_custom_va;
999 	}
1000 
1001 	exec_fixed_va_base = exec_va_base + KBASE_REG_ZONE_EXEC_VA_SIZE;
1002 
1003 	kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_EXEC_FIXED_VA, exec_fixed_va_base,
1004 				KBASE_REG_ZONE_EXEC_FIXED_VA_SIZE);
1005 
1006 	exec_fixed_va_reg =
1007 		kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_exec_fixed,
1008 					exec_fixed_va_base, KBASE_REG_ZONE_EXEC_FIXED_VA_SIZE,
1009 					KBASE_REG_ZONE_EXEC_FIXED_VA);
1010 
1011 	if (!exec_fixed_va_reg) {
1012 		err = -ENOMEM;
1013 		goto fail_free_exec_va;
1014 	}
1015 
1016 	fixed_va_base = exec_fixed_va_base + KBASE_REG_ZONE_EXEC_FIXED_VA_SIZE;
1017 	fixed_va_pages = fixed_va_end - fixed_va_base;
1018 
1019 	kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_FIXED_VA, fixed_va_base, fixed_va_pages);
1020 
1021 	fixed_va_reg = kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_fixed, fixed_va_base,
1022 					       fixed_va_pages, KBASE_REG_ZONE_FIXED_VA);
1023 
1024 	kctx->gpu_va_end = fixed_va_end;
1025 
1026 	if (!fixed_va_reg) {
1027 		err = -ENOMEM;
1028 		goto fail_free_exec_fixed_va;
1029 	}
1030 
1031 	kbase_region_tracker_ds_init(kctx, same_va_reg, custom_va_reg, exec_va_reg,
1032 				     exec_fixed_va_reg, fixed_va_reg);
1033 
1034 	INIT_LIST_HEAD(&kctx->csf.event_pages_head);
1035 #else
1036 	/* EXEC_VA zone's codepaths are slightly easier when its base_pfn is
1037 	 * initially U64_MAX
1038 	 */
1039 	kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_EXEC_VA, U64_MAX, 0u);
1040 	/* Other zones are 0: kbase_create_context() uses vzalloc */
1041 
1042 	kbase_region_tracker_ds_init(kctx, same_va_reg, custom_va_reg);
1043 	kctx->gpu_va_end = same_va_base + same_va_pages + custom_va_size;
1044 #endif
1045 	kctx->jit_va = false;
1046 
1047 	kbase_gpu_vm_unlock(kctx);
1048 	return 0;
1049 
1050 #if MALI_USE_CSF
1051 fail_free_exec_fixed_va:
1052 	kbase_free_alloced_region(exec_fixed_va_reg);
1053 fail_free_exec_va:
1054 	kbase_free_alloced_region(exec_va_reg);
1055 fail_free_custom_va:
1056 	if (custom_va_reg)
1057 		kbase_free_alloced_region(custom_va_reg);
1058 #endif
1059 
1060 fail_free_same_va:
1061 	kbase_free_alloced_region(same_va_reg);
1062 fail_unlock:
1063 	kbase_gpu_vm_unlock(kctx);
1064 	return err;
1065 }
1066 
kbase_has_exec_va_zone_locked(struct kbase_context * kctx)1067 static bool kbase_has_exec_va_zone_locked(struct kbase_context *kctx)
1068 {
1069 	struct kbase_reg_zone *exec_va_zone;
1070 
1071 	lockdep_assert_held(&kctx->reg_lock);
1072 	exec_va_zone = kbase_ctx_reg_zone_get(kctx, KBASE_REG_ZONE_EXEC_VA);
1073 
1074 	return (exec_va_zone->base_pfn != U64_MAX);
1075 }
1076 
kbase_has_exec_va_zone(struct kbase_context * kctx)1077 bool kbase_has_exec_va_zone(struct kbase_context *kctx)
1078 {
1079 	bool has_exec_va_zone;
1080 
1081 	kbase_gpu_vm_lock(kctx);
1082 	has_exec_va_zone = kbase_has_exec_va_zone_locked(kctx);
1083 	kbase_gpu_vm_unlock(kctx);
1084 
1085 	return has_exec_va_zone;
1086 }
1087 
1088 /**
1089  * kbase_region_tracker_has_allocs - Determine if any allocations have been made
1090  * on a context's region tracker
1091  *
1092  * @kctx: KBase context
1093  *
1094  * Check the context to determine if any allocations have been made yet from
1095  * any of its zones. This check should be done before resizing a zone, e.g. to
1096  * make space to add a second zone.
1097  *
1098  * Whilst a zone without allocations can be resized whilst other zones have
1099  * allocations, we still check all of @kctx 's zones anyway: this is a stronger
1100  * guarantee and should be adhered to when creating new zones anyway.
1101  *
1102  * Allocations from kbdev zones are not counted.
1103  *
1104  * Return: true if any allocs exist on any zone, false otherwise
1105  */
kbase_region_tracker_has_allocs(struct kbase_context * kctx)1106 static bool kbase_region_tracker_has_allocs(struct kbase_context *kctx)
1107 {
1108 	unsigned int zone_idx;
1109 
1110 	lockdep_assert_held(&kctx->reg_lock);
1111 
1112 	for (zone_idx = 0; zone_idx < KBASE_REG_ZONE_MAX; ++zone_idx) {
1113 		struct kbase_reg_zone *zone;
1114 		struct kbase_va_region *reg;
1115 		u64 zone_base_addr;
1116 		unsigned long zone_bits = KBASE_REG_ZONE(zone_idx);
1117 		unsigned long reg_zone;
1118 
1119 		if (!kbase_is_ctx_reg_zone(zone_bits))
1120 			continue;
1121 		zone = kbase_ctx_reg_zone_get(kctx, zone_bits);
1122 		zone_base_addr = zone->base_pfn << PAGE_SHIFT;
1123 
1124 		reg = kbase_region_tracker_find_region_base_address(
1125 			kctx, zone_base_addr);
1126 
1127 		if (!zone->va_size_pages) {
1128 			WARN(reg,
1129 			     "Should not have found a region that starts at 0x%.16llx for zone 0x%lx",
1130 			     (unsigned long long)zone_base_addr, zone_bits);
1131 			continue;
1132 		}
1133 
1134 		if (WARN(!reg,
1135 			 "There should always be a region that starts at 0x%.16llx for zone 0x%lx, couldn't find it",
1136 			 (unsigned long long)zone_base_addr, zone_bits))
1137 			return true; /* Safest return value */
1138 
1139 		reg_zone = reg->flags & KBASE_REG_ZONE_MASK;
1140 		if (WARN(reg_zone != zone_bits,
1141 			 "The region that starts at 0x%.16llx should be in zone 0x%lx but was found in the wrong zone 0x%lx",
1142 			 (unsigned long long)zone_base_addr, zone_bits,
1143 			 reg_zone))
1144 			return true; /* Safest return value */
1145 
1146 		/* Unless the region is completely free, of the same size as
1147 		 * the original zone, then it has allocs
1148 		 */
1149 		if ((!(reg->flags & KBASE_REG_FREE)) ||
1150 		    (reg->nr_pages != zone->va_size_pages))
1151 			return true;
1152 	}
1153 
1154 	/* All zones are the same size as originally made, so there are no
1155 	 * allocs
1156 	 */
1157 	return false;
1158 }
1159 
kbase_region_tracker_init_jit_64(struct kbase_context * kctx,u64 jit_va_pages)1160 static int kbase_region_tracker_init_jit_64(struct kbase_context *kctx,
1161 		u64 jit_va_pages)
1162 {
1163 	struct kbase_va_region *same_va_reg;
1164 	struct kbase_reg_zone *same_va_zone;
1165 	u64 same_va_zone_base_addr;
1166 	const unsigned long same_va_zone_bits = KBASE_REG_ZONE_SAME_VA;
1167 	struct kbase_va_region *custom_va_reg;
1168 	u64 jit_va_start;
1169 
1170 	lockdep_assert_held(&kctx->reg_lock);
1171 
1172 	/*
1173 	 * Modify the same VA free region after creation. The caller has
1174 	 * ensured that allocations haven't been made, as any allocations could
1175 	 * cause an overlap to happen with existing same VA allocations and the
1176 	 * custom VA zone.
1177 	 */
1178 	same_va_zone = kbase_ctx_reg_zone_get(kctx, same_va_zone_bits);
1179 	same_va_zone_base_addr = same_va_zone->base_pfn << PAGE_SHIFT;
1180 
1181 	same_va_reg = kbase_region_tracker_find_region_base_address(
1182 		kctx, same_va_zone_base_addr);
1183 	if (WARN(!same_va_reg,
1184 		 "Already found a free region at the start of every zone, but now cannot find any region for zone base 0x%.16llx zone 0x%lx",
1185 		 (unsigned long long)same_va_zone_base_addr, same_va_zone_bits))
1186 		return -ENOMEM;
1187 
1188 	/* kbase_region_tracker_has_allocs() in the caller has already ensured
1189 	 * that all of the zones have no allocs, so no need to check that again
1190 	 * on same_va_reg
1191 	 */
1192 	WARN_ON((!(same_va_reg->flags & KBASE_REG_FREE)) ||
1193 		same_va_reg->nr_pages != same_va_zone->va_size_pages);
1194 
1195 	if (same_va_reg->nr_pages < jit_va_pages ||
1196 	    same_va_zone->va_size_pages < jit_va_pages)
1197 		return -ENOMEM;
1198 
1199 	/* It's safe to adjust the same VA zone now */
1200 	same_va_reg->nr_pages -= jit_va_pages;
1201 	same_va_zone->va_size_pages -= jit_va_pages;
1202 	jit_va_start = kbase_reg_zone_end_pfn(same_va_zone);
1203 
1204 	/*
1205 	 * Create a custom VA zone at the end of the VA for allocations which
1206 	 * JIT can use so it doesn't have to allocate VA from the kernel.
1207 	 */
1208 	custom_va_reg = kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_custom, jit_va_start,
1209 						jit_va_pages, KBASE_REG_ZONE_CUSTOM_VA);
1210 
1211 	/*
1212 	 * The context will be destroyed if we fail here so no point
1213 	 * reverting the change we made to same_va.
1214 	 */
1215 	if (!custom_va_reg)
1216 		return -ENOMEM;
1217 	/* Since this is 64-bit, the custom zone will not have been
1218 	 * initialized, so initialize it now
1219 	 */
1220 	kbase_ctx_reg_zone_init(kctx, KBASE_REG_ZONE_CUSTOM_VA, jit_va_start,
1221 				jit_va_pages);
1222 
1223 	kbase_region_tracker_insert(custom_va_reg);
1224 	return 0;
1225 }
1226 
kbase_region_tracker_init_jit(struct kbase_context * kctx,u64 jit_va_pages,int max_allocations,int trim_level,int group_id,u64 phys_pages_limit)1227 int kbase_region_tracker_init_jit(struct kbase_context *kctx, u64 jit_va_pages,
1228 		int max_allocations, int trim_level, int group_id,
1229 		u64 phys_pages_limit)
1230 {
1231 	int err = 0;
1232 
1233 	if (trim_level < 0 || trim_level > BASE_JIT_MAX_TRIM_LEVEL)
1234 		return -EINVAL;
1235 
1236 	if (group_id < 0 || group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS)
1237 		return -EINVAL;
1238 
1239 	if (phys_pages_limit > jit_va_pages)
1240 		return -EINVAL;
1241 
1242 #if MALI_JIT_PRESSURE_LIMIT_BASE
1243 	if (phys_pages_limit != jit_va_pages)
1244 		kbase_ctx_flag_set(kctx, KCTX_JPL_ENABLED);
1245 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1246 
1247 	kbase_gpu_vm_lock(kctx);
1248 
1249 	/* Verify that a JIT_VA zone has not been created already. */
1250 	if (kctx->jit_va) {
1251 		err = -EINVAL;
1252 		goto exit_unlock;
1253 	}
1254 
1255 	/* If in 64-bit, we always lookup the SAME_VA zone. To ensure it has no
1256 	 * allocs, we can ensure there are no allocs anywhere.
1257 	 *
1258 	 * This check is also useful in 32-bit, just to make sure init of the
1259 	 * zone is always done before any allocs.
1260 	 */
1261 	if (kbase_region_tracker_has_allocs(kctx)) {
1262 		err = -ENOMEM;
1263 		goto exit_unlock;
1264 	}
1265 
1266 	if (!kbase_ctx_compat_mode(kctx))
1267 		err = kbase_region_tracker_init_jit_64(kctx, jit_va_pages);
1268 	/*
1269 	 * Nothing to do for 32-bit clients, JIT uses the existing
1270 	 * custom VA zone.
1271 	 */
1272 
1273 	if (!err) {
1274 		kctx->jit_max_allocations = max_allocations;
1275 		kctx->trim_level = trim_level;
1276 		kctx->jit_va = true;
1277 		kctx->jit_group_id = group_id;
1278 #if MALI_JIT_PRESSURE_LIMIT_BASE
1279 		kctx->jit_phys_pages_limit = phys_pages_limit;
1280 		dev_dbg(kctx->kbdev->dev, "phys_pages_limit set to %llu\n",
1281 				phys_pages_limit);
1282 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1283 	}
1284 
1285 exit_unlock:
1286 	kbase_gpu_vm_unlock(kctx);
1287 
1288 	return err;
1289 }
1290 
kbase_region_tracker_init_exec(struct kbase_context * kctx,u64 exec_va_pages)1291 int kbase_region_tracker_init_exec(struct kbase_context *kctx, u64 exec_va_pages)
1292 {
1293 #if !MALI_USE_CSF
1294 	struct kbase_va_region *exec_va_reg;
1295 	struct kbase_reg_zone *exec_va_zone;
1296 	struct kbase_reg_zone *target_zone;
1297 	struct kbase_va_region *target_reg;
1298 	u64 target_zone_base_addr;
1299 	unsigned long target_zone_bits;
1300 	u64 exec_va_start;
1301 	int err;
1302 #endif
1303 
1304 	/* The EXEC_VA zone shall be created by making space either:
1305 	 * - for 64-bit clients, at the end of the process's address space
1306 	 * - for 32-bit clients, in the CUSTOM zone
1307 	 *
1308 	 * Firstly, verify that the number of EXEC_VA pages requested by the
1309 	 * client is reasonable and then make sure that it is not greater than
1310 	 * the address space itself before calculating the base address of the
1311 	 * new zone.
1312 	 */
1313 	if (exec_va_pages == 0 || exec_va_pages > KBASE_REG_ZONE_EXEC_VA_MAX_PAGES)
1314 		return -EINVAL;
1315 
1316 #if MALI_USE_CSF
1317 	/* For CSF GPUs we now setup the EXEC_VA zone during initialization,
1318 	 * so this request is a null-op.
1319 	 */
1320 	return 0;
1321 #else
1322 	kbase_gpu_vm_lock(kctx);
1323 
1324 	/* Verify that we've not already created a EXEC_VA zone, and that the
1325 	 * EXEC_VA zone must come before JIT's CUSTOM_VA.
1326 	 */
1327 	if (kbase_has_exec_va_zone_locked(kctx) || kctx->jit_va) {
1328 		err = -EPERM;
1329 		goto exit_unlock;
1330 	}
1331 
1332 	if (exec_va_pages > kctx->gpu_va_end) {
1333 		err = -ENOMEM;
1334 		goto exit_unlock;
1335 	}
1336 
1337 	/* Verify no allocations have already been made */
1338 	if (kbase_region_tracker_has_allocs(kctx)) {
1339 		err = -ENOMEM;
1340 		goto exit_unlock;
1341 	}
1342 
1343 	if (kbase_ctx_compat_mode(kctx)) {
1344 		/* 32-bit client: take from CUSTOM_VA zone */
1345 		target_zone_bits = KBASE_REG_ZONE_CUSTOM_VA;
1346 	} else {
1347 		/* 64-bit client: take from SAME_VA zone */
1348 		target_zone_bits = KBASE_REG_ZONE_SAME_VA;
1349 	}
1350 
1351 	target_zone = kbase_ctx_reg_zone_get(kctx, target_zone_bits);
1352 	target_zone_base_addr = target_zone->base_pfn << PAGE_SHIFT;
1353 
1354 	target_reg = kbase_region_tracker_find_region_base_address(
1355 		kctx, target_zone_base_addr);
1356 	if (WARN(!target_reg,
1357 		 "Already found a free region at the start of every zone, but now cannot find any region for zone base 0x%.16llx zone 0x%lx",
1358 		 (unsigned long long)target_zone_base_addr, target_zone_bits)) {
1359 		err = -ENOMEM;
1360 		goto exit_unlock;
1361 	}
1362 	/* kbase_region_tracker_has_allocs() above has already ensured that all
1363 	 * of the zones have no allocs, so no need to check that again on
1364 	 * target_reg
1365 	 */
1366 	WARN_ON((!(target_reg->flags & KBASE_REG_FREE)) ||
1367 		target_reg->nr_pages != target_zone->va_size_pages);
1368 
1369 	if (target_reg->nr_pages <= exec_va_pages ||
1370 	    target_zone->va_size_pages <= exec_va_pages) {
1371 		err = -ENOMEM;
1372 		goto exit_unlock;
1373 	}
1374 
1375 	/* Taken from the end of the target zone */
1376 	exec_va_start = kbase_reg_zone_end_pfn(target_zone) - exec_va_pages;
1377 
1378 	exec_va_reg = kbase_alloc_free_region(kctx->kbdev, &kctx->reg_rbtree_exec, exec_va_start,
1379 					      exec_va_pages, KBASE_REG_ZONE_EXEC_VA);
1380 	if (!exec_va_reg) {
1381 		err = -ENOMEM;
1382 		goto exit_unlock;
1383 	}
1384 	/* Update EXEC_VA zone
1385 	 *
1386 	 * not using kbase_ctx_reg_zone_init() - it was already initialized
1387 	 */
1388 	exec_va_zone = kbase_ctx_reg_zone_get(kctx, KBASE_REG_ZONE_EXEC_VA);
1389 	exec_va_zone->base_pfn = exec_va_start;
1390 	exec_va_zone->va_size_pages = exec_va_pages;
1391 
1392 	/* Update target zone and corresponding region */
1393 	target_reg->nr_pages -= exec_va_pages;
1394 	target_zone->va_size_pages -= exec_va_pages;
1395 
1396 	kbase_region_tracker_insert(exec_va_reg);
1397 	err = 0;
1398 
1399 exit_unlock:
1400 	kbase_gpu_vm_unlock(kctx);
1401 	return err;
1402 #endif /* MALI_USE_CSF */
1403 }
1404 
1405 #if MALI_USE_CSF
kbase_mcu_shared_interface_region_tracker_term(struct kbase_device * kbdev)1406 void kbase_mcu_shared_interface_region_tracker_term(struct kbase_device *kbdev)
1407 {
1408 	kbase_region_tracker_term_rbtree(&kbdev->csf.shared_reg_rbtree);
1409 }
1410 
kbase_mcu_shared_interface_region_tracker_init(struct kbase_device * kbdev)1411 int kbase_mcu_shared_interface_region_tracker_init(struct kbase_device *kbdev)
1412 {
1413 	struct kbase_va_region *shared_reg;
1414 	u64 shared_reg_start_pfn;
1415 	u64 shared_reg_size;
1416 
1417 	shared_reg_start_pfn = KBASE_REG_ZONE_MCU_SHARED_BASE;
1418 	shared_reg_size = KBASE_REG_ZONE_MCU_SHARED_SIZE;
1419 
1420 	kbdev->csf.shared_reg_rbtree = RB_ROOT;
1421 
1422 	shared_reg =
1423 		kbase_alloc_free_region(kbdev, &kbdev->csf.shared_reg_rbtree, shared_reg_start_pfn,
1424 					shared_reg_size, KBASE_REG_ZONE_MCU_SHARED);
1425 	if (!shared_reg)
1426 		return -ENOMEM;
1427 
1428 	kbase_region_tracker_insert(shared_reg);
1429 	return 0;
1430 }
1431 #endif
1432 
kbasep_mem_page_size_init(struct kbase_device * kbdev)1433 static void kbasep_mem_page_size_init(struct kbase_device *kbdev)
1434 {
1435 #if IS_ENABLED(CONFIG_LARGE_PAGE_ALLOC_OVERRIDE)
1436 #if IS_ENABLED(CONFIG_LARGE_PAGE_ALLOC)
1437 	kbdev->pagesize_2mb = true;
1438 	if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_LARGE_PAGE_ALLOC) != 1) {
1439 		dev_warn(
1440 			kbdev->dev,
1441 			"2MB page is enabled by force while current GPU-HW doesn't meet the requirement to do so.\n");
1442 	}
1443 #else /* IS_ENABLED(CONFIG_LARGE_PAGE_ALLOC) */
1444 	kbdev->pagesize_2mb = false;
1445 #endif /* IS_ENABLED(CONFIG_LARGE_PAGE_ALLOC) */
1446 #else /* IS_ENABLED(CONFIG_LARGE_PAGE_ALLOC_OVERRIDE) */
1447 	/* Set it to the default based on which GPU is present */
1448 	kbdev->pagesize_2mb = kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_LARGE_PAGE_ALLOC);
1449 #endif /* IS_ENABLED(CONFIG_LARGE_PAGE_ALLOC_OVERRIDE) */
1450 }
1451 
kbase_mem_init(struct kbase_device * kbdev)1452 int kbase_mem_init(struct kbase_device *kbdev)
1453 {
1454 	int err = 0;
1455 	struct kbasep_mem_device *memdev;
1456 	char va_region_slab_name[VA_REGION_SLAB_NAME_SIZE];
1457 #if IS_ENABLED(CONFIG_OF)
1458 	struct device_node *mgm_node = NULL;
1459 #endif
1460 
1461 	KBASE_DEBUG_ASSERT(kbdev);
1462 
1463 	memdev = &kbdev->memdev;
1464 
1465 	kbasep_mem_page_size_init(kbdev);
1466 
1467 	scnprintf(va_region_slab_name, VA_REGION_SLAB_NAME_SIZE, VA_REGION_SLAB_NAME_PREFIX "%s",
1468 		  kbdev->devname);
1469 
1470 	/* Initialize slab cache for kbase_va_regions */
1471 	kbdev->va_region_slab =
1472 		kmem_cache_create(va_region_slab_name, sizeof(struct kbase_va_region), 0, 0, NULL);
1473 	if (kbdev->va_region_slab == NULL) {
1474 		dev_err(kbdev->dev, "Failed to create va_region_slab\n");
1475 		return -ENOMEM;
1476 	}
1477 
1478 	kbase_mem_migrate_init(kbdev);
1479 	kbase_mem_pool_group_config_set_max_size(&kbdev->mem_pool_defaults,
1480 		KBASE_MEM_POOL_MAX_SIZE_KCTX);
1481 
1482 	/* Initialize memory usage */
1483 	atomic_set(&memdev->used_pages, 0);
1484 
1485 	spin_lock_init(&kbdev->gpu_mem_usage_lock);
1486 	kbdev->total_gpu_pages = 0;
1487 	kbdev->process_root = RB_ROOT;
1488 	kbdev->dma_buf_root = RB_ROOT;
1489 	mutex_init(&kbdev->dma_buf_lock);
1490 
1491 #ifdef IR_THRESHOLD
1492 	atomic_set(&memdev->ir_threshold, IR_THRESHOLD);
1493 #else
1494 	atomic_set(&memdev->ir_threshold, DEFAULT_IR_THRESHOLD);
1495 #endif
1496 
1497 	kbdev->mgm_dev = &kbase_native_mgm_dev;
1498 
1499 #if IS_ENABLED(CONFIG_OF)
1500 	/* Check to see whether or not a platform-specific memory group manager
1501 	 * is configured and available.
1502 	 */
1503 	mgm_node = of_parse_phandle(kbdev->dev->of_node,
1504 		"physical-memory-group-manager", 0);
1505 	if (!mgm_node) {
1506 		dev_info(kbdev->dev,
1507 			"No memory group manager is configured\n");
1508 	} else {
1509 		struct platform_device *const pdev =
1510 			of_find_device_by_node(mgm_node);
1511 
1512 		if (!pdev) {
1513 			dev_err(kbdev->dev,
1514 				"The configured memory group manager was not found\n");
1515 		} else {
1516 			kbdev->mgm_dev = platform_get_drvdata(pdev);
1517 			if (!kbdev->mgm_dev) {
1518 				dev_info(kbdev->dev,
1519 					"Memory group manager is not ready\n");
1520 				err = -EPROBE_DEFER;
1521 			} else if (!try_module_get(kbdev->mgm_dev->owner)) {
1522 				dev_err(kbdev->dev,
1523 					"Failed to get memory group manger module\n");
1524 				err = -ENODEV;
1525 				kbdev->mgm_dev = NULL;
1526 			} else {
1527 				dev_info(kbdev->dev,
1528 					"Memory group manager successfully loaded\n");
1529 			}
1530 		}
1531 		of_node_put(mgm_node);
1532 	}
1533 #endif
1534 
1535 	if (likely(!err)) {
1536 		struct kbase_mem_pool_group_config mem_pool_defaults;
1537 
1538 		kbase_mem_pool_group_config_set_max_size(&mem_pool_defaults,
1539 			KBASE_MEM_POOL_MAX_SIZE_KBDEV);
1540 
1541 		err = kbase_mem_pool_group_init(&kbdev->mem_pools, kbdev, &mem_pool_defaults, NULL);
1542 	}
1543 
1544 	return err;
1545 }
1546 
kbase_mem_halt(struct kbase_device * kbdev)1547 void kbase_mem_halt(struct kbase_device *kbdev)
1548 {
1549 	CSTD_UNUSED(kbdev);
1550 }
1551 
kbase_mem_term(struct kbase_device * kbdev)1552 void kbase_mem_term(struct kbase_device *kbdev)
1553 {
1554 	struct kbasep_mem_device *memdev;
1555 	int pages;
1556 
1557 	KBASE_DEBUG_ASSERT(kbdev);
1558 
1559 	memdev = &kbdev->memdev;
1560 
1561 	pages = atomic_read(&memdev->used_pages);
1562 	if (pages != 0)
1563 		dev_warn(kbdev->dev, "%s: %d pages in use!\n", __func__, pages);
1564 
1565 	kbase_mem_pool_group_term(&kbdev->mem_pools);
1566 
1567 	kbase_mem_migrate_term(kbdev);
1568 
1569 	kmem_cache_destroy(kbdev->va_region_slab);
1570 	kbdev->va_region_slab = NULL;
1571 
1572 	WARN_ON(kbdev->total_gpu_pages);
1573 	WARN_ON(!RB_EMPTY_ROOT(&kbdev->process_root));
1574 	WARN_ON(!RB_EMPTY_ROOT(&kbdev->dma_buf_root));
1575 	mutex_destroy(&kbdev->dma_buf_lock);
1576 
1577 	if (kbdev->mgm_dev)
1578 		module_put(kbdev->mgm_dev->owner);
1579 }
1580 KBASE_EXPORT_TEST_API(kbase_mem_term);
1581 
1582 /**
1583  * kbase_alloc_free_region - Allocate a free region object.
1584  *
1585  * @kbdev:     kbase device
1586  * @rbtree:    Backlink to the red-black tree of memory regions.
1587  * @start_pfn: The Page Frame Number in GPU virtual address space.
1588  * @nr_pages:  The size of the region in pages.
1589  * @zone:      KBASE_REG_ZONE_CUSTOM_VA or KBASE_REG_ZONE_SAME_VA
1590  *
1591  * The allocated object is not part of any list yet, and is flagged as
1592  * KBASE_REG_FREE. No mapping is allocated yet.
1593  *
1594  * zone is KBASE_REG_ZONE_CUSTOM_VA or KBASE_REG_ZONE_SAME_VA.
1595  *
1596  * Return: pointer to the allocated region object on success, NULL otherwise.
1597  */
kbase_alloc_free_region(struct kbase_device * kbdev,struct rb_root * rbtree,u64 start_pfn,size_t nr_pages,int zone)1598 struct kbase_va_region *kbase_alloc_free_region(struct kbase_device *kbdev, struct rb_root *rbtree,
1599 						u64 start_pfn, size_t nr_pages, int zone)
1600 {
1601 	struct kbase_va_region *new_reg;
1602 
1603 	KBASE_DEBUG_ASSERT(rbtree != NULL);
1604 
1605 	/* zone argument should only contain zone related region flags */
1606 	KBASE_DEBUG_ASSERT((zone & ~KBASE_REG_ZONE_MASK) == 0);
1607 	KBASE_DEBUG_ASSERT(nr_pages > 0);
1608 	/* 64-bit address range is the max */
1609 	KBASE_DEBUG_ASSERT(start_pfn + nr_pages <= (U64_MAX / PAGE_SIZE));
1610 
1611 	new_reg = kmem_cache_zalloc(kbdev->va_region_slab, GFP_KERNEL);
1612 
1613 	if (!new_reg)
1614 		return NULL;
1615 
1616 	kbase_refcount_set(&new_reg->va_refcnt, 1);
1617 	atomic_set(&new_reg->no_user_free_count, 0);
1618 	new_reg->cpu_alloc = NULL; /* no alloc bound yet */
1619 	new_reg->gpu_alloc = NULL; /* no alloc bound yet */
1620 	new_reg->rbtree = rbtree;
1621 	new_reg->flags = zone | KBASE_REG_FREE;
1622 
1623 	new_reg->flags |= KBASE_REG_GROWABLE;
1624 
1625 	new_reg->start_pfn = start_pfn;
1626 	new_reg->nr_pages = nr_pages;
1627 
1628 	INIT_LIST_HEAD(&new_reg->jit_node);
1629 	INIT_LIST_HEAD(&new_reg->link);
1630 
1631 	return new_reg;
1632 }
1633 
1634 KBASE_EXPORT_TEST_API(kbase_alloc_free_region);
1635 
1636 /**
1637  * kbase_free_alloced_region - Free a region object.
1638  *
1639  * @reg: Region
1640  *
1641  * The described region must be freed of any mapping.
1642  *
1643  * If the region is not flagged as KBASE_REG_FREE, the region's
1644  * alloc object will be released.
1645  * It is a bug if no alloc object exists for non-free regions.
1646  *
1647  * If region is KBASE_REG_ZONE_MCU_SHARED it is freed
1648  */
kbase_free_alloced_region(struct kbase_va_region * reg)1649 void kbase_free_alloced_region(struct kbase_va_region *reg)
1650 {
1651 #if MALI_USE_CSF
1652 	if ((reg->flags & KBASE_REG_ZONE_MASK) ==
1653 			KBASE_REG_ZONE_MCU_SHARED) {
1654 		kfree(reg);
1655 		return;
1656 	}
1657 #endif
1658 	if (!(reg->flags & KBASE_REG_FREE)) {
1659 		struct kbase_context *kctx = kbase_reg_flags_to_kctx(reg);
1660 
1661 		if (WARN_ON(!kctx))
1662 			return;
1663 
1664 		if (WARN_ON(kbase_is_region_invalid(reg)))
1665 			return;
1666 
1667 		dev_dbg(kctx->kbdev->dev, "Freeing memory region %pK\n",
1668 			(void *)reg);
1669 #if MALI_USE_CSF
1670 		if (reg->flags & KBASE_REG_CSF_EVENT)
1671 			/*
1672 			 * This should not be reachable if called from 'mcu_shared' functions
1673 			 * such as:
1674 			 * kbase_csf_firmware_mcu_shared_mapping_init
1675 			 * kbase_csf_firmware_mcu_shared_mapping_term
1676 			 */
1677 
1678 			kbase_unlink_event_mem_page(kctx, reg);
1679 #endif
1680 
1681 		mutex_lock(&kctx->jit_evict_lock);
1682 
1683 		/*
1684 		 * The physical allocation should have been removed from the
1685 		 * eviction list before this function is called. However, in the
1686 		 * case of abnormal process termination or the app leaking the
1687 		 * memory kbase_mem_free_region is not called so it can still be
1688 		 * on the list at termination time of the region tracker.
1689 		 */
1690 		if (!list_empty(&reg->gpu_alloc->evict_node)) {
1691 			/*
1692 			 * Unlink the physical allocation before unmaking it
1693 			 * evictable so that the allocation isn't grown back to
1694 			 * its last backed size as we're going to unmap it
1695 			 * anyway.
1696 			 */
1697 			reg->cpu_alloc->reg = NULL;
1698 			if (reg->cpu_alloc != reg->gpu_alloc)
1699 				reg->gpu_alloc->reg = NULL;
1700 
1701 			mutex_unlock(&kctx->jit_evict_lock);
1702 
1703 			/*
1704 			 * If a region has been made evictable then we must
1705 			 * unmake it before trying to free it.
1706 			 * If the memory hasn't been reclaimed it will be
1707 			 * unmapped and freed below, if it has been reclaimed
1708 			 * then the operations below are no-ops.
1709 			 */
1710 			if (reg->flags & KBASE_REG_DONT_NEED) {
1711 				KBASE_DEBUG_ASSERT(reg->cpu_alloc->type ==
1712 						   KBASE_MEM_TYPE_NATIVE);
1713 				kbase_mem_evictable_unmake(reg->gpu_alloc);
1714 			}
1715 		} else {
1716 			mutex_unlock(&kctx->jit_evict_lock);
1717 		}
1718 
1719 		/*
1720 		 * Remove the region from the sticky resource metadata
1721 		 * list should it be there.
1722 		 */
1723 		kbase_sticky_resource_release_force(kctx, NULL,
1724 				reg->start_pfn << PAGE_SHIFT);
1725 
1726 		kbase_mem_phy_alloc_put(reg->cpu_alloc);
1727 		kbase_mem_phy_alloc_put(reg->gpu_alloc);
1728 
1729 		reg->flags |= KBASE_REG_VA_FREED;
1730 		kbase_va_region_alloc_put(kctx, reg);
1731 	} else {
1732 		kfree(reg);
1733 	}
1734 }
1735 
1736 KBASE_EXPORT_TEST_API(kbase_free_alloced_region);
1737 
kbase_gpu_mmap(struct kbase_context * kctx,struct kbase_va_region * reg,u64 addr,size_t nr_pages,size_t align,enum kbase_caller_mmu_sync_info mmu_sync_info)1738 int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg,
1739 		   u64 addr, size_t nr_pages, size_t align,
1740 		   enum kbase_caller_mmu_sync_info mmu_sync_info)
1741 {
1742 	int err;
1743 	size_t i = 0;
1744 	unsigned long attr;
1745 	unsigned long mask = ~KBASE_REG_MEMATTR_MASK;
1746 	unsigned long gwt_mask = ~0;
1747 	int group_id;
1748 	struct kbase_mem_phy_alloc *alloc;
1749 
1750 #ifdef CONFIG_MALI_CINSTR_GWT
1751 	if (kctx->gwt_enabled)
1752 		gwt_mask = ~KBASE_REG_GPU_WR;
1753 #endif
1754 
1755 	if ((kctx->kbdev->system_coherency == COHERENCY_ACE) &&
1756 		(reg->flags & KBASE_REG_SHARE_BOTH))
1757 		attr = KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_OUTER_WA);
1758 	else
1759 		attr = KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_WRITE_ALLOC);
1760 
1761 	KBASE_DEBUG_ASSERT(kctx != NULL);
1762 	KBASE_DEBUG_ASSERT(reg != NULL);
1763 
1764 	err = kbase_add_va_region(kctx, reg, addr, nr_pages, align);
1765 	if (err)
1766 		return err;
1767 
1768 	alloc = reg->gpu_alloc;
1769 	group_id = alloc->group_id;
1770 
1771 	if (reg->gpu_alloc->type == KBASE_MEM_TYPE_ALIAS) {
1772 		u64 const stride = alloc->imported.alias.stride;
1773 
1774 		KBASE_DEBUG_ASSERT(alloc->imported.alias.aliased);
1775 		for (i = 0; i < alloc->imported.alias.nents; i++) {
1776 			if (alloc->imported.alias.aliased[i].alloc) {
1777 				err = kbase_mmu_insert_aliased_pages(
1778 					kctx->kbdev, &kctx->mmu, reg->start_pfn + (i * stride),
1779 					alloc->imported.alias.aliased[i].alloc->pages +
1780 						alloc->imported.alias.aliased[i].offset,
1781 					alloc->imported.alias.aliased[i].length,
1782 					reg->flags & gwt_mask, kctx->as_nr, group_id, mmu_sync_info,
1783 					NULL);
1784 				if (err)
1785 					goto bad_aliased_insert;
1786 
1787 				/* Note: mapping count is tracked at alias
1788 				 * creation time
1789 				 */
1790 			} else {
1791 				err = kbase_mmu_insert_single_aliased_page(
1792 					kctx, reg->start_pfn + i * stride, kctx->aliasing_sink_page,
1793 					alloc->imported.alias.aliased[i].length,
1794 					(reg->flags & mask & gwt_mask) | attr, group_id,
1795 					mmu_sync_info);
1796 
1797 				if (err)
1798 					goto bad_aliased_insert;
1799 			}
1800 		}
1801 	} else {
1802 		if (reg->gpu_alloc->type == KBASE_MEM_TYPE_IMPORTED_UMM ||
1803 		    reg->gpu_alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF) {
1804 
1805 			err = kbase_mmu_insert_imported_pages(
1806 				kctx->kbdev, &kctx->mmu, reg->start_pfn,
1807 				kbase_get_gpu_phy_pages(reg), kbase_reg_current_backed_size(reg),
1808 				reg->flags & gwt_mask, kctx->as_nr, group_id, mmu_sync_info, reg);
1809 		} else {
1810 			err = kbase_mmu_insert_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn,
1811 						     kbase_get_gpu_phy_pages(reg),
1812 						     kbase_reg_current_backed_size(reg),
1813 						     reg->flags & gwt_mask, kctx->as_nr, group_id,
1814 						     mmu_sync_info, reg, true);
1815 		}
1816 
1817 		if (err)
1818 			goto bad_insert;
1819 		kbase_mem_phy_alloc_gpu_mapped(alloc);
1820 	}
1821 
1822 	if (reg->flags & KBASE_REG_IMPORT_PAD &&
1823 	    !WARN_ON(reg->nr_pages < reg->gpu_alloc->nents) &&
1824 	    reg->gpu_alloc->type == KBASE_MEM_TYPE_IMPORTED_UMM &&
1825 	    reg->gpu_alloc->imported.umm.current_mapping_usage_count) {
1826 		/* For padded imported dma-buf or user-buf memory, map the dummy
1827 		 * aliasing page from the end of the imported pages, to the end of
1828 		 * the region using a read only mapping.
1829 		 *
1830 		 * Only map when it's imported dma-buf memory that is currently
1831 		 * mapped.
1832 		 *
1833 		 * Assume reg->gpu_alloc->nents is the number of actual pages
1834 		 * in the dma-buf memory.
1835 		 */
1836 		err = kbase_mmu_insert_single_imported_page(
1837 			kctx, reg->start_pfn + reg->gpu_alloc->nents, kctx->aliasing_sink_page,
1838 			reg->nr_pages - reg->gpu_alloc->nents,
1839 			(reg->flags | KBASE_REG_GPU_RD) & ~KBASE_REG_GPU_WR, KBASE_MEM_GROUP_SINK,
1840 			mmu_sync_info);
1841 		if (err)
1842 			goto bad_insert;
1843 	}
1844 
1845 	return err;
1846 
1847 bad_aliased_insert:
1848 	while (i-- > 0) {
1849 		struct tagged_addr *phys_alloc = NULL;
1850 		u64 const stride = alloc->imported.alias.stride;
1851 
1852 		if (alloc->imported.alias.aliased[i].alloc != NULL)
1853 			phys_alloc = alloc->imported.alias.aliased[i].alloc->pages +
1854 				     alloc->imported.alias.aliased[i].offset;
1855 
1856 		kbase_mmu_teardown_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn + (i * stride),
1857 					 phys_alloc, alloc->imported.alias.aliased[i].length,
1858 					 alloc->imported.alias.aliased[i].length, kctx->as_nr,
1859 					 false);
1860 	}
1861 bad_insert:
1862 	kbase_remove_va_region(kctx->kbdev, reg);
1863 
1864 	return err;
1865 }
1866 
1867 KBASE_EXPORT_TEST_API(kbase_gpu_mmap);
1868 
1869 static void kbase_jd_user_buf_unmap(struct kbase_context *kctx, struct kbase_mem_phy_alloc *alloc,
1870 				    struct kbase_va_region *reg, bool writeable);
1871 
kbase_gpu_munmap(struct kbase_context * kctx,struct kbase_va_region * reg)1872 int kbase_gpu_munmap(struct kbase_context *kctx, struct kbase_va_region *reg)
1873 {
1874 	int err = 0;
1875 	struct kbase_mem_phy_alloc *alloc;
1876 
1877 	if (reg->start_pfn == 0)
1878 		return 0;
1879 
1880 	if (!reg->gpu_alloc)
1881 		return -EINVAL;
1882 
1883 	alloc = reg->gpu_alloc;
1884 
1885 	/* Tear down GPU page tables, depending on memory type. */
1886 	switch (alloc->type) {
1887 	case KBASE_MEM_TYPE_ALIAS: {
1888 			size_t i = 0;
1889 			/* Due to the way the number of valid PTEs and ATEs are tracked
1890 			 * currently, only the GPU virtual range that is backed & mapped
1891 			 * should be passed to the kbase_mmu_teardown_pages() function,
1892 			 * hence individual aliased regions needs to be unmapped
1893 			 * separately.
1894 			 */
1895 			for (i = 0; i < alloc->imported.alias.nents; i++) {
1896 				struct tagged_addr *phys_alloc = NULL;
1897 				int err_loop;
1898 
1899 				if (alloc->imported.alias.aliased[i].alloc != NULL)
1900 					phys_alloc = alloc->imported.alias.aliased[i].alloc->pages +
1901 						     alloc->imported.alias.aliased[i].offset;
1902 
1903 				err_loop = kbase_mmu_teardown_pages(
1904 					kctx->kbdev, &kctx->mmu,
1905 					reg->start_pfn + (i * alloc->imported.alias.stride),
1906 					phys_alloc, alloc->imported.alias.aliased[i].length,
1907 					alloc->imported.alias.aliased[i].length, kctx->as_nr,
1908 					false);
1909 
1910 				if (WARN_ON_ONCE(err_loop))
1911 					err = err_loop;
1912 			}
1913 		}
1914 		break;
1915 	case KBASE_MEM_TYPE_IMPORTED_UMM: {
1916 			size_t nr_phys_pages = reg->nr_pages;
1917 			size_t nr_virt_pages = reg->nr_pages;
1918 			/* If the region has import padding and falls under the threshold for
1919 			 * issuing a partial GPU cache flush, we want to reduce the number of
1920 			 * physical pages that get flushed.
1921 
1922 			 * This is symmetric with case of mapping the memory, which first maps
1923 			 * each imported physical page to a separate virtual page, and then
1924 			 * maps the single aliasing sink page to each of the virtual padding
1925 			 * pages.
1926 			 */
1927 			if (reg->flags & KBASE_REG_IMPORT_PAD)
1928 				nr_phys_pages = alloc->nents + 1;
1929 
1930 			err = kbase_mmu_teardown_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn,
1931 						       alloc->pages, nr_phys_pages, nr_virt_pages,
1932 						       kctx->as_nr, true);
1933 		}
1934 		break;
1935 	case KBASE_MEM_TYPE_IMPORTED_USER_BUF: {
1936 			size_t nr_reg_pages = kbase_reg_current_backed_size(reg);
1937 
1938 			err = kbase_mmu_teardown_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn,
1939 						       alloc->pages, nr_reg_pages, nr_reg_pages,
1940 						       kctx->as_nr, true);
1941 		}
1942 		break;
1943 	default: {
1944 			size_t nr_reg_pages = kbase_reg_current_backed_size(reg);
1945 
1946 			err = kbase_mmu_teardown_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn,
1947 						       alloc->pages, nr_reg_pages, nr_reg_pages,
1948 						       kctx->as_nr, false);
1949 		}
1950 		break;
1951 	}
1952 
1953 	/* Update tracking, and other cleanup, depending on memory type. */
1954 	switch (alloc->type) {
1955 	case KBASE_MEM_TYPE_ALIAS:
1956 		/* We mark the source allocs as unmapped from the GPU when
1957 		 * putting reg's allocs
1958 		 */
1959 		break;
1960 	case KBASE_MEM_TYPE_IMPORTED_USER_BUF: {
1961 		struct kbase_alloc_import_user_buf *user_buf = &alloc->imported.user_buf;
1962 
1963 		if (user_buf->current_mapping_usage_count & PINNED_ON_IMPORT) {
1964 			user_buf->current_mapping_usage_count &= ~PINNED_ON_IMPORT;
1965 
1966 			/* The allocation could still have active mappings. */
1967 			if (user_buf->current_mapping_usage_count == 0) {
1968 				kbase_jd_user_buf_unmap(kctx, alloc, reg,
1969 							(reg->flags &
1970 							 (KBASE_REG_CPU_WR | KBASE_REG_GPU_WR)));
1971 			}
1972 		}
1973 	}
1974 		fallthrough;
1975 	default:
1976 		kbase_mem_phy_alloc_gpu_unmapped(reg->gpu_alloc);
1977 		break;
1978 	}
1979 
1980 	return err;
1981 }
1982 
kbasep_find_enclosing_cpu_mapping(struct kbase_context * kctx,unsigned long uaddr,size_t size,u64 * offset)1983 static struct kbase_cpu_mapping *kbasep_find_enclosing_cpu_mapping(
1984 		struct kbase_context *kctx,
1985 		unsigned long uaddr, size_t size, u64 *offset)
1986 {
1987 	struct vm_area_struct *vma;
1988 	struct kbase_cpu_mapping *map;
1989 	unsigned long vm_pgoff_in_region;
1990 	unsigned long vm_off_in_region;
1991 	unsigned long map_start;
1992 	size_t map_size;
1993 
1994 	lockdep_assert_held(kbase_mem_get_process_mmap_lock());
1995 
1996 	if ((uintptr_t) uaddr + size < (uintptr_t) uaddr) /* overflow check */
1997 		return NULL;
1998 
1999 	vma = find_vma_intersection(current->mm, uaddr, uaddr+size);
2000 
2001 	if (!vma || vma->vm_start > uaddr)
2002 		return NULL;
2003 	if (vma->vm_ops != &kbase_vm_ops)
2004 		/* Not ours! */
2005 		return NULL;
2006 
2007 	map = vma->vm_private_data;
2008 
2009 	if (map->kctx != kctx)
2010 		/* Not from this context! */
2011 		return NULL;
2012 
2013 	vm_pgoff_in_region = vma->vm_pgoff - map->region->start_pfn;
2014 	vm_off_in_region = vm_pgoff_in_region << PAGE_SHIFT;
2015 	map_start = vma->vm_start - vm_off_in_region;
2016 	map_size = map->region->nr_pages << PAGE_SHIFT;
2017 
2018 	if ((uaddr + size) > (map_start + map_size))
2019 		/* Not within the CPU mapping */
2020 		return NULL;
2021 
2022 	*offset = (uaddr - vma->vm_start) + vm_off_in_region;
2023 
2024 	return map;
2025 }
2026 
kbasep_find_enclosing_cpu_mapping_offset(struct kbase_context * kctx,unsigned long uaddr,size_t size,u64 * offset)2027 int kbasep_find_enclosing_cpu_mapping_offset(
2028 		struct kbase_context *kctx,
2029 		unsigned long uaddr, size_t size, u64 *offset)
2030 {
2031 	struct kbase_cpu_mapping *map;
2032 
2033 	kbase_os_mem_map_lock(kctx);
2034 
2035 	map = kbasep_find_enclosing_cpu_mapping(kctx, uaddr, size, offset);
2036 
2037 	kbase_os_mem_map_unlock(kctx);
2038 
2039 	if (!map)
2040 		return -EINVAL;
2041 
2042 	return 0;
2043 }
2044 
2045 KBASE_EXPORT_TEST_API(kbasep_find_enclosing_cpu_mapping_offset);
2046 
kbasep_find_enclosing_gpu_mapping_start_and_offset(struct kbase_context * kctx,u64 gpu_addr,size_t size,u64 * start,u64 * offset)2047 int kbasep_find_enclosing_gpu_mapping_start_and_offset(struct kbase_context *kctx,
2048 		u64 gpu_addr, size_t size, u64 *start, u64 *offset)
2049 {
2050 	struct kbase_va_region *region;
2051 
2052 	kbase_gpu_vm_lock(kctx);
2053 
2054 	region = kbase_region_tracker_find_region_enclosing_address(kctx, gpu_addr);
2055 
2056 	if (!region) {
2057 		kbase_gpu_vm_unlock(kctx);
2058 		return -EINVAL;
2059 	}
2060 
2061 	*start = region->start_pfn << PAGE_SHIFT;
2062 
2063 	*offset = gpu_addr - *start;
2064 
2065 	if (((region->start_pfn + region->nr_pages) << PAGE_SHIFT) < (gpu_addr + size)) {
2066 		kbase_gpu_vm_unlock(kctx);
2067 		return -EINVAL;
2068 	}
2069 
2070 	kbase_gpu_vm_unlock(kctx);
2071 
2072 	return 0;
2073 }
2074 
2075 KBASE_EXPORT_TEST_API(kbasep_find_enclosing_gpu_mapping_start_and_offset);
2076 
kbase_sync_single(struct kbase_context * kctx,struct tagged_addr t_cpu_pa,struct tagged_addr t_gpu_pa,off_t offset,size_t size,enum kbase_sync_type sync_fn)2077 void kbase_sync_single(struct kbase_context *kctx,
2078 		struct tagged_addr t_cpu_pa, struct tagged_addr t_gpu_pa,
2079 		off_t offset, size_t size, enum kbase_sync_type sync_fn)
2080 {
2081 	struct page *cpu_page;
2082 	phys_addr_t cpu_pa = as_phys_addr_t(t_cpu_pa);
2083 	phys_addr_t gpu_pa = as_phys_addr_t(t_gpu_pa);
2084 
2085 	cpu_page = pfn_to_page(PFN_DOWN(cpu_pa));
2086 
2087 	if (likely(cpu_pa == gpu_pa)) {
2088 		dma_addr_t dma_addr;
2089 
2090 		BUG_ON(!cpu_page);
2091 		BUG_ON(offset + size > PAGE_SIZE);
2092 
2093 		dma_addr = kbase_dma_addr_from_tagged(t_cpu_pa) + offset;
2094 
2095 		if (sync_fn == KBASE_SYNC_TO_CPU)
2096 			dma_sync_single_for_cpu(kctx->kbdev->dev, dma_addr,
2097 					size, DMA_BIDIRECTIONAL);
2098 		else if (sync_fn == KBASE_SYNC_TO_DEVICE)
2099 			dma_sync_single_for_device(kctx->kbdev->dev, dma_addr,
2100 					size, DMA_BIDIRECTIONAL);
2101 	} else {
2102 		void *src = NULL;
2103 		void *dst = NULL;
2104 		struct page *gpu_page;
2105 		dma_addr_t dma_addr;
2106 
2107 		if (WARN(!gpu_pa, "No GPU PA found for infinite cache op"))
2108 			return;
2109 
2110 		gpu_page = pfn_to_page(PFN_DOWN(gpu_pa));
2111 		dma_addr = kbase_dma_addr_from_tagged(t_gpu_pa) + offset;
2112 
2113 		if (sync_fn == KBASE_SYNC_TO_DEVICE) {
2114 			src = ((unsigned char *)kmap(cpu_page)) + offset;
2115 			dst = ((unsigned char *)kmap(gpu_page)) + offset;
2116 		} else if (sync_fn == KBASE_SYNC_TO_CPU) {
2117 			dma_sync_single_for_cpu(kctx->kbdev->dev, dma_addr, size,
2118 						DMA_BIDIRECTIONAL);
2119 			src = ((unsigned char *)kmap(gpu_page)) + offset;
2120 			dst = ((unsigned char *)kmap(cpu_page)) + offset;
2121 		}
2122 
2123 		memcpy(dst, src, size);
2124 		kunmap(gpu_page);
2125 		kunmap(cpu_page);
2126 		if (sync_fn == KBASE_SYNC_TO_DEVICE)
2127 			dma_sync_single_for_device(kctx->kbdev->dev, dma_addr, size,
2128 						   DMA_BIDIRECTIONAL);
2129 	}
2130 }
2131 
kbase_do_syncset(struct kbase_context * kctx,struct basep_syncset * sset,enum kbase_sync_type sync_fn)2132 static int kbase_do_syncset(struct kbase_context *kctx,
2133 		struct basep_syncset *sset, enum kbase_sync_type sync_fn)
2134 {
2135 	int err = 0;
2136 	struct kbase_va_region *reg;
2137 	struct kbase_cpu_mapping *map;
2138 	unsigned long start;
2139 	size_t size;
2140 	struct tagged_addr *cpu_pa;
2141 	struct tagged_addr *gpu_pa;
2142 	u64 page_off, page_count;
2143 	u64 i;
2144 	u64 offset;
2145 
2146 	kbase_os_mem_map_lock(kctx);
2147 	kbase_gpu_vm_lock(kctx);
2148 
2149 	/* find the region where the virtual address is contained */
2150 	reg = kbase_region_tracker_find_region_enclosing_address(kctx,
2151 			sset->mem_handle.basep.handle);
2152 	if (kbase_is_region_invalid_or_free(reg)) {
2153 		dev_warn(kctx->kbdev->dev, "Can't find a valid region at VA 0x%016llX",
2154 				sset->mem_handle.basep.handle);
2155 		err = -EINVAL;
2156 		goto out_unlock;
2157 	}
2158 
2159 	/*
2160 	 * Handle imported memory before checking for KBASE_REG_CPU_CACHED. The
2161 	 * CPU mapping cacheability is defined by the owner of the imported
2162 	 * memory, and not by kbase, therefore we must assume that any imported
2163 	 * memory may be cached.
2164 	 */
2165 	if (kbase_mem_is_imported(reg->gpu_alloc->type)) {
2166 		err = kbase_mem_do_sync_imported(kctx, reg, sync_fn);
2167 		goto out_unlock;
2168 	}
2169 
2170 	if (!(reg->flags & KBASE_REG_CPU_CACHED))
2171 		goto out_unlock;
2172 
2173 	start = (uintptr_t)sset->user_addr;
2174 	size = (size_t)sset->size;
2175 
2176 	map = kbasep_find_enclosing_cpu_mapping(kctx, start, size, &offset);
2177 	if (!map) {
2178 		dev_warn(kctx->kbdev->dev, "Can't find CPU mapping 0x%016lX for VA 0x%016llX",
2179 				start, sset->mem_handle.basep.handle);
2180 		err = -EINVAL;
2181 		goto out_unlock;
2182 	}
2183 
2184 	page_off = offset >> PAGE_SHIFT;
2185 	offset &= ~PAGE_MASK;
2186 	page_count = (size + offset + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
2187 	cpu_pa = kbase_get_cpu_phy_pages(reg);
2188 	gpu_pa = kbase_get_gpu_phy_pages(reg);
2189 
2190 	if (page_off > reg->nr_pages ||
2191 			page_off + page_count > reg->nr_pages) {
2192 		/* Sync overflows the region */
2193 		err = -EINVAL;
2194 		goto out_unlock;
2195 	}
2196 
2197 	/* Sync first page */
2198 	if (as_phys_addr_t(cpu_pa[page_off])) {
2199 		size_t sz = MIN(((size_t) PAGE_SIZE - offset), size);
2200 
2201 		kbase_sync_single(kctx, cpu_pa[page_off], gpu_pa[page_off],
2202 				offset, sz, sync_fn);
2203 	}
2204 
2205 	/* Sync middle pages (if any) */
2206 	for (i = 1; page_count > 2 && i < page_count - 1; i++) {
2207 		/* we grow upwards, so bail on first non-present page */
2208 		if (!as_phys_addr_t(cpu_pa[page_off + i]))
2209 			break;
2210 
2211 		kbase_sync_single(kctx, cpu_pa[page_off + i],
2212 				gpu_pa[page_off + i], 0, PAGE_SIZE, sync_fn);
2213 	}
2214 
2215 	/* Sync last page (if any) */
2216 	if (page_count > 1 &&
2217 	    as_phys_addr_t(cpu_pa[page_off + page_count - 1])) {
2218 		size_t sz = ((start + size - 1) & ~PAGE_MASK) + 1;
2219 
2220 		kbase_sync_single(kctx, cpu_pa[page_off + page_count - 1],
2221 				gpu_pa[page_off + page_count - 1], 0, sz,
2222 				sync_fn);
2223 	}
2224 
2225 out_unlock:
2226 	kbase_gpu_vm_unlock(kctx);
2227 	kbase_os_mem_map_unlock(kctx);
2228 	return err;
2229 }
2230 
kbase_sync_now(struct kbase_context * kctx,struct basep_syncset * sset)2231 int kbase_sync_now(struct kbase_context *kctx, struct basep_syncset *sset)
2232 {
2233 	int err = -EINVAL;
2234 
2235 	KBASE_DEBUG_ASSERT(kctx != NULL);
2236 	KBASE_DEBUG_ASSERT(sset != NULL);
2237 
2238 	if (sset->mem_handle.basep.handle & ~PAGE_MASK) {
2239 		dev_warn(kctx->kbdev->dev,
2240 				"mem_handle: passed parameter is invalid");
2241 		return -EINVAL;
2242 	}
2243 
2244 	switch (sset->type) {
2245 	case BASE_SYNCSET_OP_MSYNC:
2246 		err = kbase_do_syncset(kctx, sset, KBASE_SYNC_TO_DEVICE);
2247 		break;
2248 
2249 	case BASE_SYNCSET_OP_CSYNC:
2250 		err = kbase_do_syncset(kctx, sset, KBASE_SYNC_TO_CPU);
2251 		break;
2252 
2253 	default:
2254 		dev_warn(kctx->kbdev->dev, "Unknown msync op %d\n", sset->type);
2255 		break;
2256 	}
2257 
2258 	return err;
2259 }
2260 
2261 KBASE_EXPORT_TEST_API(kbase_sync_now);
2262 
2263 /* vm lock must be held */
kbase_mem_free_region(struct kbase_context * kctx,struct kbase_va_region * reg)2264 int kbase_mem_free_region(struct kbase_context *kctx, struct kbase_va_region *reg)
2265 {
2266 	int err;
2267 
2268 	KBASE_DEBUG_ASSERT(kctx != NULL);
2269 	KBASE_DEBUG_ASSERT(reg != NULL);
2270 	dev_dbg(kctx->kbdev->dev, "%s %pK in kctx %pK\n",
2271 		__func__, (void *)reg, (void *)kctx);
2272 	lockdep_assert_held(&kctx->reg_lock);
2273 
2274 	if (kbase_va_region_is_no_user_free(reg)) {
2275 		dev_warn(kctx->kbdev->dev, "Attempt to free GPU memory whose freeing by user space is forbidden!\n");
2276 		return -EINVAL;
2277 	}
2278 
2279 	/* If a region has been made evictable then we must unmake it
2280 	 * before trying to free it.
2281 	 * If the memory hasn't been reclaimed it will be unmapped and freed
2282 	 * below, if it has been reclaimed then the operations below are no-ops.
2283 	 */
2284 	if (reg->flags & KBASE_REG_DONT_NEED) {
2285 		WARN_ON(reg->cpu_alloc->type != KBASE_MEM_TYPE_NATIVE);
2286 		mutex_lock(&kctx->jit_evict_lock);
2287 		/* Unlink the physical allocation before unmaking it evictable so
2288 		 * that the allocation isn't grown back to its last backed size
2289 		 * as we're going to unmap it anyway.
2290 		 */
2291 		reg->cpu_alloc->reg = NULL;
2292 		if (reg->cpu_alloc != reg->gpu_alloc)
2293 			reg->gpu_alloc->reg = NULL;
2294 		mutex_unlock(&kctx->jit_evict_lock);
2295 		kbase_mem_evictable_unmake(reg->gpu_alloc);
2296 	}
2297 
2298 	err = kbase_gpu_munmap(kctx, reg);
2299 	if (err) {
2300 		dev_warn(kctx->kbdev->dev, "Could not unmap from the GPU...\n");
2301 		goto out;
2302 	}
2303 
2304 #if MALI_USE_CSF
2305 	if (((reg->flags & KBASE_REG_ZONE_MASK) == KBASE_REG_ZONE_FIXED_VA) ||
2306 	    ((reg->flags & KBASE_REG_ZONE_MASK) == KBASE_REG_ZONE_EXEC_FIXED_VA)) {
2307 		if (reg->flags & KBASE_REG_FIXED_ADDRESS)
2308 			atomic64_dec(&kctx->num_fixed_allocs);
2309 		else
2310 			atomic64_dec(&kctx->num_fixable_allocs);
2311 	}
2312 #endif
2313 
2314 	/* This will also free the physical pages */
2315 	kbase_free_alloced_region(reg);
2316 
2317 out:
2318 	return err;
2319 }
2320 
2321 KBASE_EXPORT_TEST_API(kbase_mem_free_region);
2322 
2323 /**
2324  * kbase_mem_free - Free the region from the GPU and unregister it.
2325  *
2326  * @kctx:  KBase context
2327  * @gpu_addr: GPU address to free
2328  *
2329  * This function implements the free operation on a memory segment.
2330  * It will loudly fail if called with outstanding mappings.
2331  *
2332  * Return: 0 on success.
2333  */
kbase_mem_free(struct kbase_context * kctx,u64 gpu_addr)2334 int kbase_mem_free(struct kbase_context *kctx, u64 gpu_addr)
2335 {
2336 	int err = 0;
2337 	struct kbase_va_region *reg;
2338 
2339 	KBASE_DEBUG_ASSERT(kctx != NULL);
2340 	dev_dbg(kctx->kbdev->dev, "%s 0x%llx in kctx %pK\n",
2341 		__func__, gpu_addr, (void *)kctx);
2342 
2343 	if ((gpu_addr & ~PAGE_MASK) && (gpu_addr >= PAGE_SIZE)) {
2344 		dev_warn(kctx->kbdev->dev, "%s: gpu_addr parameter is invalid", __func__);
2345 		return -EINVAL;
2346 	}
2347 
2348 	if (gpu_addr == 0) {
2349 		dev_warn(kctx->kbdev->dev,
2350 			"gpu_addr 0 is reserved for the ringbuffer and it's an error to try to free it using %s\n",
2351 			__func__);
2352 		return -EINVAL;
2353 	}
2354 	kbase_gpu_vm_lock(kctx);
2355 
2356 	if (gpu_addr >= BASE_MEM_COOKIE_BASE &&
2357 	    gpu_addr < BASE_MEM_FIRST_FREE_ADDRESS) {
2358 		int cookie = PFN_DOWN(gpu_addr - BASE_MEM_COOKIE_BASE);
2359 
2360 		reg = kctx->pending_regions[cookie];
2361 		if (!reg) {
2362 			err = -EINVAL;
2363 			goto out_unlock;
2364 		}
2365 
2366 		/* ask to unlink the cookie as we'll free it */
2367 
2368 		kctx->pending_regions[cookie] = NULL;
2369 		bitmap_set(kctx->cookies, cookie, 1);
2370 
2371 		kbase_free_alloced_region(reg);
2372 	} else {
2373 		/* A real GPU va */
2374 		/* Validate the region */
2375 		reg = kbase_region_tracker_find_region_base_address(kctx, gpu_addr);
2376 		if (kbase_is_region_invalid_or_free(reg)) {
2377 			dev_warn(kctx->kbdev->dev, "%s called with nonexistent gpu_addr 0x%llX",
2378 				__func__, gpu_addr);
2379 			err = -EINVAL;
2380 			goto out_unlock;
2381 		}
2382 
2383 		if ((reg->flags & KBASE_REG_ZONE_MASK) == KBASE_REG_ZONE_SAME_VA) {
2384 			/* SAME_VA must be freed through munmap */
2385 			dev_warn(kctx->kbdev->dev, "%s called on SAME_VA memory 0x%llX", __func__,
2386 					gpu_addr);
2387 			err = -EINVAL;
2388 			goto out_unlock;
2389 		}
2390 		err = kbase_mem_free_region(kctx, reg);
2391 	}
2392 
2393 out_unlock:
2394 	kbase_gpu_vm_unlock(kctx);
2395 	return err;
2396 }
2397 
2398 KBASE_EXPORT_TEST_API(kbase_mem_free);
2399 
kbase_update_region_flags(struct kbase_context * kctx,struct kbase_va_region * reg,unsigned long flags)2400 int kbase_update_region_flags(struct kbase_context *kctx,
2401 		struct kbase_va_region *reg, unsigned long flags)
2402 {
2403 	KBASE_DEBUG_ASSERT(reg != NULL);
2404 	KBASE_DEBUG_ASSERT((flags & ~((1ul << BASE_MEM_FLAGS_NR_BITS) - 1)) == 0);
2405 
2406 	reg->flags |= kbase_cache_enabled(flags, reg->nr_pages);
2407 	/* all memory is now growable */
2408 	reg->flags |= KBASE_REG_GROWABLE;
2409 
2410 	if (flags & BASE_MEM_GROW_ON_GPF)
2411 		reg->flags |= KBASE_REG_PF_GROW;
2412 
2413 	if (flags & BASE_MEM_PROT_CPU_WR)
2414 		reg->flags |= KBASE_REG_CPU_WR;
2415 
2416 	if (flags & BASE_MEM_PROT_CPU_RD)
2417 		reg->flags |= KBASE_REG_CPU_RD;
2418 
2419 	if (flags & BASE_MEM_PROT_GPU_WR)
2420 		reg->flags |= KBASE_REG_GPU_WR;
2421 
2422 	if (flags & BASE_MEM_PROT_GPU_RD)
2423 		reg->flags |= KBASE_REG_GPU_RD;
2424 
2425 	if (0 == (flags & BASE_MEM_PROT_GPU_EX))
2426 		reg->flags |= KBASE_REG_GPU_NX;
2427 
2428 	if (!kbase_device_is_cpu_coherent(kctx->kbdev)) {
2429 		if (flags & BASE_MEM_COHERENT_SYSTEM_REQUIRED &&
2430 				!(flags & BASE_MEM_UNCACHED_GPU))
2431 			return -EINVAL;
2432 	} else if (flags & (BASE_MEM_COHERENT_SYSTEM |
2433 			BASE_MEM_COHERENT_SYSTEM_REQUIRED)) {
2434 		reg->flags |= KBASE_REG_SHARE_BOTH;
2435 	}
2436 
2437 	if (!(reg->flags & KBASE_REG_SHARE_BOTH) &&
2438 			flags & BASE_MEM_COHERENT_LOCAL) {
2439 		reg->flags |= KBASE_REG_SHARE_IN;
2440 	}
2441 
2442 #if !MALI_USE_CSF
2443 	if (flags & BASE_MEM_TILER_ALIGN_TOP)
2444 		reg->flags |= KBASE_REG_TILER_ALIGN_TOP;
2445 #endif /* !MALI_USE_CSF */
2446 
2447 #if MALI_USE_CSF
2448 	if (flags & BASE_MEM_CSF_EVENT) {
2449 		reg->flags |= KBASE_REG_CSF_EVENT;
2450 		reg->flags |= KBASE_REG_PERMANENT_KERNEL_MAPPING;
2451 
2452 		if (!(reg->flags & KBASE_REG_SHARE_BOTH)) {
2453 			/* On non coherent platforms need to map as uncached on
2454 			 * both sides.
2455 			 */
2456 			reg->flags &= ~KBASE_REG_CPU_CACHED;
2457 			reg->flags &= ~KBASE_REG_GPU_CACHED;
2458 		}
2459 	}
2460 #endif
2461 
2462 	/* Set up default MEMATTR usage */
2463 	if (!(reg->flags & KBASE_REG_GPU_CACHED)) {
2464 		if (kctx->kbdev->mmu_mode->flags &
2465 				KBASE_MMU_MODE_HAS_NON_CACHEABLE) {
2466 			/* Override shareability, and MEMATTR for uncached */
2467 			reg->flags &= ~(KBASE_REG_SHARE_IN | KBASE_REG_SHARE_BOTH);
2468 			reg->flags |= KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_NON_CACHEABLE);
2469 		} else {
2470 			dev_warn(kctx->kbdev->dev,
2471 				"Can't allocate GPU uncached memory due to MMU in Legacy Mode\n");
2472 			return -EINVAL;
2473 		}
2474 #if MALI_USE_CSF
2475 	} else if (reg->flags & KBASE_REG_CSF_EVENT) {
2476 		WARN_ON(!(reg->flags & KBASE_REG_SHARE_BOTH));
2477 
2478 		reg->flags |=
2479 			KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_SHARED);
2480 #endif
2481 	} else if (kctx->kbdev->system_coherency == COHERENCY_ACE &&
2482 		(reg->flags & KBASE_REG_SHARE_BOTH)) {
2483 		reg->flags |=
2484 			KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_DEFAULT_ACE);
2485 	} else {
2486 		reg->flags |=
2487 			KBASE_REG_MEMATTR_INDEX(AS_MEMATTR_INDEX_DEFAULT);
2488 	}
2489 
2490 	if (flags & BASEP_MEM_PERMANENT_KERNEL_MAPPING)
2491 		reg->flags |= KBASE_REG_PERMANENT_KERNEL_MAPPING;
2492 
2493 	if (flags & BASEP_MEM_NO_USER_FREE) {
2494 		kbase_gpu_vm_lock(kctx);
2495 		kbase_va_region_no_user_free_inc(reg);
2496 		kbase_gpu_vm_unlock(kctx);
2497 	}
2498 
2499 	if (flags & BASE_MEM_GPU_VA_SAME_4GB_PAGE)
2500 		reg->flags |= KBASE_REG_GPU_VA_SAME_4GB_PAGE;
2501 
2502 #if MALI_USE_CSF
2503 	if (flags & BASE_MEM_FIXED)
2504 		reg->flags |= KBASE_REG_FIXED_ADDRESS;
2505 #endif
2506 
2507 	return 0;
2508 }
2509 
kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc * alloc,size_t nr_pages_requested)2510 int kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc *alloc,
2511 		size_t nr_pages_requested)
2512 {
2513 	int new_page_count __maybe_unused;
2514 	size_t nr_left = nr_pages_requested;
2515 	int res;
2516 	struct kbase_context *kctx;
2517 	struct kbase_device *kbdev;
2518 	struct tagged_addr *tp;
2519 
2520 	if (WARN_ON(alloc->type != KBASE_MEM_TYPE_NATIVE) ||
2521 	    WARN_ON(alloc->imported.native.kctx == NULL) ||
2522 	    WARN_ON(alloc->group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS)) {
2523 		return -EINVAL;
2524 	}
2525 
2526 	if (alloc->reg) {
2527 		if (nr_pages_requested > alloc->reg->nr_pages - alloc->nents)
2528 			goto invalid_request;
2529 	}
2530 
2531 	kctx = alloc->imported.native.kctx;
2532 	kbdev = kctx->kbdev;
2533 
2534 	if (nr_pages_requested == 0)
2535 		goto done; /*nothing to do*/
2536 
2537 	new_page_count = atomic_add_return(
2538 		nr_pages_requested, &kctx->used_pages);
2539 	atomic_add(nr_pages_requested,
2540 		&kctx->kbdev->memdev.used_pages);
2541 
2542 	/* Increase mm counters before we allocate pages so that this
2543 	 * allocation is visible to the OOM killer
2544 	 */
2545 	kbase_process_page_usage_inc(kctx, nr_pages_requested);
2546 
2547 	tp = alloc->pages + alloc->nents;
2548 
2549 	/* Check if we have enough pages requested so we can allocate a large
2550 	 * page (512 * 4KB = 2MB )
2551 	 */
2552 	if (kbdev->pagesize_2mb && nr_left >= (SZ_2M / SZ_4K)) {
2553 		int nr_lp = nr_left / (SZ_2M / SZ_4K);
2554 
2555 		res = kbase_mem_pool_alloc_pages(&kctx->mem_pools.large[alloc->group_id],
2556 						 nr_lp * (SZ_2M / SZ_4K), tp, true, kctx->task);
2557 
2558 		if (res > 0) {
2559 			nr_left -= res;
2560 			tp += res;
2561 		}
2562 
2563 		if (nr_left) {
2564 			struct kbase_sub_alloc *sa, *temp_sa;
2565 
2566 			spin_lock(&kctx->mem_partials_lock);
2567 
2568 			list_for_each_entry_safe(sa, temp_sa,
2569 						 &kctx->mem_partials, link) {
2570 				int pidx = 0;
2571 
2572 				while (nr_left) {
2573 					pidx = find_next_zero_bit(sa->sub_pages,
2574 								  SZ_2M / SZ_4K,
2575 								  pidx);
2576 					bitmap_set(sa->sub_pages, pidx, 1);
2577 					*tp++ = as_tagged_tag(page_to_phys(sa->page +
2578 									   pidx),
2579 							      FROM_PARTIAL);
2580 					nr_left--;
2581 
2582 					if (bitmap_full(sa->sub_pages, SZ_2M / SZ_4K)) {
2583 						/* unlink from partial list when full */
2584 						list_del_init(&sa->link);
2585 						break;
2586 					}
2587 				}
2588 			}
2589 			spin_unlock(&kctx->mem_partials_lock);
2590 		}
2591 
2592 		/* only if we actually have a chunk left <512. If more it indicates
2593 		 * that we couldn't allocate a 2MB above, so no point to retry here.
2594 		 */
2595 		if (nr_left > 0 && nr_left < (SZ_2M / SZ_4K)) {
2596 			/* create a new partial and suballocate the rest from it */
2597 			struct page *np = NULL;
2598 
2599 			do {
2600 				int err;
2601 
2602 				np = kbase_mem_pool_alloc(
2603 					&kctx->mem_pools.large[
2604 						alloc->group_id]);
2605 				if (np)
2606 					break;
2607 
2608 				err = kbase_mem_pool_grow(
2609 					&kctx->mem_pools.large[alloc->group_id],
2610 					1, kctx->task);
2611 				if (err)
2612 					break;
2613 			} while (1);
2614 
2615 			if (np) {
2616 				int i;
2617 				struct kbase_sub_alloc *sa;
2618 				struct page *p;
2619 
2620 				sa = kmalloc(sizeof(*sa), GFP_KERNEL);
2621 				if (!sa) {
2622 					kbase_mem_pool_free(
2623 						&kctx->mem_pools.large[
2624 							alloc->group_id],
2625 						np,
2626 						false);
2627 					goto no_new_partial;
2628 				}
2629 
2630 				/* store pointers back to the control struct */
2631 				np->lru.next = (void *)sa;
2632 				for (p = np; p < np + SZ_2M / SZ_4K; p++)
2633 					p->lru.prev = (void *)np;
2634 				INIT_LIST_HEAD(&sa->link);
2635 				bitmap_zero(sa->sub_pages, SZ_2M / SZ_4K);
2636 				sa->page = np;
2637 
2638 				for (i = 0; i < nr_left; i++)
2639 					*tp++ = as_tagged_tag(page_to_phys(np + i), FROM_PARTIAL);
2640 
2641 				bitmap_set(sa->sub_pages, 0, nr_left);
2642 				nr_left = 0;
2643 
2644 				/* expose for later use */
2645 				spin_lock(&kctx->mem_partials_lock);
2646 				list_add(&sa->link, &kctx->mem_partials);
2647 				spin_unlock(&kctx->mem_partials_lock);
2648 			}
2649 		}
2650 	}
2651 
2652 no_new_partial:
2653 	if (nr_left) {
2654 		res = kbase_mem_pool_alloc_pages(&kctx->mem_pools.small[alloc->group_id], nr_left,
2655 						 tp, false, kctx->task);
2656 		if (res <= 0)
2657 			goto alloc_failed;
2658 	}
2659 
2660 	KBASE_TLSTREAM_AUX_PAGESALLOC(
2661 			kbdev,
2662 			kctx->id,
2663 			(u64)new_page_count);
2664 
2665 	alloc->nents += nr_pages_requested;
2666 
2667 	kbase_trace_gpu_mem_usage_inc(kctx->kbdev, kctx, nr_pages_requested);
2668 
2669 done:
2670 	return 0;
2671 
2672 alloc_failed:
2673 	/* rollback needed if got one or more 2MB but failed later */
2674 	if (nr_left != nr_pages_requested) {
2675 		size_t nr_pages_to_free = nr_pages_requested - nr_left;
2676 
2677 		alloc->nents += nr_pages_to_free;
2678 
2679 		kbase_process_page_usage_inc(kctx, nr_pages_to_free);
2680 		atomic_add(nr_pages_to_free, &kctx->used_pages);
2681 		atomic_add(nr_pages_to_free,
2682 			&kctx->kbdev->memdev.used_pages);
2683 
2684 		kbase_free_phy_pages_helper(alloc, nr_pages_to_free);
2685 	}
2686 
2687 	kbase_process_page_usage_dec(kctx, nr_pages_requested);
2688 	atomic_sub(nr_pages_requested, &kctx->used_pages);
2689 	atomic_sub(nr_pages_requested,
2690 		&kctx->kbdev->memdev.used_pages);
2691 
2692 invalid_request:
2693 	return -ENOMEM;
2694 }
2695 
kbase_alloc_phy_pages_helper_locked(struct kbase_mem_phy_alloc * alloc,struct kbase_mem_pool * pool,size_t nr_pages_requested,struct kbase_sub_alloc ** prealloc_sa)2696 struct tagged_addr *kbase_alloc_phy_pages_helper_locked(
2697 		struct kbase_mem_phy_alloc *alloc, struct kbase_mem_pool *pool,
2698 		size_t nr_pages_requested,
2699 		struct kbase_sub_alloc **prealloc_sa)
2700 {
2701 	int new_page_count __maybe_unused;
2702 	size_t nr_left = nr_pages_requested;
2703 	int res;
2704 	struct kbase_context *kctx;
2705 	struct kbase_device *kbdev;
2706 	struct tagged_addr *tp;
2707 	struct tagged_addr *new_pages = NULL;
2708 
2709 	KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_NATIVE);
2710 	KBASE_DEBUG_ASSERT(alloc->imported.native.kctx);
2711 
2712 	lockdep_assert_held(&pool->pool_lock);
2713 
2714 	kctx = alloc->imported.native.kctx;
2715 	kbdev = kctx->kbdev;
2716 
2717 	if (!kbdev->pagesize_2mb)
2718 		WARN_ON(pool->order);
2719 
2720 	if (alloc->reg) {
2721 		if (nr_pages_requested > alloc->reg->nr_pages - alloc->nents)
2722 			goto invalid_request;
2723 	}
2724 
2725 	lockdep_assert_held(&kctx->mem_partials_lock);
2726 
2727 	if (nr_pages_requested == 0)
2728 		goto done; /*nothing to do*/
2729 
2730 	new_page_count = atomic_add_return(
2731 		nr_pages_requested, &kctx->used_pages);
2732 	atomic_add(nr_pages_requested,
2733 		&kctx->kbdev->memdev.used_pages);
2734 
2735 	/* Increase mm counters before we allocate pages so that this
2736 	 * allocation is visible to the OOM killer
2737 	 */
2738 	kbase_process_page_usage_inc(kctx, nr_pages_requested);
2739 
2740 	tp = alloc->pages + alloc->nents;
2741 	new_pages = tp;
2742 
2743 	if (kbdev->pagesize_2mb && pool->order) {
2744 		int nr_lp = nr_left / (SZ_2M / SZ_4K);
2745 
2746 		res = kbase_mem_pool_alloc_pages_locked(pool,
2747 						 nr_lp * (SZ_2M / SZ_4K),
2748 						 tp);
2749 
2750 		if (res > 0) {
2751 			nr_left -= res;
2752 			tp += res;
2753 		}
2754 
2755 		if (nr_left) {
2756 			struct kbase_sub_alloc *sa, *temp_sa;
2757 
2758 			list_for_each_entry_safe(sa, temp_sa,
2759 						 &kctx->mem_partials, link) {
2760 				int pidx = 0;
2761 
2762 				while (nr_left) {
2763 					pidx = find_next_zero_bit(sa->sub_pages,
2764 								  SZ_2M / SZ_4K,
2765 								  pidx);
2766 					bitmap_set(sa->sub_pages, pidx, 1);
2767 					*tp++ = as_tagged_tag(page_to_phys(
2768 							sa->page + pidx),
2769 							FROM_PARTIAL);
2770 					nr_left--;
2771 
2772 					if (bitmap_full(sa->sub_pages,
2773 							SZ_2M / SZ_4K)) {
2774 						/* unlink from partial list when
2775 						 * full
2776 						 */
2777 						list_del_init(&sa->link);
2778 						break;
2779 					}
2780 				}
2781 			}
2782 		}
2783 
2784 		/* only if we actually have a chunk left <512. If more it
2785 		 * indicates that we couldn't allocate a 2MB above, so no point
2786 		 * to retry here.
2787 		 */
2788 		if (nr_left > 0 && nr_left < (SZ_2M / SZ_4K)) {
2789 			/* create a new partial and suballocate the rest from it
2790 			 */
2791 			struct page *np = NULL;
2792 
2793 			np = kbase_mem_pool_alloc_locked(pool);
2794 
2795 			if (np) {
2796 				int i;
2797 				struct kbase_sub_alloc *const sa = *prealloc_sa;
2798 				struct page *p;
2799 
2800 				/* store pointers back to the control struct */
2801 				np->lru.next = (void *)sa;
2802 				for (p = np; p < np + SZ_2M / SZ_4K; p++)
2803 					p->lru.prev = (void *)np;
2804 				INIT_LIST_HEAD(&sa->link);
2805 				bitmap_zero(sa->sub_pages, SZ_2M / SZ_4K);
2806 				sa->page = np;
2807 
2808 				for (i = 0; i < nr_left; i++)
2809 					*tp++ = as_tagged_tag(
2810 							page_to_phys(np + i),
2811 							FROM_PARTIAL);
2812 
2813 				bitmap_set(sa->sub_pages, 0, nr_left);
2814 				nr_left = 0;
2815 				/* Indicate to user that we'll free this memory
2816 				 * later.
2817 				 */
2818 				*prealloc_sa = NULL;
2819 
2820 				/* expose for later use */
2821 				list_add(&sa->link, &kctx->mem_partials);
2822 			}
2823 		}
2824 		if (nr_left)
2825 			goto alloc_failed;
2826 	} else {
2827 		res = kbase_mem_pool_alloc_pages_locked(pool,
2828 						 nr_left,
2829 						 tp);
2830 		if (res <= 0)
2831 			goto alloc_failed;
2832 	}
2833 
2834 	KBASE_TLSTREAM_AUX_PAGESALLOC(
2835 			kbdev,
2836 			kctx->id,
2837 			(u64)new_page_count);
2838 
2839 	alloc->nents += nr_pages_requested;
2840 
2841 	kbase_trace_gpu_mem_usage_inc(kctx->kbdev, kctx, nr_pages_requested);
2842 
2843 done:
2844 	return new_pages;
2845 
2846 alloc_failed:
2847 	/* rollback needed if got one or more 2MB but failed later */
2848 	if (nr_left != nr_pages_requested) {
2849 		size_t nr_pages_to_free = nr_pages_requested - nr_left;
2850 
2851 		struct tagged_addr *start_free = alloc->pages + alloc->nents;
2852 
2853 		if (kbdev->pagesize_2mb && pool->order) {
2854 			while (nr_pages_to_free) {
2855 				if (is_huge_head(*start_free)) {
2856 					kbase_mem_pool_free_pages_locked(
2857 						pool, 512,
2858 						start_free,
2859 						false, /* not dirty */
2860 						true); /* return to pool */
2861 					nr_pages_to_free -= 512;
2862 					start_free += 512;
2863 				} else if (is_partial(*start_free)) {
2864 					free_partial_locked(kctx, pool,
2865 							*start_free);
2866 					nr_pages_to_free--;
2867 					start_free++;
2868 				}
2869 			}
2870 		} else {
2871 			kbase_mem_pool_free_pages_locked(pool,
2872 					nr_pages_to_free,
2873 					start_free,
2874 					false, /* not dirty */
2875 					true); /* return to pool */
2876 		}
2877 	}
2878 
2879 	kbase_process_page_usage_dec(kctx, nr_pages_requested);
2880 	atomic_sub(nr_pages_requested, &kctx->used_pages);
2881 	atomic_sub(nr_pages_requested, &kctx->kbdev->memdev.used_pages);
2882 
2883 invalid_request:
2884 	return NULL;
2885 }
2886 
free_partial(struct kbase_context * kctx,int group_id,struct tagged_addr tp)2887 static void free_partial(struct kbase_context *kctx, int group_id, struct
2888 		tagged_addr tp)
2889 {
2890 	struct page *p, *head_page;
2891 	struct kbase_sub_alloc *sa;
2892 
2893 	p = as_page(tp);
2894 	head_page = (struct page *)p->lru.prev;
2895 	sa = (struct kbase_sub_alloc *)head_page->lru.next;
2896 	spin_lock(&kctx->mem_partials_lock);
2897 	clear_bit(p - head_page, sa->sub_pages);
2898 	if (bitmap_empty(sa->sub_pages, SZ_2M / SZ_4K)) {
2899 		list_del(&sa->link);
2900 		kbase_mem_pool_free(
2901 			&kctx->mem_pools.large[group_id],
2902 			head_page,
2903 			true);
2904 		kfree(sa);
2905 	} else if (bitmap_weight(sa->sub_pages, SZ_2M / SZ_4K) ==
2906 		   SZ_2M / SZ_4K - 1) {
2907 		/* expose the partial again */
2908 		list_add(&sa->link, &kctx->mem_partials);
2909 	}
2910 	spin_unlock(&kctx->mem_partials_lock);
2911 }
2912 
kbase_free_phy_pages_helper(struct kbase_mem_phy_alloc * alloc,size_t nr_pages_to_free)2913 int kbase_free_phy_pages_helper(
2914 	struct kbase_mem_phy_alloc *alloc,
2915 	size_t nr_pages_to_free)
2916 {
2917 	struct kbase_context *kctx = alloc->imported.native.kctx;
2918 	struct kbase_device *kbdev = kctx->kbdev;
2919 	bool syncback;
2920 	bool reclaimed = (alloc->evicted != 0);
2921 	struct tagged_addr *start_free;
2922 	int new_page_count __maybe_unused;
2923 	size_t freed = 0;
2924 
2925 	if (WARN_ON(alloc->type != KBASE_MEM_TYPE_NATIVE) ||
2926 	    WARN_ON(alloc->imported.native.kctx == NULL) ||
2927 	    WARN_ON(alloc->nents < nr_pages_to_free) ||
2928 	    WARN_ON(alloc->group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS)) {
2929 		return -EINVAL;
2930 	}
2931 
2932 	/* early out if nothing to do */
2933 	if (nr_pages_to_free == 0)
2934 		return 0;
2935 
2936 	start_free = alloc->pages + alloc->nents - nr_pages_to_free;
2937 
2938 	syncback = alloc->properties & KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED;
2939 
2940 	/* pad start_free to a valid start location */
2941 	while (nr_pages_to_free && is_huge(*start_free) &&
2942 	       !is_huge_head(*start_free)) {
2943 		nr_pages_to_free--;
2944 		start_free++;
2945 	}
2946 
2947 	while (nr_pages_to_free) {
2948 		if (is_huge_head(*start_free)) {
2949 			/* This is a 2MB entry, so free all the 512 pages that
2950 			 * it points to
2951 			 */
2952 			kbase_mem_pool_free_pages(
2953 				&kctx->mem_pools.large[alloc->group_id],
2954 				512,
2955 				start_free,
2956 				syncback,
2957 				reclaimed);
2958 			nr_pages_to_free -= 512;
2959 			start_free += 512;
2960 			freed += 512;
2961 		} else if (is_partial(*start_free)) {
2962 			free_partial(kctx, alloc->group_id, *start_free);
2963 			nr_pages_to_free--;
2964 			start_free++;
2965 			freed++;
2966 		} else {
2967 			struct tagged_addr *local_end_free;
2968 
2969 			local_end_free = start_free;
2970 			while (nr_pages_to_free &&
2971 				!is_huge(*local_end_free) &&
2972 				!is_partial(*local_end_free)) {
2973 				local_end_free++;
2974 				nr_pages_to_free--;
2975 			}
2976 			kbase_mem_pool_free_pages(
2977 				&kctx->mem_pools.small[alloc->group_id],
2978 				local_end_free - start_free,
2979 				start_free,
2980 				syncback,
2981 				reclaimed);
2982 			freed += local_end_free - start_free;
2983 			start_free += local_end_free - start_free;
2984 		}
2985 	}
2986 
2987 	alloc->nents -= freed;
2988 
2989 	/*
2990 	 * If the allocation was not evicted (i.e. evicted == 0) then
2991 	 * the page accounting needs to be done.
2992 	 */
2993 	if (!reclaimed) {
2994 		kbase_process_page_usage_dec(kctx, freed);
2995 		new_page_count = atomic_sub_return(freed,
2996 			&kctx->used_pages);
2997 		atomic_sub(freed,
2998 			&kctx->kbdev->memdev.used_pages);
2999 
3000 		KBASE_TLSTREAM_AUX_PAGESALLOC(
3001 			kbdev,
3002 			kctx->id,
3003 			(u64)new_page_count);
3004 
3005 		kbase_trace_gpu_mem_usage_dec(kctx->kbdev, kctx, freed);
3006 	}
3007 
3008 	return 0;
3009 }
3010 
free_partial_locked(struct kbase_context * kctx,struct kbase_mem_pool * pool,struct tagged_addr tp)3011 static void free_partial_locked(struct kbase_context *kctx,
3012 		struct kbase_mem_pool *pool, struct tagged_addr tp)
3013 {
3014 	struct page *p, *head_page;
3015 	struct kbase_sub_alloc *sa;
3016 
3017 	lockdep_assert_held(&pool->pool_lock);
3018 	lockdep_assert_held(&kctx->mem_partials_lock);
3019 
3020 	p = as_page(tp);
3021 	head_page = (struct page *)p->lru.prev;
3022 	sa = (struct kbase_sub_alloc *)head_page->lru.next;
3023 	clear_bit(p - head_page, sa->sub_pages);
3024 	if (bitmap_empty(sa->sub_pages, SZ_2M / SZ_4K)) {
3025 		list_del(&sa->link);
3026 		kbase_mem_pool_free_locked(pool, head_page, true);
3027 		kfree(sa);
3028 	} else if (bitmap_weight(sa->sub_pages, SZ_2M / SZ_4K) ==
3029 		   SZ_2M / SZ_4K - 1) {
3030 		/* expose the partial again */
3031 		list_add(&sa->link, &kctx->mem_partials);
3032 	}
3033 }
3034 
kbase_free_phy_pages_helper_locked(struct kbase_mem_phy_alloc * alloc,struct kbase_mem_pool * pool,struct tagged_addr * pages,size_t nr_pages_to_free)3035 void kbase_free_phy_pages_helper_locked(struct kbase_mem_phy_alloc *alloc,
3036 		struct kbase_mem_pool *pool, struct tagged_addr *pages,
3037 		size_t nr_pages_to_free)
3038 {
3039 	struct kbase_context *kctx = alloc->imported.native.kctx;
3040 	struct kbase_device *kbdev = kctx->kbdev;
3041 	bool syncback;
3042 	bool reclaimed = (alloc->evicted != 0);
3043 	struct tagged_addr *start_free;
3044 	size_t freed = 0;
3045 
3046 	KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_NATIVE);
3047 	KBASE_DEBUG_ASSERT(alloc->imported.native.kctx);
3048 	KBASE_DEBUG_ASSERT(alloc->nents >= nr_pages_to_free);
3049 
3050 	lockdep_assert_held(&pool->pool_lock);
3051 	lockdep_assert_held(&kctx->mem_partials_lock);
3052 
3053 	/* early out if nothing to do */
3054 	if (!nr_pages_to_free)
3055 		return;
3056 
3057 	start_free = pages;
3058 
3059 	syncback = alloc->properties & KBASE_MEM_PHY_ALLOC_ACCESSED_CACHED;
3060 
3061 	/* pad start_free to a valid start location */
3062 	while (nr_pages_to_free && is_huge(*start_free) &&
3063 	       !is_huge_head(*start_free)) {
3064 		nr_pages_to_free--;
3065 		start_free++;
3066 	}
3067 
3068 	while (nr_pages_to_free) {
3069 		if (is_huge_head(*start_free)) {
3070 			/* This is a 2MB entry, so free all the 512 pages that
3071 			 * it points to
3072 			 */
3073 			WARN_ON(!pool->order);
3074 			kbase_mem_pool_free_pages_locked(pool,
3075 					512,
3076 					start_free,
3077 					syncback,
3078 					reclaimed);
3079 			nr_pages_to_free -= 512;
3080 			start_free += 512;
3081 			freed += 512;
3082 		} else if (is_partial(*start_free)) {
3083 			WARN_ON(!pool->order);
3084 			free_partial_locked(kctx, pool, *start_free);
3085 			nr_pages_to_free--;
3086 			start_free++;
3087 			freed++;
3088 		} else {
3089 			struct tagged_addr *local_end_free;
3090 
3091 			WARN_ON(pool->order);
3092 			local_end_free = start_free;
3093 			while (nr_pages_to_free &&
3094 			       !is_huge(*local_end_free) &&
3095 			       !is_partial(*local_end_free)) {
3096 				local_end_free++;
3097 				nr_pages_to_free--;
3098 			}
3099 			kbase_mem_pool_free_pages_locked(pool,
3100 					local_end_free - start_free,
3101 					start_free,
3102 					syncback,
3103 					reclaimed);
3104 			freed += local_end_free - start_free;
3105 			start_free += local_end_free - start_free;
3106 		}
3107 	}
3108 
3109 	alloc->nents -= freed;
3110 
3111 	/*
3112 	 * If the allocation was not evicted (i.e. evicted == 0) then
3113 	 * the page accounting needs to be done.
3114 	 */
3115 	if (!reclaimed) {
3116 		int new_page_count;
3117 
3118 		kbase_process_page_usage_dec(kctx, freed);
3119 		new_page_count = atomic_sub_return(freed,
3120 			&kctx->used_pages);
3121 		atomic_sub(freed,
3122 			&kctx->kbdev->memdev.used_pages);
3123 
3124 		KBASE_TLSTREAM_AUX_PAGESALLOC(
3125 				kbdev,
3126 				kctx->id,
3127 				(u64)new_page_count);
3128 
3129 		kbase_trace_gpu_mem_usage_dec(kctx->kbdev, kctx, freed);
3130 	}
3131 }
3132 KBASE_EXPORT_TEST_API(kbase_free_phy_pages_helper_locked);
3133 
3134 #if MALI_USE_CSF
3135 /**
3136  * kbase_jd_user_buf_unpin_pages - Release the pinned pages of a user buffer.
3137  * @alloc: The allocation for the imported user buffer.
3138  *
3139  * This must only be called when terminating an alloc, when its refcount
3140  * (number of users) has become 0. This also ensures it is only called once all
3141  * CPU mappings have been closed.
3142  *
3143  * Instead call kbase_jd_user_buf_unmap() if you need to unpin pages on active
3144  * allocations
3145  */
3146 static void kbase_jd_user_buf_unpin_pages(struct kbase_mem_phy_alloc *alloc);
3147 #endif
3148 
kbase_mem_kref_free(struct kref * kref)3149 void kbase_mem_kref_free(struct kref *kref)
3150 {
3151 	struct kbase_mem_phy_alloc *alloc;
3152 
3153 	alloc = container_of(kref, struct kbase_mem_phy_alloc, kref);
3154 
3155 	switch (alloc->type) {
3156 	case KBASE_MEM_TYPE_NATIVE: {
3157 
3158 		if (!WARN_ON(!alloc->imported.native.kctx)) {
3159 			if (alloc->permanent_map)
3160 				kbase_phy_alloc_mapping_term(
3161 						alloc->imported.native.kctx,
3162 						alloc);
3163 
3164 			/*
3165 			 * The physical allocation must have been removed from
3166 			 * the eviction list before trying to free it.
3167 			 */
3168 			mutex_lock(
3169 				&alloc->imported.native.kctx->jit_evict_lock);
3170 			WARN_ON(!list_empty(&alloc->evict_node));
3171 			mutex_unlock(
3172 				&alloc->imported.native.kctx->jit_evict_lock);
3173 
3174 			kbase_process_page_usage_dec(
3175 					alloc->imported.native.kctx,
3176 					alloc->imported.native.nr_struct_pages);
3177 		}
3178 		kbase_free_phy_pages_helper(alloc, alloc->nents);
3179 		break;
3180 	}
3181 	case KBASE_MEM_TYPE_ALIAS: {
3182 		/* just call put on the underlying phy allocs */
3183 		size_t i;
3184 		struct kbase_aliased *aliased;
3185 
3186 		aliased = alloc->imported.alias.aliased;
3187 		if (aliased) {
3188 			for (i = 0; i < alloc->imported.alias.nents; i++)
3189 				if (aliased[i].alloc) {
3190 					kbase_mem_phy_alloc_gpu_unmapped(aliased[i].alloc);
3191 					kbase_mem_phy_alloc_put(aliased[i].alloc);
3192 				}
3193 			vfree(aliased);
3194 		}
3195 		break;
3196 	}
3197 	case KBASE_MEM_TYPE_RAW:
3198 		/* raw pages, external cleanup */
3199 		break;
3200 	case KBASE_MEM_TYPE_IMPORTED_UMM:
3201 		if (!IS_ENABLED(CONFIG_MALI_DMA_BUF_MAP_ON_DEMAND)) {
3202 			WARN_ONCE(alloc->imported.umm.current_mapping_usage_count != 1,
3203 					"WARNING: expected excatly 1 mapping, got %d",
3204 					alloc->imported.umm.current_mapping_usage_count);
3205 			dma_buf_unmap_attachment(
3206 					alloc->imported.umm.dma_attachment,
3207 					alloc->imported.umm.sgt,
3208 					DMA_BIDIRECTIONAL);
3209 			kbase_remove_dma_buf_usage(alloc->imported.umm.kctx,
3210 						   alloc);
3211 		}
3212 		dma_buf_detach(alloc->imported.umm.dma_buf,
3213 			       alloc->imported.umm.dma_attachment);
3214 		dma_buf_put(alloc->imported.umm.dma_buf);
3215 		break;
3216 	case KBASE_MEM_TYPE_IMPORTED_USER_BUF:
3217 #if MALI_USE_CSF
3218 		kbase_jd_user_buf_unpin_pages(alloc);
3219 #endif
3220 		if (alloc->imported.user_buf.mm)
3221 			mmdrop(alloc->imported.user_buf.mm);
3222 		if (alloc->properties & KBASE_MEM_PHY_ALLOC_LARGE)
3223 			vfree(alloc->imported.user_buf.pages);
3224 		else
3225 			kfree(alloc->imported.user_buf.pages);
3226 		break;
3227 	default:
3228 		WARN(1, "Unexecpted free of type %d\n", alloc->type);
3229 		break;
3230 	}
3231 
3232 	/* Free based on allocation type */
3233 	if (alloc->properties & KBASE_MEM_PHY_ALLOC_LARGE)
3234 		vfree(alloc);
3235 	else
3236 		kfree(alloc);
3237 }
3238 
3239 KBASE_EXPORT_TEST_API(kbase_mem_kref_free);
3240 
kbase_alloc_phy_pages(struct kbase_va_region * reg,size_t vsize,size_t size)3241 int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size)
3242 {
3243 	KBASE_DEBUG_ASSERT(reg != NULL);
3244 	KBASE_DEBUG_ASSERT(vsize > 0);
3245 
3246 	/* validate user provided arguments */
3247 	if (size > vsize || vsize > reg->nr_pages)
3248 		goto out_term;
3249 
3250 	/* Prevent vsize*sizeof from wrapping around.
3251 	 * For instance, if vsize is 2**29+1, we'll allocate 1 byte and the alloc won't fail.
3252 	 */
3253 	if ((size_t) vsize > ((size_t) -1 / sizeof(*reg->cpu_alloc->pages)))
3254 		goto out_term;
3255 
3256 	KBASE_DEBUG_ASSERT(vsize != 0);
3257 
3258 	if (kbase_alloc_phy_pages_helper(reg->cpu_alloc, size) != 0)
3259 		goto out_term;
3260 
3261 	reg->cpu_alloc->reg = reg;
3262 	if (reg->cpu_alloc != reg->gpu_alloc) {
3263 		if (kbase_alloc_phy_pages_helper(reg->gpu_alloc, size) != 0)
3264 			goto out_rollback;
3265 		reg->gpu_alloc->reg = reg;
3266 	}
3267 
3268 	return 0;
3269 
3270 out_rollback:
3271 	kbase_free_phy_pages_helper(reg->cpu_alloc, size);
3272 out_term:
3273 	return -1;
3274 }
3275 KBASE_EXPORT_TEST_API(kbase_alloc_phy_pages);
3276 
kbase_set_phy_alloc_page_status(struct kbase_mem_phy_alloc * alloc,enum kbase_page_status status)3277 void kbase_set_phy_alloc_page_status(struct kbase_mem_phy_alloc *alloc,
3278 				     enum kbase_page_status status)
3279 {
3280 	u32 i = 0;
3281 
3282 	for (; i < alloc->nents; i++) {
3283 		struct tagged_addr phys = alloc->pages[i];
3284 		struct kbase_page_metadata *page_md = kbase_page_private(as_page(phys));
3285 
3286 		/* Skip the 4KB page that is part of a large page, as the large page is
3287 		 * excluded from the migration process.
3288 		 */
3289 		if (is_huge(phys) || is_partial(phys))
3290 			continue;
3291 
3292 		if (!page_md)
3293 			continue;
3294 
3295 		spin_lock(&page_md->migrate_lock);
3296 		page_md->status = PAGE_STATUS_SET(page_md->status, (u8)status);
3297 		spin_unlock(&page_md->migrate_lock);
3298 	}
3299 }
3300 
kbase_check_alloc_flags(unsigned long flags)3301 bool kbase_check_alloc_flags(unsigned long flags)
3302 {
3303 	/* Only known input flags should be set. */
3304 	if (flags & ~BASE_MEM_FLAGS_INPUT_MASK)
3305 		return false;
3306 
3307 	/* At least one flag should be set */
3308 	if (flags == 0)
3309 		return false;
3310 
3311 	/* Either the GPU or CPU must be reading from the allocated memory */
3312 	if ((flags & (BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_GPU_RD)) == 0)
3313 		return false;
3314 
3315 	/* Either the GPU or CPU must be writing to the allocated memory */
3316 	if ((flags & (BASE_MEM_PROT_CPU_WR | BASE_MEM_PROT_GPU_WR)) == 0)
3317 		return false;
3318 
3319 	/* GPU executable memory cannot:
3320 	 * - Be written by the GPU
3321 	 * - Be grown on GPU page fault
3322 	 */
3323 	if ((flags & BASE_MEM_PROT_GPU_EX) && (flags &
3324 			(BASE_MEM_PROT_GPU_WR | BASE_MEM_GROW_ON_GPF)))
3325 		return false;
3326 
3327 #if !MALI_USE_CSF
3328 	/* GPU executable memory also cannot have the top of its initial
3329 	 * commit aligned to 'extension'
3330 	 */
3331 	if ((flags & BASE_MEM_PROT_GPU_EX) && (flags &
3332 			BASE_MEM_TILER_ALIGN_TOP))
3333 		return false;
3334 #endif /* !MALI_USE_CSF */
3335 
3336 	/* To have an allocation lie within a 4GB chunk is required only for
3337 	 * TLS memory, which will never be used to contain executable code.
3338 	 */
3339 	if ((flags & BASE_MEM_GPU_VA_SAME_4GB_PAGE) && (flags &
3340 			BASE_MEM_PROT_GPU_EX))
3341 		return false;
3342 
3343 #if !MALI_USE_CSF
3344 	/* TLS memory should also not be used for tiler heap */
3345 	if ((flags & BASE_MEM_GPU_VA_SAME_4GB_PAGE) && (flags &
3346 			BASE_MEM_TILER_ALIGN_TOP))
3347 		return false;
3348 #endif /* !MALI_USE_CSF */
3349 
3350 	/* GPU should have at least read or write access otherwise there is no
3351 	 * reason for allocating.
3352 	 */
3353 	if ((flags & (BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR)) == 0)
3354 		return false;
3355 
3356 	/* BASE_MEM_IMPORT_SHARED is only valid for imported memory */
3357 	if ((flags & BASE_MEM_IMPORT_SHARED) == BASE_MEM_IMPORT_SHARED)
3358 		return false;
3359 
3360 	/* BASE_MEM_IMPORT_SYNC_ON_MAP_UNMAP is only valid for imported memory
3361 	 */
3362 	if ((flags & BASE_MEM_IMPORT_SYNC_ON_MAP_UNMAP) ==
3363 			BASE_MEM_IMPORT_SYNC_ON_MAP_UNMAP)
3364 		return false;
3365 
3366 	/* Should not combine BASE_MEM_COHERENT_LOCAL with
3367 	 * BASE_MEM_COHERENT_SYSTEM
3368 	 */
3369 	if ((flags & (BASE_MEM_COHERENT_LOCAL | BASE_MEM_COHERENT_SYSTEM)) ==
3370 			(BASE_MEM_COHERENT_LOCAL | BASE_MEM_COHERENT_SYSTEM))
3371 		return false;
3372 
3373 #if MALI_USE_CSF
3374 	if ((flags & BASE_MEM_SAME_VA) && (flags & (BASE_MEM_FIXABLE | BASE_MEM_FIXED)))
3375 		return false;
3376 
3377 	if ((flags & BASE_MEM_FIXABLE) && (flags & BASE_MEM_FIXED))
3378 		return false;
3379 #endif
3380 
3381 	return true;
3382 }
3383 
kbase_check_import_flags(unsigned long flags)3384 bool kbase_check_import_flags(unsigned long flags)
3385 {
3386 	/* Only known input flags should be set. */
3387 	if (flags & ~BASE_MEM_FLAGS_INPUT_MASK)
3388 		return false;
3389 
3390 	/* At least one flag should be set */
3391 	if (flags == 0)
3392 		return false;
3393 
3394 	/* Imported memory cannot be GPU executable */
3395 	if (flags & BASE_MEM_PROT_GPU_EX)
3396 		return false;
3397 
3398 	/* Imported memory cannot grow on page fault */
3399 	if (flags & BASE_MEM_GROW_ON_GPF)
3400 		return false;
3401 
3402 #if MALI_USE_CSF
3403 	/* Imported memory cannot be fixed */
3404 	if ((flags & (BASE_MEM_FIXED | BASE_MEM_FIXABLE)))
3405 		return false;
3406 #else
3407 	/* Imported memory cannot be aligned to the end of its initial commit */
3408 	if (flags & BASE_MEM_TILER_ALIGN_TOP)
3409 		return false;
3410 #endif /* !MALI_USE_CSF */
3411 
3412 	/* GPU should have at least read or write access otherwise there is no
3413 	 * reason for importing.
3414 	 */
3415 	if ((flags & (BASE_MEM_PROT_GPU_RD | BASE_MEM_PROT_GPU_WR)) == 0)
3416 		return false;
3417 
3418 	/* Protected memory cannot be read by the CPU */
3419 	if ((flags & BASE_MEM_PROTECTED) && (flags & BASE_MEM_PROT_CPU_RD))
3420 		return false;
3421 
3422 	return true;
3423 }
3424 
kbase_check_alloc_sizes(struct kbase_context * kctx,unsigned long flags,u64 va_pages,u64 commit_pages,u64 large_extension)3425 int kbase_check_alloc_sizes(struct kbase_context *kctx, unsigned long flags,
3426 			    u64 va_pages, u64 commit_pages, u64 large_extension)
3427 {
3428 	struct device *dev = kctx->kbdev->dev;
3429 	int gpu_pc_bits = kctx->kbdev->gpu_props.props.core_props.log2_program_counter_size;
3430 	u64 gpu_pc_pages_max = 1ULL << gpu_pc_bits >> PAGE_SHIFT;
3431 	struct kbase_va_region test_reg;
3432 
3433 	/* kbase_va_region's extension member can be of variable size, so check against that type */
3434 	test_reg.extension = large_extension;
3435 
3436 #define KBASE_MSG_PRE "GPU allocation attempted with "
3437 
3438 	if (va_pages == 0) {
3439 		dev_warn(dev, KBASE_MSG_PRE "0 va_pages!");
3440 		return -EINVAL;
3441 	}
3442 
3443 	if (va_pages > KBASE_MEM_ALLOC_MAX_SIZE) {
3444 		dev_warn(dev, KBASE_MSG_PRE "va_pages==%lld larger than KBASE_MEM_ALLOC_MAX_SIZE!",
3445 				(unsigned long long)va_pages);
3446 		return -ENOMEM;
3447 	}
3448 
3449 	/* Note: commit_pages is checked against va_pages during
3450 	 * kbase_alloc_phy_pages()
3451 	 */
3452 
3453 	/* Limit GPU executable allocs to GPU PC size */
3454 	if ((flags & BASE_MEM_PROT_GPU_EX) && (va_pages > gpu_pc_pages_max)) {
3455 		dev_warn(dev, KBASE_MSG_PRE "BASE_MEM_PROT_GPU_EX and va_pages==%lld larger than GPU PC range %lld",
3456 				(unsigned long long)va_pages,
3457 				(unsigned long long)gpu_pc_pages_max);
3458 
3459 		return -EINVAL;
3460 	}
3461 
3462 	if ((flags & BASE_MEM_GROW_ON_GPF) && (test_reg.extension == 0)) {
3463 		dev_warn(dev, KBASE_MSG_PRE
3464 			 "BASE_MEM_GROW_ON_GPF but extension == 0\n");
3465 		return -EINVAL;
3466 	}
3467 
3468 #if !MALI_USE_CSF
3469 	if ((flags & BASE_MEM_TILER_ALIGN_TOP) && (test_reg.extension == 0)) {
3470 		dev_warn(dev, KBASE_MSG_PRE
3471 			 "BASE_MEM_TILER_ALIGN_TOP but extension == 0\n");
3472 		return -EINVAL;
3473 	}
3474 
3475 	if (!(flags & (BASE_MEM_GROW_ON_GPF | BASE_MEM_TILER_ALIGN_TOP)) &&
3476 	    test_reg.extension != 0) {
3477 		dev_warn(
3478 			dev, KBASE_MSG_PRE
3479 			"neither BASE_MEM_GROW_ON_GPF nor BASE_MEM_TILER_ALIGN_TOP set but extension != 0\n");
3480 		return -EINVAL;
3481 	}
3482 #else
3483 	if (!(flags & BASE_MEM_GROW_ON_GPF) && test_reg.extension != 0) {
3484 		dev_warn(dev, KBASE_MSG_PRE
3485 			 "BASE_MEM_GROW_ON_GPF not set but extension != 0\n");
3486 		return -EINVAL;
3487 	}
3488 #endif /* !MALI_USE_CSF */
3489 
3490 #if !MALI_USE_CSF
3491 	/* BASE_MEM_TILER_ALIGN_TOP memory has a number of restrictions */
3492 	if (flags & BASE_MEM_TILER_ALIGN_TOP) {
3493 #define KBASE_MSG_PRE_FLAG KBASE_MSG_PRE "BASE_MEM_TILER_ALIGN_TOP and "
3494 		unsigned long small_extension;
3495 
3496 		if (large_extension >
3497 		    BASE_MEM_TILER_ALIGN_TOP_EXTENSION_MAX_PAGES) {
3498 			dev_warn(dev,
3499 				 KBASE_MSG_PRE_FLAG
3500 				 "extension==%lld pages exceeds limit %lld",
3501 				 (unsigned long long)large_extension,
3502 				 BASE_MEM_TILER_ALIGN_TOP_EXTENSION_MAX_PAGES);
3503 			return -EINVAL;
3504 		}
3505 		/* For use with is_power_of_2, which takes unsigned long, so
3506 		 * must ensure e.g. on 32-bit kernel it'll fit in that type
3507 		 */
3508 		small_extension = (unsigned long)large_extension;
3509 
3510 		if (!is_power_of_2(small_extension)) {
3511 			dev_warn(dev,
3512 				 KBASE_MSG_PRE_FLAG
3513 				 "extension==%ld not a non-zero power of 2",
3514 				 small_extension);
3515 			return -EINVAL;
3516 		}
3517 
3518 		if (commit_pages > large_extension) {
3519 			dev_warn(dev,
3520 				 KBASE_MSG_PRE_FLAG
3521 				 "commit_pages==%ld exceeds extension==%ld",
3522 				 (unsigned long)commit_pages,
3523 				 (unsigned long)large_extension);
3524 			return -EINVAL;
3525 		}
3526 #undef KBASE_MSG_PRE_FLAG
3527 	}
3528 #endif /* !MALI_USE_CSF */
3529 
3530 	if ((flags & BASE_MEM_GPU_VA_SAME_4GB_PAGE) &&
3531 	    (va_pages > (BASE_MEM_PFN_MASK_4GB + 1))) {
3532 		dev_warn(dev, KBASE_MSG_PRE "BASE_MEM_GPU_VA_SAME_4GB_PAGE and va_pages==%lld greater than that needed for 4GB space",
3533 				(unsigned long long)va_pages);
3534 		return -EINVAL;
3535 	}
3536 
3537 	return 0;
3538 #undef KBASE_MSG_PRE
3539 }
3540 
kbase_gpu_vm_lock(struct kbase_context * kctx)3541 void kbase_gpu_vm_lock(struct kbase_context *kctx)
3542 {
3543 	KBASE_DEBUG_ASSERT(kctx != NULL);
3544 	mutex_lock(&kctx->reg_lock);
3545 }
3546 
3547 KBASE_EXPORT_TEST_API(kbase_gpu_vm_lock);
3548 
kbase_gpu_vm_unlock(struct kbase_context * kctx)3549 void kbase_gpu_vm_unlock(struct kbase_context *kctx)
3550 {
3551 	KBASE_DEBUG_ASSERT(kctx != NULL);
3552 	mutex_unlock(&kctx->reg_lock);
3553 }
3554 
3555 KBASE_EXPORT_TEST_API(kbase_gpu_vm_unlock);
3556 
3557 #if IS_ENABLED(CONFIG_DEBUG_FS)
3558 struct kbase_jit_debugfs_data {
3559 	int (*func)(struct kbase_jit_debugfs_data *data);
3560 	struct mutex lock;
3561 	struct kbase_context *kctx;
3562 	u64 active_value;
3563 	u64 pool_value;
3564 	u64 destroy_value;
3565 	char buffer[50];
3566 };
3567 
kbase_jit_debugfs_common_open(struct inode * inode,struct file * file,int (* func)(struct kbase_jit_debugfs_data *))3568 static int kbase_jit_debugfs_common_open(struct inode *inode,
3569 		struct file *file, int (*func)(struct kbase_jit_debugfs_data *))
3570 {
3571 	struct kbase_jit_debugfs_data *data;
3572 
3573 	data = kzalloc(sizeof(*data), GFP_KERNEL);
3574 	if (!data)
3575 		return -ENOMEM;
3576 
3577 	data->func = func;
3578 	mutex_init(&data->lock);
3579 	data->kctx = (struct kbase_context *) inode->i_private;
3580 
3581 	file->private_data = data;
3582 
3583 	return nonseekable_open(inode, file);
3584 }
3585 
kbase_jit_debugfs_common_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)3586 static ssize_t kbase_jit_debugfs_common_read(struct file *file,
3587 		char __user *buf, size_t len, loff_t *ppos)
3588 {
3589 	struct kbase_jit_debugfs_data *data;
3590 	size_t size;
3591 	int ret;
3592 
3593 	data = (struct kbase_jit_debugfs_data *) file->private_data;
3594 	mutex_lock(&data->lock);
3595 
3596 	if (*ppos) {
3597 		size = strnlen(data->buffer, sizeof(data->buffer));
3598 	} else {
3599 		if (!data->func) {
3600 			ret = -EACCES;
3601 			goto out_unlock;
3602 		}
3603 
3604 		if (data->func(data)) {
3605 			ret = -EACCES;
3606 			goto out_unlock;
3607 		}
3608 
3609 		size = scnprintf(data->buffer, sizeof(data->buffer),
3610 				"%llu,%llu,%llu\n", data->active_value,
3611 				data->pool_value, data->destroy_value);
3612 	}
3613 
3614 	ret = simple_read_from_buffer(buf, len, ppos, data->buffer, size);
3615 
3616 out_unlock:
3617 	mutex_unlock(&data->lock);
3618 	return ret;
3619 }
3620 
kbase_jit_debugfs_common_release(struct inode * inode,struct file * file)3621 static int kbase_jit_debugfs_common_release(struct inode *inode,
3622 		struct file *file)
3623 {
3624 	kfree(file->private_data);
3625 	return 0;
3626 }
3627 
3628 #define KBASE_JIT_DEBUGFS_DECLARE(__fops, __func) \
3629 static int __fops ## _open(struct inode *inode, struct file *file) \
3630 { \
3631 	return kbase_jit_debugfs_common_open(inode, file, __func); \
3632 } \
3633 static const struct file_operations __fops = { \
3634 	.owner = THIS_MODULE, \
3635 	.open = __fops ## _open, \
3636 	.release = kbase_jit_debugfs_common_release, \
3637 	.read = kbase_jit_debugfs_common_read, \
3638 	.write = NULL, \
3639 	.llseek = generic_file_llseek, \
3640 }
3641 
kbase_jit_debugfs_count_get(struct kbase_jit_debugfs_data * data)3642 static int kbase_jit_debugfs_count_get(struct kbase_jit_debugfs_data *data)
3643 {
3644 	struct kbase_context *kctx = data->kctx;
3645 	struct list_head *tmp;
3646 
3647 	mutex_lock(&kctx->jit_evict_lock);
3648 	list_for_each(tmp, &kctx->jit_active_head) {
3649 		data->active_value++;
3650 	}
3651 
3652 	list_for_each(tmp, &kctx->jit_pool_head) {
3653 		data->pool_value++;
3654 	}
3655 
3656 	list_for_each(tmp, &kctx->jit_destroy_head) {
3657 		data->destroy_value++;
3658 	}
3659 	mutex_unlock(&kctx->jit_evict_lock);
3660 
3661 	return 0;
3662 }
3663 KBASE_JIT_DEBUGFS_DECLARE(kbase_jit_debugfs_count_fops,
3664 		kbase_jit_debugfs_count_get);
3665 
kbase_jit_debugfs_vm_get(struct kbase_jit_debugfs_data * data)3666 static int kbase_jit_debugfs_vm_get(struct kbase_jit_debugfs_data *data)
3667 {
3668 	struct kbase_context *kctx = data->kctx;
3669 	struct kbase_va_region *reg;
3670 
3671 	mutex_lock(&kctx->jit_evict_lock);
3672 	list_for_each_entry(reg, &kctx->jit_active_head, jit_node) {
3673 		data->active_value += reg->nr_pages;
3674 	}
3675 
3676 	list_for_each_entry(reg, &kctx->jit_pool_head, jit_node) {
3677 		data->pool_value += reg->nr_pages;
3678 	}
3679 
3680 	list_for_each_entry(reg, &kctx->jit_destroy_head, jit_node) {
3681 		data->destroy_value += reg->nr_pages;
3682 	}
3683 	mutex_unlock(&kctx->jit_evict_lock);
3684 
3685 	return 0;
3686 }
3687 KBASE_JIT_DEBUGFS_DECLARE(kbase_jit_debugfs_vm_fops,
3688 		kbase_jit_debugfs_vm_get);
3689 
kbase_jit_debugfs_phys_get(struct kbase_jit_debugfs_data * data)3690 static int kbase_jit_debugfs_phys_get(struct kbase_jit_debugfs_data *data)
3691 {
3692 	struct kbase_context *kctx = data->kctx;
3693 	struct kbase_va_region *reg;
3694 
3695 	mutex_lock(&kctx->jit_evict_lock);
3696 	list_for_each_entry(reg, &kctx->jit_active_head, jit_node) {
3697 		data->active_value += reg->gpu_alloc->nents;
3698 	}
3699 
3700 	list_for_each_entry(reg, &kctx->jit_pool_head, jit_node) {
3701 		data->pool_value += reg->gpu_alloc->nents;
3702 	}
3703 
3704 	list_for_each_entry(reg, &kctx->jit_destroy_head, jit_node) {
3705 		data->destroy_value += reg->gpu_alloc->nents;
3706 	}
3707 	mutex_unlock(&kctx->jit_evict_lock);
3708 
3709 	return 0;
3710 }
3711 KBASE_JIT_DEBUGFS_DECLARE(kbase_jit_debugfs_phys_fops,
3712 		kbase_jit_debugfs_phys_get);
3713 
3714 #if MALI_JIT_PRESSURE_LIMIT_BASE
kbase_jit_debugfs_used_get(struct kbase_jit_debugfs_data * data)3715 static int kbase_jit_debugfs_used_get(struct kbase_jit_debugfs_data *data)
3716 {
3717 	struct kbase_context *kctx = data->kctx;
3718 	struct kbase_va_region *reg;
3719 
3720 #if !MALI_USE_CSF
3721 	mutex_lock(&kctx->jctx.lock);
3722 #endif /* !MALI_USE_CSF */
3723 	mutex_lock(&kctx->jit_evict_lock);
3724 	list_for_each_entry(reg, &kctx->jit_active_head, jit_node) {
3725 		data->active_value += reg->used_pages;
3726 	}
3727 	mutex_unlock(&kctx->jit_evict_lock);
3728 #if !MALI_USE_CSF
3729 	mutex_unlock(&kctx->jctx.lock);
3730 #endif /* !MALI_USE_CSF */
3731 
3732 	return 0;
3733 }
3734 
3735 KBASE_JIT_DEBUGFS_DECLARE(kbase_jit_debugfs_used_fops,
3736 		kbase_jit_debugfs_used_get);
3737 
3738 static int kbase_mem_jit_trim_pages_from_region(struct kbase_context *kctx,
3739 		struct kbase_va_region *reg, size_t pages_needed,
3740 		size_t *freed, bool shrink);
3741 
kbase_jit_debugfs_trim_get(struct kbase_jit_debugfs_data * data)3742 static int kbase_jit_debugfs_trim_get(struct kbase_jit_debugfs_data *data)
3743 {
3744 	struct kbase_context *kctx = data->kctx;
3745 	struct kbase_va_region *reg;
3746 
3747 #if !MALI_USE_CSF
3748 	mutex_lock(&kctx->jctx.lock);
3749 #endif /* !MALI_USE_CSF */
3750 	kbase_gpu_vm_lock(kctx);
3751 	mutex_lock(&kctx->jit_evict_lock);
3752 	list_for_each_entry(reg, &kctx->jit_active_head, jit_node) {
3753 		int err;
3754 		size_t freed = 0u;
3755 
3756 		err = kbase_mem_jit_trim_pages_from_region(kctx, reg,
3757 				SIZE_MAX, &freed, false);
3758 
3759 		if (err) {
3760 			/* Failed to calculate, try the next region */
3761 			continue;
3762 		}
3763 
3764 		data->active_value += freed;
3765 	}
3766 	mutex_unlock(&kctx->jit_evict_lock);
3767 	kbase_gpu_vm_unlock(kctx);
3768 #if !MALI_USE_CSF
3769 	mutex_unlock(&kctx->jctx.lock);
3770 #endif /* !MALI_USE_CSF */
3771 
3772 	return 0;
3773 }
3774 
3775 KBASE_JIT_DEBUGFS_DECLARE(kbase_jit_debugfs_trim_fops,
3776 		kbase_jit_debugfs_trim_get);
3777 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
3778 
kbase_jit_debugfs_init(struct kbase_context * kctx)3779 void kbase_jit_debugfs_init(struct kbase_context *kctx)
3780 {
3781 	/* prevent unprivileged use of debug file system
3782 	 * in old kernel version
3783 	 */
3784 	const mode_t mode = 0444;
3785 
3786 	/* Caller already ensures this, but we keep the pattern for
3787 	 * maintenance safety.
3788 	 */
3789 	if (WARN_ON(!kctx) ||
3790 		WARN_ON(IS_ERR_OR_NULL(kctx->kctx_dentry)))
3791 		return;
3792 
3793 
3794 
3795 	/* Debugfs entry for getting the number of JIT allocations. */
3796 	debugfs_create_file("mem_jit_count", mode, kctx->kctx_dentry,
3797 			kctx, &kbase_jit_debugfs_count_fops);
3798 
3799 	/*
3800 	 * Debugfs entry for getting the total number of virtual pages
3801 	 * used by JIT allocations.
3802 	 */
3803 	debugfs_create_file("mem_jit_vm", mode, kctx->kctx_dentry,
3804 			kctx, &kbase_jit_debugfs_vm_fops);
3805 
3806 	/*
3807 	 * Debugfs entry for getting the number of physical pages used
3808 	 * by JIT allocations.
3809 	 */
3810 	debugfs_create_file("mem_jit_phys", mode, kctx->kctx_dentry,
3811 			kctx, &kbase_jit_debugfs_phys_fops);
3812 #if MALI_JIT_PRESSURE_LIMIT_BASE
3813 	/*
3814 	 * Debugfs entry for getting the number of pages used
3815 	 * by JIT allocations for estimating the physical pressure
3816 	 * limit.
3817 	 */
3818 	debugfs_create_file("mem_jit_used", mode, kctx->kctx_dentry,
3819 			kctx, &kbase_jit_debugfs_used_fops);
3820 
3821 	/*
3822 	 * Debugfs entry for getting the number of pages that could
3823 	 * be trimmed to free space for more JIT allocations.
3824 	 */
3825 	debugfs_create_file("mem_jit_trim", mode, kctx->kctx_dentry,
3826 			kctx, &kbase_jit_debugfs_trim_fops);
3827 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
3828 }
3829 #endif /* CONFIG_DEBUG_FS */
3830 
3831 /**
3832  * kbase_jit_destroy_worker - Deferred worker which frees JIT allocations
3833  * @work: Work item
3834  *
3835  * This function does the work of freeing JIT allocations whose physical
3836  * backing has been released.
3837  */
kbase_jit_destroy_worker(struct work_struct * work)3838 static void kbase_jit_destroy_worker(struct work_struct *work)
3839 {
3840 	struct kbase_context *kctx;
3841 	struct kbase_va_region *reg;
3842 
3843 	kctx = container_of(work, struct kbase_context, jit_work);
3844 	do {
3845 		mutex_lock(&kctx->jit_evict_lock);
3846 		if (list_empty(&kctx->jit_destroy_head)) {
3847 			mutex_unlock(&kctx->jit_evict_lock);
3848 			break;
3849 		}
3850 
3851 		reg = list_first_entry(&kctx->jit_destroy_head,
3852 				struct kbase_va_region, jit_node);
3853 
3854 		list_del(&reg->jit_node);
3855 		mutex_unlock(&kctx->jit_evict_lock);
3856 
3857 		kbase_gpu_vm_lock(kctx);
3858 
3859 		/*
3860 		 * Incrementing the refcount is prevented on JIT regions.
3861 		 * If/when this ever changes we would need to compensate
3862 		 * by implementing "free on putting the last reference",
3863 		 * but only for JIT regions.
3864 		 */
3865 		WARN_ON(atomic_read(&reg->no_user_free_count) > 1);
3866 		kbase_va_region_no_user_free_dec(reg);
3867 		kbase_mem_free_region(kctx, reg);
3868 		kbase_gpu_vm_unlock(kctx);
3869 	} while (1);
3870 }
3871 
kbase_jit_init(struct kbase_context * kctx)3872 int kbase_jit_init(struct kbase_context *kctx)
3873 {
3874 	mutex_lock(&kctx->jit_evict_lock);
3875 	INIT_LIST_HEAD(&kctx->jit_active_head);
3876 	INIT_LIST_HEAD(&kctx->jit_pool_head);
3877 	INIT_LIST_HEAD(&kctx->jit_destroy_head);
3878 	INIT_WORK(&kctx->jit_work, kbase_jit_destroy_worker);
3879 
3880 #if MALI_USE_CSF
3881 	mutex_init(&kctx->csf.kcpu_queues.jit_lock);
3882 	INIT_LIST_HEAD(&kctx->csf.kcpu_queues.jit_cmds_head);
3883 	INIT_LIST_HEAD(&kctx->csf.kcpu_queues.jit_blocked_queues);
3884 #else /* !MALI_USE_CSF */
3885 	INIT_LIST_HEAD(&kctx->jctx.jit_atoms_head);
3886 	INIT_LIST_HEAD(&kctx->jctx.jit_pending_alloc);
3887 #endif /* MALI_USE_CSF */
3888 	mutex_unlock(&kctx->jit_evict_lock);
3889 
3890 	kctx->jit_max_allocations = 0;
3891 	kctx->jit_current_allocations = 0;
3892 	kctx->trim_level = 0;
3893 
3894 	return 0;
3895 }
3896 
3897 /* Check if the allocation from JIT pool is of the same size as the new JIT
3898  * allocation and also, if BASE_JIT_ALLOC_MEM_TILER_ALIGN_TOP is set, meets
3899  * the alignment requirements.
3900  */
meet_size_and_tiler_align_top_requirements(const struct kbase_va_region * walker,const struct base_jit_alloc_info * info)3901 static bool meet_size_and_tiler_align_top_requirements(
3902 	const struct kbase_va_region *walker,
3903 	const struct base_jit_alloc_info *info)
3904 {
3905 	bool meet_reqs = true;
3906 
3907 	if (walker->nr_pages != info->va_pages)
3908 		meet_reqs = false;
3909 
3910 #if !MALI_USE_CSF
3911 	if (meet_reqs && (info->flags & BASE_JIT_ALLOC_MEM_TILER_ALIGN_TOP)) {
3912 		size_t align = info->extension;
3913 		size_t align_mask = align - 1;
3914 
3915 		if ((walker->start_pfn + info->commit_pages) & align_mask)
3916 			meet_reqs = false;
3917 	}
3918 #endif /* !MALI_USE_CSF */
3919 
3920 	return meet_reqs;
3921 }
3922 
3923 #if MALI_JIT_PRESSURE_LIMIT_BASE
3924 /* Function will guarantee *@freed will not exceed @pages_needed
3925  */
kbase_mem_jit_trim_pages_from_region(struct kbase_context * kctx,struct kbase_va_region * reg,size_t pages_needed,size_t * freed,bool shrink)3926 static int kbase_mem_jit_trim_pages_from_region(struct kbase_context *kctx,
3927 		struct kbase_va_region *reg, size_t pages_needed,
3928 		size_t *freed, bool shrink)
3929 {
3930 	int err = 0;
3931 	size_t available_pages = 0u;
3932 	const size_t old_pages = kbase_reg_current_backed_size(reg);
3933 	size_t new_pages = old_pages;
3934 	size_t to_free = 0u;
3935 	size_t max_allowed_pages = old_pages;
3936 
3937 #if !MALI_USE_CSF
3938 	lockdep_assert_held(&kctx->jctx.lock);
3939 #endif /* !MALI_USE_CSF */
3940 	lockdep_assert_held(&kctx->reg_lock);
3941 
3942 	/* Is this a JIT allocation that has been reported on? */
3943 	if (reg->used_pages == reg->nr_pages)
3944 		goto out;
3945 
3946 	if (!(reg->flags & KBASE_REG_HEAP_INFO_IS_SIZE)) {
3947 		/* For address based memory usage calculation, the GPU
3948 		 * allocates objects of up to size 's', but aligns every object
3949 		 * to alignment 'a', with a < s.
3950 		 *
3951 		 * It also doesn't have to write to all bytes in an object of
3952 		 * size 's'.
3953 		 *
3954 		 * Hence, we can observe the GPU's address for the end of used
3955 		 * memory being up to (s - a) bytes into the first unallocated
3956 		 * page.
3957 		 *
3958 		 * We allow for this and only warn when it exceeds this bound
3959 		 * (rounded up to page sized units). Note, this is allowed to
3960 		 * exceed reg->nr_pages.
3961 		 */
3962 		max_allowed_pages += PFN_UP(
3963 			KBASE_GPU_ALLOCATED_OBJECT_MAX_BYTES -
3964 			KBASE_GPU_ALLOCATED_OBJECT_ALIGN_BYTES);
3965 	} else if (reg->flags & KBASE_REG_TILER_ALIGN_TOP) {
3966 		/* The GPU could report being ready to write to the next
3967 		 * 'extension' sized chunk, but didn't actually write to it, so we
3968 		 * can report up to 'extension' size pages more than the backed
3969 		 * size.
3970 		 *
3971 		 * Note, this is allowed to exceed reg->nr_pages.
3972 		 */
3973 		max_allowed_pages += reg->extension;
3974 
3975 		/* Also note that in these GPUs, the GPU may make a large (>1
3976 		 * page) initial allocation but not actually write out to all
3977 		 * of it. Hence it might report that a much higher amount of
3978 		 * memory was used than actually was written to. This does not
3979 		 * result in a real warning because on growing this memory we
3980 		 * round up the size of the allocation up to an 'extension' sized
3981 		 * chunk, hence automatically bringing the backed size up to
3982 		 * the reported size.
3983 		 */
3984 	}
3985 
3986 	if (old_pages < reg->used_pages) {
3987 		/* Prevent overflow on available_pages, but only report the
3988 		 * problem if it's in a scenario where used_pages should have
3989 		 * been consistent with the backed size
3990 		 *
3991 		 * Note: In case of a size-based report, this legitimately
3992 		 * happens in common use-cases: we allow for up to this size of
3993 		 * memory being used, but depending on the content it doesn't
3994 		 * have to use all of it.
3995 		 *
3996 		 * Hence, we're much more quiet about that in the size-based
3997 		 * report case - it's not indicating a real problem, it's just
3998 		 * for information
3999 		 */
4000 		if (max_allowed_pages < reg->used_pages) {
4001 			if (!(reg->flags & KBASE_REG_HEAP_INFO_IS_SIZE))
4002 				dev_warn(kctx->kbdev->dev,
4003 						"%s: current backed pages %zu < reported used pages %zu (allowed to be up to %zu) on JIT 0x%llx vapages %zu\n",
4004 						__func__,
4005 						old_pages, reg->used_pages,
4006 						max_allowed_pages,
4007 						reg->start_pfn << PAGE_SHIFT,
4008 						reg->nr_pages);
4009 			else
4010 				dev_dbg(kctx->kbdev->dev,
4011 						"%s: no need to trim, current backed pages %zu < reported used pages %zu on size-report for JIT 0x%llx vapages %zu\n",
4012 						__func__,
4013 						old_pages, reg->used_pages,
4014 						reg->start_pfn << PAGE_SHIFT,
4015 						reg->nr_pages);
4016 			}
4017 		/* In any case, no error condition to report here, caller can
4018 		 * try other regions
4019 		 */
4020 
4021 		goto out;
4022 	}
4023 	available_pages = old_pages - reg->used_pages;
4024 	to_free = min(available_pages, pages_needed);
4025 
4026 	if (shrink) {
4027 		new_pages -= to_free;
4028 
4029 		err = kbase_mem_shrink(kctx, reg, new_pages);
4030 	}
4031 out:
4032 	trace_mali_jit_trim_from_region(reg, to_free, old_pages,
4033 			available_pages, new_pages);
4034 	*freed = to_free;
4035 	return err;
4036 }
4037 
4038 
4039 /**
4040  * kbase_mem_jit_trim_pages - Trim JIT regions until sufficient pages have been
4041  * freed
4042  * @kctx: Pointer to the kbase context whose active JIT allocations will be
4043  * checked.
4044  * @pages_needed: The maximum number of pages to trim.
4045  *
4046  * This functions checks all active JIT allocations in @kctx for unused pages
4047  * at the end, and trim the backed memory regions of those allocations down to
4048  * the used portion and free the unused pages into the page pool.
4049  *
4050  * Specifying @pages_needed allows us to stop early when there's enough
4051  * physical memory freed to sufficiently bring down the total JIT physical page
4052  * usage (e.g. to below the pressure limit)
4053  *
4054  * Return: Total number of successfully freed pages
4055  */
kbase_mem_jit_trim_pages(struct kbase_context * kctx,size_t pages_needed)4056 static size_t kbase_mem_jit_trim_pages(struct kbase_context *kctx,
4057 		size_t pages_needed)
4058 {
4059 	struct kbase_va_region *reg, *tmp;
4060 	size_t total_freed = 0;
4061 
4062 #if !MALI_USE_CSF
4063 	lockdep_assert_held(&kctx->jctx.lock);
4064 #endif /* !MALI_USE_CSF */
4065 	lockdep_assert_held(&kctx->reg_lock);
4066 	lockdep_assert_held(&kctx->jit_evict_lock);
4067 
4068 	list_for_each_entry_safe(reg, tmp, &kctx->jit_active_head, jit_node) {
4069 		int err;
4070 		size_t freed = 0u;
4071 
4072 		err = kbase_mem_jit_trim_pages_from_region(kctx, reg,
4073 				pages_needed, &freed, true);
4074 
4075 		if (err) {
4076 			/* Failed to trim, try the next region */
4077 			continue;
4078 		}
4079 
4080 		total_freed += freed;
4081 		WARN_ON(freed > pages_needed);
4082 		pages_needed -= freed;
4083 		if (!pages_needed)
4084 			break;
4085 	}
4086 
4087 	trace_mali_jit_trim(total_freed);
4088 
4089 	return total_freed;
4090 }
4091 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4092 
kbase_jit_grow(struct kbase_context * kctx,const struct base_jit_alloc_info * info,struct kbase_va_region * reg,struct kbase_sub_alloc ** prealloc_sas,enum kbase_caller_mmu_sync_info mmu_sync_info)4093 static int kbase_jit_grow(struct kbase_context *kctx,
4094 			  const struct base_jit_alloc_info *info,
4095 			  struct kbase_va_region *reg,
4096 			  struct kbase_sub_alloc **prealloc_sas,
4097 			  enum kbase_caller_mmu_sync_info mmu_sync_info)
4098 {
4099 	size_t delta;
4100 	size_t pages_required;
4101 	size_t old_size;
4102 	struct kbase_mem_pool *pool;
4103 	int ret = -ENOMEM;
4104 	struct tagged_addr *gpu_pages;
4105 
4106 	if (info->commit_pages > reg->nr_pages) {
4107 		/* Attempted to grow larger than maximum size */
4108 		return -EINVAL;
4109 	}
4110 
4111 	lockdep_assert_held(&kctx->reg_lock);
4112 
4113 	/* Make the physical backing no longer reclaimable */
4114 	if (!kbase_mem_evictable_unmake(reg->gpu_alloc))
4115 		goto update_failed;
4116 
4117 	if (reg->gpu_alloc->nents >= info->commit_pages)
4118 		goto done;
4119 
4120 	/* Grow the backing */
4121 	old_size = reg->gpu_alloc->nents;
4122 
4123 	/* Allocate some more pages */
4124 	delta = info->commit_pages - reg->gpu_alloc->nents;
4125 	pages_required = delta;
4126 
4127 	if (kctx->kbdev->pagesize_2mb && pages_required >= (SZ_2M / SZ_4K)) {
4128 		pool = &kctx->mem_pools.large[kctx->jit_group_id];
4129 		/* Round up to number of 2 MB pages required */
4130 		pages_required += ((SZ_2M / SZ_4K) - 1);
4131 		pages_required /= (SZ_2M / SZ_4K);
4132 	} else {
4133 		pool = &kctx->mem_pools.small[kctx->jit_group_id];
4134 	}
4135 
4136 	if (reg->cpu_alloc != reg->gpu_alloc)
4137 		pages_required *= 2;
4138 
4139 	spin_lock(&kctx->mem_partials_lock);
4140 	kbase_mem_pool_lock(pool);
4141 
4142 	/* As we can not allocate memory from the kernel with the vm_lock held,
4143 	 * grow the pool to the required size with the lock dropped. We hold the
4144 	 * pool lock to prevent another thread from allocating from the pool
4145 	 * between the grow and allocation.
4146 	 */
4147 	while (kbase_mem_pool_size(pool) < pages_required) {
4148 		int pool_delta = pages_required - kbase_mem_pool_size(pool);
4149 		int ret;
4150 
4151 		kbase_mem_pool_unlock(pool);
4152 		spin_unlock(&kctx->mem_partials_lock);
4153 
4154 		kbase_gpu_vm_unlock(kctx);
4155 		ret = kbase_mem_pool_grow(pool, pool_delta, kctx->task);
4156 		kbase_gpu_vm_lock(kctx);
4157 
4158 		if (ret)
4159 			goto update_failed;
4160 
4161 		spin_lock(&kctx->mem_partials_lock);
4162 		kbase_mem_pool_lock(pool);
4163 	}
4164 
4165 	gpu_pages = kbase_alloc_phy_pages_helper_locked(reg->gpu_alloc, pool,
4166 			delta, &prealloc_sas[0]);
4167 	if (!gpu_pages) {
4168 		kbase_mem_pool_unlock(pool);
4169 		spin_unlock(&kctx->mem_partials_lock);
4170 		goto update_failed;
4171 	}
4172 
4173 	if (reg->cpu_alloc != reg->gpu_alloc) {
4174 		struct tagged_addr *cpu_pages;
4175 
4176 		cpu_pages = kbase_alloc_phy_pages_helper_locked(reg->cpu_alloc,
4177 				pool, delta, &prealloc_sas[1]);
4178 		if (!cpu_pages) {
4179 			kbase_free_phy_pages_helper_locked(reg->gpu_alloc,
4180 					pool, gpu_pages, delta);
4181 			kbase_mem_pool_unlock(pool);
4182 			spin_unlock(&kctx->mem_partials_lock);
4183 			goto update_failed;
4184 		}
4185 	}
4186 	kbase_mem_pool_unlock(pool);
4187 	spin_unlock(&kctx->mem_partials_lock);
4188 
4189 	ret = kbase_mem_grow_gpu_mapping(kctx, reg, info->commit_pages,
4190 					 old_size, mmu_sync_info);
4191 	/*
4192 	 * The grow failed so put the allocation back in the
4193 	 * pool and return failure.
4194 	 */
4195 	if (ret)
4196 		goto update_failed;
4197 
4198 done:
4199 	ret = 0;
4200 
4201 	/* Update attributes of JIT allocation taken from the pool */
4202 	reg->initial_commit = info->commit_pages;
4203 	reg->extension = info->extension;
4204 
4205 update_failed:
4206 	return ret;
4207 }
4208 
trace_jit_stats(struct kbase_context * kctx,u32 bin_id,u32 max_allocations)4209 static void trace_jit_stats(struct kbase_context *kctx,
4210 		u32 bin_id, u32 max_allocations)
4211 {
4212 	const u32 alloc_count =
4213 		kctx->jit_current_allocations_per_bin[bin_id];
4214 	struct kbase_device *kbdev = kctx->kbdev;
4215 
4216 	struct kbase_va_region *walker;
4217 	u32 va_pages = 0;
4218 	u32 ph_pages = 0;
4219 
4220 	mutex_lock(&kctx->jit_evict_lock);
4221 	list_for_each_entry(walker, &kctx->jit_active_head, jit_node) {
4222 		if (walker->jit_bin_id != bin_id)
4223 			continue;
4224 
4225 		va_pages += walker->nr_pages;
4226 		ph_pages += walker->gpu_alloc->nents;
4227 	}
4228 	mutex_unlock(&kctx->jit_evict_lock);
4229 
4230 	KBASE_TLSTREAM_AUX_JIT_STATS(kbdev, kctx->id, bin_id,
4231 		max_allocations, alloc_count, va_pages, ph_pages);
4232 }
4233 
4234 #if MALI_JIT_PRESSURE_LIMIT_BASE
4235 /**
4236  * get_jit_phys_backing() - calculate the physical backing of all JIT
4237  * allocations
4238  *
4239  * @kctx: Pointer to the kbase context whose active JIT allocations will be
4240  * checked
4241  *
4242  * Return: number of pages that are committed by JIT allocations
4243  */
get_jit_phys_backing(struct kbase_context * kctx)4244 static size_t get_jit_phys_backing(struct kbase_context *kctx)
4245 {
4246 	struct kbase_va_region *walker;
4247 	size_t backing = 0;
4248 
4249 	lockdep_assert_held(&kctx->jit_evict_lock);
4250 
4251 	list_for_each_entry(walker, &kctx->jit_active_head, jit_node) {
4252 		backing += kbase_reg_current_backed_size(walker);
4253 	}
4254 
4255 	return backing;
4256 }
4257 
kbase_jit_trim_necessary_pages(struct kbase_context * kctx,size_t needed_pages)4258 void kbase_jit_trim_necessary_pages(struct kbase_context *kctx,
4259 				    size_t needed_pages)
4260 {
4261 	size_t jit_backing = 0;
4262 	size_t pages_to_trim = 0;
4263 
4264 #if !MALI_USE_CSF
4265 	lockdep_assert_held(&kctx->jctx.lock);
4266 #endif /* !MALI_USE_CSF */
4267 	lockdep_assert_held(&kctx->reg_lock);
4268 	lockdep_assert_held(&kctx->jit_evict_lock);
4269 
4270 	jit_backing = get_jit_phys_backing(kctx);
4271 
4272 	/* It is possible that this is the case - if this is the first
4273 	 * allocation after "ignore_pressure_limit" allocation.
4274 	 */
4275 	if (jit_backing > kctx->jit_phys_pages_limit) {
4276 		pages_to_trim += (jit_backing - kctx->jit_phys_pages_limit) +
4277 				 needed_pages;
4278 	} else {
4279 		size_t backed_diff = kctx->jit_phys_pages_limit - jit_backing;
4280 
4281 		if (needed_pages > backed_diff)
4282 			pages_to_trim += needed_pages - backed_diff;
4283 	}
4284 
4285 	if (pages_to_trim) {
4286 		size_t trimmed_pages =
4287 			kbase_mem_jit_trim_pages(kctx, pages_to_trim);
4288 
4289 		/* This should never happen - we already asserted that
4290 		 * we are not violating JIT pressure limit in earlier
4291 		 * checks, which means that in-flight JIT allocations
4292 		 * must have enough unused pages to satisfy the new
4293 		 * allocation
4294 		 */
4295 		WARN_ON(trimmed_pages < pages_to_trim);
4296 	}
4297 }
4298 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4299 
4300 /**
4301  * jit_allow_allocate() - check whether basic conditions are satisfied to allow
4302  * a new JIT allocation
4303  *
4304  * @kctx: Pointer to the kbase context
4305  * @info: Pointer to JIT allocation information for the new allocation
4306  * @ignore_pressure_limit: Flag to indicate whether JIT pressure limit check
4307  * should be ignored
4308  *
4309  * Return: true if allocation can be executed, false otherwise
4310  */
jit_allow_allocate(struct kbase_context * kctx,const struct base_jit_alloc_info * info,bool ignore_pressure_limit)4311 static bool jit_allow_allocate(struct kbase_context *kctx,
4312 		const struct base_jit_alloc_info *info,
4313 		bool ignore_pressure_limit)
4314 {
4315 #if !MALI_USE_CSF
4316 	lockdep_assert_held(&kctx->jctx.lock);
4317 #else /* MALI_USE_CSF */
4318 	lockdep_assert_held(&kctx->csf.kcpu_queues.jit_lock);
4319 #endif /* !MALI_USE_CSF */
4320 
4321 #if MALI_JIT_PRESSURE_LIMIT_BASE
4322 	if (!ignore_pressure_limit &&
4323 			((kctx->jit_phys_pages_limit <= kctx->jit_current_phys_pressure) ||
4324 			(info->va_pages > (kctx->jit_phys_pages_limit - kctx->jit_current_phys_pressure)))) {
4325 		dev_dbg(kctx->kbdev->dev,
4326 			"Max JIT page allocations limit reached: active pages %llu, max pages %llu\n",
4327 			kctx->jit_current_phys_pressure + info->va_pages,
4328 			kctx->jit_phys_pages_limit);
4329 		return false;
4330 	}
4331 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4332 
4333 	if (kctx->jit_current_allocations >= kctx->jit_max_allocations) {
4334 		/* Too many current allocations */
4335 		dev_dbg(kctx->kbdev->dev,
4336 			"Max JIT allocations limit reached: active allocations %d, max allocations %d\n",
4337 			kctx->jit_current_allocations,
4338 			kctx->jit_max_allocations);
4339 		return false;
4340 	}
4341 
4342 	if (info->max_allocations > 0 &&
4343 			kctx->jit_current_allocations_per_bin[info->bin_id] >=
4344 			info->max_allocations) {
4345 		/* Too many current allocations in this bin */
4346 		dev_dbg(kctx->kbdev->dev,
4347 			"Per bin limit of max JIT allocations reached: bin_id %d, active allocations %d, max allocations %d\n",
4348 			info->bin_id,
4349 			kctx->jit_current_allocations_per_bin[info->bin_id],
4350 			info->max_allocations);
4351 		return false;
4352 	}
4353 
4354 	return true;
4355 }
4356 
4357 static struct kbase_va_region *
find_reasonable_region(const struct base_jit_alloc_info * info,struct list_head * pool_head,bool ignore_usage_id)4358 find_reasonable_region(const struct base_jit_alloc_info *info,
4359 		       struct list_head *pool_head, bool ignore_usage_id)
4360 {
4361 	struct kbase_va_region *closest_reg = NULL;
4362 	struct kbase_va_region *walker;
4363 	size_t current_diff = SIZE_MAX;
4364 
4365 	list_for_each_entry(walker, pool_head, jit_node) {
4366 		if ((ignore_usage_id ||
4367 		     walker->jit_usage_id == info->usage_id) &&
4368 		    walker->jit_bin_id == info->bin_id &&
4369 		    meet_size_and_tiler_align_top_requirements(walker, info)) {
4370 			size_t min_size, max_size, diff;
4371 
4372 			/*
4373 			 * The JIT allocations VA requirements have been met,
4374 			 * it's suitable but other allocations might be a
4375 			 * better fit.
4376 			 */
4377 			min_size = min_t(size_t, walker->gpu_alloc->nents,
4378 					 info->commit_pages);
4379 			max_size = max_t(size_t, walker->gpu_alloc->nents,
4380 					 info->commit_pages);
4381 			diff = max_size - min_size;
4382 
4383 			if (current_diff > diff) {
4384 				current_diff = diff;
4385 				closest_reg = walker;
4386 			}
4387 
4388 			/* The allocation is an exact match */
4389 			if (current_diff == 0)
4390 				break;
4391 		}
4392 	}
4393 
4394 	return closest_reg;
4395 }
4396 
kbase_jit_allocate(struct kbase_context * kctx,const struct base_jit_alloc_info * info,bool ignore_pressure_limit)4397 struct kbase_va_region *kbase_jit_allocate(struct kbase_context *kctx,
4398 		const struct base_jit_alloc_info *info,
4399 		bool ignore_pressure_limit)
4400 {
4401 	struct kbase_va_region *reg = NULL;
4402 	struct kbase_sub_alloc *prealloc_sas[2] = { NULL, NULL };
4403 	int i;
4404 
4405 	/* Calls to this function are inherently synchronous, with respect to
4406 	 * MMU operations.
4407 	 */
4408 	const enum kbase_caller_mmu_sync_info mmu_sync_info = CALLER_MMU_SYNC;
4409 
4410 #if !MALI_USE_CSF
4411 	lockdep_assert_held(&kctx->jctx.lock);
4412 #else /* MALI_USE_CSF */
4413 	lockdep_assert_held(&kctx->csf.kcpu_queues.jit_lock);
4414 #endif /* !MALI_USE_CSF */
4415 
4416 	if (!jit_allow_allocate(kctx, info, ignore_pressure_limit))
4417 		return NULL;
4418 
4419 	if (kctx->kbdev->pagesize_2mb) {
4420 		/* Preallocate memory for the sub-allocation structs */
4421 		for (i = 0; i != ARRAY_SIZE(prealloc_sas); ++i) {
4422 			prealloc_sas[i] = kmalloc(sizeof(*prealloc_sas[i]), GFP_KERNEL);
4423 			if (!prealloc_sas[i])
4424 				goto end;
4425 		}
4426 	}
4427 
4428 	kbase_gpu_vm_lock(kctx);
4429 	mutex_lock(&kctx->jit_evict_lock);
4430 
4431 	/*
4432 	 * Scan the pool for an existing allocation which meets our
4433 	 * requirements and remove it.
4434 	 */
4435 	if (info->usage_id != 0)
4436 		/* First scan for an allocation with the same usage ID */
4437 		reg = find_reasonable_region(info, &kctx->jit_pool_head, false);
4438 
4439 	if (!reg)
4440 		/* No allocation with the same usage ID, or usage IDs not in
4441 		 * use. Search for an allocation we can reuse.
4442 		 */
4443 		reg = find_reasonable_region(info, &kctx->jit_pool_head, true);
4444 
4445 	if (reg) {
4446 #if MALI_JIT_PRESSURE_LIMIT_BASE
4447 		size_t needed_pages = 0;
4448 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4449 		int ret;
4450 
4451 		/*
4452 		 * Remove the found region from the pool and add it to the
4453 		 * active list.
4454 		 */
4455 		list_move(&reg->jit_node, &kctx->jit_active_head);
4456 
4457 		WARN_ON(reg->gpu_alloc->evicted);
4458 
4459 		/*
4460 		 * Remove the allocation from the eviction list as it's no
4461 		 * longer eligible for eviction. This must be done before
4462 		 * dropping the jit_evict_lock
4463 		 */
4464 		list_del_init(&reg->gpu_alloc->evict_node);
4465 
4466 #if MALI_JIT_PRESSURE_LIMIT_BASE
4467 		if (!ignore_pressure_limit) {
4468 			if (info->commit_pages > reg->gpu_alloc->nents)
4469 				needed_pages = info->commit_pages -
4470 					       reg->gpu_alloc->nents;
4471 
4472 			/* Update early the recycled JIT region's estimate of
4473 			 * used_pages to ensure it doesn't get trimmed
4474 			 * undesirably. This is needed as the recycled JIT
4475 			 * region has been added to the active list but the
4476 			 * number of used pages for it would be zero, so it
4477 			 * could get trimmed instead of other allocations only
4478 			 * to be regrown later resulting in a breach of the JIT
4479 			 * physical pressure limit.
4480 			 * Also that trimming would disturb the accounting of
4481 			 * physical pages, i.e. the VM stats, as the number of
4482 			 * backing pages would have changed when the call to
4483 			 * kbase_mem_evictable_unmark_reclaim is made.
4484 			 *
4485 			 * The second call to update pressure at the end of
4486 			 * this function would effectively be a nop.
4487 			 */
4488 			kbase_jit_report_update_pressure(
4489 				kctx, reg, info->va_pages,
4490 				KBASE_JIT_REPORT_ON_ALLOC_OR_FREE);
4491 
4492 			kbase_jit_request_phys_increase_locked(kctx,
4493 							       needed_pages);
4494 		}
4495 #endif
4496 		mutex_unlock(&kctx->jit_evict_lock);
4497 
4498 		/* kbase_jit_grow() can release & reacquire 'kctx->reg_lock',
4499 		 * so any state protected by that lock might need to be
4500 		 * re-evaluated if more code is added here in future.
4501 		 */
4502 		ret = kbase_jit_grow(kctx, info, reg, prealloc_sas,
4503 				     mmu_sync_info);
4504 
4505 #if MALI_JIT_PRESSURE_LIMIT_BASE
4506 		if (!ignore_pressure_limit)
4507 			kbase_jit_done_phys_increase(kctx, needed_pages);
4508 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4509 
4510 		kbase_gpu_vm_unlock(kctx);
4511 
4512 		if (ret < 0) {
4513 			/*
4514 			 * An update to an allocation from the pool failed,
4515 			 * chances are slim a new allocation would fare any
4516 			 * better so return the allocation to the pool and
4517 			 * return the function with failure.
4518 			 */
4519 			dev_dbg(kctx->kbdev->dev,
4520 				"JIT allocation resize failed: va_pages 0x%llx, commit_pages 0x%llx\n",
4521 				info->va_pages, info->commit_pages);
4522 #if MALI_JIT_PRESSURE_LIMIT_BASE
4523 			/* Undo the early change made to the recycled JIT
4524 			 * region's estimate of used_pages.
4525 			 */
4526 			if (!ignore_pressure_limit) {
4527 				kbase_jit_report_update_pressure(
4528 					kctx, reg, 0,
4529 					KBASE_JIT_REPORT_ON_ALLOC_OR_FREE);
4530 			}
4531 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4532 			mutex_lock(&kctx->jit_evict_lock);
4533 			list_move(&reg->jit_node, &kctx->jit_pool_head);
4534 			mutex_unlock(&kctx->jit_evict_lock);
4535 			reg = NULL;
4536 			goto end;
4537 		} else {
4538 			/* A suitable JIT allocation existed on the evict list, so we need
4539 			 * to make sure that the NOT_MOVABLE property is cleared.
4540 			 */
4541 			if (kbase_page_migration_enabled) {
4542 				kbase_gpu_vm_lock(kctx);
4543 				mutex_lock(&kctx->jit_evict_lock);
4544 				kbase_set_phy_alloc_page_status(reg->gpu_alloc, ALLOCATED_MAPPED);
4545 				mutex_unlock(&kctx->jit_evict_lock);
4546 				kbase_gpu_vm_unlock(kctx);
4547 			}
4548 		}
4549 	} else {
4550 		/* No suitable JIT allocation was found so create a new one */
4551 		u64 flags = BASE_MEM_PROT_CPU_RD | BASE_MEM_PROT_GPU_RD |
4552 				BASE_MEM_PROT_GPU_WR | BASE_MEM_GROW_ON_GPF |
4553 				BASE_MEM_COHERENT_LOCAL |
4554 				BASEP_MEM_NO_USER_FREE;
4555 		u64 gpu_addr;
4556 
4557 #if !MALI_USE_CSF
4558 		if (info->flags & BASE_JIT_ALLOC_MEM_TILER_ALIGN_TOP)
4559 			flags |= BASE_MEM_TILER_ALIGN_TOP;
4560 #endif /* !MALI_USE_CSF */
4561 
4562 		flags |= kbase_mem_group_id_set(kctx->jit_group_id);
4563 #if MALI_JIT_PRESSURE_LIMIT_BASE
4564 		if (!ignore_pressure_limit) {
4565 			flags |= BASEP_MEM_PERFORM_JIT_TRIM;
4566 			/* The corresponding call to 'done_phys_increase' would
4567 			 * be made inside the kbase_mem_alloc().
4568 			 */
4569 			kbase_jit_request_phys_increase_locked(
4570 				kctx, info->commit_pages);
4571 		}
4572 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4573 
4574 		mutex_unlock(&kctx->jit_evict_lock);
4575 		kbase_gpu_vm_unlock(kctx);
4576 
4577 		reg = kbase_mem_alloc(kctx, info->va_pages, info->commit_pages, info->extension,
4578 				      &flags, &gpu_addr, mmu_sync_info);
4579 		if (!reg) {
4580 			/* Most likely not enough GPU virtual space left for
4581 			 * the new JIT allocation.
4582 			 */
4583 			dev_dbg(kctx->kbdev->dev,
4584 				"Failed to allocate JIT memory: va_pages 0x%llx, commit_pages 0x%llx\n",
4585 				info->va_pages, info->commit_pages);
4586 			goto end;
4587 		}
4588 
4589 		if (!ignore_pressure_limit) {
4590 			/* Due to enforcing of pressure limit, kbase_mem_alloc
4591 			 * was instructed to perform the trimming which in turn
4592 			 * would have ensured that the new JIT allocation is
4593 			 * already in the jit_active_head list, so nothing to
4594 			 * do here.
4595 			 */
4596 			WARN_ON(list_empty(&reg->jit_node));
4597 		} else {
4598 			mutex_lock(&kctx->jit_evict_lock);
4599 			list_add(&reg->jit_node, &kctx->jit_active_head);
4600 			mutex_unlock(&kctx->jit_evict_lock);
4601 		}
4602 	}
4603 
4604 	/* Similarly to tiler heap init, there is a short window of time
4605 	 * where the (either recycled or newly allocated, in our case) region has
4606 	 * "no user free" count incremented but is still missing the DONT_NEED flag, and
4607 	 * doesn't yet have the ACTIVE_JIT_ALLOC flag either. Temporarily leaking the
4608 	 * allocation is the least bad option that doesn't lead to a security issue down the
4609 	 * line (it will eventually be cleaned up during context termination).
4610 	 *
4611 	 * We also need to call kbase_gpu_vm_lock regardless, as we're updating the region
4612 	 * flags.
4613 	 */
4614 	kbase_gpu_vm_lock(kctx);
4615 	if (unlikely(atomic_read(&reg->no_user_free_count) > 1)) {
4616 		kbase_gpu_vm_unlock(kctx);
4617 		dev_err(kctx->kbdev->dev, "JIT region has no_user_free_count > 1!\n");
4618 
4619 		mutex_lock(&kctx->jit_evict_lock);
4620 		list_move(&reg->jit_node, &kctx->jit_pool_head);
4621 		mutex_unlock(&kctx->jit_evict_lock);
4622 
4623 		reg = NULL;
4624 		goto end;
4625 	}
4626 
4627 	trace_mali_jit_alloc(reg, info->id);
4628 
4629 	kctx->jit_current_allocations++;
4630 	kctx->jit_current_allocations_per_bin[info->bin_id]++;
4631 
4632 	trace_jit_stats(kctx, info->bin_id, info->max_allocations);
4633 
4634 	reg->jit_usage_id = info->usage_id;
4635 	reg->jit_bin_id = info->bin_id;
4636 	reg->flags |= KBASE_REG_ACTIVE_JIT_ALLOC;
4637 #if MALI_JIT_PRESSURE_LIMIT_BASE
4638 	if (info->flags & BASE_JIT_ALLOC_HEAP_INFO_IS_SIZE)
4639 		reg->flags = reg->flags | KBASE_REG_HEAP_INFO_IS_SIZE;
4640 	reg->heap_info_gpu_addr = info->heap_info_gpu_addr;
4641 	kbase_jit_report_update_pressure(kctx, reg, info->va_pages,
4642 			KBASE_JIT_REPORT_ON_ALLOC_OR_FREE);
4643 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4644 	kbase_gpu_vm_unlock(kctx);
4645 
4646 end:
4647 	for (i = 0; i != ARRAY_SIZE(prealloc_sas); ++i)
4648 		kfree(prealloc_sas[i]);
4649 
4650 	return reg;
4651 }
4652 
kbase_jit_free(struct kbase_context * kctx,struct kbase_va_region * reg)4653 void kbase_jit_free(struct kbase_context *kctx, struct kbase_va_region *reg)
4654 {
4655 	u64 old_pages;
4656 
4657 #if !MALI_USE_CSF
4658 	lockdep_assert_held(&kctx->jctx.lock);
4659 #else /* MALI_USE_CSF */
4660 	lockdep_assert_held(&kctx->csf.kcpu_queues.jit_lock);
4661 #endif /* !MALI_USE_CSF */
4662 
4663 	/* JIT id not immediately available here, so use 0u */
4664 	trace_mali_jit_free(reg, 0u);
4665 
4666 	/* Get current size of JIT region */
4667 	old_pages = kbase_reg_current_backed_size(reg);
4668 	if (reg->initial_commit < old_pages) {
4669 		/* Free trim_level % of region, but don't go below initial
4670 		 * commit size
4671 		 */
4672 		u64 new_size = MAX(reg->initial_commit,
4673 			div_u64(old_pages * (100 - kctx->trim_level), 100));
4674 		u64 delta = old_pages - new_size;
4675 
4676 		if (delta) {
4677 			mutex_lock(&kctx->reg_lock);
4678 			kbase_mem_shrink(kctx, reg, old_pages - delta);
4679 			mutex_unlock(&kctx->reg_lock);
4680 		}
4681 	}
4682 
4683 #if MALI_JIT_PRESSURE_LIMIT_BASE
4684 	reg->heap_info_gpu_addr = 0;
4685 	kbase_jit_report_update_pressure(kctx, reg, 0,
4686 			KBASE_JIT_REPORT_ON_ALLOC_OR_FREE);
4687 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4688 
4689 	kctx->jit_current_allocations--;
4690 	kctx->jit_current_allocations_per_bin[reg->jit_bin_id]--;
4691 
4692 	trace_jit_stats(kctx, reg->jit_bin_id, UINT_MAX);
4693 
4694 	kbase_mem_evictable_mark_reclaim(reg->gpu_alloc);
4695 
4696 	kbase_gpu_vm_lock(kctx);
4697 	reg->flags |= KBASE_REG_DONT_NEED;
4698 	reg->flags &= ~KBASE_REG_ACTIVE_JIT_ALLOC;
4699 	kbase_mem_shrink_cpu_mapping(kctx, reg, 0, reg->gpu_alloc->nents);
4700 	kbase_gpu_vm_unlock(kctx);
4701 
4702 	/*
4703 	 * Add the allocation to the eviction list and the jit pool, after this
4704 	 * point the shrink can reclaim it, or it may be reused.
4705 	 */
4706 	mutex_lock(&kctx->jit_evict_lock);
4707 
4708 	/* This allocation can't already be on a list. */
4709 	WARN_ON(!list_empty(&reg->gpu_alloc->evict_node));
4710 	list_add(&reg->gpu_alloc->evict_node, &kctx->evict_list);
4711 	atomic_add(reg->gpu_alloc->nents, &kctx->evict_nents);
4712 
4713 	list_move(&reg->jit_node, &kctx->jit_pool_head);
4714 
4715 	/* Inactive JIT regions should be freed by the shrinker and not impacted
4716 	 * by page migration. Once freed, they will enter into the page migration
4717 	 * state machine via the mempools.
4718 	 */
4719 	if (kbase_page_migration_enabled)
4720 		kbase_set_phy_alloc_page_status(reg->gpu_alloc, NOT_MOVABLE);
4721 	mutex_unlock(&kctx->jit_evict_lock);
4722 }
4723 
kbase_jit_backing_lost(struct kbase_va_region * reg)4724 void kbase_jit_backing_lost(struct kbase_va_region *reg)
4725 {
4726 	struct kbase_context *kctx = kbase_reg_flags_to_kctx(reg);
4727 
4728 	if (WARN_ON(!kctx))
4729 		return;
4730 
4731 	lockdep_assert_held(&kctx->jit_evict_lock);
4732 
4733 	/*
4734 	 * JIT allocations will always be on a list, if the region
4735 	 * is not on a list then it's not a JIT allocation.
4736 	 */
4737 	if (list_empty(&reg->jit_node))
4738 		return;
4739 
4740 	/*
4741 	 * Freeing the allocation requires locks we might not be able
4742 	 * to take now, so move the allocation to the free list and kick
4743 	 * the worker which will do the freeing.
4744 	 */
4745 	list_move(&reg->jit_node, &kctx->jit_destroy_head);
4746 
4747 	schedule_work(&kctx->jit_work);
4748 }
4749 
kbase_jit_evict(struct kbase_context * kctx)4750 bool kbase_jit_evict(struct kbase_context *kctx)
4751 {
4752 	struct kbase_va_region *reg = NULL;
4753 
4754 	lockdep_assert_held(&kctx->reg_lock);
4755 
4756 	/* Free the oldest allocation from the pool */
4757 	mutex_lock(&kctx->jit_evict_lock);
4758 	if (!list_empty(&kctx->jit_pool_head)) {
4759 		reg = list_entry(kctx->jit_pool_head.prev,
4760 				struct kbase_va_region, jit_node);
4761 		list_del(&reg->jit_node);
4762 		list_del_init(&reg->gpu_alloc->evict_node);
4763 	}
4764 	mutex_unlock(&kctx->jit_evict_lock);
4765 
4766 	if (reg) {
4767 		/*
4768 		 * Incrementing the refcount is prevented on JIT regions.
4769 		 * If/when this ever changes we would need to compensate
4770 		 * by implementing "free on putting the last reference",
4771 		 * but only for JIT regions.
4772 		 */
4773 		WARN_ON(atomic_read(&reg->no_user_free_count) > 1);
4774 		kbase_va_region_no_user_free_dec(reg);
4775 		kbase_mem_free_region(kctx, reg);
4776 	}
4777 
4778 	return (reg != NULL);
4779 }
4780 
kbase_jit_term(struct kbase_context * kctx)4781 void kbase_jit_term(struct kbase_context *kctx)
4782 {
4783 	struct kbase_va_region *walker;
4784 
4785 	/* Free all allocations for this context */
4786 
4787 	kbase_gpu_vm_lock(kctx);
4788 	mutex_lock(&kctx->jit_evict_lock);
4789 	/* Free all allocations from the pool */
4790 	while (!list_empty(&kctx->jit_pool_head)) {
4791 		walker = list_first_entry(&kctx->jit_pool_head,
4792 				struct kbase_va_region, jit_node);
4793 		list_del(&walker->jit_node);
4794 		list_del_init(&walker->gpu_alloc->evict_node);
4795 		mutex_unlock(&kctx->jit_evict_lock);
4796 		/*
4797 		 * Incrementing the refcount is prevented on JIT regions.
4798 		 * If/when this ever changes we would need to compensate
4799 		 * by implementing "free on putting the last reference",
4800 		 * but only for JIT regions.
4801 		 */
4802 		WARN_ON(atomic_read(&walker->no_user_free_count) > 1);
4803 		kbase_va_region_no_user_free_dec(walker);
4804 		kbase_mem_free_region(kctx, walker);
4805 		mutex_lock(&kctx->jit_evict_lock);
4806 	}
4807 
4808 	/* Free all allocations from active list */
4809 	while (!list_empty(&kctx->jit_active_head)) {
4810 		walker = list_first_entry(&kctx->jit_active_head,
4811 				struct kbase_va_region, jit_node);
4812 		list_del(&walker->jit_node);
4813 		list_del_init(&walker->gpu_alloc->evict_node);
4814 		mutex_unlock(&kctx->jit_evict_lock);
4815 		/*
4816 		 * Incrementing the refcount is prevented on JIT regions.
4817 		 * If/when this ever changes we would need to compensate
4818 		 * by implementing "free on putting the last reference",
4819 		 * but only for JIT regions.
4820 		 */
4821 		WARN_ON(atomic_read(&walker->no_user_free_count) > 1);
4822 		kbase_va_region_no_user_free_dec(walker);
4823 		kbase_mem_free_region(kctx, walker);
4824 		mutex_lock(&kctx->jit_evict_lock);
4825 	}
4826 #if MALI_JIT_PRESSURE_LIMIT_BASE
4827 	WARN_ON(kctx->jit_phys_pages_to_be_allocated);
4828 #endif
4829 	mutex_unlock(&kctx->jit_evict_lock);
4830 	kbase_gpu_vm_unlock(kctx);
4831 
4832 	/*
4833 	 * Flush the freeing of allocations whose backing has been freed
4834 	 * (i.e. everything in jit_destroy_head).
4835 	 */
4836 	cancel_work_sync(&kctx->jit_work);
4837 }
4838 
4839 #if MALI_JIT_PRESSURE_LIMIT_BASE
kbase_trace_jit_report_gpu_mem_trace_enabled(struct kbase_context * kctx,struct kbase_va_region * reg,unsigned int flags)4840 void kbase_trace_jit_report_gpu_mem_trace_enabled(struct kbase_context *kctx,
4841 		struct kbase_va_region *reg, unsigned int flags)
4842 {
4843 	/* Offset to the location used for a JIT report within the GPU memory
4844 	 *
4845 	 * This constants only used for this debugging function - not useful
4846 	 * anywhere else in kbase
4847 	 */
4848 	const u64 jit_report_gpu_mem_offset = sizeof(u64)*2;
4849 
4850 	u64 addr_start;
4851 	struct kbase_vmap_struct mapping;
4852 	u64 *ptr;
4853 
4854 	if (reg->heap_info_gpu_addr == 0ull)
4855 		goto out;
4856 
4857 	/* Nothing else to trace in the case the memory just contains the
4858 	 * size. Other tracepoints already record the relevant area of memory.
4859 	 */
4860 	if (reg->flags & KBASE_REG_HEAP_INFO_IS_SIZE)
4861 		goto out;
4862 
4863 	addr_start = reg->heap_info_gpu_addr - jit_report_gpu_mem_offset;
4864 
4865 	ptr = kbase_vmap_prot(kctx, addr_start, KBASE_JIT_REPORT_GPU_MEM_SIZE,
4866 			KBASE_REG_CPU_RD, &mapping);
4867 	if (!ptr) {
4868 		dev_warn(kctx->kbdev->dev,
4869 				"%s: JIT start=0x%llx unable to map memory near end pointer %llx\n",
4870 				__func__, reg->start_pfn << PAGE_SHIFT,
4871 				addr_start);
4872 		goto out;
4873 	}
4874 
4875 	trace_mali_jit_report_gpu_mem(addr_start, reg->start_pfn << PAGE_SHIFT,
4876 				ptr, flags);
4877 
4878 	kbase_vunmap(kctx, &mapping);
4879 out:
4880 	return;
4881 }
4882 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4883 
4884 #if MALI_JIT_PRESSURE_LIMIT_BASE
kbase_jit_report_update_pressure(struct kbase_context * kctx,struct kbase_va_region * reg,u64 new_used_pages,unsigned int flags)4885 void kbase_jit_report_update_pressure(struct kbase_context *kctx,
4886 		struct kbase_va_region *reg, u64 new_used_pages,
4887 		unsigned int flags)
4888 {
4889 	u64 diff;
4890 
4891 #if !MALI_USE_CSF
4892 	lockdep_assert_held(&kctx->jctx.lock);
4893 #endif /* !MALI_USE_CSF */
4894 
4895 	trace_mali_jit_report_pressure(reg, new_used_pages,
4896 		kctx->jit_current_phys_pressure + new_used_pages -
4897 			reg->used_pages,
4898 		flags);
4899 
4900 	if (WARN_ON(new_used_pages > reg->nr_pages))
4901 		return;
4902 
4903 	if (reg->used_pages > new_used_pages) {
4904 		/* We reduced the number of used pages */
4905 		diff = reg->used_pages - new_used_pages;
4906 
4907 		if (!WARN_ON(diff > kctx->jit_current_phys_pressure))
4908 			kctx->jit_current_phys_pressure -= diff;
4909 
4910 		reg->used_pages = new_used_pages;
4911 	} else {
4912 		/* We increased the number of used pages */
4913 		diff = new_used_pages - reg->used_pages;
4914 
4915 		if (!WARN_ON(diff > U64_MAX - kctx->jit_current_phys_pressure))
4916 			kctx->jit_current_phys_pressure += diff;
4917 
4918 		reg->used_pages = new_used_pages;
4919 	}
4920 
4921 }
4922 #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
4923 
kbase_unpin_user_buf_page(struct page * page)4924 void kbase_unpin_user_buf_page(struct page *page)
4925 {
4926 #if KERNEL_VERSION(5, 9, 0) > LINUX_VERSION_CODE
4927 	put_page(page);
4928 #else
4929 	unpin_user_page(page);
4930 #endif
4931 }
4932 
4933 #if MALI_USE_CSF
kbase_jd_user_buf_unpin_pages(struct kbase_mem_phy_alloc * alloc)4934 static void kbase_jd_user_buf_unpin_pages(struct kbase_mem_phy_alloc *alloc)
4935 {
4936 	/* In CSF builds, we keep pages pinned until the last reference is
4937 	 * released on the alloc. A refcount of 0 also means we can be sure
4938 	 * that all CPU mappings have been closed on this alloc, and no more
4939 	 * mappings of it will be created.
4940 	 *
4941 	 * Further, the WARN() below captures the restriction that this
4942 	 * function will not handle anything other than the alloc termination
4943 	 * path, because the caller of kbase_mem_phy_alloc_put() is not
4944 	 * required to hold the kctx's reg_lock, and so we could not handle
4945 	 * removing an existing CPU mapping here.
4946 	 *
4947 	 * Refer to this function's kernel-doc comments for alternatives for
4948 	 * unpinning a User buffer.
4949 	 */
4950 
4951 	if (alloc->nents && !WARN(kref_read(&alloc->kref) != 0,
4952 				  "must only be called on terminating an allocation")) {
4953 		struct page **pages = alloc->imported.user_buf.pages;
4954 		long i;
4955 
4956 		WARN_ON(alloc->nents != alloc->imported.user_buf.nr_pages);
4957 
4958 		for (i = 0; i < alloc->nents; i++)
4959 			kbase_unpin_user_buf_page(pages[i]);
4960 
4961 		alloc->nents = 0;
4962 	}
4963 }
4964 #endif
4965 
kbase_jd_user_buf_pin_pages(struct kbase_context * kctx,struct kbase_va_region * reg)4966 int kbase_jd_user_buf_pin_pages(struct kbase_context *kctx,
4967 		struct kbase_va_region *reg)
4968 {
4969 	struct kbase_mem_phy_alloc *alloc = reg->gpu_alloc;
4970 	struct page **pages = alloc->imported.user_buf.pages;
4971 	unsigned long address = alloc->imported.user_buf.address;
4972 	struct mm_struct *mm = alloc->imported.user_buf.mm;
4973 	long pinned_pages;
4974 	long i;
4975 	int write;
4976 
4977 	lockdep_assert_held(&kctx->reg_lock);
4978 
4979 	if (WARN_ON(alloc->type != KBASE_MEM_TYPE_IMPORTED_USER_BUF))
4980 		return -EINVAL;
4981 
4982 	if (alloc->nents) {
4983 		if (WARN_ON(alloc->nents != alloc->imported.user_buf.nr_pages))
4984 			return -EINVAL;
4985 		else
4986 			return 0;
4987 	}
4988 
4989 	if (WARN_ON(reg->gpu_alloc->imported.user_buf.mm != current->mm))
4990 		return -EINVAL;
4991 
4992 	write = reg->flags & (KBASE_REG_CPU_WR | KBASE_REG_GPU_WR);
4993 
4994 #if KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE
4995 	pinned_pages = get_user_pages_remote(NULL, mm, address, alloc->imported.user_buf.nr_pages,
4996 					     write ? FOLL_WRITE : 0, pages, NULL);
4997 #elif KERNEL_VERSION(5, 9, 0) > LINUX_VERSION_CODE
4998 	pinned_pages = get_user_pages_remote(NULL, mm, address, alloc->imported.user_buf.nr_pages,
4999 					     write ? FOLL_WRITE : 0, pages, NULL, NULL);
5000 #else
5001 	pinned_pages = pin_user_pages_remote(mm, address, alloc->imported.user_buf.nr_pages,
5002 					     write ? FOLL_WRITE : 0, pages, NULL, NULL);
5003 #endif
5004 
5005 	if (pinned_pages <= 0)
5006 		return pinned_pages;
5007 
5008 	if (pinned_pages != alloc->imported.user_buf.nr_pages) {
5009 		/* Above code already ensures there will not have been a CPU
5010 		 * mapping by ensuring alloc->nents is 0
5011 		 */
5012 		for (i = 0; i < pinned_pages; i++)
5013 			kbase_unpin_user_buf_page(pages[i]);
5014 		return -ENOMEM;
5015 	}
5016 
5017 	alloc->nents = pinned_pages;
5018 
5019 	return 0;
5020 }
5021 
kbase_jd_user_buf_map(struct kbase_context * kctx,struct kbase_va_region * reg)5022 static int kbase_jd_user_buf_map(struct kbase_context *kctx,
5023 		struct kbase_va_region *reg)
5024 {
5025 	int err;
5026 	long pinned_pages = 0;
5027 	struct kbase_mem_phy_alloc *alloc;
5028 	struct page **pages;
5029 	struct tagged_addr *pa;
5030 	long i, dma_mapped_pages;
5031 	struct device *dev;
5032 	unsigned long gwt_mask = ~0;
5033 	/* Calls to this function are inherently asynchronous, with respect to
5034 	 * MMU operations.
5035 	 */
5036 	const enum kbase_caller_mmu_sync_info mmu_sync_info = CALLER_MMU_ASYNC;
5037 
5038 	lockdep_assert_held(&kctx->reg_lock);
5039 
5040 	err = kbase_jd_user_buf_pin_pages(kctx, reg);
5041 
5042 	if (err)
5043 		return err;
5044 
5045 	alloc = reg->gpu_alloc;
5046 	pa = kbase_get_gpu_phy_pages(reg);
5047 	pinned_pages = alloc->nents;
5048 	pages = alloc->imported.user_buf.pages;
5049 	dev = kctx->kbdev->dev;
5050 
5051 	/* Manual CPU cache synchronization.
5052 	 *
5053 	 * The driver disables automatic CPU cache synchronization because the
5054 	 * memory pages that enclose the imported region may also contain
5055 	 * sub-regions which are not imported and that are allocated and used
5056 	 * by the user process. This may be the case of memory at the beginning
5057 	 * of the first page and at the end of the last page. Automatic CPU cache
5058 	 * synchronization would force some operations on those memory allocations,
5059 	 * unbeknown to the user process: in particular, a CPU cache invalidate
5060 	 * upon unmapping would destroy the content of dirty CPU caches and cause
5061 	 * the user process to lose CPU writes to the non-imported sub-regions.
5062 	 *
5063 	 * When the GPU claims ownership of the imported memory buffer, it shall
5064 	 * commit CPU writes for the whole of all pages that enclose the imported
5065 	 * region, otherwise the initial content of memory would be wrong.
5066 	 */
5067 	for (i = 0; i < pinned_pages; i++) {
5068 		dma_addr_t dma_addr;
5069 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
5070 		dma_addr = dma_map_page(dev, pages[i], 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
5071 #else
5072 		dma_addr = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE, DMA_BIDIRECTIONAL,
5073 					      DMA_ATTR_SKIP_CPU_SYNC);
5074 #endif
5075 		err = dma_mapping_error(dev, dma_addr);
5076 		if (err)
5077 			goto unwind;
5078 
5079 		alloc->imported.user_buf.dma_addrs[i] = dma_addr;
5080 		pa[i] = as_tagged(page_to_phys(pages[i]));
5081 
5082 		dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
5083 	}
5084 
5085 #ifdef CONFIG_MALI_CINSTR_GWT
5086 	if (kctx->gwt_enabled)
5087 		gwt_mask = ~KBASE_REG_GPU_WR;
5088 #endif
5089 
5090 	err = kbase_mmu_insert_imported_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn, pa,
5091 					      kbase_reg_current_backed_size(reg),
5092 					      reg->flags & gwt_mask, kctx->as_nr, alloc->group_id,
5093 					      mmu_sync_info, NULL);
5094 	if (err == 0)
5095 		return 0;
5096 
5097 	/* fall down */
5098 unwind:
5099 	alloc->nents = 0;
5100 	dma_mapped_pages = i;
5101 	/* Run the unmap loop in the same order as map loop, and perform again
5102 	 * CPU cache synchronization to re-write the content of dirty CPU caches
5103 	 * to memory. This is precautionary measure in case a GPU job has taken
5104 	 * advantage of a partially GPU-mapped range to write and corrupt the
5105 	 * content of memory, either inside or outside the imported region.
5106 	 *
5107 	 * Notice that this error recovery path doesn't try to be optimal and just
5108 	 * flushes the entire page range.
5109 	 */
5110 	for (i = 0; i < dma_mapped_pages; i++) {
5111 		dma_addr_t dma_addr = alloc->imported.user_buf.dma_addrs[i];
5112 
5113 		dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
5114 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
5115 		dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
5116 #else
5117 		dma_unmap_page_attrs(dev, dma_addr, PAGE_SIZE, DMA_BIDIRECTIONAL,
5118 				     DMA_ATTR_SKIP_CPU_SYNC);
5119 #endif
5120 	}
5121 
5122 	/* The user buffer could already have been previously pinned before
5123 	 * entering this function, and hence there could potentially be CPU
5124 	 * mappings of it
5125 	 */
5126 	kbase_mem_shrink_cpu_mapping(kctx, reg, 0, pinned_pages);
5127 
5128 	for (i = 0; i < pinned_pages; i++) {
5129 		kbase_unpin_user_buf_page(pages[i]);
5130 		pages[i] = NULL;
5131 	}
5132 
5133 	return err;
5134 }
5135 
5136 /* This function would also perform the work of unpinning pages on Job Manager
5137  * GPUs, which implies that a call to kbase_jd_user_buf_pin_pages() will NOT
5138  * have a corresponding call to kbase_jd_user_buf_unpin_pages().
5139  */
kbase_jd_user_buf_unmap(struct kbase_context * kctx,struct kbase_mem_phy_alloc * alloc,struct kbase_va_region * reg,bool writeable)5140 static void kbase_jd_user_buf_unmap(struct kbase_context *kctx, struct kbase_mem_phy_alloc *alloc,
5141 				    struct kbase_va_region *reg, bool writeable)
5142 {
5143 	long i;
5144 	struct page **pages;
5145 	unsigned long offset_within_page = alloc->imported.user_buf.address & ~PAGE_MASK;
5146 	unsigned long remaining_size = alloc->imported.user_buf.size;
5147 
5148 	lockdep_assert_held(&kctx->reg_lock);
5149 
5150 	KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF);
5151 	pages = alloc->imported.user_buf.pages;
5152 
5153 #if !MALI_USE_CSF
5154 	kbase_mem_shrink_cpu_mapping(kctx, reg, 0, alloc->nents);
5155 #else
5156 	CSTD_UNUSED(reg);
5157 #endif
5158 
5159 	for (i = 0; i < alloc->imported.user_buf.nr_pages; i++) {
5160 		unsigned long imported_size = MIN(remaining_size, PAGE_SIZE - offset_within_page);
5161 		/* Notice: this is a temporary variable that is used for DMA sync
5162 		 * operations, and that could be incremented by an offset if the
5163 		 * current page contains both imported and non-imported memory
5164 		 * sub-regions.
5165 		 *
5166 		 * It is valid to add an offset to this value, because the offset
5167 		 * is always kept within the physically contiguous dma-mapped range
5168 		 * and there's no need to translate to physical address to offset it.
5169 		 *
5170 		 * This variable is not going to be used for the actual DMA unmap
5171 		 * operation, that shall always use the original DMA address of the
5172 		 * whole memory page.
5173 		 */
5174 		dma_addr_t dma_addr = alloc->imported.user_buf.dma_addrs[i];
5175 
5176 		/* Manual CPU cache synchronization.
5177 		 *
5178 		 * When the GPU returns ownership of the buffer to the CPU, the driver
5179 		 * needs to treat imported and non-imported memory differently.
5180 		 *
5181 		 * The first case to consider is non-imported sub-regions at the
5182 		 * beginning of the first page and at the end of last page. For these
5183 		 * sub-regions: CPU cache shall be committed with a clean+invalidate,
5184 		 * in order to keep the last CPU write.
5185 		 *
5186 		 * Imported region prefers the opposite treatment: this memory has been
5187 		 * legitimately mapped and used by the GPU, hence GPU writes shall be
5188 		 * committed to memory, while CPU cache shall be invalidated to make
5189 		 * sure that CPU reads the correct memory content.
5190 		 *
5191 		 * The following diagram shows the expect value of the variables
5192 		 * used in this loop in the corner case of an imported region encloed
5193 		 * by a single memory page:
5194 		 *
5195 		 * page boundary ->|---------- | <- dma_addr (initial value)
5196 		 *                 |           |
5197 		 *                 | - - - - - | <- offset_within_page
5198 		 *                 |XXXXXXXXXXX|\
5199 		 *                 |XXXXXXXXXXX| \
5200 		 *                 |XXXXXXXXXXX|  }- imported_size
5201 		 *                 |XXXXXXXXXXX| /
5202 		 *                 |XXXXXXXXXXX|/
5203 		 *                 | - - - - - | <- offset_within_page + imported_size
5204 		 *                 |           |\
5205 		 *                 |           | }- PAGE_SIZE - imported_size - offset_within_page
5206 		 *                 |           |/
5207 		 * page boundary ->|-----------|
5208 		 *
5209 		 * If the imported region is enclosed by more than one page, then
5210 		 * offset_within_page = 0 for any page after the first.
5211 		 */
5212 
5213 		/* Only for first page: handle non-imported range at the beginning. */
5214 		if (offset_within_page > 0) {
5215 			dma_sync_single_for_device(kctx->kbdev->dev, dma_addr, offset_within_page,
5216 						   DMA_BIDIRECTIONAL);
5217 			dma_addr += offset_within_page;
5218 		}
5219 
5220 		/* For every page: handle imported range. */
5221 		if (imported_size > 0)
5222 			dma_sync_single_for_cpu(kctx->kbdev->dev, dma_addr, imported_size,
5223 						DMA_BIDIRECTIONAL);
5224 
5225 		/* Only for last page (that may coincide with first page):
5226 		 * handle non-imported range at the end.
5227 		 */
5228 		if ((imported_size + offset_within_page) < PAGE_SIZE) {
5229 			dma_addr += imported_size;
5230 			dma_sync_single_for_device(kctx->kbdev->dev, dma_addr,
5231 						   PAGE_SIZE - imported_size - offset_within_page,
5232 						   DMA_BIDIRECTIONAL);
5233 		}
5234 
5235 		/* Notice: use the original DMA address to unmap the whole memory page. */
5236 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
5237 		dma_unmap_page(kctx->kbdev->dev, alloc->imported.user_buf.dma_addrs[i], PAGE_SIZE,
5238 			       DMA_BIDIRECTIONAL);
5239 #else
5240 		dma_unmap_page_attrs(kctx->kbdev->dev, alloc->imported.user_buf.dma_addrs[i],
5241 				     PAGE_SIZE, DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
5242 #endif
5243 		if (writeable)
5244 			set_page_dirty_lock(pages[i]);
5245 #if !MALI_USE_CSF
5246 		kbase_unpin_user_buf_page(pages[i]);
5247 		pages[i] = NULL;
5248 #endif
5249 
5250 		remaining_size -= imported_size;
5251 		offset_within_page = 0;
5252 	}
5253 #if !MALI_USE_CSF
5254 	alloc->nents = 0;
5255 #endif
5256 }
5257 
kbase_mem_copy_to_pinned_user_pages(struct page ** dest_pages,void * src_page,size_t * to_copy,unsigned int nr_pages,unsigned int * target_page_nr,size_t offset)5258 int kbase_mem_copy_to_pinned_user_pages(struct page **dest_pages,
5259 		void *src_page, size_t *to_copy, unsigned int nr_pages,
5260 		unsigned int *target_page_nr, size_t offset)
5261 {
5262 	void *target_page = kmap(dest_pages[*target_page_nr]);
5263 	size_t chunk = PAGE_SIZE-offset;
5264 
5265 	if (!target_page) {
5266 		pr_err("%s: kmap failure", __func__);
5267 		return -ENOMEM;
5268 	}
5269 
5270 	chunk = min(chunk, *to_copy);
5271 
5272 	memcpy(target_page + offset, src_page, chunk);
5273 	*to_copy -= chunk;
5274 
5275 	kunmap(dest_pages[*target_page_nr]);
5276 
5277 	*target_page_nr += 1;
5278 	if (*target_page_nr >= nr_pages || *to_copy == 0)
5279 		return 0;
5280 
5281 	target_page = kmap(dest_pages[*target_page_nr]);
5282 	if (!target_page) {
5283 		pr_err("%s: kmap failure", __func__);
5284 		return -ENOMEM;
5285 	}
5286 
5287 	KBASE_DEBUG_ASSERT(target_page);
5288 
5289 	chunk = min(offset, *to_copy);
5290 	memcpy(target_page, src_page + PAGE_SIZE-offset, chunk);
5291 	*to_copy -= chunk;
5292 
5293 	kunmap(dest_pages[*target_page_nr]);
5294 
5295 	return 0;
5296 }
5297 
kbase_map_external_resource(struct kbase_context * kctx,struct kbase_va_region * reg,struct mm_struct * locked_mm)5298 int kbase_map_external_resource(struct kbase_context *kctx, struct kbase_va_region *reg,
5299 				struct mm_struct *locked_mm)
5300 {
5301 	int err = 0;
5302 	struct kbase_mem_phy_alloc *alloc = reg->gpu_alloc;
5303 
5304 	lockdep_assert_held(&kctx->reg_lock);
5305 
5306 	/* decide what needs to happen for this resource */
5307 	switch (reg->gpu_alloc->type) {
5308 	case KBASE_MEM_TYPE_IMPORTED_USER_BUF: {
5309 		if ((reg->gpu_alloc->imported.user_buf.mm != locked_mm) &&
5310 		    (!reg->gpu_alloc->nents))
5311 			return -EINVAL;
5312 
5313 		reg->gpu_alloc->imported.user_buf.current_mapping_usage_count++;
5314 		if (reg->gpu_alloc->imported.user_buf
5315 			    .current_mapping_usage_count == 1) {
5316 			err = kbase_jd_user_buf_map(kctx, reg);
5317 			if (err) {
5318 				reg->gpu_alloc->imported.user_buf.current_mapping_usage_count--;
5319 				return err;
5320 			}
5321 		}
5322 	}
5323 	break;
5324 	case KBASE_MEM_TYPE_IMPORTED_UMM: {
5325 		err = kbase_mem_umm_map(kctx, reg);
5326 		if (err)
5327 			return err;
5328 		break;
5329 	}
5330 	default:
5331 		dev_dbg(kctx->kbdev->dev,
5332 			"Invalid external resource GPU allocation type (%x) on mapping",
5333 			alloc->type);
5334 		return -EINVAL;
5335 	}
5336 
5337 	kbase_va_region_alloc_get(kctx, reg);
5338 	kbase_mem_phy_alloc_get(alloc);
5339 	return err;
5340 }
5341 
kbase_unmap_external_resource(struct kbase_context * kctx,struct kbase_va_region * reg)5342 void kbase_unmap_external_resource(struct kbase_context *kctx, struct kbase_va_region *reg)
5343 {
5344 	/* gpu_alloc was used in kbase_map_external_resources, so we need to use it for the
5345 	 * unmapping operation.
5346 	 */
5347 	struct kbase_mem_phy_alloc *alloc = reg->gpu_alloc;
5348 
5349 	lockdep_assert_held(&kctx->reg_lock);
5350 
5351 	switch (alloc->type) {
5352 	case KBASE_MEM_TYPE_IMPORTED_UMM: {
5353 		kbase_mem_umm_unmap(kctx, reg, alloc);
5354 	}
5355 	break;
5356 	case KBASE_MEM_TYPE_IMPORTED_USER_BUF: {
5357 		alloc->imported.user_buf.current_mapping_usage_count--;
5358 
5359 		if (alloc->imported.user_buf.current_mapping_usage_count == 0) {
5360 			bool writeable = true;
5361 
5362 			if (!kbase_is_region_invalid_or_free(reg)) {
5363 				kbase_mmu_teardown_pages(kctx->kbdev, &kctx->mmu, reg->start_pfn,
5364 							 alloc->pages,
5365 							 kbase_reg_current_backed_size(reg),
5366 							 kbase_reg_current_backed_size(reg),
5367 							 kctx->as_nr, true);
5368 			}
5369 
5370 			if ((reg->flags & (KBASE_REG_CPU_WR | KBASE_REG_GPU_WR)) == 0)
5371 				writeable = false;
5372 
5373 			kbase_jd_user_buf_unmap(kctx, alloc, reg, writeable);
5374 		}
5375 		}
5376 	break;
5377 	default:
5378 		WARN(1, "Invalid external resource GPU allocation type (%x) on unmapping",
5379 		     alloc->type);
5380 		return;
5381 	}
5382 	kbase_mem_phy_alloc_put(alloc);
5383 	kbase_va_region_alloc_put(kctx, reg);
5384 }
5385 
kbasep_get_va_gpu_addr(struct kbase_va_region * reg)5386 static inline u64 kbasep_get_va_gpu_addr(struct kbase_va_region *reg)
5387 {
5388 	return reg->start_pfn << PAGE_SHIFT;
5389 }
5390 
kbase_sticky_resource_acquire(struct kbase_context * kctx,u64 gpu_addr)5391 struct kbase_ctx_ext_res_meta *kbase_sticky_resource_acquire(
5392 		struct kbase_context *kctx, u64 gpu_addr)
5393 {
5394 	struct kbase_ctx_ext_res_meta *meta = NULL;
5395 	struct kbase_ctx_ext_res_meta *walker;
5396 
5397 	lockdep_assert_held(&kctx->reg_lock);
5398 
5399 	/*
5400 	 * Walk the per context external resource metadata list for the
5401 	 * metadata which matches the region which is being acquired.
5402 	 */
5403 	list_for_each_entry(walker, &kctx->ext_res_meta_head, ext_res_node) {
5404 		if (kbasep_get_va_gpu_addr(walker->reg) == gpu_addr) {
5405 			meta = walker;
5406 			meta->ref++;
5407 			break;
5408 		}
5409 	}
5410 
5411 	/* No metadata exists so create one. */
5412 	if (!meta) {
5413 		struct kbase_va_region *reg;
5414 
5415 		/* Find the region */
5416 		reg = kbase_region_tracker_find_region_enclosing_address(kctx, gpu_addr);
5417 		if (kbase_is_region_invalid_or_free(reg))
5418 			goto failed;
5419 
5420 		/* Allocate the metadata object */
5421 		meta = kzalloc(sizeof(*meta), GFP_KERNEL);
5422 		if (!meta)
5423 			goto failed;
5424 		/*
5425 		 * Fill in the metadata object and acquire a reference
5426 		 * for the physical resource.
5427 		 */
5428 		meta->reg = reg;
5429 
5430 		/* Map the external resource to the GPU allocation of the region
5431 		 * and acquire the reference to the VA region
5432 		 */
5433 		if (kbase_map_external_resource(kctx, meta->reg, NULL))
5434 			goto fail_map;
5435 		meta->ref = 1;
5436 
5437 		list_add(&meta->ext_res_node, &kctx->ext_res_meta_head);
5438 	}
5439 
5440 	return meta;
5441 
5442 fail_map:
5443 	kfree(meta);
5444 failed:
5445 	return NULL;
5446 }
5447 
5448 static struct kbase_ctx_ext_res_meta *
find_sticky_resource_meta(struct kbase_context * kctx,u64 gpu_addr)5449 find_sticky_resource_meta(struct kbase_context *kctx, u64 gpu_addr)
5450 {
5451 	struct kbase_ctx_ext_res_meta *walker;
5452 
5453 	lockdep_assert_held(&kctx->reg_lock);
5454 
5455 	/*
5456 	 * Walk the per context external resource metadata list for the
5457 	 * metadata which matches the region which is being released.
5458 	 */
5459 	list_for_each_entry(walker, &kctx->ext_res_meta_head, ext_res_node)
5460 		if (kbasep_get_va_gpu_addr(walker->reg) == gpu_addr)
5461 			return walker;
5462 
5463 	return NULL;
5464 }
5465 
release_sticky_resource_meta(struct kbase_context * kctx,struct kbase_ctx_ext_res_meta * meta)5466 static void release_sticky_resource_meta(struct kbase_context *kctx,
5467 		struct kbase_ctx_ext_res_meta *meta)
5468 {
5469 	kbase_unmap_external_resource(kctx, meta->reg);
5470 	list_del(&meta->ext_res_node);
5471 	kfree(meta);
5472 }
5473 
kbase_sticky_resource_release(struct kbase_context * kctx,struct kbase_ctx_ext_res_meta * meta,u64 gpu_addr)5474 bool kbase_sticky_resource_release(struct kbase_context *kctx,
5475 		struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr)
5476 {
5477 	lockdep_assert_held(&kctx->reg_lock);
5478 
5479 	/* Search of the metadata if one isn't provided. */
5480 	if (!meta)
5481 		meta = find_sticky_resource_meta(kctx, gpu_addr);
5482 
5483 	/* No metadata so just return. */
5484 	if (!meta)
5485 		return false;
5486 
5487 	if (--meta->ref != 0)
5488 		return true;
5489 
5490 	release_sticky_resource_meta(kctx, meta);
5491 
5492 	return true;
5493 }
5494 
kbase_sticky_resource_release_force(struct kbase_context * kctx,struct kbase_ctx_ext_res_meta * meta,u64 gpu_addr)5495 bool kbase_sticky_resource_release_force(struct kbase_context *kctx,
5496 		struct kbase_ctx_ext_res_meta *meta, u64 gpu_addr)
5497 {
5498 	lockdep_assert_held(&kctx->reg_lock);
5499 
5500 	/* Search of the metadata if one isn't provided. */
5501 	if (!meta)
5502 		meta = find_sticky_resource_meta(kctx, gpu_addr);
5503 
5504 	/* No metadata so just return. */
5505 	if (!meta)
5506 		return false;
5507 
5508 	release_sticky_resource_meta(kctx, meta);
5509 
5510 	return true;
5511 }
5512 
kbase_sticky_resource_init(struct kbase_context * kctx)5513 int kbase_sticky_resource_init(struct kbase_context *kctx)
5514 {
5515 	INIT_LIST_HEAD(&kctx->ext_res_meta_head);
5516 
5517 	return 0;
5518 }
5519 
kbase_sticky_resource_term(struct kbase_context * kctx)5520 void kbase_sticky_resource_term(struct kbase_context *kctx)
5521 {
5522 	struct kbase_ctx_ext_res_meta *walker;
5523 
5524 	lockdep_assert_held(&kctx->reg_lock);
5525 
5526 	/*
5527 	 * Free any sticky resources which haven't been unmapped.
5528 	 *
5529 	 * Note:
5530 	 * We don't care about refcounts at this point as no future
5531 	 * references to the meta data will be made.
5532 	 * Region termination would find these if we didn't free them
5533 	 * here, but it's more efficient if we do the clean up here.
5534 	 */
5535 	while (!list_empty(&kctx->ext_res_meta_head)) {
5536 		walker = list_first_entry(&kctx->ext_res_meta_head,
5537 				struct kbase_ctx_ext_res_meta, ext_res_node);
5538 
5539 		kbase_sticky_resource_release_force(kctx, walker, 0);
5540 	}
5541 }
5542