aboutsummaryrefslogtreecommitdiff
path: root/drivers/gpu/msm/adreno_coresight.c
blob: 5a4d93545c2305015f84d240b7c77676260af8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
 */

#include <linux/coresight.h>
#include <linux/of.h>

#include "adreno.h"

#define TO_ADRENO_CORESIGHT_ATTR(_attr) \
	container_of(_attr, struct adreno_coresight_attr, attr)

static int adreno_coresight_identify(const char *name)
{
	if (!strcmp(name, "coresight-gfx"))
		return GPU_CORESIGHT_GX;
	else if (!strcmp(name, "coresight-gfx-cx"))
		return GPU_CORESIGHT_CX;
	else
		return -EINVAL;
}

ssize_t adreno_coresight_show_register(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	unsigned int val = 0;
	struct kgsl_device *device = dev_get_drvdata(dev->parent);
	struct adreno_device *adreno_dev;
	struct adreno_coresight_attr *cattr = TO_ADRENO_CORESIGHT_ATTR(attr);
	bool is_cx;

	if (device == NULL)
		return -EINVAL;

	adreno_dev = ADRENO_DEVICE(device);

	if (cattr->reg == NULL)
		return -EINVAL;

	is_cx = adreno_is_cx_dbgc_register(device, cattr->reg->offset);
	/*
	 * Return the current value of the register if coresight is enabled,
	 * otherwise report 0
	 */

	mutex_lock(&device->mutex);
	if ((is_cx && test_bit(ADRENO_DEVICE_CORESIGHT_CX, &adreno_dev->priv))
		|| (!is_cx && test_bit(ADRENO_DEVICE_CORESIGHT,
			&adreno_dev->priv))) {
		/*
		 * If the device isn't power collapsed read the actual value
		 * from the hardware - otherwise return the cached value
		 */

		if (device->state == KGSL_STATE_ACTIVE ||
			device->state == KGSL_STATE_NAP) {
			if (!kgsl_active_count_get(device)) {
				if (!is_cx)
					kgsl_regread(device, cattr->reg->offset,
						&cattr->reg->value);
				else
					adreno_cx_dbgc_regread(device,
						cattr->reg->offset,
						&cattr->reg->value);
				kgsl_active_count_put(device);
			}
		}

		val = cattr->reg->value;
	}
	mutex_unlock(&device->mutex);

	return scnprintf(buf, PAGE_SIZE, "0x%X\n", val);
}

ssize_t adreno_coresight_store_register(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	struct kgsl_device *device = dev_get_drvdata(dev->parent);
	struct adreno_device *adreno_dev;
	struct adreno_coresight_attr *cattr = TO_ADRENO_CORESIGHT_ATTR(attr);
	unsigned long val;
	int ret, is_cx;

	if (device == NULL)
		return -EINVAL;

	adreno_dev = ADRENO_DEVICE(device);

	if (cattr->reg == NULL)
		return -EINVAL;

	is_cx = adreno_is_cx_dbgc_register(device, cattr->reg->offset);

	ret = kstrtoul(buf, 0, &val);
	if (ret)
		return ret;

	mutex_lock(&device->mutex);

	/* Ignore writes while coresight is off */
	if (!((is_cx && test_bit(ADRENO_DEVICE_CORESIGHT_CX, &adreno_dev->priv))
		|| (!is_cx && test_bit(ADRENO_DEVICE_CORESIGHT,
		&adreno_dev->priv))))
		goto out;

	cattr->reg->value = val;

	/* Program the hardware if it is not power collapsed */
	if (device->state == KGSL_STATE_ACTIVE ||
		device->state == KGSL_STATE_NAP) {
		if (!kgsl_active_count_get(device)) {
			if (!is_cx)
				kgsl_regwrite(device, cattr->reg->offset,
					cattr->reg->value);
			else
				adreno_cx_dbgc_regwrite(device,
					cattr->reg->offset,
					cattr->reg->value);

			kgsl_active_count_put(device);
		}
	}

out:
	mutex_unlock(&device->mutex);
	return size;
}

/**
 * adreno_coresight_disable() - Generic function to disable coresight debugging
 * @csdev: Pointer to coresight's device struct
 *
 * This is a generic function to disable coresight debug bus on adreno
 * devices. This should be used in all cases of disabling
 * coresight debug bus for adreno devices. This function in turn calls
 * the adreno device specific function through the gpudev hook.
 * This function is registered as the coresight disable function
 * with coresight driver. It should only be called through coresight driver
 * as that would ensure that the necessary setup required to be done on
 * coresight driver's part is also done.
 */
static void adreno_coresight_disable(struct coresight_device *csdev,
					struct perf_event *event)
{
	struct kgsl_device *device = dev_get_drvdata(csdev->dev.parent);
	struct adreno_device *adreno_dev;
	struct adreno_gpudev *gpudev;
	struct adreno_coresight *coresight;
	int i, cs_id;

	if (device == NULL)
		return;

	adreno_dev = ADRENO_DEVICE(device);
	gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	cs_id = adreno_coresight_identify(dev_name(&csdev->dev));

