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 #include <mali_gpu_utils.h>
18 #include <string.h>
19
20 const int32_t kMaxVersionLength = 64;
21
initialize_mali_gpu(const int32_t mali_fd,mali_gpu_info * gpu_info)22 int32_t initialize_mali_gpu(const int32_t mali_fd, mali_gpu_info* gpu_info) {
23 int32_t ret = 0;
24 int32_t version_len = 0;
25
26 // Perform version check handshake
27 struct kbase_ioctl_version_check vc = {0};
28 ret = ioctl(mali_fd, KBASE_IOCTL_VERSION_CHECK_JM, &vc);
29 if (ret < 0) {
30 ret = ioctl(mali_fd, KBASE_IOCTL_VERSION_CHECK_CSF, &vc);
31 if (ret < 0) {
32 printf("Unexpected Mali GPU architecture!");
33 return EXIT_FAILURE;
34 }
35 gpu_info->is_csf = true;
36 }
37
38 // Set flags to initialize context
39 struct kbase_ioctl_set_flags set_flags = {.create_flags = 0};
40 ret = ioctl(mali_fd, KBASE_IOCTL_SET_FLAGS, &set_flags);
41 if (ret < 0) {
42 printf("Failed to set flags!");
43 return EXIT_FAILURE;
44 }
45
46 // Map tracking page
47 gpu_info->tracking_page = mmap(NULL, getpagesize(), PROT_NONE, MAP_SHARED,
48 mali_fd, BASE_MEM_MAP_TRACKING_HANDLE);
49 if (gpu_info->tracking_page == MAP_FAILED) {
50 printf("Failed to map tracking page!");
51 return EXIT_FAILURE;
52 }
53
54 // Get KMD version string length
55 struct kbase_ioctl_get_ddk_version get_version_len = {
56 .version_buffer = 0,
57 };
58 version_len = ioctl(mali_fd, KBASE_IOCTL_GET_DDK_VERSION, &get_version_len);
59 if (version_len < 0) {
60 teardown(gpu_info);
61 printf("Unexpected KMD version string length!");
62 return EXIT_FAILURE;
63 }
64
65 // Get KMD version string
66 char version_str[kMaxVersionLength];
67 memset(version_str, '\0', kMaxVersionLength);
68 struct kbase_ioctl_get_ddk_version get_version = {
69 .version_buffer = (__u64)&version_str,
70 .size = version_len,
71 };
72 ret = ioctl(mali_fd, KBASE_IOCTL_GET_DDK_VERSION, &get_version);
73 if (ret < 0) {
74 teardown(gpu_info);
75 printf("Unexpected KMD version string!");
76 return EXIT_FAILURE;
77 }
78 version_str[version_len] = '\0';
79
80 // Parse KMD version string with the format `K:r<version>pX-XXXXXX(GPL)`
81 sscanf(version_str, "K:r%up", &(gpu_info->version));
82 if (gpu_info->version == 0) {
83 teardown(gpu_info);
84 printf("Parsing failed! Unexpected KMD version string!");
85 return EXIT_FAILURE;
86 }
87 return EXIT_SUCCESS;
88 }
89
teardown(struct mali_gpu_info * gpu_info)90 void teardown(struct mali_gpu_info* gpu_info) {
91 if (!(gpu_info->tracking_page)) {
92 munmap(gpu_info->tracking_page, getpagesize());
93 gpu_info->tracking_page = NULL;
94 }
95 }
96