Merge tag 'amd-drm-next-5.14-2021-05-19' of https://gitlab.freedesktop.org/agd5f...
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_object.h
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 #ifndef __AMDGPU_OBJECT_H__
29 #define __AMDGPU_OBJECT_H__
30
31 #include <drm/amdgpu_drm.h>
32 #include "amdgpu.h"
33 #ifdef CONFIG_MMU_NOTIFIER
34 #include <linux/mmu_notifier.h>
35 #endif
36
37 #define AMDGPU_BO_INVALID_OFFSET        LONG_MAX
38 #define AMDGPU_BO_MAX_PLACEMENTS        3
39
40 /* BO flag to indicate a KFD userptr BO */
41 #define AMDGPU_AMDKFD_CREATE_USERPTR_BO (1ULL << 63)
42 #define AMDGPU_AMDKFD_CREATE_SVM_BO     (1ULL << 62)
43
44 #define to_amdgpu_bo_user(abo) container_of((abo), struct amdgpu_bo_user, bo)
45
46 struct amdgpu_bo_param {
47         unsigned long                   size;
48         int                             byte_align;
49         u32                             bo_ptr_size;
50         u32                             domain;
51         u32                             preferred_domain;
52         u64                             flags;
53         enum ttm_bo_type                type;
54         bool                            no_wait_gpu;
55         struct dma_resv *resv;
56 };
57
58 /* bo virtual addresses in a vm */
59 struct amdgpu_bo_va_mapping {
60         struct amdgpu_bo_va             *bo_va;
61         struct list_head                list;
62         struct rb_node                  rb;
63         uint64_t                        start;
64         uint64_t                        last;
65         uint64_t                        __subtree_last;
66         uint64_t                        offset;
67         uint64_t                        flags;
68 };
69
70 /* User space allocated BO in a VM */
71 struct amdgpu_bo_va {
72         struct amdgpu_vm_bo_base        base;
73
74         /* protected by bo being reserved */
75         unsigned                        ref_count;
76
77         /* all other members protected by the VM PD being reserved */
78         struct dma_fence                *last_pt_update;
79
80         /* mappings for this bo_va */
81         struct list_head                invalids;
82         struct list_head                valids;
83
84         /* If the mappings are cleared or filled */
85         bool                            cleared;
86
87         bool                            is_xgmi;
88 };
89
90 struct amdgpu_bo {
91         /* Protected by tbo.reserved */
92         u32                             preferred_domains;
93         u32                             allowed_domains;
94         struct ttm_place                placements[AMDGPU_BO_MAX_PLACEMENTS];
95         struct ttm_placement            placement;
96         struct ttm_buffer_object        tbo;
97         struct ttm_bo_kmap_obj          kmap;
98         u64                             flags;
99         unsigned                        prime_shared_count;
100         /* per VM structure for page tables and with virtual addresses */
101         struct amdgpu_vm_bo_base        *vm_bo;
102         /* Constant after initialization */
103         struct amdgpu_bo                *parent;
104         struct amdgpu_bo                *shadow;
105
106
107
108 #ifdef CONFIG_MMU_NOTIFIER
109         struct mmu_interval_notifier    notifier;
110 #endif
111
112         struct list_head                shadow_list;
113
114         struct kgd_mem                  *kfd_bo;
115 };
116
117 struct amdgpu_bo_user {
118         struct amdgpu_bo                bo;
119         u64                             tiling_flags;
120         u64                             metadata_flags;
121         void                            *metadata;
122         u32                             metadata_size;
123
124 };
125
126 static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
127 {
128         return container_of(tbo, struct amdgpu_bo, tbo);
129 }
130
131 /**
132  * amdgpu_mem_type_to_domain - return domain corresponding to mem_type
133  * @mem_type:   ttm memory type
134  *
135  * Returns corresponding domain of the ttm mem_type
136  */
137 static inline unsigned amdgpu_mem_type_to_domain(u32 mem_type)
138 {
139         switch (mem_type) {
140         case TTM_PL_VRAM:
141                 return AMDGPU_GEM_DOMAIN_VRAM;
142         case TTM_PL_TT:
143                 return AMDGPU_GEM_DOMAIN_GTT;
144         case TTM_PL_SYSTEM:
145                 return AMDGPU_GEM_DOMAIN_CPU;
146         case AMDGPU_PL_GDS:
147                 return AMDGPU_GEM_DOMAIN_GDS;
148         case AMDGPU_PL_GWS:
149                 return AMDGPU_GEM_DOMAIN_GWS;
150         case AMDGPU_PL_OA:
151                 return AMDGPU_GEM_DOMAIN_OA;
152         default:
153                 break;
154         }
155         return 0;
156 }
157
158 /**
159  * amdgpu_bo_reserve - reserve bo
160  * @bo:         bo structure
161  * @no_intr:    don't return -ERESTARTSYS on pending signal
162  *
163  * Returns:
164  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
165  * a signal. Release all buffer reservations and return to user-space.
166  */
167 static inline int amdgpu_bo_reserve(struct amdgpu_bo *bo, bool no_intr)
168 {
169         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
170         int r;
171
172         r = ttm_bo_reserve(&bo->tbo, !no_intr, false, NULL);
173         if (unlikely(r != 0)) {
174                 if (r != -ERESTARTSYS)
175                         dev_err(adev->dev, "%p reserve failed\n", bo);
176                 return r;
177         }
178         return 0;
179 }
180
181 static inline void amdgpu_bo_unreserve(struct amdgpu_bo *bo)
182 {
183         ttm_bo_unreserve(&bo->tbo);
184 }
185
186 static inline unsigned long amdgpu_bo_size(struct amdgpu_bo *bo)
187 {
188         return bo->tbo.base.size;
189 }
190
191 static inline unsigned amdgpu_bo_ngpu_pages(struct amdgpu_bo *bo)
192 {
193         return bo->tbo.base.size / AMDGPU_GPU_PAGE_SIZE;
194 }
195
196 static inline unsigned amdgpu_bo_gpu_page_alignment(struct amdgpu_bo *bo)
197 {
198         return (bo->tbo.page_alignment << PAGE_SHIFT) / AMDGPU_GPU_PAGE_SIZE;
199 }
200
201 /**
202  * amdgpu_bo_mmap_offset - return mmap offset of bo
203  * @bo: amdgpu object for which we query the offset
204  *
205  * Returns mmap offset of the object.
206  */
207 static inline u64 amdgpu_bo_mmap_offset(struct amdgpu_bo *bo)
208 {
209         return drm_vma_node_offset_addr(&bo->tbo.base.vma_node);
210 }
211
212 /**
213  * amdgpu_bo_in_cpu_visible_vram - check if BO is (partly) in visible VRAM
214  */
215 static inline bool amdgpu_bo_in_cpu_visible_vram(struct amdgpu_bo *bo)
216 {
217         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
218         unsigned fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
219         struct drm_mm_node *node = bo->tbo.mem.mm_node;
220         unsigned long pages_left;
221
222         if (bo->tbo.mem.mem_type != TTM_PL_VRAM)
223                 return false;
224
225         for (pages_left = bo->tbo.mem.num_pages; pages_left;
226              pages_left -= node->size, node++)
227                 if (node->start < fpfn)
228                         return true;
229
230         return false;
231 }
232
233 /**
234  * amdgpu_bo_explicit_sync - return whether the bo is explicitly synced
235  */
236 static inline bool amdgpu_bo_explicit_sync(struct amdgpu_bo *bo)
237 {
238         return bo->flags & AMDGPU_GEM_CREATE_EXPLICIT_SYNC;
239 }
240
241 /**
242  * amdgpu_bo_encrypted - test if the BO is encrypted
243  * @bo: pointer to a buffer object
244  *
245  * Return true if the buffer object is encrypted, false otherwise.
246  */
247 static inline bool amdgpu_bo_encrypted(struct amdgpu_bo *bo)
248 {
249         return bo->flags & AMDGPU_GEM_CREATE_ENCRYPTED;
250 }
251
252 bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo);
253 void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain);
254
255 int amdgpu_bo_create(struct amdgpu_device *adev,
256                      struct amdgpu_bo_param *bp,
257                      struct amdgpu_bo **bo_ptr);
258 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
259                               unsigned long size, int align,
260                               u32 domain, struct amdgpu_bo **bo_ptr,
261                               u64 *gpu_addr, void **cpu_addr);
262 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
263                             unsigned long size, int align,
264                             u32 domain, struct amdgpu_bo **bo_ptr,
265                             u64 *gpu_addr, void **cpu_addr);
266 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
267                                uint64_t offset, uint64_t size, uint32_t domain,
268                                struct amdgpu_bo **bo_ptr, void **cpu_addr);
269 int amdgpu_bo_create_user(struct amdgpu_device *adev,
270                           struct amdgpu_bo_param *bp,
271                           struct amdgpu_bo_user **ubo_ptr);
272 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
273                            void **cpu_addr);
274 int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
275                             unsigned long size,
276                             struct amdgpu_bo *bo);
277 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr);
278 void *amdgpu_bo_kptr(struct amdgpu_bo *bo);
279 void amdgpu_bo_kunmap(struct amdgpu_bo *bo);
280 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo);
281 void amdgpu_bo_unref(struct amdgpu_bo **bo);
282 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain);
283 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
284                              u64 min_offset, u64 max_offset);
285 void amdgpu_bo_unpin(struct amdgpu_bo *bo);
286 int amdgpu_bo_evict_vram(struct amdgpu_device *adev);
287 int amdgpu_bo_init(struct amdgpu_device *adev);
288 void amdgpu_bo_fini(struct amdgpu_device *adev);
289 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags);
290 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags);
291 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
292                             uint32_t metadata_size, uint64_t flags);
293 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
294                            size_t buffer_size, uint32_t *metadata_size,
295                            uint64_t *flags);
296 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
297                            bool evict,
298                            struct ttm_resource *new_mem);
299 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo);
300 vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo);
301 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
302                      bool shared);
303 int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv,
304                              enum amdgpu_sync_mode sync_mode, void *owner,
305                              bool intr);
306 int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr);
307 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
308 u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo);
309 int amdgpu_bo_validate(struct amdgpu_bo *bo);
310 void amdgpu_bo_get_memory(struct amdgpu_bo *bo, uint64_t *vram_mem,
311                                 uint64_t *gtt_mem, uint64_t *cpu_mem);
312 int amdgpu_bo_restore_shadow(struct amdgpu_bo *shadow,
313                              struct dma_fence **fence);
314 uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev,
315                                             uint32_t domain);
316
317 /*
318  * sub allocation
319  */
320
321 static inline uint64_t amdgpu_sa_bo_gpu_addr(struct amdgpu_sa_bo *sa_bo)
322 {
323         return sa_bo->manager->gpu_addr + sa_bo->soffset;
324 }
325
326 static inline void * amdgpu_sa_bo_cpu_addr(struct amdgpu_sa_bo *sa_bo)
327 {
328         return sa_bo->manager->cpu_ptr + sa_bo->soffset;
329 }
330
331 int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev,
332                                      struct amdgpu_sa_manager *sa_manager,
333                                      unsigned size, u32 align, u32 domain);
334 void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev,
335                                       struct amdgpu_sa_manager *sa_manager);
336 int amdgpu_sa_bo_manager_start(struct amdgpu_device *adev,
337                                       struct amdgpu_sa_manager *sa_manager);
338 int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager,
339                      struct amdgpu_sa_bo **sa_bo,
340                      unsigned size, unsigned align);
341 void amdgpu_sa_bo_free(struct amdgpu_device *adev,
342                               struct amdgpu_sa_bo **sa_bo,
343                               struct dma_fence *fence);
344 #if defined(CONFIG_DEBUG_FS)
345 void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager,
346                                          struct seq_file *m);
347 u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m);
348 #endif
349 void amdgpu_debugfs_sa_init(struct amdgpu_device *adev);
350
351 bool amdgpu_bo_support_uswc(u64 bo_flags);
352
353
354 #endif