blob: 3607ef574ef81afc8363201ef866ed555328055a [file] [log] [blame]
Moritz Pflanzer45634b42017-08-30 12:48:18 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_TEST_HWC
25#define ARM_COMPUTE_TEST_HWC
26
27#include "hwc_names.hpp"
28
29#include <errno.h>
30#include <fcntl.h>
31#include <poll.h>
32#include <stddef.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/ioctl.h>
37#include <sys/mman.h>
38#include <unistd.h>
39
40#if defined(ANDROID) || defined(__ANDROID__)
41/* We use _IOR_BAD/_IOW_BAD rather than _IOR/_IOW otherwise fails to compile with NDK-BUILD because of _IOC_TYPECHECK is defined, not because the paramter is invalid */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000042#define MALI_IOR(a, b, c) _IOR_BAD(a, b, c)
43#define MALI_IOW(a, b, c) _IOW_BAD(a, b, c)
Moritz Pflanzer45634b42017-08-30 12:48:18 +010044#else /* defined(ANDROID) || defined(__ANDROID__) */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000045#define MALI_IOR(a, b, c) _IOR(a, b, c)
46#define MALI_IOW(a, b, c) _IOW(a, b, c)
Moritz Pflanzer45634b42017-08-30 12:48:18 +010047#endif /* defined(ANDROID) || defined(__ANDROID__) */
48
49namespace mali_userspace
50{
51union uk_header
52{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000053 uint32_t id;
54 uint32_t ret;
55 uint64_t sizer;
Moritz Pflanzer45634b42017-08-30 12:48:18 +010056};
57
58#define BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS 3
59#define BASE_MAX_COHERENT_GROUPS 16
60
61struct mali_base_gpu_core_props
62{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000063 uint32_t product_id;
64 uint16_t version_status;
65 uint16_t minor_revision;
66 uint16_t major_revision;
67 uint16_t padding;
68 uint32_t gpu_speed_mhz;
69 uint32_t gpu_freq_khz_max;
70 uint32_t gpu_freq_khz_min;
71 uint32_t log2_program_counter_size;
72 uint32_t texture_features[BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS];
73 uint64_t gpu_available_memory_size;
Moritz Pflanzer45634b42017-08-30 12:48:18 +010074};
75
76struct mali_base_gpu_l2_cache_props
77{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000078 uint8_t log2_line_size;
79 uint8_t log2_cache_size;
80 uint8_t num_l2_slices;
81 uint8_t padding[5];
Moritz Pflanzer45634b42017-08-30 12:48:18 +010082};
83
84struct mali_base_gpu_tiler_props
85{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000086 uint32_t bin_size_bytes;
87 uint32_t max_active_levels;
Moritz Pflanzer45634b42017-08-30 12:48:18 +010088};
89
90struct mali_base_gpu_thread_props
91{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +000092 uint32_t max_threads;
93 uint32_t max_workgroup_size;
94 uint32_t max_barrier_size;
95 uint16_t max_registers;
96 uint8_t max_task_queue;
97 uint8_t max_thread_group_split;
98 uint8_t impl_tech;
99 uint8_t padding[7];
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100100};
101
102struct mali_base_gpu_coherent_group
103{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000104 uint64_t core_mask;
105 uint16_t num_cores;
106 uint16_t padding[3];
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100107};
108
109struct mali_base_gpu_coherent_group_info
110{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000111 uint32_t num_groups;
112 uint32_t num_core_groups;
113 uint32_t coherency;
114 uint32_t padding;
115 mali_base_gpu_coherent_group group[BASE_MAX_COHERENT_GROUPS];
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100116};
117
118#define GPU_MAX_JOB_SLOTS 16
119struct gpu_raw_gpu_props
120{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000121 uint64_t shader_present;
122 uint64_t tiler_present;
123 uint64_t l2_present;
124 uint64_t unused_1;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100125
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000126 uint32_t l2_features;
127 uint32_t suspend_size;
128 uint32_t mem_features;
129 uint32_t mmu_features;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100130
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000131 uint32_t as_present;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100132
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000133 uint32_t js_present;
134 uint32_t js_features[GPU_MAX_JOB_SLOTS];
135 uint32_t tiler_features;
136 uint32_t texture_features[3];
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100137
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000138 uint32_t gpu_id;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100139
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000140 uint32_t thread_max_threads;
141 uint32_t thread_max_workgroup_size;
142 uint32_t thread_max_barrier_size;
143 uint32_t thread_features;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100144
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000145 uint32_t coherency_mode;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100146};
147
148struct mali_base_gpu_props
149{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000150 mali_base_gpu_core_props core_props;
151 mali_base_gpu_l2_cache_props l2_props;
152 uint64_t unused;
153 mali_base_gpu_tiler_props tiler_props;
154 mali_base_gpu_thread_props thread_props;
155 gpu_raw_gpu_props raw_props;
156 mali_base_gpu_coherent_group_info coherency_info;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100157};
158
159struct kbase_uk_gpuprops
160{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000161 uk_header header;
162 mali_base_gpu_props props;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100163};
164
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000165#define KBASE_GPUPROP_VALUE_SIZE_U8 (0x0)
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100166#define KBASE_GPUPROP_VALUE_SIZE_U16 (0x1)
167#define KBASE_GPUPROP_VALUE_SIZE_U32 (0x2)
168#define KBASE_GPUPROP_VALUE_SIZE_U64 (0x3)
169
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000170#define KBASE_GPUPROP_PRODUCT_ID 1
171#define KBASE_GPUPROP_MINOR_REVISION 3
172#define KBASE_GPUPROP_MAJOR_REVISION 4
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100173
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000174#define KBASE_GPUPROP_COHERENCY_NUM_GROUPS 61
175#define KBASE_GPUPROP_COHERENCY_NUM_CORE_GROUPS 62
176#define KBASE_GPUPROP_COHERENCY_GROUP_0 64
177#define KBASE_GPUPROP_COHERENCY_GROUP_1 65
178#define KBASE_GPUPROP_COHERENCY_GROUP_2 66
179#define KBASE_GPUPROP_COHERENCY_GROUP_3 67
180#define KBASE_GPUPROP_COHERENCY_GROUP_4 68
181#define KBASE_GPUPROP_COHERENCY_GROUP_5 69
182#define KBASE_GPUPROP_COHERENCY_GROUP_6 70
183#define KBASE_GPUPROP_COHERENCY_GROUP_7 71
184#define KBASE_GPUPROP_COHERENCY_GROUP_8 72
185#define KBASE_GPUPROP_COHERENCY_GROUP_9 73
186#define KBASE_GPUPROP_COHERENCY_GROUP_10 74
187#define KBASE_GPUPROP_COHERENCY_GROUP_11 75
188#define KBASE_GPUPROP_COHERENCY_GROUP_12 76
189#define KBASE_GPUPROP_COHERENCY_GROUP_13 77
190#define KBASE_GPUPROP_COHERENCY_GROUP_14 78
191#define KBASE_GPUPROP_COHERENCY_GROUP_15 79
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100192
193struct gpu_props
194{
195 uint32_t product_id;
196 uint16_t minor_revision;
197 uint16_t major_revision;
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000198 uint32_t num_groups;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100199 uint32_t num_core_groups;
200 uint64_t core_mask[16];
201};
202
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000203static const struct
204{
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100205 uint32_t type;
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000206 size_t offset;
207 int size;
208} gpu_property_mapping[] =
209{
210#define PROP(name, member) \
211 { \
212 KBASE_GPUPROP_##name, offsetof(struct gpu_props, member), \
213 sizeof(((struct gpu_props *)0)->member) \
214 }
215#define PROP2(name, member, off) \
216 { \
217 KBASE_GPUPROP_##name, offsetof(struct gpu_props, member) + off, \
218 sizeof(((struct gpu_props *)0)->member) \
219 }
220 PROP(PRODUCT_ID, product_id),
221 PROP(MINOR_REVISION, minor_revision),
222 PROP(MAJOR_REVISION, major_revision),
223 PROP(COHERENCY_NUM_GROUPS, num_groups),
224 PROP(COHERENCY_NUM_CORE_GROUPS, num_core_groups),
225 PROP2(COHERENCY_GROUP_0, core_mask, 0),
226 PROP2(COHERENCY_GROUP_1, core_mask, 1),
227 PROP2(COHERENCY_GROUP_2, core_mask, 2),
228 PROP2(COHERENCY_GROUP_3, core_mask, 3),
229 PROP2(COHERENCY_GROUP_4, core_mask, 4),
230 PROP2(COHERENCY_GROUP_5, core_mask, 5),
231 PROP2(COHERENCY_GROUP_6, core_mask, 6),
232 PROP2(COHERENCY_GROUP_7, core_mask, 7),
233 PROP2(COHERENCY_GROUP_8, core_mask, 8),
234 PROP2(COHERENCY_GROUP_9, core_mask, 9),
235 PROP2(COHERENCY_GROUP_10, core_mask, 10),
236 PROP2(COHERENCY_GROUP_11, core_mask, 11),
237 PROP2(COHERENCY_GROUP_12, core_mask, 12),
238 PROP2(COHERENCY_GROUP_13, core_mask, 13),
239 PROP2(COHERENCY_GROUP_14, core_mask, 14),
240 PROP2(COHERENCY_GROUP_15, core_mask, 15),
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100241#undef PROP
242#undef PROP2
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000243 { 0, 0, 0 }
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100244};
245
246struct kbase_hwcnt_reader_metadata
247{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000248 uint64_t timestamp = 0;
249 uint32_t event_id = 0;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100250 uint32_t buffer_idx = 0;
251};
252
253namespace
254{
255/** Message header */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000256union kbase_uk_hwcnt_header
257{
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100258 /* 32-bit number identifying the UK function to be called. */
259 uint32_t id;
260 /* The int return code returned by the called UK function. */
261 uint32_t ret;
262 /* Used to ensure 64-bit alignment of this union. Do not remove. */
263 uint64_t sizer;
264};
265
266/** IOCTL parameters to check version */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000267struct kbase_uk_hwcnt_reader_version_check_args
268{
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100269 union kbase_uk_hwcnt_header header;
270
271 uint16_t major;
272 uint16_t minor;
273 uint8_t padding[4];
274};
275
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000276union kbase_pointer
277{
278 void *value;
279 uint32_t compat_value;
280 uint64_t sizer;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100281};
282
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000283struct kbase_ioctl_get_gpuprops
284{
285 kbase_pointer buffer;
286 uint32_t size;
287 uint32_t flags;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100288};
289
290#define KBASE_IOCTL_TYPE 0x80
291#define KBASE_IOCTL_GET_GPUPROPS MALI_IOW(KBASE_IOCTL_TYPE, 3, struct kbase_ioctl_get_gpuprops)
292
293/** IOCTL parameters to set flags */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000294struct kbase_uk_hwcnt_reader_set_flags
295{
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100296 union kbase_uk_hwcnt_header header;
297
298 uint32_t create_flags;
299 uint32_t padding;
300};
301
302/** IOCTL parameters to configure reader */
303struct kbase_uk_hwcnt_reader_setup
304{
305 union kbase_uk_hwcnt_header header;
306
307 /* IN */
308 uint32_t buffer_count;
309 uint32_t jm_bm;
310 uint32_t shader_bm;
311 uint32_t tiler_bm;
312 uint32_t mmu_l2_bm;
313
314 /* OUT */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000315 int32_t fd;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100316};
317
318static const uint32_t HWCNT_READER_API = 1;
319
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100320struct uku_version_check_args
321{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000322 uk_header header;
323 uint16_t major;
324 uint16_t minor;
325 uint8_t padding[4];
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100326};
327
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000328enum
329{
330 UKP_FUNC_ID_CHECK_VERSION = 0,
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100331 /* Related to mali0 ioctl interface */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000332 LINUX_UK_BASE_MAGIC = 0x80,
333 BASE_CONTEXT_CREATE_KERNEL_FLAGS = 0x2,
334 KBASE_FUNC_HWCNT_UK_FUNC_ID = 512,
335 KBASE_FUNC_GPU_PROPS_REG_DUMP = KBASE_FUNC_HWCNT_UK_FUNC_ID + 14,
336 KBASE_FUNC_HWCNT_READER_SETUP = KBASE_FUNC_HWCNT_UK_FUNC_ID + 36,
337 KBASE_FUNC_HWCNT_DUMP = KBASE_FUNC_HWCNT_UK_FUNC_ID + 11,
338 KBASE_FUNC_HWCNT_CLEAR = KBASE_FUNC_HWCNT_UK_FUNC_ID + 12,
339 KBASE_FUNC_SET_FLAGS = KBASE_FUNC_HWCNT_UK_FUNC_ID + 18,
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100340
341 /* The ids of ioctl commands for the reader interface */
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000342 KBASE_HWCNT_READER = 0xBE,
343 KBASE_HWCNT_READER_GET_HWVER = MALI_IOR(KBASE_HWCNT_READER, 0x00, uint32_t),
344 KBASE_HWCNT_READER_GET_BUFFER_SIZE = MALI_IOR(KBASE_HWCNT_READER, 0x01, uint32_t),
345 KBASE_HWCNT_READER_DUMP = MALI_IOW(KBASE_HWCNT_READER, 0x10, uint32_t),
346 KBASE_HWCNT_READER_CLEAR = MALI_IOW(KBASE_HWCNT_READER, 0x11, uint32_t),
347 KBASE_HWCNT_READER_GET_BUFFER = MALI_IOR(KBASE_HWCNT_READER, 0x20, struct kbase_hwcnt_reader_metadata),
348 KBASE_HWCNT_READER_PUT_BUFFER = MALI_IOW(KBASE_HWCNT_READER, 0x21, struct kbase_hwcnt_reader_metadata),
349 KBASE_HWCNT_READER_SET_INTERVAL = MALI_IOW(KBASE_HWCNT_READER, 0x30, uint32_t),
350 KBASE_HWCNT_READER_ENABLE_EVENT = MALI_IOW(KBASE_HWCNT_READER, 0x40, uint32_t),
351 KBASE_HWCNT_READER_DISABLE_EVENT = MALI_IOW(KBASE_HWCNT_READER, 0x41, uint32_t),
352 KBASE_HWCNT_READER_GET_API_VERSION = MALI_IOW(KBASE_HWCNT_READER, 0xFF, uint32_t)
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100353
354};
355
356enum
357{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000358 PIPE_DESCRIPTOR_IN, /**< The index of a pipe's input descriptor. */
359 PIPE_DESCRIPTOR_OUT, /**< The index of a pipe's output descriptor. */
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100360
361 PIPE_DESCRIPTOR_COUNT /**< The number of descriptors forming a pipe. */
362};
363
364enum
365{
366 POLL_DESCRIPTOR_SIGNAL, /**< The index of the signal descriptor in poll fds array. */
367 POLL_DESCRIPTOR_HWCNT_READER, /**< The index of the hwcnt reader descriptor in poll fds array. */
368
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000369 POLL_DESCRIPTOR_COUNT /**< The number of descriptors poll is waiting for. */
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100370};
371
372/** Write a single byte into the pipe to interrupt the reader thread */
373typedef char poll_data_t;
374}
375
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000376template <typename T>
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100377static inline int mali_ioctl(int fd, T &arg)
378{
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000379 auto *hdr = &arg.header;
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100380 const int cmd = _IOC(_IOC_READ | _IOC_WRITE, LINUX_UK_BASE_MAGIC, hdr->id, sizeof(T));
381
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000382 if(ioctl(fd, cmd, &arg))
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100383 return -1;
Anthony Barbier88b8d8c2017-12-22 15:45:00 +0000384 if(hdr->ret)
Moritz Pflanzer45634b42017-08-30 12:48:18 +0100385 return -1;
386
387 return 0;
388}
389} // namespace mali_userspace
390#endif /* ARM_COMPUTE_TEST_HWC */