OSDN Git Service

amdgpu: use EINVAL instead of EBADMSG in amdgpu_bo_cpu_unmap()
[android-x86/external-libdrm.git] / amdgpu / amdgpu_vamgr.c
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
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 NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "amdgpu.h"
32 #include "amdgpu_drm.h"
33 #include "amdgpu_internal.h"
34 #include "util_math.h"
35
36 int amdgpu_va_range_query(amdgpu_device_handle dev,
37                           enum amdgpu_gpu_va_range type, uint64_t *start, uint64_t *end)
38 {
39         if (type == amdgpu_gpu_va_range_general) {
40                 *start = dev->dev_info.virtual_address_offset;
41                 *end = dev->dev_info.virtual_address_max;
42                 return 0;
43         }
44         return -EINVAL;
45 }
46
47 drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
48                               uint64_t max, uint64_t alignment)
49 {
50         mgr->va_offset = start;
51         mgr->va_max = max;
52         mgr->va_alignment = alignment;
53
54         list_inithead(&mgr->va_holes);
55         pthread_mutex_init(&mgr->bo_va_mutex, NULL);
56 }
57
58 drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr)
59 {
60         struct amdgpu_bo_va_hole *hole;
61         LIST_FOR_EACH_ENTRY(hole, &mgr->va_holes, list) {
62                 list_del(&hole->list);
63                 free(hole);
64         }
65         pthread_mutex_destroy(&mgr->bo_va_mutex);
66 }
67
68 drm_private uint64_t
69 amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
70                      uint64_t alignment, uint64_t base_required)
71 {
72         struct amdgpu_bo_va_hole *hole, *n;
73         uint64_t offset = 0, waste = 0;
74
75         alignment = MAX2(alignment, mgr->va_alignment);
76         size = ALIGN(size, mgr->va_alignment);
77
78         if (base_required % alignment)
79                 return AMDGPU_INVALID_VA_ADDRESS;
80
81         pthread_mutex_lock(&mgr->bo_va_mutex);
82         /* TODO: using more appropriate way to track the holes */
83         /* first look for a hole */
84         LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
85                 if (base_required) {
86                         if(hole->offset > base_required ||
87                                 (hole->offset + hole->size) < (base_required + size))
88                                 continue;
89                         waste = base_required - hole->offset;
90                         offset = base_required;
91                 } else {
92                         offset = hole->offset;
93                         waste = offset % alignment;
94                         waste = waste ? alignment - waste : 0;
95                         offset += waste;
96                         if (offset >= (hole->offset + hole->size)) {
97                                 continue;
98                         }
99                 }
100                 if (!waste && hole->size == size) {
101                         offset = hole->offset;
102                         list_del(&hole->list);
103                         free(hole);
104                         pthread_mutex_unlock(&mgr->bo_va_mutex);
105                         return offset;
106                 }
107                 if ((hole->size - waste) > size) {
108                         if (waste) {
109                                 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
110                                 n->size = waste;
111                                 n->offset = hole->offset;
112                                 list_add(&n->list, &hole->list);
113                         }
114                         hole->size -= (size + waste);
115                         hole->offset += size + waste;
116                         pthread_mutex_unlock(&mgr->bo_va_mutex);
117                         return offset;
118                 }
119                 if ((hole->size - waste) == size) {
120                         hole->size = waste;
121                         pthread_mutex_unlock(&mgr->bo_va_mutex);
122                         return offset;
123                 }
124         }
125
126         if (base_required) {
127                 if (base_required < mgr->va_offset)
128                         return AMDGPU_INVALID_VA_ADDRESS;
129                 offset = mgr->va_offset;
130                 waste = base_required - mgr->va_offset;
131         } else {
132                 offset = mgr->va_offset;
133                 waste = offset % alignment;
134                 waste = waste ? alignment - waste : 0;
135         }
136
137         if (offset + waste + size > mgr->va_max) {
138                 pthread_mutex_unlock(&mgr->bo_va_mutex);
139                 return AMDGPU_INVALID_VA_ADDRESS;
140         }
141
142         if (waste) {
143                 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
144                 n->size = waste;
145                 n->offset = offset;
146                 list_add(&n->list, &mgr->va_holes);
147         }
148
149         offset += waste;
150         mgr->va_offset += size + waste;
151         pthread_mutex_unlock(&mgr->bo_va_mutex);
152         return offset;
153 }
154
155 drm_private void
156 amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size)
157 {
158         struct amdgpu_bo_va_hole *hole;
159
160         if (va == AMDGPU_INVALID_VA_ADDRESS)
161                 return;
162
163         size = ALIGN(size, mgr->va_alignment);
164
165         pthread_mutex_lock(&mgr->bo_va_mutex);
166         if ((va + size) == mgr->va_offset) {
167                 mgr->va_offset = va;
168                 /* Delete uppermost hole if it reaches the new top */
169                 if (!LIST_IS_EMPTY(&mgr->va_holes)) {
170                         hole = container_of(mgr->va_holes.next, hole, list);
171                         if ((hole->offset + hole->size) == va) {
172                                 mgr->va_offset = hole->offset;
173                                 list_del(&hole->list);
174                                 free(hole);
175                         }
176                 }
177         } else {
178                 struct amdgpu_bo_va_hole *next;
179
180                 hole = container_of(&mgr->va_holes, hole, list);
181                 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
182                         if (next->offset < va)
183                                 break;
184                         hole = next;
185                 }
186
187                 if (&hole->list != &mgr->va_holes) {
188                         /* Grow upper hole if it's adjacent */
189                         if (hole->offset == (va + size)) {
190                                 hole->offset = va;
191                                 hole->size += size;
192                                 /* Merge lower hole if it's adjacent */
193                                 if (next != hole
194                                                 && &next->list != &mgr->va_holes
195                                                 && (next->offset + next->size) == va) {
196                                         next->size += hole->size;
197                                         list_del(&hole->list);
198                                         free(hole);
199                                 }
200                                 goto out;
201                         }
202                 }
203
204                 /* Grow lower hole if it's adjacent */
205                 if (next != hole && &next->list != &mgr->va_holes &&
206                                 (next->offset + next->size) == va) {
207                         next->size += size;
208                         goto out;
209                 }
210
211                 /* FIXME on allocation failure we just lose virtual address space
212                  * maybe print a warning
213                  */
214                 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
215                 if (next) {
216                         next->size = size;
217                         next->offset = va;
218                         list_add(&next->list, &hole->list);
219                 }
220         }
221 out:
222         pthread_mutex_unlock(&mgr->bo_va_mutex);
223 }
224
225 int amdgpu_va_range_alloc(amdgpu_device_handle dev,
226                           enum amdgpu_gpu_va_range va_range_type,
227                           uint64_t size,
228                           uint64_t va_base_alignment,
229                           uint64_t va_base_required,
230                           uint64_t *va_base_allocated,
231                           amdgpu_va_handle *va_range_handle,
232                           uint64_t flags)
233 {
234         struct amdgpu_bo_va_mgr *vamgr;
235
236         if (flags & AMDGPU_VA_RANGE_32_BIT)
237                 vamgr = dev->vamgr_32;
238         else
239                 vamgr = dev->vamgr;
240
241         va_base_alignment = MAX2(va_base_alignment, vamgr->va_alignment);
242         size = ALIGN(size, vamgr->va_alignment);
243
244         *va_base_allocated = amdgpu_vamgr_find_va(vamgr, size,
245                                         va_base_alignment, va_base_required);
246
247         if (!(flags & AMDGPU_VA_RANGE_32_BIT) &&
248             (*va_base_allocated == AMDGPU_INVALID_VA_ADDRESS)) {
249                 /* fallback to 32bit address */
250                 vamgr = dev->vamgr_32;
251                 *va_base_allocated = amdgpu_vamgr_find_va(vamgr, size,
252                                         va_base_alignment, va_base_required);
253         }
254
255         if (*va_base_allocated != AMDGPU_INVALID_VA_ADDRESS) {
256                 struct amdgpu_va* va;
257                 va = calloc(1, sizeof(struct amdgpu_va));
258                 if(!va){
259                         amdgpu_vamgr_free_va(vamgr, *va_base_allocated, size);
260                         return -ENOMEM;
261                 }
262                 va->dev = dev;
263                 va->address = *va_base_allocated;
264                 va->size = size;
265                 va->range = va_range_type;
266                 va->vamgr = vamgr;
267                 *va_range_handle = va;
268         } else {
269                 return -EINVAL;
270         }
271
272         return 0;
273 }
274
275 int amdgpu_va_range_free(amdgpu_va_handle va_range_handle)
276 {
277         if(!va_range_handle || !va_range_handle->address)
278                 return 0;
279
280         amdgpu_vamgr_free_va(va_range_handle->vamgr,
281                         va_range_handle->address,
282                         va_range_handle->size);
283         free(va_range_handle);
284         return 0;
285 }