xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/mali_kbase_defs.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * (C) COPYRIGHT 2011-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: Defintions (types, defines, etcs) common to Kbase. They are placed here
24*4882a593Smuzhiyun  * to allow the hierarchy of header files to work.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #ifndef _KBASE_DEFS_H_
28*4882a593Smuzhiyun #define _KBASE_DEFS_H_
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <mali_kbase_config.h>
31*4882a593Smuzhiyun #include <mali_base_hwconfig_features.h>
32*4882a593Smuzhiyun #include <mali_base_hwconfig_issues.h>
33*4882a593Smuzhiyun #include <mali_kbase_mem_lowlevel.h>
34*4882a593Smuzhiyun #include <mmu/mali_kbase_mmu_hw.h>
35*4882a593Smuzhiyun #include <backend/gpu/mali_kbase_instr_defs.h>
36*4882a593Smuzhiyun #include <mali_kbase_pm.h>
37*4882a593Smuzhiyun #include <mali_kbase_gpuprops_types.h>
38*4882a593Smuzhiyun #include <hwcnt/mali_kbase_hwcnt_watchdog_if.h>
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #if MALI_USE_CSF
41*4882a593Smuzhiyun #include <hwcnt/backend/mali_kbase_hwcnt_backend_csf.h>
42*4882a593Smuzhiyun #else
43*4882a593Smuzhiyun #include <hwcnt/backend/mali_kbase_hwcnt_backend_jm.h>
44*4882a593Smuzhiyun #include <hwcnt/backend/mali_kbase_hwcnt_backend_jm_watchdog.h>
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #include <protected_mode_switcher.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include <linux/atomic.h>
50*4882a593Smuzhiyun #include <linux/mempool.h>
51*4882a593Smuzhiyun #include <linux/slab.h>
52*4882a593Smuzhiyun #include <linux/file.h>
53*4882a593Smuzhiyun #include <linux/sizes.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #include "mali_kbase_fence_defs.h"
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DEBUG_FS)
59*4882a593Smuzhiyun #include <linux/debugfs.h>
60*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #ifdef CONFIG_MALI_BIFROST_DEVFREQ
63*4882a593Smuzhiyun #include <linux/devfreq.h>
64*4882a593Smuzhiyun #endif /* CONFIG_MALI_BIFROST_DEVFREQ */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
67*4882a593Smuzhiyun #include <linux/devfreq_cooling.h>
68*4882a593Smuzhiyun #endif
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #ifdef CONFIG_MALI_ARBITER_SUPPORT
71*4882a593Smuzhiyun #include <arbiter/mali_kbase_arbiter_defs.h>
72*4882a593Smuzhiyun #endif /* CONFIG_MALI_ARBITER_SUPPORT */
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #include <linux/clk.h>
75*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
76*4882a593Smuzhiyun #include <linux/memory_group_manager.h>
77*4882a593Smuzhiyun #include <soc/rockchip/rockchip_opp_select.h>
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #include "debug/mali_kbase_debug_ktrace_defs.h"
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /** Number of milliseconds before we time out on a GPU soft/hard reset */
82*4882a593Smuzhiyun #define RESET_TIMEOUT           500
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun  * BASE_JM_MAX_NR_SLOTS - The maximum number of Job Slots to support in the Hardware.
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * You can optimize this down if your target devices will only ever support a
88*4882a593Smuzhiyun  * small number of job slots.
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun #define BASE_JM_MAX_NR_SLOTS        3
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * BASE_MAX_NR_AS - The maximum number of Address Spaces to support in the Hardware.
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * You can optimize this down if your target devices will only ever support a
96*4882a593Smuzhiyun  * small number of Address Spaces
97*4882a593Smuzhiyun  */
98*4882a593Smuzhiyun #define BASE_MAX_NR_AS              16
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /* mmu */
101*4882a593Smuzhiyun #define MIDGARD_MMU_LEVEL(x) (x)
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #define MIDGARD_MMU_TOPLEVEL    MIDGARD_MMU_LEVEL(0)
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define MIDGARD_MMU_BOTTOMLEVEL MIDGARD_MMU_LEVEL(3)
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun #define GROWABLE_FLAGS_REQUIRED (KBASE_REG_PF_GROW | KBASE_REG_GPU_WR)
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /** setting in kbase_context::as_nr that indicates it's invalid */
110*4882a593Smuzhiyun #define KBASEP_AS_NR_INVALID     (-1)
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun  * KBASE_LOCK_REGION_MAX_SIZE_LOG2 - Maximum size in bytes of a MMU lock region,
114*4882a593Smuzhiyun  *                                   as a logarithm
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun #define KBASE_LOCK_REGION_MAX_SIZE_LOG2 (48) /*  256 TB */
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun  * KBASE_REG_ZONE_MAX - Maximum number of GPU memory region zones
120*4882a593Smuzhiyun  */
121*4882a593Smuzhiyun #if MALI_USE_CSF
122*4882a593Smuzhiyun #define KBASE_REG_ZONE_MAX 6ul
123*4882a593Smuzhiyun #else
124*4882a593Smuzhiyun #define KBASE_REG_ZONE_MAX 4ul
125*4882a593Smuzhiyun #endif
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #include "mali_kbase_hwaccess_defs.h"
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* Maximum number of pages of memory that require a permanent mapping, per
130*4882a593Smuzhiyun  * kbase_context
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun #define KBASE_PERMANENTLY_MAPPED_MEM_LIMIT_PAGES ((64 * 1024ul * 1024ul) >> PAGE_SHIFT)
133*4882a593Smuzhiyun /* Minimum threshold period for hwcnt dumps between different hwcnt virtualizer
134*4882a593Smuzhiyun  * clients, to reduce undesired system load.
135*4882a593Smuzhiyun  * If a virtualizer client requests a dump within this threshold period after
136*4882a593Smuzhiyun  * some other client has performed a dump, a new dump won't be performed and
137*4882a593Smuzhiyun  * the accumulated counter values for that client will be returned instead.
138*4882a593Smuzhiyun  */
139*4882a593Smuzhiyun #define KBASE_HWCNT_GPU_VIRTUALIZER_DUMP_THRESHOLD_NS (200 * NSEC_PER_USEC)
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun #if MALI_USE_CSF
142*4882a593Smuzhiyun /* The buffer count of CSF hwcnt backend ring buffer, which is used when CSF
143*4882a593Smuzhiyun  * hwcnt backend allocate the ring buffer to communicate with CSF firmware for
144*4882a593Smuzhiyun  * HWC dump samples.
145*4882a593Smuzhiyun  * To meet the hardware requirement, this number MUST be power of 2, otherwise,
146*4882a593Smuzhiyun  * CSF hwcnt backend creation will be failed.
147*4882a593Smuzhiyun  */
148*4882a593Smuzhiyun #define KBASE_HWCNT_BACKEND_CSF_RING_BUFFER_COUNT (128)
149*4882a593Smuzhiyun #endif
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /* Maximum number of clock/regulator pairs that may be referenced by
152*4882a593Smuzhiyun  * the device node.
153*4882a593Smuzhiyun  * This is dependent on support for of_property_read_u64_array() in the
154*4882a593Smuzhiyun  * kernel.
155*4882a593Smuzhiyun  * While, the number of clocks could be more than regulators,
156*4882a593Smuzhiyun  * as mentioned in power_control_init().
157*4882a593Smuzhiyun  */
158*4882a593Smuzhiyun #define BASE_MAX_NR_CLOCKS_REGULATORS (4)
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* Forward declarations */
161*4882a593Smuzhiyun struct kbase_context;
162*4882a593Smuzhiyun struct kbase_device;
163*4882a593Smuzhiyun struct kbase_as;
164*4882a593Smuzhiyun struct kbase_mmu_setup;
165*4882a593Smuzhiyun struct kbase_kinstr_jm;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * struct kbase_io_access - holds information about 1 register access
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * @addr: first bit indicates r/w (r=0, w=1)
171*4882a593Smuzhiyun  * @value: value written or read
172*4882a593Smuzhiyun  */
173*4882a593Smuzhiyun struct kbase_io_access {
174*4882a593Smuzhiyun 	uintptr_t addr;
175*4882a593Smuzhiyun 	u32 value;
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /**
179*4882a593Smuzhiyun  * struct kbase_io_history - keeps track of all recent register accesses
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * @enabled: true if register accesses are recorded, false otherwise
182*4882a593Smuzhiyun  * @lock: spinlock protecting kbase_io_access array
183*4882a593Smuzhiyun  * @count: number of registers read/written
184*4882a593Smuzhiyun  * @size: number of elements in kbase_io_access array
185*4882a593Smuzhiyun  * @buf: array of kbase_io_access
186*4882a593Smuzhiyun  */
187*4882a593Smuzhiyun struct kbase_io_history {
188*4882a593Smuzhiyun 	bool enabled;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	spinlock_t lock;
191*4882a593Smuzhiyun 	size_t count;
192*4882a593Smuzhiyun 	u16 size;
193*4882a593Smuzhiyun 	struct kbase_io_access *buf;
194*4882a593Smuzhiyun };
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun  * struct kbase_debug_copy_buffer - information about the buffer to be copied.
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * @size:	size of the buffer in bytes
200*4882a593Smuzhiyun  * @pages:	pointer to an array of pointers to the pages which contain
201*4882a593Smuzhiyun  *		the buffer
202*4882a593Smuzhiyun  * @is_vmalloc: true if @pages was allocated with vzalloc. false if @pages was
203*4882a593Smuzhiyun  *              allocated with kcalloc
204*4882a593Smuzhiyun  * @nr_pages:	number of pages
205*4882a593Smuzhiyun  * @offset:	offset into the pages
206*4882a593Smuzhiyun  * @gpu_alloc:	pointer to physical memory allocated by the GPU
207*4882a593Smuzhiyun  * @extres_pages: array of pointers to the pages containing external resources
208*4882a593Smuzhiyun  *		for this buffer
209*4882a593Smuzhiyun  * @nr_extres_pages: number of pages in @extres_pages
210*4882a593Smuzhiyun  */
211*4882a593Smuzhiyun struct kbase_debug_copy_buffer {
212*4882a593Smuzhiyun 	size_t size;
213*4882a593Smuzhiyun 	struct page **pages;
214*4882a593Smuzhiyun 	bool is_vmalloc;
215*4882a593Smuzhiyun 	int nr_pages;
216*4882a593Smuzhiyun 	size_t offset;
217*4882a593Smuzhiyun 	struct kbase_mem_phy_alloc *gpu_alloc;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	struct page **extres_pages;
220*4882a593Smuzhiyun 	int nr_extres_pages;
221*4882a593Smuzhiyun };
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun struct kbase_device_info {
224*4882a593Smuzhiyun 	u32 features;
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun struct kbase_mmu_setup {
228*4882a593Smuzhiyun 	u64	transtab;
229*4882a593Smuzhiyun 	u64	memattr;
230*4882a593Smuzhiyun 	u64	transcfg;
231*4882a593Smuzhiyun };
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun /**
234*4882a593Smuzhiyun  * struct kbase_fault - object containing data relating to a page or bus fault.
235*4882a593Smuzhiyun  * @addr:           Records the faulting address.
236*4882a593Smuzhiyun  * @extra_addr:     Records the secondary fault address.
237*4882a593Smuzhiyun  * @status:         Records the fault status as reported by Hw.
238*4882a593Smuzhiyun  * @protected_mode: Flag indicating whether the fault occurred in protected mode
239*4882a593Smuzhiyun  *                  or not.
240*4882a593Smuzhiyun  */
241*4882a593Smuzhiyun struct kbase_fault {
242*4882a593Smuzhiyun 	u64 addr;
243*4882a593Smuzhiyun 	u64 extra_addr;
244*4882a593Smuzhiyun 	u32 status;
245*4882a593Smuzhiyun 	bool protected_mode;
246*4882a593Smuzhiyun };
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /** Maximum number of memory pages that should be allocated for the array
249*4882a593Smuzhiyun  * of pointers to free PGDs.
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  * This number has been pre-calculated to deal with the maximum allocation
252*4882a593Smuzhiyun  * size expressed by the default value of KBASE_MEM_ALLOC_MAX_SIZE.
253*4882a593Smuzhiyun  * This is supposed to be enough for almost the entirety of MMU operations.
254*4882a593Smuzhiyun  * Any size greater than KBASE_MEM_ALLOC_MAX_SIZE requires being broken down
255*4882a593Smuzhiyun  * into multiple iterations, each dealing with at most KBASE_MEM_ALLOC_MAX_SIZE
256*4882a593Smuzhiyun  * bytes.
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Please update this value if KBASE_MEM_ALLOC_MAX_SIZE changes.
259*4882a593Smuzhiyun  */
260*4882a593Smuzhiyun #define MAX_PAGES_FOR_FREE_PGDS ((size_t)9)
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /* Maximum number of pointers to free PGDs */
263*4882a593Smuzhiyun #define MAX_FREE_PGDS ((PAGE_SIZE / sizeof(struct page *)) * MAX_PAGES_FOR_FREE_PGDS)
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun /**
266*4882a593Smuzhiyun  * struct kbase_mmu_table  - object representing a set of GPU page tables
267*4882a593Smuzhiyun  * @mmu_lock:             Lock to serialize the accesses made to multi level GPU
268*4882a593Smuzhiyun  *                        page tables
269*4882a593Smuzhiyun  * @pgd:                  Physical address of the page allocated for the top
270*4882a593Smuzhiyun  *                        level page table of the context, this is used for
271*4882a593Smuzhiyun  *                        MMU HW programming as the address translation will
272*4882a593Smuzhiyun  *                        start from the top level page table.
273*4882a593Smuzhiyun  * @group_id:             A memory group ID to be passed to a platform-specific
274*4882a593Smuzhiyun  *                        memory group manager.
275*4882a593Smuzhiyun  *                        Valid range is 0..(MEMORY_GROUP_MANAGER_NR_GROUPS-1).
276*4882a593Smuzhiyun  * @kctx:                 If this set of MMU tables belongs to a context then
277*4882a593Smuzhiyun  *                        this is a back-reference to the context, otherwise
278*4882a593Smuzhiyun  *                        it is NULL.
279*4882a593Smuzhiyun  * @scratch_mem:          Scratch memory used for MMU operations, which are
280*4882a593Smuzhiyun  *                        serialized by the @mmu_lock.
281*4882a593Smuzhiyun  */
282*4882a593Smuzhiyun struct kbase_mmu_table {
283*4882a593Smuzhiyun 	struct mutex mmu_lock;
284*4882a593Smuzhiyun 	phys_addr_t pgd;
285*4882a593Smuzhiyun 	u8 group_id;
286*4882a593Smuzhiyun 	struct kbase_context *kctx;
287*4882a593Smuzhiyun 	union {
288*4882a593Smuzhiyun 		/**
289*4882a593Smuzhiyun 		 * @teardown_pages: Scratch memory used for backup copies of whole
290*4882a593Smuzhiyun 		 *                  PGD pages when tearing down levels upon
291*4882a593Smuzhiyun 		 *                  termination of the MMU table.
292*4882a593Smuzhiyun 		 */
293*4882a593Smuzhiyun 		struct {
294*4882a593Smuzhiyun 			/**
295*4882a593Smuzhiyun 			 * @levels: Array of PGD pages, large enough to copy one PGD
296*4882a593Smuzhiyun 			 *          for each level of the MMU table.
297*4882a593Smuzhiyun 			 */
298*4882a593Smuzhiyun 			u64 levels[MIDGARD_MMU_BOTTOMLEVEL][PAGE_SIZE / sizeof(u64)];
299*4882a593Smuzhiyun 		} teardown_pages;
300*4882a593Smuzhiyun 		/**
301*4882a593Smuzhiyun 		 * @free_pgds: Scratch memory user for insertion, update and teardown
302*4882a593Smuzhiyun 		 *             operations to store a temporary list of PGDs to be freed
303*4882a593Smuzhiyun 		 *             at the end of the operation.
304*4882a593Smuzhiyun 		 */
305*4882a593Smuzhiyun 		struct {
306*4882a593Smuzhiyun 			/** @pgds: Array of pointers to PGDs to free. */
307*4882a593Smuzhiyun 			struct page *pgds[MAX_FREE_PGDS];
308*4882a593Smuzhiyun 			/** @head_index: Index of first free element in the PGDs array. */
309*4882a593Smuzhiyun 			size_t head_index;
310*4882a593Smuzhiyun 		} free_pgds;
311*4882a593Smuzhiyun 	} scratch_mem;
312*4882a593Smuzhiyun };
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun /**
315*4882a593Smuzhiyun  * struct kbase_reg_zone - Information about GPU memory region zones
316*4882a593Smuzhiyun  * @base_pfn: Page Frame Number in GPU virtual address space for the start of
317*4882a593Smuzhiyun  *            the Zone
318*4882a593Smuzhiyun  * @va_size_pages: Size of the Zone in pages
319*4882a593Smuzhiyun  *
320*4882a593Smuzhiyun  * Track information about a zone KBASE_REG_ZONE() and related macros.
321*4882a593Smuzhiyun  * In future, this could also store the &rb_root that are currently in
322*4882a593Smuzhiyun  * &kbase_context and &kbase_csf_device.
323*4882a593Smuzhiyun  */
324*4882a593Smuzhiyun struct kbase_reg_zone {
325*4882a593Smuzhiyun 	u64 base_pfn;
326*4882a593Smuzhiyun 	u64 va_size_pages;
327*4882a593Smuzhiyun };
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun #if MALI_USE_CSF
330*4882a593Smuzhiyun #include "csf/mali_kbase_csf_defs.h"
331*4882a593Smuzhiyun #else
332*4882a593Smuzhiyun #include "jm/mali_kbase_jm_defs.h"
333*4882a593Smuzhiyun #endif
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun #include "mali_kbase_hwaccess_time.h"
336*4882a593Smuzhiyun 
kbase_as_has_bus_fault(struct kbase_as * as,struct kbase_fault * fault)337*4882a593Smuzhiyun static inline int kbase_as_has_bus_fault(struct kbase_as *as,
338*4882a593Smuzhiyun 	struct kbase_fault *fault)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	return (fault == &as->bf_data);
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
kbase_as_has_page_fault(struct kbase_as * as,struct kbase_fault * fault)343*4882a593Smuzhiyun static inline int kbase_as_has_page_fault(struct kbase_as *as,
344*4882a593Smuzhiyun 	struct kbase_fault *fault)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	return (fault == &as->pf_data);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /**
350*4882a593Smuzhiyun  * struct kbasep_mem_device - Data stored per device for memory allocation
351*4882a593Smuzhiyun  *
352*4882a593Smuzhiyun  * @used_pages:   Tracks usage of OS shared memory. Updated when OS memory is
353*4882a593Smuzhiyun  *                allocated/freed.
354*4882a593Smuzhiyun  * @ir_threshold: Fraction of the maximum size of an allocation that grows
355*4882a593Smuzhiyun  *                on GPU page fault that can be used before the driver
356*4882a593Smuzhiyun  *                switches to incremental rendering, in 1/256ths.
357*4882a593Smuzhiyun  *                0 means disabled.
358*4882a593Smuzhiyun  */
359*4882a593Smuzhiyun struct kbasep_mem_device {
360*4882a593Smuzhiyun 	atomic_t used_pages;
361*4882a593Smuzhiyun 	atomic_t ir_threshold;
362*4882a593Smuzhiyun };
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun struct kbase_clk_rate_listener;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun  * typedef kbase_clk_rate_listener_on_change_t() - Frequency change callback
368*4882a593Smuzhiyun  *
369*4882a593Smuzhiyun  * @listener:     Clock frequency change listener.
370*4882a593Smuzhiyun  * @clk_index:    Index of the clock for which the change has occurred.
371*4882a593Smuzhiyun  * @clk_rate_hz:  Clock frequency(Hz).
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  * A callback to call when clock rate changes. The function must not
374*4882a593Smuzhiyun  * sleep. No clock rate manager functions must be called from here, as
375*4882a593Smuzhiyun  * its lock is taken.
376*4882a593Smuzhiyun  */
377*4882a593Smuzhiyun typedef void
378*4882a593Smuzhiyun kbase_clk_rate_listener_on_change_t(struct kbase_clk_rate_listener *listener,
379*4882a593Smuzhiyun 				    u32 clk_index, u32 clk_rate_hz);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /**
382*4882a593Smuzhiyun  * struct kbase_clk_rate_listener - Clock frequency listener
383*4882a593Smuzhiyun  *
384*4882a593Smuzhiyun  * @node:        List node.
385*4882a593Smuzhiyun  * @notify:    Callback to be called when GPU frequency changes.
386*4882a593Smuzhiyun  */
387*4882a593Smuzhiyun struct kbase_clk_rate_listener {
388*4882a593Smuzhiyun 	struct list_head node;
389*4882a593Smuzhiyun 	kbase_clk_rate_listener_on_change_t *notify;
390*4882a593Smuzhiyun };
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun /**
393*4882a593Smuzhiyun  * struct kbase_clk_rate_trace_manager - Data stored per device for GPU clock
394*4882a593Smuzhiyun  *                                       rate trace manager.
395*4882a593Smuzhiyun  *
396*4882a593Smuzhiyun  * @gpu_idle:           Tracks the idle state of GPU.
397*4882a593Smuzhiyun  * @clks:               Array of pointer to structures storing data for every
398*4882a593Smuzhiyun  *                      enumerated GPU clock.
399*4882a593Smuzhiyun  * @clk_rate_trace_ops: Pointer to the platform specific GPU clock rate trace
400*4882a593Smuzhiyun  *                      operations.
401*4882a593Smuzhiyun  * @listeners:          List of listener attached.
402*4882a593Smuzhiyun  * @lock:               Lock to serialize the actions of GPU clock rate trace
403*4882a593Smuzhiyun  *                      manager.
404*4882a593Smuzhiyun  */
405*4882a593Smuzhiyun struct kbase_clk_rate_trace_manager {
406*4882a593Smuzhiyun 	bool gpu_idle;
407*4882a593Smuzhiyun 	struct kbase_clk_data *clks[BASE_MAX_NR_CLOCKS_REGULATORS];
408*4882a593Smuzhiyun 	struct kbase_clk_rate_trace_op_conf *clk_rate_trace_ops;
409*4882a593Smuzhiyun 	struct list_head listeners;
410*4882a593Smuzhiyun 	spinlock_t lock;
411*4882a593Smuzhiyun };
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun /**
414*4882a593Smuzhiyun  * struct kbase_pm_device_data - Data stored per device for power management.
415*4882a593Smuzhiyun  * @lock: The lock protecting Power Management structures accessed
416*4882a593Smuzhiyun  *        outside of IRQ.
417*4882a593Smuzhiyun  *        This lock must also be held whenever the GPU is being
418*4882a593Smuzhiyun  *        powered on or off.
419*4882a593Smuzhiyun  * @active_count: The reference count of active contexts on this device.
420*4882a593Smuzhiyun  *                Note that some code paths keep shaders/the tiler
421*4882a593Smuzhiyun  *                powered whilst this is 0.
422*4882a593Smuzhiyun  *                Use kbase_pm_is_active() instead to check for such cases.
423*4882a593Smuzhiyun  * @suspending: Flag indicating suspending/suspended
424*4882a593Smuzhiyun  * @runtime_active: Flag to track if the GPU is in runtime suspended or active
425*4882a593Smuzhiyun  *                  state. This ensures that runtime_put and runtime_get
426*4882a593Smuzhiyun  *                  functions are called in pairs. For example if runtime_get
427*4882a593Smuzhiyun  *                  has already been called from the power_on callback, then
428*4882a593Smuzhiyun  *                  the call to it from runtime_gpu_active callback can be
429*4882a593Smuzhiyun  *                  skipped.
430*4882a593Smuzhiyun  * @gpu_lost: Flag indicating gpu lost
431*4882a593Smuzhiyun  *            This structure contains data for the power management framework.
432*4882a593Smuzhiyun  *            There is one instance of this structure per device in the system.
433*4882a593Smuzhiyun  * @zero_active_count_wait: Wait queue set when active_count == 0
434*4882a593Smuzhiyun  * @resume_wait: system resume of GPU device.
435*4882a593Smuzhiyun  * @debug_core_mask: Bit masks identifying the available shader cores that are
436*4882a593Smuzhiyun  *                   specified via sysfs. One mask per job slot.
437*4882a593Smuzhiyun  * @debug_core_mask_all: Bit masks identifying the available shader cores that
438*4882a593Smuzhiyun  *                       are specified via sysfs.
439*4882a593Smuzhiyun  * @callback_power_runtime_init: Callback for initializing the runtime power
440*4882a593Smuzhiyun  *                               management. Return 0 on success, else error code
441*4882a593Smuzhiyun  * @callback_power_runtime_term: Callback for terminating the runtime power
442*4882a593Smuzhiyun  *                               management.
443*4882a593Smuzhiyun  * @dvfs_period: Time in milliseconds between each dvfs sample
444*4882a593Smuzhiyun  * @backend: KBase PM backend data
445*4882a593Smuzhiyun  * @arb_vm_state: The state of the arbiter VM machine
446*4882a593Smuzhiyun  * @gpu_users_waiting: Used by virtualization to notify the arbiter that there
447*4882a593Smuzhiyun  *                     are users waiting for the GPU so that it can request
448*4882a593Smuzhiyun  *                     and resume the driver.
449*4882a593Smuzhiyun  * @clk_rtm: The state of the GPU clock rate trace manager
450*4882a593Smuzhiyun  */
451*4882a593Smuzhiyun struct kbase_pm_device_data {
452*4882a593Smuzhiyun 	struct mutex lock;
453*4882a593Smuzhiyun 	int active_count;
454*4882a593Smuzhiyun 	bool suspending;
455*4882a593Smuzhiyun #if MALI_USE_CSF
456*4882a593Smuzhiyun 	bool runtime_active;
457*4882a593Smuzhiyun #endif
458*4882a593Smuzhiyun #ifdef CONFIG_MALI_ARBITER_SUPPORT
459*4882a593Smuzhiyun 	atomic_t gpu_lost;
460*4882a593Smuzhiyun #endif /* CONFIG_MALI_ARBITER_SUPPORT */
461*4882a593Smuzhiyun 	wait_queue_head_t zero_active_count_wait;
462*4882a593Smuzhiyun 	wait_queue_head_t resume_wait;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun #if MALI_USE_CSF
465*4882a593Smuzhiyun 	u64 debug_core_mask;
466*4882a593Smuzhiyun #else
467*4882a593Smuzhiyun 	/* One mask per job slot. */
468*4882a593Smuzhiyun 	u64 debug_core_mask[BASE_JM_MAX_NR_SLOTS];
469*4882a593Smuzhiyun 	u64 debug_core_mask_all;
470*4882a593Smuzhiyun #endif /* MALI_USE_CSF */
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	int (*callback_power_runtime_init)(struct kbase_device *kbdev);
473*4882a593Smuzhiyun 	void (*callback_power_runtime_term)(struct kbase_device *kbdev);
474*4882a593Smuzhiyun 	u32 dvfs_period;
475*4882a593Smuzhiyun 	struct kbase_pm_backend_data backend;
476*4882a593Smuzhiyun #ifdef CONFIG_MALI_ARBITER_SUPPORT
477*4882a593Smuzhiyun 	struct kbase_arbiter_vm_state *arb_vm_state;
478*4882a593Smuzhiyun 	atomic_t gpu_users_waiting;
479*4882a593Smuzhiyun #endif /* CONFIG_MALI_ARBITER_SUPPORT */
480*4882a593Smuzhiyun 	struct kbase_clk_rate_trace_manager clk_rtm;
481*4882a593Smuzhiyun };
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /**
484*4882a593Smuzhiyun  * struct kbase_mem_pool - Page based memory pool for kctx/kbdev
485*4882a593Smuzhiyun  * @kbdev:                     Kbase device where memory is used
486*4882a593Smuzhiyun  * @cur_size:                  Number of free pages currently in the pool (may exceed
487*4882a593Smuzhiyun  *                             @max_size in some corner cases)
488*4882a593Smuzhiyun  * @max_size:                  Maximum number of free pages in the pool
489*4882a593Smuzhiyun  * @order:                     order = 0 refers to a pool of 4 KB pages
490*4882a593Smuzhiyun  *                             order = 9 refers to a pool of 2 MB pages (2^9 * 4KB = 2 MB)
491*4882a593Smuzhiyun  * @group_id:                  A memory group ID to be passed to a platform-specific
492*4882a593Smuzhiyun  *                             memory group manager, if present. Immutable.
493*4882a593Smuzhiyun  *                             Valid range is 0..(MEMORY_GROUP_MANAGER_NR_GROUPS-1).
494*4882a593Smuzhiyun  * @pool_lock:                 Lock protecting the pool - must be held when modifying
495*4882a593Smuzhiyun  *                             @cur_size and @page_list
496*4882a593Smuzhiyun  * @page_list:                 List of free pages in the pool
497*4882a593Smuzhiyun  * @reclaim:                   Shrinker for kernel reclaim of free pages
498*4882a593Smuzhiyun  * @isolation_in_progress_cnt: Number of pages in pool undergoing page isolation.
499*4882a593Smuzhiyun  *                             This is used to avoid race condition between pool termination
500*4882a593Smuzhiyun  *                             and page isolation for page migration.
501*4882a593Smuzhiyun  * @next_pool:                 Pointer to next pool where pages can be allocated when this
502*4882a593Smuzhiyun  *                             pool is empty. Pages will spill over to the next pool when
503*4882a593Smuzhiyun  *                             this pool is full. Can be NULL if there is no next pool.
504*4882a593Smuzhiyun  * @dying:                     true if the pool is being terminated, and any ongoing
505*4882a593Smuzhiyun  *                             operations should be abandoned
506*4882a593Smuzhiyun  * @dont_reclaim:              true if the shrinker is forbidden from reclaiming memory from
507*4882a593Smuzhiyun  *                             this pool, eg during a grow operation
508*4882a593Smuzhiyun  */
509*4882a593Smuzhiyun struct kbase_mem_pool {
510*4882a593Smuzhiyun 	struct kbase_device *kbdev;
511*4882a593Smuzhiyun 	size_t cur_size;
512*4882a593Smuzhiyun 	size_t max_size;
513*4882a593Smuzhiyun 	u8 order;
514*4882a593Smuzhiyun 	u8 group_id;
515*4882a593Smuzhiyun 	spinlock_t pool_lock;
516*4882a593Smuzhiyun 	struct list_head page_list;
517*4882a593Smuzhiyun 	struct shrinker reclaim;
518*4882a593Smuzhiyun 	atomic_t isolation_in_progress_cnt;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	struct kbase_mem_pool *next_pool;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	bool dying;
523*4882a593Smuzhiyun 	bool dont_reclaim;
524*4882a593Smuzhiyun };
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /**
527*4882a593Smuzhiyun  * struct kbase_mem_pool_group - a complete set of physical memory pools.
528*4882a593Smuzhiyun  *
529*4882a593Smuzhiyun  * @small: Array of objects containing the state for pools of 4 KiB size
530*4882a593Smuzhiyun  *         physical pages.
531*4882a593Smuzhiyun  * @large: Array of objects containing the state for pools of 2 MiB size
532*4882a593Smuzhiyun  *         physical pages.
533*4882a593Smuzhiyun  *
534*4882a593Smuzhiyun  * Memory pools are used to allow efficient reallocation of previously-freed
535*4882a593Smuzhiyun  * physical pages. A pair of memory pools is initialized for each physical
536*4882a593Smuzhiyun  * memory group: one for 4 KiB pages and one for 2 MiB pages. These arrays
537*4882a593Smuzhiyun  * should be indexed by physical memory group ID, the meaning of which is
538*4882a593Smuzhiyun  * defined by the systems integrator.
539*4882a593Smuzhiyun  */
540*4882a593Smuzhiyun struct kbase_mem_pool_group {
541*4882a593Smuzhiyun 	struct kbase_mem_pool small[MEMORY_GROUP_MANAGER_NR_GROUPS];
542*4882a593Smuzhiyun 	struct kbase_mem_pool large[MEMORY_GROUP_MANAGER_NR_GROUPS];
543*4882a593Smuzhiyun };
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun /**
546*4882a593Smuzhiyun  * struct kbase_mem_pool_config - Initial configuration for a physical memory
547*4882a593Smuzhiyun  *                                pool
548*4882a593Smuzhiyun  *
549*4882a593Smuzhiyun  * @max_size: Maximum number of free pages that the pool can hold.
550*4882a593Smuzhiyun  */
551*4882a593Smuzhiyun struct kbase_mem_pool_config {
552*4882a593Smuzhiyun 	size_t max_size;
553*4882a593Smuzhiyun };
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /**
556*4882a593Smuzhiyun  * struct kbase_mem_pool_group_config - Initial configuration for a complete
557*4882a593Smuzhiyun  *                                      set of physical memory pools
558*4882a593Smuzhiyun  *
559*4882a593Smuzhiyun  * @small: Array of initial configuration for pools of 4 KiB pages.
560*4882a593Smuzhiyun  * @large: Array of initial configuration for pools of 2 MiB pages.
561*4882a593Smuzhiyun  *
562*4882a593Smuzhiyun  * This array should be indexed by physical memory group ID, the meaning
563*4882a593Smuzhiyun  * of which is defined by the systems integrator.
564*4882a593Smuzhiyun  */
565*4882a593Smuzhiyun struct kbase_mem_pool_group_config {
566*4882a593Smuzhiyun 	struct kbase_mem_pool_config small[MEMORY_GROUP_MANAGER_NR_GROUPS];
567*4882a593Smuzhiyun 	struct kbase_mem_pool_config large[MEMORY_GROUP_MANAGER_NR_GROUPS];
568*4882a593Smuzhiyun };
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun /**
571*4882a593Smuzhiyun  * struct kbase_devfreq_opp - Lookup table for converting between nominal OPP
572*4882a593Smuzhiyun  *                            frequency, real frequencies and core mask
573*4882a593Smuzhiyun  * @real_freqs: Real GPU frequencies.
574*4882a593Smuzhiyun  * @opp_volts: OPP voltages.
575*4882a593Smuzhiyun  * @opp_freq:  Nominal OPP frequency
576*4882a593Smuzhiyun  * @core_mask: Shader core mask
577*4882a593Smuzhiyun  */
578*4882a593Smuzhiyun struct kbase_devfreq_opp {
579*4882a593Smuzhiyun 	u64 opp_freq;
580*4882a593Smuzhiyun 	u64 core_mask;
581*4882a593Smuzhiyun 	u64 real_freqs[BASE_MAX_NR_CLOCKS_REGULATORS];
582*4882a593Smuzhiyun 	u32 opp_volts[BASE_MAX_NR_CLOCKS_REGULATORS];
583*4882a593Smuzhiyun };
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun /* MMU mode flags */
586*4882a593Smuzhiyun #define KBASE_MMU_MODE_HAS_NON_CACHEABLE (1ul << 0) /* Has NON_CACHEABLE MEMATTR */
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun /**
589*4882a593Smuzhiyun  * struct kbase_mmu_mode - object containing pointer to methods invoked for
590*4882a593Smuzhiyun  *                         programming the MMU, as per the MMU mode supported
591*4882a593Smuzhiyun  *                         by Hw.
592*4882a593Smuzhiyun  * @update:           enable & setup/configure one of the GPU address space.
593*4882a593Smuzhiyun  * @get_as_setup:     retrieve the configuration of one of the GPU address space.
594*4882a593Smuzhiyun  * @disable_as:       disable one of the GPU address space.
595*4882a593Smuzhiyun  * @pte_to_phy_addr:  retrieve the physical address encoded in the page table entry.
596*4882a593Smuzhiyun  * @ate_is_valid:     check if the pte is a valid address translation entry
597*4882a593Smuzhiyun  *                    encoding the physical address of the actual mapped page.
598*4882a593Smuzhiyun  * @pte_is_valid:     check if the pte is a valid entry encoding the physical
599*4882a593Smuzhiyun  *                    address of the next lower level page table.
600*4882a593Smuzhiyun  * @entry_set_ate:    program the pte to be a valid address translation entry to
601*4882a593Smuzhiyun  *                    encode the physical address of the actual page being mapped.
602*4882a593Smuzhiyun  * @entry_set_pte:    program the pte to be a valid entry to encode the physical
603*4882a593Smuzhiyun  *                    address of the next lower level page table and also update
604*4882a593Smuzhiyun  *                    the number of valid entries.
605*4882a593Smuzhiyun  * @entries_invalidate: clear out or invalidate a range of ptes.
606*4882a593Smuzhiyun  * @get_num_valid_entries: returns the number of valid entries for a specific pgd.
607*4882a593Smuzhiyun  * @set_num_valid_entries: sets the number of valid entries for a specific pgd
608*4882a593Smuzhiyun  * @flags:            bitmask of MMU mode flags. Refer to KBASE_MMU_MODE_ constants.
609*4882a593Smuzhiyun  */
610*4882a593Smuzhiyun struct kbase_mmu_mode {
611*4882a593Smuzhiyun 	void (*update)(struct kbase_device *kbdev,
612*4882a593Smuzhiyun 			struct kbase_mmu_table *mmut,
613*4882a593Smuzhiyun 			int as_nr);
614*4882a593Smuzhiyun 	void (*get_as_setup)(struct kbase_mmu_table *mmut,
615*4882a593Smuzhiyun 			struct kbase_mmu_setup * const setup);
616*4882a593Smuzhiyun 	void (*disable_as)(struct kbase_device *kbdev, int as_nr);
617*4882a593Smuzhiyun 	phys_addr_t (*pte_to_phy_addr)(u64 entry);
618*4882a593Smuzhiyun 	int (*ate_is_valid)(u64 ate, int level);
619*4882a593Smuzhiyun 	int (*pte_is_valid)(u64 pte, int level);
620*4882a593Smuzhiyun 	void (*entry_set_ate)(u64 *entry, struct tagged_addr phy,
621*4882a593Smuzhiyun 			unsigned long flags, int level);
622*4882a593Smuzhiyun 	void (*entry_set_pte)(u64 *entry, phys_addr_t phy);
623*4882a593Smuzhiyun 	void (*entries_invalidate)(u64 *entry, u32 count);
624*4882a593Smuzhiyun 	unsigned int (*get_num_valid_entries)(u64 *pgd);
625*4882a593Smuzhiyun 	void (*set_num_valid_entries)(u64 *pgd,
626*4882a593Smuzhiyun 				      unsigned int num_of_valid_entries);
627*4882a593Smuzhiyun 	unsigned long flags;
628*4882a593Smuzhiyun };
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun struct kbase_mmu_mode const *kbase_mmu_mode_get_aarch64(void);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun #define DEVNAME_SIZE	16
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun /**
635*4882a593Smuzhiyun  * enum kbase_devfreq_work_type - The type of work to perform in the devfreq
636*4882a593Smuzhiyun  *                                suspend/resume worker.
637*4882a593Smuzhiyun  * @DEVFREQ_WORK_NONE:    Initilisation state.
638*4882a593Smuzhiyun  * @DEVFREQ_WORK_SUSPEND: Call devfreq_suspend_device().
639*4882a593Smuzhiyun  * @DEVFREQ_WORK_RESUME:  Call devfreq_resume_device().
640*4882a593Smuzhiyun  */
641*4882a593Smuzhiyun enum kbase_devfreq_work_type {
642*4882a593Smuzhiyun 	DEVFREQ_WORK_NONE,
643*4882a593Smuzhiyun 	DEVFREQ_WORK_SUSPEND,
644*4882a593Smuzhiyun 	DEVFREQ_WORK_RESUME
645*4882a593Smuzhiyun };
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun /**
648*4882a593Smuzhiyun  * struct kbase_devfreq_queue_info - Object representing an instance for managing
649*4882a593Smuzhiyun  *                                   the queued devfreq suspend/resume works.
650*4882a593Smuzhiyun  * @workq:                 Workqueue for devfreq suspend/resume requests
651*4882a593Smuzhiyun  * @work:                  Work item for devfreq suspend & resume
652*4882a593Smuzhiyun  * @req_type:              Requested work type to be performed by the devfreq
653*4882a593Smuzhiyun  *                         suspend/resume worker
654*4882a593Smuzhiyun  * @acted_type:            Work type has been acted on by the worker, i.e. the
655*4882a593Smuzhiyun  *                         internal recorded state of the suspend/resume
656*4882a593Smuzhiyun  */
657*4882a593Smuzhiyun struct kbase_devfreq_queue_info {
658*4882a593Smuzhiyun 	struct workqueue_struct *workq;
659*4882a593Smuzhiyun 	struct work_struct work;
660*4882a593Smuzhiyun 	enum kbase_devfreq_work_type req_type;
661*4882a593Smuzhiyun 	enum kbase_devfreq_work_type acted_type;
662*4882a593Smuzhiyun };
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun /**
665*4882a593Smuzhiyun  * struct kbase_process - Representing an object of a kbase process instantiated
666*4882a593Smuzhiyun  *                        when the first kbase context is created under it.
667*4882a593Smuzhiyun  * @tgid:               Thread group ID.
668*4882a593Smuzhiyun  * @total_gpu_pages:    Total gpu pages allocated across all the contexts
669*4882a593Smuzhiyun  *                      of this process, it accounts for both native allocations
670*4882a593Smuzhiyun  *                      and dma_buf imported allocations.
671*4882a593Smuzhiyun  * @kctx_list:          List of kbase contexts created for the process.
672*4882a593Smuzhiyun  * @kprcs_node:         Node to a rb_tree, kbase_device will maintain a rb_tree
673*4882a593Smuzhiyun  *                      based on key tgid, kprcs_node is the node link to
674*4882a593Smuzhiyun  *                      &struct_kbase_device.process_root.
675*4882a593Smuzhiyun  * @dma_buf_root:       RB tree of the dma-buf imported allocations, imported
676*4882a593Smuzhiyun  *                      across all the contexts created for this process.
677*4882a593Smuzhiyun  *                      Used to ensure that pages of allocation are accounted
678*4882a593Smuzhiyun  *                      only once for the process, even if the allocation gets
679*4882a593Smuzhiyun  *                      imported multiple times for the process.
680*4882a593Smuzhiyun  */
681*4882a593Smuzhiyun struct kbase_process {
682*4882a593Smuzhiyun 	pid_t tgid;
683*4882a593Smuzhiyun 	size_t total_gpu_pages;
684*4882a593Smuzhiyun 	struct list_head kctx_list;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	struct rb_node kprcs_node;
687*4882a593Smuzhiyun 	struct rb_root dma_buf_root;
688*4882a593Smuzhiyun };
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun /**
691*4882a593Smuzhiyun  * struct kbase_mem_migrate - Object representing an instance for managing
692*4882a593Smuzhiyun  *                            page migration.
693*4882a593Smuzhiyun  *
694*4882a593Smuzhiyun  * @free_pages_list:  List of deferred pages to free. Mostly used when page migration
695*4882a593Smuzhiyun  *                    is enabled. Pages in memory pool that require migrating
696*4882a593Smuzhiyun  *                    will be freed instead. However page cannot be freed
697*4882a593Smuzhiyun  *                    right away as Linux will need to release the page lock.
698*4882a593Smuzhiyun  *                    Therefore page will be added to this list and freed later.
699*4882a593Smuzhiyun  * @free_pages_lock:  This lock should be held when adding or removing pages
700*4882a593Smuzhiyun  *                    from @free_pages_list.
701*4882a593Smuzhiyun  * @free_pages_workq: Work queue to process the work items queued to free
702*4882a593Smuzhiyun  *                    pages in @free_pages_list.
703*4882a593Smuzhiyun  * @free_pages_work:  Work item to free pages in @free_pages_list.
704*4882a593Smuzhiyun  * @inode:            Pointer to inode whose address space operations are used
705*4882a593Smuzhiyun  *                    for page migration purposes.
706*4882a593Smuzhiyun  */
707*4882a593Smuzhiyun struct kbase_mem_migrate {
708*4882a593Smuzhiyun 	struct list_head free_pages_list;
709*4882a593Smuzhiyun 	spinlock_t free_pages_lock;
710*4882a593Smuzhiyun 	struct workqueue_struct *free_pages_workq;
711*4882a593Smuzhiyun 	struct work_struct free_pages_work;
712*4882a593Smuzhiyun #if (KERNEL_VERSION(6, 0, 0) > LINUX_VERSION_CODE)
713*4882a593Smuzhiyun 	struct inode *inode;
714*4882a593Smuzhiyun #endif
715*4882a593Smuzhiyun };
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun /**
718*4882a593Smuzhiyun  * struct kbase_device   - Object representing an instance of GPU platform device,
719*4882a593Smuzhiyun  *                         allocated from the probe method of mali driver.
720*4882a593Smuzhiyun  * @hw_quirks_sc:          Configuration to be used for the shader cores as per
721*4882a593Smuzhiyun  *                         the HW issues present in the GPU.
722*4882a593Smuzhiyun  * @hw_quirks_tiler:       Configuration to be used for the Tiler as per the HW
723*4882a593Smuzhiyun  *                         issues present in the GPU.
724*4882a593Smuzhiyun  * @hw_quirks_mmu:         Configuration to be used for the MMU as per the HW
725*4882a593Smuzhiyun  *                         issues present in the GPU.
726*4882a593Smuzhiyun  * @hw_quirks_gpu:         Configuration to be used for the Job Manager or CSF/MCU
727*4882a593Smuzhiyun  *                         subsystems as per the HW issues present in the GPU.
728*4882a593Smuzhiyun  * @entry:                 Links the device instance to the global list of GPU
729*4882a593Smuzhiyun  *                         devices. The list would have as many entries as there
730*4882a593Smuzhiyun  *                         are GPU device instances.
731*4882a593Smuzhiyun  * @dev:                   Pointer to the kernel's generic/base representation
732*4882a593Smuzhiyun  *                         of the GPU platform device.
733*4882a593Smuzhiyun  * @mdev:                  Pointer to the miscellaneous device registered to
734*4882a593Smuzhiyun  *                         provide Userspace access to kernel driver through the
735*4882a593Smuzhiyun  *                         device file /dev/malixx.
736*4882a593Smuzhiyun  * @reg_start:             Base address of the region in physical address space
737*4882a593Smuzhiyun  *                         where GPU registers have been mapped.
738*4882a593Smuzhiyun  * @reg_size:              Size of the region containing GPU registers
739*4882a593Smuzhiyun  * @reg:                   Kernel virtual address of the region containing GPU
740*4882a593Smuzhiyun  *                         registers, using which Driver will access the registers.
741*4882a593Smuzhiyun  * @irqs:                  Array containing IRQ resource info for 3 types of
742*4882a593Smuzhiyun  *                         interrupts : Job scheduling, MMU & GPU events (like
743*4882a593Smuzhiyun  *                         power management, cache etc.)
744*4882a593Smuzhiyun  * @irqs.irq:              irq number
745*4882a593Smuzhiyun  * @irqs.flags:            irq flags
746*4882a593Smuzhiyun  * @clocks:                Pointer to the input clock resources referenced by
747*4882a593Smuzhiyun  *                         the GPU device node.
748*4882a593Smuzhiyun  * @scmi_clk:              Pointer to the input scmi clock resources
749*4882a593Smuzhiyun  * @nr_clocks:             Number of clocks set in the clocks array.
750*4882a593Smuzhiyun  * @regulators:            Pointer to the structs corresponding to the
751*4882a593Smuzhiyun  *                         regulators referenced by the GPU device node.
752*4882a593Smuzhiyun  * @nr_regulators:         Number of regulators set in the regulators array.
753*4882a593Smuzhiyun  * @opp_table:             Pointer to the device OPP structure maintaining the
754*4882a593Smuzhiyun  *                         link to OPPs attached to a device. This is obtained
755*4882a593Smuzhiyun  *                         after setting regulator names for the device.
756*4882a593Smuzhiyun  * @token:                 Integer replacement for opp_table in kernel versions
757*4882a593Smuzhiyun  *                         6 and greater. Value is a token id number when 0 or greater,
758*4882a593Smuzhiyun  *                         and a linux errno when negative. Must be initialised
759*4882a593Smuzhiyun  *                         to an non-zero value as 0 is valid token id.
760*4882a593Smuzhiyun  * @devname:               string containing the name used for GPU device instance,
761*4882a593Smuzhiyun  *                         miscellaneous device is registered using the same name.
762*4882a593Smuzhiyun  * @id:                    Unique identifier for the device, indicates the number of
763*4882a593Smuzhiyun  *                         devices which have been created so far.
764*4882a593Smuzhiyun  * @model:                 Pointer, valid only when Driver is compiled to not access
765*4882a593Smuzhiyun  *                         the real GPU Hw, to the dummy model which tries to mimic
766*4882a593Smuzhiyun  *                         to some extent the state & behavior of GPU Hw in response
767*4882a593Smuzhiyun  *                         to the register accesses made by the Driver.
768*4882a593Smuzhiyun  * @irq_slab:              slab cache for allocating the work items queued when
769*4882a593Smuzhiyun  *                         model mimics raising of IRQ to cause an interrupt on CPU.
770*4882a593Smuzhiyun  * @irq_workq:             workqueue for processing the irq work items.
771*4882a593Smuzhiyun  * @serving_job_irq:       function to execute work items queued when model mimics
772*4882a593Smuzhiyun  *                         the raising of JS irq, mimics the interrupt handler
773*4882a593Smuzhiyun  *                         processing JS interrupts.
774*4882a593Smuzhiyun  * @serving_gpu_irq:       function to execute work items queued when model mimics
775*4882a593Smuzhiyun  *                         the raising of GPU irq, mimics the interrupt handler
776*4882a593Smuzhiyun  *                         processing GPU interrupts.
777*4882a593Smuzhiyun  * @serving_mmu_irq:       function to execute work items queued when model mimics
778*4882a593Smuzhiyun  *                         the raising of MMU irq, mimics the interrupt handler
779*4882a593Smuzhiyun  *                         processing MMU interrupts.
780*4882a593Smuzhiyun  * @reg_op_lock:           lock used by model to serialize the handling of register
781*4882a593Smuzhiyun  *                         accesses made by the driver.
782*4882a593Smuzhiyun  * @pm:                    Per device object for storing data for power management
783*4882a593Smuzhiyun  *                         framework.
784*4882a593Smuzhiyun  * @fw_load_lock:          Mutex to protect firmware loading in @ref kbase_open.
785*4882a593Smuzhiyun  * @csf:                   CSF object for the GPU device.
786*4882a593Smuzhiyun  * @js_data:               Per device object encapsulating the current context of
787*4882a593Smuzhiyun  *                         Job Scheduler, which is global to the device and is not
788*4882a593Smuzhiyun  *                         tied to any particular struct kbase_context running on
789*4882a593Smuzhiyun  *                         the device
790*4882a593Smuzhiyun  * @mem_pools:             Global pools of free physical memory pages which can
791*4882a593Smuzhiyun  *                         be used by all the contexts.
792*4882a593Smuzhiyun  * @memdev:                keeps track of the in use physical pages allocated by
793*4882a593Smuzhiyun  *                         the Driver.
794*4882a593Smuzhiyun  * @mmu_mode:              Pointer to the object containing methods for programming
795*4882a593Smuzhiyun  *                         the MMU, depending on the type of MMU supported by Hw.
796*4882a593Smuzhiyun  * @mgm_dev:               Pointer to the memory group manager device attached
797*4882a593Smuzhiyun  *                         to the GPU device. This points to an internal memory
798*4882a593Smuzhiyun  *                         group manager if no platform-specific memory group
799*4882a593Smuzhiyun  *                         manager was retrieved through device tree.
800*4882a593Smuzhiyun  * @as:                    Array of objects representing address spaces of GPU.
801*4882a593Smuzhiyun  * @as_free:               Bitpattern of free/available GPU address spaces.
802*4882a593Smuzhiyun  * @as_to_kctx:            Array of pointers to struct kbase_context, having
803*4882a593Smuzhiyun  *                         GPU adrress spaces assigned to them.
804*4882a593Smuzhiyun  * @mmu_mask_change:       Lock to serialize the access to MMU interrupt mask
805*4882a593Smuzhiyun  *                         register used in the handling of Bus & Page faults.
806*4882a593Smuzhiyun  * @pagesize_2mb:          Boolean to determine whether 2MiB page sizes are
807*4882a593Smuzhiyun  *                         supported and used where possible.
808*4882a593Smuzhiyun  * @gpu_props:             Object containing complete information about the
809*4882a593Smuzhiyun  *                         configuration/properties of GPU HW device in use.
810*4882a593Smuzhiyun  * @hw_issues_mask:        List of SW workarounds for HW issues
811*4882a593Smuzhiyun  * @hw_features_mask:      List of available HW features.
812*4882a593Smuzhiyun  * @disjoint_event:        struct for keeping track of the disjoint information,
813*4882a593Smuzhiyun  *                         that whether the GPU is in a disjoint state and the
814*4882a593Smuzhiyun  *                         number of disjoint events that have occurred on GPU.
815*4882a593Smuzhiyun  * @disjoint_event.count:  disjoint event count
816*4882a593Smuzhiyun  * @disjoint_event.state:  disjoint event state
817*4882a593Smuzhiyun  * @nr_hw_address_spaces:  Number of address spaces actually available in the
818*4882a593Smuzhiyun  *                         GPU, remains constant after driver initialisation.
819*4882a593Smuzhiyun  * @nr_user_address_spaces: Number of address spaces available to user contexts
820*4882a593Smuzhiyun  * @hwcnt_backend_csf_if_fw: Firmware interface to access CSF GPU performance
821*4882a593Smuzhiyun  *                         counters.
822*4882a593Smuzhiyun  * @hwcnt:                  Structure used for instrumentation and HW counters
823*4882a593Smuzhiyun  *                         dumping
824*4882a593Smuzhiyun  * @hwcnt.lock:            The lock should be used when accessing any of the
825*4882a593Smuzhiyun  *                         following members
826*4882a593Smuzhiyun  * @hwcnt.kctx:            kbase context
827*4882a593Smuzhiyun  * @hwcnt.addr:            HW counter address
828*4882a593Smuzhiyun  * @hwcnt.addr_bytes:      HW counter size in bytes
829*4882a593Smuzhiyun  * @hwcnt.backend:         Kbase instrumentation backend
830*4882a593Smuzhiyun  * @hwcnt_gpu_jm_backend:  Job manager GPU backend interface, used as superclass reference
831*4882a593Smuzhiyun  *                         pointer by hwcnt_gpu_iface, which wraps this implementation in
832*4882a593Smuzhiyun  *                         order to extend it with periodic dumping functionality.
833*4882a593Smuzhiyun  * @hwcnt_gpu_iface:       Backend interface for GPU hardware counter access.
834*4882a593Smuzhiyun  * @hwcnt_watchdog_timer:  Watchdog interface, used by the GPU backend hwcnt_gpu_iface to
835*4882a593Smuzhiyun  *                         perform periodic dumps in order to prevent hardware counter value
836*4882a593Smuzhiyun  *                         overflow or saturation.
837*4882a593Smuzhiyun  * @hwcnt_gpu_ctx:         Context for GPU hardware counter access.
838*4882a593Smuzhiyun  *                         @hwaccess_lock must be held when calling
839*4882a593Smuzhiyun  *                         kbase_hwcnt_context_enable() with @hwcnt_gpu_ctx.
840*4882a593Smuzhiyun  * @hwcnt_gpu_virt:        Virtualizer for GPU hardware counters.
841*4882a593Smuzhiyun  * @vinstr_ctx:            vinstr context created per device.
842*4882a593Smuzhiyun  * @kinstr_prfcnt_ctx:     kinstr_prfcnt context created per device.
843*4882a593Smuzhiyun  * @timeline_flags:        Bitmask defining which sets of timeline tracepoints
844*4882a593Smuzhiyun  *                         are enabled. If zero, there is no timeline client and
845*4882a593Smuzhiyun  *                         therefore timeline is disabled.
846*4882a593Smuzhiyun  * @timeline:              Timeline context created per device.
847*4882a593Smuzhiyun  * @ktrace:                kbase device's ktrace
848*4882a593Smuzhiyun  * @reset_timeout_ms:      Number of milliseconds to wait for the soft stop to
849*4882a593Smuzhiyun  *                         complete for the GPU jobs before proceeding with the
850*4882a593Smuzhiyun  *                         GPU reset.
851*4882a593Smuzhiyun  * @lowest_gpu_freq_khz:   Lowest frequency in KHz that the GPU can run at. Used
852*4882a593Smuzhiyun  *                         to calculate suitable timeouts for wait operations.
853*4882a593Smuzhiyun  * @backend_time:          Kbase backend time related attributes.
854*4882a593Smuzhiyun  * @cache_clean_in_progress: Set when a cache clean has been started, and
855*4882a593Smuzhiyun  *                         cleared when it has finished. This prevents multiple
856*4882a593Smuzhiyun  *                         cache cleans being done simultaneously.
857*4882a593Smuzhiyun  * @cache_clean_queued:    Pended cache clean operations invoked while another is
858*4882a593Smuzhiyun  *                         in progress. If this is not 0, another cache clean needs
859*4882a593Smuzhiyun  *                         to be triggered immediately after completion of the
860*4882a593Smuzhiyun  *                         current one.
861*4882a593Smuzhiyun  * @cache_clean_wait:      Signalled when a cache clean has finished.
862*4882a593Smuzhiyun  * @platform_context:      Platform specific private data to be accessed by
863*4882a593Smuzhiyun  *                         platform specific config files only.
864*4882a593Smuzhiyun  * @kctx_list:             List of kbase_contexts created for the device,
865*4882a593Smuzhiyun  *                         including any contexts that might be created for
866*4882a593Smuzhiyun  *                         hardware counters.
867*4882a593Smuzhiyun  * @kctx_list_lock:        Lock protecting concurrent accesses to @kctx_list.
868*4882a593Smuzhiyun  * @devfreq_profile:       Describes devfreq profile for the Mali GPU device, passed
869*4882a593Smuzhiyun  *                         to devfreq_add_device() to add devfreq feature to Mali
870*4882a593Smuzhiyun  *                         GPU device.
871*4882a593Smuzhiyun  * @devfreq:               Pointer to devfreq structure for Mali GPU device,
872*4882a593Smuzhiyun  *                         returned on the call to devfreq_add_device().
873*4882a593Smuzhiyun  * @current_freqs:         The real frequencies, corresponding to
874*4882a593Smuzhiyun  *                         @current_nominal_freq, at which the Mali GPU device
875*4882a593Smuzhiyun  *                         is currently operating, as retrieved from
876*4882a593Smuzhiyun  *                         @devfreq_table in the target callback of
877*4882a593Smuzhiyun  *                         @devfreq_profile.
878*4882a593Smuzhiyun  * @current_nominal_freq:  The nominal frequency currently used for the Mali GPU
879*4882a593Smuzhiyun  *                         device as retrieved through devfreq_recommended_opp()
880*4882a593Smuzhiyun  *                         using the freq value passed as an argument to target
881*4882a593Smuzhiyun  *                         callback of @devfreq_profile
882*4882a593Smuzhiyun  * @current_voltages:      The voltages corresponding to @current_nominal_freq,
883*4882a593Smuzhiyun  *                         as retrieved from @devfreq_table in the target
884*4882a593Smuzhiyun  *                         callback of @devfreq_profile.
885*4882a593Smuzhiyun  * @current_core_mask:     bitmask of shader cores that are currently desired &
886*4882a593Smuzhiyun  *                         enabled, corresponding to @current_nominal_freq as
887*4882a593Smuzhiyun  *                         retrieved from @devfreq_table in the target callback
888*4882a593Smuzhiyun  *                         of @devfreq_profile.
889*4882a593Smuzhiyun  * @devfreq_table:         Pointer to the lookup table for converting between
890*4882a593Smuzhiyun  *                         nominal OPP (operating performance point) frequency,
891*4882a593Smuzhiyun  *                         and real frequency and core mask. This table is
892*4882a593Smuzhiyun  *                         constructed according to operating-points-v2-mali
893*4882a593Smuzhiyun  *                         table in devicetree.
894*4882a593Smuzhiyun  * @num_opps:              Number of operating performance points available for the Mali
895*4882a593Smuzhiyun  *                         GPU device.
896*4882a593Smuzhiyun  * @last_devfreq_metrics:  last PM metrics
897*4882a593Smuzhiyun  * @devfreq_queue:         Per device object for storing data that manages devfreq
898*4882a593Smuzhiyun  *                         suspend & resume request queue and the related items.
899*4882a593Smuzhiyun  * @devfreq_cooling:       Pointer returned on registering devfreq cooling device
900*4882a593Smuzhiyun  *                         corresponding to @devfreq.
901*4882a593Smuzhiyun  * @ipa_protection_mode_switched: is set to TRUE when GPU is put into protected
902*4882a593Smuzhiyun  *                         mode. It is a sticky flag which is cleared by IPA
903*4882a593Smuzhiyun  *                         once it has made use of information that GPU had
904*4882a593Smuzhiyun  *                         previously entered protected mode.
905*4882a593Smuzhiyun  * @ipa:                   Top level structure for IPA, containing pointers to both
906*4882a593Smuzhiyun  *                         configured & fallback models.
907*4882a593Smuzhiyun  * @ipa.lock:              Access to this struct must be with ipa.lock held
908*4882a593Smuzhiyun  * @ipa.configured_model:  ipa model to use
909*4882a593Smuzhiyun  * @ipa.fallback_model:    ipa fallback model
910*4882a593Smuzhiyun  * @ipa.last_metrics:      Values of the PM utilization metrics from last time
911*4882a593Smuzhiyun  *                         the power model was invoked. The utilization is
912*4882a593Smuzhiyun  *                         calculated as the difference between last_metrics
913*4882a593Smuzhiyun  *                         and the current values.
914*4882a593Smuzhiyun  * @ipa.force_fallback_model: true if use of fallback model has been forced by
915*4882a593Smuzhiyun  *                            the User
916*4882a593Smuzhiyun  * @ipa.last_sample_time:  Records the time when counters, used for dynamic
917*4882a593Smuzhiyun  *                         energy estimation, were last sampled.
918*4882a593Smuzhiyun  * @previous_frequency:    Previous frequency of GPU clock used for
919*4882a593Smuzhiyun  *                         BASE_HW_ISSUE_GPU2017_1336 workaround, This clock is
920*4882a593Smuzhiyun  *                         restored when L2 is powered on.
921*4882a593Smuzhiyun  * @job_fault_debug:       Flag to control the dumping of debug data for job faults,
922*4882a593Smuzhiyun  *                         set when the 'job_fault' debugfs file is opened.
923*4882a593Smuzhiyun  * @mali_debugfs_directory: Root directory for the debugfs files created by the driver
924*4882a593Smuzhiyun  * @debugfs_ctx_directory: Directory inside the @mali_debugfs_directory containing
925*4882a593Smuzhiyun  *                         a sub-directory for every context.
926*4882a593Smuzhiyun  * @debugfs_instr_directory: Instrumentation debugfs directory
927*4882a593Smuzhiyun  * @debugfs_as_read_bitmap: bitmap of address spaces for which the bus or page fault
928*4882a593Smuzhiyun  *                         has occurred.
929*4882a593Smuzhiyun  * @job_fault_wq:          Waitqueue to block the job fault dumping daemon till the
930*4882a593Smuzhiyun  *                         occurrence of a job fault.
931*4882a593Smuzhiyun  * @job_fault_resume_wq:   Waitqueue on which every context with a faulty job wait
932*4882a593Smuzhiyun  *                         for the job fault dumping to complete before they can
933*4882a593Smuzhiyun  *                         do bottom half of job done for the atoms which followed
934*4882a593Smuzhiyun  *                         the faulty atom.
935*4882a593Smuzhiyun  * @job_fault_resume_workq: workqueue to process the work items queued for the faulty
936*4882a593Smuzhiyun  *                         atoms, whereby the work item function waits for the dumping
937*4882a593Smuzhiyun  *                         to get completed.
938*4882a593Smuzhiyun  * @job_fault_event_list:  List of atoms, each belonging to a different context, which
939*4882a593Smuzhiyun  *                         generated a job fault.
940*4882a593Smuzhiyun  * @job_fault_event_lock:  Lock to protect concurrent accesses to @job_fault_event_list
941*4882a593Smuzhiyun  * @regs_dump_debugfs_data: Contains the offset of register to be read through debugfs
942*4882a593Smuzhiyun  *                         file "read_register".
943*4882a593Smuzhiyun  * @regs_dump_debugfs_data.reg_offset: Contains the offset of register to be
944*4882a593Smuzhiyun  *                         read through debugfs file "read_register".
945*4882a593Smuzhiyun  * @ctx_num:               Total number of contexts created for the device.
946*4882a593Smuzhiyun  * @io_history:            Pointer to an object keeping a track of all recent
947*4882a593Smuzhiyun  *                         register accesses. The history of register accesses
948*4882a593Smuzhiyun  *                         can be read through "regs_history" debugfs file.
949*4882a593Smuzhiyun  * @hwaccess:              Contains a pointer to active kbase context and GPU
950*4882a593Smuzhiyun  *                         backend specific data for HW access layer.
951*4882a593Smuzhiyun  * @faults_pending:        Count of page/bus faults waiting for bottom half processing
952*4882a593Smuzhiyun  *                         via workqueues.
953*4882a593Smuzhiyun  * @mmu_hw_operation_in_progress: Set before sending the MMU command and is
954*4882a593Smuzhiyun  *                         cleared after the command is complete. Whilst this
955*4882a593Smuzhiyun  *                         flag is set, the write to L2_PWROFF register will be
956*4882a593Smuzhiyun  *                         skipped which is needed to workaround the HW issue
957*4882a593Smuzhiyun  *                         GPU2019-3878. PM state machine is invoked after
958*4882a593Smuzhiyun  *                         clearing this flag and @hwaccess_lock is used to
959*4882a593Smuzhiyun  *                         serialize the access.
960*4882a593Smuzhiyun  * @mmu_page_migrate_in_progress: Set before starting a MMU page migration transaction
961*4882a593Smuzhiyun  *                         and cleared after the transaction completes. PM L2 state is
962*4882a593Smuzhiyun  *                         prevented from entering powering up/down transitions when the
963*4882a593Smuzhiyun  *                         flag is set, @hwaccess_lock is used to serialize the access.
964*4882a593Smuzhiyun  * @poweroff_pending:      Set when power off operation for GPU is started, reset when
965*4882a593Smuzhiyun  *                         power on for GPU is started.
966*4882a593Smuzhiyun  * @infinite_cache_active_default: Set to enable using infinite cache for all the
967*4882a593Smuzhiyun  *                         allocations of a new context.
968*4882a593Smuzhiyun  * @mem_pool_defaults:     Default configuration for the group of memory pools
969*4882a593Smuzhiyun  *                         created for a new context.
970*4882a593Smuzhiyun  * @current_gpu_coherency_mode: coherency mode in use, which can be different
971*4882a593Smuzhiyun  *                         from @system_coherency, when using protected mode.
972*4882a593Smuzhiyun  * @system_coherency:      coherency mode as retrieved from the device tree.
973*4882a593Smuzhiyun  * @cci_snoop_enabled:     Flag to track when CCI snoops have been enabled.
974*4882a593Smuzhiyun  * @snoop_enable_smc:      SMC function ID to call into Trusted firmware to
975*4882a593Smuzhiyun  *                         enable cache snooping. Value of 0 indicates that it
976*4882a593Smuzhiyun  *                         is not used.
977*4882a593Smuzhiyun  * @snoop_disable_smc:     SMC function ID to call disable cache snooping.
978*4882a593Smuzhiyun  * @protected_ops:         Pointer to the methods for switching in or out of the
979*4882a593Smuzhiyun  *                         protected mode, as per the @protected_dev being used.
980*4882a593Smuzhiyun  * @protected_dev:         Pointer to the protected mode switcher device attached
981*4882a593Smuzhiyun  *                         to the GPU device retrieved through device tree if
982*4882a593Smuzhiyun  *                         GPU do not support protected mode switching natively.
983*4882a593Smuzhiyun  * @protected_mode:        set to TRUE when GPU is put into protected mode
984*4882a593Smuzhiyun  * @protected_mode_transition: set to TRUE when GPU is transitioning into or
985*4882a593Smuzhiyun  *                         out of protected mode.
986*4882a593Smuzhiyun  * @protected_mode_hwcnt_desired: True if we want GPU hardware counters to be
987*4882a593Smuzhiyun  *                         enabled. Counters must be disabled before transition
988*4882a593Smuzhiyun  *                         into protected mode.
989*4882a593Smuzhiyun  * @protected_mode_hwcnt_disabled: True if GPU hardware counters are not
990*4882a593Smuzhiyun  *                         enabled.
991*4882a593Smuzhiyun  * @protected_mode_hwcnt_disable_work: Work item to disable GPU hardware
992*4882a593Smuzhiyun  *                         counters, used if atomic disable is not possible.
993*4882a593Smuzhiyun  * @irq_reset_flush:        Flag to indicate that GPU reset is in-flight and flush of
994*4882a593Smuzhiyun  *                          IRQ + bottom half is being done, to prevent the writes
995*4882a593Smuzhiyun  *                          to MMU_IRQ_CLEAR & MMU_IRQ_MASK registers.
996*4882a593Smuzhiyun  * @inited_subsys:          Bitmap of inited sub systems at the time of device probe.
997*4882a593Smuzhiyun  *                          Used during device remove or for handling error in probe.
998*4882a593Smuzhiyun  * @hwaccess_lock:          Lock, which can be taken from IRQ context, to serialize
999*4882a593Smuzhiyun  *                          the updates made to Job dispatcher + scheduler states.
1000*4882a593Smuzhiyun  * @mmu_hw_mutex:           Protects access to MMU operations and address space
1001*4882a593Smuzhiyun  *                          related state.
1002*4882a593Smuzhiyun  * @serialize_jobs:         Currently used mode for serialization of jobs, both
1003*4882a593Smuzhiyun  *                          intra & inter slots serialization is supported.
1004*4882a593Smuzhiyun  * @backup_serialize_jobs:  Copy of the original value of @serialize_jobs taken
1005*4882a593Smuzhiyun  *                          when GWT is enabled. Used to restore the original value
1006*4882a593Smuzhiyun  *                          on disabling of GWT.
1007*4882a593Smuzhiyun  * @js_ctx_scheduling_mode: Context scheduling mode currently being used by
1008*4882a593Smuzhiyun  *                          Job Scheduler
1009*4882a593Smuzhiyun  * @l2_size_override:       Used to set L2 cache size via device tree blob
1010*4882a593Smuzhiyun  * @l2_hash_override:       Used to set L2 cache hash via device tree blob
1011*4882a593Smuzhiyun  * @l2_hash_values_override: true if @l2_hash_values is valid.
1012*4882a593Smuzhiyun  * @l2_hash_values:         Used to set L2 asn_hash via device tree blob
1013*4882a593Smuzhiyun  * @sysc_alloc:             Array containing values to be programmed into
1014*4882a593Smuzhiyun  *                          SYSC_ALLOC[0..7] GPU registers on L2 cache
1015*4882a593Smuzhiyun  *                          power down. These come from either DTB or
1016*4882a593Smuzhiyun  *                          via DebugFS (if it is available in kernel).
1017*4882a593Smuzhiyun  * @process_root:           rb_tree root node for maintaining a rb_tree of
1018*4882a593Smuzhiyun  *                          kbase_process based on key tgid(thread group ID).
1019*4882a593Smuzhiyun  * @dma_buf_root:           rb_tree root node for maintaining a rb_tree of
1020*4882a593Smuzhiyun  *                          &struct kbase_dma_buf based on key dma_buf.
1021*4882a593Smuzhiyun  *                          We maintain a rb_tree of dma_buf mappings under
1022*4882a593Smuzhiyun  *                          kbase_device and kbase_process, one indicates a
1023*4882a593Smuzhiyun  *                          mapping and gpu memory usage at device level and
1024*4882a593Smuzhiyun  *                          other one at process level.
1025*4882a593Smuzhiyun  * @total_gpu_pages:        Total GPU pages used for the complete GPU device.
1026*4882a593Smuzhiyun  * @dma_buf_lock:           This mutex should be held while accounting for
1027*4882a593Smuzhiyun  *                          @total_gpu_pages from imported dma buffers.
1028*4882a593Smuzhiyun  * @gpu_mem_usage_lock:     This spinlock should be held while accounting
1029*4882a593Smuzhiyun  *                          @total_gpu_pages for both native and dma-buf imported
1030*4882a593Smuzhiyun  *                          allocations.
1031*4882a593Smuzhiyun  * @dummy_job_wa:           struct for dummy job execution workaround for the
1032*4882a593Smuzhiyun  *                          GPU hang issue
1033*4882a593Smuzhiyun  * @dummy_job_wa.ctx:       dummy job workaround context
1034*4882a593Smuzhiyun  * @dummy_job_wa.jc:        dummy job workaround job
1035*4882a593Smuzhiyun  * @dummy_job_wa.slot:      dummy job workaround slot
1036*4882a593Smuzhiyun  * @dummy_job_wa.flags:     dummy job workaround flags
1037*4882a593Smuzhiyun  * @dummy_job_wa_loaded:    Flag for indicating that the workaround blob has
1038*4882a593Smuzhiyun  *                          been loaded. Protected by @fw_load_lock.
1039*4882a593Smuzhiyun  * @arb:                    Pointer to the arbiter device
1040*4882a593Smuzhiyun  * @pcm_dev:                The priority control manager device.
1041*4882a593Smuzhiyun  * @oom_notifier_block:     notifier_block containing kernel-registered out-of-
1042*4882a593Smuzhiyun  *                          memory handler.
1043*4882a593Smuzhiyun  * @mem_migrate:            Per device object for managing page migration.
1044*4882a593Smuzhiyun  * @live_fence_metadata:    Count of live fence metadata structures created by
1045*4882a593Smuzhiyun  *                          KCPU queue. These structures may outlive kbase module
1046*4882a593Smuzhiyun  *                          itself. Therefore, in such a case, a warning should be
1047*4882a593Smuzhiyun  *                          be produced.
1048*4882a593Smuzhiyun  * @mmu_as_inactive_wait_time_ms: Maximum waiting time in ms for the completion of
1049*4882a593Smuzhiyun  *                          a MMU operation
1050*4882a593Smuzhiyun  * @va_region_slab:         kmem_cache (slab) for allocated kbase_va_region structures.
1051*4882a593Smuzhiyun  */
1052*4882a593Smuzhiyun struct kbase_device {
1053*4882a593Smuzhiyun 	u32 hw_quirks_sc;
1054*4882a593Smuzhiyun 	u32 hw_quirks_tiler;
1055*4882a593Smuzhiyun 	u32 hw_quirks_mmu;
1056*4882a593Smuzhiyun 	u32 hw_quirks_gpu;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	struct list_head entry;
1059*4882a593Smuzhiyun 	struct device *dev;
1060*4882a593Smuzhiyun 	struct miscdevice mdev;
1061*4882a593Smuzhiyun 	u64 reg_start;
1062*4882a593Smuzhiyun 	size_t reg_size;
1063*4882a593Smuzhiyun 	void __iomem *reg;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	struct {
1066*4882a593Smuzhiyun 		int irq;
1067*4882a593Smuzhiyun 		int flags;
1068*4882a593Smuzhiyun 	} irqs[3];
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	struct clk *clocks[BASE_MAX_NR_CLOCKS_REGULATORS];
1071*4882a593Smuzhiyun 	unsigned int nr_clocks;
1072*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_REGULATOR)
1073*4882a593Smuzhiyun 	struct regulator *regulators[BASE_MAX_NR_CLOCKS_REGULATORS];
1074*4882a593Smuzhiyun 	unsigned int nr_regulators;
1075*4882a593Smuzhiyun #if (KERNEL_VERSION(6, 0, 0) <= LINUX_VERSION_CODE)
1076*4882a593Smuzhiyun 	int token;
1077*4882a593Smuzhiyun #elif (KERNEL_VERSION(4, 10, 0) <= LINUX_VERSION_CODE)
1078*4882a593Smuzhiyun 	struct opp_table *opp_table;
1079*4882a593Smuzhiyun #endif /* (KERNEL_VERSION(6, 0, 0) <= LINUX_VERSION_CODE) */
1080*4882a593Smuzhiyun #endif /* CONFIG_REGULATOR */
1081*4882a593Smuzhiyun 	char devname[DEVNAME_SIZE];
1082*4882a593Smuzhiyun 	u32  id;
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun #if !IS_ENABLED(CONFIG_MALI_REAL_HW)
1085*4882a593Smuzhiyun 	void *model;
1086*4882a593Smuzhiyun 	struct kmem_cache *irq_slab;
1087*4882a593Smuzhiyun 	struct workqueue_struct *irq_workq;
1088*4882a593Smuzhiyun 	atomic_t serving_job_irq;
1089*4882a593Smuzhiyun 	atomic_t serving_gpu_irq;
1090*4882a593Smuzhiyun 	atomic_t serving_mmu_irq;
1091*4882a593Smuzhiyun 	spinlock_t reg_op_lock;
1092*4882a593Smuzhiyun #endif /* !IS_ENABLED(CONFIG_MALI_REAL_HW) */
1093*4882a593Smuzhiyun 	struct kbase_pm_device_data pm;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	struct kbase_mem_pool_group mem_pools;
1096*4882a593Smuzhiyun 	struct kbasep_mem_device memdev;
1097*4882a593Smuzhiyun 	struct kbase_mmu_mode const *mmu_mode;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	struct memory_group_manager_device *mgm_dev;
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	struct kbase_as as[BASE_MAX_NR_AS];
1102*4882a593Smuzhiyun 	u16 as_free;
1103*4882a593Smuzhiyun 	struct kbase_context *as_to_kctx[BASE_MAX_NR_AS];
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	spinlock_t mmu_mask_change;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	bool pagesize_2mb;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	struct kbase_gpu_props gpu_props;
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	unsigned long hw_issues_mask[(BASE_HW_ISSUE_END + BITS_PER_LONG - 1) / BITS_PER_LONG];
1112*4882a593Smuzhiyun 	unsigned long hw_features_mask[(BASE_HW_FEATURE_END + BITS_PER_LONG - 1) / BITS_PER_LONG];
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 	struct {
1115*4882a593Smuzhiyun 		atomic_t count;
1116*4882a593Smuzhiyun 		atomic_t state;
1117*4882a593Smuzhiyun 	} disjoint_event;
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	s8 nr_hw_address_spaces;
1120*4882a593Smuzhiyun 	s8 nr_user_address_spaces;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	/**
1123*4882a593Smuzhiyun 	 * @pbha_propagate_bits:   Record of Page-Based Hardware Attribute Propagate bits to
1124*4882a593Smuzhiyun 	 *                         restore to L2_CONFIG upon GPU reset.
1125*4882a593Smuzhiyun 	 */
1126*4882a593Smuzhiyun 	u8 pbha_propagate_bits;
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun #if MALI_USE_CSF
1129*4882a593Smuzhiyun 	struct kbase_hwcnt_backend_csf_if hwcnt_backend_csf_if_fw;
1130*4882a593Smuzhiyun #else
1131*4882a593Smuzhiyun 	struct kbase_hwcnt {
1132*4882a593Smuzhiyun 		spinlock_t lock;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 		struct kbase_context *kctx;
1135*4882a593Smuzhiyun 		u64 addr;
1136*4882a593Smuzhiyun 		u64 addr_bytes;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 		struct kbase_instr_backend backend;
1139*4882a593Smuzhiyun 	} hwcnt;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	struct kbase_hwcnt_backend_interface hwcnt_gpu_jm_backend;
1142*4882a593Smuzhiyun #endif
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	struct kbase_hwcnt_backend_interface hwcnt_gpu_iface;
1145*4882a593Smuzhiyun 	struct kbase_hwcnt_watchdog_interface hwcnt_watchdog_timer;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 	struct kbase_hwcnt_context *hwcnt_gpu_ctx;
1148*4882a593Smuzhiyun 	struct kbase_hwcnt_virtualizer *hwcnt_gpu_virt;
1149*4882a593Smuzhiyun 	struct kbase_vinstr_context *vinstr_ctx;
1150*4882a593Smuzhiyun 	struct kbase_kinstr_prfcnt_context *kinstr_prfcnt_ctx;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	atomic_t               timeline_flags;
1153*4882a593Smuzhiyun 	struct kbase_timeline *timeline;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun #if KBASE_KTRACE_TARGET_RBUF
1156*4882a593Smuzhiyun 	struct kbase_ktrace ktrace;
1157*4882a593Smuzhiyun #endif
1158*4882a593Smuzhiyun 	u32 reset_timeout_ms;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	u64 lowest_gpu_freq_khz;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun #if MALI_USE_CSF
1163*4882a593Smuzhiyun 	struct kbase_backend_time backend_time;
1164*4882a593Smuzhiyun #endif
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	bool cache_clean_in_progress;
1167*4882a593Smuzhiyun 	u32 cache_clean_queued;
1168*4882a593Smuzhiyun 	wait_queue_head_t cache_clean_wait;
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	void *platform_context;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	struct list_head        kctx_list;
1173*4882a593Smuzhiyun 	struct mutex            kctx_list_lock;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	struct rockchip_opp_info opp_info;
1176*4882a593Smuzhiyun 	bool is_runtime_resumed;
1177*4882a593Smuzhiyun 	unsigned long current_nominal_freq;
1178*4882a593Smuzhiyun 	struct monitor_dev_info *mdev_info;
1179*4882a593Smuzhiyun #ifdef CONFIG_MALI_BIFROST_DEVFREQ
1180*4882a593Smuzhiyun 	struct devfreq_dev_profile devfreq_profile;
1181*4882a593Smuzhiyun 	struct devfreq *devfreq;
1182*4882a593Smuzhiyun 	unsigned long current_freqs[BASE_MAX_NR_CLOCKS_REGULATORS];
1183*4882a593Smuzhiyun 	unsigned long current_voltages[BASE_MAX_NR_CLOCKS_REGULATORS];
1184*4882a593Smuzhiyun 	u64 current_core_mask;
1185*4882a593Smuzhiyun 	struct kbase_devfreq_opp *devfreq_table;
1186*4882a593Smuzhiyun 	int num_opps;
1187*4882a593Smuzhiyun 	struct kbasep_pm_metrics last_devfreq_metrics;
1188*4882a593Smuzhiyun 	struct ipa_power_model_data *model_data;
1189*4882a593Smuzhiyun 	struct kbase_devfreq_queue_info devfreq_queue;
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DEVFREQ_THERMAL)
1192*4882a593Smuzhiyun 	struct devfreq_cooling_power dfc_power;
1193*4882a593Smuzhiyun 	struct thermal_cooling_device *devfreq_cooling;
1194*4882a593Smuzhiyun 	bool ipa_protection_mode_switched;
1195*4882a593Smuzhiyun 	struct {
1196*4882a593Smuzhiyun 		/* Access to this struct must be with ipa.lock held */
1197*4882a593Smuzhiyun 		struct mutex lock;
1198*4882a593Smuzhiyun 		struct kbase_ipa_model *configured_model;
1199*4882a593Smuzhiyun 		struct kbase_ipa_model *fallback_model;
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 		/* Values of the PM utilization metrics from last time the
1202*4882a593Smuzhiyun 		 * power model was invoked. The utilization is calculated as
1203*4882a593Smuzhiyun 		 * the difference between last_metrics and the current values.
1204*4882a593Smuzhiyun 		 */
1205*4882a593Smuzhiyun 		struct kbasep_pm_metrics last_metrics;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 		/* true if use of fallback model has been forced by the User */
1208*4882a593Smuzhiyun 		bool force_fallback_model;
1209*4882a593Smuzhiyun 		/* Records the time when counters, used for dynamic energy
1210*4882a593Smuzhiyun 		 * estimation, were last sampled.
1211*4882a593Smuzhiyun 		 */
1212*4882a593Smuzhiyun 		ktime_t last_sample_time;
1213*4882a593Smuzhiyun 	} ipa;
1214*4882a593Smuzhiyun #endif /* CONFIG_DEVFREQ_THERMAL */
1215*4882a593Smuzhiyun #endif /* CONFIG_MALI_BIFROST_DEVFREQ */
1216*4882a593Smuzhiyun 	unsigned long previous_frequency;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun #if !MALI_USE_CSF
1219*4882a593Smuzhiyun 	atomic_t job_fault_debug;
1220*4882a593Smuzhiyun #endif /* !MALI_USE_CSF */
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DEBUG_FS)
1223*4882a593Smuzhiyun 	struct dentry *mali_debugfs_directory;
1224*4882a593Smuzhiyun 	struct dentry *debugfs_ctx_directory;
1225*4882a593Smuzhiyun 	struct dentry *debugfs_instr_directory;
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun #ifdef CONFIG_MALI_BIFROST_DEBUG
1228*4882a593Smuzhiyun 	u64 debugfs_as_read_bitmap;
1229*4882a593Smuzhiyun #endif /* CONFIG_MALI_BIFROST_DEBUG */
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun #if !MALI_USE_CSF
1232*4882a593Smuzhiyun 	wait_queue_head_t job_fault_wq;
1233*4882a593Smuzhiyun 	wait_queue_head_t job_fault_resume_wq;
1234*4882a593Smuzhiyun 	struct workqueue_struct *job_fault_resume_workq;
1235*4882a593Smuzhiyun 	struct list_head job_fault_event_list;
1236*4882a593Smuzhiyun 	spinlock_t job_fault_event_lock;
1237*4882a593Smuzhiyun #endif /* !MALI_USE_CSF */
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun #if !MALI_CUSTOMER_RELEASE
1240*4882a593Smuzhiyun 	struct {
1241*4882a593Smuzhiyun 		u32 reg_offset;
1242*4882a593Smuzhiyun 	} regs_dump_debugfs_data;
1243*4882a593Smuzhiyun #endif /* !MALI_CUSTOMER_RELEASE */
1244*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	atomic_t ctx_num;
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DEBUG_FS)
1249*4882a593Smuzhiyun 	struct kbase_io_history io_history;
1250*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	struct kbase_hwaccess_data hwaccess;
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	atomic_t faults_pending;
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun #if MALI_USE_CSF
1257*4882a593Smuzhiyun 	bool mmu_hw_operation_in_progress;
1258*4882a593Smuzhiyun #endif
1259*4882a593Smuzhiyun 	bool mmu_page_migrate_in_progress;
1260*4882a593Smuzhiyun 	bool poweroff_pending;
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	bool infinite_cache_active_default;
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	struct kbase_mem_pool_group_config mem_pool_defaults;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	u32 current_gpu_coherency_mode;
1267*4882a593Smuzhiyun 	u32 system_coherency;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	bool cci_snoop_enabled;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	u32 snoop_enable_smc;
1272*4882a593Smuzhiyun 	u32 snoop_disable_smc;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	const struct protected_mode_ops *protected_ops;
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	struct protected_mode_device *protected_dev;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	bool protected_mode;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	bool protected_mode_transition;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	bool protected_mode_hwcnt_desired;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	bool protected_mode_hwcnt_disabled;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	struct work_struct protected_mode_hwcnt_disable_work;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	bool irq_reset_flush;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	u32 inited_subsys;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	spinlock_t hwaccess_lock;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	struct mutex mmu_hw_mutex;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	u8 l2_size_override;
1298*4882a593Smuzhiyun 	u8 l2_hash_override;
1299*4882a593Smuzhiyun 	bool l2_hash_values_override;
1300*4882a593Smuzhiyun 	u32 l2_hash_values[ASN_HASH_COUNT];
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	u32 sysc_alloc[SYSC_ALLOC_COUNT];
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	struct mutex fw_load_lock;
1305*4882a593Smuzhiyun #if MALI_USE_CSF
1306*4882a593Smuzhiyun 	/* CSF object for the GPU device. */
1307*4882a593Smuzhiyun 	struct kbase_csf_device csf;
1308*4882a593Smuzhiyun #else
1309*4882a593Smuzhiyun 	struct kbasep_js_device_data js_data;
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	/* See KBASE_JS_*_PRIORITY_MODE for details. */
1312*4882a593Smuzhiyun 	u32 js_ctx_scheduling_mode;
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	/* See KBASE_SERIALIZE_* for details */
1315*4882a593Smuzhiyun 	u8 serialize_jobs;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun #ifdef CONFIG_MALI_CINSTR_GWT
1318*4882a593Smuzhiyun 	u8 backup_serialize_jobs;
1319*4882a593Smuzhiyun #endif /* CONFIG_MALI_CINSTR_GWT */
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun #endif /* MALI_USE_CSF */
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	struct rb_root process_root;
1324*4882a593Smuzhiyun 	struct rb_root dma_buf_root;
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	size_t total_gpu_pages;
1327*4882a593Smuzhiyun 	struct mutex dma_buf_lock;
1328*4882a593Smuzhiyun 	spinlock_t gpu_mem_usage_lock;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 	struct {
1331*4882a593Smuzhiyun 		struct kbase_context *ctx;
1332*4882a593Smuzhiyun 		u64 jc;
1333*4882a593Smuzhiyun 		int slot;
1334*4882a593Smuzhiyun 		u64 flags;
1335*4882a593Smuzhiyun 	} dummy_job_wa;
1336*4882a593Smuzhiyun 	bool dummy_job_wa_loaded;
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun #ifdef CONFIG_MALI_ARBITER_SUPPORT
1339*4882a593Smuzhiyun 		struct kbase_arbiter_device arb;
1340*4882a593Smuzhiyun #endif
1341*4882a593Smuzhiyun 	/* Priority Control Manager device */
1342*4882a593Smuzhiyun 	struct priority_control_manager_device *pcm_dev;
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 	struct notifier_block oom_notifier_block;
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun #if !MALI_USE_CSF
1347*4882a593Smuzhiyun 	spinlock_t quick_reset_lock;
1348*4882a593Smuzhiyun 	bool quick_reset_enabled;
1349*4882a593Smuzhiyun 	/*
1350*4882a593Smuzhiyun 	 * 进入 quck_reset_mode 后 (quick_reset_enabled 为 true),
1351*4882a593Smuzhiyun 	 * 对已经进入 KBASE_JD_ATOM_STATE_HW_COMPLETED 状态的 atom 的计数.
1352*4882a593Smuzhiyun 	 *
1353*4882a593Smuzhiyun 	 * 若 num_of_atoms_hw_completed 达到一定值, 将退出 quck_reset_mode.
1354*4882a593Smuzhiyun 	 * 见 kbase_js_complete_atom() 对 num_of_atoms_hw_completed 的引用.
1355*4882a593Smuzhiyun 	 */
1356*4882a593Smuzhiyun 	u32 num_of_atoms_hw_completed;
1357*4882a593Smuzhiyun #endif
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	struct kbase_mem_migrate mem_migrate;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun #if MALI_USE_CSF && IS_ENABLED(CONFIG_SYNC_FILE)
1362*4882a593Smuzhiyun 	atomic_t live_fence_metadata;
1363*4882a593Smuzhiyun #endif
1364*4882a593Smuzhiyun 	u32 mmu_as_inactive_wait_time_ms;
1365*4882a593Smuzhiyun 	struct kmem_cache *va_region_slab;
1366*4882a593Smuzhiyun };
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun /**
1369*4882a593Smuzhiyun  * enum kbase_file_state - Initialization state of a file opened by @kbase_open
1370*4882a593Smuzhiyun  *
1371*4882a593Smuzhiyun  * @KBASE_FILE_NEED_VSN:        Initial state, awaiting API version.
1372*4882a593Smuzhiyun  * @KBASE_FILE_VSN_IN_PROGRESS: Indicates if setting an API version is in
1373*4882a593Smuzhiyun  *                              progress and other setup calls shall be
1374*4882a593Smuzhiyun  *                              rejected.
1375*4882a593Smuzhiyun  * @KBASE_FILE_NEED_CTX:        Indicates if the API version handshake has
1376*4882a593Smuzhiyun  *                              completed, awaiting context creation flags.
1377*4882a593Smuzhiyun  * @KBASE_FILE_CTX_IN_PROGRESS: Indicates if the context's setup is in progress
1378*4882a593Smuzhiyun  *                              and other setup calls shall be rejected.
1379*4882a593Smuzhiyun  * @KBASE_FILE_COMPLETE:        Indicates if the setup for context has
1380*4882a593Smuzhiyun  *                              completed, i.e. flags have been set for the
1381*4882a593Smuzhiyun  *                              context.
1382*4882a593Smuzhiyun  *
1383*4882a593Smuzhiyun  * The driver allows only limited interaction with user-space until setup
1384*4882a593Smuzhiyun  * is complete.
1385*4882a593Smuzhiyun  */
1386*4882a593Smuzhiyun enum kbase_file_state {
1387*4882a593Smuzhiyun 	KBASE_FILE_NEED_VSN,
1388*4882a593Smuzhiyun 	KBASE_FILE_VSN_IN_PROGRESS,
1389*4882a593Smuzhiyun 	KBASE_FILE_NEED_CTX,
1390*4882a593Smuzhiyun 	KBASE_FILE_CTX_IN_PROGRESS,
1391*4882a593Smuzhiyun 	KBASE_FILE_COMPLETE
1392*4882a593Smuzhiyun };
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun /**
1395*4882a593Smuzhiyun  * struct kbase_file - Object representing a file opened by @kbase_open
1396*4882a593Smuzhiyun  *
1397*4882a593Smuzhiyun  * @kbdev:               Object representing an instance of GPU platform device,
1398*4882a593Smuzhiyun  *                       allocated from the probe method of the Mali driver.
1399*4882a593Smuzhiyun  * @filp:                Pointer to the struct file corresponding to device file
1400*4882a593Smuzhiyun  *                       /dev/malixx instance, passed to the file's open method.
1401*4882a593Smuzhiyun  * @kctx:                Object representing an entity, among which GPU is
1402*4882a593Smuzhiyun  *                       scheduled and which gets its own GPU address space.
1403*4882a593Smuzhiyun  *                       Invalid until @setup_state is KBASE_FILE_COMPLETE.
1404*4882a593Smuzhiyun  * @api_version:         Contains the version number for User/kernel interface,
1405*4882a593Smuzhiyun  *                       used for compatibility check. Invalid until
1406*4882a593Smuzhiyun  *                       @setup_state is KBASE_FILE_NEED_CTX.
1407*4882a593Smuzhiyun  * @setup_state:         Initialization state of the file. Values come from
1408*4882a593Smuzhiyun  *                       the kbase_file_state enumeration.
1409*4882a593Smuzhiyun  */
1410*4882a593Smuzhiyun struct kbase_file {
1411*4882a593Smuzhiyun 	struct kbase_device  *kbdev;
1412*4882a593Smuzhiyun 	struct file          *filp;
1413*4882a593Smuzhiyun 	struct kbase_context *kctx;
1414*4882a593Smuzhiyun 	unsigned long         api_version;
1415*4882a593Smuzhiyun 	atomic_t              setup_state;
1416*4882a593Smuzhiyun };
1417*4882a593Smuzhiyun #if MALI_JIT_PRESSURE_LIMIT_BASE
1418*4882a593Smuzhiyun /**
1419*4882a593Smuzhiyun  * enum kbase_context_flags - Flags for kbase contexts
1420*4882a593Smuzhiyun  *
1421*4882a593Smuzhiyun  * @KCTX_COMPAT: Set when the context process is a compat process, 32-bit
1422*4882a593Smuzhiyun  * process on a 64-bit kernel.
1423*4882a593Smuzhiyun  *
1424*4882a593Smuzhiyun  * @KCTX_RUNNABLE_REF: Set when context is counted in
1425*4882a593Smuzhiyun  * kbdev->js_data.nr_contexts_runnable. Must hold queue_mutex when accessing.
1426*4882a593Smuzhiyun  *
1427*4882a593Smuzhiyun  * @KCTX_ACTIVE: Set when the context is active.
1428*4882a593Smuzhiyun  *
1429*4882a593Smuzhiyun  * @KCTX_PULLED: Set when last kick() caused atoms to be pulled from this
1430*4882a593Smuzhiyun  * context.
1431*4882a593Smuzhiyun  *
1432*4882a593Smuzhiyun  * @KCTX_MEM_PROFILE_INITIALIZED: Set when the context's memory profile has been
1433*4882a593Smuzhiyun  * initialized.
1434*4882a593Smuzhiyun  *
1435*4882a593Smuzhiyun  * @KCTX_INFINITE_CACHE: Set when infinite cache is to be enabled for new
1436*4882a593Smuzhiyun  * allocations. Existing allocations will not change.
1437*4882a593Smuzhiyun  *
1438*4882a593Smuzhiyun  * @KCTX_SUBMIT_DISABLED: Set to prevent context from submitting any jobs.
1439*4882a593Smuzhiyun  *
1440*4882a593Smuzhiyun  * @KCTX_PRIVILEGED:Set if the context uses an address space and should be kept
1441*4882a593Smuzhiyun  * scheduled in.
1442*4882a593Smuzhiyun  *
1443*4882a593Smuzhiyun  * @KCTX_SCHEDULED: Set when the context is scheduled on the Run Pool.
1444*4882a593Smuzhiyun  * This is only ever updated whilst the jsctx_mutex is held.
1445*4882a593Smuzhiyun  *
1446*4882a593Smuzhiyun  * @KCTX_DYING: Set when the context process is in the process of being evicted.
1447*4882a593Smuzhiyun  *
1448*4882a593Smuzhiyun  * @KCTX_FORCE_SAME_VA: Set when BASE_MEM_SAME_VA should be forced on memory
1449*4882a593Smuzhiyun  * allocations. For 64-bit clients it is enabled by default, and disabled by
1450*4882a593Smuzhiyun  * default on 32-bit clients. Being able to clear this flag is only used for
1451*4882a593Smuzhiyun  * testing purposes of the custom zone allocation on 64-bit user-space builds,
1452*4882a593Smuzhiyun  * where we also require more control than is available through e.g. the JIT
1453*4882a593Smuzhiyun  * allocation mechanism. However, the 64-bit user-space client must still
1454*4882a593Smuzhiyun  * reserve a JIT region using KBASE_IOCTL_MEM_JIT_INIT
1455*4882a593Smuzhiyun  *
1456*4882a593Smuzhiyun  * @KCTX_PULLED_SINCE_ACTIVE_JS0: Set when the context has had an atom pulled
1457*4882a593Smuzhiyun  * from it for job slot 0. This is reset when the context first goes active or
1458*4882a593Smuzhiyun  * is re-activated on that slot.
1459*4882a593Smuzhiyun  *
1460*4882a593Smuzhiyun  * @KCTX_PULLED_SINCE_ACTIVE_JS1: Set when the context has had an atom pulled
1461*4882a593Smuzhiyun  * from it for job slot 1. This is reset when the context first goes active or
1462*4882a593Smuzhiyun  * is re-activated on that slot.
1463*4882a593Smuzhiyun  *
1464*4882a593Smuzhiyun  * @KCTX_PULLED_SINCE_ACTIVE_JS2: Set when the context has had an atom pulled
1465*4882a593Smuzhiyun  * from it for job slot 2. This is reset when the context first goes active or
1466*4882a593Smuzhiyun  * is re-activated on that slot.
1467*4882a593Smuzhiyun  *
1468*4882a593Smuzhiyun  * @KCTX_AS_DISABLED_ON_FAULT: Set when the GPU address space is disabled for
1469*4882a593Smuzhiyun  * the context due to unhandled page(or bus) fault. It is cleared when the
1470*4882a593Smuzhiyun  * refcount for the context drops to 0 or on when the address spaces are
1471*4882a593Smuzhiyun  * re-enabled on GPU reset or power cycle.
1472*4882a593Smuzhiyun  *
1473*4882a593Smuzhiyun  * @KCTX_JPL_ENABLED: Set when JIT physical page limit is less than JIT virtual
1474*4882a593Smuzhiyun  * address page limit, so we must take care to not exceed the physical limit
1475*4882a593Smuzhiyun  *
1476*4882a593Smuzhiyun  * All members need to be separate bits. This enum is intended for use in a
1477*4882a593Smuzhiyun  * bitmask where multiple values get OR-ed together.
1478*4882a593Smuzhiyun  */
1479*4882a593Smuzhiyun enum kbase_context_flags {
1480*4882a593Smuzhiyun 	KCTX_COMPAT = 1U << 0,
1481*4882a593Smuzhiyun 	KCTX_RUNNABLE_REF = 1U << 1,
1482*4882a593Smuzhiyun 	KCTX_ACTIVE = 1U << 2,
1483*4882a593Smuzhiyun 	KCTX_PULLED = 1U << 3,
1484*4882a593Smuzhiyun 	KCTX_MEM_PROFILE_INITIALIZED = 1U << 4,
1485*4882a593Smuzhiyun 	KCTX_INFINITE_CACHE = 1U << 5,
1486*4882a593Smuzhiyun 	KCTX_SUBMIT_DISABLED = 1U << 6,
1487*4882a593Smuzhiyun 	KCTX_PRIVILEGED = 1U << 7,
1488*4882a593Smuzhiyun 	KCTX_SCHEDULED = 1U << 8,
1489*4882a593Smuzhiyun 	KCTX_DYING = 1U << 9,
1490*4882a593Smuzhiyun 	KCTX_FORCE_SAME_VA = 1U << 11,
1491*4882a593Smuzhiyun 	KCTX_PULLED_SINCE_ACTIVE_JS0 = 1U << 12,
1492*4882a593Smuzhiyun 	KCTX_PULLED_SINCE_ACTIVE_JS1 = 1U << 13,
1493*4882a593Smuzhiyun 	KCTX_PULLED_SINCE_ACTIVE_JS2 = 1U << 14,
1494*4882a593Smuzhiyun 	KCTX_AS_DISABLED_ON_FAULT = 1U << 15,
1495*4882a593Smuzhiyun 	KCTX_JPL_ENABLED = 1U << 16,
1496*4882a593Smuzhiyun };
1497*4882a593Smuzhiyun #else
1498*4882a593Smuzhiyun /**
1499*4882a593Smuzhiyun  * enum kbase_context_flags - Flags for kbase contexts
1500*4882a593Smuzhiyun  *
1501*4882a593Smuzhiyun  * @KCTX_COMPAT: Set when the context process is a compat process, 32-bit
1502*4882a593Smuzhiyun  * process on a 64-bit kernel.
1503*4882a593Smuzhiyun  *
1504*4882a593Smuzhiyun  * @KCTX_RUNNABLE_REF: Set when context is counted in
1505*4882a593Smuzhiyun  * kbdev->js_data.nr_contexts_runnable. Must hold queue_mutex when accessing.
1506*4882a593Smuzhiyun  *
1507*4882a593Smuzhiyun  * @KCTX_ACTIVE: Set when the context is active.
1508*4882a593Smuzhiyun  *
1509*4882a593Smuzhiyun  * @KCTX_PULLED: Set when last kick() caused atoms to be pulled from this
1510*4882a593Smuzhiyun  * context.
1511*4882a593Smuzhiyun  *
1512*4882a593Smuzhiyun  * @KCTX_MEM_PROFILE_INITIALIZED: Set when the context's memory profile has been
1513*4882a593Smuzhiyun  * initialized.
1514*4882a593Smuzhiyun  *
1515*4882a593Smuzhiyun  * @KCTX_INFINITE_CACHE: Set when infinite cache is to be enabled for new
1516*4882a593Smuzhiyun  * allocations. Existing allocations will not change.
1517*4882a593Smuzhiyun  *
1518*4882a593Smuzhiyun  * @KCTX_SUBMIT_DISABLED: Set to prevent context from submitting any jobs.
1519*4882a593Smuzhiyun  *
1520*4882a593Smuzhiyun  * @KCTX_PRIVILEGED:Set if the context uses an address space and should be kept
1521*4882a593Smuzhiyun  * scheduled in.
1522*4882a593Smuzhiyun  *
1523*4882a593Smuzhiyun  * @KCTX_SCHEDULED: Set when the context is scheduled on the Run Pool.
1524*4882a593Smuzhiyun  * This is only ever updated whilst the jsctx_mutex is held.
1525*4882a593Smuzhiyun  *
1526*4882a593Smuzhiyun  * @KCTX_DYING: Set when the context process is in the process of being evicted.
1527*4882a593Smuzhiyun  *
1528*4882a593Smuzhiyun  *
1529*4882a593Smuzhiyun  * @KCTX_FORCE_SAME_VA: Set when BASE_MEM_SAME_VA should be forced on memory
1530*4882a593Smuzhiyun  * allocations. For 64-bit clients it is enabled by default, and disabled by
1531*4882a593Smuzhiyun  * default on 32-bit clients. Being able to clear this flag is only used for
1532*4882a593Smuzhiyun  * testing purposes of the custom zone allocation on 64-bit user-space builds,
1533*4882a593Smuzhiyun  * where we also require more control than is available through e.g. the JIT
1534*4882a593Smuzhiyun  * allocation mechanism. However, the 64-bit user-space client must still
1535*4882a593Smuzhiyun  * reserve a JIT region using KBASE_IOCTL_MEM_JIT_INIT
1536*4882a593Smuzhiyun  *
1537*4882a593Smuzhiyun  * @KCTX_PULLED_SINCE_ACTIVE_JS0: Set when the context has had an atom pulled
1538*4882a593Smuzhiyun  * from it for job slot 0. This is reset when the context first goes active or
1539*4882a593Smuzhiyun  * is re-activated on that slot.
1540*4882a593Smuzhiyun  *
1541*4882a593Smuzhiyun  * @KCTX_PULLED_SINCE_ACTIVE_JS1: Set when the context has had an atom pulled
1542*4882a593Smuzhiyun  * from it for job slot 1. This is reset when the context first goes active or
1543*4882a593Smuzhiyun  * is re-activated on that slot.
1544*4882a593Smuzhiyun  *
1545*4882a593Smuzhiyun  * @KCTX_PULLED_SINCE_ACTIVE_JS2: Set when the context has had an atom pulled
1546*4882a593Smuzhiyun  * from it for job slot 2. This is reset when the context first goes active or
1547*4882a593Smuzhiyun  * is re-activated on that slot.
1548*4882a593Smuzhiyun  *
1549*4882a593Smuzhiyun  * @KCTX_AS_DISABLED_ON_FAULT: Set when the GPU address space is disabled for
1550*4882a593Smuzhiyun  * the context due to unhandled page(or bus) fault. It is cleared when the
1551*4882a593Smuzhiyun  * refcount for the context drops to 0 or on when the address spaces are
1552*4882a593Smuzhiyun  * re-enabled on GPU reset or power cycle.
1553*4882a593Smuzhiyun  *
1554*4882a593Smuzhiyun  * All members need to be separate bits. This enum is intended for use in a
1555*4882a593Smuzhiyun  * bitmask where multiple values get OR-ed together.
1556*4882a593Smuzhiyun  */
1557*4882a593Smuzhiyun enum kbase_context_flags {
1558*4882a593Smuzhiyun 	KCTX_COMPAT = 1U << 0,
1559*4882a593Smuzhiyun 	KCTX_RUNNABLE_REF = 1U << 1,
1560*4882a593Smuzhiyun 	KCTX_ACTIVE = 1U << 2,
1561*4882a593Smuzhiyun 	KCTX_PULLED = 1U << 3,
1562*4882a593Smuzhiyun 	KCTX_MEM_PROFILE_INITIALIZED = 1U << 4,
1563*4882a593Smuzhiyun 	KCTX_INFINITE_CACHE = 1U << 5,
1564*4882a593Smuzhiyun 	KCTX_SUBMIT_DISABLED = 1U << 6,
1565*4882a593Smuzhiyun 	KCTX_PRIVILEGED = 1U << 7,
1566*4882a593Smuzhiyun 	KCTX_SCHEDULED = 1U << 8,
1567*4882a593Smuzhiyun 	KCTX_DYING = 1U << 9,
1568*4882a593Smuzhiyun 	KCTX_FORCE_SAME_VA = 1U << 11,
1569*4882a593Smuzhiyun 	KCTX_PULLED_SINCE_ACTIVE_JS0 = 1U << 12,
1570*4882a593Smuzhiyun 	KCTX_PULLED_SINCE_ACTIVE_JS1 = 1U << 13,
1571*4882a593Smuzhiyun 	KCTX_PULLED_SINCE_ACTIVE_JS2 = 1U << 14,
1572*4882a593Smuzhiyun 	KCTX_AS_DISABLED_ON_FAULT = 1U << 15,
1573*4882a593Smuzhiyun };
1574*4882a593Smuzhiyun #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun struct kbase_sub_alloc {
1577*4882a593Smuzhiyun 	struct list_head link;
1578*4882a593Smuzhiyun 	struct page *page;
1579*4882a593Smuzhiyun 	DECLARE_BITMAP(sub_pages, SZ_2M / SZ_4K);
1580*4882a593Smuzhiyun };
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun /**
1583*4882a593Smuzhiyun  * struct kbase_context - Kernel base context
1584*4882a593Smuzhiyun  *
1585*4882a593Smuzhiyun  * @filp:                 Pointer to the struct file corresponding to device file
1586*4882a593Smuzhiyun  *                        /dev/malixx instance, passed to the file's open method.
1587*4882a593Smuzhiyun  * @kbdev:                Pointer to the Kbase device for which the context is created.
1588*4882a593Smuzhiyun  * @kctx_list_link:       Node into Kbase device list of contexts.
1589*4882a593Smuzhiyun  * @mmu:                  Structure holding details of the MMU tables for this
1590*4882a593Smuzhiyun  *                        context
1591*4882a593Smuzhiyun  * @id:                   Unique identifier for the context, indicates the number of
1592*4882a593Smuzhiyun  *                        contexts which have been created for the device so far.
1593*4882a593Smuzhiyun  * @api_version:          contains the version number for User/kernel interface,
1594*4882a593Smuzhiyun  *                        used for compatibility check.
1595*4882a593Smuzhiyun  * @event_list:           list of posted events about completed atoms, to be sent to
1596*4882a593Smuzhiyun  *                        event handling thread of Userpsace.
1597*4882a593Smuzhiyun  * @event_coalesce_list:  list containing events corresponding to successive atoms
1598*4882a593Smuzhiyun  *                        which have requested deferred delivery of the completion
1599*4882a593Smuzhiyun  *                        events to Userspace.
1600*4882a593Smuzhiyun  * @event_mutex:          Lock to protect the concurrent access to @event_list &
1601*4882a593Smuzhiyun  *                        @event_mutex.
1602*4882a593Smuzhiyun  * @event_closed:         Flag set through POST_TERM ioctl, indicates that Driver
1603*4882a593Smuzhiyun  *                        should stop posting events and also inform event handling
1604*4882a593Smuzhiyun  *                        thread that context termination is in progress.
1605*4882a593Smuzhiyun  * @event_workq:          Workqueue for processing work items corresponding to atoms
1606*4882a593Smuzhiyun  *                        that do not return an event to userspace.
1607*4882a593Smuzhiyun  * @event_count:          Count of the posted events to be consumed by Userspace.
1608*4882a593Smuzhiyun  * @event_coalesce_count: Count of the events present in @event_coalesce_list.
1609*4882a593Smuzhiyun  * @flags:                bitmap of enums from kbase_context_flags, indicating the
1610*4882a593Smuzhiyun  *                        state & attributes for the context.
1611*4882a593Smuzhiyun  * @aliasing_sink_page:   Special page used for KBASE_MEM_TYPE_ALIAS allocations,
1612*4882a593Smuzhiyun  *                        which can alias number of memory regions. The page is
1613*4882a593Smuzhiyun  *                        represent a region where it is mapped with a write-alloc
1614*4882a593Smuzhiyun  *                        cache setup, typically used when the write result of the
1615*4882a593Smuzhiyun  *                        GPU isn't needed, but the GPU must write anyway.
1616*4882a593Smuzhiyun  * @mem_partials_lock:    Lock for protecting the operations done on the elements
1617*4882a593Smuzhiyun  *                        added to @mem_partials list.
1618*4882a593Smuzhiyun  * @mem_partials:         List head for the list of large pages, 2MB in size, which
1619*4882a593Smuzhiyun  *                        have been split into 4 KB pages and are used partially
1620*4882a593Smuzhiyun  *                        for the allocations >= 2 MB in size.
1621*4882a593Smuzhiyun  * @reg_lock:             Lock used for GPU virtual address space management operations,
1622*4882a593Smuzhiyun  *                        like adding/freeing a memory region in the address space.
1623*4882a593Smuzhiyun  *                        Can be converted to a rwlock ?.
1624*4882a593Smuzhiyun  * @reg_rbtree_same:      RB tree of the memory regions allocated from the SAME_VA
1625*4882a593Smuzhiyun  *                        zone of the GPU virtual address space. Used for allocations
1626*4882a593Smuzhiyun  *                        having the same value for GPU & CPU virtual address.
1627*4882a593Smuzhiyun  * @reg_rbtree_custom:    RB tree of the memory regions allocated from the CUSTOM_VA
1628*4882a593Smuzhiyun  *                        zone of the GPU virtual address space.
1629*4882a593Smuzhiyun  * @reg_rbtree_exec:      RB tree of the memory regions allocated from the EXEC_VA
1630*4882a593Smuzhiyun  *                        zone of the GPU virtual address space. Used for GPU-executable
1631*4882a593Smuzhiyun  *                        allocations which don't need the SAME_VA property.
1632*4882a593Smuzhiyun  * @reg_rbtree_exec_fixed: RB tree of the memory regions allocated from the
1633*4882a593Smuzhiyun  *                         EXEC_FIXED_VA zone of the GPU virtual address space. Used for
1634*4882a593Smuzhiyun  *                        GPU-executable allocations with FIXED/FIXABLE GPU virtual
1635*4882a593Smuzhiyun  *                        addresses.
1636*4882a593Smuzhiyun  * @reg_rbtree_fixed:     RB tree of the memory regions allocated from the FIXED_VA zone
1637*4882a593Smuzhiyun  *                        of the GPU virtual address space. Used for allocations with
1638*4882a593Smuzhiyun  *                        FIXED/FIXABLE GPU virtual addresses.
1639*4882a593Smuzhiyun  * @num_fixable_allocs:   A count for the number of memory allocations with the
1640*4882a593Smuzhiyun  *                        BASE_MEM_FIXABLE property.
1641*4882a593Smuzhiyun  * @num_fixed_allocs:     A count for the number of memory allocations with the
1642*4882a593Smuzhiyun  *                        BASE_MEM_FIXED property.
1643*4882a593Smuzhiyun  * @reg_zone:             Zone information for the reg_rbtree_<...> members.
1644*4882a593Smuzhiyun  * @cookies:              Bitmask containing of BITS_PER_LONG bits, used mainly for
1645*4882a593Smuzhiyun  *                        SAME_VA allocations to defer the reservation of memory region
1646*4882a593Smuzhiyun  *                        (from the GPU virtual address space) from base_mem_alloc
1647*4882a593Smuzhiyun  *                        ioctl to mmap system call. This helps returning unique
1648*4882a593Smuzhiyun  *                        handles, disguised as GPU VA, to Userspace from base_mem_alloc
1649*4882a593Smuzhiyun  *                        and later retrieving the pointer to memory region structure
1650*4882a593Smuzhiyun  *                        in the mmap handler.
1651*4882a593Smuzhiyun  * @pending_regions:      Array containing pointers to memory region structures,
1652*4882a593Smuzhiyun  *                        used in conjunction with @cookies bitmask mainly for
1653*4882a593Smuzhiyun  *                        providing a mechansim to have the same value for CPU &
1654*4882a593Smuzhiyun  *                        GPU virtual address.
1655*4882a593Smuzhiyun  * @event_queue:          Wait queue used for blocking the thread, which consumes
1656*4882a593Smuzhiyun  *                        the base_jd_event corresponding to an atom, when there
1657*4882a593Smuzhiyun  *                        are no more posted events.
1658*4882a593Smuzhiyun  * @tgid:                 Thread group ID of the process whose thread created
1659*4882a593Smuzhiyun  *                        the context (by calling KBASE_IOCTL_VERSION_CHECK or
1660*4882a593Smuzhiyun  *                        KBASE_IOCTL_SET_FLAGS, depending on the @api_version).
1661*4882a593Smuzhiyun  *                        This is usually, but not necessarily, the same as the
1662*4882a593Smuzhiyun  *                        process whose thread opened the device file
1663*4882a593Smuzhiyun  *                        /dev/malixx instance.
1664*4882a593Smuzhiyun  * @pid:                  ID of the thread, corresponding to process @tgid,
1665*4882a593Smuzhiyun  *                        which actually created the context. This is usually,
1666*4882a593Smuzhiyun  *                        but not necessarily, the same as the thread which
1667*4882a593Smuzhiyun  *                        opened the device file /dev/malixx instance.
1668*4882a593Smuzhiyun  * @csf:                  kbase csf context
1669*4882a593Smuzhiyun  * @jctx:                 object encapsulating all the Job dispatcher related state,
1670*4882a593Smuzhiyun  *                        including the array of atoms.
1671*4882a593Smuzhiyun  * @used_pages:           Keeps a track of the number of 4KB physical pages in use
1672*4882a593Smuzhiyun  *                        for the context.
1673*4882a593Smuzhiyun  * @nonmapped_pages:      Updated in the same way as @used_pages, except for the case
1674*4882a593Smuzhiyun  *                        when special tracking page is freed by userspace where it
1675*4882a593Smuzhiyun  *                        is reset to 0.
1676*4882a593Smuzhiyun  * @permanent_mapped_pages: Usage count of permanently mapped memory
1677*4882a593Smuzhiyun  * @mem_pools:            Context-specific pools of free physical memory pages.
1678*4882a593Smuzhiyun  * @reclaim:              Shrinker object registered with the kernel containing
1679*4882a593Smuzhiyun  *                        the pointer to callback function which is invoked under
1680*4882a593Smuzhiyun  *                        low memory conditions. In the callback function Driver
1681*4882a593Smuzhiyun  *                        frees up the memory for allocations marked as
1682*4882a593Smuzhiyun  *                        evictable/reclaimable.
1683*4882a593Smuzhiyun  * @evict_list:           List head for the list containing the allocations which
1684*4882a593Smuzhiyun  *                        can be evicted or freed up in the shrinker callback.
1685*4882a593Smuzhiyun  * @evict_nents:          Total number of pages allocated by the allocations within
1686*4882a593Smuzhiyun  *                        @evict_list (atomic).
1687*4882a593Smuzhiyun  * @waiting_soft_jobs:    List head for the list containing softjob atoms, which
1688*4882a593Smuzhiyun  *                        are either waiting for the event set operation, or waiting
1689*4882a593Smuzhiyun  *                        for the signaling of input fence or waiting for the GPU
1690*4882a593Smuzhiyun  *                        device to powered on so as to dump the CPU/GPU timestamps.
1691*4882a593Smuzhiyun  * @waiting_soft_jobs_lock: Lock to protect @waiting_soft_jobs list from concurrent
1692*4882a593Smuzhiyun  *                        accesses.
1693*4882a593Smuzhiyun  * @dma_fence:            Object containing list head for the list of dma-buf fence
1694*4882a593Smuzhiyun  *                        waiting atoms and the waitqueue to process the work item
1695*4882a593Smuzhiyun  *                        queued for the atoms blocked on the signaling of dma-buf
1696*4882a593Smuzhiyun  *                        fences.
1697*4882a593Smuzhiyun  * @dma_fence.waiting_resource: list head for the list of dma-buf fence
1698*4882a593Smuzhiyun  * @dma_fence.wq:         waitqueue to process the work item queued
1699*4882a593Smuzhiyun  * @as_nr:                id of the address space being used for the scheduled in
1700*4882a593Smuzhiyun  *                        context. This is effectively part of the Run Pool, because
1701*4882a593Smuzhiyun  *                        it only has a valid setting (!=KBASEP_AS_NR_INVALID) whilst
1702*4882a593Smuzhiyun  *                        the context is scheduled in. The hwaccess_lock must be held
1703*4882a593Smuzhiyun  *                        whilst accessing this.
1704*4882a593Smuzhiyun  *                        If the context relating to this value of as_nr is required,
1705*4882a593Smuzhiyun  *                        then the context must be retained to ensure that it doesn't
1706*4882a593Smuzhiyun  *                        disappear whilst it is being used. Alternatively, hwaccess_lock
1707*4882a593Smuzhiyun  *                        can be held to ensure the context doesn't disappear (but this
1708*4882a593Smuzhiyun  *                        has restrictions on what other locks can be taken simutaneously).
1709*4882a593Smuzhiyun  * @refcount:             Keeps track of the number of users of this context. A user
1710*4882a593Smuzhiyun  *                        can be a job that is available for execution, instrumentation
1711*4882a593Smuzhiyun  *                        needing to 'pin' a context for counter collection, etc.
1712*4882a593Smuzhiyun  *                        If the refcount reaches 0 then this context is considered
1713*4882a593Smuzhiyun  *                        inactive and the previously programmed AS might be cleared
1714*4882a593Smuzhiyun  *                        at any point.
1715*4882a593Smuzhiyun  *                        Generally the reference count is incremented when the context
1716*4882a593Smuzhiyun  *                        is scheduled in and an atom is pulled from the context's per
1717*4882a593Smuzhiyun  *                        slot runnable tree in JM GPU or GPU command queue
1718*4882a593Smuzhiyun  *                        group is programmed on CSG slot in CSF GPU.
1719*4882a593Smuzhiyun  * @process_mm:           Pointer to the memory descriptor of the process which
1720*4882a593Smuzhiyun  *                        created the context. Used for accounting the physical
1721*4882a593Smuzhiyun  *                        pages used for GPU allocations, done for the context,
1722*4882a593Smuzhiyun  *                        to the memory consumed by the process. A reference is taken
1723*4882a593Smuzhiyun  *                        on this descriptor for the Userspace created contexts so that
1724*4882a593Smuzhiyun  *                        Kbase can safely access it to update the memory usage counters.
1725*4882a593Smuzhiyun  *                        The reference is dropped on context termination.
1726*4882a593Smuzhiyun  * @gpu_va_end:           End address of the GPU va space (in 4KB page units)
1727*4882a593Smuzhiyun  * @running_total_tiler_heap_nr_chunks: Running total of number of chunks in all
1728*4882a593Smuzhiyun  *                        tiler heaps of the kbase context.
1729*4882a593Smuzhiyun  * @running_total_tiler_heap_memory: Running total of the tiler heap memory in the
1730*4882a593Smuzhiyun  *                        kbase context.
1731*4882a593Smuzhiyun  * @peak_total_tiler_heap_memory: Peak value of the total tiler heap memory in the
1732*4882a593Smuzhiyun  *                        kbase context.
1733*4882a593Smuzhiyun  * @jit_va:               Indicates if a JIT_VA zone has been created.
1734*4882a593Smuzhiyun  * @mem_profile_data:     Buffer containing the profiling information provided by
1735*4882a593Smuzhiyun  *                        Userspace, can be read through the mem_profile debugfs file.
1736*4882a593Smuzhiyun  * @mem_profile_size:     Size of the @mem_profile_data.
1737*4882a593Smuzhiyun  * @mem_profile_lock:     Lock to serialize the operations related to mem_profile
1738*4882a593Smuzhiyun  *                        debugfs file.
1739*4882a593Smuzhiyun  * @kctx_dentry:          Pointer to the debugfs directory created for every context,
1740*4882a593Smuzhiyun  *                        inside kbase_device::debugfs_ctx_directory, containing
1741*4882a593Smuzhiyun  *                        context specific files.
1742*4882a593Smuzhiyun  * @reg_dump:             Buffer containing a register offset & value pair, used
1743*4882a593Smuzhiyun  *                        for dumping job fault debug info.
1744*4882a593Smuzhiyun  * @job_fault_count:      Indicates that a job fault occurred for the context and
1745*4882a593Smuzhiyun  *                        dumping of its debug info is in progress.
1746*4882a593Smuzhiyun  * @job_fault_resume_event_list: List containing atoms completed after the faulty
1747*4882a593Smuzhiyun  *                        atom but before the debug data for faulty atom was dumped.
1748*4882a593Smuzhiyun  * @mem_view_column_width: Controls the number of bytes shown in every column of the
1749*4882a593Smuzhiyun  *                         output of "mem_view" debugfs file.
1750*4882a593Smuzhiyun  * @jsctx_queue:          Per slot & priority arrays of object containing the root
1751*4882a593Smuzhiyun  *                        of RB-tree holding currently runnable atoms on the job slot
1752*4882a593Smuzhiyun  *                        and the head item of the linked list of atoms blocked on
1753*4882a593Smuzhiyun  *                        cross-slot dependencies.
1754*4882a593Smuzhiyun  * @slot_tracking:        Tracking and control of this context's use of all job
1755*4882a593Smuzhiyun  *                        slots
1756*4882a593Smuzhiyun  * @atoms_pulled_all_slots: Total number of atoms currently pulled from the
1757*4882a593Smuzhiyun  *                        context, across all slots.
1758*4882a593Smuzhiyun  * @slots_pullable:       Bitmask of slots, indicating the slots for which the
1759*4882a593Smuzhiyun  *                        context has pullable atoms in the runnable tree.
1760*4882a593Smuzhiyun  * @work:                 Work structure used for deferred ASID assignment.
1761*4882a593Smuzhiyun  * @completed_jobs:       List containing completed atoms for which base_jd_event is
1762*4882a593Smuzhiyun  *                        to be posted.
1763*4882a593Smuzhiyun  * @work_count:           Number of work items, corresponding to atoms, currently
1764*4882a593Smuzhiyun  *                        pending on job_done workqueue of @jctx.
1765*4882a593Smuzhiyun  * @soft_job_timeout:     Timer object used for failing/cancelling the waiting
1766*4882a593Smuzhiyun  *                        soft-jobs which have been blocked for more than the
1767*4882a593Smuzhiyun  *                        timeout value used for the soft-jobs
1768*4882a593Smuzhiyun  * @jit_alloc:            Array of 256 pointers to GPU memory regions, used for
1769*4882a593Smuzhiyun  *                        just-in-time memory allocations.
1770*4882a593Smuzhiyun  * @jit_max_allocations:             Maximum allowed number of in-flight
1771*4882a593Smuzhiyun  *                                   just-in-time memory allocations.
1772*4882a593Smuzhiyun  * @jit_current_allocations:         Current number of in-flight just-in-time
1773*4882a593Smuzhiyun  *                                   memory allocations.
1774*4882a593Smuzhiyun  * @jit_current_allocations_per_bin: Current number of in-flight just-in-time
1775*4882a593Smuzhiyun  *                                   memory allocations per bin.
1776*4882a593Smuzhiyun  * @jit_group_id:         A memory group ID to be passed to a platform-specific
1777*4882a593Smuzhiyun  *                        memory group manager.
1778*4882a593Smuzhiyun  *                        Valid range is 0..(MEMORY_GROUP_MANAGER_NR_GROUPS-1).
1779*4882a593Smuzhiyun  * @jit_phys_pages_limit:      Limit of physical pages to apply across all
1780*4882a593Smuzhiyun  *                             just-in-time memory allocations, applied to
1781*4882a593Smuzhiyun  *                             @jit_current_phys_pressure.
1782*4882a593Smuzhiyun  * @jit_current_phys_pressure: Current 'pressure' on physical pages, which is
1783*4882a593Smuzhiyun  *                             the sum of the worst case estimate of pages that
1784*4882a593Smuzhiyun  *                             could be used (i.e. the
1785*4882a593Smuzhiyun  *                             &struct_kbase_va_region.nr_pages for all in-use
1786*4882a593Smuzhiyun  *                             just-in-time memory regions that have not yet had
1787*4882a593Smuzhiyun  *                             a usage report) and the actual number of pages
1788*4882a593Smuzhiyun  *                             that were used (i.e. the
1789*4882a593Smuzhiyun  *                             &struct_kbase_va_region.used_pages for regions
1790*4882a593Smuzhiyun  *                             that have had a usage report).
1791*4882a593Smuzhiyun  * @jit_phys_pages_to_be_allocated: Count of the physical pages that are being
1792*4882a593Smuzhiyun  *                                  now allocated for just-in-time memory
1793*4882a593Smuzhiyun  *                                  allocations of a context (across all the
1794*4882a593Smuzhiyun  *                                  threads). This is supposed to be updated
1795*4882a593Smuzhiyun  *                                  with @reg_lock held before allocating
1796*4882a593Smuzhiyun  *                                  the backing pages. This helps ensure that
1797*4882a593Smuzhiyun  *                                  total physical memory usage for just in
1798*4882a593Smuzhiyun  *                                  time memory allocation remains within the
1799*4882a593Smuzhiyun  *                                  @jit_phys_pages_limit in multi-threaded
1800*4882a593Smuzhiyun  *                                  scenarios.
1801*4882a593Smuzhiyun  * @jit_active_head:      List containing the just-in-time memory allocations
1802*4882a593Smuzhiyun  *                        which are in use.
1803*4882a593Smuzhiyun  * @jit_pool_head:        List containing the just-in-time memory allocations
1804*4882a593Smuzhiyun  *                        which have been freed up by userspace and so not being
1805*4882a593Smuzhiyun  *                        used by them.
1806*4882a593Smuzhiyun  *                        Driver caches them to quickly fulfill requests for new
1807*4882a593Smuzhiyun  *                        JIT allocations. They are released in case of memory
1808*4882a593Smuzhiyun  *                        pressure as they are put on the @evict_list when they
1809*4882a593Smuzhiyun  *                        are freed up by userspace.
1810*4882a593Smuzhiyun  * @jit_destroy_head:     List containing the just-in-time memory allocations
1811*4882a593Smuzhiyun  *                        which were moved to it from @jit_pool_head, in the
1812*4882a593Smuzhiyun  *                        shrinker callback, after freeing their backing
1813*4882a593Smuzhiyun  *                        physical pages.
1814*4882a593Smuzhiyun  * @jit_evict_lock:       Lock used for operations done on just-in-time memory
1815*4882a593Smuzhiyun  *                        allocations and also for accessing @evict_list.
1816*4882a593Smuzhiyun  * @jit_work:             Work item queued to defer the freeing of a memory
1817*4882a593Smuzhiyun  *                        region when a just-in-time memory allocation is moved
1818*4882a593Smuzhiyun  *                        to @jit_destroy_head.
1819*4882a593Smuzhiyun  * @ext_res_meta_head:    A list of sticky external resources which were requested to
1820*4882a593Smuzhiyun  *                        be mapped on GPU side, through a softjob atom of type
1821*4882a593Smuzhiyun  *                        EXT_RES_MAP or STICKY_RESOURCE_MAP ioctl.
1822*4882a593Smuzhiyun  * @age_count:            Counter incremented on every call to jd_submit_atom,
1823*4882a593Smuzhiyun  *                        atom is assigned the snapshot of this counter, which
1824*4882a593Smuzhiyun  *                        is used to determine the atom's age when it is added to
1825*4882a593Smuzhiyun  *                        the runnable RB-tree.
1826*4882a593Smuzhiyun  * @trim_level:           Level of JIT allocation trimming to perform on free (0-100%)
1827*4882a593Smuzhiyun  * @kprcs:                Reference to @struct kbase_process that the current
1828*4882a593Smuzhiyun  *                        kbase_context belongs to.
1829*4882a593Smuzhiyun  * @kprcs_link:           List link for the list of kbase context maintained
1830*4882a593Smuzhiyun  *                        under kbase_process.
1831*4882a593Smuzhiyun  * @gwt_enabled:          Indicates if tracking of GPU writes is enabled, protected by
1832*4882a593Smuzhiyun  *                        kbase_context.reg_lock.
1833*4882a593Smuzhiyun  * @gwt_was_enabled:      Simple sticky bit flag to know if GWT was ever enabled.
1834*4882a593Smuzhiyun  * @gwt_current_list:     A list of addresses for which GPU has generated write faults,
1835*4882a593Smuzhiyun  *                        after the last snapshot of it was sent to userspace.
1836*4882a593Smuzhiyun  * @gwt_snapshot_list:    Snapshot of the @gwt_current_list for sending to user space.
1837*4882a593Smuzhiyun  * @priority:             Indicates the context priority. Used along with @atoms_count
1838*4882a593Smuzhiyun  *                        for context scheduling, protected by hwaccess_lock.
1839*4882a593Smuzhiyun  * @atoms_count:          Number of GPU atoms currently in use, per priority
1840*4882a593Smuzhiyun  * @create_flags:         Flags used in context creation.
1841*4882a593Smuzhiyun  * @kinstr_jm:            Kernel job manager instrumentation context handle
1842*4882a593Smuzhiyun  * @tl_kctx_list_node:    List item into the device timeline's list of
1843*4882a593Smuzhiyun  *                        contexts, for timeline summarization.
1844*4882a593Smuzhiyun  * @limited_core_mask:    The mask that is applied to the affinity in case of atoms
1845*4882a593Smuzhiyun  *                        marked with BASE_JD_REQ_LIMITED_CORE_MASK.
1846*4882a593Smuzhiyun  * @platform_data:        Pointer to platform specific per-context data.
1847*4882a593Smuzhiyun  * @task:                 Pointer to the task structure of the main thread of the process
1848*4882a593Smuzhiyun  *                        that created the Kbase context. It would be set only for the
1849*4882a593Smuzhiyun  *                        contexts created by the Userspace and not for the contexts
1850*4882a593Smuzhiyun  *                        created internally by the Kbase.
1851*4882a593Smuzhiyun  *
1852*4882a593Smuzhiyun  * A kernel base context is an entity among which the GPU is scheduled.
1853*4882a593Smuzhiyun  * Each context has its own GPU address space.
1854*4882a593Smuzhiyun  * Up to one context can be created for each client that opens the device file
1855*4882a593Smuzhiyun  * /dev/malixx. Context creation is deferred until a special ioctl() system call
1856*4882a593Smuzhiyun  * is made on the device file.
1857*4882a593Smuzhiyun  */
1858*4882a593Smuzhiyun struct kbase_context {
1859*4882a593Smuzhiyun 	struct file *filp;
1860*4882a593Smuzhiyun 	struct kbase_device *kbdev;
1861*4882a593Smuzhiyun 	struct list_head kctx_list_link;
1862*4882a593Smuzhiyun 	struct kbase_mmu_table mmu;
1863*4882a593Smuzhiyun 
1864*4882a593Smuzhiyun 	u32 id;
1865*4882a593Smuzhiyun 	unsigned long api_version;
1866*4882a593Smuzhiyun 	struct list_head event_list;
1867*4882a593Smuzhiyun 	struct list_head event_coalesce_list;
1868*4882a593Smuzhiyun 	struct mutex event_mutex;
1869*4882a593Smuzhiyun #if !MALI_USE_CSF
1870*4882a593Smuzhiyun 	atomic_t event_closed;
1871*4882a593Smuzhiyun #endif
1872*4882a593Smuzhiyun 	struct workqueue_struct *event_workq;
1873*4882a593Smuzhiyun 	atomic_t event_count;
1874*4882a593Smuzhiyun 	int event_coalesce_count;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	atomic_t flags;
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun 	struct tagged_addr aliasing_sink_page;
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	spinlock_t              mem_partials_lock;
1881*4882a593Smuzhiyun 	struct list_head        mem_partials;
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	struct mutex            reg_lock;
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun 	struct rb_root reg_rbtree_same;
1886*4882a593Smuzhiyun 	struct rb_root reg_rbtree_custom;
1887*4882a593Smuzhiyun 	struct rb_root reg_rbtree_exec;
1888*4882a593Smuzhiyun #if MALI_USE_CSF
1889*4882a593Smuzhiyun 	struct rb_root reg_rbtree_exec_fixed;
1890*4882a593Smuzhiyun 	struct rb_root reg_rbtree_fixed;
1891*4882a593Smuzhiyun 	atomic64_t num_fixable_allocs;
1892*4882a593Smuzhiyun 	atomic64_t num_fixed_allocs;
1893*4882a593Smuzhiyun #endif
1894*4882a593Smuzhiyun 	struct kbase_reg_zone reg_zone[KBASE_REG_ZONE_MAX];
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun #if MALI_USE_CSF
1897*4882a593Smuzhiyun 	struct kbase_csf_context csf;
1898*4882a593Smuzhiyun #else
1899*4882a593Smuzhiyun 	struct kbase_jd_context jctx;
1900*4882a593Smuzhiyun 	struct jsctx_queue jsctx_queue
1901*4882a593Smuzhiyun 		[KBASE_JS_ATOM_SCHED_PRIO_COUNT][BASE_JM_MAX_NR_SLOTS];
1902*4882a593Smuzhiyun 	struct kbase_jsctx_slot_tracking slot_tracking[BASE_JM_MAX_NR_SLOTS];
1903*4882a593Smuzhiyun 	atomic_t atoms_pulled_all_slots;
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	struct list_head completed_jobs;
1906*4882a593Smuzhiyun 	atomic_t work_count;
1907*4882a593Smuzhiyun 	struct timer_list soft_job_timeout;
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 	int priority;
1910*4882a593Smuzhiyun 	s16 atoms_count[KBASE_JS_ATOM_SCHED_PRIO_COUNT];
1911*4882a593Smuzhiyun 	u32 slots_pullable;
1912*4882a593Smuzhiyun 	u32 age_count;
1913*4882a593Smuzhiyun #endif /* MALI_USE_CSF */
1914*4882a593Smuzhiyun 
1915*4882a593Smuzhiyun 	DECLARE_BITMAP(cookies, BITS_PER_LONG);
1916*4882a593Smuzhiyun 	struct kbase_va_region *pending_regions[BITS_PER_LONG];
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	wait_queue_head_t event_queue;
1919*4882a593Smuzhiyun 	pid_t tgid;
1920*4882a593Smuzhiyun 	pid_t pid;
1921*4882a593Smuzhiyun 	atomic_t used_pages;
1922*4882a593Smuzhiyun 	atomic_t nonmapped_pages;
1923*4882a593Smuzhiyun 	atomic_t permanent_mapped_pages;
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 	struct kbase_mem_pool_group mem_pools;
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	struct shrinker         reclaim;
1928*4882a593Smuzhiyun 	struct list_head        evict_list;
1929*4882a593Smuzhiyun 	atomic_t evict_nents;
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 	struct list_head waiting_soft_jobs;
1932*4882a593Smuzhiyun 	spinlock_t waiting_soft_jobs_lock;
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun 	int as_nr;
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	atomic_t refcount;
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 	struct mm_struct *process_mm;
1939*4882a593Smuzhiyun 	u64 gpu_va_end;
1940*4882a593Smuzhiyun #if MALI_USE_CSF
1941*4882a593Smuzhiyun 	u32 running_total_tiler_heap_nr_chunks;
1942*4882a593Smuzhiyun 	u64 running_total_tiler_heap_memory;
1943*4882a593Smuzhiyun 	u64 peak_total_tiler_heap_memory;
1944*4882a593Smuzhiyun #endif
1945*4882a593Smuzhiyun 	bool jit_va;
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DEBUG_FS)
1948*4882a593Smuzhiyun 	char *mem_profile_data;
1949*4882a593Smuzhiyun 	size_t mem_profile_size;
1950*4882a593Smuzhiyun 	struct mutex mem_profile_lock;
1951*4882a593Smuzhiyun 	struct dentry *kctx_dentry;
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	unsigned int *reg_dump;
1954*4882a593Smuzhiyun 	atomic_t job_fault_count;
1955*4882a593Smuzhiyun 	struct list_head job_fault_resume_event_list;
1956*4882a593Smuzhiyun 	unsigned int mem_view_column_width;
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
1959*4882a593Smuzhiyun 	struct kbase_va_region *jit_alloc[1 + BASE_JIT_ALLOC_COUNT];
1960*4882a593Smuzhiyun 	u8 jit_max_allocations;
1961*4882a593Smuzhiyun 	u8 jit_current_allocations;
1962*4882a593Smuzhiyun 	u8 jit_current_allocations_per_bin[256];
1963*4882a593Smuzhiyun 	u8 jit_group_id;
1964*4882a593Smuzhiyun #if MALI_JIT_PRESSURE_LIMIT_BASE
1965*4882a593Smuzhiyun 	u64 jit_phys_pages_limit;
1966*4882a593Smuzhiyun 	u64 jit_current_phys_pressure;
1967*4882a593Smuzhiyun 	u64 jit_phys_pages_to_be_allocated;
1968*4882a593Smuzhiyun #endif /* MALI_JIT_PRESSURE_LIMIT_BASE */
1969*4882a593Smuzhiyun 	struct list_head jit_active_head;
1970*4882a593Smuzhiyun 	struct list_head jit_pool_head;
1971*4882a593Smuzhiyun 	struct list_head jit_destroy_head;
1972*4882a593Smuzhiyun 	struct mutex jit_evict_lock;
1973*4882a593Smuzhiyun 	struct work_struct jit_work;
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 	struct list_head ext_res_meta_head;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	u8 trim_level;
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	struct kbase_process *kprcs;
1980*4882a593Smuzhiyun 	struct list_head kprcs_link;
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun #ifdef CONFIG_MALI_CINSTR_GWT
1983*4882a593Smuzhiyun 	bool gwt_enabled;
1984*4882a593Smuzhiyun 	bool gwt_was_enabled;
1985*4882a593Smuzhiyun 	struct list_head gwt_current_list;
1986*4882a593Smuzhiyun 	struct list_head gwt_snapshot_list;
1987*4882a593Smuzhiyun #endif
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	base_context_create_flags create_flags;
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun #if !MALI_USE_CSF
1992*4882a593Smuzhiyun 	struct kbase_kinstr_jm *kinstr_jm;
1993*4882a593Smuzhiyun #endif
1994*4882a593Smuzhiyun 	struct list_head tl_kctx_list_node;
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 	u64 limited_core_mask;
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun #if !MALI_USE_CSF
1999*4882a593Smuzhiyun 	void *platform_data;
2000*4882a593Smuzhiyun #endif
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 	struct task_struct *task;
2003*4882a593Smuzhiyun };
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun #ifdef CONFIG_MALI_CINSTR_GWT
2006*4882a593Smuzhiyun /**
2007*4882a593Smuzhiyun  * struct kbasep_gwt_list_element - Structure used to collect GPU
2008*4882a593Smuzhiyun  *                                  write faults.
2009*4882a593Smuzhiyun  * @link:                           List head for adding write faults.
2010*4882a593Smuzhiyun  * @region:                         Details of the region where we have the
2011*4882a593Smuzhiyun  *                                  faulting page address.
2012*4882a593Smuzhiyun  * @page_addr:                      Page address where GPU write fault occurred.
2013*4882a593Smuzhiyun  * @num_pages:                      The number of pages modified.
2014*4882a593Smuzhiyun  *
2015*4882a593Smuzhiyun  * Using this structure all GPU write faults are stored in a list.
2016*4882a593Smuzhiyun  */
2017*4882a593Smuzhiyun struct kbasep_gwt_list_element {
2018*4882a593Smuzhiyun 	struct list_head link;
2019*4882a593Smuzhiyun 	struct kbase_va_region *region;
2020*4882a593Smuzhiyun 	u64 page_addr;
2021*4882a593Smuzhiyun 	u64 num_pages;
2022*4882a593Smuzhiyun };
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun #endif
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun /**
2027*4882a593Smuzhiyun  * struct kbase_ctx_ext_res_meta - Structure which binds an external resource
2028*4882a593Smuzhiyun  *                                 to a @kbase_context.
2029*4882a593Smuzhiyun  * @ext_res_node:                  List head for adding the metadata to a
2030*4882a593Smuzhiyun  *                                 @kbase_context.
2031*4882a593Smuzhiyun  * @reg:                           External resource information, containing
2032*4882a593Smuzhiyun  *                                 the corresponding VA region
2033*4882a593Smuzhiyun  * @ref:                           Reference count.
2034*4882a593Smuzhiyun  *
2035*4882a593Smuzhiyun  * External resources can be mapped into multiple contexts as well as the same
2036*4882a593Smuzhiyun  * context multiple times.
2037*4882a593Smuzhiyun  * As kbase_va_region is refcounted, we guarantee that it will be available
2038*4882a593Smuzhiyun  * for the duration of the external resource, meaning it is sufficient to use
2039*4882a593Smuzhiyun  * it to rederive any additional data, like the GPU address.
2040*4882a593Smuzhiyun  * This metadata structure binds a single external resource to a single
2041*4882a593Smuzhiyun  * context, ensuring that per context mapping is tracked separately so it can
2042*4882a593Smuzhiyun  * be overridden when needed and abuses by the application (freeing the resource
2043*4882a593Smuzhiyun  * multiple times) don't effect the refcount of the physical allocation.
2044*4882a593Smuzhiyun  */
2045*4882a593Smuzhiyun struct kbase_ctx_ext_res_meta {
2046*4882a593Smuzhiyun 	struct list_head ext_res_node;
2047*4882a593Smuzhiyun 	struct kbase_va_region *reg;
2048*4882a593Smuzhiyun 	u32 ref;
2049*4882a593Smuzhiyun };
2050*4882a593Smuzhiyun 
2051*4882a593Smuzhiyun enum kbase_reg_access_type {
2052*4882a593Smuzhiyun 	REG_READ,
2053*4882a593Smuzhiyun 	REG_WRITE
2054*4882a593Smuzhiyun };
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun enum kbase_share_attr_bits {
2057*4882a593Smuzhiyun 	/* (1ULL << 8) bit is reserved */
2058*4882a593Smuzhiyun 	SHARE_BOTH_BITS = (2ULL << 8),	/* inner and outer shareable coherency */
2059*4882a593Smuzhiyun 	SHARE_INNER_BITS = (3ULL << 8)	/* inner shareable coherency */
2060*4882a593Smuzhiyun };
2061*4882a593Smuzhiyun 
2062*4882a593Smuzhiyun /**
2063*4882a593Smuzhiyun  * kbase_device_is_cpu_coherent - Returns if the device is CPU coherent.
2064*4882a593Smuzhiyun  * @kbdev: kbase device
2065*4882a593Smuzhiyun  *
2066*4882a593Smuzhiyun  * Return: true if the device access are coherent, false if not.
2067*4882a593Smuzhiyun  */
kbase_device_is_cpu_coherent(struct kbase_device * kbdev)2068*4882a593Smuzhiyun static inline bool kbase_device_is_cpu_coherent(struct kbase_device *kbdev)
2069*4882a593Smuzhiyun {
2070*4882a593Smuzhiyun 	if ((kbdev->system_coherency == COHERENCY_ACE_LITE) ||
2071*4882a593Smuzhiyun 			(kbdev->system_coherency == COHERENCY_ACE))
2072*4882a593Smuzhiyun 		return true;
2073*4882a593Smuzhiyun 
2074*4882a593Smuzhiyun 	return false;
2075*4882a593Smuzhiyun }
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun /**
2078*4882a593Smuzhiyun  * kbase_get_lock_region_min_size_log2 - Returns the minimum size of the MMU lock
2079*4882a593Smuzhiyun  * region, as a logarithm
2080*4882a593Smuzhiyun  *
2081*4882a593Smuzhiyun  * @gpu_props:   GPU properties
2082*4882a593Smuzhiyun  *
2083*4882a593Smuzhiyun  * Return: the minimum size of the MMU lock region as dictated by the corresponding
2084*4882a593Smuzhiyun  * arch spec.
2085*4882a593Smuzhiyun  */
kbase_get_lock_region_min_size_log2(struct kbase_gpu_props const * gpu_props)2086*4882a593Smuzhiyun static inline u64 kbase_get_lock_region_min_size_log2(struct kbase_gpu_props const *gpu_props)
2087*4882a593Smuzhiyun {
2088*4882a593Smuzhiyun 	if (GPU_ID2_MODEL_MATCH_VALUE(gpu_props->props.core_props.product_id) >=
2089*4882a593Smuzhiyun 	    GPU_ID2_MODEL_MAKE(12, 0))
2090*4882a593Smuzhiyun 		return 12; /* 4 kB */
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	return 15; /* 32 kB */
2093*4882a593Smuzhiyun }
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun /* Conversion helpers for setting up high resolution timers */
2096*4882a593Smuzhiyun #define HR_TIMER_DELAY_MSEC(x) (ns_to_ktime(((u64)(x))*1000000U))
2097*4882a593Smuzhiyun #define HR_TIMER_DELAY_NSEC(x) (ns_to_ktime(x))
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun /* Maximum number of loops polling the GPU for a cache flush before we assume it must have completed */
2100*4882a593Smuzhiyun #define KBASE_CLEAN_CACHE_MAX_LOOPS     100000
2101*4882a593Smuzhiyun /* Maximum number of loops polling the GPU for an AS command to complete before we assume the GPU has hung */
2102*4882a593Smuzhiyun #define KBASE_AS_INACTIVE_MAX_LOOPS     100000000
2103*4882a593Smuzhiyun /* Maximum number of loops polling the GPU PRFCNT_ACTIVE bit before we assume the GPU has hung */
2104*4882a593Smuzhiyun #define KBASE_PRFCNT_ACTIVE_MAX_LOOPS   100000000
2105*4882a593Smuzhiyun #endif /* _KBASE_DEFS_H_ */
2106