2 * Copyright (C) 2019 Collabora, Ltd.
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:
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
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
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 #include "util/u_math.h"
30 #include "util/macros.h"
31 #include "util/hash_table.h"
32 #include "util/u_thread.h"
33 #include "drm-uapi/panfrost_drm.h"
34 #include "pan_encoder.h"
35 #include "pan_device.h"
36 #include "panfrost-quirks.h"
39 /* Abstraction over the raw drm_panfrost_get_param ioctl for fetching
40 * information about devices */
45 enum drm_panfrost_param param
,
47 unsigned default_value
)
49 struct drm_panfrost_get_param get_param
= {0,};
52 get_param
.param
= param
;
53 ret
= drmIoctl(fd
, DRM_IOCTL_PANFROST_GET_PARAM
, &get_param
);
60 return get_param
.value
;
64 panfrost_query_gpu_version(int fd
)
66 return panfrost_query_raw(fd
, DRM_PANFROST_PARAM_GPU_PROD_ID
, true, 0);
70 panfrost_query_core_count(int fd
)
72 /* On older kernels, worst-case to 16 cores */
74 unsigned mask
= panfrost_query_raw(fd
,
75 DRM_PANFROST_PARAM_SHADER_PRESENT
, false, 0xffff);
77 return util_bitcount(mask
);
81 panfrost_query_thread_tls_alloc(int fd
)
83 /* 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 */
87 unsigned tls
= panfrost_query_raw(fd
,
88 DRM_PANFROST_PARAM_THREAD_TLS_ALLOC
, false, 256);
96 /* Given a GPU ID like 0x860, return a prettified model name */
99 panfrost_model_name(unsigned gpu_id
)
102 case 0x600: return "Mali T600 (Panfrost)";
103 case 0x620: return "Mali T620 (Panfrost)";
104 case 0x720: return "Mali T720 (Panfrost)";
105 case 0x820: return "Mali T820 (Panfrost)";
106 case 0x830: return "Mali T830 (Panfrost)";
107 case 0x750: return "Mali T760 (Panfrost)";
108 case 0x860: return "Mali T860 (Panfrost)";
109 case 0x880: return "Mali T880 (Panfrost)";
111 unreachable("Invalid GPU ID");
116 panfrost_active_bos_hash(const void *key
)
118 const struct panfrost_bo
*bo
= key
;
120 return _mesa_hash_data(&bo
->gem_handle
, sizeof(bo
->gem_handle
));
124 panfrost_active_bos_cmp(const void *keya
, const void *keyb
)
126 const struct panfrost_bo
*a
= keya
, *b
= keyb
;
128 return a
->gem_handle
== b
->gem_handle
;
132 panfrost_open_device(void *memctx
, int fd
, struct panfrost_device
*dev
)
135 dev
->memctx
= memctx
;
136 dev
->gpu_id
= panfrost_query_gpu_version(fd
);
137 dev
->core_count
= panfrost_query_core_count(fd
);
138 dev
->thread_tls_alloc
= panfrost_query_thread_tls_alloc(fd
);
139 dev
->kernel_version
= drmGetVersion(fd
);
140 dev
->quirks
= panfrost_get_quirks(dev
->gpu_id
);
142 pthread_mutex_init(&dev
->active_bos_lock
, NULL
);
143 dev
->active_bos
= _mesa_set_create(memctx
,
144 panfrost_active_bos_hash
, panfrost_active_bos_cmp
);
146 pthread_mutex_init(&dev
->bo_cache
.lock
, NULL
);
147 list_inithead(&dev
->bo_cache
.lru
);
149 for (unsigned i
= 0; i
< ARRAY_SIZE(dev
->bo_cache
.buckets
); ++i
)
150 list_inithead(&dev
->bo_cache
.buckets
[i
]);
154 panfrost_close_device(struct panfrost_device
*dev
)
156 panfrost_bo_cache_evict_all(dev
);
157 pthread_mutex_destroy(&dev
->bo_cache
.lock
);
158 pthread_mutex_destroy(&dev
->active_bos_lock
);
159 drmFreeVersion(dev
->kernel_version
);