	if (cs_id < 0)
		return;

	coresight = gpudev->coresight[cs_id];

	if (coresight == NULL)
		return;

	mutex_lock(&device->mutex);

	if (!kgsl_active_count_get(device)) {
		if (cs_id == GPU_CORESIGHT_GX)
			for (i = 0; i < coresight->count; i++)
				kgsl_regwrite(device,
					coresight->registers[i].offset, 0);
		else if (cs_id == GPU_CORESIGHT_CX)
			for (i = 0; i < coresight->count; i++)
				adreno_cx_dbgc_regwrite(device,
					coresight->registers[i].offset, 0);

		kgsl_active_count_put(device);
	}

	if (cs_id == GPU_CORESIGHT_GX)
		clear_bit(ADRENO_DEVICE_CORESIGHT, &adreno_dev->priv);
	else if (cs_id == GPU_CORESIGHT_CX)
		clear_bit(ADRENO_DEVICE_CORESIGHT_CX, &adreno_dev->priv);

	mutex_unlock(&device->mutex);
}

/**
 * _adreno_coresight_get_and_clear(): Save the current value of coresight
 * registers and clear the registers subsequently. Clearing registers
 * has the effect of disabling coresight.
 * @adreno_dev: Pointer to adreno device struct
 */
static int _adreno_coresight_get_and_clear(struct adreno_device *adreno_dev,
						int cs_id)
{
	int i;
	struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
	struct adreno_coresight *coresight = gpudev->coresight[cs_id];

	if (coresight == NULL)
		return -ENODEV;

	kgsl_pre_hwaccess(device);
	/*
	 * Save the current value of each coresight register
	 * and then clear each register
	 */
	if (cs_id == GPU_CORESIGHT_GX) {
		for (i = 0; i < coresight->count; i++) {
			kgsl_regread(device, coresight->registers[i].offset,
				&coresight->registers[i].value);
			kgsl_regwrite(device, coresight->registers[i].offset,
				0);
		}
	} else if (cs_id == GPU_CORESIGHT_CX) {
		for (i = 0; i < coresight->count; i++) {
			adreno_cx_dbgc_regread(device,
				coresight->registers[i].offset,
				&coresight->registers[i].value);
			adreno_cx_dbgc_regwrite(device,
				coresight->registers[i].offset, 0);
		}
	}

	return 0;
}

static int _adreno_coresight_set(struct adreno_device *adreno_dev, int cs_id)
{
	struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
	struct adreno_coresight *coresight = gpudev->coresight[cs_id];
	int i;

	if (coresight == NULL)
		return -ENODEV;

	if (cs_id == GPU_CORESIGHT_GX) {
		for (i = 0; i < coresight->count; i++)
			kgsl_regwrite(device, coresight->registers[i].offset,
				coresight->registers[i].value);
	} else if (cs_id == GPU_CORESIGHT_CX) {
		for (i = 0; i < coresight->count; i++)
			adreno_cx_dbgc_regwrite(device,
				coresight->registers[i].offset,
				coresight->registers[i].value);
	}
	return 0;
}
/**
 * adreno_coresight_enable() - Generic function to enable coresight debugging
 * @csdev: Pointer to coresight's device struct
 *
 * This is a generic function to enable coresight debug bus on adreno
 * devices. This should be used in all cases of enabling
 * coresight debug bus for adreno devices. This function is registered as the
 * coresight enable function with coresight driver. It should only be called
 * through coresight driver as that would ensure that the necessary setup
 * required to be done on coresight driver's part is also done.
 */
static int adreno_coresight_enable(struct coresight_device *csdev,
				struct perf_event *event, u32 mode)
{
	struct kgsl_device *device = dev_get_drvdata(csdev->dev.parent);
	struct adreno_device *adreno_dev;
	struct adreno_gpudev *gpudev;
	struct adreno_coresight *coresight;
	int ret = 0, adreno_dev_flag = -EINVAL, cs_id;

	if (device == NULL)
		return -ENODEV;

	adreno_dev = ADRENO_DEVICE(device);
	gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	cs_id = adreno_coresight_identify(dev_name(&csdev->dev));

	if (cs_id < 0)
		return -ENODEV;

	coresight = gpudev->coresight[cs_id];

	if (coresight == NULL)
		return -ENODEV;

	if (cs_id == GPU_CORESIGHT_GX)
		adreno_dev_flag = ADRENO_DEVICE_CORESIGHT;
	else if (cs_id == GPU_CORESIGHT_CX)
		adreno_dev_flag = ADRENO_DEVICE_CORESIGHT_CX;
	else
		return -ENODEV;

	mutex_lock(&device->mutex);
	if (!test_and_set_bit(adreno_dev_flag, &adreno_dev->priv)) {
		int i;

		/* Reset all the debug registers to their default values */

		for (i = 0; i < coresight->count; i++)
			coresight->registers[i].value =
				coresight->registers[i].initial;

		if (kgsl_state_is_awake(device)) {
			ret = kgsl_active_count_get(device);
			if (!ret) {
				ret = _adreno_coresight_set(adreno_dev, cs_id);
				kgsl_active_count_put(device);
			}
		}
	}

