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