blob: 0d9ce141404ccf816b512b5556630c6f6bbb05c4 [file] [log] [blame]
Christian König78023012016-09-28 15:33:18 +02001/*
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
Andres Rodriguezb2ff0e82017-02-20 17:53:19 -050027#include <drm/amdgpu_drm.h>
Christian König78023012016-09-28 15:33:18 +020028#include "gpu_scheduler.h"
29
30/* max number of rings */
Leo Liuf72430532017-01-10 11:23:23 -050031#define AMDGPU_MAX_RINGS 18
Christian König78023012016-09-28 15:33:18 +020032#define AMDGPU_MAX_GFX_RINGS 1
33#define AMDGPU_MAX_COMPUTE_RINGS 8
34#define AMDGPU_MAX_VCE_RINGS 3
Leo Liuf72430532017-01-10 11:23:23 -050035#define AMDGPU_MAX_UVD_ENC_RINGS 2
Christian König78023012016-09-28 15:33:18 +020036
37/* some special values for the owner field */
38#define AMDGPU_FENCE_OWNER_UNDEFINED ((void*)0ul)
39#define AMDGPU_FENCE_OWNER_VM ((void*)1ul)
40
41#define AMDGPU_FENCE_FLAG_64BIT (1 << 0)
42#define AMDGPU_FENCE_FLAG_INT (1 << 1)
43
44enum amdgpu_ring_type {
45 AMDGPU_RING_TYPE_GFX,
46 AMDGPU_RING_TYPE_COMPUTE,
47 AMDGPU_RING_TYPE_SDMA,
48 AMDGPU_RING_TYPE_UVD,
Trigger Huang20687512016-10-31 02:51:18 -040049 AMDGPU_RING_TYPE_VCE,
Leo Liu50c3e232017-01-12 13:19:46 -050050 AMDGPU_RING_TYPE_KIQ,
Leo Liucca69fe2017-05-05 11:40:59 -040051 AMDGPU_RING_TYPE_UVD_ENC,
Leo Liu8ace8452017-02-21 10:36:15 -050052 AMDGPU_RING_TYPE_VCN_DEC,
53 AMDGPU_RING_TYPE_VCN_ENC
Christian König78023012016-09-28 15:33:18 +020054};
55
56struct amdgpu_device;
57struct amdgpu_ring;
58struct amdgpu_ib;
59struct amdgpu_cs_parser;
Andres Rodriguezb2ff0e82017-02-20 17:53:19 -050060struct amdgpu_job;
Christian König78023012016-09-28 15:33:18 +020061
62/*
63 * Fences.
64 */
65struct amdgpu_fence_driver {
66 uint64_t gpu_addr;
67 volatile uint32_t *cpu_addr;
68 /* sync_seq is protected by ring emission lock */
69 uint32_t sync_seq;
70 atomic_t last_seq;
71 bool initialized;
72 struct amdgpu_irq_src *irq_src;
73 unsigned irq_type;
74 struct timer_list fallback_timer;
75 unsigned num_fences_mask;
76 spinlock_t lock;
Dave Airlie220196b2016-10-28 11:33:52 +100077 struct dma_fence **fences;
Christian König78023012016-09-28 15:33:18 +020078};
79
80int amdgpu_fence_driver_init(struct amdgpu_device *adev);
81void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
82void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev);
Monk Liu65781c72017-05-11 13:36:44 +080083void amdgpu_fence_driver_force_completion_ring(struct amdgpu_ring *ring);
Christian König78023012016-09-28 15:33:18 +020084
85int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
86 unsigned num_hw_submission);
87int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
88 struct amdgpu_irq_src *irq_src,
89 unsigned irq_type);
90void amdgpu_fence_driver_suspend(struct amdgpu_device *adev);
91void amdgpu_fence_driver_resume(struct amdgpu_device *adev);
Dave Airlie220196b2016-10-28 11:33:52 +100092int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **fence);
Christian König78023012016-09-28 15:33:18 +020093void amdgpu_fence_process(struct amdgpu_ring *ring);
94int amdgpu_fence_wait_empty(struct amdgpu_ring *ring);
95unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring);
96
97/*
98 * Rings.
99 */
100
101/* provided by hw blocks that expose a ring buffer for commands */
102struct amdgpu_ring_funcs {
Christian König21cd9422016-10-05 15:36:39 +0200103 enum amdgpu_ring_type type;
Christian König79887142016-10-05 16:09:32 +0200104 uint32_t align_mask;
105 u32 nop;
Ken Wang536fbf92016-03-12 09:32:30 +0800106 bool support_64bit_ptrs;
Christian König0eeb68b2017-03-30 14:49:50 +0200107 unsigned vmhub;
Christian König21cd9422016-10-05 15:36:39 +0200108
Christian König78023012016-09-28 15:33:18 +0200109 /* ring read/write ptr handling */
Ken Wang536fbf92016-03-12 09:32:30 +0800110 u64 (*get_rptr)(struct amdgpu_ring *ring);
111 u64 (*get_wptr)(struct amdgpu_ring *ring);
Christian König78023012016-09-28 15:33:18 +0200112 void (*set_wptr)(struct amdgpu_ring *ring);
113 /* validating and patching of IBs */
114 int (*parse_cs)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
Christian Könige12f3d72016-10-05 14:29:38 +0200115 /* constants to calculate how many DW are needed for an emit */
116 unsigned emit_frame_size;
117 unsigned emit_ib_size;
Christian König78023012016-09-28 15:33:18 +0200118 /* command emit functions */
119 void (*emit_ib)(struct amdgpu_ring *ring,
120 struct amdgpu_ib *ib,
121 unsigned vm_id, bool ctx_switch);
122 void (*emit_fence)(struct amdgpu_ring *ring, uint64_t addr,
123 uint64_t seq, unsigned flags);
124 void (*emit_pipeline_sync)(struct amdgpu_ring *ring);
125 void (*emit_vm_flush)(struct amdgpu_ring *ring, unsigned vm_id,
126 uint64_t pd_addr);
127 void (*emit_hdp_flush)(struct amdgpu_ring *ring);
128 void (*emit_hdp_invalidate)(struct amdgpu_ring *ring);
129 void (*emit_gds_switch)(struct amdgpu_ring *ring, uint32_t vmid,
130 uint32_t gds_base, uint32_t gds_size,
131 uint32_t gws_base, uint32_t gws_size,
132 uint32_t oa_base, uint32_t oa_size);
133 /* testing functions */
134 int (*test_ring)(struct amdgpu_ring *ring);
135 int (*test_ib)(struct amdgpu_ring *ring, long timeout);
136 /* insert NOP packets */
137 void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
Leo Liuef44f852017-05-11 16:29:08 -0400138 void (*insert_start)(struct amdgpu_ring *ring);
Leo Liu135d4732016-12-14 15:05:00 -0500139 void (*insert_end)(struct amdgpu_ring *ring);
Christian König78023012016-09-28 15:33:18 +0200140 /* pad the indirect buffer to the necessary number of dw */
141 void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
142 unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
143 void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
144 /* note usage for clock and power gating */
145 void (*begin_use)(struct amdgpu_ring *ring);
146 void (*end_use)(struct amdgpu_ring *ring);
147 void (*emit_switch_buffer) (struct amdgpu_ring *ring);
148 void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
Xiangliang Yub6091c12017-01-10 12:53:52 +0800149 void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
150 void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
Monk Liu3b4d68e2017-05-01 18:09:22 +0800151 void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
Andres Rodriguezb2ff0e82017-02-20 17:53:19 -0500152 /* priority functions */
153 void (*set_priority) (struct amdgpu_ring *ring,
154 enum amd_sched_priority priority);
Christian König78023012016-09-28 15:33:18 +0200155};
156
157struct amdgpu_ring {
158 struct amdgpu_device *adev;
159 const struct amdgpu_ring_funcs *funcs;
160 struct amdgpu_fence_driver fence_drv;
161 struct amd_gpu_scheduler sched;
Andres Rodriguez795f2812017-03-06 16:27:55 -0500162 struct list_head lru_list;
Christian König78023012016-09-28 15:33:18 +0200163
164 struct amdgpu_bo *ring_obj;
165 volatile uint32_t *ring;
166 unsigned rptr_offs;
Ken Wang536fbf92016-03-12 09:32:30 +0800167 u64 wptr;
168 u64 wptr_old;
Christian König78023012016-09-28 15:33:18 +0200169 unsigned ring_size;
170 unsigned max_dw;
171 int count_dw;
172 uint64_t gpu_addr;
Ken Wang536fbf92016-03-12 09:32:30 +0800173 uint64_t ptr_mask;
174 uint32_t buf_mask;
Christian König78023012016-09-28 15:33:18 +0200175 bool ready;
Christian König78023012016-09-28 15:33:18 +0200176 u32 idx;
177 u32 me;
178 u32 pipe;
179 u32 queue;
180 struct amdgpu_bo *mqd_obj;
Monk Liuf3972b52017-01-24 18:33:22 +0800181 uint64_t mqd_gpu_addr;
Xiangliang Yu59a82d72017-02-17 16:03:10 +0800182 void *mqd_ptr;
Alex Deucher34534612017-03-23 02:16:07 -0400183 uint64_t eop_gpu_addr;
Christian König78023012016-09-28 15:33:18 +0200184 u32 doorbell_index;
185 bool use_doorbell;
186 unsigned wptr_offs;
187 unsigned fence_offs;
188 uint64_t current_ctx;
Christian König78023012016-09-28 15:33:18 +0200189 char name[16];
190 unsigned cond_exe_offs;
191 u64 cond_exe_gpu_addr;
192 volatile u32 *cond_exe_cpu_addr;
Christian König4789c462017-03-31 11:03:50 +0200193 unsigned vm_inv_eng;
Alex Xiedd684d32017-05-30 17:10:16 -0400194 bool has_compute_vm_bug;
Andres Rodriguezb2ff0e82017-02-20 17:53:19 -0500195
196 atomic_t num_jobs[AMD_SCHED_PRIORITY_MAX];
197 struct mutex priority_mutex;
198 /* protected by priority_mutex */
199 int priority;
200
Christian König78023012016-09-28 15:33:18 +0200201#if defined(CONFIG_DEBUG_FS)
202 struct dentry *ent;
203#endif
204};
205
206int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
207void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
208void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
209void amdgpu_ring_commit(struct amdgpu_ring *ring);
210void amdgpu_ring_undo(struct amdgpu_ring *ring);
Andres Rodriguezb2ff0e82017-02-20 17:53:19 -0500211void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
212 enum amd_sched_priority priority);
213void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
214 enum amd_sched_priority priority);
Christian König78023012016-09-28 15:33:18 +0200215int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
Christian König79887142016-10-05 16:09:32 +0200216 unsigned ring_size, struct amdgpu_irq_src *irq_src,
217 unsigned irq_type);
Christian König78023012016-09-28 15:33:18 +0200218void amdgpu_ring_fini(struct amdgpu_ring *ring);
Andres Rodriguez35161bb2017-09-26 17:43:14 -0400219int amdgpu_ring_lru_get(struct amdgpu_device *adev, int type,
220 int *blacklist, int num_blacklist,
221 bool lru_pipe_order, struct amdgpu_ring **ring);
Andres Rodriguez795f2812017-03-06 16:27:55 -0500222void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring);
Monk Liuc79ecfb2017-02-08 16:49:46 +0800223static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
224{
225 int i = 0;
Monk Liue09706f2017-03-21 18:48:45 +0800226 while (i <= ring->buf_mask)
Monk Liuc79ecfb2017-02-08 16:49:46 +0800227 ring->ring[i++] = ring->funcs->nop;
228
229}
Christian König78023012016-09-28 15:33:18 +0200230
Christian Könige8110b12017-06-28 13:43:48 +0200231static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
232{
233 if (ring->count_dw <= 0)
234 DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
235 ring->ring[ring->wptr++ & ring->buf_mask] = v;
236 ring->wptr &= ring->ptr_mask;
237 ring->count_dw--;
238}
239
240static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
241 void *src, int count_dw)
242{
243 unsigned occupied, chunk1, chunk2;
244 void *dst;
245
Christian König369421c2017-06-28 13:50:07 +0200246 if (unlikely(ring->count_dw < count_dw))
Christian Könige8110b12017-06-28 13:43:48 +0200247 DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
Christian Könige8110b12017-06-28 13:43:48 +0200248
249 occupied = ring->wptr & ring->buf_mask;
250 dst = (void *)&ring->ring[occupied];
251 chunk1 = ring->buf_mask + 1 - occupied;
252 chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
253 chunk2 = count_dw - chunk1;
254 chunk1 <<= 2;
255 chunk2 <<= 2;
256
257 if (chunk1)
258 memcpy(dst, src, chunk1);
259
260 if (chunk2) {
261 src += chunk1;
262 dst = (void *)ring->ring;
263 memcpy(dst, src, chunk2);
264 }
265
266 ring->wptr += count_dw;
267 ring->wptr &= ring->ptr_mask;
268 ring->count_dw -= count_dw;
269}
270
Christian König78023012016-09-28 15:33:18 +0200271#endif