• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * (C) COPYRIGHT 2011-2017 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15 
16 
17 
18 
19 
20 /*
21  * Base kernel property query APIs
22  */
23 
24 #include <mali_kbase.h>
25 #include <mali_midg_regmap.h>
26 #include <mali_kbase_gpuprops.h>
27 #include <mali_kbase_config_defaults.h>
28 #include <mali_kbase_hwaccess_gpuprops.h>
29 #include "mali_kbase_ioctl.h"
30 #include <linux/clk.h>
31 
32 /**
33  * KBASE_UBFX32 - Extracts bits from a 32-bit bitfield.
34  * @value:  The value from which to extract bits.
35  * @offset: The first bit to extract (0 being the LSB).
36  * @size:   The number of bits to extract.
37  *
38  * Context: @offset + @size <= 32.
39  *
40  * Return: Bits [@offset, @offset + @size) from @value.
41  */
42 /* from mali_cdsb.h */
43 #define KBASE_UBFX32(value, offset, size) \
44 	(((u32)(value) >> (u32)(offset)) & (u32)((1ULL << (u32)(size)) - 1))
45 
kbase_gpuprops_uk_get_props(struct kbase_context * kctx,struct kbase_uk_gpuprops * const kbase_props)46 int kbase_gpuprops_uk_get_props(struct kbase_context *kctx, struct kbase_uk_gpuprops * const kbase_props)
47 {
48 	kbase_gpu_clk_speed_func get_gpu_speed_mhz;
49 	u32 gpu_speed_mhz;
50 	int rc = 1;
51 
52 	KBASE_DEBUG_ASSERT(NULL != kctx);
53 	KBASE_DEBUG_ASSERT(NULL != kbase_props);
54 
55 	/* Current GPU speed is requested from the system integrator via the GPU_SPEED_FUNC function.
56 	 * If that function fails, or the function is not provided by the system integrator, we report the maximum
57 	 * GPU speed as specified by GPU_FREQ_KHZ_MAX.
58 	 */
59 	get_gpu_speed_mhz = (kbase_gpu_clk_speed_func) GPU_SPEED_FUNC;
60 	if (get_gpu_speed_mhz != NULL) {
61 		rc = get_gpu_speed_mhz(&gpu_speed_mhz);
62 #ifdef CONFIG_MALI_DEBUG
63 		/* Issue a warning message when the reported GPU speed falls outside the min/max range */
64 		if (rc == 0) {
65 			u32 gpu_speed_khz = gpu_speed_mhz * 1000;
66 
67 			if (gpu_speed_khz < kctx->kbdev->gpu_props.props.core_props.gpu_freq_khz_min ||
68 					gpu_speed_khz > kctx->kbdev->gpu_props.props.core_props.gpu_freq_khz_max)
69 				dev_warn(kctx->kbdev->dev, "GPU Speed is outside of min/max range (got %lu Khz, min %lu Khz, max %lu Khz)\n",
70 						(unsigned long)gpu_speed_khz,
71 						(unsigned long)kctx->kbdev->gpu_props.props.core_props.gpu_freq_khz_min,
72 						(unsigned long)kctx->kbdev->gpu_props.props.core_props.gpu_freq_khz_max);
73 		}
74 #endif				/* CONFIG_MALI_DEBUG */
75 	}
76 	if (kctx->kbdev->clock) {
77 		gpu_speed_mhz = clk_get_rate(kctx->kbdev->clock) / 1000000;
78 		rc = 0;
79 	}
80 	if (rc != 0)
81 		gpu_speed_mhz = kctx->kbdev->gpu_props.props.core_props.gpu_freq_khz_max / 1000;
82 
83 	kctx->kbdev->gpu_props.props.core_props.gpu_speed_mhz = gpu_speed_mhz;
84 
85 	memcpy(&kbase_props->props, &kctx->kbdev->gpu_props.props, sizeof(kbase_props->props));
86 
87 	/* Before API 8.2 they expect L3 cache info here, which was always 0 */
88 	if (kctx->api_version < KBASE_API_VERSION(8, 2))
89 		kbase_props->props.raw_props.suspend_size = 0;
90 
91 	return 0;
92 }
93 
kbase_gpuprops_construct_coherent_groups(base_gpu_props * const props)94 static void kbase_gpuprops_construct_coherent_groups(base_gpu_props * const props)
95 {
96 	struct mali_base_gpu_coherent_group *current_group;
97 	u64 group_present;
98 	u64 group_mask;
99 	u64 first_set, first_set_prev;
100 	u32 num_groups = 0;
101 
102 	KBASE_DEBUG_ASSERT(NULL != props);
103 
104 	props->coherency_info.coherency = props->raw_props.mem_features;
105 	props->coherency_info.num_core_groups = hweight64(props->raw_props.l2_present);
106 
107 	if (props->coherency_info.coherency & GROUPS_L2_COHERENT) {
108 		/* Group is l2 coherent */
109 		group_present = props->raw_props.l2_present;
110 	} else {
111 		/* Group is l1 coherent */
112 		group_present = props->raw_props.shader_present;
113 	}
114 
115 	/*
116 	 * The coherent group mask can be computed from the l2 present
117 	 * register.
118 	 *
119 	 * For the coherent group n:
120 	 * group_mask[n] = (first_set[n] - 1) & ~(first_set[n-1] - 1)
121 	 * where first_set is group_present with only its nth set-bit kept
122 	 * (i.e. the position from where a new group starts).
123 	 *
124 	 * For instance if the groups are l2 coherent and l2_present=0x0..01111:
125 	 * The first mask is:
126 	 * group_mask[1] = (first_set[1] - 1) & ~(first_set[0] - 1)
127 	 *               = (0x0..010     - 1) & ~(0x0..01      - 1)
128 	 *               =  0x0..00f
129 	 * The second mask is:
130 	 * group_mask[2] = (first_set[2] - 1) & ~(first_set[1] - 1)
131 	 *               = (0x0..100     - 1) & ~(0x0..010     - 1)
132 	 *               =  0x0..0f0
133 	 * And so on until all the bits from group_present have been cleared
134 	 * (i.e. there is no group left).
135 	 */
136 
137 	current_group = props->coherency_info.group;
138 	first_set = group_present & ~(group_present - 1);
139 
140 	while (group_present != 0 && num_groups < BASE_MAX_COHERENT_GROUPS) {
141 		group_present -= first_set;	/* Clear the current group bit */
142 		first_set_prev = first_set;
143 
144 		first_set = group_present & ~(group_present - 1);
145 		group_mask = (first_set - 1) & ~(first_set_prev - 1);
146 
147 		/* Populate the coherent_group structure for each group */
148 		current_group->core_mask = group_mask & props->raw_props.shader_present;
149 		current_group->num_cores = hweight64(current_group->core_mask);
150 
151 		num_groups++;
152 		current_group++;
153 	}
154 
155 	if (group_present != 0)
156 		pr_warn("Too many coherent groups (keeping only %d groups).\n", BASE_MAX_COHERENT_GROUPS);
157 
158 	props->coherency_info.num_groups = num_groups;
159 }
160 
161 /**
162  * kbase_gpuprops_get_props - Get the GPU configuration
163  * @gpu_props: The &base_gpu_props structure
164  * @kbdev: The &struct kbase_device structure for the device
165  *
166  * Fill the &base_gpu_props structure with values from the GPU configuration
167  * registers. Only the raw properties are filled in this function
168  */
kbase_gpuprops_get_props(base_gpu_props * const gpu_props,struct kbase_device * kbdev)169 static void kbase_gpuprops_get_props(base_gpu_props * const gpu_props, struct kbase_device *kbdev)
170 {
171 	struct kbase_gpuprops_regdump regdump;
172 	int i;
173 
174 	KBASE_DEBUG_ASSERT(NULL != kbdev);
175 	KBASE_DEBUG_ASSERT(NULL != gpu_props);
176 
177 	/* Dump relevant registers */
178 	kbase_backend_gpuprops_get(kbdev, &regdump);
179 
180 	gpu_props->raw_props.gpu_id = regdump.gpu_id;
181 	gpu_props->raw_props.tiler_features = regdump.tiler_features;
182 	gpu_props->raw_props.mem_features = regdump.mem_features;
183 	gpu_props->raw_props.mmu_features = regdump.mmu_features;
184 	gpu_props->raw_props.l2_features = regdump.l2_features;
185 	gpu_props->raw_props.suspend_size = regdump.suspend_size;
186 
187 	gpu_props->raw_props.as_present = regdump.as_present;
188 	gpu_props->raw_props.js_present = regdump.js_present;
189 	gpu_props->raw_props.shader_present =
190 		((u64) regdump.shader_present_hi << 32) +
191 		regdump.shader_present_lo;
192 	gpu_props->raw_props.tiler_present =
193 		((u64) regdump.tiler_present_hi << 32) +
194 		regdump.tiler_present_lo;
195 	gpu_props->raw_props.l2_present =
196 		((u64) regdump.l2_present_hi << 32) +
197 		regdump.l2_present_lo;
198 #ifdef CONFIG_MALI_CORESTACK
199 	gpu_props->raw_props.stack_present =
200 		((u64) regdump.stack_present_hi << 32) +
201 		regdump.stack_present_lo;
202 #else /* CONFIG_MALI_CORESTACK */
203 	gpu_props->raw_props.stack_present = 0;
204 #endif /* CONFIG_MALI_CORESTACK */
205 
206 	for (i = 0; i < GPU_MAX_JOB_SLOTS; i++)
207 		gpu_props->raw_props.js_features[i] = regdump.js_features[i];
208 
209 	for (i = 0; i < BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS; i++)
210 		gpu_props->raw_props.texture_features[i] = regdump.texture_features[i];
211 
212 	gpu_props->raw_props.thread_max_barrier_size = regdump.thread_max_barrier_size;
213 	gpu_props->raw_props.thread_max_threads = regdump.thread_max_threads;
214 	gpu_props->raw_props.thread_max_workgroup_size = regdump.thread_max_workgroup_size;
215 	gpu_props->raw_props.thread_features = regdump.thread_features;
216 }
217 
kbase_gpuprops_update_core_props_gpu_id(base_gpu_props * const gpu_props)218 void kbase_gpuprops_update_core_props_gpu_id(base_gpu_props * const gpu_props)
219 {
220 	gpu_props->core_props.version_status = KBASE_UBFX32(gpu_props->raw_props.gpu_id, 0U, 4);
221 	gpu_props->core_props.minor_revision = KBASE_UBFX32(gpu_props->raw_props.gpu_id, 4U, 8);
222 	gpu_props->core_props.major_revision = KBASE_UBFX32(gpu_props->raw_props.gpu_id, 12U, 4);
223 	gpu_props->core_props.product_id = KBASE_UBFX32(gpu_props->raw_props.gpu_id, 16U, 16);
224 }
225 
226 /**
227  * kbase_gpuprops_calculate_props - Calculate the derived properties
228  * @gpu_props: The &base_gpu_props structure
229  * @kbdev:     The &struct kbase_device structure for the device
230  *
231  * Fill the &base_gpu_props structure with values derived from the GPU
232  * configuration registers
233  */
kbase_gpuprops_calculate_props(base_gpu_props * const gpu_props,struct kbase_device * kbdev)234 static void kbase_gpuprops_calculate_props(base_gpu_props * const gpu_props, struct kbase_device *kbdev)
235 {
236 	int i;
237 
238 	/* Populate the base_gpu_props structure */
239 	kbase_gpuprops_update_core_props_gpu_id(gpu_props);
240 	gpu_props->core_props.log2_program_counter_size = KBASE_GPU_PC_SIZE_LOG2;
241 	gpu_props->core_props.gpu_available_memory_size = totalram_pages() << PAGE_SHIFT;
242 
243 	for (i = 0; i < BASE_GPU_NUM_TEXTURE_FEATURES_REGISTERS; i++)
244 		gpu_props->core_props.texture_features[i] = gpu_props->raw_props.texture_features[i];
245 
246 	gpu_props->l2_props.log2_line_size = KBASE_UBFX32(gpu_props->raw_props.l2_features, 0U, 8);
247 	gpu_props->l2_props.log2_cache_size = KBASE_UBFX32(gpu_props->raw_props.l2_features, 16U, 8);
248 
249 	/* Field with number of l2 slices is added to MEM_FEATURES register
250 	 * since t76x. Below code assumes that for older GPU reserved bits will
251 	 * be read as zero. */
252 	gpu_props->l2_props.num_l2_slices =
253 		KBASE_UBFX32(gpu_props->raw_props.mem_features, 8U, 4) + 1;
254 
255 	gpu_props->tiler_props.bin_size_bytes = 1 << KBASE_UBFX32(gpu_props->raw_props.tiler_features, 0U, 6);
256 	gpu_props->tiler_props.max_active_levels = KBASE_UBFX32(gpu_props->raw_props.tiler_features, 8U, 4);
257 
258 	if (gpu_props->raw_props.thread_max_threads == 0)
259 		gpu_props->thread_props.max_threads = THREAD_MT_DEFAULT;
260 	else
261 		gpu_props->thread_props.max_threads = gpu_props->raw_props.thread_max_threads;
262 
263 	if (gpu_props->raw_props.thread_max_workgroup_size == 0)
264 		gpu_props->thread_props.max_workgroup_size = THREAD_MWS_DEFAULT;
265 	else
266 		gpu_props->thread_props.max_workgroup_size = gpu_props->raw_props.thread_max_workgroup_size;
267 
268 	if (gpu_props->raw_props.thread_max_barrier_size == 0)
269 		gpu_props->thread_props.max_barrier_size = THREAD_MBS_DEFAULT;
270 	else
271 		gpu_props->thread_props.max_barrier_size = gpu_props->raw_props.thread_max_barrier_size;
272 
273 	gpu_props->thread_props.max_registers = KBASE_UBFX32(gpu_props->raw_props.thread_features, 0U, 16);
274 	gpu_props->thread_props.max_task_queue = KBASE_UBFX32(gpu_props->raw_props.thread_features, 16U, 8);
275 	gpu_props->thread_props.max_thread_group_split = KBASE_UBFX32(gpu_props->raw_props.thread_features, 24U, 6);
276 	gpu_props->thread_props.impl_tech = KBASE_UBFX32(gpu_props->raw_props.thread_features, 30U, 2);
277 
278 	/* If values are not specified, then use defaults */
279 	if (gpu_props->thread_props.max_registers == 0) {
280 		gpu_props->thread_props.max_registers = THREAD_MR_DEFAULT;
281 		gpu_props->thread_props.max_task_queue = THREAD_MTQ_DEFAULT;
282 		gpu_props->thread_props.max_thread_group_split = THREAD_MTGS_DEFAULT;
283 	}
284 	/* Initialize the coherent_group structure for each group */
285 	kbase_gpuprops_construct_coherent_groups(gpu_props);
286 }
287 
kbase_gpuprops_set(struct kbase_device * kbdev)288 void kbase_gpuprops_set(struct kbase_device *kbdev)
289 {
290 	struct kbase_gpu_props *gpu_props;
291 	struct gpu_raw_gpu_props *raw;
292 
293 	KBASE_DEBUG_ASSERT(NULL != kbdev);
294 	gpu_props = &kbdev->gpu_props;
295 	raw = &gpu_props->props.raw_props;
296 
297 	/* Initialize the base_gpu_props structure from the hardware */
298 	kbase_gpuprops_get_props(&gpu_props->props, kbdev);
299 
300 	/* Populate the derived properties */
301 	kbase_gpuprops_calculate_props(&gpu_props->props, kbdev);
302 
303 	/* Populate kbase-only fields */
304 	gpu_props->l2_props.associativity = KBASE_UBFX32(raw->l2_features, 8U, 8);
305 	gpu_props->l2_props.external_bus_width = KBASE_UBFX32(raw->l2_features, 24U, 8);
306 
307 	gpu_props->mem.core_group = KBASE_UBFX32(raw->mem_features, 0U, 1);
308 
309 	gpu_props->mmu.va_bits = KBASE_UBFX32(raw->mmu_features, 0U, 8);
310 	gpu_props->mmu.pa_bits = KBASE_UBFX32(raw->mmu_features, 8U, 8);
311 
312 	gpu_props->num_cores = hweight64(raw->shader_present);
313 	gpu_props->num_core_groups = hweight64(raw->l2_present);
314 	gpu_props->num_address_spaces = hweight32(raw->as_present);
315 	gpu_props->num_job_slots = hweight32(raw->js_present);
316 }
317 
kbase_gpuprops_set_features(struct kbase_device * kbdev)318 void kbase_gpuprops_set_features(struct kbase_device *kbdev)
319 {
320 	base_gpu_props *gpu_props;
321 	struct kbase_gpuprops_regdump regdump;
322 
323 	gpu_props = &kbdev->gpu_props.props;
324 
325 	/* Dump relevant registers */
326 	kbase_backend_gpuprops_get_features(kbdev, &regdump);
327 
328 	/*
329 	 * Copy the raw value from the register, later this will get turned
330 	 * into the selected coherency mode.
331 	 * Additionally, add non-coherent mode, as this is always supported.
332 	 */
333 	gpu_props->raw_props.coherency_mode = regdump.coherency_features |
334 		COHERENCY_FEATURE_BIT(COHERENCY_NONE);
335 }
336 
337 static struct {
338 	u32 type;
339 	size_t offset;
340 	int size;
341 } gpu_property_mapping[] = {
342 #define PROP(name, member) \
343 	{KBASE_GPUPROP_ ## name, offsetof(struct mali_base_gpu_props, member), \
344 		sizeof(((struct mali_base_gpu_props *)0)->member)}
345 	PROP(PRODUCT_ID,                  core_props.product_id),
346 	PROP(VERSION_STATUS,              core_props.version_status),
347 	PROP(MINOR_REVISION,              core_props.minor_revision),
348 	PROP(MAJOR_REVISION,              core_props.major_revision),
349 	PROP(GPU_SPEED_MHZ,               core_props.gpu_speed_mhz),
350 	PROP(GPU_FREQ_KHZ_MAX,            core_props.gpu_freq_khz_max),
351 	PROP(GPU_FREQ_KHZ_MIN,            core_props.gpu_freq_khz_min),
352 	PROP(LOG2_PROGRAM_COUNTER_SIZE,   core_props.log2_program_counter_size),
353 	PROP(TEXTURE_FEATURES_0,          core_props.texture_features[0]),
354 	PROP(TEXTURE_FEATURES_1,          core_props.texture_features[1]),
355 	PROP(TEXTURE_FEATURES_2,          core_props.texture_features[2]),
356 	PROP(GPU_AVAILABLE_MEMORY_SIZE,   core_props.gpu_available_memory_size),
357 
358 	PROP(L2_LOG2_LINE_SIZE,           l2_props.log2_line_size),
359 	PROP(L2_LOG2_CACHE_SIZE,          l2_props.log2_cache_size),
360 	PROP(L2_NUM_L2_SLICES,            l2_props.num_l2_slices),
361 
362 	PROP(TILER_BIN_SIZE_BYTES,        tiler_props.bin_size_bytes),
363 	PROP(TILER_MAX_ACTIVE_LEVELS,     tiler_props.max_active_levels),
364 
365 	PROP(MAX_THREADS,                 thread_props.max_threads),
366 	PROP(MAX_WORKGROUP_SIZE,          thread_props.max_workgroup_size),
367 	PROP(MAX_BARRIER_SIZE,            thread_props.max_barrier_size),
368 	PROP(MAX_REGISTERS,               thread_props.max_registers),
369 	PROP(MAX_TASK_QUEUE,              thread_props.max_task_queue),
370 	PROP(MAX_THREAD_GROUP_SPLIT,      thread_props.max_thread_group_split),
371 	PROP(IMPL_TECH,                   thread_props.impl_tech),
372 
373 	PROP(RAW_SHADER_PRESENT,          raw_props.shader_present),
374 	PROP(RAW_TILER_PRESENT,           raw_props.tiler_present),
375 	PROP(RAW_L2_PRESENT,              raw_props.l2_present),
376 	PROP(RAW_STACK_PRESENT,           raw_props.stack_present),
377 	PROP(RAW_L2_FEATURES,             raw_props.l2_features),
378 	PROP(RAW_SUSPEND_SIZE,            raw_props.suspend_size),
379 	PROP(RAW_MEM_FEATURES,            raw_props.mem_features),
380 	PROP(RAW_MMU_FEATURES,            raw_props.mmu_features),
381 	PROP(RAW_AS_PRESENT,              raw_props.as_present),
382 	PROP(RAW_JS_PRESENT,              raw_props.js_present),
383 	PROP(RAW_JS_FEATURES_0,           raw_props.js_features[0]),
384 	PROP(RAW_JS_FEATURES_1,           raw_props.js_features[1]),
385 	PROP(RAW_JS_FEATURES_2,           raw_props.js_features[2]),
386 	PROP(RAW_JS_FEATURES_3,           raw_props.js_features[3]),
387 	PROP(RAW_JS_FEATURES_4,           raw_props.js_features[4]),
388 	PROP(RAW_JS_FEATURES_5,           raw_props.js_features[5]),
389 	PROP(RAW_JS_FEATURES_6,           raw_props.js_features[6]),
390 	PROP(RAW_JS_FEATURES_7,           raw_props.js_features[7]),
391 	PROP(RAW_JS_FEATURES_8,           raw_props.js_features[8]),
392 	PROP(RAW_JS_FEATURES_9,           raw_props.js_features[9]),
393 	PROP(RAW_JS_FEATURES_10,          raw_props.js_features[10]),
394 	PROP(RAW_JS_FEATURES_11,          raw_props.js_features[11]),
395 	PROP(RAW_JS_FEATURES_12,          raw_props.js_features[12]),
396 	PROP(RAW_JS_FEATURES_13,          raw_props.js_features[13]),
397 	PROP(RAW_JS_FEATURES_14,          raw_props.js_features[14]),
398 	PROP(RAW_JS_FEATURES_15,          raw_props.js_features[15]),
399 	PROP(RAW_TILER_FEATURES,          raw_props.tiler_features),
400 	PROP(RAW_TEXTURE_FEATURES_0,      raw_props.texture_features[0]),
401 	PROP(RAW_TEXTURE_FEATURES_1,      raw_props.texture_features[1]),
402 	PROP(RAW_TEXTURE_FEATURES_2,      raw_props.texture_features[2]),
403 	PROP(RAW_GPU_ID,                  raw_props.gpu_id),
404 	PROP(RAW_THREAD_MAX_THREADS,      raw_props.thread_max_threads),
405 	PROP(RAW_THREAD_MAX_WORKGROUP_SIZE,
406 			raw_props.thread_max_workgroup_size),
407 	PROP(RAW_THREAD_MAX_BARRIER_SIZE, raw_props.thread_max_barrier_size),
408 	PROP(RAW_THREAD_FEATURES,         raw_props.thread_features),
409 	PROP(RAW_COHERENCY_MODE,          raw_props.coherency_mode),
410 
411 	PROP(COHERENCY_NUM_GROUPS,        coherency_info.num_groups),
412 	PROP(COHERENCY_NUM_CORE_GROUPS,   coherency_info.num_core_groups),
413 	PROP(COHERENCY_COHERENCY,         coherency_info.coherency),
414 	PROP(COHERENCY_GROUP_0,           coherency_info.group[0].core_mask),
415 	PROP(COHERENCY_GROUP_1,           coherency_info.group[1].core_mask),
416 	PROP(COHERENCY_GROUP_2,           coherency_info.group[2].core_mask),
417 	PROP(COHERENCY_GROUP_3,           coherency_info.group[3].core_mask),
418 	PROP(COHERENCY_GROUP_4,           coherency_info.group[4].core_mask),
419 	PROP(COHERENCY_GROUP_5,           coherency_info.group[5].core_mask),
420 	PROP(COHERENCY_GROUP_6,           coherency_info.group[6].core_mask),
421 	PROP(COHERENCY_GROUP_7,           coherency_info.group[7].core_mask),
422 	PROP(COHERENCY_GROUP_8,           coherency_info.group[8].core_mask),
423 	PROP(COHERENCY_GROUP_9,           coherency_info.group[9].core_mask),
424 	PROP(COHERENCY_GROUP_10,          coherency_info.group[10].core_mask),
425 	PROP(COHERENCY_GROUP_11,          coherency_info.group[11].core_mask),
426 	PROP(COHERENCY_GROUP_12,          coherency_info.group[12].core_mask),
427 	PROP(COHERENCY_GROUP_13,          coherency_info.group[13].core_mask),
428 	PROP(COHERENCY_GROUP_14,          coherency_info.group[14].core_mask),
429 	PROP(COHERENCY_GROUP_15,          coherency_info.group[15].core_mask),
430 
431 #undef PROP
432 };
433 
kbase_gpuprops_populate_user_buffer(struct kbase_device * kbdev)434 int kbase_gpuprops_populate_user_buffer(struct kbase_device *kbdev)
435 {
436 	struct kbase_gpu_props *kprops = &kbdev->gpu_props;
437 	struct mali_base_gpu_props *props = &kprops->props;
438 	u32 count = ARRAY_SIZE(gpu_property_mapping);
439 	u32 i;
440 	u32 size = 0;
441 	u8 *p;
442 
443 	for (i = 0; i < count; i++) {
444 		/* 4 bytes for the ID, and the size of the property */
445 		size += 4 + gpu_property_mapping[i].size;
446 	}
447 
448 	kprops->prop_buffer_size = size;
449 	kprops->prop_buffer = kmalloc(size, GFP_KERNEL);
450 
451 	if (!kprops->prop_buffer) {
452 		kprops->prop_buffer_size = 0;
453 		return -ENOMEM;
454 	}
455 
456 	p = kprops->prop_buffer;
457 
458 #define WRITE_U8(v) (*p++ = (v) & 0xFF)
459 #define WRITE_U16(v) do { WRITE_U8(v); WRITE_U8((v) >> 8); } while (0)
460 #define WRITE_U32(v) do { WRITE_U16(v); WRITE_U16((v) >> 16); } while (0)
461 #define WRITE_U64(v) do { WRITE_U32(v); WRITE_U32((v) >> 32); } while (0)
462 
463 	for (i = 0; i < count; i++) {
464 		u32 type = gpu_property_mapping[i].type;
465 		u8 type_size;
466 		void *field = ((u8 *)props) + gpu_property_mapping[i].offset;
467 
468 		switch (gpu_property_mapping[i].size) {
469 		case 1:
470 			type_size = KBASE_GPUPROP_VALUE_SIZE_U8;
471 			break;
472 		case 2:
473 			type_size = KBASE_GPUPROP_VALUE_SIZE_U16;
474 			break;
475 		case 4:
476 			type_size = KBASE_GPUPROP_VALUE_SIZE_U32;
477 			break;
478 		case 8:
479 			type_size = KBASE_GPUPROP_VALUE_SIZE_U64;
480 			break;
481 		default:
482 			dev_err(kbdev->dev,
483 				"Invalid gpu_property_mapping type=%d size=%d",
484 				type, gpu_property_mapping[i].size);
485 			return -EINVAL;
486 		}
487 
488 		WRITE_U32((type<<2) | type_size);
489 
490 		switch (type_size) {
491 		case KBASE_GPUPROP_VALUE_SIZE_U8:
492 			WRITE_U8(*((u8 *)field));
493 			break;
494 		case KBASE_GPUPROP_VALUE_SIZE_U16:
495 			WRITE_U16(*((u16 *)field));
496 			break;
497 		case KBASE_GPUPROP_VALUE_SIZE_U32:
498 			WRITE_U32(*((u32 *)field));
499 			break;
500 		case KBASE_GPUPROP_VALUE_SIZE_U64:
501 			WRITE_U64(*((u64 *)field));
502 			break;
503 		default: /* Cannot be reached */
504 			WARN_ON(1);
505 			return -EINVAL;
506 		}
507 	}
508 
509 	return 0;
510 }
511