xref: /dflybsd-src/sys/dev/drm/amd/display/dc/basics/vector.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2012-15 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Authors: AMD
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  */
25b843c749SSergey Zigachev 
26b843c749SSergey Zigachev #include "dm_services.h"
27b843c749SSergey Zigachev #include "include/vector.h"
28b843c749SSergey Zigachev 
dal_vector_construct(struct vector * vector,struct dc_context * ctx,uint32_t capacity,uint32_t struct_size)29b843c749SSergey Zigachev bool dal_vector_construct(
30b843c749SSergey Zigachev 	struct vector *vector,
31b843c749SSergey Zigachev 	struct dc_context *ctx,
32b843c749SSergey Zigachev 	uint32_t capacity,
33b843c749SSergey Zigachev 	uint32_t struct_size)
34b843c749SSergey Zigachev {
35b843c749SSergey Zigachev 	vector->container = NULL;
36b843c749SSergey Zigachev 
37b843c749SSergey Zigachev 	if (!struct_size || !capacity) {
38b843c749SSergey Zigachev 		/* Container must be non-zero size*/
39b843c749SSergey Zigachev 		BREAK_TO_DEBUGGER();
40b843c749SSergey Zigachev 		return false;
41b843c749SSergey Zigachev 	}
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev 	vector->container = kcalloc(capacity, struct_size, GFP_KERNEL);
44b843c749SSergey Zigachev 	if (vector->container == NULL)
45b843c749SSergey Zigachev 		return false;
46b843c749SSergey Zigachev 	vector->capacity = capacity;
47b843c749SSergey Zigachev 	vector->struct_size = struct_size;
48b843c749SSergey Zigachev 	vector->count = 0;
49b843c749SSergey Zigachev 	vector->ctx = ctx;
50b843c749SSergey Zigachev 	return true;
51b843c749SSergey Zigachev }
52b843c749SSergey Zigachev 
53*78973132SSergey Zigachev static
dal_vector_presized_costruct(struct vector * vector,struct dc_context * ctx,uint32_t count,void * initial_value,uint32_t struct_size)54b843c749SSergey Zigachev bool dal_vector_presized_costruct(
55b843c749SSergey Zigachev 	struct vector *vector,
56b843c749SSergey Zigachev 	struct dc_context *ctx,
57b843c749SSergey Zigachev 	uint32_t count,
58b843c749SSergey Zigachev 	void *initial_value,
59b843c749SSergey Zigachev 	uint32_t struct_size)
60b843c749SSergey Zigachev {
61b843c749SSergey Zigachev 	uint32_t i;
62b843c749SSergey Zigachev 
63b843c749SSergey Zigachev 	vector->container = NULL;
64b843c749SSergey Zigachev 
65b843c749SSergey Zigachev 	if (!struct_size || !count) {
66b843c749SSergey Zigachev 		/* Container must be non-zero size*/
67b843c749SSergey Zigachev 		BREAK_TO_DEBUGGER();
68b843c749SSergey Zigachev 		return false;
69b843c749SSergey Zigachev 	}
70b843c749SSergey Zigachev 
71b843c749SSergey Zigachev 	vector->container = kcalloc(count, struct_size, GFP_KERNEL);
72b843c749SSergey Zigachev 
73b843c749SSergey Zigachev 	if (vector->container == NULL)
74b843c749SSergey Zigachev 		return false;
75b843c749SSergey Zigachev 
76b843c749SSergey Zigachev 	/* If caller didn't supply initial value then the default
77b843c749SSergey Zigachev 	 * of all zeros is expected, which is exactly what dal_alloc()
78b843c749SSergey Zigachev 	 * initialises the memory to. */
79b843c749SSergey Zigachev 	if (NULL != initial_value) {
80b843c749SSergey Zigachev 		for (i = 0; i < count; ++i)
81b843c749SSergey Zigachev 			memmove(
82b843c749SSergey Zigachev 				vector->container + i * struct_size,
83b843c749SSergey Zigachev 				initial_value,
84b843c749SSergey Zigachev 				struct_size);
85b843c749SSergey Zigachev 	}
86b843c749SSergey Zigachev 
87b843c749SSergey Zigachev 	vector->capacity = count;
88b843c749SSergey Zigachev 	vector->struct_size = struct_size;
89b843c749SSergey Zigachev 	vector->count = count;
90b843c749SSergey Zigachev 	return true;
91b843c749SSergey Zigachev }
92b843c749SSergey Zigachev 
dal_vector_presized_create(struct dc_context * ctx,uint32_t size,void * initial_value,uint32_t struct_size)93b843c749SSergey Zigachev struct vector *dal_vector_presized_create(
94b843c749SSergey Zigachev 	struct dc_context *ctx,
95b843c749SSergey Zigachev 	uint32_t size,
96b843c749SSergey Zigachev 	void *initial_value,
97b843c749SSergey Zigachev 	uint32_t struct_size)
98b843c749SSergey Zigachev {
99b843c749SSergey Zigachev 	struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
100b843c749SSergey Zigachev 
101b843c749SSergey Zigachev 	if (vector == NULL)
102b843c749SSergey Zigachev 		return NULL;
103b843c749SSergey Zigachev 
104b843c749SSergey Zigachev 	if (dal_vector_presized_costruct(
105b843c749SSergey Zigachev 		vector, ctx, size, initial_value, struct_size))
106b843c749SSergey Zigachev 		return vector;
107b843c749SSergey Zigachev 
108b843c749SSergey Zigachev 	BREAK_TO_DEBUGGER();
109b843c749SSergey Zigachev 	kfree(vector);
110b843c749SSergey Zigachev 	return NULL;
111b843c749SSergey Zigachev }
112b843c749SSergey Zigachev 
dal_vector_create(struct dc_context * ctx,uint32_t capacity,uint32_t struct_size)113b843c749SSergey Zigachev struct vector *dal_vector_create(
114b843c749SSergey Zigachev 	struct dc_context *ctx,
115b843c749SSergey Zigachev 	uint32_t capacity,
116b843c749SSergey Zigachev 	uint32_t struct_size)
117b843c749SSergey Zigachev {
118b843c749SSergey Zigachev 	struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
119b843c749SSergey Zigachev 
120b843c749SSergey Zigachev 	if (vector == NULL)
121b843c749SSergey Zigachev 		return NULL;
122b843c749SSergey Zigachev 
123b843c749SSergey Zigachev 	if (dal_vector_construct(vector, ctx, capacity, struct_size))
124b843c749SSergey Zigachev 		return vector;
125b843c749SSergey Zigachev 
126b843c749SSergey Zigachev 	BREAK_TO_DEBUGGER();
127b843c749SSergey Zigachev 	kfree(vector);
128b843c749SSergey Zigachev 	return NULL;
129b843c749SSergey Zigachev }
130b843c749SSergey Zigachev 
dal_vector_destruct(struct vector * vector)131b843c749SSergey Zigachev void dal_vector_destruct(
132b843c749SSergey Zigachev 	struct vector *vector)
133b843c749SSergey Zigachev {
134b843c749SSergey Zigachev 	kfree(vector->container);
135b843c749SSergey Zigachev 	vector->count = 0;
136b843c749SSergey Zigachev 	vector->capacity = 0;
137b843c749SSergey Zigachev }
138b843c749SSergey Zigachev 
dal_vector_destroy(struct vector ** vector)139b843c749SSergey Zigachev void dal_vector_destroy(
140b843c749SSergey Zigachev 	struct vector **vector)
141b843c749SSergey Zigachev {
142b843c749SSergey Zigachev 	if (vector == NULL || *vector == NULL)
143b843c749SSergey Zigachev 		return;
144b843c749SSergey Zigachev 	dal_vector_destruct(*vector);
145b843c749SSergey Zigachev 	kfree(*vector);
146b843c749SSergey Zigachev 	*vector = NULL;
147b843c749SSergey Zigachev }
148b843c749SSergey Zigachev 
dal_vector_get_count(const struct vector * vector)149b843c749SSergey Zigachev uint32_t dal_vector_get_count(
150b843c749SSergey Zigachev 	const struct vector *vector)
151b843c749SSergey Zigachev {
152b843c749SSergey Zigachev 	return vector->count;
153b843c749SSergey Zigachev }
154b843c749SSergey Zigachev 
dal_vector_at_index(const struct vector * vector,uint32_t index)155b843c749SSergey Zigachev void *dal_vector_at_index(
156b843c749SSergey Zigachev 	const struct vector *vector,
157b843c749SSergey Zigachev 	uint32_t index)
158b843c749SSergey Zigachev {
159b843c749SSergey Zigachev 	if (vector->container == NULL || index >= vector->count)
160b843c749SSergey Zigachev 		return NULL;
161b843c749SSergey Zigachev 	return vector->container + (index * vector->struct_size);
162b843c749SSergey Zigachev }
163b843c749SSergey Zigachev 
dal_vector_remove_at_index(struct vector * vector,uint32_t index)164b843c749SSergey Zigachev bool dal_vector_remove_at_index(
165b843c749SSergey Zigachev 	struct vector *vector,
166b843c749SSergey Zigachev 	uint32_t index)
167b843c749SSergey Zigachev {
168b843c749SSergey Zigachev 	if (index >= vector->count)
169b843c749SSergey Zigachev 		return false;
170b843c749SSergey Zigachev 
171b843c749SSergey Zigachev 	if (index != vector->count - 1)
172b843c749SSergey Zigachev 		memmove(
173b843c749SSergey Zigachev 			vector->container + (index * vector->struct_size),
174b843c749SSergey Zigachev 			vector->container + ((index + 1) * vector->struct_size),
175b843c749SSergey Zigachev 			(vector->count - index - 1) * vector->struct_size);
176b843c749SSergey Zigachev 	vector->count -= 1;
177b843c749SSergey Zigachev 
178b843c749SSergey Zigachev 	return true;
179b843c749SSergey Zigachev }
180b843c749SSergey Zigachev 
dal_vector_set_at_index(const struct vector * vector,const void * what,uint32_t index)181b843c749SSergey Zigachev void dal_vector_set_at_index(
182b843c749SSergey Zigachev 	const struct vector *vector,
183b843c749SSergey Zigachev 	const void *what,
184b843c749SSergey Zigachev 	uint32_t index)
185b843c749SSergey Zigachev {
186b843c749SSergey Zigachev 	void *where = dal_vector_at_index(vector, index);
187b843c749SSergey Zigachev 
188b843c749SSergey Zigachev 	if (!where) {
189b843c749SSergey Zigachev 		BREAK_TO_DEBUGGER();
190b843c749SSergey Zigachev 		return;
191b843c749SSergey Zigachev 	}
192b843c749SSergey Zigachev 	memmove(
193b843c749SSergey Zigachev 		where,
194b843c749SSergey Zigachev 		what,
195b843c749SSergey Zigachev 		vector->struct_size);
196b843c749SSergey Zigachev }
197b843c749SSergey Zigachev 
calc_increased_capacity(uint32_t old_capacity)198b843c749SSergey Zigachev static inline uint32_t calc_increased_capacity(
199b843c749SSergey Zigachev 	uint32_t old_capacity)
200b843c749SSergey Zigachev {
201b843c749SSergey Zigachev 	return old_capacity * 2;
202b843c749SSergey Zigachev }
203b843c749SSergey Zigachev 
dal_vector_insert_at(struct vector * vector,const void * what,uint32_t position)204b843c749SSergey Zigachev bool dal_vector_insert_at(
205b843c749SSergey Zigachev 	struct vector *vector,
206b843c749SSergey Zigachev 	const void *what,
207b843c749SSergey Zigachev 	uint32_t position)
208b843c749SSergey Zigachev {
209b843c749SSergey Zigachev 	uint8_t *insert_address;
210b843c749SSergey Zigachev 
211b843c749SSergey Zigachev 	if (vector->count == vector->capacity) {
212b843c749SSergey Zigachev 		if (!dal_vector_reserve(
213b843c749SSergey Zigachev 			vector,
214b843c749SSergey Zigachev 			calc_increased_capacity(vector->capacity)))
215b843c749SSergey Zigachev 			return false;
216b843c749SSergey Zigachev 	}
217b843c749SSergey Zigachev 
218b843c749SSergey Zigachev 	insert_address = vector->container + (vector->struct_size * position);
219b843c749SSergey Zigachev 
220b843c749SSergey Zigachev 	if (vector->count && position < vector->count)
221b843c749SSergey Zigachev 		memmove(
222b843c749SSergey Zigachev 			insert_address + vector->struct_size,
223b843c749SSergey Zigachev 			insert_address,
224b843c749SSergey Zigachev 			vector->struct_size * (vector->count - position));
225b843c749SSergey Zigachev 
226b843c749SSergey Zigachev 	memmove(
227b843c749SSergey Zigachev 		insert_address,
228b843c749SSergey Zigachev 		what,
229b843c749SSergey Zigachev 		vector->struct_size);
230b843c749SSergey Zigachev 
231b843c749SSergey Zigachev 	vector->count++;
232b843c749SSergey Zigachev 
233b843c749SSergey Zigachev 	return true;
234b843c749SSergey Zigachev }
235b843c749SSergey Zigachev 
dal_vector_append(struct vector * vector,const void * item)236b843c749SSergey Zigachev bool dal_vector_append(
237b843c749SSergey Zigachev 	struct vector *vector,
238b843c749SSergey Zigachev 	const void *item)
239b843c749SSergey Zigachev {
240b843c749SSergey Zigachev 	return dal_vector_insert_at(vector, item, vector->count);
241b843c749SSergey Zigachev }
242b843c749SSergey Zigachev 
dal_vector_clone(const struct vector * vector)243b843c749SSergey Zigachev struct vector *dal_vector_clone(
244b843c749SSergey Zigachev 	const struct vector *vector)
245b843c749SSergey Zigachev {
246b843c749SSergey Zigachev 	struct vector *vec_cloned;
247b843c749SSergey Zigachev 	uint32_t count;
248b843c749SSergey Zigachev 
249b843c749SSergey Zigachev 	/* create new vector */
250b843c749SSergey Zigachev 	count = dal_vector_get_count(vector);
251b843c749SSergey Zigachev 
252b843c749SSergey Zigachev 	if (count == 0)
253b843c749SSergey Zigachev 		/* when count is 0 we still want to create clone of the vector
254b843c749SSergey Zigachev 		 */
255b843c749SSergey Zigachev 		vec_cloned = dal_vector_create(
256b843c749SSergey Zigachev 			vector->ctx,
257b843c749SSergey Zigachev 			vector->capacity,
258b843c749SSergey Zigachev 			vector->struct_size);
259b843c749SSergey Zigachev 	else
260b843c749SSergey Zigachev 		/* Call "presized create" version, independently of how the
261b843c749SSergey Zigachev 		 * original vector was created.
262b843c749SSergey Zigachev 		 * The owner of original vector must know how to treat the new
263b843c749SSergey Zigachev 		 * vector - as "presized" or as "regular".
264b843c749SSergey Zigachev 		 * But from vector point of view it doesn't matter. */
265b843c749SSergey Zigachev 		vec_cloned = dal_vector_presized_create(vector->ctx, count,
266b843c749SSergey Zigachev 			NULL,/* no initial value */
267b843c749SSergey Zigachev 			vector->struct_size);
268b843c749SSergey Zigachev 
269b843c749SSergey Zigachev 	if (NULL == vec_cloned) {
270b843c749SSergey Zigachev 		BREAK_TO_DEBUGGER();
271b843c749SSergey Zigachev 		return NULL;
272b843c749SSergey Zigachev 	}
273b843c749SSergey Zigachev 
274b843c749SSergey Zigachev 	/* copy vector's data */
275b843c749SSergey Zigachev 	memmove(vec_cloned->container, vector->container,
276b843c749SSergey Zigachev 			vec_cloned->struct_size * vec_cloned->capacity);
277b843c749SSergey Zigachev 
278b843c749SSergey Zigachev 	return vec_cloned;
279b843c749SSergey Zigachev }
280b843c749SSergey Zigachev 
dal_vector_capacity(const struct vector * vector)281b843c749SSergey Zigachev uint32_t dal_vector_capacity(const struct vector *vector)
282b843c749SSergey Zigachev {
283b843c749SSergey Zigachev 	return vector->capacity;
284b843c749SSergey Zigachev }
285b843c749SSergey Zigachev 
dal_vector_reserve(struct vector * vector,uint32_t capacity)286b843c749SSergey Zigachev bool dal_vector_reserve(struct vector *vector, uint32_t capacity)
287b843c749SSergey Zigachev {
288b843c749SSergey Zigachev 	void *new_container;
289b843c749SSergey Zigachev 
290b843c749SSergey Zigachev 	if (capacity <= vector->capacity)
291b843c749SSergey Zigachev 		return true;
292b843c749SSergey Zigachev 
293b843c749SSergey Zigachev 	new_container = krealloc(vector->container,
294*78973132SSergey Zigachev 				 capacity * vector->struct_size, M_DRM, GFP_KERNEL);
295b843c749SSergey Zigachev 
296b843c749SSergey Zigachev 	if (new_container) {
297b843c749SSergey Zigachev 		vector->container = new_container;
298b843c749SSergey Zigachev 		vector->capacity = capacity;
299b843c749SSergey Zigachev 		return true;
300b843c749SSergey Zigachev 	}
301b843c749SSergey Zigachev 
302b843c749SSergey Zigachev 	return false;
303b843c749SSergey Zigachev }
304b843c749SSergey Zigachev 
dal_vector_clear(struct vector * vector)305b843c749SSergey Zigachev void dal_vector_clear(struct vector *vector)
306b843c749SSergey Zigachev {
307b843c749SSergey Zigachev 	vector->count = 0;
308b843c749SSergey Zigachev }
309