amdgpu: add CS dependencies v2
[platform/upstream/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 #include <stdlib.h>
25 #include <string.h>
26 #include "amdgpu.h"
27 #include "amdgpu_drm.h"
28 #include "amdgpu_internal.h"
29 #include "util_math.h"
30
31 static struct amdgpu_bo_va_mgr vamgr = {{0}};
32
33 static void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, struct amdgpu_device *dev)
34 {
35         mgr->va_offset = dev->dev_info.virtual_address_offset;
36         mgr->va_max = dev->dev_info.virtual_address_max;
37         mgr->va_alignment = dev->dev_info.virtual_address_alignment;
38
39         list_inithead(&mgr->va_holes);
40         pthread_mutex_init(&mgr->bo_va_mutex, NULL);
41 }
42
43 static void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr)
44 {
45         struct amdgpu_bo_va_hole *hole;
46         LIST_FOR_EACH_ENTRY(hole, &mgr->va_holes, list) {
47                 list_del(&hole->list);
48                 free(hole);
49         }
50         pthread_mutex_destroy(&mgr->bo_va_mutex);
51 }
52
53 struct amdgpu_bo_va_mgr * amdgpu_vamgr_get_global(struct amdgpu_device *dev)
54 {
55         int ref;
56         ref = atomic_inc_return(&vamgr.refcount);
57
58         if (ref == 1)
59                 amdgpu_vamgr_init(&vamgr, dev);
60         return &vamgr;
61 }
62
63 void amdgpu_vamgr_reference(struct amdgpu_bo_va_mgr **dst,
64                                 struct amdgpu_bo_va_mgr *src)
65 {
66         if (update_references(&(*dst)->refcount, NULL))
67                 amdgpu_vamgr_deinit(*dst);
68         *dst = src;
69 }
70
71 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr,
72                                 uint64_t size, uint64_t alignment)
73 {
74         struct amdgpu_bo_va_hole *hole, *n;
75         uint64_t offset = 0, waste = 0;
76
77         alignment = MAX2(alignment, mgr->va_alignment);
78         size = ALIGN(size, mgr->va_alignment);
79
80         pthread_mutex_lock(&mgr->bo_va_mutex);
81         /* TODO: using more appropriate way to track the holes */
82         /* first look for a hole */
83         LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
84                 offset = hole->offset;
85                 waste = offset % alignment;
86                 waste = waste ? alignment - waste : 0;
87                 offset += waste;
88                 if (offset >= (hole->offset + hole->size)) {
89                         continue;
90                 }
91                 if (!waste && hole->size == size) {
92                         offset = hole->offset;
93                         list_del(&hole->list);
94                         free(hole);
95                         pthread_mutex_unlock(&mgr->bo_va_mutex);
96                         return offset;
97                 }
98                 if ((hole->size - waste) > size) {
99                         if (waste) {
100                                 n = calloc(1,
101                                                 sizeof(struct amdgpu_bo_va_hole));
102                                 n->size = waste;
103                                 n->offset = hole->offset;
104                                 list_add(&n->list, &hole->list);
105                         }
106                         hole->size -= (size + waste);
107                         hole->offset += size + waste;
108                         pthread_mutex_unlock(&mgr->bo_va_mutex);
109                         return offset;
110                 }
111                 if ((hole->size - waste) == size) {
112                         hole->size = waste;
113                         pthread_mutex_unlock(&mgr->bo_va_mutex);
114                         return offset;
115                 }
116         }
117
118         offset = mgr->va_offset;
119         waste = offset % alignment;
120         waste = waste ? alignment - waste : 0;
121
122         if (offset + waste + size > mgr->va_max) {
123                 pthread_mutex_unlock(&mgr->bo_va_mutex);
124                 return AMDGPU_INVALID_VA_ADDRESS;
125         }
126
127         if (waste) {
128                 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
129                 n->size = waste;
130                 n->offset = offset;
131                 list_add(&n->list, &mgr->va_holes);
132         }
133         offset += waste;
134         mgr->va_offset += size + waste;
135         pthread_mutex_unlock(&mgr->bo_va_mutex);
136         return offset;
137 }
138
139 void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr,
140                                 uint64_t va, uint64_t size)
141 {
142         struct amdgpu_bo_va_hole *hole;
143
144         if (va == AMDGPU_INVALID_VA_ADDRESS)
145                 return;
146
147         size = ALIGN(size, mgr->va_alignment);
148
149         pthread_mutex_lock(&mgr->bo_va_mutex);
150         if ((va + size) == mgr->va_offset) {
151                 mgr->va_offset = va;
152                 /* Delete uppermost hole if it reaches the new top */
153                 if (!LIST_IS_EMPTY(&mgr->va_holes)) {
154                         hole = container_of(mgr->va_holes.next, hole, list);
155                         if ((hole->offset + hole->size) == va) {
156                                 mgr->va_offset = hole->offset;
157                                 list_del(&hole->list);
158                                 free(hole);
159                         }
160                 }
161         } else {
162                 struct amdgpu_bo_va_hole *next;
163
164                 hole = container_of(&mgr->va_holes, hole, list);
165                 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
166                         if (next->offset < va)
167                                 break;
168                         hole = next;
169                 }
170
171                 if (&hole->list != &mgr->va_holes) {
172                         /* Grow upper hole if it's adjacent */
173                         if (hole->offset == (va + size)) {
174                                 hole->offset = va;
175                                 hole->size += size;
176                                 /* Merge lower hole if it's adjacent */
177                                 if (next != hole
178                                                 && &next->list != &mgr->va_holes
179                                                 && (next->offset + next->size) == va) {
180                                         next->size += hole->size;
181                                         list_del(&hole->list);
182                                         free(hole);
183                                 }
184                                 goto out;
185                         }
186                 }
187
188                 /* Grow lower hole if it's adjacent */
189                 if (next != hole && &next->list != &mgr->va_holes &&
190                                 (next->offset + next->size) == va) {
191                         next->size += size;
192                         goto out;
193                 }
194
195                 /* FIXME on allocation failure we just lose virtual address space
196                  * maybe print a warning
197                  */
198                 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
199                 if (next) {
200                         next->size = size;
201                         next->offset = va;
202                         list_add(&next->list, &hole->list);
203                 }
204         }
205 out:
206         pthread_mutex_unlock(&mgr->bo_va_mutex);
207 }