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