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 | */ |
| 27 | #include <linux/list_sort.h> |
| 28 | #include <drm/drmP.h> |
| 29 | #include <drm/amdgpu_drm.h> |
| 30 | #include "amdgpu.h" |
| 31 | #include "amdgpu_trace.h" |
| 32 | |
| 33 | #define AMDGPU_CS_MAX_PRIORITY 32u |
| 34 | #define AMDGPU_CS_NUM_BUCKETS (AMDGPU_CS_MAX_PRIORITY + 1) |
| 35 | |
| 36 | /* This is based on the bucket sort with O(n) time complexity. |
| 37 | * An item with priority "i" is added to bucket[i]. The lists are then |
| 38 | * concatenated in descending order. |
| 39 | */ |
| 40 | struct amdgpu_cs_buckets { |
| 41 | struct list_head bucket[AMDGPU_CS_NUM_BUCKETS]; |
| 42 | }; |
| 43 | |
| 44 | static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets *b) |
| 45 | { |
| 46 | unsigned i; |
| 47 | |
| 48 | for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) |
| 49 | INIT_LIST_HEAD(&b->bucket[i]); |
| 50 | } |
| 51 | |
| 52 | static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets *b, |
| 53 | struct list_head *item, unsigned priority) |
| 54 | { |
| 55 | /* Since buffers which appear sooner in the relocation list are |
| 56 | * likely to be used more often than buffers which appear later |
| 57 | * in the list, the sort mustn't change the ordering of buffers |
| 58 | * with the same priority, i.e. it must be stable. |
| 59 | */ |
| 60 | list_add_tail(item, &b->bucket[min(priority, AMDGPU_CS_MAX_PRIORITY)]); |
| 61 | } |
| 62 | |
| 63 | static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets *b, |
| 64 | struct list_head *out_list) |
| 65 | { |
| 66 | unsigned i; |
| 67 | |
| 68 | /* Connect the sorted buckets in the output list. */ |
| 69 | for (i = 0; i < AMDGPU_CS_NUM_BUCKETS; i++) { |
| 70 | list_splice(&b->bucket[i], out_list); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type, |
| 75 | u32 ip_instance, u32 ring, |
| 76 | struct amdgpu_ring **out_ring) |
| 77 | { |
| 78 | /* Right now all IPs have only one instance - multiple rings. */ |
| 79 | if (ip_instance != 0) { |
| 80 | DRM_ERROR("invalid ip instance: %d\n", ip_instance); |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | |
| 84 | switch (ip_type) { |
| 85 | default: |
| 86 | DRM_ERROR("unknown ip type: %d\n", ip_type); |
| 87 | return -EINVAL; |
| 88 | case AMDGPU_HW_IP_GFX: |
| 89 | if (ring < adev->gfx.num_gfx_rings) { |
| 90 | *out_ring = &adev->gfx.gfx_ring[ring]; |
| 91 | } else { |
| 92 | DRM_ERROR("only %d gfx rings are supported now\n", |
| 93 | adev->gfx.num_gfx_rings); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | break; |
| 97 | case AMDGPU_HW_IP_COMPUTE: |
| 98 | if (ring < adev->gfx.num_compute_rings) { |
| 99 | *out_ring = &adev->gfx.compute_ring[ring]; |
| 100 | } else { |
| 101 | DRM_ERROR("only %d compute rings are supported now\n", |
| 102 | adev->gfx.num_compute_rings); |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | break; |
| 106 | case AMDGPU_HW_IP_DMA: |
| 107 | if (ring < 2) { |
| 108 | *out_ring = &adev->sdma[ring].ring; |
| 109 | } else { |
| 110 | DRM_ERROR("only two SDMA rings are supported\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | break; |
| 114 | case AMDGPU_HW_IP_UVD: |
| 115 | *out_ring = &adev->uvd.ring; |
| 116 | break; |
| 117 | case AMDGPU_HW_IP_VCE: |
| 118 | if (ring < 2){ |
| 119 | *out_ring = &adev->vce.ring[ring]; |
| 120 | } else { |
| 121 | DRM_ERROR("only two VCE rings are supported\n"); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 129 | static void amdgpu_job_work_func(struct work_struct *work) |
| 130 | { |
| 131 | struct amdgpu_cs_parser *sched_job = |
| 132 | container_of(work, struct amdgpu_cs_parser, |
| 133 | job_work); |
| 134 | mutex_lock(&sched_job->job_lock); |
Chunming Zhou | afe1008 | 2015-07-28 16:11:52 +0800 | [diff] [blame] | 135 | if (sched_job->free_job) |
| 136 | sched_job->free_job(sched_job); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 137 | mutex_unlock(&sched_job->job_lock); |
| 138 | /* after processing job, free memory */ |
Chunming Zhou | 281b422 | 2015-08-12 12:58:31 +0800 | [diff] [blame^] | 139 | fence_put(&sched_job->s_fence->base); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 140 | kfree(sched_job); |
| 141 | } |
| 142 | struct amdgpu_cs_parser *amdgpu_cs_parser_create(struct amdgpu_device *adev, |
| 143 | struct drm_file *filp, |
| 144 | struct amdgpu_ctx *ctx, |
| 145 | struct amdgpu_ib *ibs, |
| 146 | uint32_t num_ibs) |
| 147 | { |
| 148 | struct amdgpu_cs_parser *parser; |
| 149 | int i; |
| 150 | |
| 151 | parser = kzalloc(sizeof(struct amdgpu_cs_parser), GFP_KERNEL); |
| 152 | if (!parser) |
| 153 | return NULL; |
| 154 | |
| 155 | parser->adev = adev; |
| 156 | parser->filp = filp; |
| 157 | parser->ctx = ctx; |
| 158 | parser->ibs = ibs; |
| 159 | parser->num_ibs = num_ibs; |
| 160 | if (amdgpu_enable_scheduler) { |
| 161 | mutex_init(&parser->job_lock); |
| 162 | INIT_WORK(&parser->job_work, amdgpu_job_work_func); |
| 163 | } |
| 164 | for (i = 0; i < num_ibs; i++) |
| 165 | ibs[i].ctx = ctx; |
| 166 | |
| 167 | return parser; |
| 168 | } |
| 169 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 170 | int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data) |
| 171 | { |
| 172 | union drm_amdgpu_cs *cs = data; |
| 173 | uint64_t *chunk_array_user; |
| 174 | uint64_t *chunk_array = NULL; |
| 175 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 176 | struct amdgpu_bo_list *bo_list = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 177 | unsigned size, i; |
| 178 | int r = 0; |
| 179 | |
| 180 | if (!cs->in.num_chunks) |
| 181 | goto out; |
| 182 | |
Christian König | 3cb485f | 2015-05-11 15:34:59 +0200 | [diff] [blame] | 183 | p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id); |
| 184 | if (!p->ctx) { |
| 185 | r = -EINVAL; |
| 186 | goto out; |
| 187 | } |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 188 | bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle); |
| 189 | if (bo_list && !bo_list->has_userptr) { |
Christian König | 34cb581 | 2015-08-04 11:54:48 +0200 | [diff] [blame] | 190 | p->bo_list = amdgpu_bo_list_clone(bo_list); |
| 191 | amdgpu_bo_list_put(bo_list); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 192 | if (!p->bo_list) |
| 193 | return -ENOMEM; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 194 | } else if (bo_list && bo_list->has_userptr) |
| 195 | p->bo_list = bo_list; |
| 196 | else |
| 197 | p->bo_list = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 198 | |
| 199 | /* get chunks */ |
| 200 | INIT_LIST_HEAD(&p->validated); |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 201 | chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 202 | if (chunk_array == NULL) { |
| 203 | r = -ENOMEM; |
| 204 | goto out; |
| 205 | } |
| 206 | |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 207 | chunk_array_user = (uint64_t __user *)(cs->in.chunks); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 208 | if (copy_from_user(chunk_array, chunk_array_user, |
| 209 | sizeof(uint64_t)*cs->in.num_chunks)) { |
| 210 | r = -EFAULT; |
| 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | p->nchunks = cs->in.num_chunks; |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 215 | p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk), |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 216 | GFP_KERNEL); |
| 217 | if (p->chunks == NULL) { |
| 218 | r = -ENOMEM; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | for (i = 0; i < p->nchunks; i++) { |
| 223 | struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL; |
| 224 | struct drm_amdgpu_cs_chunk user_chunk; |
| 225 | uint32_t __user *cdata; |
| 226 | |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 227 | chunk_ptr = (void __user *)chunk_array[i]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 228 | if (copy_from_user(&user_chunk, chunk_ptr, |
| 229 | sizeof(struct drm_amdgpu_cs_chunk))) { |
| 230 | r = -EFAULT; |
| 231 | goto out; |
| 232 | } |
| 233 | p->chunks[i].chunk_id = user_chunk.chunk_id; |
| 234 | p->chunks[i].length_dw = user_chunk.length_dw; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 235 | |
| 236 | size = p->chunks[i].length_dw; |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 237 | cdata = (void __user *)user_chunk.chunk_data; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 238 | p->chunks[i].user_ptr = cdata; |
| 239 | |
| 240 | p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t)); |
| 241 | if (p->chunks[i].kdata == NULL) { |
| 242 | r = -ENOMEM; |
| 243 | goto out; |
| 244 | } |
| 245 | size *= sizeof(uint32_t); |
| 246 | if (copy_from_user(p->chunks[i].kdata, cdata, size)) { |
| 247 | r = -EFAULT; |
| 248 | goto out; |
| 249 | } |
| 250 | |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 251 | switch (p->chunks[i].chunk_id) { |
| 252 | case AMDGPU_CHUNK_ID_IB: |
| 253 | p->num_ibs++; |
| 254 | break; |
| 255 | |
| 256 | case AMDGPU_CHUNK_ID_FENCE: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 257 | size = sizeof(struct drm_amdgpu_cs_chunk_fence); |
| 258 | if (p->chunks[i].length_dw * sizeof(uint32_t) >= size) { |
| 259 | uint32_t handle; |
| 260 | struct drm_gem_object *gobj; |
| 261 | struct drm_amdgpu_cs_chunk_fence *fence_data; |
| 262 | |
| 263 | fence_data = (void *)p->chunks[i].kdata; |
| 264 | handle = fence_data->handle; |
| 265 | gobj = drm_gem_object_lookup(p->adev->ddev, |
| 266 | p->filp, handle); |
| 267 | if (gobj == NULL) { |
| 268 | r = -EINVAL; |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | p->uf.bo = gem_to_amdgpu_bo(gobj); |
| 273 | p->uf.offset = fence_data->offset; |
| 274 | } else { |
| 275 | r = -EINVAL; |
| 276 | goto out; |
| 277 | } |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 278 | break; |
| 279 | |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 280 | case AMDGPU_CHUNK_ID_DEPENDENCIES: |
| 281 | break; |
| 282 | |
Christian König | 9a5e8fb | 2015-06-23 17:07:03 +0200 | [diff] [blame] | 283 | default: |
| 284 | r = -EINVAL; |
| 285 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
monk.liu | e60b344 | 2015-07-17 18:39:25 +0800 | [diff] [blame] | 289 | |
| 290 | p->ibs = kmalloc_array(p->num_ibs, sizeof(struct amdgpu_ib), GFP_KERNEL); |
| 291 | if (!p->ibs) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 292 | r = -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 293 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 294 | out: |
| 295 | kfree(chunk_array); |
| 296 | return r; |
| 297 | } |
| 298 | |
| 299 | /* Returns how many bytes TTM can move per IB. |
| 300 | */ |
| 301 | static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev) |
| 302 | { |
| 303 | u64 real_vram_size = adev->mc.real_vram_size; |
| 304 | u64 vram_usage = atomic64_read(&adev->vram_usage); |
| 305 | |
| 306 | /* This function is based on the current VRAM usage. |
| 307 | * |
| 308 | * - If all of VRAM is free, allow relocating the number of bytes that |
| 309 | * is equal to 1/4 of the size of VRAM for this IB. |
| 310 | |
| 311 | * - If more than one half of VRAM is occupied, only allow relocating |
| 312 | * 1 MB of data for this IB. |
| 313 | * |
| 314 | * - From 0 to one half of used VRAM, the threshold decreases |
| 315 | * linearly. |
| 316 | * __________________ |
| 317 | * 1/4 of -|\ | |
| 318 | * VRAM | \ | |
| 319 | * | \ | |
| 320 | * | \ | |
| 321 | * | \ | |
| 322 | * | \ | |
| 323 | * | \ | |
| 324 | * | \________|1 MB |
| 325 | * |----------------| |
| 326 | * VRAM 0 % 100 % |
| 327 | * used used |
| 328 | * |
| 329 | * Note: It's a threshold, not a limit. The threshold must be crossed |
| 330 | * for buffer relocations to stop, so any buffer of an arbitrary size |
| 331 | * can be moved as long as the threshold isn't crossed before |
| 332 | * the relocation takes place. We don't want to disable buffer |
| 333 | * relocations completely. |
| 334 | * |
| 335 | * The idea is that buffers should be placed in VRAM at creation time |
| 336 | * and TTM should only do a minimum number of relocations during |
| 337 | * command submission. In practice, you need to submit at least |
| 338 | * a dozen IBs to move all buffers to VRAM if they are in GTT. |
| 339 | * |
| 340 | * Also, things can get pretty crazy under memory pressure and actual |
| 341 | * VRAM usage can change a lot, so playing safe even at 50% does |
| 342 | * consistently increase performance. |
| 343 | */ |
| 344 | |
| 345 | u64 half_vram = real_vram_size >> 1; |
| 346 | u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage; |
| 347 | u64 bytes_moved_threshold = half_free_vram >> 1; |
| 348 | return max(bytes_moved_threshold, 1024*1024ull); |
| 349 | } |
| 350 | |
| 351 | int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p) |
| 352 | { |
| 353 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
| 354 | struct amdgpu_vm *vm = &fpriv->vm; |
| 355 | struct amdgpu_device *adev = p->adev; |
| 356 | struct amdgpu_bo_list_entry *lobj; |
| 357 | struct list_head duplicates; |
| 358 | struct amdgpu_bo *bo; |
| 359 | u64 bytes_moved = 0, initial_bytes_moved; |
| 360 | u64 bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(adev); |
| 361 | int r; |
| 362 | |
| 363 | INIT_LIST_HEAD(&duplicates); |
| 364 | r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, &duplicates); |
| 365 | if (unlikely(r != 0)) { |
| 366 | return r; |
| 367 | } |
| 368 | |
| 369 | list_for_each_entry(lobj, &p->validated, tv.head) { |
| 370 | bo = lobj->robj; |
| 371 | if (!bo->pin_count) { |
| 372 | u32 domain = lobj->prefered_domains; |
| 373 | u32 current_domain = |
| 374 | amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
| 375 | |
| 376 | /* Check if this buffer will be moved and don't move it |
| 377 | * if we have moved too many buffers for this IB already. |
| 378 | * |
| 379 | * Note that this allows moving at least one buffer of |
| 380 | * any size, because it doesn't take the current "bo" |
| 381 | * into account. We don't want to disallow buffer moves |
| 382 | * completely. |
| 383 | */ |
| 384 | if (current_domain != AMDGPU_GEM_DOMAIN_CPU && |
| 385 | (domain & current_domain) == 0 && /* will be moved */ |
| 386 | bytes_moved > bytes_moved_threshold) { |
| 387 | /* don't move it */ |
| 388 | domain = current_domain; |
| 389 | } |
| 390 | |
| 391 | retry: |
| 392 | amdgpu_ttm_placement_from_domain(bo, domain); |
| 393 | initial_bytes_moved = atomic64_read(&adev->num_bytes_moved); |
| 394 | r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); |
| 395 | bytes_moved += atomic64_read(&adev->num_bytes_moved) - |
| 396 | initial_bytes_moved; |
| 397 | |
| 398 | if (unlikely(r)) { |
| 399 | if (r != -ERESTARTSYS && domain != lobj->allowed_domains) { |
| 400 | domain = lobj->allowed_domains; |
| 401 | goto retry; |
| 402 | } |
| 403 | ttm_eu_backoff_reservation(&p->ticket, &p->validated); |
| 404 | return r; |
| 405 | } |
| 406 | } |
| 407 | lobj->bo_va = amdgpu_vm_bo_find(vm, bo); |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser *p) |
| 413 | { |
| 414 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
| 415 | struct amdgpu_cs_buckets buckets; |
monk.liu | 840d514 | 2015-04-27 15:19:20 +0800 | [diff] [blame] | 416 | bool need_mmap_lock = false; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 417 | int i, r; |
| 418 | |
monk.liu | 840d514 | 2015-04-27 15:19:20 +0800 | [diff] [blame] | 419 | if (p->bo_list) { |
| 420 | need_mmap_lock = p->bo_list->has_userptr; |
| 421 | amdgpu_cs_buckets_init(&buckets); |
| 422 | for (i = 0; i < p->bo_list->num_entries; i++) |
| 423 | amdgpu_cs_buckets_add(&buckets, &p->bo_list->array[i].tv.head, |
| 424 | p->bo_list->array[i].priority); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 425 | |
monk.liu | 840d514 | 2015-04-27 15:19:20 +0800 | [diff] [blame] | 426 | amdgpu_cs_buckets_get_list(&buckets, &p->validated); |
| 427 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 428 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 429 | p->vm_bos = amdgpu_vm_get_bos(p->adev, &fpriv->vm, |
| 430 | &p->validated); |
| 431 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 432 | if (need_mmap_lock) |
| 433 | down_read(¤t->mm->mmap_sem); |
| 434 | |
| 435 | r = amdgpu_cs_list_validate(p); |
| 436 | |
| 437 | if (need_mmap_lock) |
| 438 | up_read(¤t->mm->mmap_sem); |
| 439 | |
| 440 | return r; |
| 441 | } |
| 442 | |
| 443 | static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p) |
| 444 | { |
| 445 | struct amdgpu_bo_list_entry *e; |
| 446 | int r; |
| 447 | |
| 448 | list_for_each_entry(e, &p->validated, tv.head) { |
| 449 | struct reservation_object *resv = e->robj->tbo.resv; |
| 450 | r = amdgpu_sync_resv(p->adev, &p->ibs[0].sync, resv, p->filp); |
| 451 | |
| 452 | if (r) |
| 453 | return r; |
| 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int cmp_size_smaller_first(void *priv, struct list_head *a, |
| 459 | struct list_head *b) |
| 460 | { |
| 461 | struct amdgpu_bo_list_entry *la = list_entry(a, struct amdgpu_bo_list_entry, tv.head); |
| 462 | struct amdgpu_bo_list_entry *lb = list_entry(b, struct amdgpu_bo_list_entry, tv.head); |
| 463 | |
| 464 | /* Sort A before B if A is smaller. */ |
| 465 | return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages; |
| 466 | } |
| 467 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 468 | static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser *parser, int error, bool backoff) |
| 469 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 470 | if (!error) { |
| 471 | /* Sort the buffer list from the smallest to largest buffer, |
| 472 | * which affects the order of buffers in the LRU list. |
| 473 | * This assures that the smallest buffers are added first |
| 474 | * to the LRU list, so they are likely to be later evicted |
| 475 | * first, instead of large buffers whose eviction is more |
| 476 | * expensive. |
| 477 | * |
| 478 | * This slightly lowers the number of bytes moved by TTM |
| 479 | * per frame under memory pressure. |
| 480 | */ |
| 481 | list_sort(NULL, &parser->validated, cmp_size_smaller_first); |
| 482 | |
| 483 | ttm_eu_fence_buffer_objects(&parser->ticket, |
| 484 | &parser->validated, |
| 485 | &parser->ibs[parser->num_ibs-1].fence->base); |
| 486 | } else if (backoff) { |
| 487 | ttm_eu_backoff_reservation(&parser->ticket, |
| 488 | &parser->validated); |
| 489 | } |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 490 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 491 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 492 | static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser *parser) |
| 493 | { |
| 494 | unsigned i; |
Christian König | 3cb485f | 2015-05-11 15:34:59 +0200 | [diff] [blame] | 495 | if (parser->ctx) |
| 496 | amdgpu_ctx_put(parser->ctx); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 497 | if (parser->bo_list) { |
| 498 | if (!parser->bo_list->has_userptr) |
| 499 | amdgpu_bo_list_free(parser->bo_list); |
| 500 | else |
| 501 | amdgpu_bo_list_put(parser->bo_list); |
| 502 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 503 | drm_free_large(parser->vm_bos); |
| 504 | for (i = 0; i < parser->nchunks; i++) |
| 505 | drm_free_large(parser->chunks[i].kdata); |
| 506 | kfree(parser->chunks); |
Christian König | b8682ac | 2015-06-22 14:54:32 +0200 | [diff] [blame] | 507 | if (parser->ibs) |
| 508 | for (i = 0; i < parser->num_ibs; i++) |
| 509 | amdgpu_ib_free(parser->adev, &parser->ibs[i]); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 510 | kfree(parser->ibs); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 511 | if (parser->uf.bo) |
| 512 | drm_gem_object_unreference_unlocked(&parser->uf.bo->gem_base); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 513 | |
| 514 | if (!amdgpu_enable_scheduler) |
| 515 | kfree(parser); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 516 | } |
| 517 | |
Christian König | 351dba7 | 2015-08-03 20:39:12 +0200 | [diff] [blame] | 518 | /** |
| 519 | * cs_parser_fini() - clean parser states |
| 520 | * @parser: parser structure holding parsing context. |
| 521 | * @error: error number |
| 522 | * |
| 523 | * If error is set than unvalidate buffer, otherwise just free memory |
| 524 | * used by parsing context. |
| 525 | **/ |
| 526 | static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff) |
| 527 | { |
| 528 | amdgpu_cs_parser_fini_early(parser, error, backoff); |
| 529 | amdgpu_cs_parser_fini_late(parser); |
| 530 | } |
| 531 | |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 532 | static int amdgpu_cs_parser_run_job(struct amdgpu_cs_parser *sched_job) |
Christian König | 351dba7 | 2015-08-03 20:39:12 +0200 | [diff] [blame] | 533 | { |
| 534 | amdgpu_cs_parser_fini_early(sched_job, 0, true); |
| 535 | return 0; |
| 536 | } |
| 537 | |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 538 | static int amdgpu_cs_parser_free_job(struct amdgpu_cs_parser *sched_job) |
Christian König | 351dba7 | 2015-08-03 20:39:12 +0200 | [diff] [blame] | 539 | { |
| 540 | amdgpu_cs_parser_fini_late(sched_job); |
| 541 | return 0; |
| 542 | } |
| 543 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 544 | static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p, |
| 545 | struct amdgpu_vm *vm) |
| 546 | { |
| 547 | struct amdgpu_device *adev = p->adev; |
| 548 | struct amdgpu_bo_va *bo_va; |
| 549 | struct amdgpu_bo *bo; |
| 550 | int i, r; |
| 551 | |
| 552 | r = amdgpu_vm_update_page_directory(adev, vm); |
| 553 | if (r) |
| 554 | return r; |
| 555 | |
| 556 | r = amdgpu_vm_clear_freed(adev, vm); |
| 557 | if (r) |
| 558 | return r; |
| 559 | |
| 560 | if (p->bo_list) { |
| 561 | for (i = 0; i < p->bo_list->num_entries; i++) { |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 562 | struct fence *f; |
| 563 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 564 | /* ignore duplicates */ |
| 565 | bo = p->bo_list->array[i].robj; |
| 566 | if (!bo) |
| 567 | continue; |
| 568 | |
| 569 | bo_va = p->bo_list->array[i].bo_va; |
| 570 | if (bo_va == NULL) |
| 571 | continue; |
| 572 | |
| 573 | r = amdgpu_vm_bo_update(adev, bo_va, &bo->tbo.mem); |
| 574 | if (r) |
| 575 | return r; |
| 576 | |
Chunming Zhou | bb1e38a4 | 2015-08-03 18:19:38 +0800 | [diff] [blame] | 577 | f = bo_va->last_pt_update; |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 578 | r = amdgpu_sync_fence(adev, &p->ibs[0].sync, f); |
| 579 | if (r) |
| 580 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
monk.liu | cfe2c97 | 2015-05-26 15:01:54 +0800 | [diff] [blame] | 584 | return amdgpu_vm_clear_invalids(adev, vm, &p->ibs[0].sync); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev, |
| 588 | struct amdgpu_cs_parser *parser) |
| 589 | { |
| 590 | struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; |
| 591 | struct amdgpu_vm *vm = &fpriv->vm; |
| 592 | struct amdgpu_ring *ring; |
| 593 | int i, r; |
| 594 | |
| 595 | if (parser->num_ibs == 0) |
| 596 | return 0; |
| 597 | |
| 598 | /* Only for UVD/VCE VM emulation */ |
| 599 | for (i = 0; i < parser->num_ibs; i++) { |
| 600 | ring = parser->ibs[i].ring; |
| 601 | if (ring->funcs->parse_cs) { |
| 602 | r = amdgpu_ring_parse_cs(ring, parser, i); |
| 603 | if (r) |
| 604 | return r; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | mutex_lock(&vm->mutex); |
| 609 | r = amdgpu_bo_vm_update_pte(parser, vm); |
| 610 | if (r) { |
| 611 | goto out; |
| 612 | } |
| 613 | amdgpu_cs_sync_rings(parser); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 614 | if (!amdgpu_enable_scheduler) |
| 615 | r = amdgpu_ib_schedule(adev, parser->num_ibs, parser->ibs, |
| 616 | parser->filp); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 617 | |
| 618 | out: |
| 619 | mutex_unlock(&vm->mutex); |
| 620 | return r; |
| 621 | } |
| 622 | |
| 623 | static int amdgpu_cs_handle_lockup(struct amdgpu_device *adev, int r) |
| 624 | { |
| 625 | if (r == -EDEADLK) { |
| 626 | r = amdgpu_gpu_reset(adev); |
| 627 | if (!r) |
| 628 | r = -EAGAIN; |
| 629 | } |
| 630 | return r; |
| 631 | } |
| 632 | |
| 633 | static int amdgpu_cs_ib_fill(struct amdgpu_device *adev, |
| 634 | struct amdgpu_cs_parser *parser) |
| 635 | { |
| 636 | struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; |
| 637 | struct amdgpu_vm *vm = &fpriv->vm; |
| 638 | int i, j; |
| 639 | int r; |
| 640 | |
| 641 | for (i = 0, j = 0; i < parser->nchunks && j < parser->num_ibs; i++) { |
| 642 | struct amdgpu_cs_chunk *chunk; |
| 643 | struct amdgpu_ib *ib; |
| 644 | struct drm_amdgpu_cs_chunk_ib *chunk_ib; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 645 | struct amdgpu_ring *ring; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 646 | |
| 647 | chunk = &parser->chunks[i]; |
| 648 | ib = &parser->ibs[j]; |
| 649 | chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata; |
| 650 | |
| 651 | if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB) |
| 652 | continue; |
| 653 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 654 | r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type, |
| 655 | chunk_ib->ip_instance, chunk_ib->ring, |
| 656 | &ring); |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 657 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 658 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 659 | |
| 660 | if (ring->funcs->parse_cs) { |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 661 | struct amdgpu_bo_va_mapping *m; |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 662 | struct amdgpu_bo *aobj = NULL; |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 663 | uint64_t offset; |
| 664 | uint8_t *kptr; |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 665 | |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 666 | m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start, |
| 667 | &aobj); |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 668 | if (!aobj) { |
| 669 | DRM_ERROR("IB va_start is invalid\n"); |
| 670 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 671 | } |
| 672 | |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 673 | if ((chunk_ib->va_start + chunk_ib->ib_bytes) > |
| 674 | (m->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) { |
| 675 | DRM_ERROR("IB va_start+ib_bytes is invalid\n"); |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 679 | /* the IB should be reserved at this point */ |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 680 | r = amdgpu_bo_kmap(aobj, (void **)&kptr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 681 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 682 | return r; |
| 683 | } |
| 684 | |
Christian König | 4802ce1 | 2015-06-10 17:20:11 +0200 | [diff] [blame] | 685 | offset = ((uint64_t)m->it.start) * AMDGPU_GPU_PAGE_SIZE; |
| 686 | kptr += chunk_ib->va_start - offset; |
| 687 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 688 | r = amdgpu_ib_get(ring, NULL, chunk_ib->ib_bytes, ib); |
| 689 | if (r) { |
| 690 | DRM_ERROR("Failed to get ib !\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 691 | return r; |
| 692 | } |
| 693 | |
| 694 | memcpy(ib->ptr, kptr, chunk_ib->ib_bytes); |
| 695 | amdgpu_bo_kunmap(aobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 696 | } else { |
| 697 | r = amdgpu_ib_get(ring, vm, 0, ib); |
| 698 | if (r) { |
| 699 | DRM_ERROR("Failed to get ib !\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 700 | return r; |
| 701 | } |
| 702 | |
| 703 | ib->gpu_addr = chunk_ib->va_start; |
| 704 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 705 | |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 706 | ib->length_dw = chunk_ib->ib_bytes / 4; |
Jammy Zhou | de807f8 | 2015-05-11 23:41:41 +0800 | [diff] [blame] | 707 | ib->flags = chunk_ib->flags; |
Christian König | 3cb485f | 2015-05-11 15:34:59 +0200 | [diff] [blame] | 708 | ib->ctx = parser->ctx; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 709 | j++; |
| 710 | } |
| 711 | |
| 712 | if (!parser->num_ibs) |
| 713 | return 0; |
| 714 | |
| 715 | /* add GDS resources to first IB */ |
| 716 | if (parser->bo_list) { |
| 717 | struct amdgpu_bo *gds = parser->bo_list->gds_obj; |
| 718 | struct amdgpu_bo *gws = parser->bo_list->gws_obj; |
| 719 | struct amdgpu_bo *oa = parser->bo_list->oa_obj; |
| 720 | struct amdgpu_ib *ib = &parser->ibs[0]; |
| 721 | |
| 722 | if (gds) { |
| 723 | ib->gds_base = amdgpu_bo_gpu_offset(gds); |
| 724 | ib->gds_size = amdgpu_bo_size(gds); |
| 725 | } |
| 726 | if (gws) { |
| 727 | ib->gws_base = amdgpu_bo_gpu_offset(gws); |
| 728 | ib->gws_size = amdgpu_bo_size(gws); |
| 729 | } |
| 730 | if (oa) { |
| 731 | ib->oa_base = amdgpu_bo_gpu_offset(oa); |
| 732 | ib->oa_size = amdgpu_bo_size(oa); |
| 733 | } |
| 734 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 735 | /* wrap the last IB with user fence */ |
| 736 | if (parser->uf.bo) { |
| 737 | struct amdgpu_ib *ib = &parser->ibs[parser->num_ibs - 1]; |
| 738 | |
| 739 | /* UVD & VCE fw doesn't support user fences */ |
| 740 | if (ib->ring->type == AMDGPU_RING_TYPE_UVD || |
| 741 | ib->ring->type == AMDGPU_RING_TYPE_VCE) |
| 742 | return -EINVAL; |
| 743 | |
| 744 | ib->user = &parser->uf; |
| 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 750 | static int amdgpu_cs_dependencies(struct amdgpu_device *adev, |
| 751 | struct amdgpu_cs_parser *p) |
| 752 | { |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 753 | struct amdgpu_fpriv *fpriv = p->filp->driver_priv; |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 754 | struct amdgpu_ib *ib; |
| 755 | int i, j, r; |
| 756 | |
| 757 | if (!p->num_ibs) |
| 758 | return 0; |
| 759 | |
| 760 | /* Add dependencies to first IB */ |
| 761 | ib = &p->ibs[0]; |
| 762 | for (i = 0; i < p->nchunks; ++i) { |
| 763 | struct drm_amdgpu_cs_chunk_dep *deps; |
| 764 | struct amdgpu_cs_chunk *chunk; |
| 765 | unsigned num_deps; |
| 766 | |
| 767 | chunk = &p->chunks[i]; |
| 768 | |
| 769 | if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES) |
| 770 | continue; |
| 771 | |
| 772 | deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata; |
| 773 | num_deps = chunk->length_dw * 4 / |
| 774 | sizeof(struct drm_amdgpu_cs_chunk_dep); |
| 775 | |
| 776 | for (j = 0; j < num_deps; ++j) { |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 777 | struct amdgpu_ring *ring; |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 778 | struct amdgpu_ctx *ctx; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 779 | struct fence *fence; |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 780 | |
| 781 | r = amdgpu_cs_get_ring(adev, deps[j].ip_type, |
| 782 | deps[j].ip_instance, |
| 783 | deps[j].ring, &ring); |
| 784 | if (r) |
| 785 | return r; |
| 786 | |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 787 | ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id); |
| 788 | if (ctx == NULL) |
| 789 | return -EINVAL; |
| 790 | |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 791 | fence = amdgpu_ctx_get_fence(ctx, ring, |
| 792 | deps[j].handle); |
| 793 | if (IS_ERR(fence)) { |
| 794 | r = PTR_ERR(fence); |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 795 | amdgpu_ctx_put(ctx); |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 796 | return r; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 797 | |
| 798 | } else if (fence) { |
| 799 | r = amdgpu_sync_fence(adev, &ib->sync, fence); |
| 800 | fence_put(fence); |
| 801 | amdgpu_ctx_put(ctx); |
| 802 | if (r) |
| 803 | return r; |
Christian König | 76a1ea6 | 2015-07-06 19:42:10 +0200 | [diff] [blame] | 804 | } |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 811 | static int amdgpu_cs_parser_prepare_job(struct amdgpu_cs_parser *sched_job) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 812 | { |
Jammy Zhou | dd01d07 | 2015-07-30 17:19:52 +0800 | [diff] [blame] | 813 | int r, i; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 814 | struct amdgpu_cs_parser *parser = sched_job; |
| 815 | struct amdgpu_device *adev = sched_job->adev; |
Jammy Zhou | dd01d07 | 2015-07-30 17:19:52 +0800 | [diff] [blame] | 816 | bool reserved_buffers = false; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 817 | |
Jammy Zhou | dd01d07 | 2015-07-30 17:19:52 +0800 | [diff] [blame] | 818 | r = amdgpu_cs_parser_relocs(parser); |
| 819 | if (r) { |
| 820 | if (r != -ERESTARTSYS) { |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 821 | if (r == -ENOMEM) |
| 822 | DRM_ERROR("Not enough memory for command submission!\n"); |
| 823 | else |
| 824 | DRM_ERROR("Failed to process the buffer list %d!\n", r); |
| 825 | } |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | if (!r) { |
Marek Olšák | 3ccec53 | 2015-06-02 17:44:49 +0200 | [diff] [blame] | 829 | reserved_buffers = true; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 830 | r = amdgpu_cs_ib_fill(adev, parser); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 831 | } |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 832 | if (!r) { |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 833 | r = amdgpu_cs_dependencies(adev, parser); |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 834 | if (r) |
| 835 | DRM_ERROR("Failed in the dependencies handling %d!\n", r); |
| 836 | } |
Jammy Zhou | dd01d07 | 2015-07-30 17:19:52 +0800 | [diff] [blame] | 837 | if (r) { |
| 838 | amdgpu_cs_parser_fini(parser, r, reserved_buffers); |
| 839 | return r; |
| 840 | } |
Christian König | 2b48d32 | 2015-06-19 17:31:29 +0200 | [diff] [blame] | 841 | |
Jammy Zhou | dd01d07 | 2015-07-30 17:19:52 +0800 | [diff] [blame] | 842 | for (i = 0; i < parser->num_ibs; i++) |
| 843 | trace_amdgpu_cs(parser, i); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 844 | |
Jammy Zhou | dd01d07 | 2015-07-30 17:19:52 +0800 | [diff] [blame] | 845 | r = amdgpu_cs_ib_vm_chunk(adev, parser); |
| 846 | return r; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static struct amdgpu_ring *amdgpu_cs_parser_get_ring( |
| 850 | struct amdgpu_device *adev, |
| 851 | struct amdgpu_cs_parser *parser) |
| 852 | { |
| 853 | int i, r; |
| 854 | |
| 855 | struct amdgpu_cs_chunk *chunk; |
| 856 | struct drm_amdgpu_cs_chunk_ib *chunk_ib; |
| 857 | struct amdgpu_ring *ring; |
| 858 | for (i = 0; i < parser->nchunks; i++) { |
| 859 | chunk = &parser->chunks[i]; |
| 860 | chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata; |
| 861 | |
| 862 | if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB) |
| 863 | continue; |
| 864 | |
| 865 | r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type, |
| 866 | chunk_ib->ip_instance, chunk_ib->ring, |
| 867 | &ring); |
| 868 | if (r) |
| 869 | return NULL; |
| 870 | break; |
| 871 | } |
| 872 | return ring; |
| 873 | } |
| 874 | |
| 875 | int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) |
| 876 | { |
| 877 | struct amdgpu_device *adev = dev->dev_private; |
| 878 | union drm_amdgpu_cs *cs = data; |
| 879 | struct amdgpu_cs_parser *parser; |
| 880 | int r; |
| 881 | |
| 882 | down_read(&adev->exclusive_lock); |
| 883 | if (!adev->accel_working) { |
| 884 | up_read(&adev->exclusive_lock); |
| 885 | return -EBUSY; |
| 886 | } |
| 887 | |
| 888 | parser = amdgpu_cs_parser_create(adev, filp, NULL, NULL, 0); |
| 889 | if (!parser) |
| 890 | return -ENOMEM; |
| 891 | r = amdgpu_cs_parser_init(parser, data); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 892 | if (r) { |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 893 | DRM_ERROR("Failed to initialize parser !\n"); |
| 894 | amdgpu_cs_parser_fini(parser, r, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 895 | up_read(&adev->exclusive_lock); |
| 896 | r = amdgpu_cs_handle_lockup(adev, r); |
| 897 | return r; |
| 898 | } |
| 899 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 900 | if (amdgpu_enable_scheduler && parser->num_ibs) { |
| 901 | struct amdgpu_ring * ring = |
| 902 | amdgpu_cs_parser_get_ring(adev, parser); |
Chunming Zhou | 4274f5d | 2015-07-21 16:04:39 +0800 | [diff] [blame] | 903 | if (ring->is_pte_ring || (parser->bo_list && parser->bo_list->has_userptr)) { |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 904 | r = amdgpu_cs_parser_prepare_job(parser); |
| 905 | if (r) |
| 906 | goto out; |
| 907 | } else |
| 908 | parser->prepare_job = amdgpu_cs_parser_prepare_job; |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 909 | parser->ring = ring; |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 910 | parser->run_job = amdgpu_cs_parser_run_job; |
| 911 | parser->free_job = amdgpu_cs_parser_free_job; |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 912 | mutex_lock(&parser->job_lock); |
| 913 | r = amd_sched_push_job(ring->scheduler, |
| 914 | &parser->ctx->rings[ring->idx].entity, |
| 915 | parser, |
| 916 | &parser->s_fence); |
| 917 | if (r) { |
| 918 | mutex_unlock(&parser->job_lock); |
| 919 | goto out; |
| 920 | } |
| 921 | parser->ibs[parser->num_ibs - 1].sequence = |
| 922 | amdgpu_ctx_add_fence(parser->ctx, ring, |
| 923 | &parser->s_fence->base, |
| 924 | parser->s_fence->v_seq); |
| 925 | cs->out.handle = parser->s_fence->v_seq; |
| 926 | mutex_unlock(&parser->job_lock); |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 927 | up_read(&adev->exclusive_lock); |
| 928 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 929 | } |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 930 | r = amdgpu_cs_parser_prepare_job(parser); |
| 931 | if (r) |
| 932 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 933 | |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 934 | cs->out.handle = parser->ibs[parser->num_ibs - 1].sequence; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 935 | out: |
Chunming Zhou | 049fc52 | 2015-07-21 14:36:51 +0800 | [diff] [blame] | 936 | amdgpu_cs_parser_fini(parser, r, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 937 | up_read(&adev->exclusive_lock); |
| 938 | r = amdgpu_cs_handle_lockup(adev, r); |
| 939 | return r; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * amdgpu_cs_wait_ioctl - wait for a command submission to finish |
| 944 | * |
| 945 | * @dev: drm device |
| 946 | * @data: data from userspace |
| 947 | * @filp: file private |
| 948 | * |
| 949 | * Wait for the command submission identified by handle to finish. |
| 950 | */ |
| 951 | int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data, |
| 952 | struct drm_file *filp) |
| 953 | { |
| 954 | union drm_amdgpu_wait_cs *wait = data; |
| 955 | struct amdgpu_device *adev = dev->dev_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 956 | unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout); |
Christian König | 03507c4 | 2015-06-19 17:00:19 +0200 | [diff] [blame] | 957 | struct amdgpu_ring *ring = NULL; |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 958 | struct amdgpu_ctx *ctx; |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 959 | struct fence *fence; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 960 | long r; |
| 961 | |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 962 | r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance, |
| 963 | wait->in.ring, &ring); |
| 964 | if (r) |
| 965 | return r; |
| 966 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 967 | ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id); |
| 968 | if (ctx == NULL) |
| 969 | return -EINVAL; |
Chunming Zhou | 4b559c9 | 2015-07-21 15:53:04 +0800 | [diff] [blame] | 970 | |
| 971 | fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle); |
| 972 | if (IS_ERR(fence)) |
| 973 | r = PTR_ERR(fence); |
| 974 | else if (fence) { |
| 975 | r = fence_wait_timeout(fence, true, timeout); |
| 976 | fence_put(fence); |
| 977 | } else |
Christian König | 21c16bf | 2015-07-07 17:24:49 +0200 | [diff] [blame] | 978 | r = 1; |
| 979 | |
Jammy Zhou | 66b3cf2 | 2015-05-08 17:29:40 +0800 | [diff] [blame] | 980 | amdgpu_ctx_put(ctx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 981 | if (r < 0) |
| 982 | return r; |
| 983 | |
| 984 | memset(wait, 0, sizeof(*wait)); |
| 985 | wait->out.status = (r == 0); |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * amdgpu_cs_find_bo_va - find bo_va for VM address |
| 992 | * |
| 993 | * @parser: command submission parser context |
| 994 | * @addr: VM address |
| 995 | * @bo: resulting BO of the mapping found |
| 996 | * |
| 997 | * Search the buffer objects in the command submission context for a certain |
| 998 | * virtual memory address. Returns allocation structure when found, NULL |
| 999 | * otherwise. |
| 1000 | */ |
| 1001 | struct amdgpu_bo_va_mapping * |
| 1002 | amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser, |
| 1003 | uint64_t addr, struct amdgpu_bo **bo) |
| 1004 | { |
| 1005 | struct amdgpu_bo_list_entry *reloc; |
| 1006 | struct amdgpu_bo_va_mapping *mapping; |
| 1007 | |
| 1008 | addr /= AMDGPU_GPU_PAGE_SIZE; |
| 1009 | |
| 1010 | list_for_each_entry(reloc, &parser->validated, tv.head) { |
| 1011 | if (!reloc->bo_va) |
| 1012 | continue; |
| 1013 | |
Christian König | 7fc1195 | 2015-07-30 11:53:42 +0200 | [diff] [blame] | 1014 | list_for_each_entry(mapping, &reloc->bo_va->valids, list) { |
| 1015 | if (mapping->it.start > addr || |
| 1016 | addr > mapping->it.last) |
| 1017 | continue; |
| 1018 | |
| 1019 | *bo = reloc->bo_va->bo; |
| 1020 | return mapping; |
| 1021 | } |
| 1022 | |
| 1023 | list_for_each_entry(mapping, &reloc->bo_va->invalids, list) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1024 | if (mapping->it.start > addr || |
| 1025 | addr > mapping->it.last) |
| 1026 | continue; |
| 1027 | |
| 1028 | *bo = reloc->bo_va->bo; |
| 1029 | return mapping; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | return NULL; |
| 1034 | } |