| 1 |
/* |
| 2 |
* Copyright © 2023 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* This file exposes some core KMD functionalities in a driver-agnostic way. |
| 8 |
* The drivers are still assumed to be regular DRM drivers, such that some |
| 9 |
* operations can be handled generically. |
| 10 |
* |
| 11 |
* Any operation that's too specific to be abstracted can either have a backend |
| 12 |
* specific helper exposed through pan_kmod_<backend>.h, or no helper at all |
| 13 |
* (in the latter case, users are expected to call the ioctl directly). |
| 14 |
* |
| 15 |
* If some operations are not natively supported by a KMD, the kmod backend |
| 16 |
* should fail or emulate the functionality (if deemed necessary). |
| 17 |
*/ |
| 18 |
|
| 19 |
#pragma once |
| 20 |
|
| 21 |
#include <fcntl.h> |
| 22 |
#include <inttypes.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <xf86drm.h> |
| 25 |
|
| 26 |
#include "drm-uapi/drm.h" |
| 27 |
|
| 28 |
#include "util/cache_ops.h" |
| 29 |
#include "util/log.h" |
| 30 |
#include "util/macros.h" |
| 31 |
#include "util/os_file.h" |
| 32 |
#include "util/os_mman.h" |
| 33 |
#include "util/ralloc.h" |
| 34 |
#include "util/simple_mtx.h" |
| 35 |
#include "util/sparse_array.h" |
| 36 |
#include "util/u_atomic.h" |
| 37 |
#include "util/u_dynarray.h" |
| 38 |
|
| 39 |
#include "kmod/panthor_kmod.h" |
| 40 |
#include "pan_trace.h" |
| 41 |
|
| 42 |
#if defined(__cplusplus) |
| 43 |
extern "C" { |
| 44 |
#endif |
| 45 |
|
| 46 |
struct pan_kmod_dev; |
| 47 |
|
| 48 |
/* GPU VM creation flags. */ |
| 49 |
enum pan_kmod_vm_flags { |
| 50 |
/* Set if you want the VM to automatically assign virtual addresses when |
| 51 |
* pan_kmod_vm_map(). If this flag is set, all pan_kmod_vm_map() calls |
| 52 |
* must have va=PAN_KMOD_VM_MAP_AUTO_VA. |
| 53 |
*/ |
| 54 |
PAN_KMOD_VM_FLAG_AUTO_VA = BITFIELD_BIT(0), |
| 55 |
|
| 56 |
/* Let the backend know whether it should track the VM activity or not. |
| 57 |
* Needed if PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT is used. |
| 58 |
*/ |
| 59 |
PAN_KMOD_VM_FLAG_TRACK_ACTIVITY = BITFIELD_BIT(1), |
| 60 |
}; |
| 61 |
|
| 62 |
#define PAN_PGSIZE_4K (uint64_t)0x1000 |
| 63 |
#define PAN_PGSIZE_2M (uint64_t)0x200000 |
| 64 |
|
| 65 |
/* Object representing a GPU VM. */ |
| 66 |
struct pan_kmod_vm { |
| 67 |
/* Page sizes supported by this VM. */ |
| 68 |
uint64_t pgsize_bitmap; |
| 69 |
|
| 70 |
/* Combination of pan_kmod_vm_flags flags. */ |
| 71 |
uint32_t flags; |
| 72 |
|
| 73 |
/* The VM handle returned by the KMD. If the KMD supports only one VM per |
| 74 |
* context, this should be zero. |
| 75 |
*/ |
| 76 |
uint32_t handle; |
| 77 |
|
| 78 |
/* Device this VM was created from. */ |
| 79 |
struct pan_kmod_dev *dev; |
| 80 |
}; |
| 81 |
|
| 82 |
/* Buffer object flags. */ |
| 83 |
enum pan_kmod_bo_flags { |
| 84 |
/* Allow GPU execution on this buffer. */ |
| 85 |
PAN_KMOD_BO_FLAG_EXECUTABLE = BITFIELD_BIT(0), |
| 86 |
|
| 87 |
/* Allocate memory when a GPU fault occurs instead of allocating |
| 88 |
* up-front. |
| 89 |
*/ |
| 90 |
PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT = BITFIELD_BIT(1), |
| 91 |
|
| 92 |
/* If set, the buffer object will never be CPU-mapped in userspace. */ |
| 93 |
PAN_KMOD_BO_FLAG_NO_MMAP = BITFIELD_BIT(2), |
| 94 |
|
| 95 |
/* Set when the buffer object has been exported. Users don't directly |
| 96 |
* control this flag, it's set when pan_kmod_bo_export() is called. |
| 97 |
*/ |
| 98 |
PAN_KMOD_BO_FLAG_EXPORTED = BITFIELD_BIT(3), |
| 99 |
|
| 100 |
/* Set when the buffer object has been impported. Users don't directly |
| 101 |
* control this flag, it's set when pan_kmod_bo_import() is called. |
| 102 |
*/ |
| 103 |
PAN_KMOD_BO_FLAG_IMPORTED = BITFIELD_BIT(4), |
| 104 |
|
| 105 |
/* If set, the buffer in mapped GPU-uncached when pan_kmod_vm_map() |
| 106 |
* is called. |
| 107 |
*/ |
| 108 |
PAN_KMOD_BO_FLAG_GPU_UNCACHED = BITFIELD_BIT(5), |
| 109 |
|
| 110 |
/* CPU map the buffer object in userspace by forcing the "Write-Back |
| 111 |
* Cacheable" cacheability attribute. The mapping otherwise uses the |
| 112 |
* "Non-Cacheable" attribute if the ACE-Lite coherency protocol isn't |
| 113 |
* supported by the GPU. |
| 114 |
*/ |
| 115 |
PAN_KMOD_BO_FLAG_WB_MMAP = BITFIELD_BIT(6), |
| 116 |
|
| 117 |
/* Set by default when the device is IO coherent. We might want to |
| 118 |
* make it optional at some point and pass a NON_COHERENT flag to |
| 119 |
* the KMD to force non-coherent mappings on IO coherent setup. |
| 120 |
*/ |
| 121 |
PAN_KMOD_BO_FLAG_IO_COHERENT = BITFIELD_BIT(7), |
| 122 |
}; |
| 123 |
|
| 124 |
/* Allowed group priority flags. */ |
| 125 |
enum pan_kmod_group_allow_priority_flags { |
| 126 |
/* Allow low priority group. */ |
| 127 |
PAN_KMOD_GROUP_ALLOW_PRIORITY_LOW = BITFIELD_BIT(0), |
| 128 |
|
| 129 |
/* Allow medium priority group. */ |
| 130 |
PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM = BITFIELD_BIT(1), |
| 131 |
|
| 132 |
/* Allow high priority group. */ |
| 133 |
PAN_KMOD_GROUP_ALLOW_PRIORITY_HIGH = BITFIELD_BIT(2), |
| 134 |
|
| 135 |
/* Allow realtime priority group. */ |
| 136 |
PAN_KMOD_GROUP_ALLOW_PRIORITY_REALTIME = BITFIELD_BIT(3), |
| 137 |
}; |
| 138 |
|
| 139 |
/* Buffer object. */ |
| 140 |
struct pan_kmod_bo { |
| 141 |
/* Atomic reference count. The only reason we need to refcnt BOs at this |
| 142 |
* level is because of how DRM prime import works: the import logic |
| 143 |
* returns the handle of an existing object if the object was previously |
| 144 |
* imported or was created by the driver. |
| 145 |
* In order to prevent call GEM_CLOSE on an object that's still supposed |
| 146 |
* to be active, we need count the number of users left. |
| 147 |
*/ |
| 148 |
int32_t refcnt; |
| 149 |
|
| 150 |
/* Size of the buffer object. */ |
| 151 |
uint64_t size; |
| 152 |
|
| 153 |
/* Handle attached to the buffer object. */ |
| 154 |
uint32_t handle; |
| 155 |
|
| 156 |
/* Combination of pan_kmod_bo_flags flags. */ |
| 157 |
uint32_t flags; |
| 158 |
|
| 159 |
/* True if some deferred syncs targetting this BO are pending. */ |
| 160 |
bool has_pending_deferred_syncs; |
| 161 |
|
| 162 |
/* If non-NULL, the buffer object can only by mapped on this VM. Typical |
| 163 |
* the case for all internal/non-shareable buffers. The backend can |
| 164 |
* optimize things based on this information. Calling pan_kmod_bo_export() |
| 165 |
* on such buffer objects is forbidden. |
| 166 |
*/ |
| 167 |
struct pan_kmod_vm *exclusive_vm; |
| 168 |
|
| 169 |
/* The device this buffer object was created from. */ |
| 170 |
struct pan_kmod_dev *dev; |
| 171 |
|
| 172 |
/* User private data. Use pan_kmod_bo_{set,get}_user_priv() to access it. */ |
| 173 |
void *user_priv; |
| 174 |
}; |
| 175 |
|
| 176 |
/* List of GPU properties needed by the UMD. */ |
| 177 |
struct pan_kmod_dev_props { |
| 178 |
/* GPU ID. */ |
| 179 |
uint64_t gpu_id; |
| 180 |
|
| 181 |
/* GPU variant. */ |
| 182 |
uint32_t gpu_variant; |
| 183 |
|
| 184 |
/* Bitmask encoding the number of shader cores exposed by the GPU. */ |
| 185 |
uint64_t shader_present; |
| 186 |
|
| 187 |
/* Tiler features bits. */ |
| 188 |
uint32_t tiler_features; |
| 189 |
|
| 190 |
/* Memory related feature bits. */ |
| 191 |
uint32_t mem_features; |
| 192 |
|
| 193 |
/* MMU feature bits. */ |
| 194 |
uint32_t mmu_features; |
| 195 |
#define MMU_FEATURES_VA_BITS(mmu_features) (mmu_features & 0xff) |
| 196 |
|
| 197 |
/* Texture feature bits. */ |
| 198 |
uint32_t texture_features[4]; |
| 199 |
|
| 200 |
/* Maximum number of threads per core. */ |
| 201 |
uint32_t max_threads_per_core; |
| 202 |
|
| 203 |
/* Maximum number of compute tasks per core. */ |
| 204 |
uint8_t max_tasks_per_core; |
| 205 |
|
| 206 |
/* Maximum number of threads per workgroup. */ |
| 207 |
uint32_t max_threads_per_wg; |
| 208 |
|
| 209 |
/* Number of registers per core. Can be used to determine the maximum |
| 210 |
* number of threads that can be allocated for a specific shader based on |
| 211 |
* the number of registers assigned to this shader. |
| 212 |
*/ |
| 213 |
uint32_t num_registers_per_core; |
| 214 |
|
| 215 |
/* Maximum number of thread-local storage instance per core. |
| 216 |
* If the GPU doesn't have a THREAD_TLS_ALLOC register or the register |
| 217 |
* value is zero, the backend should assign the value of max_threads_per_core |
| 218 |
* here. |
| 219 |
*/ |
| 220 |
uint32_t max_tls_instance_per_core; |
| 221 |
|
| 222 |
/* AFBC feature bits. */ |
| 223 |
uint32_t afbc_features; |
| 224 |
|
| 225 |
/* Support cycle count and timestamp propagation as job requirement */ |
| 226 |
bool gpu_can_query_timestamp; |
| 227 |
|
| 228 |
/* Cycle counter and timestamp device coherent propogation is enabled */ |
| 229 |
bool timestamp_device_coherent; |
| 230 |
|
| 231 |
/* GPU Timestamp frequency */ |
| 232 |
uint64_t timestamp_frequency; |
| 233 |
|
| 234 |
/* A mask of flags containing the allowed group priorities. */ |
| 235 |
enum pan_kmod_group_allow_priority_flags allowed_group_priorities_mask; |
| 236 |
|
| 237 |
/* Mask of BO flags supported by the KMD. */ |
| 238 |
uint32_t supported_bo_flags; |
| 239 |
|
| 240 |
/* GPU is IO coherent, meaning BOs can be created with WB_MMAP without |
| 241 |
* requiring explicit CPU cache maintenance. |
| 242 |
*/ |
| 243 |
bool is_io_coherent; |
| 244 |
}; |
| 245 |
|
| 246 |
/* Memory allocator for kmod internal allocations. */ |
| 247 |
struct pan_kmod_allocator { |
| 248 |
/* Allocate and set to zero. */ |
| 249 |
void *(*zalloc)(const struct pan_kmod_allocator *allocator, size_t size, |
| 250 |
bool transient); |
| 251 |
|
| 252 |
/* Free. */ |
| 253 |
void (*free)(const struct pan_kmod_allocator *allocator, void *data); |
| 254 |
|
| 255 |
/* Private data allocator data. Can be NULL if unused. */ |
| 256 |
void *priv; |
| 257 |
}; |
| 258 |
|
| 259 |
/* Synchronization type. */ |
| 260 |
enum pan_kmod_sync_type { |
| 261 |
PAN_KMOD_SYNC_TYPE_WAIT = 0, |
| 262 |
PAN_KMOD_SYNC_TYPE_SIGNAL, |
| 263 |
}; |
| 264 |
|
| 265 |
/* Synchronization operation. */ |
| 266 |
struct pan_kmod_sync_op { |
| 267 |
/* Type of operation. */ |
| 268 |
enum pan_kmod_sync_type type; |
| 269 |
|
| 270 |
/* Syncobj handle. */ |
| 271 |
uint32_t handle; |
| 272 |
|
| 273 |
/* Syncobj point. Zero for binary syncobjs. */ |
| 274 |
uint64_t point; |
| 275 |
}; |
| 276 |
|
| 277 |
/* Special value passed to pan_kmod_vm_map() to signify the VM it should |
| 278 |
* automatically allocate a VA. Only valid if the VM was created with |
| 279 |
* PAN_KMOD_VM_FLAG_AUTO_VA. |
| 280 |
*/ |
| 281 |
#define PAN_KMOD_VM_MAP_AUTO_VA ~0ull |
| 282 |
|
| 283 |
/* Special value return when the vm_map() operation failed. */ |
| 284 |
#define PAN_KMOD_VM_MAP_FAILED ~0ull |
| 285 |
|
| 286 |
/* VM operations can be executed in different modes. */ |
| 287 |
enum pan_kmod_vm_op_mode { |
| 288 |
/* The map/unmap operation is executed immediately, which might cause |
| 289 |
* GPU faults if the GPU was still accessing buffers when we unmap or |
| 290 |
* remap. |
| 291 |
*/ |
| 292 |
PAN_KMOD_VM_OP_MODE_IMMEDIATE, |
| 293 |
|
| 294 |
/* The map/unmap operation is executed asynchronously, and the user |
| 295 |
* provides explicit wait/signal sync operations. |
| 296 |
*/ |
| 297 |
PAN_KMOD_VM_OP_MODE_ASYNC, |
| 298 |
|
| 299 |
/* The map/unmap operation is executed when the next GPU/VM idle-point |
| 300 |
* is reached. This guarantees fault-free unmap/remap operations when the |
| 301 |
* kmod user doesn't want to deal with synchronizations explicitly. |
| 302 |
*/ |
| 303 |
PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT, |
| 304 |
}; |
| 305 |
|
| 306 |
/* VM operation type. */ |
| 307 |
enum pan_kmod_vm_op_type { |
| 308 |
/* Map a buffer object. */ |
| 309 |
PAN_KMOD_VM_OP_TYPE_MAP, |
| 310 |
|
| 311 |
/* Unmap a VA range. */ |
| 312 |
PAN_KMOD_VM_OP_TYPE_UNMAP, |
| 313 |
|
| 314 |
/* Do nothing. Used as a way to execute sync operations on a VM queue, |
| 315 |
* without touching the VM. |
| 316 |
*/ |
| 317 |
PAN_KMOD_VM_OP_TYPE_SYNC_ONLY, |
| 318 |
}; |
| 319 |
|
| 320 |
/* VM operation data. */ |
| 321 |
struct pan_kmod_vm_op { |
| 322 |
/* The type of operation being requested. */ |
| 323 |
enum pan_kmod_vm_op_type type; |
| 324 |
|
| 325 |
/* VA range. */ |
| 326 |
struct { |
| 327 |
/* Start of the VA range. |
| 328 |
* Must be PAN_KMOD_VM_MAP_AUTO_VA if PAN_KMOD_VM_FLAG_AUTO_VA was set |
| 329 |
* at VM creation time. In that case, the allocated VA is returned |
| 330 |
* in this field. |
| 331 |
*/ |
| 332 |
uint64_t start; |
| 333 |
|
| 334 |
/* Size of the VA range */ |
| 335 |
uint64_t size; |
| 336 |
} va; |
| 337 |
|
| 338 |
union { |
| 339 |
/* Arguments specific to map operations. */ |
| 340 |
struct { |
| 341 |
/* Buffer object to map. */ |
| 342 |
struct pan_kmod_bo *bo; |
| 343 |
|
| 344 |
/* Offset in the buffer object. */ |
| 345 |
off_t bo_offset; |
| 346 |
} map; |
| 347 |
}; |
| 348 |
|
| 349 |
/* Synchronization operations attached to the VM operation. */ |
| 350 |
struct { |
| 351 |
/* Number of synchronization operations. Must be zero if mode is |
| 352 |
* PAN_KMOD_VM_OP_MODE_IMMEDIATE or PAN_KMOD_VM_OP_MODE_WAIT_IDLE. |
| 353 |
*/ |
| 354 |
uint32_t count; |
| 355 |
|
| 356 |
/* Array of synchronization operation descriptors. NULL if count is zero. */ |
| 357 |
const struct pan_kmod_sync_op *array; |
| 358 |
} syncs; |
| 359 |
}; |
| 360 |
|
| 361 |
/* VM state. */ |
| 362 |
enum pan_kmod_vm_state { |
| 363 |
PAN_KMOD_VM_USABLE, |
| 364 |
PAN_KMOD_VM_FAULTY, |
| 365 |
}; |
| 366 |
|
| 367 |
/* Device flags. */ |
| 368 |
enum pan_kmod_dev_flags { |
| 369 |
/* Set when the fd passed to pan_kmod_create() is expected to be |
| 370 |
* owned by the device, iff the device creation succeeded. |
| 371 |
*/ |
| 372 |
PAN_KMOD_DEV_FLAG_OWNS_FD = (1 << 0), |
| 373 |
|
| 374 |
/* Force BO syncs through the kernel. */ |
| 375 |
PAN_KMOD_DEV_FLAG_MMAP_SYNC_THROUGH_KERNEL = (1 << 1), |
| 376 |
}; |
| 377 |
|
| 378 |
/* Encode a virtual address range. */ |
| 379 |
struct pan_kmod_va_range { |
| 380 |
/* Start of the VA range. */ |
| 381 |
uint64_t start; |
| 382 |
|
| 383 |
/* Size of the VA range. */ |
| 384 |
uint64_t size; |
| 385 |
}; |
| 386 |
|
| 387 |
/* KMD backend vtable. |
| 388 |
* |
| 389 |
* All methods described there are mandatory, unless explicitly flagged as |
| 390 |
* optional. |
| 391 |
*/ |
| 392 |
struct pan_kmod_ops { |
| 393 |
/* Create a pan_kmod_dev object. |
| 394 |
* Return NULL if the creation fails for any reason. |
| 395 |
*/ |
| 396 |
struct pan_kmod_dev *(*dev_create)( |
| 397 |
int fd, uint32_t flags, const drmVersionPtr version, |
| 398 |
const struct pan_kmod_allocator *allocator); |
| 399 |
|
| 400 |
/* Destroy a pan_kmod_dev object. */ |
| 401 |
void (*dev_destroy)(struct pan_kmod_dev *dev); |
| 402 |
|
| 403 |
/* Query the maxium user VA range. |
| 404 |
* Users are free to use a subset of this range if they need less VA space. |
| 405 |
* This method is optional, when not specified, kmod assumes the whole VA |
| 406 |
* space (extracted from MMU_FEATURES.VA_BITS) is usable. |
| 407 |
*/ |
| 408 |
struct pan_kmod_va_range (*dev_query_user_va_range)( |
| 409 |
const struct pan_kmod_dev *dev); |
| 410 |
|
| 411 |
/* Allocate a buffer object. |
| 412 |
* Return NULL if the creation fails for any reason. |
| 413 |
*/ |
| 414 |
struct pan_kmod_bo *(*bo_alloc)(struct pan_kmod_dev *dev, |
| 415 |
struct pan_kmod_vm *exclusive_vm, |
| 416 |
uint64_t size, uint32_t flags); |
| 417 |
|
| 418 |
/* Free buffer object. */ |
| 419 |
void (*bo_free)(struct pan_kmod_bo *bo); |
| 420 |
|
| 421 |
/* Import a buffer object. |
| 422 |
* Return NULL if the import fails for any reason. |
| 423 |
*/ |
| 424 |
struct pan_kmod_bo *(*bo_import)(struct pan_kmod_dev *dev, uint32_t handle, |
| 425 |
uint64_t size); |
| 426 |
|
| 427 |
/* Post export operations. |
| 428 |
* Return 0 on success, -1 otherwise. |
| 429 |
* This method is optional. |
| 430 |
*/ |
| 431 |
int (*bo_export)(struct pan_kmod_bo *bo, int dmabuf_fd); |
| 432 |
|
| 433 |
/* Get the file offset to use to mmap() a buffer object. */ |
| 434 |
off_t (*bo_get_mmap_offset)(struct pan_kmod_bo *bo); |
| 435 |
|
| 436 |
/* Flush the pending BO map syncs. */ |
| 437 |
int (*flush_bo_map_syncs)(struct pan_kmod_dev *dev); |
| 438 |
|
| 439 |
/* Wait for a buffer object to be ready for read or read/write accesses. */ |
| 440 |
bool (*bo_wait)(struct pan_kmod_bo *bo, int64_t timeout_ns, |
| 441 |
bool for_read_only_access); |
| 442 |
|
| 443 |
/* Make a buffer object evictable. This method is optional. */ |
| 444 |
void (*bo_make_evictable)(struct pan_kmod_bo *bo); |
| 445 |
|
| 446 |
/* Make the buffer object unevictable. This method is optional. */ |
| 447 |
bool (*bo_make_unevictable)(struct pan_kmod_bo *bo); |
| 448 |
|
| 449 |
/* Create a VM object. */ |
| 450 |
struct pan_kmod_vm *(*vm_create)(struct pan_kmod_dev *dev, uint32_t flags, |
| 451 |
uint64_t va_start, uint64_t va_range); |
| 452 |
|
| 453 |
/* Destroy a VM object. */ |
| 454 |
void (*vm_destroy)(struct pan_kmod_vm *vm); |
| 455 |
|
| 456 |
/* Execute VM operations. |
| 457 |
* Return 0 if the submission suceeds, -1 otherwise. |
| 458 |
* For PAN_KMOD_VM_OP_MODE_IMMEDIATE submissions, the return value also |
| 459 |
* reflects the successfulness of the VM operation, for other modes, |
| 460 |
* if any of the VM operation fails, the VM might be flagged as unusable |
| 461 |
* and users should create a new VM to recover. |
| 462 |
*/ |
| 463 |
int (*vm_bind)(struct pan_kmod_vm *vm, enum pan_kmod_vm_op_mode mode, |
| 464 |
struct pan_kmod_vm_op *ops, uint32_t op_count); |
| 465 |
|
| 466 |
/* Query the VM state. |
| 467 |
* This method is optional. When missing the VM is assumed to always be |
| 468 |
* usable. |
| 469 |
*/ |
| 470 |
enum pan_kmod_vm_state (*vm_query_state)(struct pan_kmod_vm *vm); |
| 471 |
|
| 472 |
/* Query the current GPU timestamp */ |
| 473 |
uint64_t (*query_timestamp)(const struct pan_kmod_dev *dev); |
| 474 |
|
| 475 |
/* Label the BO */ |
| 476 |
void (*bo_set_label)(struct pan_kmod_dev *dev, struct pan_kmod_bo *bo, const char *label); |
| 477 |
}; |
| 478 |
|
| 479 |
/* KMD information. */ |
| 480 |
struct pan_kmod_driver { |
| 481 |
/* KMD version. */ |
| 482 |
struct { |
| 483 |
uint32_t major; |
| 484 |
uint32_t minor; |
| 485 |
} version; |
| 486 |
}; |
| 487 |
|
| 488 |
static inline bool |
| 489 |
pan_kmod_driver_version_at_least(const struct pan_kmod_driver *driver, |
| 490 |
uint32_t major, uint32_t minor) |
| 491 |
{ |
| 492 |
if (driver->version.major < major) |
| 493 |
return false; |
| 494 |
|
| 495 |
if (driver->version.major > major) |
| 496 |
return true; |
| 497 |
|
| 498 |
return driver->version.minor >= minor; |
| 499 |
} |
| 500 |
|
| 501 |
enum pan_kmod_bo_sync_type { |
| 502 |
PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH, |
| 503 |
PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE, |
| 504 |
}; |
| 505 |
|
| 506 |
/* Used to queue BO sync operations. */ |
| 507 |
struct pan_kmod_deferred_bo_sync { |
| 508 |
struct pan_kmod_bo *bo; |
| 509 |
uint64_t start; |
| 510 |
uint64_t size; |
| 511 |
enum pan_kmod_bo_sync_type type; |
| 512 |
}; |
| 513 |
|
| 514 |
/* Device object. */ |
| 515 |
struct pan_kmod_dev { |
| 516 |
/* FD attached to the device. */ |
| 517 |
int fd; |
| 518 |
|
| 519 |
/* Device flags. */ |
| 520 |
uint32_t flags; |
| 521 |
|
| 522 |
/* KMD backing this device. */ |
| 523 |
struct pan_kmod_driver driver; |
| 524 |
|
| 525 |
/* KMD-agnostic device properties. */ |
| 526 |
struct pan_kmod_dev_props props; |
| 527 |
|
| 528 |
/* kmod backend ops assigned at device creation. */ |
| 529 |
const struct pan_kmod_ops *ops; |
| 530 |
|
| 531 |
/* DRM prime import returns the handle of a pre-existing GEM if we are |
| 532 |
* importing an object that was created by us or previously imported. |
| 533 |
* We need to make sure we return the same pan_kmod_bo in that case, |
| 534 |
* otherwise freeing one pan_kmod_bo will make all other BOs sharing |
| 535 |
* the same handle invalid. |
| 536 |
*/ |
| 537 |
struct { |
| 538 |
struct util_sparse_array array; |
| 539 |
simple_mtx_t lock; |
| 540 |
} handle_to_bo; |
| 541 |
|
| 542 |
/* Pending BO syncs. */ |
| 543 |
struct { |
| 544 |
bool user_cache_ops_pending; |
| 545 |
struct util_dynarray array; |
| 546 |
simple_mtx_t lock; |
| 547 |
} pending_bo_syncs; |
| 548 |
|
| 549 |
/* Allocator attached to the device. */ |
| 550 |
const struct pan_kmod_allocator *allocator; |
| 551 |
|
| 552 |
/* User private data. Use pan_kmod_dev_{set,get}_user_priv() to access it. */ |
| 553 |
void *user_priv; |
| 554 |
}; |
| 555 |
|
| 556 |
#define pan_kmod_ioctl(fd, op, arg) \ |
| 557 |
({ \ |
| 558 |
PAN_TRACE_SCOPE(PAN_TRACE_LIB_KMOD, "pan_kmod_ioctl op=" #op); \ |
| 559 |
drmIoctl(fd, op, arg); \ |
| 560 |
}) |
| 561 |
|
| 562 |
struct pan_kmod_dev * |
| 563 |
pan_kmod_dev_create(int fd, uint32_t flags, |
| 564 |
const struct pan_kmod_allocator *allocator); |
| 565 |
|
| 566 |
void pan_kmod_dev_destroy(struct pan_kmod_dev *dev); |
| 567 |
|
| 568 |
static inline struct pan_kmod_va_range |
| 569 |
pan_kmod_dev_query_user_va_range(const struct pan_kmod_dev *dev) |
| 570 |
{ |
| 571 |
if (dev->ops->dev_query_user_va_range) |
| 572 |
return dev->ops->dev_query_user_va_range(dev); |
| 573 |
|
| 574 |
return (struct pan_kmod_va_range){ |
| 575 |
.start = 0, |
| 576 |
.size = 1ull << MMU_FEATURES_VA_BITS(dev->props.mmu_features), |
| 577 |
}; |
| 578 |
} |
| 579 |
|
| 580 |
static inline void |
| 581 |
pan_kmod_dev_set_user_priv(struct pan_kmod_dev *dev, void *data) |
| 582 |
{ |
| 583 |
dev->user_priv = data; |
| 584 |
} |
| 585 |
|
| 586 |
static inline void * |
| 587 |
pan_kmod_dev_get_user_priv(struct pan_kmod_dev *dev) |
| 588 |
{ |
| 589 |
return dev->user_priv; |
| 590 |
} |
| 591 |
|
| 592 |
struct pan_kmod_bo *pan_kmod_bo_alloc(struct pan_kmod_dev *dev, |
| 593 |
struct pan_kmod_vm *exclusive_vm, |
| 594 |
uint64_t size, uint32_t flags); |
| 595 |
|
| 596 |
static inline struct pan_kmod_bo * |
| 597 |
pan_kmod_bo_get(struct pan_kmod_bo *bo) |
| 598 |
{ |
| 599 |
if (!bo) |
| 600 |
return NULL; |
| 601 |
|
| 602 |
ASSERTED int32_t refcnt = p_atomic_inc_return(&bo->refcnt); |
| 603 |
|
| 604 |
/* If refcnt was zero before our increment, we're in trouble. */ |
| 605 |
assert(refcnt > 1); |
| 606 |
|
| 607 |
return bo; |
| 608 |
} |
| 609 |
|
| 610 |
void pan_kmod_bo_put(struct pan_kmod_bo *bo); |
| 611 |
|
| 612 |
static inline void * |
| 613 |
pan_kmod_bo_cmdxchg_user_priv(struct pan_kmod_bo *bo, void *old_data, |
| 614 |
void *new_data) |
| 615 |
{ |
| 616 |
return (void *)p_atomic_cmpxchg((uintptr_t *)&bo->user_priv, |
| 617 |
(uintptr_t)old_data, (uintptr_t)new_data); |
| 618 |
} |
| 619 |
|
| 620 |
static inline void |
| 621 |
pan_kmod_bo_set_user_priv(struct pan_kmod_bo *bo, void *data) |
| 622 |
{ |
| 623 |
bo->user_priv = data; |
| 624 |
} |
| 625 |
|
| 626 |
static inline void * |
| 627 |
pan_kmod_bo_get_user_priv(const struct pan_kmod_bo *bo) |
| 628 |
{ |
| 629 |
return bo->user_priv; |
| 630 |
} |
| 631 |
|
| 632 |
struct pan_kmod_bo *pan_kmod_bo_import(struct pan_kmod_dev *dev, int fd); |
| 633 |
|
| 634 |
static inline int |
| 635 |
pan_kmod_bo_export(struct pan_kmod_bo *bo) |
| 636 |
{ |
| 637 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 638 |
|
| 639 |
int fd; |
| 640 |
|
| 641 |
if (drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR, |
| 642 |
&fd)) { |
| 643 |
mesa_loge("drmPrimeHandleToFD() failed (err=%d)", errno); |
| 644 |
return -1; |
| 645 |
} |
| 646 |
|
| 647 |
if (bo->dev->ops->bo_export && bo->dev->ops->bo_export(bo, fd)) { |
| 648 |
close(fd); |
| 649 |
return -1; |
| 650 |
} |
| 651 |
|
| 652 |
bo->flags |= PAN_KMOD_BO_FLAG_EXPORTED; |
| 653 |
return fd; |
| 654 |
} |
| 655 |
|
| 656 |
static inline bool |
| 657 |
pan_kmod_bo_wait(struct pan_kmod_bo *bo, int64_t timeout_ns, |
| 658 |
bool for_read_only_access) |
| 659 |
{ |
| 660 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 661 |
|
| 662 |
return bo->dev->ops->bo_wait(bo, timeout_ns, for_read_only_access); |
| 663 |
} |
| 664 |
|
| 665 |
static inline void |
| 666 |
pan_kmod_bo_make_evictable(struct pan_kmod_bo *bo) |
| 667 |
{ |
| 668 |
if (bo->dev->ops->bo_make_evictable) |
| 669 |
bo->dev->ops->bo_make_evictable(bo); |
| 670 |
} |
| 671 |
|
| 672 |
static inline bool |
| 673 |
pan_kmod_bo_make_unevictable(struct pan_kmod_bo *bo) |
| 674 |
{ |
| 675 |
if (bo->dev->ops->bo_make_unevictable) |
| 676 |
return bo->dev->ops->bo_make_unevictable(bo); |
| 677 |
|
| 678 |
return true; |
| 679 |
} |
| 680 |
|
| 681 |
static inline void * |
| 682 |
pan_kmod_bo_mmap(struct pan_kmod_bo *bo, int prot, int flags, void *host_addr) |
| 683 |
{ |
| 684 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 685 |
|
| 686 |
off_t mmap_offset; |
| 687 |
|
| 688 |
/* Don't bother trying an mmap() if it's not allowed. */ |
| 689 |
if (bo->flags & PAN_KMOD_BO_FLAG_NO_MMAP) |
| 690 |
return MAP_FAILED; |
| 691 |
|
| 692 |
mmap_offset = bo->dev->ops->bo_get_mmap_offset(bo); |
| 693 |
if (mmap_offset < 0) |
| 694 |
return MAP_FAILED; |
| 695 |
|
| 696 |
host_addr = |
| 697 |
os_mmap(host_addr, bo->size, prot, flags, bo->dev->fd, mmap_offset); |
| 698 |
if (host_addr == MAP_FAILED) |
| 699 |
mesa_loge("mmap(..., size=%" PRIu64 ", prot=%d, flags=0x%x) failed: %s", |
| 700 |
bo->size, prot, flags, strerror(errno)); |
| 701 |
|
| 702 |
return host_addr; |
| 703 |
} |
| 704 |
|
| 705 |
static inline bool |
| 706 |
pan_kmod_can_sync_bo_map_from_userland(struct pan_kmod_dev *dev) |
| 707 |
{ |
| 708 |
return util_has_cache_ops() && |
| 709 |
!(dev->flags & PAN_KMOD_DEV_FLAG_MMAP_SYNC_THROUGH_KERNEL); |
| 710 |
} |
| 711 |
|
| 712 |
void pan_kmod_queue_bo_map_sync(struct pan_kmod_bo *bo, uint64_t bo_offset, |
| 713 |
void *cpu_ptr, uint64_t range, |
| 714 |
enum pan_kmod_bo_sync_type type); |
| 715 |
|
| 716 |
void pan_kmod_flush_bo_map_syncs(struct pan_kmod_dev *dev); |
| 717 |
|
| 718 |
static inline void |
| 719 |
pan_kmod_set_bo_label(struct pan_kmod_dev *dev, struct pan_kmod_bo *bo, const char *label) |
| 720 |
{ |
| 721 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 722 |
|
| 723 |
if (dev->ops->bo_set_label) |
| 724 |
dev->ops->bo_set_label(dev, bo, label); |
| 725 |
} |
| 726 |
|
| 727 |
static inline uint64_t |
| 728 |
pan_kmod_bo_size(struct pan_kmod_bo *bo) |
| 729 |
{ |
| 730 |
return bo->size; |
| 731 |
} |
| 732 |
|
| 733 |
static inline uint32_t |
| 734 |
pan_kmod_bo_handle(struct pan_kmod_bo *bo) |
| 735 |
{ |
| 736 |
return bo->handle; |
| 737 |
} |
| 738 |
|
| 739 |
static inline struct pan_kmod_vm * |
| 740 |
pan_kmod_vm_create(struct pan_kmod_dev *dev, uint32_t flags, uint64_t va_start, |
| 741 |
uint64_t va_range) |
| 742 |
{ |
| 743 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 744 |
|
| 745 |
return dev->ops->vm_create(dev, flags, va_start, va_range); |
| 746 |
} |
| 747 |
|
| 748 |
static inline void |
| 749 |
pan_kmod_vm_destroy(struct pan_kmod_vm *vm) |
| 750 |
{ |
| 751 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 752 |
|
| 753 |
vm->dev->ops->vm_destroy(vm); |
| 754 |
} |
| 755 |
|
| 756 |
static inline int |
| 757 |
pan_kmod_vm_bind(struct pan_kmod_vm *vm, enum pan_kmod_vm_op_mode mode, |
| 758 |
struct pan_kmod_vm_op *ops, uint32_t op_count) |
| 759 |
{ |
| 760 |
PAN_TRACE_FUNC(PAN_TRACE_LIB_KMOD); |
| 761 |
|
| 762 |
return vm->dev->ops->vm_bind(vm, mode, ops, op_count); |
| 763 |
} |
| 764 |
|
| 765 |
static inline enum pan_kmod_vm_state |
| 766 |
pan_kmod_vm_query_state(struct pan_kmod_vm *vm) |
| 767 |
{ |
| 768 |
if (vm->dev->ops->vm_query_state) |
| 769 |
return vm->dev->ops->vm_query_state(vm); |
| 770 |
|
| 771 |
return PAN_KMOD_VM_USABLE; |
| 772 |
} |
| 773 |
|
| 774 |
static inline uint32_t |
| 775 |
pan_kmod_vm_handle(struct pan_kmod_vm *vm) |
| 776 |
{ |
| 777 |
return vm->handle; |
| 778 |
} |
| 779 |
|
| 780 |
static inline uint64_t |
| 781 |
pan_kmod_query_timestamp(const struct pan_kmod_dev *dev) |
| 782 |
{ |
| 783 |
return dev->ops->query_timestamp(dev); |
| 784 |
} |
| 785 |
|
| 786 |
#if defined(__cplusplus) |
| 787 |
} // extern "C" |
| 788 |
#endif |