3 * (C) COPYRIGHT ARM Limited. All rights reserved.
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
10 * A copy of the licence is included with the program, and can also be obtained
11 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12 * Boston, MA 02110-1301, USA.
21 * @file mali_kbase_mem.h
22 * Base kernel memory APIs
29 #error "Don't include this file directly, use mali_kbase.h instead"
32 #include <malisw/mali_malisw.h>
33 #include <linux/kref.h>
36 #include <linux/ump.h>
37 #endif /* CONFIG_UMP */
38 #include "mali_base_kernel.h"
39 #include <mali_kbase_hw.h>
40 #include "mali_kbase_pm.h"
41 #include "mali_kbase_defs.h"
42 #ifdef CONFIG_MALI_GATOR_SUPPORT
43 #include "mali_kbase_gator.h"
44 #endif /*CONFIG_MALI_GATOR_SUPPORT*/
46 /* Part of the workaround for uTLB invalid pages is to ensure we grow/shrink tmem by 4 pages at a time */
47 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316 (2) /* round to 4 pages */
49 /* Part of the workaround for PRLAM-9630 requires us to grow/shrink memory by 8 pages.
50 The MMU reads in 8 page table entries from memory at a time, if we have more than one page fault within the same 8 pages and
51 page tables are updated accordingly, the MMU does not re-read the page table entries from memory for the subsequent page table
52 updates and generates duplicate page faults as the page table information used by the MMU is not valid. */
53 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630 (3) /* round to 8 pages */
55 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2 (0) /* round to 1 page */
57 /* This must always be a power of 2 */
58 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2)
59 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_8316 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_8316)
60 #define KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_HW_ISSUE_9630 (1u << KBASEP_TMEM_GROWABLE_BLOCKSIZE_PAGES_LOG2_HW_ISSUE_9630)
64 typedef struct kbase_cpu_mapping {
65 struct list_head mappings_list;
66 struct kbase_mem_phy_alloc *alloc;
67 struct kbase_context *kctx;
68 struct kbase_va_region *region;
72 unsigned long vm_start;
76 enum kbase_memory_type {
77 KBASE_MEM_TYPE_NATIVE,
78 KBASE_MEM_TYPE_IMPORTED_UMP,
79 KBASE_MEM_TYPE_IMPORTED_UMM,
85 /* internal structure, mirroring base_mem_aliasing_info,
86 * but with alloc instead of a gpu va (handle) */
87 struct kbase_aliased {
88 struct kbase_mem_phy_alloc *alloc; /* NULL for special, non-NULL for native */
89 u64 offset; /* in pages */
90 u64 length; /* in pages */
93 /* physical pages tracking object.
94 * Set up to track N pages.
95 * N not stored here, the creator holds that info.
96 * This object only tracks how many elements are actually valid (present).
97 * Changing of nents or *pages should only happen if the kbase_mem_phy_alloc is not
98 * shared with another region or client. CPU mappings are OK to exist when changing, as
99 * long as the tracked mappings objects are updated as part of the change.
101 struct kbase_mem_phy_alloc
103 struct kref kref; /* number of users of this alloc */
104 atomic_t gpu_mappings;
105 size_t nents; /* 0..N */
106 phys_addr_t * pages; /* N elements, only 0..nents are valid */
108 /* kbase_cpu_mappings */
109 struct list_head mappings;
112 enum kbase_memory_type type;
116 /* member in union valid based on @a type */
119 ump_dd_handle ump_handle;
120 #endif /* CONFIG_UMP */
121 #if defined(CONFIG_DMA_SHARED_BUFFER)
123 struct dma_buf *dma_buf;
124 struct dma_buf_attachment *dma_attachment;
125 unsigned int current_mapping_usage_count;
126 struct sg_table *sgt;
128 #endif /* defined(CONFIG_DMA_SHARED_BUFFER) */
132 struct kbase_aliased *aliased;
134 /* Used by type = (KBASE_MEM_TYPE_NATIVE, KBASE_MEM_TYPE_TB) */
135 struct kbase_context *kctx;
139 static inline void kbase_mem_phy_alloc_gpu_mapped(struct kbase_mem_phy_alloc *alloc)
141 KBASE_DEBUG_ASSERT(alloc);
142 /* we only track mappings of NATIVE buffers */
143 if (alloc->type == KBASE_MEM_TYPE_NATIVE)
144 atomic_inc(&alloc->gpu_mappings);
147 static inline void kbase_mem_phy_alloc_gpu_unmapped(struct kbase_mem_phy_alloc *alloc)
149 KBASE_DEBUG_ASSERT(alloc);
150 /* we only track mappings of NATIVE buffers */
151 if (alloc->type == KBASE_MEM_TYPE_NATIVE)
152 if (0 > atomic_dec_return(&alloc->gpu_mappings)) {
153 pr_err("Mismatched %s:\n", __func__);
158 void kbase_mem_kref_free(struct kref * kref);
160 mali_error kbase_mem_init(kbase_device * kbdev);
161 void kbase_mem_halt(kbase_device * kbdev);
162 void kbase_mem_term(kbase_device * kbdev);
164 static inline struct kbase_mem_phy_alloc * kbase_mem_phy_alloc_get(struct kbase_mem_phy_alloc * alloc)
166 kref_get(&alloc->kref);
170 static inline struct kbase_mem_phy_alloc * kbase_mem_phy_alloc_put(struct kbase_mem_phy_alloc * alloc)
172 kref_put(&alloc->kref, kbase_mem_kref_free);
177 * A GPU memory region, and attributes for CPU mappings.
179 typedef struct kbase_va_region {
180 struct rb_node rblink;
181 struct list_head link;
183 kbase_context *kctx; /* Backlink to base context */
185 u64 start_pfn; /* The PFN in GPU space */
189 #define KBASE_REG_FREE (1ul << 0)
190 /* CPU write access */
191 #define KBASE_REG_CPU_WR (1ul << 1)
192 /* GPU write access */
193 #define KBASE_REG_GPU_WR (1ul << 2)
194 /* No eXecute flag */
195 #define KBASE_REG_GPU_NX (1ul << 3)
197 #define KBASE_REG_CPU_CACHED (1ul << 4)
199 #define KBASE_REG_GPU_CACHED (1ul << 5)
201 #define KBASE_REG_GROWABLE (1ul << 6)
202 /* Can grow on pf? */
203 #define KBASE_REG_PF_GROW (1ul << 7)
205 /* VA managed by us */
206 #define KBASE_REG_CUSTOM_VA (1ul << 8)
208 /* inner shareable coherency */
209 #define KBASE_REG_SHARE_IN (1ul << 9)
210 /* inner & outer shareable coherency */
211 #define KBASE_REG_SHARE_BOTH (1ul << 10)
213 /* Space for 4 different zones */
214 #define KBASE_REG_ZONE_MASK (3ul << 11)
215 #define KBASE_REG_ZONE(x) (((x) & 3) << 11)
217 /* GPU read access */
218 #define KBASE_REG_GPU_RD (1ul<<13)
219 /* CPU read access */
220 #define KBASE_REG_CPU_RD (1ul<<14)
222 /* Aligned for GPU EX in SAME_VA */
223 #define KBASE_REG_ALIGNED (1ul<<15)
225 /* Index of chosen MEMATTR for this region (0..7) */
226 #define KBASE_REG_MEMATTR_MASK (7ul << 16)
227 #define KBASE_REG_MEMATTR_INDEX(x) (((x) & 7) << 16)
228 #define KBASE_REG_MEMATTR_VALUE(x) (((x) & KBASE_REG_MEMATTR_MASK) >> 16)
230 #define KBASE_REG_ZONE_SAME_VA KBASE_REG_ZONE(0)
232 /* only used with 32-bit clients */
234 * On a 32bit platform, custom VA should be wired from (4GB + shader region)
235 * to the VA limit of the GPU. Unfortunately, the Linux mmap() interface
236 * limits us to 2^32 pages (2^44 bytes, see mmap64 man page for reference).
237 * So we put the default limit to the maximum possible on Linux and shrink
238 * it down, if required by the GPU, during initialization.
240 #define KBASE_REG_ZONE_EXEC KBASE_REG_ZONE(1) /* Dedicated 16MB region for shader code */
241 #define KBASE_REG_ZONE_EXEC_BASE ((1ULL << 32) >> PAGE_SHIFT)
242 #define KBASE_REG_ZONE_EXEC_SIZE ((16ULL * 1024 * 1024) >> PAGE_SHIFT)
244 #define KBASE_REG_ZONE_CUSTOM_VA KBASE_REG_ZONE(2)
245 #define KBASE_REG_ZONE_CUSTOM_VA_BASE (KBASE_REG_ZONE_EXEC_BASE + KBASE_REG_ZONE_EXEC_SIZE) /* Starting after KBASE_REG_ZONE_EXEC */
246 #define KBASE_REG_ZONE_CUSTOM_VA_SIZE (((1ULL << 44) >> PAGE_SHIFT) - KBASE_REG_ZONE_CUSTOM_VA_BASE)
247 /* end 32-bit clients only */
251 size_t extent; /* nr of pages alloc'd on PF */
253 struct kbase_mem_phy_alloc * alloc; /* the one alloc object we mmap to the GPU and CPU when mapping this region */
255 /* non-NULL if this memory object is a kds_resource */
256 struct kds_resource *kds_res;
260 /* Common functions */
261 static INLINE phys_addr_t *kbase_get_phy_pages(struct kbase_va_region *reg)
263 KBASE_DEBUG_ASSERT(reg);
264 KBASE_DEBUG_ASSERT(reg->alloc);
266 return reg->alloc->pages;
269 static INLINE size_t kbase_reg_current_backed_size(struct kbase_va_region * reg)
271 KBASE_DEBUG_ASSERT(reg);
272 /* if no alloc object the backed size naturally is 0 */
274 return reg->alloc->nents;
279 static INLINE struct kbase_mem_phy_alloc * kbase_alloc_create(size_t nr_pages, enum kbase_memory_type type)
281 struct kbase_mem_phy_alloc *alloc;
283 /* Prevent nr_pages*sizeof + sizeof(*alloc) from wrapping around. */
284 if (nr_pages > ((((size_t) -1) - sizeof(*alloc)) / sizeof(*alloc->pages)))
285 return ERR_PTR(-ENOMEM);
287 alloc = vzalloc(sizeof(*alloc) + sizeof(*alloc->pages) * nr_pages);
289 return ERR_PTR(-ENOMEM);
291 kref_init(&alloc->kref);
292 atomic_set(&alloc->gpu_mappings, 0);
294 alloc->pages = (void*)(alloc + 1);
295 INIT_LIST_HEAD(&alloc->mappings);
301 static INLINE int kbase_reg_prepare_native(struct kbase_va_region * reg, struct kbase_context * kctx)
303 KBASE_DEBUG_ASSERT(reg);
304 KBASE_DEBUG_ASSERT(!reg->alloc);
305 KBASE_DEBUG_ASSERT(reg->flags & KBASE_REG_FREE);
307 reg->alloc = kbase_alloc_create(reg->nr_pages, KBASE_MEM_TYPE_NATIVE);
308 if (IS_ERR(reg->alloc))
309 return PTR_ERR(reg->alloc);
310 else if (!reg->alloc)
312 reg->alloc->imported.kctx = kctx;
313 reg->flags &= ~KBASE_REG_FREE;
317 static inline int kbase_atomic_add_pages(int num_pages, atomic_t *used_pages)
319 int new_val = atomic_add_return(num_pages, used_pages);
320 #ifdef CONFIG_MALI_GATOR_SUPPORT
321 kbase_trace_mali_total_alloc_pages_change((long long int)new_val);
326 static inline int kbase_atomic_sub_pages(int num_pages, atomic_t *used_pages)
328 int new_val = atomic_sub_return(num_pages, used_pages);
329 #ifdef CONFIG_MALI_GATOR_SUPPORT
330 kbase_trace_mali_total_alloc_pages_change((long long int)new_val);
336 * @brief Initialize an OS based memory allocator.
338 * Initializes a allocator.
339 * Must be called before any allocation is attempted.
340 * \a kbase_mem_allocator_alloc and \a kbase_mem_allocator_free is used
341 * to allocate and free memory.
342 * \a kbase_mem_allocator_term must be called to clean up the allocator.
343 * All memory obtained via \a kbase_mem_allocator_alloc must have been
344 * \a kbase_mem_allocator_free before \a kbase_mem_allocator_term is called.
346 * @param allocator Allocator object to initialize
347 * @param max_size Maximum number of pages to keep on the freelist.
348 * @return MALI_ERROR_NONE on success, an error code indicating what failed on error.
350 mali_error kbase_mem_allocator_init(kbase_mem_allocator * allocator, unsigned int max_size);
353 * @brief Allocate memory via an OS based memory allocator.
355 * @param[in] allocator Allocator to obtain the memory from
356 * @param nr_pages Number of pages to allocate
357 * @param[out] pages Pointer to an array where the physical address of the allocated pages will be stored
358 * @return MALI_ERROR_NONE if the pages were allocated, an error code indicating what failed on error
360 mali_error kbase_mem_allocator_alloc(kbase_mem_allocator * allocator, size_t nr_pages, phys_addr_t *pages);
363 * @brief Free memory obtained for an OS based memory allocator.
365 * @param[in] allocator Allocator to free the memory back to
366 * @param nr_pages Number of pages to free
367 * @param[in] pages Pointer to an array holding the physical address of the paghes to free.
368 * @param[in] sync_back MALI_TRUE case the memory should be synced back
370 void kbase_mem_allocator_free(kbase_mem_allocator * allocator, size_t nr_pages, phys_addr_t *pages, mali_bool sync_back);
373 * @brief Terminate an OS based memory allocator.
375 * Frees all cached allocations and clean up internal state.
376 * All allocate pages must have been \a kbase_mem_allocator_free before
377 * this function is called.
379 * @param[in] allocator Allocator to terminate
381 void kbase_mem_allocator_term(kbase_mem_allocator * allocator);
385 mali_error kbase_region_tracker_init(kbase_context *kctx);
386 void kbase_region_tracker_term(kbase_context *kctx);
388 struct kbase_va_region *kbase_region_tracker_find_region_enclosing_range(kbase_context *kctx, u64 start_pgoff, size_t nr_pages);
390 struct kbase_va_region *kbase_region_tracker_find_region_enclosing_address(kbase_context *kctx, mali_addr64 gpu_addr);
393 * @brief Check that a pointer is actually a valid region.
395 * Must be called with context lock held.
397 struct kbase_va_region *kbase_region_tracker_find_region_base_address(kbase_context *kctx, mali_addr64 gpu_addr);
399 struct kbase_va_region *kbase_alloc_free_region(kbase_context *kctx, u64 start_pfn, size_t nr_pages, int zone);
400 void kbase_free_alloced_region(struct kbase_va_region *reg);
401 mali_error kbase_add_va_region(kbase_context *kctx, struct kbase_va_region *reg, mali_addr64 addr, size_t nr_pages, size_t align);
403 mali_error kbase_gpu_mmap(kbase_context *kctx, struct kbase_va_region *reg, mali_addr64 addr, size_t nr_pages, size_t align);
404 mali_bool kbase_check_alloc_flags(unsigned long flags);
405 void kbase_update_region_flags(struct kbase_va_region *reg, unsigned long flags);
407 void kbase_gpu_vm_lock(kbase_context *kctx);
408 void kbase_gpu_vm_unlock(kbase_context *kctx);
410 int kbase_alloc_phy_pages(struct kbase_va_region *reg, size_t vsize, size_t size);
412 mali_error kbase_mmu_init(kbase_context *kctx);
413 void kbase_mmu_term(kbase_context *kctx);
415 phys_addr_t kbase_mmu_alloc_pgd(kbase_context *kctx);
416 void kbase_mmu_free_pgd(kbase_context *kctx);
417 mali_error kbase_mmu_insert_pages(kbase_context *kctx, u64 vpfn,
418 phys_addr_t *phys, size_t nr,
419 unsigned long flags);
420 mali_error kbase_mmu_insert_single_page(kbase_context *kctx, u64 vpfn,
421 phys_addr_t phys, size_t nr,
422 unsigned long flags);
424 mali_error kbase_mmu_teardown_pages(kbase_context *kctx, u64 vpfn, size_t nr);
425 mali_error kbase_mmu_update_pages(kbase_context* kctx, u64 vpfn, phys_addr_t* phys, size_t nr, unsigned long flags);
428 * @brief Register region and map it on the GPU.
430 * Call kbase_add_va_region() and map the region on the GPU.
432 mali_error kbase_gpu_mmap(kbase_context *kctx, struct kbase_va_region *reg, mali_addr64 addr, size_t nr_pages, size_t align);
435 * @brief Remove the region from the GPU and unregister it.
437 * Must be called with context lock held.
439 mali_error kbase_gpu_munmap(kbase_context *kctx, struct kbase_va_region *reg);
442 * The caller has the following locking conditions:
443 * - It must hold kbase_as::transaction_mutex on kctx's address space
444 * - It must hold the kbasep_js_device_data::runpool_irq::lock
446 void kbase_mmu_update(kbase_context *kctx);
449 * The caller has the following locking conditions:
450 * - It must hold kbase_as::transaction_mutex on kctx's address space
451 * - It must hold the kbasep_js_device_data::runpool_irq::lock
453 void kbase_mmu_disable(kbase_context *kctx);
455 void kbase_mmu_interrupt(kbase_device *kbdev, u32 irq_stat);
457 /** Dump the MMU tables to a buffer
459 * This function allocates a buffer (of @c nr_pages pages) to hold a dump of the MMU tables and fills it. If the
460 * buffer is too small then the return value will be NULL.
462 * The GPU vm lock must be held when calling this function.
464 * The buffer returned should be freed with @ref vfree when it is no longer required.
466 * @param[in] kctx The kbase context to dump
467 * @param[in] nr_pages The number of pages to allocate for the buffer.
469 * @return The address of the buffer containing the MMU dump or NULL on error (including if the @c nr_pages is too
472 void *kbase_mmu_dump(kbase_context *kctx, int nr_pages);
474 mali_error kbase_sync_now(kbase_context *kctx, base_syncset *syncset);
475 void kbase_pre_job_sync(kbase_context *kctx, base_syncset *syncsets, size_t nr);
476 void kbase_post_job_sync(kbase_context *kctx, base_syncset *syncsets, size_t nr);
479 * Set attributes for imported tmem region
481 * This function sets (extends with) requested attributes for given region
482 * of imported external memory
484 * @param[in] kctx The kbase context which the tmem belongs to
485 * @param[in] gpu_adr The base address of the tmem region
486 * @param[in] attributes The attributes of tmem region to be set
488 * @return MALI_ERROR_NONE on success. Any other value indicates failure.
490 mali_error kbase_tmem_set_attributes(kbase_context *kctx, mali_addr64 gpu_adr, u32 attributes );
493 * Get attributes of imported tmem region
495 * This function retrieves the attributes of imported external memory
497 * @param[in] kctx The kbase context which the tmem belongs to
498 * @param[in] gpu_adr The base address of the tmem region
499 * @param[out] attributes The actual attributes of tmem region
501 * @return MALI_ERROR_NONE on success. Any other value indicates failure.
503 mali_error kbase_tmem_get_attributes(kbase_context *kctx, mali_addr64 gpu_adr, u32 * const attributes );
505 /* OS specific functions */
506 mali_error kbase_mem_free(kbase_context *kctx, mali_addr64 gpu_addr);
507 mali_error kbase_mem_free_region(kbase_context *kctx, struct kbase_va_region *reg);
508 void kbase_os_mem_map_lock(kbase_context *kctx);
509 void kbase_os_mem_map_unlock(kbase_context *kctx);
512 * @brief Update the memory allocation counters for the current process
514 * OS specific call to updates the current memory allocation counters for the current process with
515 * the supplied delta.
517 * @param[in] kctx The kbase context
518 * @param[in] pages The desired delta to apply to the memory usage counters.
521 void kbasep_os_process_page_usage_update( struct kbase_context * kctx, int pages );
524 * @brief Add to the memory allocation counters for the current process
526 * OS specific call to add to the current memory allocation counters for the current process by
527 * the supplied amount.
529 * @param[in] kctx The kernel base context used for the allocation.
530 * @param[in] pages The desired delta to apply to the memory usage counters.
533 static INLINE void kbase_process_page_usage_inc( struct kbase_context *kctx, int pages )
535 kbasep_os_process_page_usage_update( kctx, pages );
539 * @brief Subtract from the memory allocation counters for the current process
541 * OS specific call to subtract from the current memory allocation counters for the current process by
542 * the supplied amount.
544 * @param[in] kctx The kernel base context used for the allocation.
545 * @param[in] pages The desired delta to apply to the memory usage counters.
548 static INLINE void kbase_process_page_usage_dec( struct kbase_context *kctx, int pages )
550 kbasep_os_process_page_usage_update( kctx, 0 - pages );
554 * @brief Find the offset of the CPU mapping of a memory allocation containing
555 * a given address range
557 * Searches for a CPU mapping of any part of the region starting at @p gpu_addr
558 * that fully encloses the CPU virtual address range specified by @p uaddr and
559 * @p size. Returns a failure indication if only part of the address range lies
560 * within a CPU mapping, or the address range lies within a CPU mapping of a
563 * @param[in,out] kctx The kernel base context used for the allocation.
564 * @param[in] gpu_addr GPU address of the start of the allocated region
565 * within which to search.
566 * @param[in] uaddr Start of the CPU virtual address range.
567 * @param[in] size Size of the CPU virtual address range (in bytes).
568 * @param[out] offset The offset from the start of the allocation to the
569 * specified CPU virtual address.
571 * @return MALI_ERROR_NONE if offset was obtained successfully. Error code
574 mali_error kbasep_find_enclosing_cpu_mapping_offset(kbase_context *kctx,
575 mali_addr64 gpu_addr,
578 mali_size64 *offset);
580 enum hrtimer_restart kbasep_as_poke_timer_callback(struct hrtimer *timer);
581 void kbase_as_poking_timer_retain_atom(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *katom);
582 void kbase_as_poking_timer_release_atom(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *katom);
585 * @brief Allocates physical pages.
587 * Allocates \a nr_pages_requested and updates the alloc object.
589 * @param[in] alloc allocation object to add pages to
590 * @param[in] nr_pages_requested number of physical pages to allocate
592 * @return 0 if all pages have been successfully allocated. Error code otherwise
594 int kbase_alloc_phy_pages_helper(struct kbase_mem_phy_alloc * alloc, size_t nr_pages_requested);
597 * @brief Free physical pages.
599 * Frees \a nr_pages and updates the alloc object.
601 * @param[in] alloc allocation object to free pages from
602 * @param[in] nr_pages_to_free number of physical pages to free
604 int kbase_free_phy_pages_helper(struct kbase_mem_phy_alloc * alloc, size_t nr_pages_to_free);
606 #ifdef CONFIG_MALI_NO_MALI
607 static inline void kbase_wait_write_flush(kbase_context *kctx)
611 void kbase_wait_write_flush(kbase_context *kctx);
615 #endif /* _KBASE_MEM_H_ */