	mutex_unlock(&device->mutex);

	return ret;
}

/**
 * adreno_coresight_stop() - Reprogram coresight registers after power collapse
 * @adreno_dev: Pointer to the adreno device structure
 *
 * Cache the current coresight register values so they can be restored after
 * power collapse
 */
void adreno_coresight_stop(struct adreno_device *adreno_dev)
{
	int i, adreno_dev_flag = -EINVAL;

	for (i = 0; i < GPU_CORESIGHT_MAX; ++i) {
		if (i == GPU_CORESIGHT_GX)
			adreno_dev_flag = ADRENO_DEVICE_CORESIGHT;
		else if (i == GPU_CORESIGHT_CX)
			adreno_dev_flag = ADRENO_DEVICE_CORESIGHT_CX;
		else
			return;

		if (test_bit(adreno_dev_flag, &adreno_dev->priv))
			_adreno_coresight_get_and_clear(adreno_dev, i);
	}
}

/**
 * adreno_coresight_start() - Reprogram coresight registers after power collapse
 * @adreno_dev: Pointer to the adreno device structure
 *
 * Reprogram the cached values to the coresight registers on power up
 */
void adreno_coresight_start(struct adreno_device *adreno_dev)
{
	int i, adreno_dev_flag = -EINVAL;

	for (i = 0; i < GPU_CORESIGHT_MAX; ++i) {
		if (i == GPU_CORESIGHT_GX)
			adreno_dev_flag = ADRENO_DEVICE_CORESIGHT;
		else if (i == GPU_CORESIGHT_CX)
			adreno_dev_flag = ADRENO_DEVICE_CORESIGHT_CX;
		else
			return;

		if (test_bit(adreno_dev_flag, &adreno_dev->priv))
			_adreno_coresight_set(adreno_dev, i);
	}
}

static int adreno_coresight_trace_id(struct coresight_device *csdev)
{
	struct kgsl_device *device = dev_get_drvdata(csdev->dev.parent);
	struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(ADRENO_DEVICE(device));
	int cs_id;

	cs_id = adreno_coresight_identify(dev_name(&csdev->dev));

	if (cs_id < 0)
		return -ENODEV;

	return gpudev->coresight[cs_id]->atid;
}

static const struct coresight_ops_source adreno_coresight_source_ops = {
	.trace_id = adreno_coresight_trace_id,
	.enable = adreno_coresight_enable,
	.disable = adreno_coresight_disable,
};

static const struct coresight_ops adreno_coresight_ops = {
	.source_ops = &adreno_coresight_source_ops,
};

void adreno_coresight_remove(struct adreno_device *adreno_dev)
{
	int i, adreno_dev_flag = -EINVAL;

	for (i = 0; i < GPU_CORESIGHT_MAX; ++i) {
		if (i == GPU_CORESIGHT_GX)
			adreno_dev_flag = ADRENO_DEVICE_CORESIGHT;
		else if (i == GPU_CORESIGHT_CX)
			adreno_dev_flag = ADRENO_DEVICE_CORESIGHT_CX;
		else
			return;

		if (test_bit(adreno_dev_flag, &adreno_dev->priv)) {
			coresight_unregister(adreno_dev->csdev[i]);
			adreno_dev->csdev[i] = NULL;
		}
	}
}

int adreno_coresight_init(struct adreno_device *adreno_dev)
{
	int ret = 0;
	struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
	struct coresight_desc desc;
	int i = 0;
	struct device_node *node, *child;

	node = of_find_compatible_node(device->pdev->dev.of_node,
					NULL, "qcom,gpu-coresight");

	for_each_child_of_node(node, child) {
		memset(&desc, 0, sizeof(desc));
		desc.pdata = of_get_coresight_platform_data(&device->pdev->dev,
				child);
		if (IS_ERR(desc.pdata)) {
			ret = PTR_ERR(desc.pdata);
			goto err;
		}
		if (gpudev->coresight[i] == NULL) {
			ret = -ENODEV;
			goto err;
		}

		desc.type = CORESIGHT_DEV_TYPE_SOURCE;
		desc.subtype.source_subtype =
			CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE;
		desc.ops = &adreno_coresight_ops;
		desc.dev = &device->pdev->dev;
		desc.groups = gpudev->coresight[i]->groups;

		adreno_dev->csdev[i] = coresight_register(&desc);
		if (IS_ERR(adreno_dev->csdev[i])) {
			ret = PTR_ERR(adreno_dev->csdev[i]);
			adreno_dev->csdev[i] = NULL;
			goto err;
		}
		if (of_property_read_u32(child, "coresight-atid",
			&gpudev->coresight[i]->atid)) {
			coresight_unregister(adreno_dev->csdev[i]);
			adreno_dev->csdev[i] = NULL;
			ret = -EINVAL;
			goto err;
		}
		i++;
	}

err:
	if (ret)
		of_node_put(child);

	of_node_put(node);
	return ret;
}