Home | History | Annotate | Line # | Download | only in acpi
acpi_pci_n1sdp.c revision 1.2
      1 /* $NetBSD: acpi_pci_n1sdp.c,v 1.2 2020/02/13 00:02:40 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2020 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_pci_n1sdp.c,v 1.2 2020/02/13 00:02:40 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/device.h>
     38 #include <sys/intr.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/extent.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <machine/cpu.h>
     45 
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pciconf.h>
     49 
     50 #include <dev/acpi/acpivar.h>
     51 #include <dev/acpi/acpi_pci.h>
     52 #include <dev/acpi/acpi_mcfg.h>
     53 
     54 #include <arm/acpi/acpi_pci_machdep.h>
     55 
     56 extern struct bus_space arm_generic_bs_tag;
     57 
     58 /* Shared memory location written by the SCP at boot */
     59 #define	N1SDP_SHARED_MEM_BASE	0x06000000
     60 
     61 #define	N1SDP_NSEGS		2
     62 #define	N1SDP_TABLE_SIZE	0x4000
     63 
     64 #define	N1SDP_BUS_SHIFT		20
     65 #define	N1SDP_DEV_SHIFT		15
     66 #define	N1SDP_FUNC_SHIFT	12
     67 
     68 struct n1sdp_pcie_discovery_data {
     69 	uint32_t	rc_base_addr;
     70 	uint32_t	nr_bdfs;
     71 	uint32_t	valid_bdfs[0];
     72 };
     73 
     74 static struct n1sdp_pcie_discovery_data *n1sdp_data[N1SDP_NSEGS];
     75 
     76 static bool
     77 acpi_pci_n1sdp_valid(pci_chipset_tag_t pc, pcitag_t tag)
     78 {
     79 	struct acpi_pci_context *ap = pc->pc_conf_v;
     80 	u_int n, bdfaddr;
     81 	int b, d, f;
     82 
     83 	if (ap->ap_seg >= N1SDP_NSEGS || n1sdp_data[ap->ap_seg] == NULL)
     84 		return false;
     85 
     86 	pci_decompose_tag(pc, tag, &b, &d, &f);
     87 
     88 	bdfaddr = (b << N1SDP_BUS_SHIFT) +
     89 		  (d << N1SDP_DEV_SHIFT) +
     90 		  (f << N1SDP_FUNC_SHIFT);
     91 
     92 	for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
     93 		if (n1sdp_data[ap->ap_seg]->valid_bdfs[n] == bdfaddr)
     94 			return true;
     95 	}
     96 
     97 	return false;
     98 }
     99 
    100 static int
    101 acpi_pci_n1sdp_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
    102 {
    103 	struct acpi_pci_context *ap = pc->pc_conf_v;
    104 	int b, d, f;
    105 
    106 	pci_decompose_tag(pc, tag, &b, &d, &f);
    107 
    108 	if (ap->ap_bus == b) {
    109 		if (d > 0 || f > 0 || reg >= PCI_EXTCONF_SIZE) {
    110 			*data = -1;
    111 			return EINVAL;
    112 		}
    113 		*data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg);
    114 		return 0;
    115 	}
    116 
    117 	if (!acpi_pci_n1sdp_valid(pc, tag)) {
    118 		*data = -1;
    119 		return 0;
    120 	}
    121 
    122 	return acpimcfg_conf_read(pc, tag, reg, data);
    123 }
    124 
    125 static int
    126 acpi_pci_n1sdp_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    127 {
    128 	struct acpi_pci_context *ap = pc->pc_conf_v;
    129 	int b, d, f;
    130 
    131 	pci_decompose_tag(pc, tag, &b, &d, &f);
    132 
    133 	if (ap->ap_bus == b) {
    134 		if (d > 0 || f > 0 || reg >= PCI_EXTCONF_SIZE) {
    135 			return EINVAL;
    136 		}
    137 		bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data);
    138 		return 0;
    139 	}
    140 
    141 	if (!acpi_pci_n1sdp_valid(pc, tag))
    142 		return 0;
    143 
    144 	return acpimcfg_conf_write(pc, tag, reg, data);
    145 }
    146 
    147 void
    148 acpi_pci_n1sdp_init(struct acpi_pci_context *ap)
    149 {
    150 	bus_space_tag_t bst = &arm_generic_bs_tag;
    151 	bus_space_handle_t bsh;
    152 	paddr_t pa;
    153 	int error;
    154 	u_int n;
    155 
    156 	if (ap->ap_seg >= N1SDP_NSEGS)
    157 		return;
    158 
    159 	if (n1sdp_data[ap->ap_seg] == NULL) {
    160 		aprint_normal_dev(ap->ap_dev, "applying N1SDP quirk for segment %d\n", ap->ap_seg);
    161 
    162 		pa = N1SDP_SHARED_MEM_BASE + ap->ap_seg * N1SDP_TABLE_SIZE;
    163 		if (bus_space_map(bst, pa, N1SDP_TABLE_SIZE, BUS_SPACE_MAP_LINEAR, &bsh) != 0)
    164 			panic("acpi_pci_n1sdp_init: couldn't map PCIe discovery table");
    165 
    166 		n1sdp_data[ap->ap_seg] = bus_space_vaddr(bst, bsh);
    167 		if (n1sdp_data[ap->ap_seg] == NULL)
    168 			panic("acpi_pci_n1sdp_init: couldn't get PCIe discovery table VA");
    169 
    170 		error = bus_space_map(bst, n1sdp_data[ap->ap_seg]->rc_base_addr, PCI_EXTCONF_SIZE,
    171 		    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &ap->ap_conf_bsh);
    172 		if (error != 0)
    173 			panic("acpi_pci_n1sdp_init: couldn't map segment %d", ap->ap_seg);
    174 
    175 		aprint_debug_dev(ap->ap_dev, "N1SDP: RC @ 0x%08x, %d devices\n",
    176 		    n1sdp_data[ap->ap_seg]->rc_base_addr, n1sdp_data[ap->ap_seg]->nr_bdfs);
    177 		for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
    178 			const uint32_t bdf = n1sdp_data[ap->ap_seg]->valid_bdfs[n];
    179 			const int b = (bdf >> N1SDP_BUS_SHIFT) & 0xff;
    180 			const int d = (bdf >> N1SDP_DEV_SHIFT) & 0x1f;
    181 			const int f = (bdf >> N1SDP_FUNC_SHIFT) & 0x7;
    182 			aprint_debug_dev(ap->ap_dev, "N1SDP: %02x:%02x.%x (%08x)\n", b, d, f, bdf);
    183 		}
    184 	}
    185 
    186 	ap->ap_conf_read = acpi_pci_n1sdp_conf_read;
    187 	ap->ap_conf_write = acpi_pci_n1sdp_conf_write;
    188 
    189 	/* IO space access seems to cause async SErrors, so disable for now */
    190 	ap->ap_pciflags_clear = PCI_FLAGS_IO_OKAY;
    191 }
    192