Home | History | Annotate | Line # | Download | only in acpi
      1  1.7  jmcneill /* $NetBSD: acpi_pci_n1sdp.c,v 1.7 2022/10/15 11:07:38 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmcneill  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  1.1  jmcneill  *
     10  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11  1.1  jmcneill  * modification, are permitted provided that the following conditions
     12  1.1  jmcneill  * are met:
     13  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18  1.1  jmcneill  *
     19  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/cdefs.h>
     33  1.7  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_pci_n1sdp.c,v 1.7 2022/10/15 11:07:38 jmcneill Exp $");
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <sys/param.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/device.h>
     38  1.1  jmcneill #include <sys/intr.h>
     39  1.1  jmcneill #include <sys/systm.h>
     40  1.1  jmcneill #include <sys/kernel.h>
     41  1.1  jmcneill #include <sys/kmem.h>
     42  1.3        ad #include <sys/cpu.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/pci/pcireg.h>
     45  1.1  jmcneill #include <dev/pci/pcivar.h>
     46  1.1  jmcneill #include <dev/pci/pciconf.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #include <dev/acpi/acpivar.h>
     49  1.1  jmcneill #include <dev/acpi/acpi_pci.h>
     50  1.1  jmcneill #include <dev/acpi/acpi_mcfg.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #include <arm/acpi/acpi_pci_machdep.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill extern struct bus_space arm_generic_bs_tag;
     55  1.1  jmcneill 
     56  1.1  jmcneill /* Shared memory location written by the SCP at boot */
     57  1.1  jmcneill #define	N1SDP_SHARED_MEM_BASE	0x06000000
     58  1.1  jmcneill 
     59  1.1  jmcneill #define	N1SDP_NSEGS		2
     60  1.1  jmcneill #define	N1SDP_TABLE_SIZE	0x4000
     61  1.1  jmcneill 
     62  1.1  jmcneill #define	N1SDP_BUS_SHIFT		20
     63  1.1  jmcneill #define	N1SDP_DEV_SHIFT		15
     64  1.1  jmcneill #define	N1SDP_FUNC_SHIFT	12
     65  1.1  jmcneill 
     66  1.1  jmcneill struct n1sdp_pcie_discovery_data {
     67  1.1  jmcneill 	uint32_t	rc_base_addr;
     68  1.1  jmcneill 	uint32_t	nr_bdfs;
     69  1.1  jmcneill 	uint32_t	valid_bdfs[0];
     70  1.1  jmcneill };
     71  1.1  jmcneill 
     72  1.1  jmcneill static struct n1sdp_pcie_discovery_data *n1sdp_data[N1SDP_NSEGS];
     73  1.1  jmcneill 
     74  1.1  jmcneill static bool
     75  1.1  jmcneill acpi_pci_n1sdp_valid(pci_chipset_tag_t pc, pcitag_t tag)
     76  1.1  jmcneill {
     77  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
     78  1.1  jmcneill 	u_int n, bdfaddr;
     79  1.1  jmcneill 	int b, d, f;
     80  1.1  jmcneill 
     81  1.1  jmcneill 	if (ap->ap_seg >= N1SDP_NSEGS || n1sdp_data[ap->ap_seg] == NULL)
     82  1.1  jmcneill 		return false;
     83  1.1  jmcneill 
     84  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
     85  1.1  jmcneill 
     86  1.6     skrll 	bdfaddr = (b << N1SDP_BUS_SHIFT) +
     87  1.1  jmcneill 		  (d << N1SDP_DEV_SHIFT) +
     88  1.1  jmcneill 		  (f << N1SDP_FUNC_SHIFT);
     89  1.1  jmcneill 
     90  1.1  jmcneill 	for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
     91  1.1  jmcneill 		if (n1sdp_data[ap->ap_seg]->valid_bdfs[n] == bdfaddr)
     92  1.1  jmcneill 			return true;
     93  1.1  jmcneill 	}
     94  1.1  jmcneill 
     95  1.1  jmcneill 	return false;
     96  1.1  jmcneill }
     97  1.1  jmcneill 
     98  1.1  jmcneill static int
     99  1.1  jmcneill acpi_pci_n1sdp_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
    100  1.1  jmcneill {
    101  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
    102  1.1  jmcneill 	int b, d, f;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    105  1.1  jmcneill 
    106  1.1  jmcneill 	if (ap->ap_bus == b) {
    107  1.1  jmcneill 		if (d > 0 || f > 0 || reg >= PCI_EXTCONF_SIZE) {
    108  1.1  jmcneill 			*data = -1;
    109  1.1  jmcneill 			return EINVAL;
    110  1.1  jmcneill 		}
    111  1.1  jmcneill 		*data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg);
    112  1.1  jmcneill 		return 0;
    113  1.1  jmcneill 	}
    114  1.1  jmcneill 
    115  1.1  jmcneill 	if (!acpi_pci_n1sdp_valid(pc, tag)) {
    116  1.1  jmcneill 		*data = -1;
    117  1.1  jmcneill 		return 0;
    118  1.1  jmcneill 	}
    119  1.6     skrll 
    120  1.1  jmcneill 	return acpimcfg_conf_read(pc, tag, reg, data);
    121  1.1  jmcneill }
    122  1.1  jmcneill 
    123  1.1  jmcneill static int
    124  1.1  jmcneill acpi_pci_n1sdp_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    125  1.1  jmcneill {
    126  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
    127  1.1  jmcneill 	int b, d, f;
    128  1.1  jmcneill 
    129  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    130  1.1  jmcneill 
    131  1.1  jmcneill 	if (ap->ap_bus == b) {
    132  1.1  jmcneill 		if (d > 0 || f > 0 || reg >= PCI_EXTCONF_SIZE) {
    133  1.1  jmcneill 			return EINVAL;
    134  1.1  jmcneill 		}
    135  1.1  jmcneill 		bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data);
    136  1.1  jmcneill 		return 0;
    137  1.1  jmcneill 	}
    138  1.1  jmcneill 
    139  1.1  jmcneill 	if (!acpi_pci_n1sdp_valid(pc, tag))
    140  1.1  jmcneill 		return 0;
    141  1.6     skrll 
    142  1.1  jmcneill 	return acpimcfg_conf_write(pc, tag, reg, data);
    143  1.1  jmcneill }
    144  1.1  jmcneill 
    145  1.1  jmcneill void
    146  1.1  jmcneill acpi_pci_n1sdp_init(struct acpi_pci_context *ap)
    147  1.1  jmcneill {
    148  1.1  jmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
    149  1.1  jmcneill 	bus_space_handle_t bsh;
    150  1.1  jmcneill 	paddr_t pa;
    151  1.1  jmcneill 	int error;
    152  1.1  jmcneill 	u_int n;
    153  1.1  jmcneill 
    154  1.1  jmcneill 	if (ap->ap_seg >= N1SDP_NSEGS)
    155  1.1  jmcneill 		return;
    156  1.1  jmcneill 
    157  1.1  jmcneill 	if (n1sdp_data[ap->ap_seg] == NULL) {
    158  1.1  jmcneill 		aprint_normal_dev(ap->ap_dev, "applying N1SDP quirk for segment %d\n", ap->ap_seg);
    159  1.1  jmcneill 
    160  1.1  jmcneill 		pa = N1SDP_SHARED_MEM_BASE + ap->ap_seg * N1SDP_TABLE_SIZE;
    161  1.1  jmcneill 		if (bus_space_map(bst, pa, N1SDP_TABLE_SIZE, BUS_SPACE_MAP_LINEAR, &bsh) != 0)
    162  1.1  jmcneill 			panic("acpi_pci_n1sdp_init: couldn't map PCIe discovery table");
    163  1.1  jmcneill 
    164  1.1  jmcneill 		n1sdp_data[ap->ap_seg] = bus_space_vaddr(bst, bsh);
    165  1.1  jmcneill 		if (n1sdp_data[ap->ap_seg] == NULL)
    166  1.1  jmcneill 			panic("acpi_pci_n1sdp_init: couldn't get PCIe discovery table VA");
    167  1.1  jmcneill 
    168  1.1  jmcneill 		error = bus_space_map(bst, n1sdp_data[ap->ap_seg]->rc_base_addr, PCI_EXTCONF_SIZE,
    169  1.7  jmcneill 		    BUS_SPACE_MAP_NONPOSTED, &ap->ap_conf_bsh);
    170  1.1  jmcneill 		if (error != 0)
    171  1.1  jmcneill 			panic("acpi_pci_n1sdp_init: couldn't map segment %d", ap->ap_seg);
    172  1.1  jmcneill 
    173  1.1  jmcneill 		aprint_debug_dev(ap->ap_dev, "N1SDP: RC @ 0x%08x, %d devices\n",
    174  1.1  jmcneill 		    n1sdp_data[ap->ap_seg]->rc_base_addr, n1sdp_data[ap->ap_seg]->nr_bdfs);
    175  1.1  jmcneill 		for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
    176  1.5  jmcneill 			const uint32_t bdf = le32toh(n1sdp_data[ap->ap_seg]->valid_bdfs[n]);
    177  1.1  jmcneill 			const int b = (bdf >> N1SDP_BUS_SHIFT) & 0xff;
    178  1.1  jmcneill 			const int d = (bdf >> N1SDP_DEV_SHIFT) & 0x1f;
    179  1.1  jmcneill 			const int f = (bdf >> N1SDP_FUNC_SHIFT) & 0x7;
    180  1.1  jmcneill 			aprint_debug_dev(ap->ap_dev, "N1SDP: %02x:%02x.%x (%08x)\n", b, d, f, bdf);
    181  1.1  jmcneill 		}
    182  1.1  jmcneill 	}
    183  1.1  jmcneill 
    184  1.1  jmcneill 	ap->ap_conf_read = acpi_pci_n1sdp_conf_read;
    185  1.1  jmcneill 	ap->ap_conf_write = acpi_pci_n1sdp_conf_write;
    186  1.1  jmcneill 
    187  1.2  jmcneill 	/* IO space access seems to cause async SErrors, so disable for now */
    188  1.2  jmcneill 	ap->ap_pciflags_clear = PCI_FLAGS_IO_OKAY;
    189  1.1  jmcneill }
    190