| 1 |
/* |
| 2 |
* Copyright © 2023 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <errno.h> |
| 7 |
#include <fcntl.h> |
| 8 |
#include <string.h> |
| 9 |
#include <xf86drm.h> |
| 10 |
|
| 11 |
#include "util/hash_table.h" |
| 12 |
#include "util/macros.h" |
| 13 |
#include "util/simple_mtx.h" |
| 14 |
#include "util/stack_array.h" |
| 15 |
|
| 16 |
#include "drm-uapi/panfrost_drm.h" |
| 17 |
|
| 18 |
#include "pan_kmod_backend.h" |
| 19 |
|
| 20 |
#include "pan_props.h" |
| 21 |
|
| 22 |
/* Maximum kmod BO label length, including NUL-terminator */ |
| 23 |
#define PANFROST_BO_LABEL_MAXLEN 4096 |
| 24 |
|
| 25 |
const struct pan_kmod_ops panfrost_kmod_ops; |
| 26 |
|
| 27 |
struct panfrost_kmod_vm { |
| 28 |
struct pan_kmod_vm base; |
| 29 |
}; |
| 30 |
|
| 31 |
struct panfrost_kmod_dev { |
| 32 |
struct pan_kmod_dev base; |
| 33 |
struct panfrost_kmod_vm *vm; |
| 34 |
}; |
| 35 |
|
| 36 |
struct panfrost_kmod_bo { |
| 37 |
struct pan_kmod_bo base; |
| 38 |
|
| 39 |
/* This is actually the VA assigned to the BO at creation/import time. |
| 40 |
* We don't control it, it's automatically assigned by the kernel driver. |
| 41 |
*/ |
| 42 |
uint64_t offset; |
| 43 |
}; |
| 44 |
|
| 45 |
/* Abstraction over the raw drm_panfrost_get_param ioctl for fetching |
| 46 |
* information about devices. |
| 47 |
*/ |
| 48 |
static __u64 |
| 49 |
panfrost_query_raw(int fd, enum drm_panfrost_param param, bool required, |
| 50 |
unsigned default_value) |
| 51 |
{ |
| 52 |
struct drm_panfrost_get_param get_param = {}; |
| 53 |
ASSERTED int ret; |
| 54 |
|
| 55 |
get_param.param = param; |
| 56 |
ret = pan_kmod_ioctl(fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param); |
| 57 |
|
| 58 |
if (ret) { |
| 59 |
assert(!required); |
| 60 |
return default_value; |
| 61 |
} |
| 62 |
|
| 63 |
return get_param.value; |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
panfrost_dev_query_thread_props(struct panfrost_kmod_dev *panfrost_dev) |
| 68 |
{ |
| 69 |
struct pan_kmod_dev_props *props = &panfrost_dev->base.props; |
| 70 |
const struct pan_kmod_dev *dev = &panfrost_dev->base; |
| 71 |
int fd = dev->fd; |
| 72 |
|
| 73 |
props->max_threads_per_core = |
| 74 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_MAX_THREADS, true, 0); |
| 75 |
if (!props->max_threads_per_core) { |
| 76 |
switch (pan_arch(props->gpu_id)) { |
| 77 |
case 4: |
| 78 |
case 5: |
| 79 |
props->max_threads_per_core = 256; |
| 80 |
break; |
| 81 |
|
| 82 |
case 6: |
| 83 |
/* Bifrost, first generation */ |
| 84 |
props->max_threads_per_core = 384; |
| 85 |
break; |
| 86 |
|
| 87 |
case 7: |
| 88 |
/* Bifrost, second generation (G31 is 512 but it doesn't matter) */ |
| 89 |
props->max_threads_per_core = 768; |
| 90 |
break; |
| 91 |
|
| 92 |
case 9: |
| 93 |
/* Valhall, first generation. */ |
| 94 |
props->max_threads_per_core = 512; |
| 95 |
break; |
| 96 |
|
| 97 |
default: |
| 98 |
assert(!"Unsupported arch"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
props->max_threads_per_wg = panfrost_query_raw( |
| 103 |
fd, DRM_PANFROST_PARAM_THREAD_MAX_WORKGROUP_SZ, true, 0); |
| 104 |
if (!props->max_threads_per_wg) |
| 105 |
props->max_threads_per_wg = props->max_threads_per_core; |
| 106 |
|
| 107 |
uint32_t thread_features = |
| 108 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_THREAD_FEATURES, true, 0); |
| 109 |
props->max_tasks_per_core = MAX2(thread_features >> 24, 1); |
| 110 |
props->num_registers_per_core = thread_features & 0xffff; |
| 111 |
if (!props->num_registers_per_core) { |
| 112 |
switch (pan_arch(props->gpu_id)) { |
| 113 |
case 4: |
| 114 |
case 5: |
| 115 |
/* Assume we can always schedule max_threads_per_core when using 4 |
| 116 |
* registers per-shader or less. |
| 117 |
*/ |
| 118 |
props->num_registers_per_core = props->max_threads_per_core * 4; |
| 119 |
break; |
| 120 |
|
| 121 |
case 6: |
| 122 |
/* Assume we can always schedule max_threads_per_core for shader |
| 123 |
* using the full per-shader register file (64 regs). |
| 124 |
*/ |
| 125 |
props->num_registers_per_core = props->max_threads_per_core * 64; |
| 126 |
break; |
| 127 |
|
| 128 |
case 7: |
| 129 |
case 9: |
| 130 |
/* Assume we can always schedule max_threads_per_core for shaders |
| 131 |
* using half the per-shader register file (32 regs). |
| 132 |
*/ |
| 133 |
props->num_registers_per_core = props->max_threads_per_core * 32; |
| 134 |
break; |
| 135 |
|
| 136 |
default: |
| 137 |
assert(!"Unsupported arch"); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
props->max_tls_instance_per_core = |
| 142 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_THREAD_TLS_ALLOC, true, 0); |
| 143 |
if (!props->max_tls_instance_per_core) |
| 144 |
props->max_tls_instance_per_core = props->max_threads_per_core; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
panfrost_dev_query_props(struct panfrost_kmod_dev *panfrost_dev) |
| 149 |
{ |
| 150 |
struct pan_kmod_dev_props *props = &panfrost_dev->base.props; |
| 151 |
const struct pan_kmod_dev *dev = &panfrost_dev->base; |
| 152 |
int fd = dev->fd; |
| 153 |
|
| 154 |
memset(props, 0, sizeof(*props)); |
| 155 |
props->gpu_id = |
| 156 |
(panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_PROD_ID, true, 0) << 16) | |
| 157 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_GPU_REVISION, true, 0); |
| 158 |
props->shader_present = |
| 159 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_SHADER_PRESENT, true, 0); |
| 160 |
props->tiler_features = |
| 161 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_TILER_FEATURES, true, 0); |
| 162 |
props->mem_features = |
| 163 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_MEM_FEATURES, true, 0); |
| 164 |
props->mmu_features = |
| 165 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_MMU_FEATURES, true, 0); |
| 166 |
|
| 167 |
for (unsigned i = 0; i < ARRAY_SIZE(props->texture_features); i++) { |
| 168 |
props->texture_features[i] = panfrost_query_raw( |
| 169 |
fd, DRM_PANFROST_PARAM_TEXTURE_FEATURES0 + i, true, 0); |
| 170 |
} |
| 171 |
|
| 172 |
props->afbc_features = |
| 173 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_AFBC_FEATURES, true, 0); |
| 174 |
|
| 175 |
panfrost_dev_query_thread_props(panfrost_dev); |
| 176 |
|
| 177 |
if (pan_kmod_driver_version_at_least(&dev->driver, 1, 3)) { |
| 178 |
props->gpu_can_query_timestamp = true; |
| 179 |
props->timestamp_frequency = panfrost_query_raw( |
| 180 |
fd, DRM_PANFROST_PARAM_SYSTEM_TIMESTAMP_FREQUENCY, true, 0); |
| 181 |
} |
| 182 |
|
| 183 |
/* Device coherent timestamps are always enabled on panfrost */ |
| 184 |
props->timestamp_device_coherent = true; |
| 185 |
|
| 186 |
/* Support for priorities was added in panfrost 1.5, assumes default |
| 187 |
* priority as medium if the param doesn't exist. */ |
| 188 |
uint64_t prios = |
| 189 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_ALLOWED_JM_CTX_PRIORITIES, |
| 190 |
false, BITFIELD_BIT(PANFROST_JM_CTX_PRIORITY_MEDIUM)); |
| 191 |
|
| 192 |
if (prios & BITFIELD_BIT(PANFROST_JM_CTX_PRIORITY_LOW)) |
| 193 |
props->allowed_group_priorities_mask |= PAN_KMOD_GROUP_ALLOW_PRIORITY_LOW; |
| 194 |
if (prios & BITFIELD_BIT(PANFROST_JM_CTX_PRIORITY_MEDIUM)) |
| 195 |
props->allowed_group_priorities_mask |= |
| 196 |
PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM; |
| 197 |
if (prios & BITFIELD_BIT(PANFROST_JM_CTX_PRIORITY_HIGH)) |
| 198 |
props->allowed_group_priorities_mask |= |
| 199 |
PAN_KMOD_GROUP_ALLOW_PRIORITY_HIGH; |
| 200 |
|
| 201 |
props->supported_bo_flags = PAN_KMOD_BO_FLAG_EXECUTABLE | |
| 202 |
PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT | |
| 203 |
PAN_KMOD_BO_FLAG_NO_MMAP; |
| 204 |
|
| 205 |
if (pan_kmod_driver_version_at_least(&dev->driver, 1, 6)) { |
| 206 |
uint32_t selected_coherency = |
| 207 |
panfrost_query_raw(fd, DRM_PANFROST_PARAM_SELECTED_COHERENCY, true, |
| 208 |
DRM_PANFROST_GPU_COHERENCY_NONE); |
| 209 |
|
| 210 |
props->supported_bo_flags |= PAN_KMOD_BO_FLAG_WB_MMAP; |
| 211 |
props->is_io_coherent = |
| 212 |
selected_coherency != DRM_PANFROST_GPU_COHERENCY_NONE; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static struct pan_kmod_dev * |
| 217 |
panfrost_kmod_dev_create(int fd, uint32_t flags, drmVersionPtr version, |
| 218 |
const struct pan_kmod_allocator *allocator) |
| 219 |
{ |
| 220 |
if (version->version_major < 1 || |
| 221 |
(version->version_major == 1 && version->version_minor < 1)) { |
| 222 |
mesa_loge("kernel driver is too old (requires at least 1.1, found %d.%d)", |
| 223 |
version->version_major, version->version_minor); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
struct panfrost_kmod_dev *panfrost_dev = |
| 228 |
pan_kmod_alloc(allocator, sizeof(*panfrost_dev)); |
| 229 |
if (!panfrost_dev) { |
| 230 |
mesa_loge("failed to allocate a panfrost_kmod_dev object"); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
pan_kmod_dev_init(&panfrost_dev->base, fd, flags, version, |
| 235 |
&panfrost_kmod_ops, allocator); |
| 236 |
panfrost_dev_query_props(panfrost_dev); |
| 237 |
|
| 238 |
return &panfrost_dev->base; |
| 239 |
} |
| 240 |
|
| 241 |
static void |
| 242 |
panfrost_kmod_dev_destroy(struct pan_kmod_dev *dev) |
| 243 |
{ |
| 244 |
struct panfrost_kmod_dev *panfrost_dev = |
| 245 |
container_of(dev, struct panfrost_kmod_dev, base); |
| 246 |
|
| 247 |
pan_kmod_dev_cleanup(dev); |
| 248 |
pan_kmod_free(dev->allocator, panfrost_dev); |
| 249 |
} |
| 250 |
|
| 251 |
static uint32_t |
| 252 |
to_panfrost_bo_flags(struct pan_kmod_dev *dev, uint32_t flags) |
| 253 |
{ |
| 254 |
uint32_t panfrost_flags = 0; |
| 255 |
|
| 256 |
if (pan_kmod_driver_version_at_least(&dev->driver, 1, 1)) { |
| 257 |
/* The alloc-on-fault feature is only used for the tiler HEAP object, |
| 258 |
* hence the name of the flag on panfrost. |
| 259 |
*/ |
| 260 |
if (flags & PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT) |
| 261 |
panfrost_flags |= PANFROST_BO_HEAP; |
| 262 |
|
| 263 |
if (!(flags & PAN_KMOD_BO_FLAG_EXECUTABLE)) |
| 264 |
panfrost_flags |= PANFROST_BO_NOEXEC; |
| 265 |
} |
| 266 |
|
| 267 |
if (flags & PAN_KMOD_BO_FLAG_WB_MMAP) { |
| 268 |
assert(!(flags & PAN_KMOD_BO_FLAG_NO_MMAP)); |
| 269 |
panfrost_flags |= PANFROST_BO_WB_MMAP; |
| 270 |
} |
| 271 |
|
| 272 |
return panfrost_flags; |
| 273 |
} |
| 274 |
|
| 275 |
static struct pan_kmod_bo * |
| 276 |
panfrost_kmod_bo_alloc(struct pan_kmod_dev *dev, |
| 277 |
struct pan_kmod_vm *exclusive_vm, uint64_t size, |
| 278 |
uint32_t flags) |
| 279 |
{ |
| 280 |
/* The ioctl uses u32 for size. */ |
| 281 |
if ((uint64_t)(uint32_t)size != size) |
| 282 |
return NULL; |
| 283 |
|
| 284 |
/* We can't map GPU uncached. */ |
| 285 |
if (flags & PAN_KMOD_BO_FLAG_GPU_UNCACHED) |
| 286 |
return NULL; |
| 287 |
|
| 288 |
struct panfrost_kmod_bo *bo = pan_kmod_dev_alloc(dev, sizeof(*bo)); |
| 289 |
if (!bo) |
| 290 |
return NULL; |
| 291 |
|
| 292 |
struct drm_panfrost_create_bo req = { |
| 293 |
.size = size, |
| 294 |
.flags = to_panfrost_bo_flags(dev, flags), |
| 295 |
}; |
| 296 |
|
| 297 |
int ret = pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANFROST_CREATE_BO, &req); |
| 298 |
if (ret) { |
| 299 |
mesa_loge("DRM_IOCTL_PANFROST_CREATE_BO failed (err=%d)", errno); |
| 300 |
goto err_free_bo; |
| 301 |
} |
| 302 |
|
| 303 |
pan_kmod_bo_init(&bo->base, dev, exclusive_vm, req.size, flags, req.handle); |
| 304 |
bo->offset = req.offset; |
| 305 |
return &bo->base; |
| 306 |
|
| 307 |
err_free_bo: |
| 308 |
pan_kmod_dev_free(dev, bo); |
| 309 |
return NULL; |
| 310 |
} |
| 311 |
|
| 312 |
static void |
| 313 |
panfrost_kmod_bo_free(struct pan_kmod_bo *bo) |
| 314 |
{ |
| 315 |
pan_kmod_bo_cleanup(bo); |
| 316 |
drmCloseBufferHandle(bo->dev->fd, bo->handle); |
| 317 |
pan_kmod_dev_free(bo->dev, bo); |
| 318 |
} |
| 319 |
|
| 320 |
static struct pan_kmod_bo * |
| 321 |
panfrost_kmod_bo_import(struct pan_kmod_dev *dev, uint32_t handle, |
| 322 |
uint64_t size) |
| 323 |
{ |
| 324 |
struct panfrost_kmod_bo *panfrost_bo = |
| 325 |
pan_kmod_dev_alloc(dev, sizeof(*panfrost_bo)); |
| 326 |
if (!panfrost_bo) { |
| 327 |
mesa_loge("failed to allocate a panfrost_kmod_bo object"); |
| 328 |
return NULL; |
| 329 |
} |
| 330 |
|
| 331 |
struct drm_panfrost_get_bo_offset get_bo_offset = {.handle = handle, 0}; |
| 332 |
int ret = |
| 333 |
pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, |
| 334 |
&get_bo_offset); |
| 335 |
if (ret) { |
| 336 |
mesa_loge("DRM_IOCTL_PANFROST_GET_BO_OFFSET failed (err=%d)", errno); |
| 337 |
goto err_free_bo; |
| 338 |
} |
| 339 |
|
| 340 |
panfrost_bo->offset = get_bo_offset.offset; |
| 341 |
|
| 342 |
uint32_t flags = PAN_KMOD_BO_FLAG_IMPORTED; |
| 343 |
if (pan_kmod_driver_version_at_least(&dev->driver, 1, 6)) { |
| 344 |
struct drm_panfrost_query_bo_info args = { |
| 345 |
.handle = handle, |
| 346 |
}; |
| 347 |
|
| 348 |
ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_QUERY_BO_INFO, &args); |
| 349 |
if (ret) { |
| 350 |
mesa_loge("PANFROST_BO_QUERY_INFO failed (err=%d)", errno); |
| 351 |
goto err_free_bo; |
| 352 |
} |
| 353 |
|
| 354 |
/* FIXME: If the BO comes from a different subsystem |
| 355 |
* (args.extra_flags & DRM_PANTHOR_BO_IS_IMPORTED), we should normally |
| 356 |
* add extra DMA_BUF_IOCTL_SYNC calls around CPU accesses to ensure the |
| 357 |
* CPU mapping consistency, but this is something we never worried about |
| 358 |
* (we've always assumed exporters were exposing uncached mappings with |
| 359 |
* NOP {begin,end}_cpu_access() implementations), and it worked fine until |
| 360 |
* now. |
| 361 |
* The long term plan is to hook up DMA_BUF_IOCTL_SYNC, but this requires |
| 362 |
* more work. |
| 363 |
*/ |
| 364 |
} |
| 365 |
|
| 366 |
pan_kmod_bo_init(&panfrost_bo->base, dev, NULL, size, flags, handle); |
| 367 |
return &panfrost_bo->base; |
| 368 |
|
| 369 |
err_free_bo: |
| 370 |
pan_kmod_dev_free(dev, panfrost_bo); |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
|
| 374 |
static off_t |
| 375 |
panfrost_kmod_bo_get_mmap_offset(struct pan_kmod_bo *bo) |
| 376 |
{ |
| 377 |
struct drm_panfrost_mmap_bo mmap_bo = {.handle = bo->handle}; |
| 378 |
int ret = pan_kmod_ioctl(bo->dev->fd, DRM_IOCTL_PANFROST_MMAP_BO, |
| 379 |
&mmap_bo); |
| 380 |
if (ret) { |
| 381 |
fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %m\n"); |
| 382 |
assert(0); |
| 383 |
} |
| 384 |
|
| 385 |
return mmap_bo.offset; |
| 386 |
} |
| 387 |
|
| 388 |
static bool |
| 389 |
panfrost_kmod_bo_wait(struct pan_kmod_bo *bo, int64_t timeout_ns, |
| 390 |
bool for_read_only_access) |
| 391 |
{ |
| 392 |
struct drm_panfrost_wait_bo req = { |
| 393 |
.handle = bo->handle, |
| 394 |
.timeout_ns = timeout_ns, |
| 395 |
}; |
| 396 |
|
| 397 |
/* The ioctl returns >= 0 value when the BO we are waiting for is ready |
| 398 |
* -1 otherwise. |
| 399 |
*/ |
| 400 |
if (pan_kmod_ioctl(bo->dev->fd, DRM_IOCTL_PANFROST_WAIT_BO, &req) != -1) |
| 401 |
return true; |
| 402 |
|
| 403 |
assert(errno == ETIMEDOUT || errno == EBUSY); |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
static int |
| 408 |
panfrost_kmod_flush_bo_map_syncs(struct pan_kmod_dev *dev) |
| 409 |
{ |
| 410 |
STACK_ARRAY(struct drm_panfrost_bo_sync_op, panfrost_ops, |
| 411 |
util_dynarray_num_elements(&dev->pending_bo_syncs.array, |
| 412 |
struct pan_kmod_deferred_bo_sync)); |
| 413 |
|
| 414 |
uint32_t panfrost_count = 0; |
| 415 |
util_dynarray_foreach(&dev->pending_bo_syncs.array, |
| 416 |
struct pan_kmod_deferred_bo_sync, sync) { |
| 417 |
panfrost_ops[panfrost_count++] = (struct drm_panfrost_bo_sync_op){ |
| 418 |
.handle = sync->bo->handle, |
| 419 |
.type = sync->type == PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH |
| 420 |
? PANFROST_BO_SYNC_CPU_CACHE_FLUSH |
| 421 |
: PANFROST_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE, |
| 422 |
.offset = sync->start, |
| 423 |
.size = sync->size, |
| 424 |
}; |
| 425 |
} |
| 426 |
|
| 427 |
struct drm_panfrost_sync_bo req = { |
| 428 |
.ops = (uintptr_t)panfrost_ops, |
| 429 |
.op_count = panfrost_count, |
| 430 |
}; |
| 431 |
int ret = pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANFROST_SYNC_BO, &req); |
| 432 |
if (ret) |
| 433 |
mesa_loge("DRM_IOCTL_PANFROST_BO_SYNC failed (err=%d)", errno); |
| 434 |
|
| 435 |
STACK_ARRAY_FINISH(panfrost_ops); |
| 436 |
|
| 437 |
return ret; |
| 438 |
} |
| 439 |
|
| 440 |
static void |
| 441 |
panfrost_kmod_bo_make_evictable(struct pan_kmod_bo *bo) |
| 442 |
{ |
| 443 |
struct drm_panfrost_madvise req = { |
| 444 |
.handle = bo->handle, |
| 445 |
.madv = PANFROST_MADV_DONTNEED, |
| 446 |
}; |
| 447 |
|
| 448 |
pan_kmod_ioctl(bo->dev->fd, DRM_IOCTL_PANFROST_MADVISE, &req); |
| 449 |
} |
| 450 |
|
| 451 |
static bool |
| 452 |
panfrost_kmod_bo_make_unevictable(struct pan_kmod_bo *bo) |
| 453 |
{ |
| 454 |
struct drm_panfrost_madvise req = { |
| 455 |
.handle = bo->handle, |
| 456 |
.madv = PANFROST_MADV_WILLNEED, |
| 457 |
}; |
| 458 |
|
| 459 |
if (pan_kmod_ioctl(bo->dev->fd, DRM_IOCTL_PANFROST_MADVISE, &req) == 0 && |
| 460 |
req.retained == 0) |
| 461 |
return false; |
| 462 |
|
| 463 |
return true; |
| 464 |
} |
| 465 |
|
| 466 |
/* The VA range is restricted by the kernel driver. Lower 32MB are reserved, and |
| 467 |
* the address space is limited to 32-bit. |
| 468 |
*/ |
| 469 |
#define PANFROST_KMOD_VA_START 0x2000000ull |
| 470 |
#define PANFROST_KMOD_VA_END (1ull << 32) |
| 471 |
|
| 472 |
static struct pan_kmod_va_range |
| 473 |
panfrost_kmod_dev_query_user_va_range(const struct pan_kmod_dev *dev) |
| 474 |
{ |
| 475 |
return (struct pan_kmod_va_range){ |
| 476 |
.start = PANFROST_KMOD_VA_START, |
| 477 |
.size = PANFROST_KMOD_VA_END - PANFROST_KMOD_VA_START, |
| 478 |
}; |
| 479 |
} |
| 480 |
|
| 481 |
static struct pan_kmod_vm * |
| 482 |
panfrost_kmod_vm_create(struct pan_kmod_dev *dev, uint32_t flags, |
| 483 |
uint64_t va_start, uint64_t va_range) |
| 484 |
{ |
| 485 |
struct panfrost_kmod_dev *panfrost_dev = |
| 486 |
container_of(dev, struct panfrost_kmod_dev, base); |
| 487 |
|
| 488 |
/* Only one VM per device. */ |
| 489 |
if (panfrost_dev->vm) { |
| 490 |
mesa_loge("panfrost_kmod only supports one VM per device"); |
| 491 |
return NULL; |
| 492 |
} |
| 493 |
|
| 494 |
/* Panfrost kernel driver doesn't support userspace VA management. */ |
| 495 |
if (!(flags & PAN_KMOD_VM_FLAG_AUTO_VA)) { |
| 496 |
mesa_loge("panfrost_kmod only supports PAN_KMOD_VM_FLAG_AUTO_VA"); |
| 497 |
assert(0); |
| 498 |
return NULL; |
| 499 |
} |
| 500 |
|
| 501 |
struct panfrost_kmod_vm *vm = pan_kmod_dev_alloc(dev, sizeof(*vm)); |
| 502 |
if (!vm) { |
| 503 |
mesa_loge("failed to allocate a panfrost_kmod_vm object"); |
| 504 |
return NULL; |
| 505 |
} |
| 506 |
|
| 507 |
pan_kmod_vm_init(&vm->base, dev, 0, flags, PAN_PGSIZE_4K); |
| 508 |
panfrost_dev->vm = vm; |
| 509 |
return &vm->base; |
| 510 |
} |
| 511 |
|
| 512 |
static void |
| 513 |
panfrost_kmod_vm_destroy(struct pan_kmod_vm *vm) |
| 514 |
{ |
| 515 |
struct panfrost_kmod_dev *panfrost_dev = |
| 516 |
container_of(vm->dev, struct panfrost_kmod_dev, base); |
| 517 |
|
| 518 |
panfrost_dev->vm = NULL; |
| 519 |
pan_kmod_dev_free(vm->dev, vm); |
| 520 |
} |
| 521 |
|
| 522 |
static int |
| 523 |
panfrost_kmod_vm_bind(struct pan_kmod_vm *vm, enum pan_kmod_vm_op_mode mode, |
| 524 |
struct pan_kmod_vm_op *ops, uint32_t op_count) |
| 525 |
{ |
| 526 |
UNUSED struct panfrost_kmod_vm *panfrost_vm = |
| 527 |
container_of(vm, struct panfrost_kmod_vm, base); |
| 528 |
|
| 529 |
/* We only support IMMEDIATE and WAIT_IDLE mode. Actually we always do |
| 530 |
* WAIT_IDLE in practice, but it shouldn't matter. |
| 531 |
*/ |
| 532 |
if (mode != PAN_KMOD_VM_OP_MODE_IMMEDIATE && |
| 533 |
mode != PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT) { |
| 534 |
mesa_loge("panfrost_kmod doesn't support mode=%d", mode); |
| 535 |
assert(0); |
| 536 |
return -1; |
| 537 |
} |
| 538 |
|
| 539 |
for (uint32_t i = 0; i < op_count; i++) { |
| 540 |
|
| 541 |
if (ops[i].type == PAN_KMOD_VM_OP_TYPE_MAP) { |
| 542 |
struct panfrost_kmod_bo *panfrost_bo = |
| 543 |
container_of(ops[i].map.bo, struct panfrost_kmod_bo, base); |
| 544 |
|
| 545 |
/* Panfrost kernel driver doesn't support userspace VA management. */ |
| 546 |
if (ops[i].va.start != PAN_KMOD_VM_MAP_AUTO_VA) { |
| 547 |
mesa_loge("panfrost_kmod can only do auto-VA allocation"); |
| 548 |
assert(0); |
| 549 |
return -1; |
| 550 |
} |
| 551 |
|
| 552 |
/* Panfrost kernel driver only support full BO mapping. */ |
| 553 |
if (ops[i].map.bo_offset != 0 || |
| 554 |
ops[i].va.size != ops[i].map.bo->size) { |
| 555 |
mesa_loge("panfrost_kmod doesn't support partial BO mapping"); |
| 556 |
assert(0); |
| 557 |
return -1; |
| 558 |
} |
| 559 |
|
| 560 |
ops[i].va.start = panfrost_bo->offset; |
| 561 |
} else if (ops[i].type == PAN_KMOD_VM_OP_TYPE_UNMAP) { |
| 562 |
/* Do nothing, unmapping is done at BO destruction time. */ |
| 563 |
} else { |
| 564 |
/* We reject PAN_KMOD_VM_OP_TYPE_SYNC_ONLY as this implies |
| 565 |
* supporting PAN_KMOD_VM_OP_MODE_ASYNC, which we don't support. |
| 566 |
*/ |
| 567 |
mesa_loge("panfrost_kmod doesn't support op=%d", ops[i].type); |
| 568 |
assert(0); |
| 569 |
return -1; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
static uint64_t |
| 577 |
panfrost_kmod_query_timestamp(const struct pan_kmod_dev *dev) |
| 578 |
{ |
| 579 |
return panfrost_query_raw(dev->fd, DRM_PANFROST_PARAM_SYSTEM_TIMESTAMP, |
| 580 |
false, 0); |
| 581 |
} |
| 582 |
|
| 583 |
static void |
| 584 |
panfrost_kmod_bo_label(struct pan_kmod_dev *dev, struct pan_kmod_bo *bo, const char *label) |
| 585 |
{ |
| 586 |
char truncated_label[PANFROST_BO_LABEL_MAXLEN]; |
| 587 |
|
| 588 |
if (!pan_kmod_driver_version_at_least(&dev->driver, 1, 4)) |
| 589 |
return; |
| 590 |
|
| 591 |
if (strnlen(label, PANFROST_BO_LABEL_MAXLEN) == PANFROST_BO_LABEL_MAXLEN) { |
| 592 |
strncpy(truncated_label, label, PANFROST_BO_LABEL_MAXLEN - 1); |
| 593 |
truncated_label[PANFROST_BO_LABEL_MAXLEN - 1] = '\0'; |
| 594 |
label = truncated_label; |
| 595 |
} |
| 596 |
|
| 597 |
struct drm_panfrost_set_label_bo set_label = |
| 598 |
(struct drm_panfrost_set_label_bo) { |
| 599 |
.handle = bo->handle, |
| 600 |
.label = (uint64_t)(uintptr_t)label, |
| 601 |
}; |
| 602 |
|
| 603 |
int ret = |
| 604 |
pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANFROST_SET_LABEL_BO, |
| 605 |
&set_label); |
| 606 |
if (ret) |
| 607 |
mesa_loge("DRM_IOCTL_PANFROST_SET_LABEL_BO failed (err=%d)", errno); |
| 608 |
} |
| 609 |
|
| 610 |
const struct pan_kmod_ops panfrost_kmod_ops = { |
| 611 |
.dev_create = panfrost_kmod_dev_create, |
| 612 |
.dev_destroy = panfrost_kmod_dev_destroy, |
| 613 |
.dev_query_user_va_range = panfrost_kmod_dev_query_user_va_range, |
| 614 |
.bo_alloc = panfrost_kmod_bo_alloc, |
| 615 |
.bo_free = panfrost_kmod_bo_free, |
| 616 |
.bo_import = panfrost_kmod_bo_import, |
| 617 |
.bo_get_mmap_offset = panfrost_kmod_bo_get_mmap_offset, |
| 618 |
.bo_wait = panfrost_kmod_bo_wait, |
| 619 |
.flush_bo_map_syncs = panfrost_kmod_flush_bo_map_syncs, |
| 620 |
.bo_make_evictable = panfrost_kmod_bo_make_evictable, |
| 621 |
.bo_make_unevictable = panfrost_kmod_bo_make_unevictable, |
| 622 |
.vm_create = panfrost_kmod_vm_create, |
| 623 |
.vm_destroy = panfrost_kmod_vm_destroy, |
| 624 |
.vm_bind = panfrost_kmod_vm_bind, |
| 625 |
.query_timestamp = panfrost_kmod_query_timestamp, |
| 626 |
.bo_set_label = panfrost_kmod_bo_label, |
| 627 |
}; |