amdgpu: validate the upper limit of virtual address 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 void amdgpu_vamgr_init(struct amdgpu_device *dev)
32 {
33         struct amdgpu_bo_va_mgr *vamgr = &dev->vamgr;
34
35         vamgr->va_offset = dev->dev_info.virtual_address_offset;
36         vamgr->va_max = dev->dev_info.virtual_address_max;
37         vamgr->va_alignment = dev->dev_info.virtual_address_alignment;
38
39         list_inithead(&vamgr->va_holes);
40         pthread_mutex_init(&vamgr->bo_va_mutex, NULL);
41 }
42
43 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr,
44                                uint64_t size, uint64_t alignment)
45 {
46         struct amdgpu_bo_va_hole *hole, *n;
47         uint64_t offset = 0, waste = 0;
48
49         alignment = MAX2(alignment, mgr->va_alignment);
50         size = ALIGN(size, mgr->va_alignment);
51
52         pthread_mutex_lock(&mgr->bo_va_mutex);
53         /* TODO: using more appropriate way to track the holes */
54         /* first look for a hole */
55         LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
56                 offset = hole->offset;
57                 waste = offset % alignment;
58                 waste = waste ? alignment - waste : 0;
59                 offset += waste;
60                 if (offset >= (hole->offset + hole->size)) {
61                         continue;
62                 }
63                 if (!waste && hole->size == size) {
64                         offset = hole->offset;
65                         list_del(&hole->list);
66                         free(hole);
67                         pthread_mutex_unlock(&mgr->bo_va_mutex);
68                         return offset;
69                 }
70                 if ((hole->size - waste) > size) {
71                         if (waste) {
72                                 n = calloc(1,
73                                            sizeof(struct amdgpu_bo_va_hole));
74                                 n->size = waste;
75                                 n->offset = hole->offset;
76                                 list_add(&n->list, &hole->list);
77                         }
78                         hole->size -= (size + waste);
79                         hole->offset += size + waste;
80                         pthread_mutex_unlock(&mgr->bo_va_mutex);
81                         return offset;
82                 }
83                 if ((hole->size - waste) == size) {
84                         hole->size = waste;
85                         pthread_mutex_unlock(&mgr->bo_va_mutex);
86                         return offset;
87                 }
88         }
89
90         offset = mgr->va_offset;
91         waste = offset % alignment;
92         waste = waste ? alignment - waste : 0;
93
94         if (offset + waste + size > mgr->va_max) {
95                 pthread_mutex_unlock(&mgr->bo_va_mutex);
96                 return AMDGPU_INVALID_VA_ADDRESS;
97         }
98
99         if (waste) {
100                 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
101                 n->size = waste;
102                 n->offset = offset;
103                 list_add(&n->list, &mgr->va_holes);
104         }
105         offset += waste;
106         mgr->va_offset += size + waste;
107         pthread_mutex_unlock(&mgr->bo_va_mutex);
108         return offset;
109 }
110
111 void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
112                            uint64_t size)
113 {
114         struct amdgpu_bo_va_hole *hole;
115
116         size = ALIGN(size, mgr->va_alignment);
117
118         pthread_mutex_lock(&mgr->bo_va_mutex);
119         if ((va + size) == mgr->va_offset) {
120                 mgr->va_offset = va;
121                 /* Delete uppermost hole if it reaches the new top */
122                 if (!LIST_IS_EMPTY(&mgr->va_holes)) {
123                         hole = container_of(mgr->va_holes.next, hole, list);
124                         if ((hole->offset + hole->size) == va) {
125                                 mgr->va_offset = hole->offset;
126                                 list_del(&hole->list);
127                                 free(hole);
128                         }
129                 }
130         } else {
131                 struct amdgpu_bo_va_hole *next;
132
133                 hole = container_of(&mgr->va_holes, hole, list);
134                 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
135                         if (next->offset < va)
136                                 break;
137                         hole = next;
138                 }
139
140                 if (&hole->list != &mgr->va_holes) {
141                         /* Grow upper hole if it's adjacent */
142                         if (hole->offset == (va + size)) {
143                                 hole->offset = va;
144                                 hole->size += size;
145                                 /* Merge lower hole if it's adjacent */
146                                 if (next != hole
147                                     && &next->list != &mgr->va_holes
148                                     && (next->offset + next->size) == va) {
149                                         next->size += hole->size;
150                                         list_del(&hole->list);
151                                         free(hole);
152                                 }
153                                 goto out;
154                         }
155                 }
156
157                 /* Grow lower hole if it's adjacent */
158                 if (next != hole && &next->list != &mgr->va_holes &&
159                     (next->offset + next->size) == va) {
160                         next->size += size;
161                         goto out;
162                 }
163
164                 /* FIXME on allocation failure we just lose virtual address space
165                  * maybe print a warning
166                  */
167                 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
168                 if (next) {
169                         next->size = size;
170                         next->offset = va;
171                         list_add(&next->list, &hole->list);
172                 }
173         }
174 out:
175         pthread_mutex_unlock(&mgr->bo_va_mutex);
176 }