2 * Copyright 2011 Christian König.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Christian König <deathsimple@vodafone.de>
34 static int allocate_semaphores(struct radeon_device *rdev)
36 const unsigned long bo_size = PAGE_SIZE * 4;
39 struct list_head new_entrys;
40 unsigned long irq_flags;
45 r = radeon_bo_create(rdev, bo_size, RADEON_GPU_PAGE_SIZE, true,
46 RADEON_GEM_DOMAIN_GTT, &bo);
48 dev_err(rdev->dev, "(%d) failed to allocate semaphore bo\n", r);
52 r = radeon_bo_reserve(bo, false);
55 dev_err(rdev->dev, "(%d) failed to reserve semaphore bo\n", r);
59 r = radeon_bo_kmap(bo, &map);
61 radeon_bo_unreserve(bo);
63 dev_err(rdev->dev, "(%d) semaphore map failed\n", r);
66 memset(map, 0, bo_size);
69 r = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
71 radeon_bo_unreserve(bo);
73 dev_err(rdev->dev, "(%d) semaphore pin failed\n", r);
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);
82 sem->gpu_addr = gpu_addr;
84 list_add_tail(&sem->list, &new_entrys);
87 radeon_bo_unreserve(bo);
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);
94 DRM_INFO("%d new semaphores allocated\n", (int)(bo_size/8));
99 int radeon_semaphore_create(struct radeon_device *rdev,
100 struct radeon_semaphore **semaphore)
102 unsigned long irq_flags;
104 write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
105 if (list_empty(&rdev->semaphore_drv.free)) {
107 write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
108 r = allocate_semaphores(rdev);
111 write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
114 *semaphore = list_first_entry(&rdev->semaphore_drv.free, struct radeon_semaphore, list);
115 list_del(&(*semaphore)->list);
117 write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
121 void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
122 struct radeon_semaphore *semaphore)
124 radeon_semaphore_ring_emit(rdev, ring, &rdev->cp[ring], semaphore, false);
127 void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
128 struct radeon_semaphore *semaphore)
130 radeon_semaphore_ring_emit(rdev, ring, &rdev->cp[ring], semaphore, true);
133 void radeon_semaphore_free(struct radeon_device *rdev,
134 struct radeon_semaphore *semaphore)
136 unsigned long irq_flags;
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);
143 void radeon_semaphore_driver_fini(struct radeon_device *rdev)
145 struct radeon_semaphore *i, *n;
146 struct list_head entrys;
147 unsigned long irq_flags;
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);
154 INIT_LIST_HEAD(&rdev->semaphore_drv.free);
155 write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
157 list_for_each_entry_safe(i, n, &entrys, list) {
158 radeon_bo_unref(&i->robj);