summaryrefslogtreecommitdiff
path: root/sys/arch/arm64/dev/acpiiort.c
blob: 8569b4932d7484bf7398ce5d32b827663d9db638 (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
/* $OpenBSD: acpiiort.c,v 1.3 2021/03/15 22:56:48 patrick Exp $ */
/*
 * Copyright (c) 2021 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/dsdt.h>

#include <arm64/dev/acpiiort.h>

SIMPLEQ_HEAD(, acpiiort_smmu) acpiiort_smmu_list =
    SIMPLEQ_HEAD_INITIALIZER(acpiiort_smmu_list);

int acpiiort_match(struct device *, void *, void *);
void acpiiort_attach(struct device *, struct device *, void *);

struct cfattach acpiiort_ca = {
	sizeof(struct device), acpiiort_match, acpiiort_attach
};

struct cfdriver acpiiort_cd = {
	NULL, "acpiiort", DV_DULL
};

int
acpiiort_match(struct device *parent, void *match, void *aux)
{
	struct acpi_attach_args *aaa = aux;
	struct acpi_table_header *hdr;

	/* If we do not have a table, it is not us */
	if (aaa->aaa_table == NULL)
		return 0;

	/* If it is an IORT table, we can attach */
	hdr = (struct acpi_table_header *)aaa->aaa_table;
	if (memcmp(hdr->signature, IORT_SIG, sizeof(IORT_SIG) - 1) != 0)
		return 0;

	return 1;
}

void
acpiiort_attach(struct device *parent, struct device *self, void *aux)
{
	struct acpi_attach_args *aaa = aux;
	struct acpi_iort *iort = (struct acpi_iort *)aaa->aaa_table;
	struct acpi_iort_node *node;
	struct acpiiort_attach_args aia;
	uint32_t offset;
	int i;

	printf("\n");

	memset(&aia, 0, sizeof(aia));
	aia.aia_iot = aaa->aaa_iot;
	aia.aia_memt = aaa->aaa_memt;
	aia.aia_dmat = aaa->aaa_dmat;

	offset = iort->offset;
	for (i = 0; i < iort->number_of_nodes; i++) {
		node = (struct acpi_iort_node *)((char *)iort + offset);
		aia.aia_node = node;
		config_found(self, &aia, NULL);
		offset += node->length;
	}
}

void
acpiiort_smmu_register(struct acpiiort_smmu *as)
{
	SIMPLEQ_INSERT_TAIL(&acpiiort_smmu_list, as, as_list);
}

bus_dma_tag_t
acpiiort_smmu_map(struct acpi_iort_node *node, uint32_t rid,
    bus_dma_tag_t dmat)
{
	struct acpiiort_smmu *as;

	SIMPLEQ_FOREACH(as, &acpiiort_smmu_list, as_list) {
		if (as->as_node == node)
			return as->as_map(as->as_cookie, rid, dmat);
	}

	return dmat;
}

bus_dma_tag_t
acpiiort_device_map(struct aml_node *root, bus_dma_tag_t dmat)
{
	struct acpi_table_header *hdr;
	struct acpi_iort *iort = NULL;
	struct acpi_iort_node *node;
	struct acpi_iort_mapping *map;
	struct acpi_iort_nc_node *nc;
	struct acpi_q *entry;
	const char *name;
	uint32_t rid, offset;
	int i;

	name = aml_nodename(root);

	/* Look for IORT table. */
	SIMPLEQ_FOREACH(entry, &acpi_softc->sc_tables, q_next) {
		hdr = entry->q_table;
		if (strncmp(hdr->signature, IORT_SIG,
		    sizeof(hdr->signature)) == 0) {
			iort = entry->q_table;
			break;
		}
	}
	if (iort == NULL)
		return dmat;

	/* Find our named component. */
	offset = iort->offset;
	for (i = 0; i < iort->number_of_nodes; i++) {
		node = (struct acpi_iort_node *)((char *)iort + offset);
		if (node->type == ACPI_IORT_NAMED_COMPONENT) {
			nc = (struct acpi_iort_nc_node *)&node[1];
			if (strcmp(nc->device_object_name, name) == 0)
				break;
		}
		offset += node->length;
	}

	/* No NC found? Weird. */
	if (i >= iort->number_of_nodes)
		return dmat;

	/* Find our output base towards SMMU. */
	map = (struct acpi_iort_mapping *)((char *)node + node->mapping_offset);
	for (i = 0; i < node->number_of_mappings; i++) {
		offset = map[i].output_reference;

		if (map[i].flags & ACPI_IORT_MAPPING_SINGLE) {
			rid = map[i].output_base;
			break;
		}

		/* Mapping encodes number of IDs in the range minus one. */
		if (map[i].input_base <= rid &&
		    rid <= map[i].input_base + map[i].number_of_ids) {
			rid = map[i].output_base + (rid - map[i].input_base);
			break;
		}
	}

	/* No mapping found? Even weirder. */
	if (i >= node->number_of_mappings)
		return dmat;

	node = (struct acpi_iort_node *)((char *)iort + offset);
	if (node->type == ACPI_IORT_SMMU)
		return acpiiort_smmu_map(node, rid, dmat);

	return dmat;
}