2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2018 Marvell International Ltd.
6 * Author: Jayachandran C Nair <jchandra@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <machine/intr.h>
42 #include <contrib/dev/acpica/include/acpi.h>
43 #include <contrib/dev/acpica/include/accommon.h>
44 #include <contrib/dev/acpica/include/actables.h>
46 #include <dev/acpica/acpivar.h>
49 * Track next XREF available for ITS groups.
51 static u_int acpi_its_xref = ACPI_MSI_XREF;
54 * Some types of IORT nodes have a set of mappings. Each of them map
55 * a range of device IDs [base..end] from the current node to another
56 * node. The corresponding device IDs on destination node starts at
59 struct iort_map_entry {
64 u_int out_node_offset;
65 struct iort_node *out_node;
69 * The ITS group node does not have any outgoing mappings. It has a
70 * of a list of GIC ITS blocks which can handle the device ID. We
71 * will store the PIC XREF used by the block and the blocks proximity
72 * data here, so that it can be retrieved together.
74 struct iort_its_entry {
81 * IORT node. Each node has some device specific data depending on the
82 * type of the node. The node can also have a set of mappings, OR in
83 * case of ITS group nodes a set of ITS entries.
84 * The nodes are kept in a TAILQ by type.
87 TAILQ_ENTRY(iort_node) next; /* next entry with same type */
88 enum AcpiIortNodeType type; /* ACPI type */
89 u_int node_offset; /* offset in IORT - node ID */
90 u_int nentries; /* items in array below */
91 u_int usecount; /* for bookkeeping */
92 u_int revision; /* node revision */
94 ACPI_IORT_ROOT_COMPLEX pci_rc; /* PCI root complex */
96 ACPI_IORT_SMMU_V3 smmu_v3;
99 struct iort_map_entry *mappings; /* node mappings */
100 struct iort_its_entry *its; /* ITS IDs array */
104 /* Lists for each of the types. */
105 static TAILQ_HEAD(, iort_node) pci_nodes = TAILQ_HEAD_INITIALIZER(pci_nodes);
106 static TAILQ_HEAD(, iort_node) smmu_nodes = TAILQ_HEAD_INITIALIZER(smmu_nodes);
107 static TAILQ_HEAD(, iort_node) its_groups = TAILQ_HEAD_INITIALIZER(its_groups);
110 iort_entry_get_id_mapping_index(struct iort_node *node)
114 case ACPI_IORT_NODE_SMMU_V3:
115 /* The ID mapping field was added in version 1 */
116 if (node->revision < 1)
120 * If all the control interrupts are GISCV based the ID
121 * mapping field is ignored.
123 if (node->data.smmu_v3.EventGsiv != 0 &&
124 node->data.smmu_v3.PriGsiv != 0 &&
125 node->data.smmu_v3.GerrGsiv != 0 &&
126 node->data.smmu_v3.SyncGsiv != 0)
129 if (node->data.smmu_v3.IdMappingIndex >= node->nentries)
132 return (node->data.smmu_v3.IdMappingIndex);
133 case ACPI_IORT_NODE_PMCG:
143 * Lookup an ID in the mappings array. If successful, map the input ID
144 * to the output ID and return the output node found.
146 static struct iort_node *
147 iort_entry_lookup(struct iort_node *node, u_int id, u_int *outid)
149 struct iort_map_entry *entry;
152 id_map = iort_entry_get_id_mapping_index(node);
153 entry = node->entries.mappings;
154 for (i = 0; i < node->nentries; i++, entry++) {
157 if (entry->base <= id && id <= entry->end)
160 if (i == node->nentries)
162 if ((entry->flags & ACPI_IORT_ID_SINGLE_MAPPING) == 0)
163 *outid = entry->outbase + (id - entry->base);
165 *outid = entry->outbase;
166 return (entry->out_node);
170 * Map a PCI RID to a SMMU node or an ITS node, based on outtype.
172 static struct iort_node *
173 iort_pci_rc_map(u_int seg, u_int rid, u_int outtype, u_int *outid)
175 struct iort_node *node, *out_node;
179 TAILQ_FOREACH(node, &pci_nodes, next) {
180 if (node->data.pci_rc.PciSegmentNumber != seg)
182 out_node = iort_entry_lookup(node, rid, &nxtid);
183 if (out_node != NULL)
187 /* Could not find a PCI RC node with segment and device ID. */
188 if (out_node == NULL)
191 /* Node can be SMMU or ITS. If SMMU, we need another lookup. */
192 if (outtype == ACPI_IORT_NODE_ITS_GROUP &&
193 (out_node->type == ACPI_IORT_NODE_SMMU_V3 ||
194 out_node->type == ACPI_IORT_NODE_SMMU)) {
195 out_node = iort_entry_lookup(out_node, nxtid, &nxtid);
196 if (out_node == NULL)
200 KASSERT(out_node->type == outtype, ("mapping fail"));
207 * Not implemented, map a PCIe device to the SMMU it is associated with.
210 acpi_iort_map_smmu(u_int seg, u_int devid, void **smmu, u_int *sid)
212 /* XXX: convert oref to SMMU device */
218 * Allocate memory for a node, initialize and copy mappings. 'start'
219 * argument provides the table start used to calculate the node offset.
222 iort_copy_data(struct iort_node *node, ACPI_IORT_NODE *node_entry)
224 ACPI_IORT_ID_MAPPING *map_entry;
225 struct iort_map_entry *mapping;
228 map_entry = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node_entry,
229 node_entry->MappingOffset);
230 node->nentries = node_entry->MappingCount;
232 mapping = malloc(sizeof(*mapping) * node->nentries, M_DEVBUF,
234 node->entries.mappings = mapping;
235 for (i = 0; i < node->nentries; i++, mapping++, map_entry++) {
236 mapping->base = map_entry->InputBase;
238 * IdCount means "The number of IDs in the range minus one" (ARM DEN 0049D).
239 * We use <= for comparison against this field, so don't add one here.
241 mapping->end = map_entry->InputBase + map_entry->IdCount;
242 mapping->outbase = map_entry->OutputBase;
243 mapping->out_node_offset = map_entry->OutputReference;
244 mapping->flags = map_entry->Flags;
245 mapping->out_node = NULL;
250 * Allocate and copy an ITS group.
253 iort_copy_its(struct iort_node *node, ACPI_IORT_NODE *node_entry)
255 struct iort_its_entry *its;
256 ACPI_IORT_ITS_GROUP *itsg_entry;
260 itsg_entry = (ACPI_IORT_ITS_GROUP *)node_entry->NodeData;
261 node->nentries = itsg_entry->ItsCount;
263 its = malloc(sizeof(*its) * node->nentries, M_DEVBUF, M_WAITOK | M_ZERO);
264 node->entries.its = its;
265 id = &itsg_entry->Identifiers[0];
266 for (i = 0; i < node->nentries; i++, its++, id++) {
274 * Walk the IORT table and add nodes to corresponding list.
277 iort_add_nodes(ACPI_IORT_NODE *node_entry, u_int node_offset)
279 ACPI_IORT_ROOT_COMPLEX *pci_rc;
280 ACPI_IORT_SMMU *smmu;
281 ACPI_IORT_SMMU_V3 *smmu_v3;
282 struct iort_node *node;
284 node = malloc(sizeof(*node), M_DEVBUF, M_WAITOK | M_ZERO);
285 node->type = node_entry->Type;
286 node->node_offset = node_offset;
287 node->revision = node_entry->Revision;
289 /* copy nodes depending on type */
290 switch(node_entry->Type) {
291 case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
292 pci_rc = (ACPI_IORT_ROOT_COMPLEX *)node_entry->NodeData;
293 memcpy(&node->data.pci_rc, pci_rc, sizeof(*pci_rc));
294 iort_copy_data(node, node_entry);
295 TAILQ_INSERT_TAIL(&pci_nodes, node, next);
297 case ACPI_IORT_NODE_SMMU:
298 smmu = (ACPI_IORT_SMMU *)node_entry->NodeData;
299 memcpy(&node->data.smmu, smmu, sizeof(*smmu));
300 iort_copy_data(node, node_entry);
301 TAILQ_INSERT_TAIL(&smmu_nodes, node, next);
303 case ACPI_IORT_NODE_SMMU_V3:
304 smmu_v3 = (ACPI_IORT_SMMU_V3 *)node_entry->NodeData;
305 memcpy(&node->data.smmu_v3, smmu_v3, sizeof(*smmu_v3));
306 iort_copy_data(node, node_entry);
307 TAILQ_INSERT_TAIL(&smmu_nodes, node, next);
309 case ACPI_IORT_NODE_ITS_GROUP:
310 iort_copy_its(node, node_entry);
311 TAILQ_INSERT_TAIL(&its_groups, node, next);
314 printf("ACPI: IORT: Dropping unhandled type %u\n",
316 free(node, M_DEVBUF);
322 * For the mapping entry given, walk thru all the possible destination
323 * nodes and resolve the output reference.
326 iort_resolve_node(struct iort_map_entry *entry, int check_smmu)
328 struct iort_node *node, *np;
332 TAILQ_FOREACH(np, &smmu_nodes, next) {
333 if (entry->out_node_offset == np->node_offset) {
340 TAILQ_FOREACH(np, &its_groups, next) {
341 if (entry->out_node_offset == np->node_offset) {
349 entry->out_node = node;
351 printf("ACPI: IORT: Firmware Bug: no mapping for node %u\n",
352 entry->out_node_offset);
357 * Resolve all output node references to node pointers.
360 iort_post_process_mappings(void)
362 struct iort_node *node;
365 TAILQ_FOREACH(node, &pci_nodes, next)
366 for (i = 0; i < node->nentries; i++)
367 iort_resolve_node(&node->entries.mappings[i], TRUE);
368 TAILQ_FOREACH(node, &smmu_nodes, next)
369 for (i = 0; i < node->nentries; i++)
370 iort_resolve_node(&node->entries.mappings[i], FALSE);
371 /* TODO: named nodes */
375 * Walk MADT table, assign PIC xrefs to all ITS entries.
378 madt_resolve_its_xref(ACPI_SUBTABLE_HEADER *entry, void *arg)
380 ACPI_MADT_GENERIC_TRANSLATOR *gict;
381 struct iort_node *its_node;
382 struct iort_its_entry *its_entry;
386 if (entry->Type != ACPI_MADT_TYPE_GENERIC_TRANSLATOR)
389 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
391 xref = acpi_its_xref++;
392 TAILQ_FOREACH(its_node, &its_groups, next) {
393 its_entry = its_node->entries.its;
394 for (i = 0; i < its_node->nentries; i++, its_entry++) {
395 if (its_entry->its_id == gict->TranslationId) {
396 its_entry->xref = xref;
402 printf("ACPI: IORT: Unused ITS block, ID %u\n",
403 gict->TranslationId);
407 * Walk SRAT, assign proximity to all ITS entries.
410 srat_resolve_its_pxm(ACPI_SUBTABLE_HEADER *entry, void *arg)
412 ACPI_SRAT_GIC_ITS_AFFINITY *gicits;
413 struct iort_node *its_node;
414 struct iort_its_entry *its_entry;
418 if (entry->Type != ACPI_SRAT_TYPE_GIC_ITS_AFFINITY)
423 gicits = (ACPI_SRAT_GIC_ITS_AFFINITY *)entry;
424 dom = acpi_map_pxm_to_vm_domainid(gicits->ProximityDomain);
427 * Catch firmware and config errors. map_counts keeps a
428 * count of ProximityDomain values mapping to a domain ID
432 printf("Firmware Error: Proximity Domain %d could not be"
433 " mapped for GIC ITS ID %d!\n",
434 gicits->ProximityDomain, gicits->ItsId);
436 /* use dom + 1 as index to handle the case where dom == -1 */
437 i = ++map_counts[dom + 1];
441 printf("ERROR: Multiple Proximity Domains map to the"
442 " same NUMA domain %d!\n", dom);
444 printf("WARNING: multiple Proximity Domains in SRAT but NUMA"
448 TAILQ_FOREACH(its_node, &its_groups, next) {
449 its_entry = its_node->entries.its;
450 for (i = 0; i < its_node->nentries; i++, its_entry++) {
451 if (its_entry->its_id == gicits->ItsId) {
452 its_entry->pxm = dom;
458 printf("ACPI: IORT: ITS block %u in SRAT not found in IORT!\n",
463 * Cross check the ITS Id with MADT and (if available) SRAT.
466 iort_post_process_its(void)
468 ACPI_TABLE_MADT *madt;
469 ACPI_TABLE_SRAT *srat;
470 vm_paddr_t madt_pa, srat_pa;
471 int map_counts[MAXMEMDOM + 1] = { 0 };
473 /* Check ITS block in MADT */
474 madt_pa = acpi_find_table(ACPI_SIG_MADT);
475 KASSERT(madt_pa != 0, ("no MADT!"));
476 madt = acpi_map_table(madt_pa, ACPI_SIG_MADT);
477 KASSERT(madt != NULL, ("can't map MADT!"));
478 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
479 madt_resolve_its_xref, NULL);
480 acpi_unmap_table(madt);
482 /* Get proximtiy if available */
483 srat_pa = acpi_find_table(ACPI_SIG_SRAT);
485 srat = acpi_map_table(srat_pa, ACPI_SIG_SRAT);
486 KASSERT(srat != NULL, ("can't map SRAT!"));
487 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
488 srat_resolve_its_pxm, map_counts);
489 acpi_unmap_table(srat);
495 * Find, parse, and save IO Remapping Table ("IORT").
498 acpi_parse_iort(void *dummy __unused)
500 ACPI_TABLE_IORT *iort;
501 ACPI_IORT_NODE *node_entry;
505 iort_pa = acpi_find_table(ACPI_SIG_IORT);
509 iort = acpi_map_table(iort_pa, ACPI_SIG_IORT);
511 printf("ACPI: Unable to map the IORT table!\n");
514 for (node_offset = iort->NodeOffset;
515 node_offset < iort->Header.Length;
516 node_offset += node_entry->Length) {
517 node_entry = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, node_offset);
518 iort_add_nodes(node_entry, node_offset);
520 acpi_unmap_table(iort);
521 iort_post_process_mappings();
522 iort_post_process_its();
525 SYSINIT(acpi_parse_iort, SI_SUB_DRIVERS, SI_ORDER_FIRST, acpi_parse_iort, NULL);
528 * Provide ITS ID to PIC xref mapping.
531 acpi_iort_its_lookup(u_int its_id, u_int *xref, int *pxm)
533 struct iort_node *its_node;
534 struct iort_its_entry *its_entry;
537 TAILQ_FOREACH(its_node, &its_groups, next) {
538 its_entry = its_node->entries.its;
539 for (i = 0; i < its_node->nentries; i++, its_entry++) {
540 if (its_entry->its_id == its_id) {
541 *xref = its_entry->xref;
542 *pxm = its_entry->pxm;
551 * Find mapping for a PCIe device given segment and device ID
552 * returns the XREF for MSI interrupt setup and the device ID to
553 * use for the interrupt setup
556 acpi_iort_map_pci_msi(u_int seg, u_int rid, u_int *xref, u_int *devid)
558 struct iort_node *node;
560 node = iort_pci_rc_map(seg, rid, ACPI_IORT_NODE_ITS_GROUP, devid);
564 /* This should be an ITS node */
565 KASSERT(node->type == ACPI_IORT_NODE_ITS_GROUP, ("bad group"));
567 /* return first node, we don't handle more than that now. */
568 *xref = node->entries.its[0].xref;
573 acpi_iort_map_pci_smmuv3(u_int seg, u_int rid, u_int *xref, u_int *sid)
575 ACPI_IORT_SMMU_V3 *smmu;
576 struct iort_node *node;
578 node = iort_pci_rc_map(seg, rid, ACPI_IORT_NODE_SMMU_V3, sid);
582 /* This should be an SMMU node. */
583 KASSERT(node->type == ACPI_IORT_NODE_SMMU_V3, ("bad node"));
585 smmu = (ACPI_IORT_SMMU_V3 *)&node->data.smmu_v3;
586 *xref = smmu->BaseAddress;