.. | .. |
---|
1 | 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ |
---|
2 | 2 | /* |
---|
3 | 3 | * |
---|
4 | | - * (C) COPYRIGHT 2010-2021 ARM Limited. All rights reserved. |
---|
| 4 | + * (C) COPYRIGHT 2010-2023 ARM Limited. All rights reserved. |
---|
5 | 5 | * |
---|
6 | 6 | * This program is free software and is provided to you under the terms of the |
---|
7 | 7 | * GNU General Public License version 2 as published by the Free Software |
---|
.. | .. |
---|
37 | 37 | #include "mali_kbase_defs.h" |
---|
38 | 38 | /* Required for kbase_mem_evictable_unmake */ |
---|
39 | 39 | #include "mali_kbase_mem_linux.h" |
---|
| 40 | +#include "mali_kbase_mem_migrate.h" |
---|
| 41 | +#include "mali_kbase_refcount_defs.h" |
---|
40 | 42 | |
---|
41 | 43 | static inline void kbase_process_page_usage_inc(struct kbase_context *kctx, |
---|
42 | 44 | int pages); |
---|
.. | .. |
---|
182 | 184 | } imported; |
---|
183 | 185 | }; |
---|
184 | 186 | |
---|
| 187 | +/** |
---|
| 188 | + * enum kbase_page_status - Status of a page used for page migration. |
---|
| 189 | + * |
---|
| 190 | + * @MEM_POOL: Stable state. Page is located in a memory pool and can safely |
---|
| 191 | + * be migrated. |
---|
| 192 | + * @ALLOCATE_IN_PROGRESS: Transitory state. A page is set to this status as |
---|
| 193 | + * soon as it leaves a memory pool. |
---|
| 194 | + * @SPILL_IN_PROGRESS: Transitory state. Corner case where pages in a memory |
---|
| 195 | + * pool of a dying context are being moved to the device |
---|
| 196 | + * memory pool. |
---|
| 197 | + * @NOT_MOVABLE: Stable state. Page has been allocated for an object that is |
---|
| 198 | + * not movable, but may return to be movable when the object |
---|
| 199 | + * is freed. |
---|
| 200 | + * @ALLOCATED_MAPPED: Stable state. Page has been allocated, mapped to GPU |
---|
| 201 | + * and has reference to kbase_mem_phy_alloc object. |
---|
| 202 | + * @PT_MAPPED: Stable state. Similar to ALLOCATED_MAPPED, but page doesn't |
---|
| 203 | + * reference kbase_mem_phy_alloc object. Used as a page in MMU |
---|
| 204 | + * page table. |
---|
| 205 | + * @FREE_IN_PROGRESS: Transitory state. A page is set to this status as soon as |
---|
| 206 | + * the driver manages to acquire a lock on the page while |
---|
| 207 | + * unmapping it. This status means that a memory release is |
---|
| 208 | + * happening and it's still not complete. |
---|
| 209 | + * @FREE_ISOLATED_IN_PROGRESS: Transitory state. This is a very particular corner case. |
---|
| 210 | + * A page is isolated while it is in ALLOCATED_MAPPED state, |
---|
| 211 | + * but then the driver tries to destroy the allocation. |
---|
| 212 | + * @FREE_PT_ISOLATED_IN_PROGRESS: Transitory state. This is a very particular corner case. |
---|
| 213 | + * A page is isolated while it is in PT_MAPPED state, but |
---|
| 214 | + * then the driver tries to destroy the allocation. |
---|
| 215 | + * |
---|
| 216 | + * Pages can only be migrated in stable states. |
---|
| 217 | + */ |
---|
| 218 | +enum kbase_page_status { |
---|
| 219 | + MEM_POOL = 0, |
---|
| 220 | + ALLOCATE_IN_PROGRESS, |
---|
| 221 | + SPILL_IN_PROGRESS, |
---|
| 222 | + NOT_MOVABLE, |
---|
| 223 | + ALLOCATED_MAPPED, |
---|
| 224 | + PT_MAPPED, |
---|
| 225 | + FREE_IN_PROGRESS, |
---|
| 226 | + FREE_ISOLATED_IN_PROGRESS, |
---|
| 227 | + FREE_PT_ISOLATED_IN_PROGRESS, |
---|
| 228 | +}; |
---|
| 229 | + |
---|
| 230 | +#define PGD_VPFN_LEVEL_MASK ((u64)0x3) |
---|
| 231 | +#define PGD_VPFN_LEVEL_GET_LEVEL(pgd_vpfn_level) (pgd_vpfn_level & PGD_VPFN_LEVEL_MASK) |
---|
| 232 | +#define PGD_VPFN_LEVEL_GET_VPFN(pgd_vpfn_level) (pgd_vpfn_level & ~PGD_VPFN_LEVEL_MASK) |
---|
| 233 | +#define PGD_VPFN_LEVEL_SET(pgd_vpfn, level) \ |
---|
| 234 | + ((pgd_vpfn & ~PGD_VPFN_LEVEL_MASK) | (level & PGD_VPFN_LEVEL_MASK)) |
---|
| 235 | + |
---|
| 236 | +/** |
---|
| 237 | + * struct kbase_page_metadata - Metadata for each page in kbase |
---|
| 238 | + * |
---|
| 239 | + * @kbdev: Pointer to kbase device. |
---|
| 240 | + * @dma_addr: DMA address mapped to page. |
---|
| 241 | + * @migrate_lock: A spinlock to protect the private metadata. |
---|
| 242 | + * @data: Member in union valid based on @status. |
---|
| 243 | + * @status: Status to keep track if page can be migrated at any |
---|
| 244 | + * given moment. MSB will indicate if page is isolated. |
---|
| 245 | + * Protected by @migrate_lock. |
---|
| 246 | + * @vmap_count: Counter of kernel mappings. |
---|
| 247 | + * @group_id: Memory group ID obtained at the time of page allocation. |
---|
| 248 | + * |
---|
| 249 | + * Each 4KB page will have a reference to this struct in the private field. |
---|
| 250 | + * This will be used to keep track of information required for Linux page |
---|
| 251 | + * migration functionality as well as address for DMA mapping. |
---|
| 252 | + */ |
---|
| 253 | +struct kbase_page_metadata { |
---|
| 254 | + dma_addr_t dma_addr; |
---|
| 255 | + spinlock_t migrate_lock; |
---|
| 256 | + |
---|
| 257 | + union { |
---|
| 258 | + struct { |
---|
| 259 | + struct kbase_mem_pool *pool; |
---|
| 260 | + /* Pool could be terminated after page is isolated and therefore |
---|
| 261 | + * won't be able to get reference to kbase device. |
---|
| 262 | + */ |
---|
| 263 | + struct kbase_device *kbdev; |
---|
| 264 | + } mem_pool; |
---|
| 265 | + struct { |
---|
| 266 | + struct kbase_va_region *reg; |
---|
| 267 | + struct kbase_mmu_table *mmut; |
---|
| 268 | + u64 vpfn; |
---|
| 269 | + } mapped; |
---|
| 270 | + struct { |
---|
| 271 | + struct kbase_mmu_table *mmut; |
---|
| 272 | + u64 pgd_vpfn_level; |
---|
| 273 | + } pt_mapped; |
---|
| 274 | + struct { |
---|
| 275 | + struct kbase_device *kbdev; |
---|
| 276 | + } free_isolated; |
---|
| 277 | + struct { |
---|
| 278 | + struct kbase_device *kbdev; |
---|
| 279 | + } free_pt_isolated; |
---|
| 280 | + } data; |
---|
| 281 | + |
---|
| 282 | + u8 status; |
---|
| 283 | + u8 vmap_count; |
---|
| 284 | + u8 group_id; |
---|
| 285 | +}; |
---|
| 286 | + |
---|
185 | 287 | /* The top bit of kbase_alloc_import_user_buf::current_mapping_usage_count is |
---|
186 | 288 | * used to signify that a buffer was pinned when it was imported. Since the |
---|
187 | 289 | * reference count is limited by the number of atoms that can be submitted at |
---|
.. | .. |
---|
204 | 306 | KBASE_JIT_REPORT_ON_ALLOC_OR_FREE = (1u << 0) |
---|
205 | 307 | }; |
---|
206 | 308 | |
---|
| 309 | +/** |
---|
| 310 | + * kbase_set_phy_alloc_page_status - Set the page migration status of the underlying |
---|
| 311 | + * physical allocation. |
---|
| 312 | + * @alloc: the physical allocation containing the pages whose metadata is going |
---|
| 313 | + * to be modified |
---|
| 314 | + * @status: the status the pages should end up in |
---|
| 315 | + * |
---|
| 316 | + * Note that this function does not go through all of the checking to ensure that |
---|
| 317 | + * proper states are set. Instead, it is only used when we change the allocation |
---|
| 318 | + * to NOT_MOVABLE or from NOT_MOVABLE to ALLOCATED_MAPPED |
---|
| 319 | + */ |
---|
| 320 | +void kbase_set_phy_alloc_page_status(struct kbase_mem_phy_alloc *alloc, |
---|
| 321 | + enum kbase_page_status status); |
---|
| 322 | + |
---|
207 | 323 | static inline void kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc *alloc) |
---|
208 | 324 | { |
---|
209 | 325 | KBASE_DEBUG_ASSERT(alloc); |
---|
.. | .. |
---|
224 | 340 | } |
---|
225 | 341 | |
---|
226 | 342 | /** |
---|
227 | | - * kbase_mem_phy_alloc_kernel_mapped - Increment kernel_mappings |
---|
228 | | - * counter for a memory region to prevent commit and flag changes |
---|
| 343 | + * kbase_mem_phy_alloc_kernel_mapped - Increment kernel_mappings counter for a |
---|
| 344 | + * memory region to prevent commit and flag |
---|
| 345 | + * changes |
---|
229 | 346 | * |
---|
230 | 347 | * @alloc: Pointer to physical pages tracking object |
---|
231 | 348 | */ |
---|
.. | .. |
---|
287 | 404 | * that triggered incremental rendering by growing too much. |
---|
288 | 405 | * @rbtree: Backlink to the red-black tree of memory regions. |
---|
289 | 406 | * @start_pfn: The Page Frame Number in GPU virtual address space. |
---|
| 407 | + * @user_data: The address of GPU command queue when VA region represents |
---|
| 408 | + * a ring buffer. |
---|
290 | 409 | * @nr_pages: The size of the region in pages. |
---|
291 | 410 | * @initial_commit: Initial commit, for aligning the start address and |
---|
292 | 411 | * correctly growing KBASE_REG_TILER_ALIGN_TOP regions. |
---|
.. | .. |
---|
301 | 420 | * @jit_usage_id: The last just-in-time memory usage ID for this region. |
---|
302 | 421 | * @jit_bin_id: The just-in-time memory bin this region came from. |
---|
303 | 422 | * @va_refcnt: Number of users of this region. Protected by reg_lock. |
---|
| 423 | + * @no_user_free_count: Number of contexts that want to prevent the region |
---|
| 424 | + * from being freed by userspace. |
---|
304 | 425 | * @heap_info_gpu_addr: Pointer to an object in GPU memory defining an end of |
---|
305 | 426 | * an allocated region |
---|
306 | 427 | * The object can be one of: |
---|
.. | .. |
---|
324 | 445 | struct list_head link; |
---|
325 | 446 | struct rb_root *rbtree; |
---|
326 | 447 | u64 start_pfn; |
---|
| 448 | + void *user_data; |
---|
327 | 449 | size_t nr_pages; |
---|
328 | 450 | size_t initial_commit; |
---|
329 | 451 | size_t threshold_pages; |
---|
.. | .. |
---|
356 | 478 | /* inner & outer shareable coherency */ |
---|
357 | 479 | #define KBASE_REG_SHARE_BOTH (1ul << 10) |
---|
358 | 480 | |
---|
| 481 | +#if MALI_USE_CSF |
---|
| 482 | +/* Space for 8 different zones */ |
---|
| 483 | +#define KBASE_REG_ZONE_BITS 3 |
---|
| 484 | +#else |
---|
359 | 485 | /* Space for 4 different zones */ |
---|
360 | | -#define KBASE_REG_ZONE_MASK ((KBASE_REG_ZONE_MAX - 1ul) << 11) |
---|
361 | | -#define KBASE_REG_ZONE(x) (((x) & (KBASE_REG_ZONE_MAX - 1ul)) << 11) |
---|
| 486 | +#define KBASE_REG_ZONE_BITS 2 |
---|
| 487 | +#endif |
---|
| 488 | + |
---|
| 489 | +#define KBASE_REG_ZONE_MASK (((1 << KBASE_REG_ZONE_BITS) - 1ul) << 11) |
---|
| 490 | +#define KBASE_REG_ZONE(x) (((x) & ((1 << KBASE_REG_ZONE_BITS) - 1ul)) << 11) |
---|
362 | 491 | #define KBASE_REG_ZONE_IDX(x) (((x) & KBASE_REG_ZONE_MASK) >> 11) |
---|
363 | 492 | |
---|
364 | | -#if ((KBASE_REG_ZONE_MAX - 1) & 0x3) != (KBASE_REG_ZONE_MAX - 1) |
---|
365 | | -#error KBASE_REG_ZONE_MAX too large for allocation of KBASE_REG_<...> bits |
---|
| 493 | +#if KBASE_REG_ZONE_MAX > (1 << KBASE_REG_ZONE_BITS) |
---|
| 494 | +#error "Too many zones for the number of zone bits defined" |
---|
366 | 495 | #endif |
---|
367 | 496 | |
---|
368 | 497 | /* GPU read access */ |
---|
369 | | -#define KBASE_REG_GPU_RD (1ul<<13) |
---|
| 498 | +#define KBASE_REG_GPU_RD (1ul << 14) |
---|
370 | 499 | /* CPU read access */ |
---|
371 | | -#define KBASE_REG_CPU_RD (1ul<<14) |
---|
| 500 | +#define KBASE_REG_CPU_RD (1ul << 15) |
---|
372 | 501 | |
---|
373 | 502 | /* Index of chosen MEMATTR for this region (0..7) */ |
---|
374 | 503 | #define KBASE_REG_MEMATTR_MASK (7ul << 16) |
---|
.. | .. |
---|
377 | 506 | |
---|
378 | 507 | #define KBASE_REG_PROTECTED (1ul << 19) |
---|
379 | 508 | |
---|
| 509 | +/* Region belongs to a shrinker. |
---|
| 510 | + * |
---|
| 511 | + * This can either mean that it is part of the JIT/Ephemeral or tiler heap |
---|
| 512 | + * shrinker paths. Should be removed only after making sure that there are |
---|
| 513 | + * no references remaining to it in these paths, as it may cause the physical |
---|
| 514 | + * backing of the region to disappear during use. |
---|
| 515 | + */ |
---|
380 | 516 | #define KBASE_REG_DONT_NEED (1ul << 20) |
---|
381 | 517 | |
---|
382 | 518 | /* Imported buffer is padded? */ |
---|
.. | .. |
---|
406 | 542 | #define KBASE_REG_RESERVED_BIT_23 (1ul << 23) |
---|
407 | 543 | #endif /* !MALI_USE_CSF */ |
---|
408 | 544 | |
---|
409 | | -/* Whilst this flag is set the GPU allocation is not supposed to be freed by |
---|
410 | | - * user space. The flag will remain set for the lifetime of JIT allocations. |
---|
411 | | - */ |
---|
412 | | -#define KBASE_REG_NO_USER_FREE (1ul << 24) |
---|
| 545 | +/* Bit 24 is currently unused and is available for use for a new flag */ |
---|
413 | 546 | |
---|
414 | 547 | /* Memory has permanent kernel side mapping */ |
---|
415 | 548 | #define KBASE_REG_PERMANENT_KERNEL_MAPPING (1ul << 25) |
---|
.. | .. |
---|
439 | 572 | /* Allocation is actively used for JIT memory */ |
---|
440 | 573 | #define KBASE_REG_ACTIVE_JIT_ALLOC (1ul << 28) |
---|
441 | 574 | |
---|
442 | | -#define KBASE_REG_ZONE_SAME_VA KBASE_REG_ZONE(0) |
---|
443 | | - |
---|
444 | | -/* only used with 32-bit clients */ |
---|
445 | | -/* |
---|
446 | | - * On a 32bit platform, custom VA should be wired from 4GB |
---|
447 | | - * to the VA limit of the GPU. Unfortunately, the Linux mmap() interface |
---|
448 | | - * limits us to 2^32 pages (2^44 bytes, see mmap64 man page for reference). |
---|
449 | | - * So we put the default limit to the maximum possible on Linux and shrink |
---|
450 | | - * it down, if required by the GPU, during initialization. |
---|
| 575 | +#if MALI_USE_CSF |
---|
| 576 | +/* This flag only applies to allocations in the EXEC_FIXED_VA and FIXED_VA |
---|
| 577 | + * memory zones, and it determines whether they were created with a fixed |
---|
| 578 | + * GPU VA address requested by the user. |
---|
451 | 579 | */ |
---|
| 580 | +#define KBASE_REG_FIXED_ADDRESS (1ul << 29) |
---|
| 581 | +#else |
---|
| 582 | +#define KBASE_REG_RESERVED_BIT_29 (1ul << 29) |
---|
| 583 | +#endif |
---|
| 584 | + |
---|
| 585 | +#define KBASE_REG_ZONE_SAME_VA KBASE_REG_ZONE(0) |
---|
452 | 586 | |
---|
453 | 587 | #define KBASE_REG_ZONE_CUSTOM_VA KBASE_REG_ZONE(1) |
---|
454 | 588 | #define KBASE_REG_ZONE_CUSTOM_VA_BASE (0x100000000ULL >> PAGE_SHIFT) |
---|
455 | | -#define KBASE_REG_ZONE_CUSTOM_VA_SIZE (((1ULL << 44) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE) |
---|
| 589 | + |
---|
| 590 | +#if MALI_USE_CSF |
---|
| 591 | +/* only used with 32-bit clients */ |
---|
| 592 | +/* On a 32bit platform, custom VA should be wired from 4GB to 2^(43). |
---|
| 593 | + */ |
---|
| 594 | +#define KBASE_REG_ZONE_CUSTOM_VA_SIZE \ |
---|
| 595 | + (((1ULL << 43) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE) |
---|
| 596 | +#else |
---|
| 597 | +/* only used with 32-bit clients */ |
---|
| 598 | +/* On a 32bit platform, custom VA should be wired from 4GB to the VA limit of the |
---|
| 599 | + * GPU. Unfortunately, the Linux mmap() interface limits us to 2^32 pages (2^44 |
---|
| 600 | + * bytes, see mmap64 man page for reference). So we put the default limit to the |
---|
| 601 | + * maximum possible on Linux and shrink it down, if required by the GPU, during |
---|
| 602 | + * initialization. |
---|
| 603 | + */ |
---|
| 604 | +#define KBASE_REG_ZONE_CUSTOM_VA_SIZE \ |
---|
| 605 | + (((1ULL << 44) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE) |
---|
456 | 606 | /* end 32-bit clients only */ |
---|
| 607 | +#endif |
---|
457 | 608 | |
---|
458 | 609 | /* The starting address and size of the GPU-executable zone are dynamic |
---|
459 | 610 | * and depend on the platform and the number of pages requested by the |
---|
.. | .. |
---|
467 | 618 | #define KBASE_REG_ZONE_MCU_SHARED_BASE (0x04000000ULL >> PAGE_SHIFT) |
---|
468 | 619 | #define KBASE_REG_ZONE_MCU_SHARED_SIZE (((0x08000000ULL) >> PAGE_SHIFT) - \ |
---|
469 | 620 | KBASE_REG_ZONE_MCU_SHARED_BASE) |
---|
| 621 | + |
---|
| 622 | +/* For CSF GPUs, the EXEC_VA zone is always 4GB in size, and starts at 2^47 for 64-bit |
---|
| 623 | + * clients, and 2^43 for 32-bit clients. |
---|
| 624 | + */ |
---|
| 625 | +#define KBASE_REG_ZONE_EXEC_VA_BASE_64 ((1ULL << 47) >> PAGE_SHIFT) |
---|
| 626 | +#define KBASE_REG_ZONE_EXEC_VA_BASE_32 ((1ULL << 43) >> PAGE_SHIFT) |
---|
| 627 | +#define KBASE_REG_ZONE_EXEC_VA_SIZE KBASE_REG_ZONE_EXEC_VA_MAX_PAGES |
---|
| 628 | + |
---|
| 629 | +/* Executable zone supporting FIXED/FIXABLE allocations. |
---|
| 630 | + * It is always 4GB in size. |
---|
| 631 | + */ |
---|
| 632 | + |
---|
| 633 | +#define KBASE_REG_ZONE_EXEC_FIXED_VA KBASE_REG_ZONE(4) |
---|
| 634 | +#define KBASE_REG_ZONE_EXEC_FIXED_VA_SIZE KBASE_REG_ZONE_EXEC_VA_MAX_PAGES |
---|
| 635 | + |
---|
| 636 | +/* Non-executable zone supporting FIXED/FIXABLE allocations. |
---|
| 637 | + * It extends from (2^47) up to (2^48)-1, for 64-bit userspace clients, and from |
---|
| 638 | + * (2^43) up to (2^44)-1 for 32-bit userspace clients. |
---|
| 639 | + */ |
---|
| 640 | +#define KBASE_REG_ZONE_FIXED_VA KBASE_REG_ZONE(5) |
---|
| 641 | + |
---|
| 642 | +/* Again - 32-bit userspace cannot map addresses beyond 2^44, but 64-bit can - and so |
---|
| 643 | + * the end of the FIXED_VA zone for 64-bit clients is (2^48)-1. |
---|
| 644 | + */ |
---|
| 645 | +#define KBASE_REG_ZONE_FIXED_VA_END_64 ((1ULL << 48) >> PAGE_SHIFT) |
---|
| 646 | +#define KBASE_REG_ZONE_FIXED_VA_END_32 ((1ULL << 44) >> PAGE_SHIFT) |
---|
| 647 | + |
---|
470 | 648 | #endif |
---|
471 | 649 | |
---|
472 | 650 | unsigned long flags; |
---|
.. | .. |
---|
476 | 654 | struct list_head jit_node; |
---|
477 | 655 | u16 jit_usage_id; |
---|
478 | 656 | u8 jit_bin_id; |
---|
| 657 | + |
---|
479 | 658 | #if MALI_JIT_PRESSURE_LIMIT_BASE |
---|
480 | 659 | /* Pointer to an object in GPU memory defining an end of an allocated |
---|
481 | 660 | * region |
---|
.. | .. |
---|
503 | 682 | size_t used_pages; |
---|
504 | 683 | #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */ |
---|
505 | 684 | |
---|
506 | | - int va_refcnt; |
---|
| 685 | + kbase_refcount_t va_refcnt; |
---|
| 686 | + atomic_t no_user_free_count; |
---|
507 | 687 | }; |
---|
| 688 | + |
---|
| 689 | +/** |
---|
| 690 | + * kbase_is_ctx_reg_zone - determine whether a KBASE_REG_ZONE_<...> is for a |
---|
| 691 | + * context or for a device |
---|
| 692 | + * @zone_bits: A KBASE_REG_ZONE_<...> to query |
---|
| 693 | + * |
---|
| 694 | + * Return: True if the zone for @zone_bits is a context zone, False otherwise |
---|
| 695 | + */ |
---|
| 696 | +static inline bool kbase_is_ctx_reg_zone(unsigned long zone_bits) |
---|
| 697 | +{ |
---|
| 698 | + WARN_ON((zone_bits & KBASE_REG_ZONE_MASK) != zone_bits); |
---|
| 699 | + return (zone_bits == KBASE_REG_ZONE_SAME_VA || |
---|
| 700 | +#if MALI_USE_CSF |
---|
| 701 | + zone_bits == KBASE_REG_ZONE_EXEC_FIXED_VA || zone_bits == KBASE_REG_ZONE_FIXED_VA || |
---|
| 702 | +#endif |
---|
| 703 | + zone_bits == KBASE_REG_ZONE_CUSTOM_VA || zone_bits == KBASE_REG_ZONE_EXEC_VA); |
---|
| 704 | +} |
---|
508 | 705 | |
---|
509 | 706 | /* Special marker for failed JIT allocations that still must be marked as |
---|
510 | 707 | * in-use |
---|
.. | .. |
---|
529 | 726 | return (kbase_is_region_invalid(reg) || kbase_is_region_free(reg)); |
---|
530 | 727 | } |
---|
531 | 728 | |
---|
532 | | -int kbase_remove_va_region(struct kbase_va_region *reg); |
---|
533 | | -static inline void kbase_region_refcnt_free(struct kbase_va_region *reg) |
---|
| 729 | +/** |
---|
| 730 | + * kbase_is_region_shrinkable - Check if a region is "shrinkable". |
---|
| 731 | + * A shrinkable regions is a region for which its backing pages (reg->gpu_alloc->pages) |
---|
| 732 | + * can be freed at any point, even though the kbase_va_region structure itself |
---|
| 733 | + * may have been refcounted. |
---|
| 734 | + * Regions that aren't on a shrinker, but could be shrunk at any point in future |
---|
| 735 | + * without warning are still considered "shrinkable" (e.g. Active JIT allocs) |
---|
| 736 | + * |
---|
| 737 | + * @reg: Pointer to region |
---|
| 738 | + * |
---|
| 739 | + * Return: true if the region is "shrinkable", false if not. |
---|
| 740 | + */ |
---|
| 741 | +static inline bool kbase_is_region_shrinkable(struct kbase_va_region *reg) |
---|
| 742 | +{ |
---|
| 743 | + return (reg->flags & KBASE_REG_DONT_NEED) || (reg->flags & KBASE_REG_ACTIVE_JIT_ALLOC); |
---|
| 744 | +} |
---|
| 745 | + |
---|
| 746 | +void kbase_remove_va_region(struct kbase_device *kbdev, |
---|
| 747 | + struct kbase_va_region *reg); |
---|
| 748 | +static inline void kbase_region_refcnt_free(struct kbase_device *kbdev, |
---|
| 749 | + struct kbase_va_region *reg) |
---|
534 | 750 | { |
---|
535 | 751 | /* If region was mapped then remove va region*/ |
---|
536 | 752 | if (reg->start_pfn) |
---|
537 | | - kbase_remove_va_region(reg); |
---|
| 753 | + kbase_remove_va_region(kbdev, reg); |
---|
538 | 754 | |
---|
539 | 755 | /* To detect use-after-free in debug builds */ |
---|
540 | 756 | KBASE_DEBUG_CODE(reg->flags |= KBASE_REG_FREE); |
---|
.. | .. |
---|
544 | 760 | static inline struct kbase_va_region *kbase_va_region_alloc_get( |
---|
545 | 761 | struct kbase_context *kctx, struct kbase_va_region *region) |
---|
546 | 762 | { |
---|
547 | | - lockdep_assert_held(&kctx->reg_lock); |
---|
| 763 | + WARN_ON(!kbase_refcount_read(®ion->va_refcnt)); |
---|
| 764 | + WARN_ON(kbase_refcount_read(®ion->va_refcnt) == INT_MAX); |
---|
548 | 765 | |
---|
549 | | - WARN_ON(!region->va_refcnt); |
---|
550 | | - |
---|
551 | | - /* non-atomic as kctx->reg_lock is held */ |
---|
552 | 766 | dev_dbg(kctx->kbdev->dev, "va_refcnt %d before get %pK\n", |
---|
553 | | - region->va_refcnt, (void *)region); |
---|
554 | | - region->va_refcnt++; |
---|
| 767 | + kbase_refcount_read(®ion->va_refcnt), (void *)region); |
---|
| 768 | + kbase_refcount_inc(®ion->va_refcnt); |
---|
555 | 769 | |
---|
556 | 770 | return region; |
---|
557 | 771 | } |
---|
.. | .. |
---|
559 | 773 | static inline struct kbase_va_region *kbase_va_region_alloc_put( |
---|
560 | 774 | struct kbase_context *kctx, struct kbase_va_region *region) |
---|
561 | 775 | { |
---|
562 | | - lockdep_assert_held(&kctx->reg_lock); |
---|
563 | | - |
---|
564 | | - WARN_ON(region->va_refcnt <= 0); |
---|
| 776 | + WARN_ON(kbase_refcount_read(®ion->va_refcnt) <= 0); |
---|
565 | 777 | WARN_ON(region->flags & KBASE_REG_FREE); |
---|
566 | 778 | |
---|
567 | | - /* non-atomic as kctx->reg_lock is held */ |
---|
568 | | - region->va_refcnt--; |
---|
569 | | - dev_dbg(kctx->kbdev->dev, "va_refcnt %d after put %pK\n", |
---|
570 | | - region->va_refcnt, (void *)region); |
---|
571 | | - if (!region->va_refcnt) |
---|
572 | | - kbase_region_refcnt_free(region); |
---|
| 779 | + if (kbase_refcount_dec_and_test(®ion->va_refcnt)) |
---|
| 780 | + kbase_region_refcnt_free(kctx->kbdev, region); |
---|
| 781 | + else |
---|
| 782 | + dev_dbg(kctx->kbdev->dev, "va_refcnt %d after put %pK\n", |
---|
| 783 | + kbase_refcount_read(®ion->va_refcnt), (void *)region); |
---|
573 | 784 | |
---|
574 | 785 | return NULL; |
---|
| 786 | +} |
---|
| 787 | + |
---|
| 788 | +/** |
---|
| 789 | + * kbase_va_region_is_no_user_free - Check if user free is forbidden for the region. |
---|
| 790 | + * A region that must not be freed by userspace indicates that it is owned by some other |
---|
| 791 | + * kbase subsystem, for example tiler heaps, JIT memory or CSF queues. |
---|
| 792 | + * Such regions must not be shrunk (i.e. have their backing pages freed), except by the |
---|
| 793 | + * current owner. |
---|
| 794 | + * Hence, callers cannot rely on this check alone to determine if a region might be shrunk |
---|
| 795 | + * by any part of kbase. Instead they should use kbase_is_region_shrinkable(). |
---|
| 796 | + * |
---|
| 797 | + * @region: Pointer to region. |
---|
| 798 | + * |
---|
| 799 | + * Return: true if userspace cannot free the region, false if userspace can free the region. |
---|
| 800 | + */ |
---|
| 801 | +static inline bool kbase_va_region_is_no_user_free(struct kbase_va_region *region) |
---|
| 802 | +{ |
---|
| 803 | + return atomic_read(®ion->no_user_free_count) > 0; |
---|
| 804 | +} |
---|
| 805 | + |
---|
| 806 | +/** |
---|
| 807 | + * kbase_va_region_no_user_free_inc - Increment "no user free" count for a region. |
---|
| 808 | + * Calling this function will prevent the region to be shrunk by parts of kbase that |
---|
| 809 | + * don't own the region (as long as the count stays above zero). Refer to |
---|
| 810 | + * kbase_va_region_is_no_user_free() for more information. |
---|
| 811 | + * |
---|
| 812 | + * @region: Pointer to region (not shrinkable). |
---|
| 813 | + * |
---|
| 814 | + * Return: the pointer to the region passed as argument. |
---|
| 815 | + */ |
---|
| 816 | +static inline void kbase_va_region_no_user_free_inc(struct kbase_va_region *region) |
---|
| 817 | +{ |
---|
| 818 | + WARN_ON(kbase_is_region_shrinkable(region)); |
---|
| 819 | + WARN_ON(atomic_read(®ion->no_user_free_count) == INT_MAX); |
---|
| 820 | + |
---|
| 821 | + /* non-atomic as kctx->reg_lock is held */ |
---|
| 822 | + atomic_inc(®ion->no_user_free_count); |
---|
| 823 | +} |
---|
| 824 | + |
---|
| 825 | +/** |
---|
| 826 | + * kbase_va_region_no_user_free_dec - Decrement "no user free" count for a region. |
---|
| 827 | + * |
---|
| 828 | + * @region: Pointer to region (not shrinkable). |
---|
| 829 | + */ |
---|
| 830 | +static inline void kbase_va_region_no_user_free_dec(struct kbase_va_region *region) |
---|
| 831 | +{ |
---|
| 832 | + WARN_ON(!kbase_va_region_is_no_user_free(region)); |
---|
| 833 | + |
---|
| 834 | + atomic_dec(®ion->no_user_free_count); |
---|
575 | 835 | } |
---|
576 | 836 | |
---|
577 | 837 | /* Common functions */ |
---|
.. | .. |
---|
787 | 1047 | * |
---|
788 | 1048 | * Return: 0 on success, negative -errno on error |
---|
789 | 1049 | */ |
---|
790 | | -int kbase_mem_pool_init(struct kbase_mem_pool *pool, |
---|
791 | | - const struct kbase_mem_pool_config *config, |
---|
792 | | - unsigned int order, |
---|
793 | | - int group_id, |
---|
794 | | - struct kbase_device *kbdev, |
---|
795 | | - struct kbase_mem_pool *next_pool); |
---|
| 1050 | +int kbase_mem_pool_init(struct kbase_mem_pool *pool, const struct kbase_mem_pool_config *config, |
---|
| 1051 | + unsigned int order, int group_id, struct kbase_device *kbdev, |
---|
| 1052 | + struct kbase_mem_pool *next_pool); |
---|
796 | 1053 | |
---|
797 | 1054 | /** |
---|
798 | 1055 | * kbase_mem_pool_term - Destroy a memory pool |
---|
.. | .. |
---|
872 | 1129 | * @pages: Pointer to array where the physical address of the allocated |
---|
873 | 1130 | * pages will be stored. |
---|
874 | 1131 | * @partial_allowed: If fewer pages allocated is allowed |
---|
| 1132 | + * @page_owner: Pointer to the task that created the Kbase context for which |
---|
| 1133 | + * the pages are being allocated. It can be NULL if the pages |
---|
| 1134 | + * won't be associated with any Kbase context. |
---|
875 | 1135 | * |
---|
876 | 1136 | * Like kbase_mem_pool_alloc() but optimized for allocating many pages. |
---|
877 | 1137 | * |
---|
.. | .. |
---|
888 | 1148 | * this lock, it should use kbase_mem_pool_alloc_pages_locked() instead. |
---|
889 | 1149 | */ |
---|
890 | 1150 | int kbase_mem_pool_alloc_pages(struct kbase_mem_pool *pool, size_t nr_4k_pages, |
---|
891 | | - struct tagged_addr *pages, bool partial_allowed); |
---|
| 1151 | + struct tagged_addr *pages, bool partial_allowed, |
---|
| 1152 | + struct task_struct *page_owner); |
---|
892 | 1153 | |
---|
893 | 1154 | /** |
---|
894 | 1155 | * kbase_mem_pool_alloc_pages_locked - Allocate pages from memory pool |
---|
.. | .. |
---|
1000 | 1261 | * kbase_mem_pool_grow - Grow the pool |
---|
1001 | 1262 | * @pool: Memory pool to grow |
---|
1002 | 1263 | * @nr_to_grow: Number of pages to add to the pool |
---|
| 1264 | + * @page_owner: Pointer to the task that created the Kbase context for which |
---|
| 1265 | + * the memory pool is being grown. It can be NULL if the pages |
---|
| 1266 | + * to be allocated won't be associated with any Kbase context. |
---|
1003 | 1267 | * |
---|
1004 | 1268 | * Adds @nr_to_grow pages to the pool. Note that this may cause the pool to |
---|
1005 | 1269 | * become larger than the maximum size specified. |
---|
1006 | 1270 | * |
---|
1007 | | - * Returns: 0 on success, -ENOMEM if unable to allocate sufficent pages |
---|
| 1271 | + * Return: 0 on success, -ENOMEM if unable to allocate sufficent pages |
---|
1008 | 1272 | */ |
---|
1009 | | -int kbase_mem_pool_grow(struct kbase_mem_pool *pool, size_t nr_to_grow); |
---|
| 1273 | +int kbase_mem_pool_grow(struct kbase_mem_pool *pool, size_t nr_to_grow, |
---|
| 1274 | + struct task_struct *page_owner); |
---|
1010 | 1275 | |
---|
1011 | 1276 | /** |
---|
1012 | 1277 | * kbase_mem_pool_trim - Grow or shrink the pool to a new size |
---|
.. | .. |
---|
1038 | 1303 | * Return: A new page or NULL if no memory |
---|
1039 | 1304 | */ |
---|
1040 | 1305 | struct page *kbase_mem_alloc_page(struct kbase_mem_pool *pool); |
---|
| 1306 | + |
---|
| 1307 | +/** |
---|
| 1308 | + * kbase_mem_pool_free_page - Free a page from a memory pool. |
---|
| 1309 | + * @pool: Memory pool to free a page from |
---|
| 1310 | + * @p: Page to free |
---|
| 1311 | + * |
---|
| 1312 | + * This will free any associated data stored for the page and release |
---|
| 1313 | + * the page back to the kernel. |
---|
| 1314 | + */ |
---|
| 1315 | +void kbase_mem_pool_free_page(struct kbase_mem_pool *pool, struct page *p); |
---|
1041 | 1316 | |
---|
1042 | 1317 | /** |
---|
1043 | 1318 | * kbase_region_tracker_init - Initialize the region tracker data structure |
---|
.. | .. |
---|
1086 | 1361 | /** |
---|
1087 | 1362 | * kbase_region_tracker_term_rbtree - Free memory for a region tracker |
---|
1088 | 1363 | * |
---|
1089 | | - * This will free all the regions within the region tracker |
---|
1090 | | - * |
---|
1091 | 1364 | * @rbtree: Region tracker tree root |
---|
| 1365 | + * |
---|
| 1366 | + * This will free all the regions within the region tracker |
---|
1092 | 1367 | */ |
---|
1093 | 1368 | void kbase_region_tracker_term_rbtree(struct rb_root *rbtree); |
---|
1094 | 1369 | |
---|
.. | .. |
---|
1098 | 1373 | struct rb_root *rbtree, u64 gpu_addr); |
---|
1099 | 1374 | |
---|
1100 | 1375 | /** |
---|
1101 | | - * Check that a pointer is actually a valid region. |
---|
| 1376 | + * kbase_region_tracker_find_region_base_address - Check that a pointer is |
---|
| 1377 | + * actually a valid region. |
---|
1102 | 1378 | * @kctx: kbase context containing the region |
---|
1103 | 1379 | * @gpu_addr: pointer to check |
---|
1104 | 1380 | * |
---|
1105 | 1381 | * Must be called with context lock held. |
---|
| 1382 | + * |
---|
| 1383 | + * Return: pointer to the valid region on success, NULL otherwise |
---|
1106 | 1384 | */ |
---|
1107 | 1385 | struct kbase_va_region *kbase_region_tracker_find_region_base_address( |
---|
1108 | 1386 | struct kbase_context *kctx, u64 gpu_addr); |
---|
1109 | 1387 | struct kbase_va_region *kbase_find_region_base_address(struct rb_root *rbtree, |
---|
1110 | 1388 | u64 gpu_addr); |
---|
1111 | 1389 | |
---|
1112 | | -struct kbase_va_region *kbase_alloc_free_region(struct rb_root *rbtree, |
---|
1113 | | - u64 start_pfn, size_t nr_pages, int zone); |
---|
| 1390 | +struct kbase_va_region *kbase_alloc_free_region(struct kbase_device *kbdev, struct rb_root *rbtree, |
---|
| 1391 | + u64 start_pfn, size_t nr_pages, int zone); |
---|
1114 | 1392 | void kbase_free_alloced_region(struct kbase_va_region *reg); |
---|
1115 | 1393 | int kbase_add_va_region(struct kbase_context *kctx, struct kbase_va_region *reg, |
---|
1116 | 1394 | u64 addr, size_t nr_pages, size_t align); |
---|
.. | .. |
---|
1120 | 1398 | |
---|
1121 | 1399 | bool kbase_check_alloc_flags(unsigned long flags); |
---|
1122 | 1400 | bool kbase_check_import_flags(unsigned long flags); |
---|
| 1401 | + |
---|
| 1402 | +static inline bool kbase_import_size_is_valid(struct kbase_device *kbdev, u64 va_pages) |
---|
| 1403 | +{ |
---|
| 1404 | + if (va_pages > KBASE_MEM_ALLOC_MAX_SIZE) { |
---|
| 1405 | + dev_dbg( |
---|
| 1406 | + kbdev->dev, |
---|
| 1407 | + "Import attempted with va_pages==%lld larger than KBASE_MEM_ALLOC_MAX_SIZE!", |
---|
| 1408 | + (unsigned long long)va_pages); |
---|
| 1409 | + return false; |
---|
| 1410 | + } |
---|
| 1411 | + |
---|
| 1412 | + return true; |
---|
| 1413 | +} |
---|
| 1414 | + |
---|
| 1415 | +static inline bool kbase_alias_size_is_valid(struct kbase_device *kbdev, u64 va_pages) |
---|
| 1416 | +{ |
---|
| 1417 | + if (va_pages > KBASE_MEM_ALLOC_MAX_SIZE) { |
---|
| 1418 | + dev_dbg( |
---|
| 1419 | + kbdev->dev, |
---|
| 1420 | + "Alias attempted with va_pages==%lld larger than KBASE_MEM_ALLOC_MAX_SIZE!", |
---|
| 1421 | + (unsigned long long)va_pages); |
---|
| 1422 | + return false; |
---|
| 1423 | + } |
---|
| 1424 | + |
---|
| 1425 | + return true; |
---|
| 1426 | +} |
---|
1123 | 1427 | |
---|
1124 | 1428 | /** |
---|
1125 | 1429 | * kbase_check_alloc_sizes - check user space sizes parameters for an |
---|
.. | .. |
---|
1155 | 1459 | int kbase_update_region_flags(struct kbase_context *kctx, |
---|
1156 | 1460 | struct kbase_va_region *reg, unsigned long flags); |
---|
1157 | 1461 | |
---|
| 1462 | +/** |
---|
| 1463 | + * kbase_gpu_vm_lock() - Acquire the per-context region list lock |
---|
| 1464 | + * @kctx: KBase context |
---|
| 1465 | + * |
---|
| 1466 | + * Care must be taken when making an allocation whilst holding this lock, because of interaction |
---|
| 1467 | + * with the Kernel's OoM-killer and use of this lock in &vm_operations_struct close() handlers. |
---|
| 1468 | + * |
---|
| 1469 | + * If this lock is taken during a syscall, and/or the allocation is 'small' then it is safe to use. |
---|
| 1470 | + * |
---|
| 1471 | + * If the caller is not in a syscall, and the allocation is 'large', then it must not hold this |
---|
| 1472 | + * lock. |
---|
| 1473 | + * |
---|
| 1474 | + * This is because the kernel OoM killer might target the process corresponding to that same kbase |
---|
| 1475 | + * context, and attempt to call the context's close() handlers for its open VMAs. This is safe if |
---|
| 1476 | + * the allocating caller is in a syscall, because the VMA close() handlers are delayed until all |
---|
| 1477 | + * syscalls have finished (noting that no new syscalls can start as the remaining user threads will |
---|
| 1478 | + * have been killed too), and so there is no possibility of contention between the thread |
---|
| 1479 | + * allocating with this lock held, and the VMA close() handler. |
---|
| 1480 | + * |
---|
| 1481 | + * However, outside of a syscall (e.g. a kworker or other kthread), one of kbase's VMA close() |
---|
| 1482 | + * handlers (kbase_cpu_vm_close()) also takes this lock, and so prevents the process from being |
---|
| 1483 | + * killed until the caller of the function allocating memory has released this lock. On subsequent |
---|
| 1484 | + * retries for allocating a page, the OoM killer would be re-invoked but skips over the process |
---|
| 1485 | + * stuck in its close() handler. |
---|
| 1486 | + * |
---|
| 1487 | + * Also because the caller is not in a syscall, the page allocation code in the kernel is not aware |
---|
| 1488 | + * that the allocation is being done on behalf of another process, and so does not realize that |
---|
| 1489 | + * process has received a kill signal due to an OoM, and so will continually retry with the OoM |
---|
| 1490 | + * killer until enough memory has been released, or until all other killable processes have been |
---|
| 1491 | + * killed (at which point the kernel halts with a panic). |
---|
| 1492 | + * |
---|
| 1493 | + * However, if the allocation outside of a syscall is small enough to be satisfied by killing |
---|
| 1494 | + * another process, then the allocation completes, the caller releases this lock, and |
---|
| 1495 | + * kbase_cpu_vm_close() can unblock and allow the process to be killed. |
---|
| 1496 | + * |
---|
| 1497 | + * Hence, this is effectively a deadlock with kbase_cpu_vm_close(), except that if the memory |
---|
| 1498 | + * allocation is small enough the deadlock can be resolved. For that reason, such a memory deadlock |
---|
| 1499 | + * is NOT discovered with CONFIG_PROVE_LOCKING. |
---|
| 1500 | + * |
---|
| 1501 | + * If this may be called outside of a syscall, consider moving allocations outside of this lock, or |
---|
| 1502 | + * use __GFP_NORETRY for such allocations (which will allow direct-reclaim attempts, but will |
---|
| 1503 | + * prevent OoM kills to satisfy the allocation, and will just fail the allocation instead). |
---|
| 1504 | + */ |
---|
1158 | 1505 | void kbase_gpu_vm_lock(struct kbase_context *kctx); |
---|
| 1506 | + |
---|
| 1507 | +/** |
---|
| 1508 | + * kbase_gpu_vm_unlock() - Release the per-context region list lock |
---|
| 1509 | + * @kctx: KBase context |
---|
| 1510 | + */ |
---|
1159 | 1511 | void kbase_gpu_vm_unlock(struct kbase_context *kctx); |
---|
1160 | 1512 | |
---|
1161 | 1513 | int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size); |
---|
1162 | 1514 | |
---|
1163 | 1515 | /** |
---|
1164 | | - * Register region and map it on the GPU. |
---|
| 1516 | + * kbase_gpu_mmap - Register region and map it on the GPU. |
---|
| 1517 | + * |
---|
1165 | 1518 | * @kctx: kbase context containing the region |
---|
1166 | 1519 | * @reg: the region to add |
---|
1167 | 1520 | * @addr: the address to insert the region at |
---|
1168 | 1521 | * @nr_pages: the number of pages in the region |
---|
1169 | 1522 | * @align: the minimum alignment in pages |
---|
| 1523 | + * @mmu_sync_info: Indicates whether this call is synchronous wrt MMU ops. |
---|
1170 | 1524 | * |
---|
1171 | 1525 | * Call kbase_add_va_region() and map the region on the GPU. |
---|
| 1526 | + * |
---|
| 1527 | + * Return: 0 on success, error code otherwise. |
---|
1172 | 1528 | */ |
---|
1173 | | -int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg, u64 addr, size_t nr_pages, size_t align); |
---|
| 1529 | +int kbase_gpu_mmap(struct kbase_context *kctx, struct kbase_va_region *reg, |
---|
| 1530 | + u64 addr, size_t nr_pages, size_t align, |
---|
| 1531 | + enum kbase_caller_mmu_sync_info mmu_sync_info); |
---|
1174 | 1532 | |
---|
1175 | 1533 | /** |
---|
1176 | | - * Remove the region from the GPU and unregister it. |
---|
| 1534 | + * kbase_gpu_munmap - Remove the region from the GPU and unregister it. |
---|
| 1535 | + * |
---|
1177 | 1536 | * @kctx: KBase context |
---|
1178 | 1537 | * @reg: The region to remove |
---|
1179 | 1538 | * |
---|
1180 | 1539 | * Must be called with context lock held. |
---|
| 1540 | + * |
---|
| 1541 | + * Return: 0 on success, error code otherwise. |
---|
1181 | 1542 | */ |
---|
1182 | 1543 | int kbase_gpu_munmap(struct kbase_context *kctx, struct kbase_va_region *reg); |
---|
1183 | 1544 | |
---|
.. | .. |
---|
1185 | 1546 | * kbase_mmu_update - Configure an address space on the GPU to the specified |
---|
1186 | 1547 | * MMU tables |
---|
1187 | 1548 | * |
---|
1188 | | - * The caller has the following locking conditions: |
---|
1189 | | - * - It must hold kbase_device->mmu_hw_mutex |
---|
1190 | | - * - It must hold the hwaccess_lock |
---|
1191 | | - * |
---|
1192 | 1549 | * @kbdev: Kbase device structure |
---|
1193 | 1550 | * @mmut: The set of MMU tables to be configured on the address space |
---|
1194 | 1551 | * @as_nr: The address space to be configured |
---|
| 1552 | + * |
---|
| 1553 | + * The caller has the following locking conditions: |
---|
| 1554 | + * - It must hold kbase_device->mmu_hw_mutex |
---|
| 1555 | + * - It must hold the hwaccess_lock |
---|
1195 | 1556 | */ |
---|
1196 | 1557 | void kbase_mmu_update(struct kbase_device *kbdev, struct kbase_mmu_table *mmut, |
---|
1197 | 1558 | int as_nr); |
---|
.. | .. |
---|
1224 | 1585 | |
---|
1225 | 1586 | void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat); |
---|
1226 | 1587 | |
---|
| 1588 | +#if defined(CONFIG_MALI_VECTOR_DUMP) |
---|
1227 | 1589 | /** |
---|
1228 | 1590 | * kbase_mmu_dump() - Dump the MMU tables to a buffer. |
---|
| 1591 | + * |
---|
| 1592 | + * @kctx: The kbase context to dump |
---|
| 1593 | + * @nr_pages: The number of pages to allocate for the buffer. |
---|
1229 | 1594 | * |
---|
1230 | 1595 | * This function allocates a buffer (of @c nr_pages pages) to hold a dump |
---|
1231 | 1596 | * of the MMU tables and fills it. If the buffer is too small |
---|
.. | .. |
---|
1236 | 1601 | * The buffer returned should be freed with @ref vfree when it is no longer |
---|
1237 | 1602 | * required. |
---|
1238 | 1603 | * |
---|
1239 | | - * @kctx: The kbase context to dump |
---|
1240 | | - * @nr_pages: The number of pages to allocate for the buffer. |
---|
1241 | | - * |
---|
1242 | 1604 | * Return: The address of the buffer containing the MMU dump or NULL on error |
---|
1243 | 1605 | * (including if the @c nr_pages is too small) |
---|
1244 | 1606 | */ |
---|
1245 | 1607 | void *kbase_mmu_dump(struct kbase_context *kctx, int nr_pages); |
---|
| 1608 | +#endif |
---|
1246 | 1609 | |
---|
1247 | 1610 | /** |
---|
1248 | 1611 | * kbase_sync_now - Perform cache maintenance on a memory region |
---|
.. | .. |
---|
1268 | 1631 | * kbasep_os_process_page_usage_update() - Update the memory allocation |
---|
1269 | 1632 | * counters for the current process. |
---|
1270 | 1633 | * |
---|
1271 | | - * OS specific call to updates the current memory allocation counters |
---|
1272 | | - * for the current process with the supplied delta. |
---|
1273 | | - * |
---|
1274 | 1634 | * @kctx: The kbase context |
---|
1275 | 1635 | * @pages: The desired delta to apply to the memory usage counters. |
---|
| 1636 | + * |
---|
| 1637 | + * OS specific call to updates the current memory allocation counters |
---|
| 1638 | + * for the current process with the supplied delta. |
---|
1276 | 1639 | */ |
---|
1277 | 1640 | |
---|
1278 | 1641 | void kbasep_os_process_page_usage_update(struct kbase_context *kctx, int pages); |
---|
.. | .. |
---|
1281 | 1644 | * kbase_process_page_usage_inc() - Add to the memory allocation counters for |
---|
1282 | 1645 | * the current process |
---|
1283 | 1646 | * |
---|
1284 | | - * OS specific call to add to the current memory allocation counters for |
---|
1285 | | - * the current process by the supplied amount. |
---|
1286 | | - * |
---|
1287 | 1647 | * @kctx: The kernel base context used for the allocation. |
---|
1288 | 1648 | * @pages: The desired delta to apply to the memory usage counters. |
---|
| 1649 | + * |
---|
| 1650 | + * OS specific call to add to the current memory allocation counters for |
---|
| 1651 | + * the current process by the supplied amount. |
---|
1289 | 1652 | */ |
---|
1290 | 1653 | |
---|
1291 | 1654 | static inline void kbase_process_page_usage_inc(struct kbase_context *kctx, int pages) |
---|
.. | .. |
---|
1297 | 1660 | * kbase_process_page_usage_dec() - Subtract from the memory allocation |
---|
1298 | 1661 | * counters for the current process. |
---|
1299 | 1662 | * |
---|
1300 | | - * OS specific call to subtract from the current memory allocation counters |
---|
1301 | | - * for the current process by the supplied amount. |
---|
1302 | | - * |
---|
1303 | 1663 | * @kctx: The kernel base context used for the allocation. |
---|
1304 | 1664 | * @pages: The desired delta to apply to the memory usage counters. |
---|
| 1665 | + * |
---|
| 1666 | + * OS specific call to subtract from the current memory allocation counters |
---|
| 1667 | + * for the current process by the supplied amount. |
---|
1305 | 1668 | */ |
---|
1306 | 1669 | |
---|
1307 | 1670 | static inline void kbase_process_page_usage_dec(struct kbase_context *kctx, int pages) |
---|
.. | .. |
---|
1313 | 1676 | * kbasep_find_enclosing_cpu_mapping_offset() - Find the offset of the CPU |
---|
1314 | 1677 | * mapping of a memory allocation containing a given address range |
---|
1315 | 1678 | * |
---|
1316 | | - * Searches for a CPU mapping of any part of any region that fully encloses the |
---|
1317 | | - * CPU virtual address range specified by @uaddr and @size. Returns a failure |
---|
1318 | | - * indication if only part of the address range lies within a CPU mapping. |
---|
1319 | | - * |
---|
1320 | 1679 | * @kctx: The kernel base context used for the allocation. |
---|
1321 | 1680 | * @uaddr: Start of the CPU virtual address range. |
---|
1322 | 1681 | * @size: Size of the CPU virtual address range (in bytes). |
---|
1323 | 1682 | * @offset: The offset from the start of the allocation to the specified CPU |
---|
1324 | 1683 | * virtual address. |
---|
| 1684 | + * |
---|
| 1685 | + * Searches for a CPU mapping of any part of any region that fully encloses the |
---|
| 1686 | + * CPU virtual address range specified by @uaddr and @size. Returns a failure |
---|
| 1687 | + * indication if only part of the address range lies within a CPU mapping. |
---|
1325 | 1688 | * |
---|
1326 | 1689 | * Return: 0 if offset was obtained successfully. Error code otherwise. |
---|
1327 | 1690 | */ |
---|
.. | .. |
---|
1334 | 1697 | * the start of GPU virtual memory region which encloses @gpu_addr for the |
---|
1335 | 1698 | * @size length in bytes |
---|
1336 | 1699 | * |
---|
1337 | | - * Searches for the memory region in GPU virtual memory space which contains |
---|
1338 | | - * the region defined by the @gpu_addr and @size, where @gpu_addr is the |
---|
1339 | | - * beginning and @size the length in bytes of the provided region. If found, |
---|
1340 | | - * the location of the start address of the GPU virtual memory region is |
---|
1341 | | - * passed in @start pointer and the location of the offset of the region into |
---|
1342 | | - * the GPU virtual memory region is passed in @offset pointer. |
---|
1343 | | - * |
---|
1344 | 1700 | * @kctx: The kernel base context within which the memory is searched. |
---|
1345 | 1701 | * @gpu_addr: GPU virtual address for which the region is sought; defines |
---|
1346 | 1702 | * the beginning of the provided region. |
---|
.. | .. |
---|
1350 | 1706 | * the found GPU virtual memory region is. |
---|
1351 | 1707 | * @offset: Pointer to the location where the offset of @gpu_addr into |
---|
1352 | 1708 | * the found GPU virtual memory region is. |
---|
| 1709 | + * |
---|
| 1710 | + * Searches for the memory region in GPU virtual memory space which contains |
---|
| 1711 | + * the region defined by the @gpu_addr and @size, where @gpu_addr is the |
---|
| 1712 | + * beginning and @size the length in bytes of the provided region. If found, |
---|
| 1713 | + * the location of the start address of the GPU virtual memory region is |
---|
| 1714 | + * passed in @start pointer and the location of the offset of the region into |
---|
| 1715 | + * the GPU virtual memory region is passed in @offset pointer. |
---|
| 1716 | + * |
---|
| 1717 | + * Return: 0 on success, error code otherwise. |
---|
1353 | 1718 | */ |
---|
1354 | 1719 | int kbasep_find_enclosing_gpu_mapping_start_and_offset( |
---|
1355 | 1720 | struct kbase_context *kctx, |
---|
.. | .. |
---|
1360 | 1725 | * @alloc: allocation object to add pages to |
---|
1361 | 1726 | * @nr_pages_requested: number of physical pages to allocate |
---|
1362 | 1727 | * |
---|
1363 | | - * Allocates \a nr_pages_requested and updates the alloc object. |
---|
| 1728 | + * Allocates @nr_pages_requested and updates the alloc object. |
---|
1364 | 1729 | * |
---|
1365 | | - * Return: 0 if all pages have been successfully allocated. Error code otherwise |
---|
| 1730 | + * Note: if kbase_gpu_vm_lock() is to be held around this function to ensure thread-safe updating |
---|
| 1731 | + * of @alloc, then refer to the documentation of kbase_gpu_vm_lock() about the requirements of |
---|
| 1732 | + * either calling during a syscall, or ensuring the allocation is small. These requirements prevent |
---|
| 1733 | + * an effective deadlock between the kernel's OoM killer and kbase's VMA close() handlers, which |
---|
| 1734 | + * could take kbase_gpu_vm_lock() too. |
---|
1366 | 1735 | * |
---|
1367 | | - * Note : The caller must not hold vm_lock, as this could cause a deadlock if |
---|
1368 | | - * the kernel OoM killer runs. If the caller must allocate pages while holding |
---|
1369 | | - * this lock, it should use kbase_mem_pool_alloc_pages_locked() instead. |
---|
| 1736 | + * If the requirements of kbase_gpu_vm_lock() cannot be satisfied when calling this function, but |
---|
| 1737 | + * @alloc must still be updated in a thread-safe way, then instead use |
---|
| 1738 | + * kbase_alloc_phy_pages_helper_locked() and restructure callers into the sequence outlined there. |
---|
1370 | 1739 | * |
---|
1371 | 1740 | * This function cannot be used from interrupt context |
---|
| 1741 | + * |
---|
| 1742 | + * Return: 0 if all pages have been successfully allocated. Error code otherwise |
---|
1372 | 1743 | */ |
---|
1373 | 1744 | int kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc *alloc, |
---|
1374 | 1745 | size_t nr_pages_requested); |
---|
.. | .. |
---|
1378 | 1749 | * @alloc: allocation object to add pages to |
---|
1379 | 1750 | * @pool: Memory pool to allocate from |
---|
1380 | 1751 | * @nr_pages_requested: number of physical pages to allocate |
---|
1381 | | - * @prealloc_sa: Information about the partial allocation if the amount |
---|
1382 | | - * of memory requested is not a multiple of 2MB. One |
---|
1383 | | - * instance of struct kbase_sub_alloc must be allocated by |
---|
1384 | | - * the caller iff CONFIG_MALI_2MB_ALLOC is enabled. |
---|
1385 | 1752 | * |
---|
1386 | | - * Allocates \a nr_pages_requested and updates the alloc object. This function |
---|
1387 | | - * does not allocate new pages from the kernel, and therefore will never trigger |
---|
1388 | | - * the OoM killer. Therefore, it can be run while the vm_lock is held. |
---|
| 1753 | + * @prealloc_sa: Information about the partial allocation if the amount of memory requested |
---|
| 1754 | + * is not a multiple of 2MB. One instance of struct kbase_sub_alloc must be |
---|
| 1755 | + * allocated by the caller if kbdev->pagesize_2mb is enabled. |
---|
1389 | 1756 | * |
---|
1390 | | - * As new pages can not be allocated, the caller must ensure there are |
---|
1391 | | - * sufficient pages in the pool. Usage of this function should look like : |
---|
| 1757 | + * Allocates @nr_pages_requested and updates the alloc object. This function does not allocate new |
---|
| 1758 | + * pages from the kernel, and therefore will never trigger the OoM killer. Therefore, it can be |
---|
| 1759 | + * called whilst a thread operating outside of a syscall has held the region list lock |
---|
| 1760 | + * (kbase_gpu_vm_lock()), as it will not cause an effective deadlock with VMA close() handlers used |
---|
| 1761 | + * by the OoM killer. |
---|
| 1762 | + * |
---|
| 1763 | + * As new pages can not be allocated, the caller must ensure there are sufficient pages in the |
---|
| 1764 | + * pool. Usage of this function should look like : |
---|
1392 | 1765 | * |
---|
1393 | 1766 | * kbase_gpu_vm_lock(kctx); |
---|
1394 | 1767 | * kbase_mem_pool_lock(pool) |
---|
.. | .. |
---|
1401 | 1774 | * } |
---|
1402 | 1775 | * kbase_alloc_phy_pages_helper_locked(pool) |
---|
1403 | 1776 | * kbase_mem_pool_unlock(pool) |
---|
1404 | | - * Perform other processing that requires vm_lock... |
---|
| 1777 | + * // Perform other processing that requires vm_lock... |
---|
1405 | 1778 | * kbase_gpu_vm_unlock(kctx); |
---|
1406 | 1779 | * |
---|
1407 | | - * This ensures that the pool can be grown to the required size and that the |
---|
1408 | | - * allocation can complete without another thread using the newly grown pages. |
---|
| 1780 | + * This ensures that the pool can be grown to the required size and that the allocation can |
---|
| 1781 | + * complete without another thread using the newly grown pages. |
---|
1409 | 1782 | * |
---|
1410 | | - * If CONFIG_MALI_2MB_ALLOC is defined and the allocation is >= 2MB, then |
---|
1411 | | - * @pool must be alloc->imported.native.kctx->lp_mem_pool. Otherwise it must be |
---|
1412 | | - * alloc->imported.native.kctx->mem_pool. |
---|
1413 | | - * @prealloc_sa is used to manage the non-2MB sub-allocation. It has to be |
---|
1414 | | - * pre-allocated because we must not sleep (due to the usage of kmalloc()) |
---|
1415 | | - * whilst holding pool->pool_lock. |
---|
1416 | | - * @prealloc_sa shall be set to NULL if it has been consumed by this function |
---|
1417 | | - * to indicate that the caller must not free it. |
---|
| 1783 | + * If kbdev->pagesize_2mb is enabled and the allocation is >= 2MB, then @pool must be one of the |
---|
| 1784 | + * pools from alloc->imported.native.kctx->mem_pools.large[]. Otherwise it must be one of the |
---|
| 1785 | + * mempools from alloc->imported.native.kctx->mem_pools.small[]. |
---|
| 1786 | + * |
---|
| 1787 | + * @prealloc_sa is used to manage the non-2MB sub-allocation. It has to be pre-allocated because we |
---|
| 1788 | + * must not sleep (due to the usage of kmalloc()) whilst holding pool->pool_lock. @prealloc_sa |
---|
| 1789 | + * shall be set to NULL if it has been consumed by this function to indicate that the caller no |
---|
| 1790 | + * longer owns it and should not access it further. |
---|
| 1791 | + * |
---|
| 1792 | + * Note: Caller must hold @pool->pool_lock |
---|
1418 | 1793 | * |
---|
1419 | 1794 | * Return: Pointer to array of allocated pages. NULL on failure. |
---|
1420 | | - * |
---|
1421 | | - * Note : Caller must hold pool->pool_lock |
---|
1422 | 1795 | */ |
---|
1423 | 1796 | struct tagged_addr *kbase_alloc_phy_pages_helper_locked( |
---|
1424 | 1797 | struct kbase_mem_phy_alloc *alloc, struct kbase_mem_pool *pool, |
---|
.. | .. |
---|
1428 | 1801 | /** |
---|
1429 | 1802 | * kbase_free_phy_pages_helper() - Free physical pages. |
---|
1430 | 1803 | * |
---|
1431 | | - * Frees \a nr_pages and updates the alloc object. |
---|
1432 | | - * |
---|
1433 | 1804 | * @alloc: allocation object to free pages from |
---|
1434 | 1805 | * @nr_pages_to_free: number of physical pages to free |
---|
| 1806 | + * |
---|
| 1807 | + * Free @nr_pages_to_free pages and updates the alloc object. |
---|
1435 | 1808 | * |
---|
1436 | 1809 | * Return: 0 on success, otherwise a negative error code |
---|
1437 | 1810 | */ |
---|
.. | .. |
---|
1457 | 1830 | struct kbase_mem_pool *pool, struct tagged_addr *pages, |
---|
1458 | 1831 | size_t nr_pages_to_free); |
---|
1459 | 1832 | |
---|
1460 | | -static inline void kbase_set_dma_addr(struct page *p, dma_addr_t dma_addr) |
---|
| 1833 | +static inline void kbase_set_dma_addr_as_priv(struct page *p, dma_addr_t dma_addr) |
---|
1461 | 1834 | { |
---|
1462 | 1835 | SetPagePrivate(p); |
---|
1463 | 1836 | if (sizeof(dma_addr_t) > sizeof(p->private)) { |
---|
.. | .. |
---|
1473 | 1846 | } |
---|
1474 | 1847 | } |
---|
1475 | 1848 | |
---|
1476 | | -static inline dma_addr_t kbase_dma_addr(struct page *p) |
---|
| 1849 | +static inline dma_addr_t kbase_dma_addr_as_priv(struct page *p) |
---|
1477 | 1850 | { |
---|
1478 | 1851 | if (sizeof(dma_addr_t) > sizeof(p->private)) |
---|
1479 | 1852 | return ((dma_addr_t)page_private(p)) << PAGE_SHIFT; |
---|
.. | .. |
---|
1481 | 1854 | return (dma_addr_t)page_private(p); |
---|
1482 | 1855 | } |
---|
1483 | 1856 | |
---|
1484 | | -static inline void kbase_clear_dma_addr(struct page *p) |
---|
| 1857 | +static inline void kbase_clear_dma_addr_as_priv(struct page *p) |
---|
1485 | 1858 | { |
---|
1486 | 1859 | ClearPagePrivate(p); |
---|
| 1860 | +} |
---|
| 1861 | + |
---|
| 1862 | +static inline struct kbase_page_metadata *kbase_page_private(struct page *p) |
---|
| 1863 | +{ |
---|
| 1864 | + return (struct kbase_page_metadata *)page_private(p); |
---|
| 1865 | +} |
---|
| 1866 | + |
---|
| 1867 | +static inline dma_addr_t kbase_dma_addr(struct page *p) |
---|
| 1868 | +{ |
---|
| 1869 | + if (kbase_page_migration_enabled) |
---|
| 1870 | + return kbase_page_private(p)->dma_addr; |
---|
| 1871 | + |
---|
| 1872 | + return kbase_dma_addr_as_priv(p); |
---|
| 1873 | +} |
---|
| 1874 | + |
---|
| 1875 | +static inline dma_addr_t kbase_dma_addr_from_tagged(struct tagged_addr tagged_pa) |
---|
| 1876 | +{ |
---|
| 1877 | + phys_addr_t pa = as_phys_addr_t(tagged_pa); |
---|
| 1878 | + struct page *page = pfn_to_page(PFN_DOWN(pa)); |
---|
| 1879 | + dma_addr_t dma_addr = |
---|
| 1880 | + is_huge(tagged_pa) ? kbase_dma_addr_as_priv(page) : kbase_dma_addr(page); |
---|
| 1881 | + |
---|
| 1882 | + return dma_addr; |
---|
1487 | 1883 | } |
---|
1488 | 1884 | |
---|
1489 | 1885 | /** |
---|
.. | .. |
---|
1529 | 1925 | * kbase_jit_init - Initialize the JIT memory pool management |
---|
1530 | 1926 | * @kctx: kbase context |
---|
1531 | 1927 | * |
---|
1532 | | - * Returns zero on success or negative error number on failure. |
---|
| 1928 | + * Return: zero on success or negative error number on failure. |
---|
1533 | 1929 | */ |
---|
1534 | 1930 | int kbase_jit_init(struct kbase_context *kctx); |
---|
1535 | 1931 | |
---|
.. | .. |
---|
1644 | 2040 | unsigned int flags); |
---|
1645 | 2041 | |
---|
1646 | 2042 | /** |
---|
1647 | | - * jit_trim_necessary_pages() - calculate and trim the least pages possible to |
---|
1648 | | - * satisfy a new JIT allocation |
---|
| 2043 | + * kbase_jit_trim_necessary_pages() - calculate and trim the least pages |
---|
| 2044 | + * possible to satisfy a new JIT allocation |
---|
1649 | 2045 | * |
---|
1650 | 2046 | * @kctx: Pointer to the kbase context |
---|
1651 | 2047 | * @needed_pages: Number of JIT physical pages by which trimming is requested. |
---|
.. | .. |
---|
1767 | 2163 | /** |
---|
1768 | 2164 | * kbase_has_exec_va_zone - EXEC_VA zone predicate |
---|
1769 | 2165 | * |
---|
| 2166 | + * @kctx: kbase context |
---|
| 2167 | + * |
---|
1770 | 2168 | * Determine whether an EXEC_VA zone has been created for the GPU address space |
---|
1771 | 2169 | * of the given kbase context. |
---|
1772 | | - * |
---|
1773 | | - * @kctx: kbase context |
---|
1774 | 2170 | * |
---|
1775 | 2171 | * Return: True if the kbase context has an EXEC_VA zone. |
---|
1776 | 2172 | */ |
---|
.. | .. |
---|
1779 | 2175 | /** |
---|
1780 | 2176 | * kbase_map_external_resource - Map an external resource to the GPU. |
---|
1781 | 2177 | * @kctx: kbase context. |
---|
1782 | | - * @reg: The region to map. |
---|
| 2178 | + * @reg: External resource to map. |
---|
1783 | 2179 | * @locked_mm: The mm_struct which has been locked for this operation. |
---|
1784 | 2180 | * |
---|
1785 | | - * Return: The physical allocation which backs the region on success or NULL |
---|
1786 | | - * on failure. |
---|
| 2181 | + * On successful mapping, the VA region and the gpu_alloc refcounts will be |
---|
| 2182 | + * increased, making it safe to use and store both values directly. |
---|
| 2183 | + * |
---|
| 2184 | + * Return: Zero on success, or negative error code. |
---|
1787 | 2185 | */ |
---|
1788 | | -struct kbase_mem_phy_alloc *kbase_map_external_resource( |
---|
1789 | | - struct kbase_context *kctx, struct kbase_va_region *reg, |
---|
1790 | | - struct mm_struct *locked_mm); |
---|
| 2186 | +int kbase_map_external_resource(struct kbase_context *kctx, struct kbase_va_region *reg, |
---|
| 2187 | + struct mm_struct *locked_mm); |
---|
1791 | 2188 | |
---|
1792 | 2189 | /** |
---|
1793 | 2190 | * kbase_unmap_external_resource - Unmap an external resource from the GPU. |
---|
1794 | 2191 | * @kctx: kbase context. |
---|
1795 | | - * @reg: The region to unmap or NULL if it has already been released. |
---|
1796 | | - * @alloc: The physical allocation being unmapped. |
---|
| 2192 | + * @reg: VA region corresponding to external resource |
---|
| 2193 | + * |
---|
| 2194 | + * On successful unmapping, the VA region and the gpu_alloc refcounts will |
---|
| 2195 | + * be decreased. If the refcount reaches zero, both @reg and the corresponding |
---|
| 2196 | + * allocation may be freed, so using them after returning from this function |
---|
| 2197 | + * requires the caller to explicitly check their state. |
---|
1797 | 2198 | */ |
---|
1798 | | -void kbase_unmap_external_resource(struct kbase_context *kctx, |
---|
1799 | | - struct kbase_va_region *reg, struct kbase_mem_phy_alloc *alloc); |
---|
| 2199 | +void kbase_unmap_external_resource(struct kbase_context *kctx, struct kbase_va_region *reg); |
---|
1800 | 2200 | |
---|
| 2201 | +/** |
---|
| 2202 | + * kbase_unpin_user_buf_page - Unpin a page of a user buffer. |
---|
| 2203 | + * @page: page to unpin |
---|
| 2204 | + * |
---|
| 2205 | + * The caller must have ensured that there are no CPU mappings for @page (as |
---|
| 2206 | + * might be created from the struct kbase_mem_phy_alloc that tracks @page), and |
---|
| 2207 | + * that userspace will not be able to recreate the CPU mappings again. |
---|
| 2208 | + */ |
---|
| 2209 | +void kbase_unpin_user_buf_page(struct page *page); |
---|
1801 | 2210 | |
---|
1802 | 2211 | /** |
---|
1803 | 2212 | * kbase_jd_user_buf_pin_pages - Pin the pages of a user buffer. |
---|
.. | .. |
---|
1817 | 2226 | * kbase_sticky_resource_init - Initialize sticky resource management. |
---|
1818 | 2227 | * @kctx: kbase context |
---|
1819 | 2228 | * |
---|
1820 | | - * Returns zero on success or negative error number on failure. |
---|
| 2229 | + * Return: zero on success or negative error number on failure. |
---|
1821 | 2230 | */ |
---|
1822 | 2231 | int kbase_sticky_resource_init(struct kbase_context *kctx); |
---|
1823 | 2232 | |
---|
.. | .. |
---|
1879 | 2288 | } |
---|
1880 | 2289 | |
---|
1881 | 2290 | /** |
---|
1882 | | - * kbase_mem_pool_lock - Release a memory pool |
---|
| 2291 | + * kbase_mem_pool_unlock - Release a memory pool |
---|
1883 | 2292 | * @pool: Memory pool to lock |
---|
1884 | 2293 | */ |
---|
1885 | 2294 | static inline void kbase_mem_pool_unlock(struct kbase_mem_pool *pool) |
---|
.. | .. |
---|
1939 | 2348 | * manage the shared interface segment of MCU firmware address space. |
---|
1940 | 2349 | * @kbdev: Pointer to the kbase device |
---|
1941 | 2350 | * |
---|
1942 | | - * Returns zero on success or negative error number on failure. |
---|
| 2351 | + * Return: zero on success or negative error number on failure. |
---|
1943 | 2352 | */ |
---|
1944 | 2353 | int kbase_mcu_shared_interface_region_tracker_init(struct kbase_device *kbdev); |
---|
1945 | 2354 | |
---|
.. | .. |
---|
1958 | 2367 | * |
---|
1959 | 2368 | * Map a dma-buf on the GPU. The mappings are reference counted. |
---|
1960 | 2369 | * |
---|
1961 | | - * Returns 0 on success, or a negative error code. |
---|
| 2370 | + * Return: 0 on success, or a negative error code. |
---|
1962 | 2371 | */ |
---|
1963 | 2372 | int kbase_mem_umm_map(struct kbase_context *kctx, |
---|
1964 | 2373 | struct kbase_va_region *reg); |
---|
.. | .. |
---|
1978 | 2387 | * @alloc must be a valid physical allocation of type |
---|
1979 | 2388 | * KBASE_MEM_TYPE_IMPORTED_UMM that was previously mapped by |
---|
1980 | 2389 | * kbase_mem_umm_map(). The dma-buf attachment referenced by @alloc will |
---|
1981 | | - * release it's mapping reference, and if the refcount reaches 0, also be be |
---|
| 2390 | + * release it's mapping reference, and if the refcount reaches 0, also be |
---|
1982 | 2391 | * unmapped, regardless of the value of @reg. |
---|
1983 | 2392 | */ |
---|
1984 | 2393 | void kbase_mem_umm_unmap(struct kbase_context *kctx, |
---|
.. | .. |
---|
2025 | 2434 | unsigned int *target_page_nr, size_t offset); |
---|
2026 | 2435 | |
---|
2027 | 2436 | /** |
---|
2028 | | - * kbase_ctx_reg_zone_end_pfn - return the end Page Frame Number of @zone |
---|
| 2437 | + * kbase_reg_zone_end_pfn - return the end Page Frame Number of @zone |
---|
2029 | 2438 | * @zone: zone to query |
---|
2030 | 2439 | * |
---|
2031 | 2440 | * Return: The end of the zone corresponding to @zone |
---|
.. | .. |
---|
2050 | 2459 | struct kbase_reg_zone *zone; |
---|
2051 | 2460 | |
---|
2052 | 2461 | lockdep_assert_held(&kctx->reg_lock); |
---|
2053 | | - WARN_ON((zone_bits & KBASE_REG_ZONE_MASK) != zone_bits); |
---|
| 2462 | + WARN_ON(!kbase_is_ctx_reg_zone(zone_bits)); |
---|
2054 | 2463 | |
---|
2055 | 2464 | zone = &kctx->reg_zone[KBASE_REG_ZONE_IDX(zone_bits)]; |
---|
2056 | 2465 | *zone = (struct kbase_reg_zone){ |
---|
.. | .. |
---|
2073 | 2482 | kbase_ctx_reg_zone_get_nolock(struct kbase_context *kctx, |
---|
2074 | 2483 | unsigned long zone_bits) |
---|
2075 | 2484 | { |
---|
2076 | | - WARN_ON((zone_bits & KBASE_REG_ZONE_MASK) != zone_bits); |
---|
| 2485 | + WARN_ON(!kbase_is_ctx_reg_zone(zone_bits)); |
---|
2077 | 2486 | |
---|
2078 | 2487 | return &kctx->reg_zone[KBASE_REG_ZONE_IDX(zone_bits)]; |
---|
2079 | 2488 | } |
---|
.. | .. |
---|
2091 | 2500 | kbase_ctx_reg_zone_get(struct kbase_context *kctx, unsigned long zone_bits) |
---|
2092 | 2501 | { |
---|
2093 | 2502 | lockdep_assert_held(&kctx->reg_lock); |
---|
2094 | | - WARN_ON((zone_bits & KBASE_REG_ZONE_MASK) != zone_bits); |
---|
| 2503 | + WARN_ON(!kbase_is_ctx_reg_zone(zone_bits)); |
---|
2095 | 2504 | |
---|
2096 | 2505 | return &kctx->reg_zone[KBASE_REG_ZONE_IDX(zone_bits)]; |
---|
2097 | 2506 | } |
---|
2098 | 2507 | |
---|
| 2508 | +/** |
---|
| 2509 | + * kbase_mem_allow_alloc - Check if allocation of GPU memory is allowed |
---|
| 2510 | + * @kctx: Pointer to kbase context |
---|
| 2511 | + * |
---|
| 2512 | + * Don't allow the allocation of GPU memory if the ioctl has been issued |
---|
| 2513 | + * from the forked child process using the mali device file fd inherited from |
---|
| 2514 | + * the parent process. |
---|
| 2515 | + * |
---|
| 2516 | + * Return: true if allocation is allowed. |
---|
| 2517 | + */ |
---|
| 2518 | +static inline bool kbase_mem_allow_alloc(struct kbase_context *kctx) |
---|
| 2519 | +{ |
---|
| 2520 | + return (kctx->process_mm == current->mm); |
---|
| 2521 | +} |
---|
| 2522 | + |
---|
| 2523 | +/** |
---|
| 2524 | + * kbase_mem_mmgrab - Wrapper function to take reference on mm_struct of current process |
---|
| 2525 | + */ |
---|
| 2526 | +static inline void kbase_mem_mmgrab(void) |
---|
| 2527 | +{ |
---|
| 2528 | + /* This merely takes a reference on the memory descriptor structure |
---|
| 2529 | + * i.e. mm_struct of current process and not on its address space and |
---|
| 2530 | + * so won't block the freeing of address space on process exit. |
---|
| 2531 | + */ |
---|
| 2532 | +#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE |
---|
| 2533 | + atomic_inc(¤t->mm->mm_count); |
---|
| 2534 | +#else |
---|
| 2535 | + mmgrab(current->mm); |
---|
| 2536 | +#endif |
---|
| 2537 | +} |
---|
| 2538 | + |
---|
| 2539 | +/** |
---|
| 2540 | + * kbase_mem_group_id_get - Get group ID from flags |
---|
| 2541 | + * @flags: Flags to pass to base_mem_alloc |
---|
| 2542 | + * |
---|
| 2543 | + * This inline function extracts the encoded group ID from flags |
---|
| 2544 | + * and converts it into numeric value (0~15). |
---|
| 2545 | + * |
---|
| 2546 | + * Return: group ID(0~15) extracted from the parameter |
---|
| 2547 | + */ |
---|
| 2548 | +static inline int kbase_mem_group_id_get(base_mem_alloc_flags flags) |
---|
| 2549 | +{ |
---|
| 2550 | + KBASE_DEBUG_ASSERT((flags & ~BASE_MEM_FLAGS_INPUT_MASK) == 0); |
---|
| 2551 | + return (int)BASE_MEM_GROUP_ID_GET(flags); |
---|
| 2552 | +} |
---|
| 2553 | + |
---|
| 2554 | +/** |
---|
| 2555 | + * kbase_mem_group_id_set - Set group ID into base_mem_alloc_flags |
---|
| 2556 | + * @id: group ID(0~15) you want to encode |
---|
| 2557 | + * |
---|
| 2558 | + * This inline function encodes specific group ID into base_mem_alloc_flags. |
---|
| 2559 | + * Parameter 'id' should lie in-between 0 to 15. |
---|
| 2560 | + * |
---|
| 2561 | + * Return: base_mem_alloc_flags with the group ID (id) encoded |
---|
| 2562 | + * |
---|
| 2563 | + * The return value can be combined with other flags against base_mem_alloc |
---|
| 2564 | + * to identify a specific memory group. |
---|
| 2565 | + */ |
---|
| 2566 | +static inline base_mem_alloc_flags kbase_mem_group_id_set(int id) |
---|
| 2567 | +{ |
---|
| 2568 | + return BASE_MEM_GROUP_ID_SET(id); |
---|
| 2569 | +} |
---|
2099 | 2570 | #endif /* _KBASE_MEM_H_ */ |
---|