]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
drm/amdgpu: rework sched_list generation
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_ring.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_RING_H__
25 #define __AMDGPU_RING_H__
26
27 #include <drm/amdgpu_drm.h>
28 #include <drm/gpu_scheduler.h>
29 #include <drm/drm_print.h>
30
31 /* max number of rings */
32 #define AMDGPU_MAX_RINGS                28
33 #define AMDGPU_MAX_HWIP_RINGS           8
34 #define AMDGPU_MAX_GFX_RINGS            2
35 #define AMDGPU_MAX_COMPUTE_RINGS        8
36 #define AMDGPU_MAX_VCE_RINGS            3
37 #define AMDGPU_MAX_UVD_ENC_RINGS        2
38
39 #define AMDGPU_RING_PRIO_DEFAULT        1
40 #define AMDGPU_RING_PRIO_MAX            AMDGPU_GFX_PIPE_PRIO_MAX
41
42 /* some special values for the owner field */
43 #define AMDGPU_FENCE_OWNER_UNDEFINED    ((void *)0ul)
44 #define AMDGPU_FENCE_OWNER_VM           ((void *)1ul)
45 #define AMDGPU_FENCE_OWNER_KFD          ((void *)2ul)
46
47 #define AMDGPU_FENCE_FLAG_64BIT         (1 << 0)
48 #define AMDGPU_FENCE_FLAG_INT           (1 << 1)
49 #define AMDGPU_FENCE_FLAG_TC_WB_ONLY    (1 << 2)
50
51 #define to_amdgpu_ring(s) container_of((s), struct amdgpu_ring, sched)
52
53 enum amdgpu_ring_type {
54         AMDGPU_RING_TYPE_GFX            = AMDGPU_HW_IP_GFX,
55         AMDGPU_RING_TYPE_COMPUTE        = AMDGPU_HW_IP_COMPUTE,
56         AMDGPU_RING_TYPE_SDMA           = AMDGPU_HW_IP_DMA,
57         AMDGPU_RING_TYPE_UVD            = AMDGPU_HW_IP_UVD,
58         AMDGPU_RING_TYPE_VCE            = AMDGPU_HW_IP_VCE,
59         AMDGPU_RING_TYPE_UVD_ENC        = AMDGPU_HW_IP_UVD_ENC,
60         AMDGPU_RING_TYPE_VCN_DEC        = AMDGPU_HW_IP_VCN_DEC,
61         AMDGPU_RING_TYPE_VCN_ENC        = AMDGPU_HW_IP_VCN_ENC,
62         AMDGPU_RING_TYPE_VCN_JPEG       = AMDGPU_HW_IP_VCN_JPEG,
63         AMDGPU_RING_TYPE_KIQ
64 };
65
66 struct amdgpu_device;
67 struct amdgpu_ring;
68 struct amdgpu_ib;
69 struct amdgpu_cs_parser;
70 struct amdgpu_job;
71
72 struct amdgpu_sched {
73         u32                             num_scheds;
74         struct drm_gpu_scheduler        *sched[AMDGPU_MAX_HWIP_RINGS];
75 };
76
77 /*
78  * Fences.
79  */
80 struct amdgpu_fence_driver {
81         uint64_t                        gpu_addr;
82         volatile uint32_t               *cpu_addr;
83         /* sync_seq is protected by ring emission lock */
84         uint32_t                        sync_seq;
85         atomic_t                        last_seq;
86         bool                            initialized;
87         struct amdgpu_irq_src           *irq_src;
88         unsigned                        irq_type;
89         struct timer_list               fallback_timer;
90         unsigned                        num_fences_mask;
91         spinlock_t                      lock;
92         struct dma_fence                **fences;
93 };
94
95 int amdgpu_fence_driver_init(struct amdgpu_device *adev);
96 void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
97 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
98
99 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
100                                   unsigned num_hw_submission);
101 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
102                                    struct amdgpu_irq_src *irq_src,
103                                    unsigned irq_type);
104 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev);
105 void amdgpu_fence_driver_resume(struct amdgpu_device *adev);
106 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **fence,
107                       unsigned flags);
108 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s);
109 bool amdgpu_fence_process(struct amdgpu_ring *ring);
110 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring);
111 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
112                                       uint32_t wait_seq,
113                                       signed long timeout);
114 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring);
115
116 /*
117  * Rings.
118  */
119
120 /* provided by hw blocks that expose a ring buffer for commands */
121 struct amdgpu_ring_funcs {
122         enum amdgpu_ring_type   type;
123         uint32_t                align_mask;
124         u32                     nop;
125         bool                    support_64bit_ptrs;
126         bool                    no_user_fence;
127         unsigned                vmhub;
128         unsigned                extra_dw;
129
130         /* ring read/write ptr handling */
131         u64 (*get_rptr)(struct amdgpu_ring *ring);
132         u64 (*get_wptr)(struct amdgpu_ring *ring);
133         void (*set_wptr)(struct amdgpu_ring *ring);
134         /* validating and patching of IBs */
135         int (*parse_cs)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
136         int (*patch_cs_in_place)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
137         /* constants to calculate how many DW are needed for an emit */
138         unsigned emit_frame_size;
139         unsigned emit_ib_size;
140         /* command emit functions */
141         void (*emit_ib)(struct amdgpu_ring *ring,
142                         struct amdgpu_job *job,
143                         struct amdgpu_ib *ib,
144                         uint32_t flags);
145         void (*emit_fence)(struct amdgpu_ring *ring, uint64_t addr,
146                            uint64_t seq, unsigned flags);
147         void (*emit_pipeline_sync)(struct amdgpu_ring *ring);
148         void (*emit_vm_flush)(struct amdgpu_ring *ring, unsigned vmid,
149                               uint64_t pd_addr);
150         void (*emit_hdp_flush)(struct amdgpu_ring *ring);
151         void (*emit_gds_switch)(struct amdgpu_ring *ring, uint32_t vmid,
152                                 uint32_t gds_base, uint32_t gds_size,
153                                 uint32_t gws_base, uint32_t gws_size,
154                                 uint32_t oa_base, uint32_t oa_size);
155         /* testing functions */
156         int (*test_ring)(struct amdgpu_ring *ring);
157         int (*test_ib)(struct amdgpu_ring *ring, long timeout);
158         /* insert NOP packets */
159         void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
160         void (*insert_start)(struct amdgpu_ring *ring);
161         void (*insert_end)(struct amdgpu_ring *ring);
162         /* pad the indirect buffer to the necessary number of dw */
163         void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
164         unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
165         void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
166         /* note usage for clock and power gating */
167         void (*begin_use)(struct amdgpu_ring *ring);
168         void (*end_use)(struct amdgpu_ring *ring);
169         void (*emit_switch_buffer) (struct amdgpu_ring *ring);
170         void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
171         void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
172         void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
173         void (*emit_reg_wait)(struct amdgpu_ring *ring, uint32_t reg,
174                               uint32_t val, uint32_t mask);
175         void (*emit_reg_write_reg_wait)(struct amdgpu_ring *ring,
176                                         uint32_t reg0, uint32_t reg1,
177                                         uint32_t ref, uint32_t mask);
178         void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
179         /* Try to soft recover the ring to make the fence signal */
180         void (*soft_recovery)(struct amdgpu_ring *ring, unsigned vmid);
181         int (*preempt_ib)(struct amdgpu_ring *ring);
182 };
183
184 struct amdgpu_ring {
185         struct amdgpu_device            *adev;
186         const struct amdgpu_ring_funcs  *funcs;
187         struct amdgpu_fence_driver      fence_drv;
188         struct drm_gpu_scheduler        sched;
189
190         struct amdgpu_bo        *ring_obj;
191         volatile uint32_t       *ring;
192         unsigned                rptr_offs;
193         u64                     wptr;
194         u64                     wptr_old;
195         unsigned                ring_size;
196         unsigned                max_dw;
197         int                     count_dw;
198         uint64_t                gpu_addr;
199         uint64_t                ptr_mask;
200         uint32_t                buf_mask;
201         u32                     idx;
202         u32                     me;
203         u32                     pipe;
204         u32                     queue;
205         struct amdgpu_bo        *mqd_obj;
206         uint64_t                mqd_gpu_addr;
207         void                    *mqd_ptr;
208         uint64_t                eop_gpu_addr;
209         u32                     doorbell_index;
210         bool                    use_doorbell;
211         bool                    use_pollmem;
212         unsigned                wptr_offs;
213         unsigned                fence_offs;
214         uint64_t                current_ctx;
215         char                    name[16];
216         u32                     trail_seq;
217         unsigned                trail_fence_offs;
218         u64                     trail_fence_gpu_addr;
219         volatile u32            *trail_fence_cpu_addr;
220         unsigned                cond_exe_offs;
221         u64                     cond_exe_gpu_addr;
222         volatile u32            *cond_exe_cpu_addr;
223         unsigned                vm_inv_eng;
224         struct dma_fence        *vmid_wait;
225         bool                    has_compute_vm_bug;
226
227         atomic_t                num_jobs[DRM_SCHED_PRIORITY_MAX];
228         struct mutex            priority_mutex;
229         /* protected by priority_mutex */
230         int                     priority;
231
232 #if defined(CONFIG_DEBUG_FS)
233         struct dentry *ent;
234 #endif
235 };
236
237 #define amdgpu_ring_parse_cs(r, p, ib) ((r)->funcs->parse_cs((p), (ib)))
238 #define amdgpu_ring_patch_cs_in_place(r, p, ib) ((r)->funcs->patch_cs_in_place((p), (ib)))
239 #define amdgpu_ring_test_ring(r) (r)->funcs->test_ring((r))
240 #define amdgpu_ring_test_ib(r, t) (r)->funcs->test_ib((r), (t))
241 #define amdgpu_ring_get_rptr(r) (r)->funcs->get_rptr((r))
242 #define amdgpu_ring_get_wptr(r) (r)->funcs->get_wptr((r))
243 #define amdgpu_ring_set_wptr(r) (r)->funcs->set_wptr((r))
244 #define amdgpu_ring_emit_ib(r, job, ib, flags) ((r)->funcs->emit_ib((r), (job), (ib), (flags)))
245 #define amdgpu_ring_emit_pipeline_sync(r) (r)->funcs->emit_pipeline_sync((r))
246 #define amdgpu_ring_emit_vm_flush(r, vmid, addr) (r)->funcs->emit_vm_flush((r), (vmid), (addr))
247 #define amdgpu_ring_emit_fence(r, addr, seq, flags) (r)->funcs->emit_fence((r), (addr), (seq), (flags))
248 #define amdgpu_ring_emit_gds_switch(r, v, db, ds, wb, ws, ab, as) (r)->funcs->emit_gds_switch((r), (v), (db), (ds), (wb), (ws), (ab), (as))
249 #define amdgpu_ring_emit_hdp_flush(r) (r)->funcs->emit_hdp_flush((r))
250 #define amdgpu_ring_emit_switch_buffer(r) (r)->funcs->emit_switch_buffer((r))
251 #define amdgpu_ring_emit_cntxcntl(r, d) (r)->funcs->emit_cntxcntl((r), (d))
252 #define amdgpu_ring_emit_rreg(r, d) (r)->funcs->emit_rreg((r), (d))
253 #define amdgpu_ring_emit_wreg(r, d, v) (r)->funcs->emit_wreg((r), (d), (v))
254 #define amdgpu_ring_emit_reg_wait(r, d, v, m) (r)->funcs->emit_reg_wait((r), (d), (v), (m))
255 #define amdgpu_ring_emit_reg_write_reg_wait(r, d0, d1, v, m) (r)->funcs->emit_reg_write_reg_wait((r), (d0), (d1), (v), (m))
256 #define amdgpu_ring_emit_tmz(r, b) (r)->funcs->emit_tmz((r), (b))
257 #define amdgpu_ring_pad_ib(r, ib) ((r)->funcs->pad_ib((r), (ib)))
258 #define amdgpu_ring_init_cond_exec(r) (r)->funcs->init_cond_exec((r))
259 #define amdgpu_ring_patch_cond_exec(r,o) (r)->funcs->patch_cond_exec((r),(o))
260 #define amdgpu_ring_preempt_ib(r) (r)->funcs->preempt_ib(r)
261
262 int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
263 void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
264 void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
265 void amdgpu_ring_commit(struct amdgpu_ring *ring);
266 void amdgpu_ring_undo(struct amdgpu_ring *ring);
267 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
268                      unsigned int ring_size, struct amdgpu_irq_src *irq_src,
269                      unsigned int irq_type, unsigned int prio);
270 void amdgpu_ring_fini(struct amdgpu_ring *ring);
271 void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
272                                                 uint32_t reg0, uint32_t val0,
273                                                 uint32_t reg1, uint32_t val1);
274 bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid,
275                                struct dma_fence *fence);
276
277 static inline void amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
278                                                         bool cond_exec)
279 {
280         *ring->cond_exe_cpu_addr = cond_exec;
281 }
282
283 static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
284 {
285         int i = 0;
286         while (i <= ring->buf_mask)
287                 ring->ring[i++] = ring->funcs->nop;
288
289 }
290
291 static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
292 {
293         if (ring->count_dw <= 0)
294                 DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
295         ring->ring[ring->wptr++ & ring->buf_mask] = v;
296         ring->wptr &= ring->ptr_mask;
297         ring->count_dw--;
298 }
299
300 static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
301                                               void *src, int count_dw)
302 {
303         unsigned occupied, chunk1, chunk2;
304         void *dst;
305
306         if (unlikely(ring->count_dw < count_dw))
307                 DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
308
309         occupied = ring->wptr & ring->buf_mask;
310         dst = (void *)&ring->ring[occupied];
311         chunk1 = ring->buf_mask + 1 - occupied;
312         chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
313         chunk2 = count_dw - chunk1;
314         chunk1 <<= 2;
315         chunk2 <<= 2;
316
317         if (chunk1)
318                 memcpy(dst, src, chunk1);
319
320         if (chunk2) {
321                 src += chunk1;
322                 dst = (void *)ring->ring;
323                 memcpy(dst, src, chunk2);
324         }
325
326         ring->wptr += count_dw;
327         ring->wptr &= ring->ptr_mask;
328         ring->count_dw -= count_dw;
329 }
330
331 int amdgpu_ring_test_helper(struct amdgpu_ring *ring);
332
333 int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
334                              struct amdgpu_ring *ring);
335 void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring);
336
337 #endif
This page took 0.055671 seconds and 4 git commands to generate.