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