1b1670691SRuslan Bukin /*-
2c9ea007cSRuslan Bukin * Copyright (c) 2018-2020 Ruslan Bukin <br@bsdpad.com>
3b1670691SRuslan Bukin * All rights reserved.
4b1670691SRuslan Bukin *
5b1670691SRuslan Bukin * This software was developed by SRI International and the University of
6b1670691SRuslan Bukin * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7b1670691SRuslan Bukin * ("CTSRD"), as part of the DARPA CRASH research programme.
8b1670691SRuslan Bukin *
9b1670691SRuslan Bukin * Redistribution and use in source and binary forms, with or without
10b1670691SRuslan Bukin * modification, are permitted provided that the following conditions
11b1670691SRuslan Bukin * are met:
12b1670691SRuslan Bukin * 1. Redistributions of source code must retain the above copyright
13b1670691SRuslan Bukin * notice, this list of conditions and the following disclaimer.
14b1670691SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
15b1670691SRuslan Bukin * notice, this list of conditions and the following disclaimer in the
16b1670691SRuslan Bukin * documentation and/or other materials provided with the distribution.
17b1670691SRuslan Bukin *
18b1670691SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b1670691SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b1670691SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b1670691SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b1670691SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b1670691SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b1670691SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b1670691SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b1670691SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b1670691SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b1670691SRuslan Bukin * SUCH DAMAGE.
29b1670691SRuslan Bukin */
30b1670691SRuslan Bukin
31b1670691SRuslan Bukin #include <sys/param.h>
32b1670691SRuslan Bukin #include <sys/systm.h>
33b1670691SRuslan Bukin #include <sys/bus.h>
34b1670691SRuslan Bukin #include <sys/rman.h>
35b1670691SRuslan Bukin #include <sys/kernel.h>
36b1670691SRuslan Bukin #include <sys/module.h>
37b1670691SRuslan Bukin #include <machine/bus.h>
38b1670691SRuslan Bukin
39b1670691SRuslan Bukin #include <arm64/coresight/coresight.h>
40b1670691SRuslan Bukin
41b1670691SRuslan Bukin #include "coresight_if.h"
42b1670691SRuslan Bukin
43b1670691SRuslan Bukin extern struct coresight_device_list cs_devs;
44b1670691SRuslan Bukin
45b1670691SRuslan Bukin static struct coresight_device *
coresight_next_device(struct coresight_device * cs_dev,struct coresight_event * event)46b1670691SRuslan Bukin coresight_next_device(struct coresight_device *cs_dev,
47b1670691SRuslan Bukin struct coresight_event *event)
48b1670691SRuslan Bukin {
49b1670691SRuslan Bukin struct coresight_device *out;
50b1670691SRuslan Bukin struct endpoint *out_endp;
51b1670691SRuslan Bukin struct endpoint *endp;
52b1670691SRuslan Bukin
53b1670691SRuslan Bukin TAILQ_FOREACH(endp, &cs_dev->pdata->endpoints, link) {
54c9ea007cSRuslan Bukin if (endp->input != 0)
55b1670691SRuslan Bukin continue;
56b1670691SRuslan Bukin
57b1670691SRuslan Bukin out = coresight_get_output_device(endp, &out_endp);
58b1670691SRuslan Bukin if (out != NULL) {
59b1670691SRuslan Bukin if (LIST_EMPTY(&event->endplist)) {
60b1670691SRuslan Bukin /* Add source device */
61b1670691SRuslan Bukin endp->cs_dev = cs_dev;
62b1670691SRuslan Bukin LIST_INSERT_HEAD(&event->endplist, endp,
63b1670691SRuslan Bukin endplink);
64b1670691SRuslan Bukin }
65b1670691SRuslan Bukin
66b1670691SRuslan Bukin /* Add output device */
67c9ea007cSRuslan Bukin if (bootverbose)
68c9ea007cSRuslan Bukin printf("Adding device %s to the chain\n",
69c9ea007cSRuslan Bukin device_get_nameunit(out->dev));
70b1670691SRuslan Bukin out_endp->cs_dev = out;
71b1670691SRuslan Bukin LIST_INSERT_HEAD(&event->endplist, out_endp, endplink);
72b1670691SRuslan Bukin
73b1670691SRuslan Bukin return (out);
74b1670691SRuslan Bukin }
75b1670691SRuslan Bukin }
76b1670691SRuslan Bukin
77b1670691SRuslan Bukin return (NULL);
78b1670691SRuslan Bukin }
79b1670691SRuslan Bukin
80b1670691SRuslan Bukin static int
coresight_build_list(struct coresight_device * cs_dev,struct coresight_event * event)81b1670691SRuslan Bukin coresight_build_list(struct coresight_device *cs_dev,
82b1670691SRuslan Bukin struct coresight_event *event)
83b1670691SRuslan Bukin {
84b1670691SRuslan Bukin struct coresight_device *out;
85b1670691SRuslan Bukin
86b1670691SRuslan Bukin out = cs_dev;
87b1670691SRuslan Bukin while (out != NULL)
88b1670691SRuslan Bukin out = coresight_next_device(out, event);
89b1670691SRuslan Bukin
90b1670691SRuslan Bukin return (0);
91b1670691SRuslan Bukin }
92b1670691SRuslan Bukin
93b1670691SRuslan Bukin int
coresight_init_event(int cpu,struct coresight_event * event)94b1670691SRuslan Bukin coresight_init_event(int cpu, struct coresight_event *event)
95b1670691SRuslan Bukin {
96b1670691SRuslan Bukin struct coresight_device *cs_dev;
97b1670691SRuslan Bukin struct endpoint *endp;
98b1670691SRuslan Bukin
99b1670691SRuslan Bukin /* Start building path from source device */
100b1670691SRuslan Bukin TAILQ_FOREACH(cs_dev, &cs_devs, link) {
101b1670691SRuslan Bukin if (cs_dev->dev_type == event->src &&
102b1670691SRuslan Bukin cs_dev->pdata->cpu == cpu) {
103b1670691SRuslan Bukin LIST_INIT(&event->endplist);
104b1670691SRuslan Bukin coresight_build_list(cs_dev, event);
105b1670691SRuslan Bukin break;
106b1670691SRuslan Bukin }
107b1670691SRuslan Bukin }
108b1670691SRuslan Bukin
109b1670691SRuslan Bukin /* Ensure Coresight is initialized for the CPU */
110b1670691SRuslan Bukin TAILQ_FOREACH(cs_dev, &cs_devs, link) {
111b1670691SRuslan Bukin if (cs_dev->dev_type == CORESIGHT_CPU_DEBUG &&
112b1670691SRuslan Bukin cs_dev->pdata->cpu == cpu)
113b1670691SRuslan Bukin CORESIGHT_INIT(cs_dev->dev);
114b1670691SRuslan Bukin }
115b1670691SRuslan Bukin
116b1670691SRuslan Bukin /* Init all devices in the path */
117b1670691SRuslan Bukin LIST_FOREACH(endp, &event->endplist, endplink) {
118b1670691SRuslan Bukin cs_dev = endp->cs_dev;
119b1670691SRuslan Bukin CORESIGHT_INIT(cs_dev->dev);
120b1670691SRuslan Bukin }
121b1670691SRuslan Bukin
122b1670691SRuslan Bukin return (0);
123b1670691SRuslan Bukin }
124b1670691SRuslan Bukin
125b1670691SRuslan Bukin void
coresight_enable(int cpu,struct coresight_event * event)126b1670691SRuslan Bukin coresight_enable(int cpu, struct coresight_event *event)
127b1670691SRuslan Bukin {
128b1670691SRuslan Bukin struct coresight_device *cs_dev;
129b1670691SRuslan Bukin struct endpoint *endp;
130b1670691SRuslan Bukin
131b1670691SRuslan Bukin LIST_FOREACH(endp, &event->endplist, endplink) {
132b1670691SRuslan Bukin cs_dev = endp->cs_dev;
133b1670691SRuslan Bukin CORESIGHT_ENABLE(cs_dev->dev, endp, event);
134b1670691SRuslan Bukin }
135b1670691SRuslan Bukin }
136b1670691SRuslan Bukin
137b1670691SRuslan Bukin void
coresight_disable(int cpu,struct coresight_event * event)138b1670691SRuslan Bukin coresight_disable(int cpu, struct coresight_event *event)
139b1670691SRuslan Bukin {
140b1670691SRuslan Bukin struct coresight_device *cs_dev;
141b1670691SRuslan Bukin struct endpoint *endp;
142b1670691SRuslan Bukin
143b1670691SRuslan Bukin LIST_FOREACH(endp, &event->endplist, endplink) {
144b1670691SRuslan Bukin cs_dev = endp->cs_dev;
145b1670691SRuslan Bukin CORESIGHT_DISABLE(cs_dev->dev, endp, event);
146b1670691SRuslan Bukin }
147b1670691SRuslan Bukin }
148b1670691SRuslan Bukin
149b1670691SRuslan Bukin void
coresight_read(int cpu,struct coresight_event * event)150b1670691SRuslan Bukin coresight_read(int cpu, struct coresight_event *event)
151b1670691SRuslan Bukin {
152b1670691SRuslan Bukin struct endpoint *endp;
153b1670691SRuslan Bukin
154b1670691SRuslan Bukin LIST_FOREACH(endp, &event->endplist, endplink)
155b1670691SRuslan Bukin CORESIGHT_READ(endp->cs_dev->dev, endp, event);
156b1670691SRuslan Bukin }
157