1 /*
2 * Copyright 2012-15 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 * Authors: AMD
23 *
24 */
25
26 #include "dm_services.h"
27 #include "include/vector.h"
28
dal_vector_construct(struct vector * vector,struct dc_context * ctx,uint32_t capacity,uint32_t struct_size)29 bool dal_vector_construct(
30 struct vector *vector,
31 struct dc_context *ctx,
32 uint32_t capacity,
33 uint32_t struct_size)
34 {
35 vector->container = NULL;
36
37 if (!struct_size || !capacity) {
38 /* Container must be non-zero size*/
39 BREAK_TO_DEBUGGER();
40 return false;
41 }
42
43 vector->container = kcalloc(capacity, struct_size, GFP_KERNEL);
44 if (vector->container == NULL)
45 return false;
46 vector->capacity = capacity;
47 vector->struct_size = struct_size;
48 vector->count = 0;
49 vector->ctx = ctx;
50 return true;
51 }
52
53 static
dal_vector_presized_costruct(struct vector * vector,struct dc_context * ctx,uint32_t count,void * initial_value,uint32_t struct_size)54 bool dal_vector_presized_costruct(
55 struct vector *vector,
56 struct dc_context *ctx,
57 uint32_t count,
58 void *initial_value,
59 uint32_t struct_size)
60 {
61 uint32_t i;
62
63 vector->container = NULL;
64
65 if (!struct_size || !count) {
66 /* Container must be non-zero size*/
67 BREAK_TO_DEBUGGER();
68 return false;
69 }
70
71 vector->container = kcalloc(count, struct_size, GFP_KERNEL);
72
73 if (vector->container == NULL)
74 return false;
75
76 /* If caller didn't supply initial value then the default
77 * of all zeros is expected, which is exactly what dal_alloc()
78 * initialises the memory to. */
79 if (NULL != initial_value) {
80 for (i = 0; i < count; ++i)
81 memmove(
82 vector->container + i * struct_size,
83 initial_value,
84 struct_size);
85 }
86
87 vector->capacity = count;
88 vector->struct_size = struct_size;
89 vector->count = count;
90 return true;
91 }
92
dal_vector_presized_create(struct dc_context * ctx,uint32_t size,void * initial_value,uint32_t struct_size)93 struct vector *dal_vector_presized_create(
94 struct dc_context *ctx,
95 uint32_t size,
96 void *initial_value,
97 uint32_t struct_size)
98 {
99 struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
100
101 if (vector == NULL)
102 return NULL;
103
104 if (dal_vector_presized_costruct(
105 vector, ctx, size, initial_value, struct_size))
106 return vector;
107
108 BREAK_TO_DEBUGGER();
109 kfree(vector);
110 return NULL;
111 }
112
dal_vector_create(struct dc_context * ctx,uint32_t capacity,uint32_t struct_size)113 struct vector *dal_vector_create(
114 struct dc_context *ctx,
115 uint32_t capacity,
116 uint32_t struct_size)
117 {
118 struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
119
120 if (vector == NULL)
121 return NULL;
122
123 if (dal_vector_construct(vector, ctx, capacity, struct_size))
124 return vector;
125
126 BREAK_TO_DEBUGGER();
127 kfree(vector);
128 return NULL;
129 }
130
dal_vector_destruct(struct vector * vector)131 void dal_vector_destruct(
132 struct vector *vector)
133 {
134 kfree(vector->container);
135 vector->count = 0;
136 vector->capacity = 0;
137 }
138
dal_vector_destroy(struct vector ** vector)139 void dal_vector_destroy(
140 struct vector **vector)
141 {
142 if (vector == NULL || *vector == NULL)
143 return;
144 dal_vector_destruct(*vector);
145 kfree(*vector);
146 *vector = NULL;
147 }
148
dal_vector_get_count(const struct vector * vector)149 uint32_t dal_vector_get_count(
150 const struct vector *vector)
151 {
152 return vector->count;
153 }
154
dal_vector_at_index(const struct vector * vector,uint32_t index)155 void *dal_vector_at_index(
156 const struct vector *vector,
157 uint32_t index)
158 {
159 if (vector->container == NULL || index >= vector->count)
160 return NULL;
161 return vector->container + (index * vector->struct_size);
162 }
163
dal_vector_remove_at_index(struct vector * vector,uint32_t index)164 bool dal_vector_remove_at_index(
165 struct vector *vector,
166 uint32_t index)
167 {
168 if (index >= vector->count)
169 return false;
170
171 if (index != vector->count - 1)
172 memmove(
173 vector->container + (index * vector->struct_size),
174 vector->container + ((index + 1) * vector->struct_size),
175 (vector->count - index - 1) * vector->struct_size);
176 vector->count -= 1;
177
178 return true;
179 }
180
dal_vector_set_at_index(const struct vector * vector,const void * what,uint32_t index)181 void dal_vector_set_at_index(
182 const struct vector *vector,
183 const void *what,
184 uint32_t index)
185 {
186 void *where = dal_vector_at_index(vector, index);
187
188 if (!where) {
189 BREAK_TO_DEBUGGER();
190 return;
191 }
192 memmove(
193 where,
194 what,
195 vector->struct_size);
196 }
197
calc_increased_capacity(uint32_t old_capacity)198 static inline uint32_t calc_increased_capacity(
199 uint32_t old_capacity)
200 {
201 return old_capacity * 2;
202 }
203
dal_vector_insert_at(struct vector * vector,const void * what,uint32_t position)204 bool dal_vector_insert_at(
205 struct vector *vector,
206 const void *what,
207 uint32_t position)
208 {
209 uint8_t *insert_address;
210
211 if (vector->count == vector->capacity) {
212 if (!dal_vector_reserve(
213 vector,
214 calc_increased_capacity(vector->capacity)))
215 return false;
216 }
217
218 insert_address = vector->container + (vector->struct_size * position);
219
220 if (vector->count && position < vector->count)
221 memmove(
222 insert_address + vector->struct_size,
223 insert_address,
224 vector->struct_size * (vector->count - position));
225
226 memmove(
227 insert_address,
228 what,
229 vector->struct_size);
230
231 vector->count++;
232
233 return true;
234 }
235
dal_vector_append(struct vector * vector,const void * item)236 bool dal_vector_append(
237 struct vector *vector,
238 const void *item)
239 {
240 return dal_vector_insert_at(vector, item, vector->count);
241 }
242
dal_vector_clone(const struct vector * vector)243 struct vector *dal_vector_clone(
244 const struct vector *vector)
245 {
246 struct vector *vec_cloned;
247 uint32_t count;
248
249 /* create new vector */
250 count = dal_vector_get_count(vector);
251
252 if (count == 0)
253 /* when count is 0 we still want to create clone of the vector
254 */
255 vec_cloned = dal_vector_create(
256 vector->ctx,
257 vector->capacity,
258 vector->struct_size);
259 else
260 /* Call "presized create" version, independently of how the
261 * original vector was created.
262 * The owner of original vector must know how to treat the new
263 * vector - as "presized" or as "regular".
264 * But from vector point of view it doesn't matter. */
265 vec_cloned = dal_vector_presized_create(vector->ctx, count,
266 NULL,/* no initial value */
267 vector->struct_size);
268
269 if (NULL == vec_cloned) {
270 BREAK_TO_DEBUGGER();
271 return NULL;
272 }
273
274 /* copy vector's data */
275 memmove(vec_cloned->container, vector->container,
276 vec_cloned->struct_size * vec_cloned->capacity);
277
278 return vec_cloned;
279 }
280
dal_vector_capacity(const struct vector * vector)281 uint32_t dal_vector_capacity(const struct vector *vector)
282 {
283 return vector->capacity;
284 }
285
dal_vector_reserve(struct vector * vector,uint32_t capacity)286 bool dal_vector_reserve(struct vector *vector, uint32_t capacity)
287 {
288 void *new_container;
289
290 if (capacity <= vector->capacity)
291 return true;
292
293 new_container = krealloc(vector->container,
294 capacity * vector->struct_size, M_DRM, GFP_KERNEL);
295
296 if (new_container) {
297 vector->container = new_container;
298 vector->capacity = capacity;
299 return true;
300 }
301
302 return false;
303 }
304
dal_vector_clear(struct vector * vector)305 void dal_vector_clear(struct vector *vector)
306 {
307 vector->count = 0;
308 }
309