]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
drm/amdgpu: unify the interface of amd_pm_funcs
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vm.h
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Christian König
23  */
24 #ifndef __AMDGPU_VM_H__
25 #define __AMDGPU_VM_H__
26
27 #include <linux/rbtree.h>
28
29 #include "gpu_scheduler.h"
30 #include "amdgpu_sync.h"
31 #include "amdgpu_ring.h"
32
33 struct amdgpu_bo_va;
34 struct amdgpu_job;
35 struct amdgpu_bo_list_entry;
36
37 /*
38  * GPUVM handling
39  */
40
41 /* maximum number of VMIDs */
42 #define AMDGPU_NUM_VM   16
43
44 /* Maximum number of PTEs the hardware can write with one command */
45 #define AMDGPU_VM_MAX_UPDATE_SIZE       0x3FFFF
46
47 /* number of entries in page table */
48 #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size)
49
50 /* PTBs (Page Table Blocks) need to be aligned to 32K */
51 #define AMDGPU_VM_PTB_ALIGN_SIZE   32768
52
53 #define AMDGPU_PTE_VALID        (1ULL << 0)
54 #define AMDGPU_PTE_SYSTEM       (1ULL << 1)
55 #define AMDGPU_PTE_SNOOPED      (1ULL << 2)
56
57 /* VI only */
58 #define AMDGPU_PTE_EXECUTABLE   (1ULL << 4)
59
60 #define AMDGPU_PTE_READABLE     (1ULL << 5)
61 #define AMDGPU_PTE_WRITEABLE    (1ULL << 6)
62
63 #define AMDGPU_PTE_FRAG(x)      ((x & 0x1fULL) << 7)
64
65 /* TILED for VEGA10, reserved for older ASICs  */
66 #define AMDGPU_PTE_PRT          (1ULL << 51)
67
68 /* PDE is handled as PTE for VEGA10 */
69 #define AMDGPU_PDE_PTE          (1ULL << 54)
70
71 /* VEGA10 only */
72 #define AMDGPU_PTE_MTYPE(a)    ((uint64_t)a << 57)
73 #define AMDGPU_PTE_MTYPE_MASK   AMDGPU_PTE_MTYPE(3ULL)
74
75 /* How to programm VM fault handling */
76 #define AMDGPU_VM_FAULT_STOP_NEVER      0
77 #define AMDGPU_VM_FAULT_STOP_FIRST      1
78 #define AMDGPU_VM_FAULT_STOP_ALWAYS     2
79
80 /* max number of VMHUB */
81 #define AMDGPU_MAX_VMHUBS                       2
82 #define AMDGPU_GFXHUB                           0
83 #define AMDGPU_MMHUB                            1
84
85 /* hardcode that limit for now */
86 #define AMDGPU_VA_RESERVED_SIZE                 (8 << 20)
87 /* max vmids dedicated for process */
88 #define AMDGPU_VM_MAX_RESERVED_VMID     1
89
90 #define AMDGPU_VM_CONTEXT_GFX 0
91 #define AMDGPU_VM_CONTEXT_COMPUTE 1
92
93 /* See vm_update_mode */
94 #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
95 #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
96
97 /* base structure for tracking BO usage in a VM */
98 struct amdgpu_vm_bo_base {
99         /* constant after initialization */
100         struct amdgpu_vm                *vm;
101         struct amdgpu_bo                *bo;
102
103         /* protected by bo being reserved */
104         struct list_head                bo_list;
105
106         /* protected by spinlock */
107         struct list_head                vm_status;
108
109         /* protected by the BO being reserved */
110         bool                            moved;
111 };
112
113 struct amdgpu_vm_pt {
114         struct amdgpu_vm_bo_base        base;
115         uint64_t                        addr;
116
117         /* array of page tables, one for each directory entry */
118         struct amdgpu_vm_pt             *entries;
119         unsigned                        last_entry_used;
120 };
121
122 struct amdgpu_vm {
123         /* tree of virtual addresses mapped */
124         struct rb_root          va;
125
126         /* protecting invalidated */
127         spinlock_t              status_lock;
128
129         /* BOs who needs a validation */
130         struct list_head        evicted;
131
132         /* PT BOs which relocated and their parent need an update */
133         struct list_head        relocated;
134
135         /* BOs moved, but not yet updated in the PT */
136         struct list_head        moved;
137
138         /* BO mappings freed, but not yet updated in the PT */
139         struct list_head        freed;
140
141         /* contains the page directory */
142         struct amdgpu_vm_pt     root;
143         struct dma_fence        *last_update;
144
145         /* protecting freed */
146         spinlock_t              freed_lock;
147
148         /* Scheduler entity for page table updates */
149         struct amd_sched_entity entity;
150
151         /* client id */
152         u64                     client_id;
153         /* dedicated to vm */
154         struct amdgpu_vm_id     *reserved_vmid[AMDGPU_MAX_VMHUBS];
155
156         /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */
157         bool                    use_cpu_for_update;
158
159         /* Flag to indicate ATS support from PTE for GFX9 */
160         bool                    pte_support_ats;
161 };
162
163 struct amdgpu_vm_id {
164         struct list_head        list;
165         struct amdgpu_sync      active;
166         struct dma_fence                *last_flush;
167         atomic64_t              owner;
168
169         uint64_t                pd_gpu_addr;
170         /* last flushed PD/PT update */
171         struct dma_fence                *flushed_updates;
172
173         uint32_t                current_gpu_reset_count;
174
175         uint32_t                gds_base;
176         uint32_t                gds_size;
177         uint32_t                gws_base;
178         uint32_t                gws_size;
179         uint32_t                oa_base;
180         uint32_t                oa_size;
181 };
182
183 struct amdgpu_vm_id_manager {
184         struct mutex            lock;
185         unsigned                num_ids;
186         struct list_head        ids_lru;
187         struct amdgpu_vm_id     ids[AMDGPU_NUM_VM];
188         atomic_t                reserved_vmid_num;
189 };
190
191 struct amdgpu_vm_manager {
192         /* Handling of VMIDs */
193         struct amdgpu_vm_id_manager             id_mgr[AMDGPU_MAX_VMHUBS];
194
195         /* Handling of VM fences */
196         u64                                     fence_context;
197         unsigned                                seqno[AMDGPU_MAX_RINGS];
198
199         uint64_t                                max_pfn;
200         uint32_t                                num_level;
201         uint64_t                                vm_size;
202         uint32_t                                block_size;
203         uint32_t                                fragment_size;
204         /* vram base address for page table entry  */
205         u64                                     vram_base_offset;
206         /* vm pte handling */
207         const struct amdgpu_vm_pte_funcs        *vm_pte_funcs;
208         struct amdgpu_ring                      *vm_pte_rings[AMDGPU_MAX_RINGS];
209         unsigned                                vm_pte_num_rings;
210         atomic_t                                vm_pte_next_ring;
211         /* client id counter */
212         atomic64_t                              client_counter;
213
214         /* partial resident texture handling */
215         spinlock_t                              prt_lock;
216         atomic_t                                num_prt_users;
217
218         /* controls how VM page tables are updated for Graphics and Compute.
219          * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU
220          * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
221          */
222         int                                     vm_update_mode;
223 };
224
225 void amdgpu_vm_manager_init(struct amdgpu_device *adev);
226 void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
227 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
228                    int vm_context);
229 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
230 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
231                          struct list_head *validated,
232                          struct amdgpu_bo_list_entry *entry);
233 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
234 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
235                               int (*callback)(void *p, struct amdgpu_bo *bo),
236                               void *param);
237 int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
238                         struct amdgpu_vm *vm,
239                         uint64_t saddr, uint64_t size);
240 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
241                       struct amdgpu_sync *sync, struct dma_fence *fence,
242                       struct amdgpu_job *job);
243 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync);
244 void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
245                         unsigned vmid);
246 void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev);
247 int amdgpu_vm_update_directories(struct amdgpu_device *adev,
248                                  struct amdgpu_vm *vm);
249 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
250                           struct amdgpu_vm *vm,
251                           struct dma_fence **fence);
252 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
253                            struct amdgpu_vm *vm);
254 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
255                         struct amdgpu_bo_va *bo_va,
256                         bool clear);
257 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
258                              struct amdgpu_bo *bo, bool evicted);
259 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
260                                        struct amdgpu_bo *bo);
261 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
262                                       struct amdgpu_vm *vm,
263                                       struct amdgpu_bo *bo);
264 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
265                      struct amdgpu_bo_va *bo_va,
266                      uint64_t addr, uint64_t offset,
267                      uint64_t size, uint64_t flags);
268 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
269                              struct amdgpu_bo_va *bo_va,
270                              uint64_t addr, uint64_t offset,
271                              uint64_t size, uint64_t flags);
272 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
273                        struct amdgpu_bo_va *bo_va,
274                        uint64_t addr);
275 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
276                                 struct amdgpu_vm *vm,
277                                 uint64_t saddr, uint64_t size);
278 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
279                                                          uint64_t addr);
280 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
281                       struct amdgpu_bo_va *bo_va);
282 void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,
283                                 uint32_t fragment_size_default);
284 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size,
285                                 uint32_t fragment_size_default);
286 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
287 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
288                                   struct amdgpu_job *job);
289 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev);
290
291 #endif
This page took 0.049774 seconds and 4 git commands to generate.