2 * Copyright 2011 Red Hat Inc.
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 * Jerome Glisse <glisse@freedesktop.org>
34 int radeon_sa_bo_manager_init(struct radeon_device *rdev,
35 struct radeon_sa_manager *sa_manager,
36 unsigned size, u32 domain)
40 spin_lock_init(&sa_manager->lock);
41 sa_manager->bo = NULL;
42 sa_manager->size = size;
43 sa_manager->domain = domain;
44 INIT_LIST_HEAD(&sa_manager->sa_bo);
46 r = radeon_bo_create(rdev, size, RADEON_GPU_PAGE_SIZE, true,
47 RADEON_GEM_DOMAIN_CPU, &sa_manager->bo);
49 dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
56 void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
57 struct radeon_sa_manager *sa_manager)
59 struct radeon_sa_bo *sa_bo, *tmp;
61 if (!list_empty(&sa_manager->sa_bo)) {
62 dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n");
64 list_for_each_entry_safe(sa_bo, tmp, &sa_manager->sa_bo, list) {
65 list_del_init(&sa_bo->list);
67 radeon_bo_unref(&sa_manager->bo);
71 int radeon_sa_bo_manager_start(struct radeon_device *rdev,
72 struct radeon_sa_manager *sa_manager)
76 if (sa_manager->bo == NULL) {
77 dev_err(rdev->dev, "no bo for sa manager\n");
82 r = radeon_bo_reserve(sa_manager->bo, false);
84 dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
87 r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
89 radeon_bo_unreserve(sa_manager->bo);
90 dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
93 r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
94 radeon_bo_unreserve(sa_manager->bo);
98 int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
99 struct radeon_sa_manager *sa_manager)
103 if (sa_manager->bo == NULL) {
104 dev_err(rdev->dev, "no bo for sa manager\n");
108 r = radeon_bo_reserve(sa_manager->bo, false);
110 radeon_bo_kunmap(sa_manager->bo);
111 radeon_bo_unpin(sa_manager->bo);
112 radeon_bo_unreserve(sa_manager->bo);
118 * Principe is simple, we keep a list of sub allocation in offset
119 * order (first entry has offset == 0, last entry has the highest
122 * When allocating new object we first check if there is room at
123 * the end total_size - (last_object_offset + last_object_size) >=
124 * alloc_size. If so we allocate new object there.
126 * When there is not enough room at the end, we start waiting for
127 * each sub object until we reach object_offset+object_size >=
128 * alloc_size, this object then become the sub object we return.
130 * Alignment can't be bigger than page size
132 int radeon_sa_bo_new(struct radeon_device *rdev,
133 struct radeon_sa_manager *sa_manager,
134 struct radeon_sa_bo *sa_bo,
135 unsigned size, unsigned align)
137 struct radeon_sa_bo *tmp;
138 struct list_head *head;
139 unsigned offset = 0, wasted = 0;
141 BUG_ON(align > RADEON_GPU_PAGE_SIZE);
142 BUG_ON(size > sa_manager->size);
143 spin_lock(&sa_manager->lock);
146 head = sa_manager->sa_bo.prev;
147 if (list_empty(&sa_manager->sa_bo)) {
151 /* look for a hole big enough */
153 list_for_each_entry(tmp, &sa_manager->sa_bo, list) {
154 /* room before this object ? */
155 if (offset < tmp->offset && (tmp->offset - offset) >= size) {
156 head = tmp->list.prev;
159 offset = tmp->offset + tmp->size;
160 wasted = offset % align;
162 wasted = align - wasted;
166 /* room at the end ? */
167 head = sa_manager->sa_bo.prev;
168 tmp = list_entry(head, struct radeon_sa_bo, list);
169 offset = tmp->offset + tmp->size;
170 wasted = offset % align;
172 wasted = align - wasted;
175 if ((sa_manager->size - offset) < size) {
176 /* failed to find somethings big enough */
177 spin_unlock(&sa_manager->lock);
182 sa_bo->manager = sa_manager;
183 sa_bo->offset = offset;
185 list_add(&sa_bo->list, head);
186 spin_unlock(&sa_manager->lock);
190 void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo *sa_bo)
192 spin_lock(&sa_bo->manager->lock);
193 list_del_init(&sa_bo->list);
194 spin_unlock(&sa_bo->manager->lock);
197 #if defined(CONFIG_DEBUG_FS)
198 void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
201 struct radeon_sa_bo *i;
203 spin_lock(&sa_manager->lock);
204 list_for_each_entry(i, &sa_manager->sa_bo, list) {
205 seq_printf(m, "offset %08d: size %4d\n", i->offset, i->size);
207 spin_unlock(&sa_manager->lock);