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