0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #ifndef __AMDGPU_VM_H__
0025 #define __AMDGPU_VM_H__
0026
0027 #include <linux/idr.h>
0028 #include <linux/kfifo.h>
0029 #include <linux/rbtree.h>
0030 #include <drm/gpu_scheduler.h>
0031 #include <drm/drm_file.h>
0032 #include <drm/ttm/ttm_bo_driver.h>
0033 #include <linux/sched/mm.h>
0034
0035 #include "amdgpu_sync.h"
0036 #include "amdgpu_ring.h"
0037 #include "amdgpu_ids.h"
0038
0039 struct amdgpu_bo_va;
0040 struct amdgpu_job;
0041 struct amdgpu_bo_list_entry;
0042 struct amdgpu_bo_vm;
0043
0044
0045
0046
0047
0048
0049 #define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF
0050
0051
0052 #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
0053
0054 #define AMDGPU_PTE_VALID (1ULL << 0)
0055 #define AMDGPU_PTE_SYSTEM (1ULL << 1)
0056 #define AMDGPU_PTE_SNOOPED (1ULL << 2)
0057
0058
0059 #define AMDGPU_PTE_TMZ (1ULL << 3)
0060
0061
0062 #define AMDGPU_PTE_EXECUTABLE (1ULL << 4)
0063
0064 #define AMDGPU_PTE_READABLE (1ULL << 5)
0065 #define AMDGPU_PTE_WRITEABLE (1ULL << 6)
0066
0067 #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7)
0068
0069
0070 #define AMDGPU_PTE_PRT (1ULL << 51)
0071
0072
0073 #define AMDGPU_PDE_PTE (1ULL << 54)
0074
0075 #define AMDGPU_PTE_LOG (1ULL << 55)
0076
0077
0078 #define AMDGPU_PTE_TF (1ULL << 56)
0079
0080
0081 #define AMDGPU_PTE_NOALLOC (1ULL << 58)
0082
0083
0084 #define AMDGPU_PDE_BFS(a) ((uint64_t)a << 59)
0085
0086
0087
0088 #define AMDGPU_PTE_MTYPE_VG10(a) ((uint64_t)(a) << 57)
0089 #define AMDGPU_PTE_MTYPE_VG10_MASK AMDGPU_PTE_MTYPE_VG10(3ULL)
0090
0091 #define AMDGPU_MTYPE_NC 0
0092 #define AMDGPU_MTYPE_CC 2
0093
0094 #define AMDGPU_PTE_DEFAULT_ATC (AMDGPU_PTE_SYSTEM \
0095 | AMDGPU_PTE_SNOOPED \
0096 | AMDGPU_PTE_EXECUTABLE \
0097 | AMDGPU_PTE_READABLE \
0098 | AMDGPU_PTE_WRITEABLE \
0099 | AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_CC))
0100
0101
0102 #define AMDGPU_PTE_MTYPE_NV10(a) ((uint64_t)(a) << 48)
0103 #define AMDGPU_PTE_MTYPE_NV10_MASK AMDGPU_PTE_MTYPE_NV10(7ULL)
0104
0105
0106 #define AMDGPU_VM_FAULT_STOP_NEVER 0
0107 #define AMDGPU_VM_FAULT_STOP_FIRST 1
0108 #define AMDGPU_VM_FAULT_STOP_ALWAYS 2
0109
0110
0111 #define AMDGPU_VM_RESERVED_VRAM (8ULL << 20)
0112
0113
0114 #define AMDGPU_MAX_VMHUBS 3
0115 #define AMDGPU_GFXHUB_0 0
0116 #define AMDGPU_MMHUB_0 1
0117 #define AMDGPU_MMHUB_1 2
0118
0119
0120 #define AMDGPU_VA_RESERVED_SIZE (2ULL << 20)
0121
0122
0123 #define AMDGPU_VM_MAX_RESERVED_VMID 1
0124
0125
0126 #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
0127 #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
0128
0129
0130
0131
0132 enum amdgpu_vm_level {
0133 AMDGPU_VM_PDB2,
0134 AMDGPU_VM_PDB1,
0135 AMDGPU_VM_PDB0,
0136 AMDGPU_VM_PTB
0137 };
0138
0139
0140 struct amdgpu_vm_bo_base {
0141
0142 struct amdgpu_vm *vm;
0143 struct amdgpu_bo *bo;
0144
0145
0146 struct amdgpu_vm_bo_base *next;
0147
0148
0149 struct list_head vm_status;
0150
0151
0152 bool moved;
0153 };
0154
0155
0156 struct amdgpu_vm_pte_funcs {
0157
0158 unsigned copy_pte_num_dw;
0159
0160
0161 void (*copy_pte)(struct amdgpu_ib *ib,
0162 uint64_t pe, uint64_t src,
0163 unsigned count);
0164
0165
0166 void (*write_pte)(struct amdgpu_ib *ib, uint64_t pe,
0167 uint64_t value, unsigned count,
0168 uint32_t incr);
0169
0170 void (*set_pte_pde)(struct amdgpu_ib *ib,
0171 uint64_t pe,
0172 uint64_t addr, unsigned count,
0173 uint32_t incr, uint64_t flags);
0174 };
0175
0176 struct amdgpu_task_info {
0177 char process_name[TASK_COMM_LEN];
0178 char task_name[TASK_COMM_LEN];
0179 pid_t pid;
0180 pid_t tgid;
0181 };
0182
0183
0184
0185
0186
0187
0188
0189
0190 struct amdgpu_vm_update_params {
0191
0192
0193
0194
0195 struct amdgpu_device *adev;
0196
0197
0198
0199
0200 struct amdgpu_vm *vm;
0201
0202
0203
0204
0205 bool immediate;
0206
0207
0208
0209
0210 bool unlocked;
0211
0212
0213
0214
0215
0216
0217 dma_addr_t *pages_addr;
0218
0219
0220
0221
0222 struct amdgpu_job *job;
0223
0224
0225
0226
0227 unsigned int num_dw_left;
0228
0229
0230
0231
0232 bool table_freed;
0233 };
0234
0235 struct amdgpu_vm_update_funcs {
0236 int (*map_table)(struct amdgpu_bo_vm *bo);
0237 int (*prepare)(struct amdgpu_vm_update_params *p, struct dma_resv *resv,
0238 enum amdgpu_sync_mode sync_mode);
0239 int (*update)(struct amdgpu_vm_update_params *p,
0240 struct amdgpu_bo_vm *bo, uint64_t pe, uint64_t addr,
0241 unsigned count, uint32_t incr, uint64_t flags);
0242 int (*commit)(struct amdgpu_vm_update_params *p,
0243 struct dma_fence **fence);
0244 };
0245
0246 struct amdgpu_vm {
0247
0248 struct rb_root_cached va;
0249
0250
0251
0252
0253 struct mutex eviction_lock;
0254 bool evicting;
0255 unsigned int saved_flags;
0256
0257
0258 struct list_head evicted;
0259
0260
0261 struct list_head relocated;
0262
0263
0264 struct list_head moved;
0265
0266
0267 struct list_head idle;
0268
0269
0270 struct list_head invalidated;
0271 spinlock_t invalidated_lock;
0272
0273
0274 struct list_head freed;
0275
0276
0277 struct list_head done;
0278
0279
0280 struct amdgpu_vm_bo_base root;
0281 struct dma_fence *last_update;
0282
0283
0284 struct drm_sched_entity immediate;
0285 struct drm_sched_entity delayed;
0286
0287
0288 atomic64_t tlb_seq;
0289 struct dma_fence *last_tlb_flush;
0290
0291
0292 struct dma_fence *last_unlocked;
0293
0294 unsigned int pasid;
0295
0296 struct amdgpu_vmid *reserved_vmid[AMDGPU_MAX_VMHUBS];
0297
0298
0299 bool use_cpu_for_update;
0300
0301
0302 const struct amdgpu_vm_update_funcs *update_funcs;
0303
0304
0305 bool pte_support_ats;
0306
0307
0308 DECLARE_KFIFO(faults, u64, 128);
0309
0310
0311 struct amdkfd_process_info *process_info;
0312
0313
0314 struct list_head vm_list_node;
0315
0316
0317 uint64_t pd_phys_addr;
0318
0319
0320 struct amdgpu_task_info task_info;
0321
0322
0323 struct ttm_lru_bulk_move lru_bulk_move;
0324
0325 bool is_compute_context;
0326 };
0327
0328 struct amdgpu_vm_manager {
0329
0330 struct amdgpu_vmid_mgr id_mgr[AMDGPU_MAX_VMHUBS];
0331 unsigned int first_kfd_vmid;
0332 bool concurrent_flush;
0333
0334
0335 u64 fence_context;
0336 unsigned seqno[AMDGPU_MAX_RINGS];
0337
0338 uint64_t max_pfn;
0339 uint32_t num_level;
0340 uint32_t block_size;
0341 uint32_t fragment_size;
0342 enum amdgpu_vm_level root_level;
0343
0344 u64 vram_base_offset;
0345
0346 const struct amdgpu_vm_pte_funcs *vm_pte_funcs;
0347 struct drm_gpu_scheduler *vm_pte_scheds[AMDGPU_MAX_RINGS];
0348 unsigned vm_pte_num_scheds;
0349 struct amdgpu_ring *page_fault;
0350
0351
0352 spinlock_t prt_lock;
0353 atomic_t num_prt_users;
0354
0355
0356
0357
0358
0359 int vm_update_mode;
0360
0361
0362
0363
0364 struct xarray pasids;
0365 };
0366
0367 struct amdgpu_bo_va_mapping;
0368
0369 #define amdgpu_vm_copy_pte(adev, ib, pe, src, count) ((adev)->vm_manager.vm_pte_funcs->copy_pte((ib), (pe), (src), (count)))
0370 #define amdgpu_vm_write_pte(adev, ib, pe, value, count, incr) ((adev)->vm_manager.vm_pte_funcs->write_pte((ib), (pe), (value), (count), (incr)))
0371 #define amdgpu_vm_set_pte_pde(adev, ib, pe, addr, count, incr, flags) ((adev)->vm_manager.vm_pte_funcs->set_pte_pde((ib), (pe), (addr), (count), (incr), (flags)))
0372
0373 extern const struct amdgpu_vm_update_funcs amdgpu_vm_cpu_funcs;
0374 extern const struct amdgpu_vm_update_funcs amdgpu_vm_sdma_funcs;
0375
0376 void amdgpu_vm_manager_init(struct amdgpu_device *adev);
0377 void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
0378
0379 int amdgpu_vm_set_pasid(struct amdgpu_device *adev, struct amdgpu_vm *vm,
0380 u32 pasid);
0381
0382 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout);
0383 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm);
0384 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
0385 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
0386 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
0387 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
0388 struct list_head *validated,
0389 struct amdgpu_bo_list_entry *entry);
0390 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
0391 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
0392 int (*callback)(void *p, struct amdgpu_bo *bo),
0393 void *param);
0394 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
0395 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
0396 struct amdgpu_vm *vm, bool immediate);
0397 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
0398 struct amdgpu_vm *vm,
0399 struct dma_fence **fence);
0400 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
0401 struct amdgpu_vm *vm);
0402 void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
0403 struct amdgpu_vm *vm, struct amdgpu_bo *bo);
0404 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
0405 bool immediate, bool unlocked, bool flush_tlb,
0406 struct dma_resv *resv, uint64_t start, uint64_t last,
0407 uint64_t flags, uint64_t offset, uint64_t vram_base,
0408 struct ttm_resource *res, dma_addr_t *pages_addr,
0409 struct dma_fence **fence);
0410 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
0411 struct amdgpu_bo_va *bo_va,
0412 bool clear);
0413 bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
0414 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
0415 struct amdgpu_bo *bo, bool evicted);
0416 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
0417 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
0418 struct amdgpu_bo *bo);
0419 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
0420 struct amdgpu_vm *vm,
0421 struct amdgpu_bo *bo);
0422 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
0423 struct amdgpu_bo_va *bo_va,
0424 uint64_t addr, uint64_t offset,
0425 uint64_t size, uint64_t flags);
0426 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
0427 struct amdgpu_bo_va *bo_va,
0428 uint64_t addr, uint64_t offset,
0429 uint64_t size, uint64_t flags);
0430 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
0431 struct amdgpu_bo_va *bo_va,
0432 uint64_t addr);
0433 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
0434 struct amdgpu_vm *vm,
0435 uint64_t saddr, uint64_t size);
0436 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
0437 uint64_t addr);
0438 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket);
0439 void amdgpu_vm_bo_del(struct amdgpu_device *adev,
0440 struct amdgpu_bo_va *bo_va);
0441 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
0442 uint32_t fragment_size_default, unsigned max_level,
0443 unsigned max_bits);
0444 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
0445 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
0446 struct amdgpu_job *job);
0447 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
0448
0449 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid,
0450 struct amdgpu_task_info *task_info);
0451 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
0452 uint64_t addr, bool write_fault);
0453
0454 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
0455
0456 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
0457 struct amdgpu_vm *vm);
0458 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem,
0459 uint64_t *gtt_mem, uint64_t *cpu_mem);
0460
0461 int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
0462 struct amdgpu_bo_vm *vmbo, bool immediate);
0463 int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
0464 int level, bool immediate, struct amdgpu_bo_vm **vmbo);
0465 void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm);
0466 bool amdgpu_vm_pt_is_root_clean(struct amdgpu_device *adev,
0467 struct amdgpu_vm *vm);
0468
0469 int amdgpu_vm_pde_update(struct amdgpu_vm_update_params *params,
0470 struct amdgpu_vm_bo_base *entry);
0471 int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
0472 uint64_t start, uint64_t end,
0473 uint64_t dst, uint64_t flags);
0474
0475 #if defined(CONFIG_DEBUG_FS)
0476 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);
0477 #endif
0478
0479
0480
0481
0482
0483
0484
0485
0486 static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
0487 {
0488 return atomic64_read(&vm->tlb_seq);
0489 }
0490
0491 #endif