1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2017 Etnaviv Project
3bf215546Sopenharmony_ci * Copyright (C) 2017 Zodiac Inflight Innovations
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci * Authors:
25bf215546Sopenharmony_ci *    Christian Gmeiner <christian.gmeiner@gmail.com>
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "etnaviv_priv.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cistatic int etna_perfmon_query_signals(struct etna_perfmon *pm, struct etna_perfmon_domain *dom)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci	struct etna_device *dev = pm->pipe->gpu->dev;
33bf215546Sopenharmony_ci	struct drm_etnaviv_pm_signal req = {
34bf215546Sopenharmony_ci		.pipe = pm->pipe->id,
35bf215546Sopenharmony_ci		.domain = dom->id
36bf215546Sopenharmony_ci	};
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci	do {
39bf215546Sopenharmony_ci		struct etna_perfmon_signal *sig;
40bf215546Sopenharmony_ci		int ret;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci		ret = drmCommandWriteRead(dev->fd, DRM_ETNAVIV_PM_QUERY_SIG, &req, sizeof(req));
43bf215546Sopenharmony_ci		if (ret)
44bf215546Sopenharmony_ci			break;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci		sig = calloc(1, sizeof(*sig));
47bf215546Sopenharmony_ci		if (!sig)
48bf215546Sopenharmony_ci			return -ENOMEM;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci		DEBUG_MSG("perfmon signal:");
51bf215546Sopenharmony_ci		DEBUG_MSG("id         = %d", req.id);
52bf215546Sopenharmony_ci		DEBUG_MSG("name       = %s", req.name);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci		sig->domain = dom;
55bf215546Sopenharmony_ci		sig->signal = req.id;
56bf215546Sopenharmony_ci		strncpy(sig->name, req.name, sizeof(sig->name));
57bf215546Sopenharmony_ci		list_addtail(&sig->head, &dom->signals);
58bf215546Sopenharmony_ci	} while (req.iter != 0xffff);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci	return 0;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistatic int etna_perfmon_query_domains(struct etna_perfmon *pm)
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci	struct etna_device *dev = pm->pipe->gpu->dev;
66bf215546Sopenharmony_ci	struct drm_etnaviv_pm_domain req = {
67bf215546Sopenharmony_ci		.pipe = pm->pipe->id
68bf215546Sopenharmony_ci	};
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci	do {
71bf215546Sopenharmony_ci		struct etna_perfmon_domain *dom;
72bf215546Sopenharmony_ci		int ret;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci		ret = drmCommandWriteRead(dev->fd, DRM_ETNAVIV_PM_QUERY_DOM, &req, sizeof(req));
75bf215546Sopenharmony_ci		if (ret)
76bf215546Sopenharmony_ci			break;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci		dom = calloc(1, sizeof(*dom));
79bf215546Sopenharmony_ci		if (!dom)
80bf215546Sopenharmony_ci			return -ENOMEM;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci		list_inithead(&dom->signals);
83bf215546Sopenharmony_ci		dom->id = req.id;
84bf215546Sopenharmony_ci		strncpy(dom->name, req.name, sizeof(dom->name));
85bf215546Sopenharmony_ci		list_addtail(&dom->head, &pm->domains);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci		DEBUG_MSG("perfmon domain:");
88bf215546Sopenharmony_ci		DEBUG_MSG("id         = %d", req.id);
89bf215546Sopenharmony_ci		DEBUG_MSG("name       = %s", req.name);
90bf215546Sopenharmony_ci		DEBUG_MSG("nr_signals = %d", req.nr_signals);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci		/* Query all available signals for this domain. */
93bf215546Sopenharmony_ci		if (req.nr_signals > 0) {
94bf215546Sopenharmony_ci			ret = etna_perfmon_query_signals(pm, dom);
95bf215546Sopenharmony_ci			if (ret)
96bf215546Sopenharmony_ci				return ret;
97bf215546Sopenharmony_ci		}
98bf215546Sopenharmony_ci	} while (req.iter != 0xff);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci	return 0;
101bf215546Sopenharmony_ci}
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cistatic void etna_perfmon_free_signals(struct etna_perfmon_domain *dom)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci	struct etna_perfmon_signal *sig, *next;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci	LIST_FOR_EACH_ENTRY_SAFE(sig, next, &dom->signals, head) {
108bf215546Sopenharmony_ci		list_del(&sig->head);
109bf215546Sopenharmony_ci		free(sig);
110bf215546Sopenharmony_ci	}
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_cistatic void etna_perfmon_free_domains(struct etna_perfmon *pm)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci	struct etna_perfmon_domain *dom, *next;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci	LIST_FOR_EACH_ENTRY_SAFE(dom, next, &pm->domains, head) {
118bf215546Sopenharmony_ci		etna_perfmon_free_signals(dom);
119bf215546Sopenharmony_ci		list_del(&dom->head);
120bf215546Sopenharmony_ci		free(dom);
121bf215546Sopenharmony_ci	}
122bf215546Sopenharmony_ci}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_cistruct etna_perfmon *etna_perfmon_create(struct etna_pipe *pipe)
125bf215546Sopenharmony_ci{
126bf215546Sopenharmony_ci	struct etna_perfmon *pm;
127bf215546Sopenharmony_ci	int ret;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci	pm = calloc(1, sizeof(*pm));
130bf215546Sopenharmony_ci	if (!pm) {
131bf215546Sopenharmony_ci		ERROR_MSG("allocation failed");
132bf215546Sopenharmony_ci		return NULL;
133bf215546Sopenharmony_ci	}
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci	list_inithead(&pm->domains);
136bf215546Sopenharmony_ci	pm->pipe = pipe;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci	/* query all available domains and sources for this device */
139bf215546Sopenharmony_ci	ret = etna_perfmon_query_domains(pm);
140bf215546Sopenharmony_ci	if (ret)
141bf215546Sopenharmony_ci		goto fail;
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci	return pm;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_cifail:
146bf215546Sopenharmony_ci	etna_perfmon_del(pm);
147bf215546Sopenharmony_ci	return NULL;
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_civoid etna_perfmon_del(struct etna_perfmon *pm)
151bf215546Sopenharmony_ci{
152bf215546Sopenharmony_ci	if (!pm)
153bf215546Sopenharmony_ci		return;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci	etna_perfmon_free_domains(pm);
156bf215546Sopenharmony_ci	free(pm);
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cistruct etna_perfmon_domain *etna_perfmon_get_dom_by_name(struct etna_perfmon *pm, const char *name)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci	struct etna_perfmon_domain *dom;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci	if (pm) {
164bf215546Sopenharmony_ci		LIST_FOR_EACH_ENTRY(dom, &pm->domains, head) {
165bf215546Sopenharmony_ci			if (!strcmp(dom->name, name))
166bf215546Sopenharmony_ci				return dom;
167bf215546Sopenharmony_ci		}
168bf215546Sopenharmony_ci	}
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci	return NULL;
171bf215546Sopenharmony_ci}
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_cistruct etna_perfmon_signal *etna_perfmon_get_sig_by_name(struct etna_perfmon_domain *dom, const char *name)
174bf215546Sopenharmony_ci{
175bf215546Sopenharmony_ci	struct etna_perfmon_signal *signal;
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci	if (dom) {
178bf215546Sopenharmony_ci		LIST_FOR_EACH_ENTRY(signal, &dom->signals, head) {
179bf215546Sopenharmony_ci			if (!strcmp(signal->name, name))
180bf215546Sopenharmony_ci				return signal;
181bf215546Sopenharmony_ci		}
182bf215546Sopenharmony_ci	}
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci	return NULL;
185bf215546Sopenharmony_ci}
186