Merge branch 'drm-radeon-testing' of ../drm-radeon-next into drm-core-next
[linux-2.6-block.git] / drivers / gpu / 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  */
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 static int allocate_semaphores(struct radeon_device *rdev)
35 {
36         const unsigned long bo_size = PAGE_SIZE * 4;
37
38         struct radeon_bo *bo;
39         struct list_head new_entrys;
40         unsigned long irq_flags;
41         uint64_t gpu_addr;
42         void *map;
43         int i, r;
44
45         r = radeon_bo_create(rdev, bo_size, RADEON_GPU_PAGE_SIZE, true,
46                              RADEON_GEM_DOMAIN_GTT, &bo);
47         if (r) {
48                 dev_err(rdev->dev, "(%d) failed to allocate semaphore bo\n", r);
49                 return r;
50         }
51
52         r = radeon_bo_reserve(bo, false);
53         if (r) {
54                 radeon_bo_unref(&bo);
55                 dev_err(rdev->dev, "(%d) failed to reserve semaphore bo\n", r);
56                 return r;
57         }
58
59         r = radeon_bo_kmap(bo, &map);
60         if (r) {
61                 radeon_bo_unreserve(bo);
62                 radeon_bo_unref(&bo);
63                 dev_err(rdev->dev, "(%d) semaphore map failed\n", r);
64                 return r;
65         }
66         memset(map, 0, bo_size);
67         radeon_bo_kunmap(bo);
68
69         r = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
70         if (r) {
71                 radeon_bo_unreserve(bo);
72                 radeon_bo_unref(&bo);
73                 dev_err(rdev->dev, "(%d) semaphore pin failed\n", r);
74                 return r;
75         }
76
77         INIT_LIST_HEAD(&new_entrys);
78         for (i = 0; i < bo_size/8; ++i) {
79                 struct radeon_semaphore *sem = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
80                 ttm_bo_reference(&bo->tbo);
81                 sem->robj = bo;
82                 sem->gpu_addr = gpu_addr;
83                 gpu_addr += 8;
84                 list_add_tail(&sem->list, &new_entrys);
85         }
86
87         radeon_bo_unreserve(bo);
88         radeon_bo_unref(&bo);
89
90         write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
91         list_splice_tail(&new_entrys, &rdev->semaphore_drv.free);
92         write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
93
94         DRM_INFO("%d new semaphores allocated\n", (int)(bo_size/8));
95
96         return 0;
97 }
98
99 int radeon_semaphore_create(struct radeon_device *rdev,
100                             struct radeon_semaphore **semaphore)
101 {
102         unsigned long irq_flags;
103
104         write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
105         if (list_empty(&rdev->semaphore_drv.free)) {
106                 int r;
107                 write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
108                 r = allocate_semaphores(rdev);
109                 if (r)
110                         return r;
111                 write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
112         }
113
114         *semaphore = list_first_entry(&rdev->semaphore_drv.free, struct radeon_semaphore, list);
115         list_del(&(*semaphore)->list);
116
117         write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
118         return 0;
119 }
120
121 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
122                                   struct radeon_semaphore *semaphore)
123 {
124         radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
125 }
126
127 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
128                                 struct radeon_semaphore *semaphore)
129 {
130         radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
131 }
132
133 void radeon_semaphore_free(struct radeon_device *rdev,
134                           struct radeon_semaphore *semaphore)
135 {
136         unsigned long irq_flags;
137
138         write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
139         list_add_tail(&semaphore->list, &rdev->semaphore_drv.free);
140         write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
141 }
142
143 void radeon_semaphore_driver_fini(struct radeon_device *rdev)
144 {
145         struct radeon_semaphore *i, *n;
146         struct list_head entrys;
147         unsigned long irq_flags;
148
149         INIT_LIST_HEAD(&entrys);
150         write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
151         if (!list_empty(&rdev->semaphore_drv.free)) {
152                 list_splice(&rdev->semaphore_drv.free, &entrys);
153         }
154         INIT_LIST_HEAD(&rdev->semaphore_drv.free);
155         write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
156
157         list_for_each_entry_safe(i, n, &entrys, list) {
158                 radeon_bo_unref(&i->robj);
159                 kfree(i);
160         }
161 }