| 1 |
/* |
| 2 |
* Copyright 2021 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include <stdint.h> |
| 9 |
#include <xf86drm.h> |
| 10 |
#include "drm-uapi/asahi_drm.h" |
| 11 |
#include "util/ralloc.h" |
| 12 |
#include "util/simple_mtx.h" |
| 13 |
#include "util/sparse_array.h" |
| 14 |
#include "util/timespec.h" |
| 15 |
#include "util/u_printf.h" |
| 16 |
#include "util/vma.h" |
| 17 |
#include "agx_bo.h" |
| 18 |
#include "agx_pack.h" |
| 19 |
#include "decode.h" |
| 20 |
#include "layout.h" |
| 21 |
#include "libagx_dgc.h" |
| 22 |
|
| 23 |
#include "vdrm.h" |
| 24 |
|
| 25 |
#include "asahi_proto.h" |
| 26 |
|
| 27 |
enum agx_dbg { |
| 28 |
AGX_DBG_TRACE = BITFIELD_BIT(0), |
| 29 |
AGX_DBG_BODUMP = BITFIELD_BIT(1), |
| 30 |
AGX_DBG_NO16 = BITFIELD_BIT(2), |
| 31 |
AGX_DBG_DIRTY = BITFIELD_BIT(3), |
| 32 |
AGX_DBG_PRECOMPILE = BITFIELD_BIT(4), |
| 33 |
AGX_DBG_PERF = BITFIELD_BIT(5), |
| 34 |
AGX_DBG_NOCOMPRESS = BITFIELD_BIT(6), |
| 35 |
AGX_DBG_NOCLUSTER = BITFIELD_BIT(7), |
| 36 |
AGX_DBG_SYNC = BITFIELD_BIT(8), |
| 37 |
AGX_DBG_STATS = BITFIELD_BIT(9), |
| 38 |
AGX_DBG_RESOURCE = BITFIELD_BIT(10), |
| 39 |
AGX_DBG_BATCH = BITFIELD_BIT(11), |
| 40 |
AGX_DBG_NOWC = BITFIELD_BIT(12), |
| 41 |
AGX_DBG_SYNCTVB = BITFIELD_BIT(13), |
| 42 |
AGX_DBG_SMALLTILE = BITFIELD_BIT(14), |
| 43 |
AGX_DBG_NOMSAA = BITFIELD_BIT(15), |
| 44 |
AGX_DBG_NOSHADOW = BITFIELD_BIT(16), |
| 45 |
AGX_DBG_BODUMPVERBOSE = BITFIELD_BIT(17), |
| 46 |
AGX_DBG_SCRATCH = BITFIELD_BIT(18), |
| 47 |
AGX_DBG_NOSOFT = BITFIELD_BIT(19), |
| 48 |
AGX_DBG_FEEDBACK = BITFIELD_BIT(20), |
| 49 |
AGX_DBG_1QUEUE = BITFIELD_BIT(21), |
| 50 |
AGX_DBG_NOMERGE = BITFIELD_BIT(22), |
| 51 |
}; |
| 52 |
|
| 53 |
/* How many power-of-two levels in the BO cache do we want? 2^14 minimum chosen |
| 54 |
* as it is the page size that all allocations are rounded to |
| 55 |
*/ |
| 56 |
#define MIN_BO_CACHE_BUCKET (14) /* 2^14 = 16KB */ |
| 57 |
#define MAX_BO_CACHE_BUCKET (22) /* 2^22 = 4MB */ |
| 58 |
|
| 59 |
/* Fencepost problem, hence the off-by-one */ |
| 60 |
#define NR_BO_CACHE_BUCKETS (MAX_BO_CACHE_BUCKET - MIN_BO_CACHE_BUCKET + 1) |
| 61 |
|
| 62 |
/* Forward decl only, do not pull in all of NIR */ |
| 63 |
struct nir_shader; |
| 64 |
|
| 65 |
#define BARRIER_RENDER (1 << DRM_ASAHI_SUBQUEUE_RENDER) |
| 66 |
#define BARRIER_COMPUTE (1 << DRM_ASAHI_SUBQUEUE_COMPUTE) |
| 67 |
|
| 68 |
struct agx_submit_virt { |
| 69 |
uint32_t extres_count; |
| 70 |
struct asahi_ccmd_submit_res *extres; |
| 71 |
uint32_t ring_idx; |
| 72 |
}; |
| 73 |
|
| 74 |
typedef struct { |
| 75 |
struct agx_bo *(*bo_alloc)(struct agx_device *dev, size_t size, size_t align, |
| 76 |
enum agx_bo_flags flags); |
| 77 |
int (*bo_bind)(struct agx_device *dev, struct drm_asahi_gem_bind_op *ops, |
| 78 |
uint32_t count); |
| 79 |
void (*bo_mmap)(struct agx_device *dev, struct agx_bo *bo, void *fixed_addr); |
| 80 |
ssize_t (*get_params)(struct agx_device *dev, void *buf, size_t size); |
| 81 |
int (*submit)(struct agx_device *dev, struct drm_asahi_submit *submit, |
| 82 |
struct agx_submit_virt *virt); |
| 83 |
int (*bo_bind_object)(struct agx_device *dev, |
| 84 |
struct drm_asahi_gem_bind_object *bind); |
| 85 |
int (*bo_unbind_object)(struct agx_device *dev, uint32_t object_handle); |
| 86 |
} agx_device_ops_t; |
| 87 |
|
| 88 |
int agx_bo_bind(struct agx_device *dev, struct agx_bo *bo, uint64_t addr, |
| 89 |
size_t size_B, uint64_t offset_B, uint32_t flags); |
| 90 |
|
| 91 |
int agx_bind_timestamps(struct agx_device *dev, struct agx_bo *bo, |
| 92 |
uint32_t *handle); |
| 93 |
|
| 94 |
struct agx_device { |
| 95 |
uint32_t debug; |
| 96 |
|
| 97 |
/* Precompiled libagx binary table */ |
| 98 |
const uint32_t **libagx_programs; |
| 99 |
|
| 100 |
char name[64]; |
| 101 |
struct drm_asahi_params_global params; |
| 102 |
bool is_virtio; |
| 103 |
agx_device_ops_t ops; |
| 104 |
|
| 105 |
/* vdrm device */ |
| 106 |
struct vdrm_device *vdrm; |
| 107 |
uint32_t next_blob_id; |
| 108 |
|
| 109 |
/* Device handle */ |
| 110 |
int fd; |
| 111 |
|
| 112 |
/* VM handle */ |
| 113 |
uint32_t vm_id; |
| 114 |
|
| 115 |
/* Global queue handle */ |
| 116 |
uint32_t queue_id; |
| 117 |
|
| 118 |
/* VMA heaps */ |
| 119 |
simple_mtx_t vma_lock; |
| 120 |
uint64_t shader_base; |
| 121 |
struct util_vma_heap main_heap; |
| 122 |
struct util_vma_heap usc_heap; |
| 123 |
uint64_t guard_size; |
| 124 |
|
| 125 |
/* To emulate sparse-resident buffers, we map buffers in both the bottom half |
| 126 |
* and top half of the address space. sparse_ro_offset controls the |
| 127 |
* partitioning. This is a power-of-two that &'s zero in bottom (read-write) |
| 128 |
* buffers but non-zero in top (read-only) shadow mappings. |
| 129 |
* |
| 130 |
* In other words, given an address X, we can check if it is in the top half |
| 131 |
* if (X & sparse_ro_offset) != 0. |
| 132 |
* |
| 133 |
* Given a bottom half address X, we can get the top half address |
| 134 |
* equivalently as (X + sparse_ro_offset) or (X | sparse_ro_offset). |
| 135 |
*/ |
| 136 |
uint64_t sparse_ro_offset; |
| 137 |
|
| 138 |
struct agx_bo *zero_bo, *scratch_bo; |
| 139 |
|
| 140 |
struct renderonly *ro; |
| 141 |
|
| 142 |
pthread_mutex_t bo_map_lock; |
| 143 |
struct util_sparse_array bo_map; |
| 144 |
uint32_t max_handle; |
| 145 |
|
| 146 |
struct { |
| 147 |
simple_mtx_t lock; |
| 148 |
|
| 149 |
/* List containing all cached BOs sorted in LRU (Least Recently Used) |
| 150 |
* order so we can quickly evict BOs that are more than 1 second old. |
| 151 |
*/ |
| 152 |
struct list_head lru; |
| 153 |
|
| 154 |
/* The BO cache is a set of buckets with power-of-two sizes. Each bucket |
| 155 |
* is a linked list of free panfrost_bo objects. |
| 156 |
*/ |
| 157 |
struct list_head buckets[NR_BO_CACHE_BUCKETS]; |
| 158 |
|
| 159 |
/* Current size of the BO cache in bytes (sum of sizes of cached BOs) */ |
| 160 |
size_t size; |
| 161 |
|
| 162 |
/* Number of hits/misses for the BO cache */ |
| 163 |
uint64_t hits, misses; |
| 164 |
} bo_cache; |
| 165 |
|
| 166 |
struct agxdecode_ctx *agxdecode; |
| 167 |
|
| 168 |
/* Prepacked USC Sampler word to bind the txf sampler, used for |
| 169 |
* precompiled shaders on both drivers. |
| 170 |
*/ |
| 171 |
struct agx_usc_sampler_packed txf_sampler; |
| 172 |
|
| 173 |
/* Simplified device selection */ |
| 174 |
enum agx_chip chip; |
| 175 |
|
| 176 |
struct { |
| 177 |
uint64_t num; |
| 178 |
uint64_t den; |
| 179 |
} user_timestamp_to_ns; |
| 180 |
|
| 181 |
struct u_printf_ctx printf; |
| 182 |
}; |
| 183 |
|
| 184 |
/* |
| 185 |
* Determine if an address is in the read-only section. See the documentation |
| 186 |
* for sparse_ro_offset. |
| 187 |
*/ |
| 188 |
static inline bool |
| 189 |
agx_addr_is_ro(struct agx_device *dev, uint64_t addr) |
| 190 |
{ |
| 191 |
return (addr & dev->sparse_ro_offset); |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* Convert a read-write address to its read-only shadow address. See the |
| 196 |
* documentation for sparse_ro_offset. |
| 197 |
*/ |
| 198 |
static inline uint64_t |
| 199 |
agx_rw_addr_to_ro(struct agx_device *dev, uint64_t addr) |
| 200 |
{ |
| 201 |
assert(!agx_addr_is_ro(dev, addr)); |
| 202 |
return addr + dev->sparse_ro_offset; |
| 203 |
} |
| 204 |
|
| 205 |
static inline void * |
| 206 |
agx_bo_map_placed(struct agx_bo *bo, void *fixed_addr) |
| 207 |
{ |
| 208 |
if (!bo->_map) |
| 209 |
bo->dev->ops.bo_mmap(bo->dev, bo, fixed_addr); |
| 210 |
|
| 211 |
return bo->_map; |
| 212 |
} |
| 213 |
|
| 214 |
static inline void * |
| 215 |
agx_bo_map(struct agx_bo *bo) |
| 216 |
{ |
| 217 |
return agx_bo_map_placed(bo, NULL); |
| 218 |
} |
| 219 |
|
| 220 |
static inline bool |
| 221 |
agx_has_soft_fault(struct agx_device *dev) |
| 222 |
{ |
| 223 |
return (dev->params.features & DRM_ASAHI_FEATURE_SOFT_FAULTS) && |
| 224 |
!(dev->debug & AGX_DBG_NOSOFT); |
| 225 |
} |
| 226 |
|
| 227 |
static uint32_t |
| 228 |
agx_usc_addr(struct agx_device *dev, uint64_t addr) |
| 229 |
{ |
| 230 |
assert(addr >= dev->shader_base); |
| 231 |
assert((addr - dev->shader_base) <= UINT32_MAX); |
| 232 |
|
| 233 |
return addr - dev->shader_base; |
| 234 |
} |
| 235 |
|
| 236 |
bool agx_open_device(void *memctx, struct agx_device *dev); |
| 237 |
|
| 238 |
void agx_close_device(struct agx_device *dev); |
| 239 |
|
| 240 |
static inline struct agx_bo * |
| 241 |
agx_lookup_bo(struct agx_device *dev, uint32_t handle) |
| 242 |
{ |
| 243 |
return util_sparse_array_get(&dev->bo_map, handle); |
| 244 |
} |
| 245 |
|
| 246 |
uint32_t agx_create_command_queue(struct agx_device *dev, |
| 247 |
enum drm_asahi_priority priority); |
| 248 |
int agx_destroy_command_queue(struct agx_device *dev, uint32_t queue_id); |
| 249 |
|
| 250 |
int agx_import_sync_file(struct agx_device *dev, struct agx_bo *bo, int fd); |
| 251 |
int agx_export_sync_file(struct agx_device *dev, struct agx_bo *bo); |
| 252 |
|
| 253 |
void agx_debug_fault(struct agx_device *dev, uint64_t addr); |
| 254 |
|
| 255 |
uint64_t agx_get_gpu_timestamp(struct agx_device *dev); |
| 256 |
|
| 257 |
static inline uint64_t |
| 258 |
agx_gpu_timestamp_to_ns(struct agx_device *dev, uint64_t gpu_timestamp) |
| 259 |
{ |
| 260 |
return (gpu_timestamp * dev->user_timestamp_to_ns.num) / |
| 261 |
dev->user_timestamp_to_ns.den; |
| 262 |
} |
| 263 |
|
| 264 |
void agx_get_device_uuid(const struct agx_device *dev, void *uuid); |
| 265 |
void agx_get_driver_uuid(void *uuid); |
| 266 |
unsigned agx_get_num_cores(const struct agx_device *dev); |
| 267 |
|
| 268 |
struct agx_device_key agx_gather_device_key(struct agx_device *dev); |
| 269 |
|
| 270 |
struct agx_va *agx_va_alloc(struct agx_device *dev, uint64_t size_B, |
| 271 |
uint64_t align_B, enum agx_va_flags flags, |
| 272 |
uint64_t fixed_va); |
| 273 |
void agx_va_free(struct agx_device *dev, struct agx_va *va, bool unbind); |
| 274 |
|
| 275 |
static inline struct drm_asahi_cmd_header |
| 276 |
agx_cmd_header(bool compute, uint16_t barrier_vdm, uint16_t barrier_cdm) |
| 277 |
{ |
| 278 |
return (struct drm_asahi_cmd_header){ |
| 279 |
.cmd_type = compute ? DRM_ASAHI_CMD_COMPUTE : DRM_ASAHI_CMD_RENDER, |
| 280 |
.size = compute ? sizeof(struct drm_asahi_cmd_compute) |
| 281 |
: sizeof(struct drm_asahi_cmd_render), |
| 282 |
.vdm_barrier = barrier_vdm, |
| 283 |
.cdm_barrier = barrier_cdm, |
| 284 |
}; |
| 285 |
} |