Home | History | Annotate | Line # | Download | only in acpi
      1  1.2  jmcneill /* $NetBSD: acpi_pci_smccc.c,v 1.2 2022/10/15 10:45:40 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_pci_smccc.c,v 1.2 2022/10/15 10:45:40 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/kernel.h>
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <dev/pci/pcireg.h>
     36  1.1  jmcneill #include <dev/pci/pcivar.h>
     37  1.1  jmcneill #include <dev/pci/pciconf.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/acpi/acpivar.h>
     40  1.1  jmcneill #include <dev/acpi/acpi_pci.h>
     41  1.1  jmcneill #include <dev/acpi/acpi_mcfg.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <arm/acpi/acpi_pci_machdep.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <arm/pci/pci_smccc.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill static int
     48  1.1  jmcneill acpi_pci_smccc_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg,
     49  1.1  jmcneill     pcireg_t *data)
     50  1.1  jmcneill {
     51  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
     52  1.1  jmcneill 	int b, d, f;
     53  1.1  jmcneill 	int status;
     54  1.1  jmcneill 
     55  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
     56  1.1  jmcneill 
     57  1.1  jmcneill 	if (b < ap->ap_bus || b > ap->ap_maxbus) {
     58  1.1  jmcneill 		*data = -1;
     59  1.1  jmcneill 		return EINVAL;
     60  1.1  jmcneill 	}
     61  1.1  jmcneill 
     62  1.1  jmcneill 	status = pci_smccc_read(PCI_SMCCC_SBDF(ap->ap_seg, b, d, f), reg,
     63  1.1  jmcneill 				PCI_SMCCC_ACCESS_32BIT, data);
     64  1.1  jmcneill 	if (!PCI_SMCCC_SUCCESS(status)) {
     65  1.1  jmcneill 		*data = -1;
     66  1.1  jmcneill 		return EINVAL;
     67  1.1  jmcneill 	}
     68  1.1  jmcneill 
     69  1.1  jmcneill 	return 0;
     70  1.1  jmcneill }
     71  1.1  jmcneill 
     72  1.1  jmcneill static int
     73  1.1  jmcneill acpi_pci_smccc_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg,
     74  1.1  jmcneill     pcireg_t data)
     75  1.1  jmcneill {
     76  1.1  jmcneill 	struct acpi_pci_context *ap = pc->pc_conf_v;
     77  1.1  jmcneill 	int b, d, f;
     78  1.1  jmcneill 	int status;
     79  1.1  jmcneill 
     80  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
     81  1.1  jmcneill 
     82  1.1  jmcneill 	if (b < ap->ap_bus || b > ap->ap_maxbus) {
     83  1.1  jmcneill 		return EINVAL;
     84  1.1  jmcneill 	}
     85  1.1  jmcneill 
     86  1.1  jmcneill 	status = pci_smccc_write(PCI_SMCCC_SBDF(ap->ap_seg, b, d, f), reg,
     87  1.2  jmcneill 				 PCI_SMCCC_ACCESS_32BIT, data);
     88  1.1  jmcneill 	if (!PCI_SMCCC_SUCCESS(status)) {
     89  1.1  jmcneill 		return EINVAL;
     90  1.1  jmcneill 	}
     91  1.1  jmcneill 
     92  1.1  jmcneill 	return 0;
     93  1.1  jmcneill }
     94  1.1  jmcneill 
     95  1.1  jmcneill 
     96  1.1  jmcneill void
     97  1.1  jmcneill acpi_pci_smccc_init(struct acpi_pci_context *ap)
     98  1.1  jmcneill {
     99  1.1  jmcneill 	int status, ver;
    100  1.1  jmcneill 	uint8_t bus_start, bus_end;
    101  1.1  jmcneill 	uint16_t next_seg;
    102  1.1  jmcneill 
    103  1.1  jmcneill 	ver = pci_smccc_version();
    104  1.1  jmcneill 	if (!PCI_SMCCC_SUCCESS(ver)) {
    105  1.1  jmcneill 		aprint_error_dev(ap->ap_dev,
    106  1.1  jmcneill 		    "SMCCC: PCI_VERSION call failed, status %#x\n", ver);
    107  1.1  jmcneill 		return;
    108  1.1  jmcneill 	}
    109  1.1  jmcneill 	aprint_normal_dev(ap->ap_dev, "SMCCC: PCI impl. version %u.%u\n",
    110  1.1  jmcneill 	    (ver >> 16) & 0x7fff, ver & 0xffff);
    111  1.1  jmcneill 
    112  1.1  jmcneill 	status = pci_smccc_get_seg_info(ap->ap_seg, &bus_start, &bus_end,
    113  1.1  jmcneill 	    &next_seg);
    114  1.1  jmcneill 	if (!PCI_SMCCC_SUCCESS(status)) {
    115  1.1  jmcneill 		aprint_error_dev(ap->ap_dev,
    116  1.1  jmcneill 		    "SMCCC: No info for segment %u, status %#x\n",
    117  1.1  jmcneill 		    ap->ap_seg, status);
    118  1.1  jmcneill 		return;
    119  1.1  jmcneill 	}
    120  1.1  jmcneill 	aprint_normal_dev(ap->ap_dev, "SMCCC: segment %u, bus %u-%u\n",
    121  1.1  jmcneill 	    ap->ap_seg, bus_start, bus_end);
    122  1.1  jmcneill 
    123  1.1  jmcneill 	ap->ap_bus = bus_start;
    124  1.1  jmcneill 	ap->ap_maxbus = bus_end;
    125  1.1  jmcneill 	ap->ap_conf_read = acpi_pci_smccc_conf_read;
    126  1.1  jmcneill 	ap->ap_conf_write = acpi_pci_smccc_conf_write;
    127  1.1  jmcneill 	ap->ap_flags |= ACPI_PCI_FLAG_NO_MCFG;
    128  1.1  jmcneill 	ap->ap_pciflags_clear = PCI_FLAGS_MSI_OKAY | PCI_FLAGS_MSIX_OKAY;
    129  1.1  jmcneill }
    130