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