blob: 76a1f823d983f12f0e076cca2cee6465bf9b125b [file] [log] [blame]
Chunming Zhouc1b69ed2015-07-21 13:45:14 +08001/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 *
23 */
24#include <linux/kthread.h>
25#include <linux/wait.h>
26#include <linux/sched.h>
27#include <drm/drmP.h>
28#include "amdgpu.h"
Chunming Zhou7034dec2015-11-11 14:56:00 +080029#include "amdgpu_trace.h"
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080030
Junwei Zhang4c7eb912015-09-09 09:05:55 +080031static struct fence *amdgpu_sched_dependency(struct amd_sched_job *sched_job)
Christian Könige61235d2015-08-25 11:05:36 +020032{
Junwei Zhanga6db8a32015-09-09 09:21:19 +080033 struct amdgpu_job *job = to_amdgpu_job(sched_job);
Christian König8d0a7ce2015-11-03 20:58:50 +010034 struct amdgpu_sync *sync = &job->ibs->sync;
35 struct amdgpu_vm *vm = job->ibs->vm;
36
37 struct fence *fence = amdgpu_sync_get_fence(sync);
38
39 if (fence == NULL && vm && !job->ibs->grabbed_vmid) {
40 struct amdgpu_ring *ring = job->ibs->ring;
Christian König8d0a7ce2015-11-03 20:58:50 +010041 int r;
42
Christian König94dd0a42016-01-18 17:01:42 +010043 r = amdgpu_vm_grab_id(vm, ring, sync,
44 &job->base.s_fence->base);
45 if (r)
Christian König8d0a7ce2015-11-03 20:58:50 +010046 DRM_ERROR("Error getting VM ID (%d)\n", r);
Christian König94dd0a42016-01-18 17:01:42 +010047 else
Christian König8d0a7ce2015-11-03 20:58:50 +010048 job->ibs->grabbed_vmid = true;
Christian König8d0a7ce2015-11-03 20:58:50 +010049
50 fence = amdgpu_sync_get_fence(sync);
51 }
52
53 return fence;
Christian Könige61235d2015-08-25 11:05:36 +020054}
55
Junwei Zhang4c7eb912015-09-09 09:05:55 +080056static struct fence *amdgpu_sched_run_job(struct amd_sched_job *sched_job)
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080057{
Christian König1886d1a2015-08-31 17:28:28 +020058 struct amdgpu_fence *fence = NULL;
Junwei Zhang4c7eb912015-09-09 09:05:55 +080059 struct amdgpu_job *job;
Christian Königbd755d02015-08-24 14:57:26 +020060 int r;
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080061
Junwei Zhang4c7eb912015-09-09 09:05:55 +080062 if (!sched_job) {
Chunming Zhou4cef9262015-08-05 19:52:14 +080063 DRM_ERROR("job is null\n");
Christian König6f0e54a2015-08-05 21:22:10 +020064 return NULL;
Chunming Zhou4cef9262015-08-05 19:52:14 +080065 }
Junwei Zhanga6db8a32015-09-09 09:21:19 +080066 job = to_amdgpu_job(sched_job);
Chunming Zhou7034dec2015-11-11 14:56:00 +080067 trace_amdgpu_sched_run_job(job);
Christian Könige2840222015-11-05 19:49:48 +010068 r = amdgpu_ib_schedule(job->adev, job->num_ibs, job->ibs, job->owner);
Christian König1886d1a2015-08-31 17:28:28 +020069 if (r) {
70 DRM_ERROR("Error scheduling IBs (%d)\n", r);
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080071 goto err;
Christian König1886d1a2015-08-31 17:28:28 +020072 }
73
Christian König6ef68c12015-10-22 15:16:22 +020074 fence = job->ibs[job->num_ibs - 1].fence;
75 fence_get(&fence->base);
Chunming Zhou74846672015-08-04 11:30:09 +080076
Christian König1886d1a2015-08-31 17:28:28 +020077err:
Junwei Zhang4c7eb912015-09-09 09:05:55 +080078 if (job->free_job)
79 job->free_job(job);
Christian Königbf7ebae2015-08-18 15:30:26 +020080
Junwei Zhang4c7eb912015-09-09 09:05:55 +080081 kfree(job);
Christian König1886d1a2015-08-31 17:28:28 +020082 return fence ? &fence->base : NULL;
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080083}
84
85struct amd_sched_backend_ops amdgpu_sched_ops = {
Christian Könige61235d2015-08-25 11:05:36 +020086 .dependency = amdgpu_sched_dependency,
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080087 .run_job = amdgpu_sched_run_job,
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080088};
89
Chunming Zhou3c704e92015-07-29 10:33:14 +080090int amdgpu_sched_ib_submit_kernel_helper(struct amdgpu_device *adev,
91 struct amdgpu_ring *ring,
92 struct amdgpu_ib *ibs,
93 unsigned num_ibs,
Chunming Zhoubb977d32015-08-18 15:16:40 +080094 int (*free_job)(struct amdgpu_job *),
Chunming Zhou17635522015-08-03 11:43:19 +080095 void *owner,
96 struct fence **f)
Chunming Zhou3c704e92015-07-29 10:33:14 +080097{
Chunming Zhoucadf97b2016-01-15 11:25:00 +080098 struct amdgpu_job *job =
99 kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
100 if (!job)
101 return -ENOMEM;
102 job->base.sched = &ring->sched;
103 job->base.s_entity = &adev->kernel_ctx.rings[ring->idx].entity;
104 job->base.s_fence = amd_sched_fence_create(job->base.s_entity, owner);
105 if (!job->base.s_fence) {
106 kfree(job);
107 return -ENOMEM;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800108 }
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800109 *f = fence_get(&job->base.s_fence->base);
110
111 job->adev = adev;
112 job->ibs = ibs;
113 job->num_ibs = num_ibs;
114 job->owner = owner;
115 job->free_job = free_job;
116 amd_sched_entity_push_job(&job->base);
Chunming Zhou3c623382015-08-20 18:33:59 +0800117
Chunming Zhou17635522015-08-03 11:43:19 +0800118 return 0;
Chunming Zhou3c704e92015-07-29 10:33:14 +0800119}