1 /*	$OpenBSD: radeon_semaphore.c,v 1.3 2015/04/18 11:41:29 jsg Exp $	*/
2 /*
3  * Copyright 2011 Christian König.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
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 NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  */
27 /*
28  * Authors:
29  *    Christian König <deathsimple@vodafone.de>
30  */
31 #include <dev/pci/drm/drmP.h>
32 #include "radeon.h"
33 #include "radeon_trace.h"
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 	trace_radeon_semaphore_signale(ring, semaphore);
61 
62 	--semaphore->waiters;
63 	radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
64 }
65 
66 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
67 			        struct radeon_semaphore *semaphore)
68 {
69 	trace_radeon_semaphore_wait(ring, semaphore);
70 
71 	++semaphore->waiters;
72 	radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
73 }
74 
75 /* caller must hold ring lock */
76 int radeon_semaphore_sync_rings(struct radeon_device *rdev,
77 				struct radeon_semaphore *semaphore,
78 				int signaler, int waiter)
79 {
80 	int r;
81 
82 	/* no need to signal and wait on the same ring */
83 	if (signaler == waiter) {
84 		return 0;
85 	}
86 
87 	/* prevent GPU deadlocks */
88 	if (!rdev->ring[signaler].ready) {
89 		dev_err(rdev->dev, "Trying to sync to a disabled ring!");
90 		return -EINVAL;
91 	}
92 
93 	r = radeon_ring_alloc(rdev, &rdev->ring[signaler], 8);
94 	if (r) {
95 		return r;
96 	}
97 	radeon_semaphore_emit_signal(rdev, signaler, semaphore);
98 	radeon_ring_commit(rdev, &rdev->ring[signaler]);
99 
100 	/* we assume caller has already allocated space on waiters ring */
101 	radeon_semaphore_emit_wait(rdev, waiter, semaphore);
102 
103 	/* for debugging lockup only, used by sysfs debug files */
104 	rdev->ring[signaler].last_semaphore_signal_addr = semaphore->gpu_addr;
105 	rdev->ring[waiter].last_semaphore_wait_addr = semaphore->gpu_addr;
106 
107 	return 0;
108 }
109 
110 void radeon_semaphore_free(struct radeon_device *rdev,
111 			   struct radeon_semaphore **semaphore,
112 			   struct radeon_fence *fence)
113 {
114 	if (semaphore == NULL || *semaphore == NULL) {
115 		return;
116 	}
117 	if ((*semaphore)->waiters > 0) {
118 		dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
119 			" hardware lockup imminent!\n", *semaphore);
120 	}
121 	radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
122 	kfree(*semaphore);
123 	*semaphore = NULL;
124 }
125