amdgpu: remove pointer arithmetic from command submission
[platform/upstream/libdrm.git] / amdgpu / amdgpu_cs.c
1 /*
2  * Copyright 2014 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 */
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <pthread.h>
28 #include <sched.h>
29 #include <sys/ioctl.h>
30
31 #include "xf86drm.h"
32 #include "amdgpu_drm.h"
33 #include "amdgpu_internal.h"
34
35 /**
36  * Create command submission context
37  *
38  * \param   dev - \c [in] amdgpu device handle
39  * \param   context - \c [out] amdgpu context handle
40  *
41  * \return  0 on success otherwise POSIX Error code
42 */
43 int amdgpu_cs_ctx_create(amdgpu_device_handle dev,
44                          amdgpu_context_handle *context)
45 {
46         struct amdgpu_bo_alloc_request alloc_buffer = {};
47         struct amdgpu_bo_alloc_result info = {};
48         struct amdgpu_context *gpu_context;
49         union drm_amdgpu_ctx args;
50         int r;
51
52         if (NULL == dev)
53                 return -EINVAL;
54         if (NULL == context)
55                 return -EINVAL;
56
57         gpu_context = calloc(1, sizeof(struct amdgpu_context));
58         if (NULL == gpu_context)
59                 return -ENOMEM;
60
61         gpu_context->dev = dev;
62
63         r = pthread_mutex_init(&gpu_context->sequence_mutex, NULL);
64         if (r)
65                 goto error_mutex;
66
67         /* Create the fence BO */
68         alloc_buffer.alloc_size = 4 * 1024;
69         alloc_buffer.phys_alignment = 4 * 1024;
70         alloc_buffer.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
71
72         r = amdgpu_bo_alloc(dev, &alloc_buffer, &info);
73         if (r)
74                 goto error_fence_alloc;
75         gpu_context->fence_bo = info.buf_handle;
76
77         r = amdgpu_bo_cpu_map(gpu_context->fence_bo, &gpu_context->fence_cpu);
78         if (r)
79                 goto error_fence_map;
80
81         /* Create the context */
82         memset(&args, 0, sizeof(args));
83         args.in.op = AMDGPU_CTX_OP_ALLOC_CTX;
84         r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_CTX, &args, sizeof(args));
85         if (r)
86                 goto error_kernel;
87
88         gpu_context->id = args.out.alloc.ctx_id;
89         *context = (amdgpu_context_handle)gpu_context;
90
91         return 0;
92
93 error_kernel:
94         amdgpu_bo_cpu_unmap(gpu_context->fence_bo);
95
96 error_fence_map:
97         amdgpu_bo_free(gpu_context->fence_bo);
98
99 error_fence_alloc:
100         pthread_mutex_destroy(&gpu_context->sequence_mutex);
101
102 error_mutex:
103         free(gpu_context);
104         return r;
105 }
106
107 /**
108  * Release command submission context
109  *
110  * \param   dev - \c [in] amdgpu device handle
111  * \param   context - \c [in] amdgpu context handle
112  *
113  * \return  0 on success otherwise POSIX Error code
114 */
115 int amdgpu_cs_ctx_free(amdgpu_context_handle context)
116 {
117         union drm_amdgpu_ctx args;
118         int r;
119
120         if (NULL == context)
121                 return -EINVAL;
122
123         r = amdgpu_bo_cpu_unmap(context->fence_bo);
124         if (r)
125                 return r;
126
127         r = amdgpu_bo_free(context->fence_bo);
128         if (r)
129                 return r;
130
131         pthread_mutex_destroy(&context->sequence_mutex);
132
133         /* now deal with kernel side */
134         memset(&args, 0, sizeof(args));
135         args.in.op = AMDGPU_CTX_OP_FREE_CTX;
136         args.in.ctx_id = context->id;
137         r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
138                                 &args, sizeof(args));
139
140         free(context);
141
142         return r;
143 }
144
145 int amdgpu_cs_query_reset_state(amdgpu_context_handle context,
146                                 uint32_t *state, uint32_t *hangs)
147 {
148         union drm_amdgpu_ctx args;
149         int r;
150
151         if (!context)
152                 return -EINVAL;
153
154         memset(&args, 0, sizeof(args));
155         args.in.op = AMDGPU_CTX_OP_QUERY_STATE;
156         args.in.ctx_id = context->id;
157         r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CTX,
158                                 &args, sizeof(args));
159         if (!r) {
160                 *state = args.out.state.reset_status;
161                 *hangs = args.out.state.hangs;
162         }
163         return r;
164 }
165
166 static uint32_t amdgpu_cs_fence_index(unsigned ip, unsigned ring)
167 {
168         return ip * AMDGPU_CS_MAX_RINGS + ring;
169 }
170
171 /**
172  * Submit command to kernel DRM
173  * \param   dev - \c [in]  Device handle
174  * \param   context - \c [in]  GPU Context
175  * \param   ibs_request - \c [in]  Pointer to submission requests
176  * \param   fence - \c [out] return fence for this submission
177  *
178  * \return  0 on success otherwise POSIX Error code
179  * \sa amdgpu_cs_submit()
180 */
181 static int amdgpu_cs_submit_one(amdgpu_context_handle context,
182                                 struct amdgpu_cs_request *ibs_request,
183                                 uint64_t *fence)
184 {
185         union drm_amdgpu_cs cs;
186         uint64_t *chunk_array;
187         struct drm_amdgpu_cs_chunk *chunks;
188         struct drm_amdgpu_cs_chunk_data *chunk_data;
189         uint32_t i, size;
190         int r = 0;
191
192         if (ibs_request->ip_type >= AMDGPU_HW_IP_NUM)
193                 return -EINVAL;
194         if (ibs_request->ring >= AMDGPU_CS_MAX_RINGS)
195                 return -EINVAL;
196         if (ibs_request->number_of_ibs > AMDGPU_CS_MAX_IBS_PER_SUBMIT)
197                 return -EINVAL;
198
199         size = ibs_request->number_of_ibs + 1;
200
201         chunk_array = alloca(sizeof(uint64_t) * size);
202         chunks = alloca(sizeof(struct drm_amdgpu_cs_chunk) * size);
203         chunk_data = alloca(sizeof(struct drm_amdgpu_cs_chunk_data) * size);
204
205         memset(&cs, 0, sizeof(cs));
206         cs.in.chunks = (uint64_t)(uintptr_t)chunk_array;
207         cs.in.ctx_id = context->id;
208         if (ibs_request->resources)
209                 cs.in.bo_list_handle = ibs_request->resources->handle;
210         cs.in.num_chunks = ibs_request->number_of_ibs;
211         /* IB chunks */
212         for (i = 0; i < ibs_request->number_of_ibs; i++) {
213                 struct amdgpu_cs_ib_info *ib;
214                 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
215                 chunks[i].chunk_id = AMDGPU_CHUNK_ID_IB;
216                 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
217                 chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
218
219                 ib = &ibs_request->ibs[i];
220
221                 chunk_data[i].ib_data._pad = 0;
222                 chunk_data[i].ib_data.va_start = ib->ib_mc_address;
223                 chunk_data[i].ib_data.ib_bytes = ib->size * 4;
224                 chunk_data[i].ib_data.ip_type = ibs_request->ip_type;
225                 chunk_data[i].ib_data.ip_instance = ibs_request->ip_instance;
226                 chunk_data[i].ib_data.ring = ibs_request->ring;
227                 chunk_data[i].ib_data.flags = ib->flags;
228         }
229
230         pthread_mutex_lock(&context->sequence_mutex);
231
232         if (ibs_request->ip_type != AMDGPU_HW_IP_UVD &&
233             ibs_request->ip_type != AMDGPU_HW_IP_VCE) {
234                 i = cs.in.num_chunks++;
235
236                 /* fence chunk */
237                 chunk_array[i] = (uint64_t)(uintptr_t)&chunks[i];
238                 chunks[i].chunk_id = AMDGPU_CHUNK_ID_FENCE;
239                 chunks[i].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
240                 chunks[i].chunk_data = (uint64_t)(uintptr_t)&chunk_data[i];
241
242                 /* fence bo handle */
243                 chunk_data[i].fence_data.handle = context->fence_bo->handle;
244                 /* offset */
245                 chunk_data[i].fence_data.offset = amdgpu_cs_fence_index(
246                         ibs_request->ip_type, ibs_request->ring);
247                 chunk_data[i].fence_data.offset *= sizeof(uint64_t);
248         }
249
250         r = drmCommandWriteRead(context->dev->fd, DRM_AMDGPU_CS,
251                                 &cs, sizeof(cs));
252         if (r)
253                 goto error_unlock;
254
255         *fence = cs.out.handle;
256
257 error_unlock:
258         pthread_mutex_unlock(&context->sequence_mutex);
259         return r;
260 }
261
262 int amdgpu_cs_submit(amdgpu_context_handle context,
263                      uint64_t flags,
264                      struct amdgpu_cs_request *ibs_request,
265                      uint32_t number_of_requests,
266                      uint64_t *fences)
267 {
268         uint32_t i;
269         int r;
270
271         if (NULL == context)
272                 return -EINVAL;
273         if (NULL == ibs_request)
274                 return -EINVAL;
275         if (NULL == fences)
276                 return -EINVAL;
277
278         r = 0;
279         for (i = 0; i < number_of_requests; i++) {
280                 r = amdgpu_cs_submit_one(context, ibs_request, fences);
281                 if (r)
282                         break;
283                 fences++;
284                 ibs_request++;
285         }
286
287         return r;
288 }
289
290 /**
291  * Calculate absolute timeout.
292  *
293  * \param   timeout - \c [in] timeout in nanoseconds.
294  *
295  * \return  absolute timeout in nanoseconds
296 */
297 uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout)
298 {
299         int r;
300
301         if (timeout != AMDGPU_TIMEOUT_INFINITE) {
302                 struct timespec current;
303                 r = clock_gettime(CLOCK_MONOTONIC, &current);
304                 if (r)
305                         return r;
306
307                 timeout += ((uint64_t)current.tv_sec) * 1000000000ull;
308                 timeout += current.tv_nsec;
309         }
310         return timeout;
311 }
312
313 static int amdgpu_ioctl_wait_cs(amdgpu_context_handle context,
314                                 unsigned ip,
315                                 unsigned ip_instance,
316                                 uint32_t ring,
317                                 uint64_t handle,
318                                 uint64_t timeout_ns,
319                                 bool *busy)
320 {
321         amdgpu_device_handle dev = context->dev;
322         union drm_amdgpu_wait_cs args;
323         int r;
324
325         memset(&args, 0, sizeof(args));
326         args.in.handle = handle;
327         args.in.ip_type = ip;
328         args.in.ip_instance = ip_instance;
329         args.in.ring = ring;
330         args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
331         args.in.ctx_id = context->id;
332
333         /* Handle errors manually here because of timeout */
334         r = ioctl(dev->fd, DRM_IOCTL_AMDGPU_WAIT_CS, &args);
335         if (r == -1 && (errno == EINTR || errno == EAGAIN)) {
336                 *busy = true;
337                 return 0;
338         } else if (r)
339                 return -errno;
340
341         *busy = args.out.status;
342         return 0;
343 }
344
345 int amdgpu_cs_query_fence_status(struct amdgpu_cs_query_fence *fence,
346                                  uint32_t *expired)
347 {
348         amdgpu_context_handle context;
349         uint64_t *signaled_fence;
350         uint64_t *expired_fence;
351         unsigned ip_type, ip_instance;
352         uint32_t ring;
353         bool busy = true;
354         int r;
355
356         if (NULL == fence)
357                 return -EINVAL;
358         if (NULL == expired)
359                 return -EINVAL;
360         if (NULL == fence->context)
361                 return -EINVAL;
362         if (fence->ip_type >= AMDGPU_HW_IP_NUM)
363                 return -EINVAL;
364         if (fence->ring >= AMDGPU_CS_MAX_RINGS)
365                 return -EINVAL;
366
367         context = fence->context;
368         ip_type = fence->ip_type;
369         ip_instance = fence->ip_instance;
370         ring = fence->ring;
371         signaled_fence = context->fence_cpu;
372         signaled_fence += amdgpu_cs_fence_index(ip_type, ring);
373         expired_fence = &context->expired_fences[ip_type][ip_instance][ring];
374         *expired = false;
375
376         pthread_mutex_lock(&context->sequence_mutex);
377         if (fence->fence <= *expired_fence) {
378                 /* This fence value is expired already. */
379                 pthread_mutex_unlock(&context->sequence_mutex);
380                 *expired = true;
381                 return 0;
382         }
383
384         if (fence->fence <= *signaled_fence) {
385                 /* This fence value is signaled already. */
386                 *expired_fence = *signaled_fence;
387                 pthread_mutex_unlock(&context->sequence_mutex);
388                 *expired = true;
389                 return 0;
390         }
391
392         if (fence->timeout_ns == 0) {
393                 pthread_mutex_unlock(&context->sequence_mutex);
394                 return 0;
395         }
396
397         pthread_mutex_unlock(&context->sequence_mutex);
398
399         r = amdgpu_ioctl_wait_cs(context, ip_type, ip_instance, ring,
400                                  fence->fence, fence->timeout_ns, &busy);
401         if (!r && !busy) {
402                 *expired = true;
403                 pthread_mutex_lock(&context->sequence_mutex);
404                 /* The thread doesn't hold sequence_mutex. Other thread could
405                    update *expired_fence already. Check whether there is a
406                    newerly expired fence. */
407                 if (fence->fence > *expired_fence)
408                         *expired_fence = fence->fence;
409                 pthread_mutex_unlock(&context->sequence_mutex);
410         }
411
412         return r;
413 }
414