drm/radeon: Import the Radeon KMS driver from FreeBSD
[dragonfly.git] / sys / dev / drm / radeon / radeon_semaphore.c
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  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_semaphore.c 254885 2013-08-25 19:37:15Z dumbbell $
26  */
27
28 /*
29  * Authors:
30  *    Christian König <deathsimple@vodafone.de>
31  */
32 #include <drm/drmP.h>
33 #include "radeon.h"
34
35
36 int radeon_semaphore_create(struct radeon_device *rdev,
37                             struct radeon_semaphore **semaphore)
38 {
39         int r;
40
41         *semaphore = kmalloc(sizeof(struct radeon_semaphore), DRM_MEM_DRIVER,
42                              M_WAITOK);
43         if (*semaphore == NULL) {
44                 return -ENOMEM;
45         }
46         r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
47                              &(*semaphore)->sa_bo, 8, 8, true);
48         if (r) {
49                 drm_free(*semaphore, DRM_MEM_DRIVER);
50                 *semaphore = NULL;
51                 return r;
52         }
53         (*semaphore)->waiters = 0;
54         (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
55         *((uint64_t*)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
56         return 0;
57 }
58
59 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
60                                   struct radeon_semaphore *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         ++semaphore->waiters;
70         radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
71 }
72
73 /* caller must hold ring lock */
74 int radeon_semaphore_sync_rings(struct radeon_device *rdev,
75                                 struct radeon_semaphore *semaphore,
76                                 int signaler, int waiter)
77 {
78         int r;
79
80         /* no need to signal and wait on the same ring */
81         if (signaler == waiter) {
82                 return 0;
83         }
84
85         /* prevent GPU deadlocks */
86         if (!rdev->ring[signaler].ready) {
87                 dev_err(rdev->dev, "Trying to sync to a disabled ring!");
88                 return -EINVAL;
89         }
90
91         r = radeon_ring_alloc(rdev, &rdev->ring[signaler], 8);
92         if (r) {
93                 return r;
94         }
95         radeon_semaphore_emit_signal(rdev, signaler, semaphore);
96         radeon_ring_commit(rdev, &rdev->ring[signaler]);
97
98         /* we assume caller has already allocated space on waiters ring */
99         radeon_semaphore_emit_wait(rdev, waiter, semaphore);
100
101         /* for debugging lockup only, used by sysfs debug files */
102         rdev->ring[signaler].last_semaphore_signal_addr = semaphore->gpu_addr;
103         rdev->ring[waiter].last_semaphore_wait_addr = semaphore->gpu_addr;
104
105         return 0;
106 }
107
108 void radeon_semaphore_free(struct radeon_device *rdev,
109                            struct radeon_semaphore **semaphore,
110                            struct radeon_fence *fence)
111 {
112         if (semaphore == NULL || *semaphore == NULL) {
113                 return;
114         }
115         if ((*semaphore)->waiters > 0) {
116                 dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
117                         " hardware lockup imminent!\n", *semaphore);
118         }
119         radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
120         drm_free(*semaphore, DRM_MEM_DRIVER);
121         *semaphore = NULL;
122 }