Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Jerome Glisse. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * 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 | * PRECISION INSIGHT AND/OR ITS SUPPLIERS 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 OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Jerome Glisse <glisse@freedesktop.org> |
| 26 | */ |
Stephen Rothwell | 568d7c7 | 2016-03-17 15:30:49 +1100 | [diff] [blame] | 27 | #include <linux/pagemap.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 28 | #include <drm/drmP.h> |
| 29 | #include <drm/amdgpu_drm.h> |
| 30 | #include "amdgpu.h" |
| 31 | #include "amdgpu_trace.h" |
| 32 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 33 | int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type, |
| 34 | u32 ip_instance, u32 ring, |
| 35 | struct amdgpu_ring **out_ring) |
| 36 | { |
| 37 | /* Right now all IPs have only one instance - multiple rings. */ |
| 38 | if (ip_instance != 0) { |
| 39 | DRM_ERROR("invalid ip instance: %d\n", ip_instance); |
| 40 | return -EINVAL; |
| 41 | } |
| 42 | |
| 43 | switch (ip_type) { |
| 44 | default: |
| 45 | DRM_ERROR("unknown ip type: %d\n", ip_type); |
| 46 | return -EINVAL; |
| 47 | case AMDGPU_HW_IP_GFX: |
| 48 | if (ring < adev->gfx.num_gfx_rings) { |
| 49 | *out_ring = &adev->gfx.gfx_ring[ring]; |
| 50 | } else { |
| 51 | DRM_ERROR("only %d gfx rings are supported now\n", |
| 52 | adev->gfx.num_gfx_rings); |
| 53 | return -EINVAL; |
| 54 | } |
| 55 | break; |
| 56 | case AMDGPU_HW_IP_COMPUTE: |
| 57 | if (ring < adev->gfx.num_compute_rings) { |
| 58 | *out_ring = &adev->gfx.compute_ring[ring]; |
| 59 | } else { |
| 60 | DRM_ERROR("only %d compute rings are supported now\n", |
| 61 | adev->gfx.num_compute_rings); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | break; |
| 65 | case AMDGPU_HW_IP_DMA: |
Alex Deucher | c113ea1 | 2015-10-08 16:30:37 -0400 | [diff] [blame] | 66 | if (ring < adev->sdma.num_instances) { |
| 67 | *out_ring = &adev->sdma.instance[ring].ring; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 68 | } else { |
Alex Deucher | c113ea1 | 2015-10-08 16:30:37 -0400 | [diff] [blame] | 69 | DRM_ERROR("only %d SDMA rings are supported\n", |
| 70 | adev->sdma.num_instances); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 71 | return -EINVAL; |
| 72 | } |
| 73 | break; |
| 74 | case AMDGPU_HW_IP_UVD: |
| 75 | *out_ring = &adev->uvd.ring; |
| 76 | break; |
| 77 | case AMDGPU_HW_IP_VCE: |
Alex Deucher | 034041f | 2017-01-11 16:11:48 -0500 | [diff] [blame] | 78 | if (ring < adev->vce.num_rings){ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 79 | *out_ring = &adev->vce.ring[ring]; |
| 80 | } else { |
Alex Deucher | 034041f | 2017-01-11 16:11:48 -0500 | [diff] [blame] | 81 | DRM_ERROR("only %d VCE rings are supported\n", adev->vce.num_rings); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 82 | return -EINVAL; |
| 83 | } |
| 84 | break; |
Leo Liu | 166c817 | 2017-01-10 11:57:24 -0500 | [diff] [blame] | 85 | case AMDGPU_HW_IP_UVD_ENC: |
| 86 | if (ring < adev->uvd.num_enc_rings){ |
| 87 | *out_ring = &adev->uvd.ring_enc[ring]; |
| 88 | } else { |
| 89 | DRM_ERROR("only %d UVD ENC rings are supported\n", |
| 90 | adev->uvd.num_enc_rings); |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | break; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 94 | } |
Ding Pixel | c5f21c9 | 2017-01-18 17:26:38 +0800 | [diff] [blame] | 95 | |
| 96 | if (!(*out_ring && (*out_ring)->adev)) { |
| 97 | DRM_ERROR("Ring %d is not initialized on IP %d\n", |
| 98 | ring, ip_type); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 105 | static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p, |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 106 | struct drm_amdgpu_cs_chunk_fence *data, |
| 107 | uint32_t *offset) |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 108 | { |
| 109 | struct drm_gem_object *gobj; |
Christian König | aa29040 | 2016-09-09 11:21:43 +0200 | [diff] [blame] | 110 | unsigned long size; |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 111 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 112 | gobj = drm_gem_object_lookup(p->filp, data->handle); |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 113 | if (gobj == NULL) |
| 114 | return -EINVAL; |
| 115 | |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 116 | p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj)); |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 117 | p->uf_entry.priority = 0; |
| 118 | p->uf_entry.tv.bo = &p->uf_entry.robj->tbo; |
| 119 | p->uf_entry.tv.shared = true; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 120 | p->uf_entry.user_pages = NULL; |
Christian König | aa29040 | 2016-09-09 11:21:43 +0200 | [diff] [blame] | 121 | |
| 122 | size = amdgpu_bo_size(p->uf_entry.robj); |
| 123 | if (size != PAGE_SIZE || (data->offset + 8) > size) |
| 124 | return -EINVAL; |
| 125 | |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 126 | *offset = data->offset; |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 127 | |
| 128 | drm_gem_object_unreference_unlocked(gobj); |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 129 | |
| 130 | if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) { |
| 131 | amdgpu_bo_unref(&p->uf_entry.robj); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 138 | int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data) |
| 139 | { |
Christian König | 4c0b242 | 2016-02-01 11:20:37 +0100 | [diff] [blame] | 140 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
Monk Liu | c563783 | 2016-04-19 20:11:32 +0800 | [diff] [blame] | 141 | struct amdgpu_vm *vm = &fpriv->vm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 142 | union drm_amdgpu_cs *cs = data; |
| 143 | uint64_t *chunk_array_user; |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 144 | uint64_t *chunk_array; |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 145 | unsigned size, num_ibs = 0; |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 146 | uint32_t uf_offset = 0; |
Dan Carpenter | 5431350 | 2015-09-25 14:36:55 +0300 | [diff] [blame] | 147 | int i; |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 148 | int ret; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 149 | |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 150 | if (cs->in.num_chunks == 0) |
| 151 | return 0; |
| 152 | |
| 153 | chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL); |
| 154 | if (!chunk_array) |
| 155 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 156 | |
Christian König | 3cb485f | 2015-05-11 15:34:59 +0200 | [diff] [blame] | 157 | p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id); |
| 158 | if (!p->ctx) { |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 159 | ret = -EINVAL; |
| 160 | goto free_chunk; |
Christian König | 3cb485f | 2015-05-11 15:34:59 +0200 | [diff] [blame] | 161 | } |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 162 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 163 | /* get chunks */ |
Arnd Bergmann | 028423b | 2015-10-07 09:41:27 +0200 | [diff] [blame] | 164 | chunk_array_user = (uint64_t __user *)(unsigned long)(cs->in.chunks); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 165 | if (copy_from_user(chunk_array, chunk_array_user, |
| 166 | sizeof(uint64_t)*cs->in.num_chunks)) { |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 167 | ret = -EFAULT; |
Christian König | 2a7d9bd | 2015-12-18 20:33:52 +0100 | [diff] [blame] | 168 | goto put_ctx; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | p->nchunks = cs->in.num_chunks; |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 172 | p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk), |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 173 | GFP_KERNEL); |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 174 | if (!p->chunks) { |
| 175 | ret = -ENOMEM; |
Christian König | 2a7d9bd | 2015-12-18 20:33:52 +0100 | [diff] [blame] | 176 | goto put_ctx; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | for (i = 0; i < p->nchunks; i++) { |
| 180 | struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL; |
| 181 | struct drm_amdgpu_cs_chunk user_chunk; |
| 182 | uint32_t __user *cdata; |
| 183 | |
Arnd Bergmann | 028423b | 2015-10-07 09:41:27 +0200 | [diff] [blame] | 184 | chunk_ptr = (void __user *)(unsigned long)chunk_array[i]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 185 | if (copy_from_user(&user_chunk, chunk_ptr, |
| 186 | sizeof(struct drm_amdgpu_cs_chunk))) { |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 187 | ret = -EFAULT; |
| 188 | i--; |
| 189 | goto free_partial_kdata; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 190 | } |
| 191 | p->chunks[i].chunk_id = user_chunk.chunk_id; |
| 192 | p->chunks[i].length_dw = user_chunk.length_dw; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 193 | |
| 194 | size = p->chunks[i].length_dw; |
Arnd Bergmann | 028423b | 2015-10-07 09:41:27 +0200 | [diff] [blame] | 195 | cdata = (void __user *)(unsigned long)user_chunk.chunk_data; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 196 | |
| 197 | p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t)); |
| 198 | if (p->chunks[i].kdata == NULL) { |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 199 | ret = -ENOMEM; |
| 200 | i--; |
| 201 | goto free_partial_kdata; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 202 | } |
| 203 | size *= sizeof(uint32_t); |
| 204 | if (copy_from_user(p->chunks[i].kdata, cdata, size)) { |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 205 | ret = -EFAULT; |
| 206 | goto free_partial_kdata; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 209 | switch (p->chunks[i].chunk_id) { |
| 210 | case AMDGPU_CHUNK_ID_IB: |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 211 | ++num_ibs; |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 212 | break; |
| 213 | |
| 214 | case AMDGPU_CHUNK_ID_FENCE: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 215 | size = sizeof(struct drm_amdgpu_cs_chunk_fence); |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 216 | if (p->chunks[i].length_dw * sizeof(uint32_t) < size) { |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 217 | ret = -EINVAL; |
| 218 | goto free_partial_kdata; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 219 | } |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 220 | |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 221 | ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata, |
| 222 | &uf_offset); |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 223 | if (ret) |
| 224 | goto free_partial_kdata; |
| 225 | |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 226 | break; |
| 227 | |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 228 | case AMDGPU_CHUNK_ID_DEPENDENCIES: |
| 229 | break; |
| 230 | |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 231 | default: |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 232 | ret = -EINVAL; |
| 233 | goto free_partial_kdata; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Monk Liu | c563783 | 2016-04-19 20:11:32 +0800 | [diff] [blame] | 237 | ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm); |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 238 | if (ret) |
Christian König | 4acabfe | 2016-01-31 11:32:04 +0100 | [diff] [blame] | 239 | goto free_all_kdata; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 240 | |
Christian König | b5f5acb | 2016-06-29 13:26:41 +0200 | [diff] [blame] | 241 | if (p->uf_entry.robj) |
| 242 | p->job->uf_addr = uf_offset; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 243 | kfree(chunk_array); |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 244 | return 0; |
| 245 | |
| 246 | free_all_kdata: |
| 247 | i = p->nchunks - 1; |
| 248 | free_partial_kdata: |
| 249 | for (; i >= 0; i--) |
| 250 | drm_free_large(p->chunks[i].kdata); |
| 251 | kfree(p->chunks); |
Dave Airlie | 607523d | 2017-03-10 12:13:04 +1000 | [diff] [blame] | 252 | p->chunks = NULL; |
| 253 | p->nchunks = 0; |
Christian König | 2a7d9bd | 2015-12-18 20:33:52 +0100 | [diff] [blame] | 254 | put_ctx: |
Dan Carpenter | 1d26347 | 2015-09-23 13:59:28 +0300 | [diff] [blame] | 255 | amdgpu_ctx_put(p->ctx); |
| 256 | free_chunk: |
| 257 | kfree(chunk_array); |
| 258 | |
| 259 | return ret; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 262 | /* Convert microseconds to bytes. */ |
| 263 | static u64 us_to_bytes(struct amdgpu_device *adev, s64 us) |
| 264 | { |
| 265 | if (us <= 0 || !adev->mm_stats.log2_max_MBps) |
| 266 | return 0; |
| 267 | |
| 268 | /* Since accum_us is incremented by a million per second, just |
| 269 | * multiply it by the number of MB/s to get the number of bytes. |
| 270 | */ |
| 271 | return us << adev->mm_stats.log2_max_MBps; |
| 272 | } |
| 273 | |
| 274 | static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes) |
| 275 | { |
| 276 | if (!adev->mm_stats.log2_max_MBps) |
| 277 | return 0; |
| 278 | |
| 279 | return bytes >> adev->mm_stats.log2_max_MBps; |
| 280 | } |
| 281 | |
| 282 | /* Returns how many bytes TTM can move right now. If no bytes can be moved, |
| 283 | * it returns 0. If it returns non-zero, it's OK to move at least one buffer, |
| 284 | * which means it can go over the threshold once. If that happens, the driver |
| 285 | * will be in debt and no other buffer migrations can be done until that debt |
| 286 | * is repaid. |
| 287 | * |
| 288 | * This approach allows moving a buffer of any size (it's important to allow |
| 289 | * that). |
| 290 | * |
| 291 | * The currency is simply time in microseconds and it increases as the clock |
| 292 | * ticks. The accumulated microseconds (us) are converted to bytes and |
| 293 | * returned. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 294 | */ |
| 295 | static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev) |
| 296 | { |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 297 | s64 time_us, increment_us; |
| 298 | u64 max_bytes; |
| 299 | u64 free_vram, total_vram, used_vram; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 300 | |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 301 | /* Allow a maximum of 200 accumulated ms. This is basically per-IB |
| 302 | * throttling. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 303 | * |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 304 | * It means that in order to get full max MBps, at least 5 IBs per |
| 305 | * second must be submitted and not more than 200ms apart from each |
| 306 | * other. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 307 | */ |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 308 | const s64 us_upper_bound = 200000; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 309 | |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 310 | if (!adev->mm_stats.log2_max_MBps) |
| 311 | return 0; |
| 312 | |
| 313 | total_vram = adev->mc.real_vram_size - adev->vram_pin_size; |
| 314 | used_vram = atomic64_read(&adev->vram_usage); |
| 315 | free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram; |
| 316 | |
| 317 | spin_lock(&adev->mm_stats.lock); |
| 318 | |
| 319 | /* Increase the amount of accumulated us. */ |
| 320 | time_us = ktime_to_us(ktime_get()); |
| 321 | increment_us = time_us - adev->mm_stats.last_update_us; |
| 322 | adev->mm_stats.last_update_us = time_us; |
| 323 | adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us, |
| 324 | us_upper_bound); |
| 325 | |
| 326 | /* This prevents the short period of low performance when the VRAM |
| 327 | * usage is low and the driver is in debt or doesn't have enough |
| 328 | * accumulated us to fill VRAM quickly. |
| 329 | * |
| 330 | * The situation can occur in these cases: |
| 331 | * - a lot of VRAM is freed by userspace |
| 332 | * - the presence of a big buffer causes a lot of evictions |
| 333 | * (solution: split buffers into smaller ones) |
| 334 | * |
| 335 | * If 128 MB or 1/8th of VRAM is free, start filling it now by setting |
| 336 | * accum_us to a positive number. |
| 337 | */ |
| 338 | if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) { |
| 339 | s64 min_us; |
| 340 | |
| 341 | /* Be more aggresive on dGPUs. Try to fill a portion of free |
| 342 | * VRAM now. |
| 343 | */ |
| 344 | if (!(adev->flags & AMD_IS_APU)) |
| 345 | min_us = bytes_to_us(adev, free_vram / 4); |
| 346 | else |
| 347 | min_us = 0; /* Reset accum_us on APUs. */ |
| 348 | |
| 349 | adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us); |
| 350 | } |
| 351 | |
| 352 | /* This returns 0 if the driver is in debt to disallow (optional) |
| 353 | * buffer moves. |
| 354 | */ |
| 355 | max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us); |
| 356 | |
| 357 | spin_unlock(&adev->mm_stats.lock); |
| 358 | return max_bytes; |
| 359 | } |
| 360 | |
| 361 | /* Report how many bytes have really been moved for the last command |
| 362 | * submission. This can result in a debt that can stop buffer migrations |
| 363 | * temporarily. |
| 364 | */ |
Samuel Pitoiset | fad0612 | 2017-02-09 11:33:37 +0100 | [diff] [blame] | 365 | void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes) |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 366 | { |
| 367 | spin_lock(&adev->mm_stats.lock); |
| 368 | adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes); |
| 369 | spin_unlock(&adev->mm_stats.lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 372 | static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p, |
| 373 | struct amdgpu_bo *bo) |
| 374 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 375 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 376 | u64 initial_bytes_moved; |
| 377 | uint32_t domain; |
| 378 | int r; |
| 379 | |
| 380 | if (bo->pin_count) |
| 381 | return 0; |
| 382 | |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 383 | /* Don't move this buffer if we have depleted our allowance |
| 384 | * to move it. Don't move anything if the threshold is zero. |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 385 | */ |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 386 | if (p->bytes_moved < p->bytes_moved_threshold) |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 387 | domain = bo->prefered_domains; |
| 388 | else |
| 389 | domain = bo->allowed_domains; |
| 390 | |
| 391 | retry: |
| 392 | amdgpu_ttm_placement_from_domain(bo, domain); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 393 | initial_bytes_moved = atomic64_read(&adev->num_bytes_moved); |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 394 | r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 395 | p->bytes_moved += atomic64_read(&adev->num_bytes_moved) - |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 396 | initial_bytes_moved; |
| 397 | |
Christian König | 1abdc3d | 2016-08-31 17:28:11 +0200 | [diff] [blame] | 398 | if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) { |
| 399 | domain = bo->allowed_domains; |
| 400 | goto retry; |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | return r; |
| 404 | } |
| 405 | |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 406 | /* Last resort, try to evict something from the current working set */ |
| 407 | static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p, |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 408 | struct amdgpu_bo *validated) |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 409 | { |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 410 | uint32_t domain = validated->allowed_domains; |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 411 | int r; |
| 412 | |
| 413 | if (!p->evictable) |
| 414 | return false; |
| 415 | |
| 416 | for (;&p->evictable->tv.head != &p->validated; |
| 417 | p->evictable = list_prev_entry(p->evictable, tv.head)) { |
| 418 | |
| 419 | struct amdgpu_bo_list_entry *candidate = p->evictable; |
| 420 | struct amdgpu_bo *bo = candidate->robj; |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 421 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 422 | u64 initial_bytes_moved; |
| 423 | uint32_t other; |
| 424 | |
| 425 | /* If we reached our current BO we can forget it */ |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 426 | if (candidate->robj == validated) |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
| 430 | |
| 431 | /* Check if this BO is in one of the domains we need space for */ |
| 432 | if (!(other & domain)) |
| 433 | continue; |
| 434 | |
| 435 | /* Check if we can move this BO somewhere else */ |
| 436 | other = bo->allowed_domains & ~domain; |
| 437 | if (!other) |
| 438 | continue; |
| 439 | |
| 440 | /* Good we can try to move this BO somewhere else */ |
| 441 | amdgpu_ttm_placement_from_domain(bo, other); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 442 | initial_bytes_moved = atomic64_read(&adev->num_bytes_moved); |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 443 | r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 444 | p->bytes_moved += atomic64_read(&adev->num_bytes_moved) - |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 445 | initial_bytes_moved; |
| 446 | |
| 447 | if (unlikely(r)) |
| 448 | break; |
| 449 | |
| 450 | p->evictable = list_prev_entry(p->evictable, tv.head); |
| 451 | list_move(&candidate->tv.head, &p->validated); |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 459 | static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo) |
| 460 | { |
| 461 | struct amdgpu_cs_parser *p = param; |
| 462 | int r; |
| 463 | |
| 464 | do { |
| 465 | r = amdgpu_cs_bo_validate(p, bo); |
| 466 | } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo)); |
| 467 | if (r) |
| 468 | return r; |
| 469 | |
| 470 | if (bo->shadow) |
Alex Xie | 1cd99a8 | 2016-11-30 17:19:40 -0500 | [diff] [blame] | 471 | r = amdgpu_cs_bo_validate(p, bo->shadow); |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 472 | |
| 473 | return r; |
| 474 | } |
| 475 | |
Baoyou Xie | 761c2e8 | 2016-09-03 13:57:14 +0800 | [diff] [blame] | 476 | static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p, |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 477 | struct list_head *validated) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 478 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 479 | struct amdgpu_bo_list_entry *lobj; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 480 | int r; |
| 481 | |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 482 | list_for_each_entry(lobj, validated, tv.head) { |
Christian König | 36409d12 | 2015-12-21 20:31:35 +0100 | [diff] [blame] | 483 | struct amdgpu_bo *bo = lobj->robj; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 484 | bool binding_userptr = false; |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 485 | struct mm_struct *usermm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 486 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 487 | usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm); |
| 488 | if (usermm && usermm != current->mm) |
| 489 | return -EPERM; |
| 490 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 491 | /* Check if we have user pages and nobody bound the BO already */ |
| 492 | if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) { |
| 493 | size_t size = sizeof(struct page *); |
| 494 | |
| 495 | size *= bo->tbo.ttm->num_pages; |
| 496 | memcpy(bo->tbo.ttm->pages, lobj->user_pages, size); |
| 497 | binding_userptr = true; |
| 498 | } |
| 499 | |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 500 | if (p->evictable == lobj) |
| 501 | p->evictable = NULL; |
| 502 | |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 503 | r = amdgpu_cs_validate(p, bo); |
Chunming Zhou | 14fd833 | 2016-08-04 13:05:46 +0800 | [diff] [blame] | 504 | if (r) |
Christian König | 36409d12 | 2015-12-21 20:31:35 +0100 | [diff] [blame] | 505 | return r; |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 506 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 507 | if (binding_userptr) { |
| 508 | drm_free_large(lobj->user_pages); |
| 509 | lobj->user_pages = NULL; |
| 510 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 511 | } |
| 512 | return 0; |
| 513 | } |
| 514 | |
Christian König | 2a7d9bd | 2015-12-18 20:33:52 +0100 | [diff] [blame] | 515 | static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, |
| 516 | union drm_amdgpu_cs *cs) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 517 | { |
| 518 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 519 | struct amdgpu_bo_list_entry *e; |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 520 | struct list_head duplicates; |
monk.liu | 840d514 | 2015-04-27 15:19:20 +0800 | [diff] [blame] | 521 | bool need_mmap_lock = false; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 522 | unsigned i, tries = 10; |
Christian König | 636ce25 | 2015-12-18 21:26:47 +0100 | [diff] [blame] | 523 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 524 | |
Christian König | 2a7d9bd | 2015-12-18 20:33:52 +0100 | [diff] [blame] | 525 | INIT_LIST_HEAD(&p->validated); |
| 526 | |
| 527 | p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle); |
monk.liu | 840d514 | 2015-04-27 15:19:20 +0800 | [diff] [blame] | 528 | if (p->bo_list) { |
Christian König | 211dff5 | 2016-02-22 15:40:59 +0100 | [diff] [blame] | 529 | need_mmap_lock = p->bo_list->first_userptr != |
| 530 | p->bo_list->num_entries; |
Christian König | 636ce25 | 2015-12-18 21:26:47 +0100 | [diff] [blame] | 531 | amdgpu_bo_list_get_list(p->bo_list, &p->validated); |
monk.liu | 840d514 | 2015-04-27 15:19:20 +0800 | [diff] [blame] | 532 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 533 | |
Christian König | 3c0eea6 | 2015-12-11 14:39:05 +0100 | [diff] [blame] | 534 | INIT_LIST_HEAD(&duplicates); |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 535 | amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 536 | |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 537 | if (p->uf_entry.robj) |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 538 | list_add(&p->uf_entry.tv.head, &p->validated); |
| 539 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 540 | if (need_mmap_lock) |
| 541 | down_read(¤t->mm->mmap_sem); |
| 542 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 543 | while (1) { |
| 544 | struct list_head need_pages; |
| 545 | unsigned i; |
| 546 | |
| 547 | r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, |
| 548 | &duplicates); |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 549 | if (unlikely(r != 0)) { |
jimqu | 57d7f9b | 2016-10-20 14:58:04 +0800 | [diff] [blame] | 550 | if (r != -ERESTARTSYS) |
| 551 | DRM_ERROR("ttm_eu_reserve_buffers failed.\n"); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 552 | goto error_free_pages; |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 553 | } |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 554 | |
| 555 | /* Without a BO list we don't have userptr BOs */ |
| 556 | if (!p->bo_list) |
| 557 | break; |
| 558 | |
| 559 | INIT_LIST_HEAD(&need_pages); |
| 560 | for (i = p->bo_list->first_userptr; |
| 561 | i < p->bo_list->num_entries; ++i) { |
| 562 | |
| 563 | e = &p->bo_list->array[i]; |
| 564 | |
| 565 | if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm, |
| 566 | &e->user_invalidated) && e->user_pages) { |
| 567 | |
| 568 | /* We acquired a page array, but somebody |
| 569 | * invalidated it. Free it an try again |
| 570 | */ |
| 571 | release_pages(e->user_pages, |
| 572 | e->robj->tbo.ttm->num_pages, |
| 573 | false); |
| 574 | drm_free_large(e->user_pages); |
| 575 | e->user_pages = NULL; |
| 576 | } |
| 577 | |
| 578 | if (e->robj->tbo.ttm->state != tt_bound && |
| 579 | !e->user_pages) { |
| 580 | list_del(&e->tv.head); |
| 581 | list_add(&e->tv.head, &need_pages); |
| 582 | |
| 583 | amdgpu_bo_unreserve(e->robj); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (list_empty(&need_pages)) |
| 588 | break; |
| 589 | |
| 590 | /* Unreserve everything again. */ |
| 591 | ttm_eu_backoff_reservation(&p->ticket, &p->validated); |
| 592 | |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 593 | /* We tried too many times, just abort */ |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 594 | if (!--tries) { |
| 595 | r = -EDEADLK; |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 596 | DRM_ERROR("deadlock in %s\n", __func__); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 597 | goto error_free_pages; |
| 598 | } |
| 599 | |
| 600 | /* Fill the page arrays for all useptrs. */ |
| 601 | list_for_each_entry(e, &need_pages, tv.head) { |
| 602 | struct ttm_tt *ttm = e->robj->tbo.ttm; |
| 603 | |
| 604 | e->user_pages = drm_calloc_large(ttm->num_pages, |
| 605 | sizeof(struct page*)); |
| 606 | if (!e->user_pages) { |
| 607 | r = -ENOMEM; |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 608 | DRM_ERROR("calloc failure in %s\n", __func__); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 609 | goto error_free_pages; |
| 610 | } |
| 611 | |
| 612 | r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages); |
| 613 | if (r) { |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 614 | DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n"); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 615 | drm_free_large(e->user_pages); |
| 616 | e->user_pages = NULL; |
| 617 | goto error_free_pages; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /* And try again. */ |
| 622 | list_splice(&need_pages, &p->validated); |
| 623 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 624 | |
Christian König | f69f90a1 | 2015-12-21 19:47:42 +0100 | [diff] [blame] | 625 | p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev); |
| 626 | p->bytes_moved = 0; |
Christian König | 662bfa6 | 2016-09-01 12:13:18 +0200 | [diff] [blame] | 627 | p->evictable = list_last_entry(&p->validated, |
| 628 | struct amdgpu_bo_list_entry, |
| 629 | tv.head); |
Christian König | f69f90a1 | 2015-12-21 19:47:42 +0100 | [diff] [blame] | 630 | |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 631 | r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm, |
| 632 | amdgpu_cs_validate, p); |
| 633 | if (r) { |
| 634 | DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n"); |
| 635 | goto error_validate; |
| 636 | } |
| 637 | |
Christian König | f69f90a1 | 2015-12-21 19:47:42 +0100 | [diff] [blame] | 638 | r = amdgpu_cs_list_validate(p, &duplicates); |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 639 | if (r) { |
| 640 | DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n"); |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 641 | goto error_validate; |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 642 | } |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 643 | |
Christian König | f69f90a1 | 2015-12-21 19:47:42 +0100 | [diff] [blame] | 644 | r = amdgpu_cs_list_validate(p, &p->validated); |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 645 | if (r) { |
| 646 | DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n"); |
Christian König | a848030 | 2016-01-05 16:03:39 +0100 | [diff] [blame] | 647 | goto error_validate; |
Marek Olšák | f103795 | 2016-07-30 00:48:39 +0200 | [diff] [blame] | 648 | } |
Christian König | a848030 | 2016-01-05 16:03:39 +0100 | [diff] [blame] | 649 | |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 650 | amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved); |
| 651 | |
Christian König | 5a712a8 | 2016-06-21 16:28:15 +0200 | [diff] [blame] | 652 | fpriv->vm.last_eviction_counter = |
| 653 | atomic64_read(&p->adev->num_evictions); |
| 654 | |
Christian König | a848030 | 2016-01-05 16:03:39 +0100 | [diff] [blame] | 655 | if (p->bo_list) { |
Christian König | d88bf58 | 2016-05-06 17:50:03 +0200 | [diff] [blame] | 656 | struct amdgpu_bo *gds = p->bo_list->gds_obj; |
| 657 | struct amdgpu_bo *gws = p->bo_list->gws_obj; |
| 658 | struct amdgpu_bo *oa = p->bo_list->oa_obj; |
Christian König | a848030 | 2016-01-05 16:03:39 +0100 | [diff] [blame] | 659 | struct amdgpu_vm *vm = &fpriv->vm; |
| 660 | unsigned i; |
| 661 | |
| 662 | for (i = 0; i < p->bo_list->num_entries; i++) { |
| 663 | struct amdgpu_bo *bo = p->bo_list->array[i].robj; |
| 664 | |
| 665 | p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo); |
| 666 | } |
Christian König | d88bf58 | 2016-05-06 17:50:03 +0200 | [diff] [blame] | 667 | |
| 668 | if (gds) { |
| 669 | p->job->gds_base = amdgpu_bo_gpu_offset(gds); |
| 670 | p->job->gds_size = amdgpu_bo_size(gds); |
| 671 | } |
| 672 | if (gws) { |
| 673 | p->job->gws_base = amdgpu_bo_gpu_offset(gws); |
| 674 | p->job->gws_size = amdgpu_bo_size(gws); |
| 675 | } |
| 676 | if (oa) { |
| 677 | p->job->oa_base = amdgpu_bo_gpu_offset(oa); |
| 678 | p->job->oa_size = amdgpu_bo_size(oa); |
| 679 | } |
Christian König | a848030 | 2016-01-05 16:03:39 +0100 | [diff] [blame] | 680 | } |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 681 | |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 682 | if (!r && p->uf_entry.robj) { |
| 683 | struct amdgpu_bo *uf = p->uf_entry.robj; |
| 684 | |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 685 | r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem); |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 686 | p->job->uf_addr += amdgpu_bo_gpu_offset(uf); |
| 687 | } |
Christian König | b5f5acb | 2016-06-29 13:26:41 +0200 | [diff] [blame] | 688 | |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 689 | error_validate: |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 690 | if (r) { |
| 691 | amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm); |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 692 | ttm_eu_backoff_reservation(&p->ticket, &p->validated); |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 693 | } |
Christian König | a5b7505 | 2015-09-03 16:40:39 +0200 | [diff] [blame] | 694 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 695 | error_free_pages: |
| 696 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 697 | if (need_mmap_lock) |
| 698 | up_read(¤t->mm->mmap_sem); |
| 699 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 700 | if (p->bo_list) { |
| 701 | for (i = p->bo_list->first_userptr; |
| 702 | i < p->bo_list->num_entries; ++i) { |
| 703 | e = &p->bo_list->array[i]; |
| 704 | |
| 705 | if (!e->user_pages) |
| 706 | continue; |
| 707 | |
| 708 | release_pages(e->user_pages, |
| 709 | e->robj->tbo.ttm->num_pages, |
| 710 | false); |
| 711 | drm_free_large(e->user_pages); |
| 712 | } |
| 713 | } |
| 714 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 715 | return r; |
| 716 | } |
| 717 | |
| 718 | static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p) |
| 719 | { |
| 720 | struct amdgpu_bo_list_entry *e; |
| 721 | int r; |
| 722 | |
| 723 | list_for_each_entry(e, &p->validated, tv.head) { |
| 724 | struct reservation_object *resv = e->robj->tbo.resv; |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 725 | r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 726 | |
| 727 | if (r) |
| 728 | return r; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
Christian König | 984810f | 2015-11-14 21:05:35 +0100 | [diff] [blame] | 733 | /** |
| 734 | * cs_parser_fini() - clean parser states |
| 735 | * @parser: parser structure holding parsing context. |
| 736 | * @error: error number |
| 737 | * |
| 738 | * If error is set than unvalidate buffer, otherwise just free memory |
| 739 | * used by parsing context. |
| 740 | **/ |
| 741 | static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff) |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 742 | { |
Christian König | eceb8a1 | 2016-01-11 15:35:21 +0100 | [diff] [blame] | 743 | struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; |
Christian König | 984810f | 2015-11-14 21:05:35 +0100 | [diff] [blame] | 744 | unsigned i; |
| 745 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 746 | if (!error) { |
Nicolai Hähnle | 28b8d66 | 2016-01-27 11:04:19 -0500 | [diff] [blame] | 747 | amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm); |
| 748 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 749 | ttm_eu_fence_buffer_objects(&parser->ticket, |
Christian König | 984810f | 2015-11-14 21:05:35 +0100 | [diff] [blame] | 750 | &parser->validated, |
| 751 | parser->fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 752 | } else if (backoff) { |
| 753 | ttm_eu_backoff_reservation(&parser->ticket, |
| 754 | &parser->validated); |
| 755 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 756 | dma_fence_put(parser->fence); |
Christian König | 7e52a81 | 2015-11-04 15:44:39 +0100 | [diff] [blame] | 757 | |
Christian König | 3cb485f | 2015-05-11 15:34:59 +0200 | [diff] [blame] | 758 | if (parser->ctx) |
| 759 | amdgpu_ctx_put(parser->ctx); |
Chunming Zhou | a3348bb | 2015-08-18 16:25:46 +0800 | [diff] [blame] | 760 | if (parser->bo_list) |
| 761 | amdgpu_bo_list_put(parser->bo_list); |
| 762 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 763 | for (i = 0; i < parser->nchunks; i++) |
| 764 | drm_free_large(parser->chunks[i].kdata); |
| 765 | kfree(parser->chunks); |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 766 | if (parser->job) |
| 767 | amdgpu_job_free(parser->job); |
Christian König | 91acbeb | 2015-12-14 16:42:31 +0100 | [diff] [blame] | 768 | amdgpu_bo_unref(&parser->uf_entry.robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 769 | } |
| 770 | |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 771 | static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 772 | { |
| 773 | struct amdgpu_device *adev = p->adev; |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 774 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
| 775 | struct amdgpu_vm *vm = &fpriv->vm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 776 | struct amdgpu_bo_va *bo_va; |
| 777 | struct amdgpu_bo *bo; |
| 778 | int i, r; |
| 779 | |
| 780 | r = amdgpu_vm_update_page_directory(adev, vm); |
| 781 | if (r) |
| 782 | return r; |
| 783 | |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 784 | r = amdgpu_sync_fence(adev, &p->job->sync, vm->page_directory_fence); |
Bas Nieuwenhuizen | 05906de | 2015-08-14 20:08:40 +0200 | [diff] [blame] | 785 | if (r) |
| 786 | return r; |
| 787 | |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame^] | 788 | r = amdgpu_vm_clear_freed(adev, vm, NULL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 789 | if (r) |
| 790 | return r; |
| 791 | |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 792 | r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false); |
| 793 | if (r) |
| 794 | return r; |
| 795 | |
| 796 | r = amdgpu_sync_fence(adev, &p->job->sync, |
| 797 | fpriv->prt_va->last_pt_update); |
| 798 | if (r) |
| 799 | return r; |
| 800 | |
Monk Liu | 2493664 | 2017-01-09 15:54:32 +0800 | [diff] [blame] | 801 | if (amdgpu_sriov_vf(adev)) { |
| 802 | struct dma_fence *f; |
| 803 | bo_va = vm->csa_bo_va; |
| 804 | BUG_ON(!bo_va); |
| 805 | r = amdgpu_vm_bo_update(adev, bo_va, false); |
| 806 | if (r) |
| 807 | return r; |
| 808 | |
| 809 | f = bo_va->last_pt_update; |
| 810 | r = amdgpu_sync_fence(adev, &p->job->sync, f); |
| 811 | if (r) |
| 812 | return r; |
| 813 | } |
| 814 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 815 | if (p->bo_list) { |
| 816 | for (i = 0; i < p->bo_list->num_entries; i++) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 817 | struct dma_fence *f; |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 818 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 819 | /* ignore duplicates */ |
| 820 | bo = p->bo_list->array[i].robj; |
| 821 | if (!bo) |
| 822 | continue; |
| 823 | |
| 824 | bo_va = p->bo_list->array[i].bo_va; |
| 825 | if (bo_va == NULL) |
| 826 | continue; |
| 827 | |
Christian König | 99e124f | 2016-08-16 14:43:17 +0200 | [diff] [blame] | 828 | r = amdgpu_vm_bo_update(adev, bo_va, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 829 | if (r) |
| 830 | return r; |
| 831 | |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 832 | f = bo_va->last_pt_update; |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 833 | r = amdgpu_sync_fence(adev, &p->job->sync, f); |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 834 | if (r) |
| 835 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 836 | } |
Christian König | b495bd3 | 2015-09-10 14:00:35 +0200 | [diff] [blame] | 837 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 838 | } |
| 839 | |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 840 | r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync); |
Christian König | b495bd3 | 2015-09-10 14:00:35 +0200 | [diff] [blame] | 841 | |
| 842 | if (amdgpu_vm_debug && p->bo_list) { |
| 843 | /* Invalidate all BOs to test for userspace bugs */ |
| 844 | for (i = 0; i < p->bo_list->num_entries; i++) { |
| 845 | /* ignore duplicates */ |
| 846 | bo = p->bo_list->array[i].robj; |
| 847 | if (!bo) |
| 848 | continue; |
| 849 | |
| 850 | amdgpu_vm_bo_invalidate(adev, bo); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev, |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 858 | struct amdgpu_cs_parser *p) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 859 | { |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 860 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 861 | struct amdgpu_vm *vm = &fpriv->vm; |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 862 | struct amdgpu_ring *ring = p->job->ring; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 863 | int i, r; |
| 864 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 865 | /* Only for UVD/VCE VM emulation */ |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 866 | if (ring->funcs->parse_cs) { |
| 867 | for (i = 0; i < p->job->num_ibs; i++) { |
| 868 | r = amdgpu_ring_parse_cs(ring, p, i); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 869 | if (r) |
| 870 | return r; |
| 871 | } |
Christian König | 45088ef | 2016-10-05 16:49:19 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (p->job->vm) { |
Christian König | 9a79588 | 2016-06-22 14:25:55 +0200 | [diff] [blame] | 875 | p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->page_directory); |
| 876 | |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 877 | r = amdgpu_bo_vm_update_pte(p); |
Christian König | 9a79588 | 2016-06-22 14:25:55 +0200 | [diff] [blame] | 878 | if (r) |
| 879 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 880 | } |
| 881 | |
Christian König | 9a79588 | 2016-06-22 14:25:55 +0200 | [diff] [blame] | 882 | return amdgpu_cs_sync_rings(p); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 883 | } |
| 884 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 885 | static int amdgpu_cs_ib_fill(struct amdgpu_device *adev, |
| 886 | struct amdgpu_cs_parser *parser) |
| 887 | { |
| 888 | struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; |
| 889 | struct amdgpu_vm *vm = &fpriv->vm; |
| 890 | int i, j; |
| 891 | int r; |
| 892 | |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 893 | for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 894 | struct amdgpu_cs_chunk *chunk; |
| 895 | struct amdgpu_ib *ib; |
| 896 | struct drm_amdgpu_cs_chunk_ib *chunk_ib; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 897 | struct amdgpu_ring *ring; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 898 | |
| 899 | chunk = &parser->chunks[i]; |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 900 | ib = &parser->job->ibs[j]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 901 | chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata; |
| 902 | |
| 903 | if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB) |
| 904 | continue; |
| 905 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 906 | r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type, |
| 907 | chunk_ib->ip_instance, chunk_ib->ring, |
| 908 | &ring); |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 909 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 910 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 911 | |
Monk Liu | 753ad49 | 2016-08-26 13:28:28 +0800 | [diff] [blame] | 912 | if (ib->flags & AMDGPU_IB_FLAG_PREAMBLE) { |
| 913 | parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT; |
| 914 | if (!parser->ctx->preamble_presented) { |
| 915 | parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST; |
| 916 | parser->ctx->preamble_presented = true; |
| 917 | } |
| 918 | } |
| 919 | |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 920 | if (parser->job->ring && parser->job->ring != ring) |
| 921 | return -EINVAL; |
| 922 | |
| 923 | parser->job->ring = ring; |
| 924 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 925 | if (ring->funcs->parse_cs) { |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 926 | struct amdgpu_bo_va_mapping *m; |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 927 | struct amdgpu_bo *aobj = NULL; |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 928 | uint64_t offset; |
| 929 | uint8_t *kptr; |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 930 | |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 931 | m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start, |
| 932 | &aobj); |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 933 | if (!aobj) { |
| 934 | DRM_ERROR("IB va_start is invalid\n"); |
| 935 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 936 | } |
| 937 | |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 938 | if ((chunk_ib->va_start + chunk_ib->ib_bytes) > |
| 939 | (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) { |
| 940 | DRM_ERROR("IB va_start+ib_bytes is invalid\n"); |
| 941 | return -EINVAL; |
| 942 | } |
| 943 | |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 944 | /* the IB should be reserved at this point */ |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 945 | r = amdgpu_bo_kmap(aobj, (void **)&kptr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 946 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 947 | return r; |
| 948 | } |
| 949 | |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 950 | offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE; |
| 951 | kptr += chunk_ib->va_start - offset; |
| 952 | |
Christian König | 45088ef | 2016-10-05 16:49:19 +0200 | [diff] [blame] | 953 | r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 954 | if (r) { |
| 955 | DRM_ERROR("Failed to get ib !\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 956 | return r; |
| 957 | } |
| 958 | |
| 959 | memcpy(ib->ptr, kptr, chunk_ib->ib_bytes); |
| 960 | amdgpu_bo_kunmap(aobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 961 | } else { |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 962 | r = amdgpu_ib_get(adev, vm, 0, ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 963 | if (r) { |
| 964 | DRM_ERROR("Failed to get ib !\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 965 | return r; |
| 966 | } |
| 967 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 968 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 969 | |
Christian König | 45088ef | 2016-10-05 16:49:19 +0200 | [diff] [blame] | 970 | ib->gpu_addr = chunk_ib->va_start; |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 971 | ib->length_dw = chunk_ib->ib_bytes / 4; |
Jammy Zhou | de807f8 | 2015-05-11 23:41:41 +0800 | [diff] [blame] | 972 | ib->flags = chunk_ib->flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 973 | j++; |
| 974 | } |
| 975 | |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 976 | /* UVD & VCE fw doesn't support user fences */ |
Christian König | b5f5acb | 2016-06-29 13:26:41 +0200 | [diff] [blame] | 977 | if (parser->job->uf_addr && ( |
Christian König | 21cd942 | 2016-10-05 15:36:39 +0200 | [diff] [blame] | 978 | parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD || |
| 979 | parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE)) |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 980 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 985 | static int amdgpu_cs_dependencies(struct amdgpu_device *adev, |
| 986 | struct amdgpu_cs_parser *p) |
| 987 | { |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 988 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 989 | int i, j, r; |
| 990 | |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 991 | for (i = 0; i < p->nchunks; ++i) { |
| 992 | struct drm_amdgpu_cs_chunk_dep *deps; |
| 993 | struct amdgpu_cs_chunk *chunk; |
| 994 | unsigned num_deps; |
| 995 | |
| 996 | chunk = &p->chunks[i]; |
| 997 | |
| 998 | if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES) |
| 999 | continue; |
| 1000 | |
| 1001 | deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata; |
| 1002 | num_deps = chunk->length_dw * 4 / |
| 1003 | sizeof(struct drm_amdgpu_cs_chunk_dep); |
| 1004 | |
| 1005 | for (j = 0; j < num_deps; ++j) { |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 1006 | struct amdgpu_ring *ring; |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 1007 | struct amdgpu_ctx *ctx; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1008 | struct dma_fence *fence; |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 1009 | |
| 1010 | r = amdgpu_cs_get_ring(adev, deps[j].ip_type, |
| 1011 | deps[j].ip_instance, |
| 1012 | deps[j].ring, &ring); |
| 1013 | if (r) |
| 1014 | return r; |
| 1015 | |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 1016 | ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id); |
| 1017 | if (ctx == NULL) |
| 1018 | return -EINVAL; |
| 1019 | |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 1020 | fence = amdgpu_ctx_get_fence(ctx, ring, |
| 1021 | deps[j].handle); |
| 1022 | if (IS_ERR(fence)) { |
| 1023 | r = PTR_ERR(fence); |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 1024 | amdgpu_ctx_put(ctx); |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 1025 | return r; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 1026 | |
| 1027 | } else if (fence) { |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 1028 | r = amdgpu_sync_fence(adev, &p->job->sync, |
| 1029 | fence); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1030 | dma_fence_put(fence); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 1031 | amdgpu_ctx_put(ctx); |
| 1032 | if (r) |
| 1033 | return r; |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 1034 | } |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Christian König | cd75dc6 | 2016-01-31 11:30:55 +0100 | [diff] [blame] | 1041 | static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, |
| 1042 | union drm_amdgpu_cs *cs) |
| 1043 | { |
Christian König | b07c60c | 2016-01-31 12:29:04 +0100 | [diff] [blame] | 1044 | struct amdgpu_ring *ring = p->job->ring; |
Christian König | 92f2509 | 2016-05-06 15:57:42 +0200 | [diff] [blame] | 1045 | struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity; |
Christian König | cd75dc6 | 2016-01-31 11:30:55 +0100 | [diff] [blame] | 1046 | struct amdgpu_job *job; |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 1047 | int r; |
Christian König | cd75dc6 | 2016-01-31 11:30:55 +0100 | [diff] [blame] | 1048 | |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 1049 | job = p->job; |
| 1050 | p->job = NULL; |
Christian König | cd75dc6 | 2016-01-31 11:30:55 +0100 | [diff] [blame] | 1051 | |
Christian König | 595a9cd | 2016-06-30 10:52:03 +0200 | [diff] [blame] | 1052 | r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp); |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 1053 | if (r) { |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1054 | amdgpu_job_free(job); |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 1055 | return r; |
Christian König | cd75dc6 | 2016-01-31 11:30:55 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 1058 | job->owner = p->filp; |
Monk Liu | 3aecd24 | 2016-08-25 15:40:48 +0800 | [diff] [blame] | 1059 | job->fence_ctx = entity->fence_context; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1060 | p->fence = dma_fence_get(&job->base.s_fence->finished); |
Christian König | 595a9cd | 2016-06-30 10:52:03 +0200 | [diff] [blame] | 1061 | cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence); |
Christian König | 758ac17 | 2016-05-06 22:14:00 +0200 | [diff] [blame] | 1062 | job->uf_sequence = cs->out.handle; |
Christian König | a5fb4ec | 2016-06-29 15:10:31 +0200 | [diff] [blame] | 1063 | amdgpu_job_free_resources(job); |
Christian König | cd75dc6 | 2016-01-31 11:30:55 +0100 | [diff] [blame] | 1064 | |
| 1065 | trace_amdgpu_cs_ioctl(job); |
| 1066 | amd_sched_entity_push_job(&job->base); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 1071 | int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) |
| 1072 | { |
| 1073 | struct amdgpu_device *adev = dev->dev_private; |
| 1074 | union drm_amdgpu_cs *cs = data; |
Christian König | 7e52a81 | 2015-11-04 15:44:39 +0100 | [diff] [blame] | 1075 | struct amdgpu_cs_parser parser = {}; |
Christian König | 26a6980 | 2015-08-18 21:09:33 +0200 | [diff] [blame] | 1076 | bool reserved_buffers = false; |
| 1077 | int i, r; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 1078 | |
Christian König | 0c418f1 | 2015-09-01 15:13:53 +0200 | [diff] [blame] | 1079 | if (!adev->accel_working) |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 1080 | return -EBUSY; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 1081 | |
Christian König | 7e52a81 | 2015-11-04 15:44:39 +0100 | [diff] [blame] | 1082 | parser.adev = adev; |
| 1083 | parser.filp = filp; |
| 1084 | |
| 1085 | r = amdgpu_cs_parser_init(&parser, data); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1086 | if (r) { |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 1087 | DRM_ERROR("Failed to initialize parser !\n"); |
Huang Rui | a414cd7 | 2016-10-30 23:05:47 +0800 | [diff] [blame] | 1088 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1089 | } |
Huang Rui | a414cd7 | 2016-10-30 23:05:47 +0800 | [diff] [blame] | 1090 | |
Christian König | 2a7d9bd | 2015-12-18 20:33:52 +0100 | [diff] [blame] | 1091 | r = amdgpu_cs_parser_bos(&parser, data); |
Huang Rui | a414cd7 | 2016-10-30 23:05:47 +0800 | [diff] [blame] | 1092 | if (r) { |
| 1093 | if (r == -ENOMEM) |
| 1094 | DRM_ERROR("Not enough memory for command submission!\n"); |
| 1095 | else if (r != -ERESTARTSYS) |
| 1096 | DRM_ERROR("Failed to process the buffer list %d!\n", r); |
| 1097 | goto out; |
Christian König | 26a6980 | 2015-08-18 21:09:33 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
Huang Rui | a414cd7 | 2016-10-30 23:05:47 +0800 | [diff] [blame] | 1100 | reserved_buffers = true; |
| 1101 | r = amdgpu_cs_ib_fill(adev, &parser); |
Christian König | 26a6980 | 2015-08-18 21:09:33 +0200 | [diff] [blame] | 1102 | if (r) |
| 1103 | goto out; |
| 1104 | |
Huang Rui | a414cd7 | 2016-10-30 23:05:47 +0800 | [diff] [blame] | 1105 | r = amdgpu_cs_dependencies(adev, &parser); |
| 1106 | if (r) { |
| 1107 | DRM_ERROR("Failed in the dependencies handling %d!\n", r); |
| 1108 | goto out; |
| 1109 | } |
| 1110 | |
Christian König | 50838c8 | 2016-02-03 13:44:52 +0100 | [diff] [blame] | 1111 | for (i = 0; i < parser.job->num_ibs; i++) |
Christian König | 7e52a81 | 2015-11-04 15:44:39 +0100 | [diff] [blame] | 1112 | trace_amdgpu_cs(&parser, i); |
Christian König | 26a6980 | 2015-08-18 21:09:33 +0200 | [diff] [blame] | 1113 | |
Christian König | 7e52a81 | 2015-11-04 15:44:39 +0100 | [diff] [blame] | 1114 | r = amdgpu_cs_ib_vm_chunk(adev, &parser); |
Chunming Zhou | 4fe6311 | 2015-08-18 16:12:15 +0800 | [diff] [blame] | 1115 | if (r) |
| 1116 | goto out; |
| 1117 | |
Christian König | 4acabfe | 2016-01-31 11:32:04 +0100 | [diff] [blame] | 1118 | r = amdgpu_cs_submit(&parser, cs); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1119 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1120 | out: |
Christian König | 7e52a81 | 2015-11-04 15:44:39 +0100 | [diff] [blame] | 1121 | amdgpu_cs_parser_fini(&parser, r, reserved_buffers); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1122 | return r; |
| 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * amdgpu_cs_wait_ioctl - wait for a command submission to finish |
| 1127 | * |
| 1128 | * @dev: drm device |
| 1129 | * @data: data from userspace |
| 1130 | * @filp: file private |
| 1131 | * |
| 1132 | * Wait for the command submission identified by handle to finish. |
| 1133 | */ |
| 1134 | int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data, |
| 1135 | struct drm_file *filp) |
| 1136 | { |
| 1137 | union drm_amdgpu_wait_cs *wait = data; |
| 1138 | struct amdgpu_device *adev = dev->dev_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1139 | unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout); |
Christian König | 03507c4 | 2015-06-19 17:00:19 +0200 | [diff] [blame] | 1140 | struct amdgpu_ring *ring = NULL; |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 1141 | struct amdgpu_ctx *ctx; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1142 | struct dma_fence *fence; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1143 | long r; |
| 1144 | |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 1145 | r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance, |
| 1146 | wait->in.ring, &ring); |
| 1147 | if (r) |
| 1148 | return r; |
| 1149 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 1150 | ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id); |
| 1151 | if (ctx == NULL) |
| 1152 | return -EINVAL; |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 1153 | |
| 1154 | fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle); |
| 1155 | if (IS_ERR(fence)) |
| 1156 | r = PTR_ERR(fence); |
| 1157 | else if (fence) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1158 | r = dma_fence_wait_timeout(fence, true, timeout); |
| 1159 | dma_fence_put(fence); |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 1160 | } else |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 1161 | r = 1; |
| 1162 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 1163 | amdgpu_ctx_put(ctx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1164 | if (r < 0) |
| 1165 | return r; |
| 1166 | |
| 1167 | memset(wait, 0, sizeof(*wait)); |
| 1168 | wait->out.status = (r == 0); |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | /** |
Junwei Zhang | eef18a8 | 2016-11-04 16:16:10 -0400 | [diff] [blame] | 1174 | * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence |
| 1175 | * |
| 1176 | * @adev: amdgpu device |
| 1177 | * @filp: file private |
| 1178 | * @user: drm_amdgpu_fence copied from user space |
| 1179 | */ |
| 1180 | static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev, |
| 1181 | struct drm_file *filp, |
| 1182 | struct drm_amdgpu_fence *user) |
| 1183 | { |
| 1184 | struct amdgpu_ring *ring; |
| 1185 | struct amdgpu_ctx *ctx; |
| 1186 | struct dma_fence *fence; |
| 1187 | int r; |
| 1188 | |
| 1189 | r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance, |
| 1190 | user->ring, &ring); |
| 1191 | if (r) |
| 1192 | return ERR_PTR(r); |
| 1193 | |
| 1194 | ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id); |
| 1195 | if (ctx == NULL) |
| 1196 | return ERR_PTR(-EINVAL); |
| 1197 | |
| 1198 | fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no); |
| 1199 | amdgpu_ctx_put(ctx); |
| 1200 | |
| 1201 | return fence; |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * amdgpu_cs_wait_all_fence - wait on all fences to signal |
| 1206 | * |
| 1207 | * @adev: amdgpu device |
| 1208 | * @filp: file private |
| 1209 | * @wait: wait parameters |
| 1210 | * @fences: array of drm_amdgpu_fence |
| 1211 | */ |
| 1212 | static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev, |
| 1213 | struct drm_file *filp, |
| 1214 | union drm_amdgpu_wait_fences *wait, |
| 1215 | struct drm_amdgpu_fence *fences) |
| 1216 | { |
| 1217 | uint32_t fence_count = wait->in.fence_count; |
| 1218 | unsigned int i; |
| 1219 | long r = 1; |
| 1220 | |
| 1221 | for (i = 0; i < fence_count; i++) { |
| 1222 | struct dma_fence *fence; |
| 1223 | unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns); |
| 1224 | |
| 1225 | fence = amdgpu_cs_get_fence(adev, filp, &fences[i]); |
| 1226 | if (IS_ERR(fence)) |
| 1227 | return PTR_ERR(fence); |
| 1228 | else if (!fence) |
| 1229 | continue; |
| 1230 | |
| 1231 | r = dma_fence_wait_timeout(fence, true, timeout); |
| 1232 | if (r < 0) |
| 1233 | return r; |
| 1234 | |
| 1235 | if (r == 0) |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | memset(wait, 0, sizeof(*wait)); |
| 1240 | wait->out.status = (r > 0); |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * amdgpu_cs_wait_any_fence - wait on any fence to signal |
| 1247 | * |
| 1248 | * @adev: amdgpu device |
| 1249 | * @filp: file private |
| 1250 | * @wait: wait parameters |
| 1251 | * @fences: array of drm_amdgpu_fence |
| 1252 | */ |
| 1253 | static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev, |
| 1254 | struct drm_file *filp, |
| 1255 | union drm_amdgpu_wait_fences *wait, |
| 1256 | struct drm_amdgpu_fence *fences) |
| 1257 | { |
| 1258 | unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns); |
| 1259 | uint32_t fence_count = wait->in.fence_count; |
| 1260 | uint32_t first = ~0; |
| 1261 | struct dma_fence **array; |
| 1262 | unsigned int i; |
| 1263 | long r; |
| 1264 | |
| 1265 | /* Prepare the fence array */ |
| 1266 | array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL); |
| 1267 | |
| 1268 | if (array == NULL) |
| 1269 | return -ENOMEM; |
| 1270 | |
| 1271 | for (i = 0; i < fence_count; i++) { |
| 1272 | struct dma_fence *fence; |
| 1273 | |
| 1274 | fence = amdgpu_cs_get_fence(adev, filp, &fences[i]); |
| 1275 | if (IS_ERR(fence)) { |
| 1276 | r = PTR_ERR(fence); |
| 1277 | goto err_free_fence_array; |
| 1278 | } else if (fence) { |
| 1279 | array[i] = fence; |
| 1280 | } else { /* NULL, the fence has been already signaled */ |
| 1281 | r = 1; |
| 1282 | goto out; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | r = dma_fence_wait_any_timeout(array, fence_count, true, timeout, |
| 1287 | &first); |
| 1288 | if (r < 0) |
| 1289 | goto err_free_fence_array; |
| 1290 | |
| 1291 | out: |
| 1292 | memset(wait, 0, sizeof(*wait)); |
| 1293 | wait->out.status = (r > 0); |
| 1294 | wait->out.first_signaled = first; |
| 1295 | /* set return value 0 to indicate success */ |
| 1296 | r = 0; |
| 1297 | |
| 1298 | err_free_fence_array: |
| 1299 | for (i = 0; i < fence_count; i++) |
| 1300 | dma_fence_put(array[i]); |
| 1301 | kfree(array); |
| 1302 | |
| 1303 | return r; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish |
| 1308 | * |
| 1309 | * @dev: drm device |
| 1310 | * @data: data from userspace |
| 1311 | * @filp: file private |
| 1312 | */ |
| 1313 | int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data, |
| 1314 | struct drm_file *filp) |
| 1315 | { |
| 1316 | struct amdgpu_device *adev = dev->dev_private; |
| 1317 | union drm_amdgpu_wait_fences *wait = data; |
| 1318 | uint32_t fence_count = wait->in.fence_count; |
| 1319 | struct drm_amdgpu_fence *fences_user; |
| 1320 | struct drm_amdgpu_fence *fences; |
| 1321 | int r; |
| 1322 | |
| 1323 | /* Get the fences from userspace */ |
| 1324 | fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence), |
| 1325 | GFP_KERNEL); |
| 1326 | if (fences == NULL) |
| 1327 | return -ENOMEM; |
| 1328 | |
| 1329 | fences_user = (void __user *)(unsigned long)(wait->in.fences); |
| 1330 | if (copy_from_user(fences, fences_user, |
| 1331 | sizeof(struct drm_amdgpu_fence) * fence_count)) { |
| 1332 | r = -EFAULT; |
| 1333 | goto err_free_fences; |
| 1334 | } |
| 1335 | |
| 1336 | if (wait->in.wait_all) |
| 1337 | r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences); |
| 1338 | else |
| 1339 | r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences); |
| 1340 | |
| 1341 | err_free_fences: |
| 1342 | kfree(fences); |
| 1343 | |
| 1344 | return r; |
| 1345 | } |
| 1346 | |
| 1347 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1348 | * amdgpu_cs_find_bo_va - find bo_va for VM address |
| 1349 | * |
| 1350 | * @parser: command submission parser context |
| 1351 | * @addr: VM address |
| 1352 | * @bo: resulting BO of the mapping found |
| 1353 | * |
| 1354 | * Search the buffer objects in the command submission context for a certain |
| 1355 | * virtual memory address. Returns allocation structure when found, NULL |
| 1356 | * otherwise. |
| 1357 | */ |
| 1358 | struct amdgpu_bo_va_mapping * |
| 1359 | amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser, |
| 1360 | uint64_t addr, struct amdgpu_bo **bo) |
| 1361 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1362 | struct amdgpu_bo_va_mapping *mapping; |
Christian König | 15486fd2 | 2015-12-22 16:06:12 +0100 | [diff] [blame] | 1363 | unsigned i; |
| 1364 | |
| 1365 | if (!parser->bo_list) |
| 1366 | return NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1367 | |
| 1368 | addr /= AMDGPU_GPU_PAGE_SIZE; |
| 1369 | |
Christian König | 15486fd2 | 2015-12-22 16:06:12 +0100 | [diff] [blame] | 1370 | for (i = 0; i < parser->bo_list->num_entries; i++) { |
| 1371 | struct amdgpu_bo_list_entry *lobj; |
| 1372 | |
| 1373 | lobj = &parser->bo_list->array[i]; |
| 1374 | if (!lobj->bo_va) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1375 | continue; |
| 1376 | |
Christian König | 15486fd2 | 2015-12-22 16:06:12 +0100 | [diff] [blame] | 1377 | list_for_each_entry(mapping, &lobj->bo_va->valids, list) { |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1378 | if (mapping->it.start > addr || |
| 1379 | addr > mapping->it.last) |
| 1380 | continue; |
| 1381 | |
Christian König | 15486fd2 | 2015-12-22 16:06:12 +0100 | [diff] [blame] | 1382 | *bo = lobj->bo_va->bo; |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1383 | return mapping; |
| 1384 | } |
| 1385 | |
Christian König | 15486fd2 | 2015-12-22 16:06:12 +0100 | [diff] [blame] | 1386 | list_for_each_entry(mapping, &lobj->bo_va->invalids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1387 | if (mapping->it.start > addr || |
| 1388 | addr > mapping->it.last) |
| 1389 | continue; |
| 1390 | |
Christian König | 15486fd2 | 2015-12-22 16:06:12 +0100 | [diff] [blame] | 1391 | *bo = lobj->bo_va->bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1392 | return mapping; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | return NULL; |
| 1397 | } |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 1398 | |
| 1399 | /** |
| 1400 | * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM |
| 1401 | * |
| 1402 | * @parser: command submission parser context |
| 1403 | * |
| 1404 | * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM. |
| 1405 | */ |
| 1406 | int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser) |
| 1407 | { |
| 1408 | unsigned i; |
| 1409 | int r; |
| 1410 | |
| 1411 | if (!parser->bo_list) |
| 1412 | return 0; |
| 1413 | |
| 1414 | for (i = 0; i < parser->bo_list->num_entries; i++) { |
| 1415 | struct amdgpu_bo *bo = parser->bo_list->array[i].robj; |
| 1416 | |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 1417 | r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem); |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 1418 | if (unlikely(r)) |
| 1419 | return r; |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 1420 | |
| 1421 | if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) |
| 1422 | continue; |
| 1423 | |
| 1424 | bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
| 1425 | amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains); |
| 1426 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); |
| 1427 | if (unlikely(r)) |
| 1428 | return r; |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | return 0; |
| 1432 | } |