Home | History | Annotate | Line # | Download | only in acpi
acpi_iort.c revision 1.1
      1 /* $NetBSD: acpi_iort.c,v 1.1 2018/12/08 15:04:40 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.1 2018/12/08 15:04:40 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 
     39 #include <dev/acpi/acpireg.h>
     40 #include <dev/acpi/acpivar.h>
     41 
     42 #include <arm/acpi/acpi_iort.h>
     43 
     44 static ACPI_IORT_ID_MAPPING *
     45 acpi_iort_id_map(ACPI_IORT_NODE *node, uint32_t *id)
     46 {
     47 	ACPI_IORT_ID_MAPPING *map;
     48 	uint32_t offset, n;
     49 
     50 	offset = node->MappingOffset;
     51 	for (n = 0; n < node->MappingCount; n++) {
     52 		map = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node, offset);
     53 		if (map->Flags & ACPI_IORT_ID_SINGLE_MAPPING) {
     54 			*id = map->InputBase;
     55 			return map;
     56 		}
     57 		if (*id >= map->InputBase && *id <= map->InputBase + map->IdCount) {
     58 			*id = *id - map->InputBase + map->OutputBase;
     59 			return map;
     60 		}
     61 		offset += sizeof(ACPI_IORT_ID_MAPPING);
     62 	}
     63 
     64 	return NULL;
     65 }
     66 
     67 static ACPI_IORT_NODE *
     68 acpi_iort_find_ref(ACPI_TABLE_IORT *iort, ACPI_IORT_NODE *node, uint32_t *id)
     69 {
     70 	ACPI_IORT_ID_MAPPING *map;
     71 
     72 	map = acpi_iort_id_map(node, id);
     73 	if (map == NULL)
     74 		return NULL;
     75 
     76 	return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, map->OutputReference);
     77 }
     78 
     79 uint32_t
     80 acpi_iort_pci_root_map(u_int seg, uint32_t devid)
     81 {
     82 	ACPI_TABLE_IORT *iort;
     83 	ACPI_IORT_NODE *node;
     84 	ACPI_IORT_ROOT_COMPLEX *root;
     85 	uint32_t offset, n;
     86 	ACPI_STATUS rv;
     87 
     88 	rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
     89 	if (ACPI_FAILURE(rv))
     90 		return devid;
     91 
     92 	offset = iort->NodeOffset;
     93 	for (n = 0; n < iort->NodeCount; n++) {
     94 		node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
     95 		if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
     96 			root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
     97 			if (root->PciSegmentNumber == seg) {
     98 				const uint32_t odevid = devid;
     99 				do {
    100 					node = acpi_iort_find_ref(iort, node, &devid);
    101 				} while (node != NULL);
    102 				aprint_debug("ACPI: IORT remapped devid %#x -> %#x\n", odevid, devid);
    103 				return devid;
    104 			}
    105 		}
    106 		offset += node->Length;
    107 	}
    108 
    109 	return devid;
    110 }
    111