blob: 47551f2650da61085198a6380fe3d57e71b6adc2 [file] [log] [blame]
Dongjin Kimc1fea302024-09-13 12:12:25 +09001/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2/*
3 *
Dongjin Kim1618ab82025-02-12 16:56:02 +09004 * (C) COPYRIGHT 2022-2023 ARM Limited. All rights reserved.
Dongjin Kimc1fea302024-09-13 12:12:25 +09005 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22#ifndef _VERSION_COMPAT_DEFS_H_
23#define _VERSION_COMPAT_DEFS_H_
24
25#include <linux/version.h>
Dongjin Kim1618ab82025-02-12 16:56:02 +090026#include <linux/highmem.h>
27#include <linux/timer.h>
28
29#if (KERNEL_VERSION(4, 4, 267) < LINUX_VERSION_CODE)
30#include <linux/overflow.h>
31#endif
32
33#include <linux/bitops.h>
34#if (KERNEL_VERSION(4, 19, 0) <= LINUX_VERSION_CODE)
35#include <linux/bits.h>
36#endif
37
38#ifndef BITS_PER_TYPE
39#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
40#endif
Dongjin Kimc1fea302024-09-13 12:12:25 +090041
42#if KERNEL_VERSION(4, 16, 0) > LINUX_VERSION_CODE
43typedef unsigned int __poll_t;
44#endif
45
46#if KERNEL_VERSION(4, 9, 78) >= LINUX_VERSION_CODE
47
48#ifndef EPOLLHUP
49#define EPOLLHUP POLLHUP
50#endif
51
52#ifndef EPOLLERR
53#define EPOLLERR POLLERR
54#endif
55
56#ifndef EPOLLIN
57#define EPOLLIN POLLIN
58#endif
59
60#ifndef EPOLLRDNORM
61#define EPOLLRDNORM POLLRDNORM
62#endif
63
64#endif
65
Dongjin Kim1618ab82025-02-12 16:56:02 +090066#if KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE
67/* This is defined inside kbase for matching the default to kernel's
68 * mmap_min_addr, used inside file mali_kbase_mmap.c.
69 * Note: the value is set at compile time, matching a kernel's configuration
70 * value. It would not be able to track any runtime update of mmap_min_addr.
71 */
72#ifdef CONFIG_MMU
73#define kbase_mmap_min_addr CONFIG_DEFAULT_MMAP_MIN_ADDR
74
75#ifdef CONFIG_LSM_MMAP_MIN_ADDR
76#if (CONFIG_LSM_MMAP_MIN_ADDR > CONFIG_DEFAULT_MMAP_MIN_ADDR)
77/* Replace the default definition with CONFIG_LSM_MMAP_MIN_ADDR */
78#undef kbase_mmap_min_addr
79#define kbase_mmap_min_addr CONFIG_LSM_MMAP_MIN_ADDR
80#define KBASE_COMPILED_MMAP_MIN_ADDR_MSG \
81 "* MALI kbase_mmap_min_addr compiled to CONFIG_LSM_MMAP_MIN_ADDR, no runtime update possible! *"
82#endif /* (CONFIG_LSM_MMAP_MIN_ADDR > CONFIG_DEFAULT_MMAP_MIN_ADDR) */
83#endif /* CONFIG_LSM_MMAP_MIN_ADDR */
84
85#if (kbase_mmap_min_addr == CONFIG_DEFAULT_MMAP_MIN_ADDR)
86#define KBASE_COMPILED_MMAP_MIN_ADDR_MSG \
87 "* MALI kbase_mmap_min_addr compiled to CONFIG_DEFAULT_MMAP_MIN_ADDR, no runtime update possible! *"
88#endif
89
90#else /* CONFIG_MMU */
91#define kbase_mmap_min_addr (0UL)
92#define KBASE_COMPILED_MMAP_MIN_ADDR_MSG \
93 "* MALI kbase_mmap_min_addr compiled to (0UL), no runtime update possible! *"
94#endif /* CONFIG_MMU */
95#endif /* KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE */
96
97static inline void kbase_timer_setup(struct timer_list *timer,
98 void (*callback)(struct timer_list *timer))
99{
100#if KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE
101 setup_timer(timer, (void (*)(unsigned long))callback, (unsigned long)timer);
102#else
103 timer_setup(timer, callback, 0);
104#endif
105}
106
107#ifndef WRITE_ONCE
108#ifdef ASSIGN_ONCE
109#define WRITE_ONCE(x, val) ASSIGN_ONCE(val, x)
110#else
111#define WRITE_ONCE(x, val) (ACCESS_ONCE(x) = (val))
112#endif
113#endif
114
115#ifndef READ_ONCE
116#define READ_ONCE(x) ACCESS_ONCE(x)
117#endif
118
119static inline void *kbase_kmap(struct page *p)
120{
121#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
122 return kmap_local_page(p);
123#else
124 return kmap(p);
125#endif /* KERNEL_VERSION(5, 11, 0) */
126}
127
128static inline void *kbase_kmap_atomic(struct page *p)
129{
130#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
131 return kmap_local_page(p);
132#else
133 return kmap_atomic(p);
134#endif /* KERNEL_VERSION(5, 11, 0) */
135}
136
137static inline void kbase_kunmap(struct page *p, void *address)
138{
139#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
140 kunmap_local(address);
141#else
142 kunmap(p);
143#endif /* KERNEL_VERSION(5, 11, 0) */
144}
145
146static inline void kbase_kunmap_atomic(void *address)
147{
148#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
149 kunmap_local(address);
150#else
151 kunmap_atomic(address);
152#endif /* KERNEL_VERSION(5, 11, 0) */
153}
154
155/* Some of the older 4.4 kernel patch versions do
156 * not contain the overflow check functions. However,
157 * they are based on compiler instrinsics, so they
158 * are simple to reproduce.
159 */
160#if (KERNEL_VERSION(4, 4, 267) >= LINUX_VERSION_CODE)
161/* Some of the older 4.4 kernel patch versions do
162 * not contain the overflow check functions. However,
163 * they are based on compiler instrinsics, so they
164 * are simple to reproduce.
165 */
166#define check_mul_overflow(a, b, d) __builtin_mul_overflow(a, b, d)
167#endif
168
169/*
170 * There was a big rename in the 4.10 kernel (fence* -> dma_fence*),
171 * with most of the related functions keeping the same signatures.
172 */
173
174#if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
175
176#include <linux/fence.h>
177
178#define dma_fence fence
179#define dma_fence_ops fence_ops
180#define dma_fence_context_alloc(a) fence_context_alloc(a)
181#define dma_fence_init(a, b, c, d, e) fence_init(a, b, c, d, e)
182#define dma_fence_get(a) fence_get(a)
183#define dma_fence_put(a) fence_put(a)
184#define dma_fence_signal(a) fence_signal(a)
185#define dma_fence_is_signaled(a) fence_is_signaled(a)
186#define dma_fence_add_callback(a, b, c) fence_add_callback(a, b, c)
187#define dma_fence_remove_callback(a, b) fence_remove_callback(a, b)
188#define dma_fence_default_wait fence_default_wait
189
190#if (KERNEL_VERSION(4, 9, 68) <= LINUX_VERSION_CODE)
191#define dma_fence_get_status(a) (fence_is_signaled(a) ? (a)->error ?: 1 : 0)
192#else
193#define dma_fence_get_status(a) (fence_is_signaled(a) ? (a)->status ?: 1 : 0)
194#endif
195
196#else
197
198#include <linux/dma-fence.h>
199
200#if (KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE)
201#define dma_fence_get_status(a) (dma_fence_is_signaled(a) ? (a)->status ?: 1 : 0)
202#endif
203
204#endif /* < 4.10.0 */
205
206static inline void dma_fence_set_error_helper(
207#if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE)
208 struct fence *fence,
209#else
210 struct dma_fence *fence,
211#endif
212 int error)
213{
214#if (KERNEL_VERSION(4, 11, 0) <= LINUX_VERSION_CODE)
215 dma_fence_set_error(fence, error);
216#elif (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE && \
217 KERNEL_VERSION(4, 9, 68) <= LINUX_VERSION_CODE)
218 fence_set_error(fence, error);
219#else
220 fence->status = error;
221#endif
222}
223
224#include <linux/mm.h>
225#if !((KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE) || \
226 ((KERNEL_VERSION(6, 1, 25) <= LINUX_VERSION_CODE) && defined(__ANDROID_COMMON_KERNEL__)))
227static inline void vm_flags_set(struct vm_area_struct *vma, vm_flags_t flags)
228{
229 vma->vm_flags |= flags;
230}
231static inline void vm_flags_clear(struct vm_area_struct *vma, vm_flags_t flags)
232{
233 vma->vm_flags &= ~flags;
234}
235#endif
236
237#if (KERNEL_VERSION(6, 4, 0) <= LINUX_VERSION_CODE)
238#define KBASE_CLASS_CREATE(owner, name) class_create(name)
239#else
240#define KBASE_CLASS_CREATE(owner, name) class_create(owner, name)
241#endif
242
Dongjin Kimc1fea302024-09-13 12:12:25 +0900243#endif /* _VERSION_COMPAT_DEFS_H_ */