1 /*
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  */
30 #include "drmP.h"
31 #include "drm.h"
32 #include "radeon.h"
33 
34 
35 int radeon_semaphore_create(struct radeon_device *rdev,
36 			    struct radeon_semaphore **semaphore)
37 {
38 	int r;
39 
40 	*semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
41 	if (*semaphore == NULL) {
42 		return -ENOMEM;
43 	}
44 	r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
45 			     &(*semaphore)->sa_bo, 8, 8, true);
46 	if (r) {
47 		kfree(*semaphore);
48 		*semaphore = NULL;
49 		return r;
50 	}
51 	(*semaphore)->waiters = 0;
52 	(*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
53 	*((uint64_t*)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
54 	return 0;
55 }
56 
57 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
58 			          struct radeon_semaphore *semaphore)
59 {
60 	--semaphore->waiters;
61 	radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
62 }
63 
64 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
65 			        struct radeon_semaphore *semaphore)
66 {
67 	++semaphore->waiters;
68 	radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
69 }
70 
71 int radeon_semaphore_sync_rings(struct radeon_device *rdev,
72 				struct radeon_semaphore *semaphore,
73 				bool sync_to[RADEON_NUM_RINGS],
74 				int dst_ring)
75 {
76 	int i = 0, r;
77 
78 	mutex_lock(&rdev->ring_lock);
79 	r = radeon_ring_alloc(rdev, &rdev->ring[dst_ring], RADEON_NUM_RINGS * 8);
80 	if (r) {
81 		goto error;
82 	}
83 
84 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
85 		/* no need to sync to our own or unused rings */
86 		if (!sync_to[i] || i == dst_ring)
87 			continue;
88 
89 		/* prevent GPU deadlocks */
90 		if (!rdev->ring[i].ready) {
91 			dev_err(rdev->dev, "Trying to sync to a disabled ring!");
92 			r = -EINVAL;
93 			goto error;
94 		}
95 
96 		r = radeon_ring_alloc(rdev, &rdev->ring[i], 8);
97 		if (r) {
98 			goto error;
99 		}
100 
101 		radeon_semaphore_emit_signal(rdev, i, semaphore);
102 		radeon_semaphore_emit_wait(rdev, dst_ring, semaphore);
103 
104 		radeon_ring_commit(rdev, &rdev->ring[i]);
105 	}
106 
107 	radeon_ring_commit(rdev, &rdev->ring[dst_ring]);
108 	mutex_unlock(&rdev->ring_lock);
109 
110 	return 0;
111 
112 error:
113 	/* unlock all locks taken so far */
114 	for (--i; i >= 0; --i) {
115 		if (sync_to[i] || i == dst_ring) {
116 			radeon_ring_undo(&rdev->ring[i]);
117 		}
118 	}
119 	radeon_ring_undo(&rdev->ring[dst_ring]);
120 	mutex_unlock(&rdev->ring_lock);
121 	return r;
122 }
123 
124 void radeon_semaphore_free(struct radeon_device *rdev,
125 			   struct radeon_semaphore *semaphore,
126 			   struct radeon_fence *fence)
127 {
128 	if (semaphore == NULL) {
129 		return;
130 	}
131 	if (semaphore->waiters > 0) {
132 		dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
133 			" hardware lockup imminent!\n", semaphore);
134 	}
135 	radeon_sa_bo_free(rdev, &semaphore->sa_bo, fence);
136 	kfree(semaphore);
137 }
138