1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <err.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/ioctl.h>
27 #include <sys/mman.h>
28 #include <unistd.h>
29 
30 /* Kernel UAPI */
31 #define __u8 uint8_t
32 #define __u16 uint16_t
33 #define __u32 uint32_t
34 #define __u64 uint64_t
35 
36 #define LOCAL_PAGE_SHIFT 12
37 #define BASE_MEM_MAP_TRACKING_HANDLE (3ul << LOCAL_PAGE_SHIFT)
38 
39 /* IOCTL */
40 #define KBASE_IOCTL_TYPE 0x80
41 
42 typedef struct mali_gpu_info {
43     bool is_csf;
44     uint32_t version;
45     void* tracking_page;
46 } mali_gpu_info;
47 
48 struct kbase_ioctl_version_check {
49     __u16 major;
50     __u16 minor;
51 };
52 
53 struct kbase_ioctl_set_flags {
54     __u32 create_flags;
55 };
56 
57 struct kbase_ioctl_get_gpuprops {
58     __u64 buffer;
59     __u32 size;
60     __u32 flags;
61 };
62 
63 struct kbase_ioctl_get_ddk_version {
64     __u64 version_buffer;
65     __u32 size;
66     __u32 padding;
67 };
68 
69 #define KBASE_IOCTL_GET_DDK_VERSION _IOW(KBASE_IOCTL_TYPE, 13, struct kbase_ioctl_get_ddk_version)
70 #define KBASE_IOCTL_SET_FLAGS _IOW(KBASE_IOCTL_TYPE, 1, struct kbase_ioctl_set_flags)
71 #define KBASE_IOCTL_VERSION_CHECK_JM _IOWR(KBASE_IOCTL_TYPE, 0, struct kbase_ioctl_version_check)
72 #define KBASE_IOCTL_VERSION_CHECK_CSF _IOWR(KBASE_IOCTL_TYPE, 52, struct kbase_ioctl_version_check)
73 
74 int32_t initialize_mali_gpu(const int32_t mali_fd, mali_gpu_info* gpu_info);
75 void teardown(mali_gpu_info* gpu_info);
76