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