blob: 831b203bd042b93ed06b1b57b9924ca7b0b7cc0a [file] [log] [blame]
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -05001/*
2 * Copyright (C) 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27#include <xf86drm.h>
28
29#include "util/u_math.h"
30#include "util/macros.h"
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -040031#include "util/hash_table.h"
32#include "util/u_thread.h"
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -050033#include "drm-uapi/panfrost_drm.h"
34#include "pan_encoder.h"
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -040035#include "pan_device.h"
36#include "panfrost-quirks.h"
37#include "pan_bo.h"
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -050038
39/* Abstraction over the raw drm_panfrost_get_param ioctl for fetching
40 * information about devices */
41
42static __u64
43panfrost_query_raw(
44 int fd,
45 enum drm_panfrost_param param,
46 bool required,
47 unsigned default_value)
48{
49 struct drm_panfrost_get_param get_param = {0,};
50 ASSERTED int ret;
51
Alyssa Rosenzweigf6ca7ea2020-02-25 11:52:52 -050052 get_param.param = param;
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -050053 ret = drmIoctl(fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
54
55 if (ret) {
56 assert(!required);
57 return default_value;
58 }
59
60 return get_param.value;
61}
62
63unsigned
64panfrost_query_gpu_version(int fd)
65{
66 return panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_PROD_ID, true, 0);
67}
68
69unsigned
70panfrost_query_core_count(int fd)
71{
72 /* On older kernels, worst-case to 16 cores */
73
74 unsigned mask = panfrost_query_raw(fd,
75 DRM_PANFROST_PARAM_SHADER_PRESENT, false, 0xffff);
76
77 return util_bitcount(mask);
78}
79
80unsigned
81panfrost_query_thread_tls_alloc(int fd)
82{
Alyssa Rosenzweigf37cec32020-02-25 15:34:51 -050083 /* On older kernels, we worst-case to 256 threads, the architectural
84 * maximum for Midgard. On my current kernel/hardware, I'm seeing this
85 * readback as 0, so we'll worst-case there too */
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -050086
Alyssa Rosenzweigf37cec32020-02-25 15:34:51 -050087 unsigned tls = panfrost_query_raw(fd,
88 DRM_PANFROST_PARAM_THREAD_TLS_ALLOC, false, 256);
89
90 if (tls)
91 return tls;
92 else
93 return 256;
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -050094}
95
Alyssa Rosenzweig407a0522020-07-10 10:42:24 -040096static uint32_t
97panfrost_query_compressed_formats(int fd)
98{
99 /* If unspecified, assume ASTC/ETC only. Factory default for Juno, and
100 * should exist on any Mali configuration. All hardware should report
101 * these texture formats but the kernel might not be new enough. */
102
103 uint32_t default_set =
104 (1 << MALI_ETC2_RGB8) |
105 (1 << MALI_ETC2_R11_UNORM) |
106 (1 << MALI_ETC2_RGBA8) |
107 (1 << MALI_ETC2_RG11_UNORM) |
108 (1 << MALI_ETC2_R11_SNORM) |
109 (1 << MALI_ETC2_RG11_SNORM) |
110 (1 << MALI_ETC2_RGB8A1) |
111 (1 << MALI_ASTC_3D_LDR) |
112 (1 << MALI_ASTC_3D_HDR) |
113 (1 << MALI_ASTC_2D_LDR) |
114 (1 << MALI_ASTC_2D_HDR);
115
116 return panfrost_query_raw(fd, DRM_PANFROST_PARAM_TEXTURE_FEATURES0,
117 false, default_set);
118}
119
120/* DRM_PANFROST_PARAM_TEXTURE_FEATURES0 will return a bitmask of supported
121 * compressed formats, so we offer a helper to test if a format is supported */
122
123bool
124panfrost_supports_compressed_format(struct panfrost_device *dev, unsigned fmt)
125{
126 if (MALI_EXTRACT_TYPE(fmt) != MALI_FORMAT_COMPRESSED)
127 return true;
128
129 unsigned idx = fmt & ~MALI_FORMAT_COMPRESSED;
130 assert(idx < 32);
131
132 return dev->compressed_formats & (1 << idx);
133}
134
Alyssa Rosenzweig09a2c742019-12-09 16:02:03 -0500135/* Given a GPU ID like 0x860, return a prettified model name */
Alyssa Rosenzweiga2152892019-12-09 15:54:09 -0500136
Alyssa Rosenzweig09a2c742019-12-09 16:02:03 -0500137const char *
138panfrost_model_name(unsigned gpu_id)
139{
140 switch (gpu_id) {
141 case 0x600: return "Mali T600 (Panfrost)";
142 case 0x620: return "Mali T620 (Panfrost)";
143 case 0x720: return "Mali T720 (Panfrost)";
144 case 0x820: return "Mali T820 (Panfrost)";
145 case 0x830: return "Mali T830 (Panfrost)";
146 case 0x750: return "Mali T760 (Panfrost)";
147 case 0x860: return "Mali T860 (Panfrost)";
148 case 0x880: return "Mali T880 (Panfrost)";
Boris Brezillonfefb3e92020-09-23 11:08:02 +0200149 case 0x6221: return "Mali G72 (Panfrost)";
Alyssa Rosenzweigbe8cbe02020-05-29 19:24:05 -0400150 case 0x7093: return "Mali G31 (Panfrost)";
151 case 0x7212: return "Mali G52 (Panfrost)";
Alyssa Rosenzweig09a2c742019-12-09 16:02:03 -0500152 default:
153 unreachable("Invalid GPU ID");
154 }
155}
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400156
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400157void
158panfrost_open_device(void *memctx, int fd, struct panfrost_device *dev)
159{
160 dev->fd = fd;
161 dev->memctx = memctx;
162 dev->gpu_id = panfrost_query_gpu_version(fd);
163 dev->core_count = panfrost_query_core_count(fd);
164 dev->thread_tls_alloc = panfrost_query_thread_tls_alloc(fd);
165 dev->kernel_version = drmGetVersion(fd);
166 dev->quirks = panfrost_get_quirks(dev->gpu_id);
Alyssa Rosenzweig407a0522020-07-10 10:42:24 -0400167 dev->compressed_formats = panfrost_query_compressed_formats(fd);
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400168
Alyssa Rosenzweig169dbb52020-05-25 19:48:30 -0400169 util_sparse_array_init(&dev->bo_map, sizeof(struct panfrost_bo), 512);
170
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400171 pthread_mutex_init(&dev->bo_cache.lock, NULL);
172 list_inithead(&dev->bo_cache.lru);
173
174 for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i)
175 list_inithead(&dev->bo_cache.buckets[i]);
Alyssa Rosenzweigd8deb1e2020-08-17 13:14:54 -0400176
177 /* Tiler heap is internally required by the tiler, which can only be
178 * active for a single job chain at once, so a single heap can be
179 * shared across batches/contextes */
180
181 dev->tiler_heap = panfrost_bo_create(dev, 4096 * 4096,
182 PAN_BO_INVISIBLE | PAN_BO_GROWABLE);
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400183}
184
185void
186panfrost_close_device(struct panfrost_device *dev)
187{
Alyssa Rosenzweig293f2512020-07-09 13:42:25 -0400188 panfrost_bo_unreference(dev->blit_shaders.bo);
Alyssa Rosenzweigd8deb1e2020-08-17 13:14:54 -0400189 panfrost_bo_unreference(dev->tiler_heap);
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400190 panfrost_bo_cache_evict_all(dev);
191 pthread_mutex_destroy(&dev->bo_cache.lock);
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400192 drmFreeVersion(dev->kernel_version);
Alyssa Rosenzweig169dbb52020-05-25 19:48:30 -0400193 util_sparse_array_finish(&dev->bo_map);
Alyssa Rosenzweig39378ee2020-03-24 13:40:12 -0400194
195}