summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/msm/disp/msm_disp_snapshot.c
blob: a4a7cb06bc878f72318b887353fc96cde817b388 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
 */

#define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__

#include "msm_disp_snapshot.h"

static ssize_t __maybe_unused disp_devcoredump_read(char *buffer, loff_t offset,
		size_t count, void *data, size_t datalen)
{
	struct drm_print_iterator iter;
	struct drm_printer p;
	struct msm_disp_state *disp_state;

	disp_state = data;

	iter.data = buffer;
	iter.offset = 0;
	iter.start = offset;
	iter.remain = count;

	p = drm_coredump_printer(&iter);

	msm_disp_state_print(disp_state, &p);

	return count - iter.remain;
}

static void _msm_disp_snapshot_work(struct kthread_work *work)
{
	struct msm_kms *kms = container_of(work, struct msm_kms, dump_work);
	struct drm_device *drm_dev = kms->dev;
	struct msm_disp_state *disp_state;
	struct drm_printer p;

	disp_state = kzalloc(sizeof(struct msm_disp_state), GFP_KERNEL);
	if (!disp_state)
		return;

	disp_state->dev = drm_dev->dev;
	disp_state->drm_dev = drm_dev;

	INIT_LIST_HEAD(&disp_state->blocks);

	/* Serialize dumping here */
	mutex_lock(&kms->dump_mutex);

	msm_disp_snapshot_capture_state(disp_state);

	mutex_unlock(&kms->dump_mutex);

	if (MSM_DISP_SNAPSHOT_DUMP_IN_CONSOLE) {
		p = drm_info_printer(disp_state->drm_dev->dev);
		msm_disp_state_print(disp_state, &p);
	}

	/*
	 * If COREDUMP is disabled, the stub will call the free function.
	 * If there is a codedump pending for the device, the dev_coredumpm()
	 * will also free new coredump state.
	 */
	dev_coredumpm(disp_state->dev, THIS_MODULE, disp_state, 0, GFP_KERNEL,
			disp_devcoredump_read, msm_disp_state_free);
}

void msm_disp_snapshot_state(struct drm_device *drm_dev)
{
	struct msm_drm_private *priv;
	struct msm_kms *kms;

	if (!drm_dev) {
		DRM_ERROR("invalid params\n");
		return;
	}

	priv = drm_dev->dev_private;
	kms = priv->kms;

	kthread_queue_work(kms->dump_worker, &kms->dump_work);
}

int msm_disp_snapshot_init(struct drm_device *drm_dev)
{
	struct msm_drm_private *priv;
	struct msm_kms *kms;

	if (!drm_dev) {
		DRM_ERROR("invalid params\n");
		return -EINVAL;
	}

	priv = drm_dev->dev_private;
	kms = priv->kms;

	mutex_init(&kms->dump_mutex);

	kms->dump_worker = kthread_create_worker(0, "%s", "disp_snapshot");
	if (IS_ERR(kms->dump_worker))
		DRM_ERROR("failed to create disp state task\n");

	kthread_init_work(&kms->dump_work, _msm_disp_snapshot_work);

	return 0;
}

void msm_disp_snapshot_destroy(struct drm_device *drm_dev)
{
	struct msm_kms *kms;
	struct msm_drm_private *priv;

	if (!drm_dev) {
		DRM_ERROR("invalid params\n");
		return;
	}

	priv = drm_dev->dev_private;
	kms = priv->kms;

	if (kms->dump_worker)
		kthread_destroy_worker(kms->dump_worker);

	mutex_destroy(&kms->dump_mutex);
}