blob: 97d73bfc8782c177713a74795bc0ad8209b7eb37 [file] [log] [blame]
Christian König15d33322011-09-15 19:02:22 +02001/*
2 * Copyright 2011 Christian König.
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
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26/*
27 * Authors:
28 * Christian König <deathsimple@vodafone.de>
29 */
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
Christian König15d33322011-09-15 19:02:22 +020031#include "radeon.h"
Christian Königbd80c8b2013-11-12 12:58:04 +010032#include "radeon_trace.h"
Jerome Glissec1341e52011-12-21 12:13:47 -050033
Christian König15d33322011-09-15 19:02:22 +020034int radeon_semaphore_create(struct radeon_device *rdev,
35 struct radeon_semaphore **semaphore)
36{
Jerome Glissec1341e52011-12-21 12:13:47 -050037 int r;
Christian König15d33322011-09-15 19:02:22 +020038
Jerome Glissea8c05942012-05-09 15:34:57 +020039 *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
Jerome Glissec1341e52011-12-21 12:13:47 -050040 if (*semaphore == NULL) {
Jerome Glissec1341e52011-12-21 12:13:47 -050041 return -ENOMEM;
Christian König15d33322011-09-15 19:02:22 +020042 }
Jerome Glissec507f7e2012-05-09 15:34:58 +020043 r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
Jerome Glissea8c05942012-05-09 15:34:57 +020044 &(*semaphore)->sa_bo, 8, 8, true);
45 if (r) {
46 kfree(*semaphore);
47 *semaphore = NULL;
48 return r;
49 }
50 (*semaphore)->waiters = 0;
51 (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
52 *((uint64_t*)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
Christian König15d33322011-09-15 19:02:22 +020053 return 0;
54}
55
56void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
57 struct radeon_semaphore *semaphore)
58{
Christian Königbd80c8b2013-11-12 12:58:04 +010059 trace_radeon_semaphore_signale(ring, semaphore);
60
Jerome Glissea8c05942012-05-09 15:34:57 +020061 --semaphore->waiters;
Christian Könige32eb502011-10-23 12:56:27 +020062 radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
Christian König15d33322011-09-15 19:02:22 +020063}
64
65void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
66 struct radeon_semaphore *semaphore)
67{
Christian Königbd80c8b2013-11-12 12:58:04 +010068 trace_radeon_semaphore_wait(ring, semaphore);
69
Jerome Glissea8c05942012-05-09 15:34:57 +020070 ++semaphore->waiters;
Christian Könige32eb502011-10-23 12:56:27 +020071 radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
Christian König15d33322011-09-15 19:02:22 +020072}
73
Christian König220907d2012-05-10 16:46:43 +020074/* caller must hold ring lock */
Christian König8f676c42012-05-02 15:11:18 +020075int radeon_semaphore_sync_rings(struct radeon_device *rdev,
76 struct radeon_semaphore *semaphore,
Christian König220907d2012-05-10 16:46:43 +020077 int signaler, int waiter)
Christian König8f676c42012-05-02 15:11:18 +020078{
Christian König220907d2012-05-10 16:46:43 +020079 int r;
Christian Königd6999bc2012-05-09 15:34:45 +020080
Christian König220907d2012-05-10 16:46:43 +020081 /* no need to signal and wait on the same ring */
82 if (signaler == waiter) {
83 return 0;
84 }
85
86 /* prevent GPU deadlocks */
87 if (!rdev->ring[signaler].ready) {
88 dev_err(rdev->dev, "Trying to sync to a disabled ring!");
89 return -EINVAL;
90 }
91
92 r = radeon_ring_alloc(rdev, &rdev->ring[signaler], 8);
Christian Königd6999bc2012-05-09 15:34:45 +020093 if (r) {
Christian König220907d2012-05-10 16:46:43 +020094 return r;
Christian Königd6999bc2012-05-09 15:34:45 +020095 }
Christian König220907d2012-05-10 16:46:43 +020096 radeon_semaphore_emit_signal(rdev, signaler, semaphore);
97 radeon_ring_commit(rdev, &rdev->ring[signaler]);
Christian König8f676c42012-05-02 15:11:18 +020098
Christian König220907d2012-05-10 16:46:43 +020099 /* we assume caller has already allocated space on waiters ring */
100 radeon_semaphore_emit_wait(rdev, waiter, semaphore);
Christian König8f676c42012-05-02 15:11:18 +0200101
Jerome Glisse5f0839c2013-01-11 15:19:43 -0500102 /* for debugging lockup only, used by sysfs debug files */
103 rdev->ring[signaler].last_semaphore_signal_addr = semaphore->gpu_addr;
104 rdev->ring[waiter].last_semaphore_wait_addr = semaphore->gpu_addr;
105
Christian König8f676c42012-05-02 15:11:18 +0200106 return 0;
Christian König8f676c42012-05-02 15:11:18 +0200107}
108
Christian König15d33322011-09-15 19:02:22 +0200109void radeon_semaphore_free(struct radeon_device *rdev,
Christian König220907d2012-05-10 16:46:43 +0200110 struct radeon_semaphore **semaphore,
Jerome Glissea8c05942012-05-09 15:34:57 +0200111 struct radeon_fence *fence)
Christian König15d33322011-09-15 19:02:22 +0200112{
Christian König220907d2012-05-10 16:46:43 +0200113 if (semaphore == NULL || *semaphore == NULL) {
Jerome Glissea8c05942012-05-09 15:34:57 +0200114 return;
Christian König15d33322011-09-15 19:02:22 +0200115 }
Christian König220907d2012-05-10 16:46:43 +0200116 if ((*semaphore)->waiters > 0) {
Jerome Glissea8c05942012-05-09 15:34:57 +0200117 dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
Christian König220907d2012-05-10 16:46:43 +0200118 " hardware lockup imminent!\n", *semaphore);
Jerome Glissea8c05942012-05-09 15:34:57 +0200119 }
Christian König220907d2012-05-10 16:46:43 +0200120 radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
121 kfree(*semaphore);
122 *semaphore = NULL;
Christian König15d33322011-09-15 19:02:22 +0200123}