1 1.5 jmcneill /* $NetBSD: acpi_pci_graviton.c,v 1.5 2022/10/15 11:07:38 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.2 ad * Copyright (c) 2018, 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.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_pci_graviton.c,v 1.5 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.2 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 static int 55 1.1 jmcneill acpi_pci_graviton_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data) 56 1.1 jmcneill { 57 1.1 jmcneill struct acpi_pci_context *ap = pc->pc_conf_v; 58 1.1 jmcneill int b, d, f; 59 1.1 jmcneill 60 1.1 jmcneill pci_decompose_tag(pc, tag, &b, &d, &f); 61 1.1 jmcneill 62 1.1 jmcneill if (ap->ap_bus == b) { 63 1.1 jmcneill if (d > 0 || f > 0) { 64 1.1 jmcneill *data = -1; 65 1.1 jmcneill return EINVAL; 66 1.1 jmcneill } 67 1.1 jmcneill *data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg); 68 1.1 jmcneill return 0; 69 1.1 jmcneill } 70 1.4 skrll 71 1.1 jmcneill return acpimcfg_conf_read(pc, tag, reg, data); 72 1.1 jmcneill } 73 1.1 jmcneill 74 1.1 jmcneill static int 75 1.1 jmcneill acpi_pci_graviton_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data) 76 1.1 jmcneill { 77 1.1 jmcneill struct acpi_pci_context *ap = pc->pc_conf_v; 78 1.1 jmcneill int b, d, f; 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 (ap->ap_bus == b) { 83 1.1 jmcneill if (d > 0 || f > 0) { 84 1.1 jmcneill return EINVAL; 85 1.1 jmcneill } 86 1.1 jmcneill bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data); 87 1.1 jmcneill return 0; 88 1.1 jmcneill } 89 1.4 skrll 90 1.1 jmcneill return acpimcfg_conf_write(pc, tag, reg, data); 91 1.1 jmcneill } 92 1.1 jmcneill 93 1.1 jmcneill static ACPI_STATUS 94 1.1 jmcneill acpi_pci_graviton_map(ACPI_HANDLE handle, UINT32 level, void *ctx, void **retval) 95 1.1 jmcneill { 96 1.1 jmcneill struct acpi_pci_context *ap = ctx; 97 1.1 jmcneill struct acpi_resources res; 98 1.1 jmcneill struct acpi_mem *mem; 99 1.1 jmcneill ACPI_HANDLE parent; 100 1.1 jmcneill ACPI_INTEGER seg; 101 1.1 jmcneill ACPI_STATUS rv; 102 1.1 jmcneill int error; 103 1.1 jmcneill 104 1.1 jmcneill rv = AcpiGetParent(handle, &parent); 105 1.1 jmcneill if (ACPI_FAILURE(rv)) 106 1.1 jmcneill return rv; 107 1.1 jmcneill rv = acpi_eval_integer(parent, "_SEG", &seg); 108 1.1 jmcneill if (ACPI_FAILURE(rv)) 109 1.1 jmcneill seg = 0; 110 1.1 jmcneill if (ap->ap_seg != seg) 111 1.1 jmcneill return AE_OK; 112 1.1 jmcneill 113 1.1 jmcneill rv = acpi_resource_parse(ap->ap_dev, handle, "_CRS", &res, &acpi_resource_parse_ops_quiet); 114 1.1 jmcneill if (ACPI_FAILURE(rv)) 115 1.1 jmcneill return rv; 116 1.1 jmcneill 117 1.1 jmcneill mem = acpi_res_mem(&res, 0); 118 1.1 jmcneill if (mem == NULL) { 119 1.1 jmcneill acpi_resource_cleanup(&res); 120 1.1 jmcneill return AE_NOT_FOUND; 121 1.1 jmcneill } 122 1.1 jmcneill 123 1.1 jmcneill error = bus_space_map(ap->ap_bst, mem->ar_base, mem->ar_length, 124 1.5 jmcneill BUS_SPACE_MAP_NONPOSTED, &ap->ap_conf_bsh); 125 1.1 jmcneill if (error != 0) 126 1.1 jmcneill return AE_NO_MEMORY; 127 1.1 jmcneill 128 1.1 jmcneill ap->ap_conf_read = acpi_pci_graviton_conf_read; 129 1.1 jmcneill ap->ap_conf_write = acpi_pci_graviton_conf_write; 130 1.1 jmcneill 131 1.1 jmcneill return AE_CTRL_TERMINATE; 132 1.1 jmcneill } 133 1.1 jmcneill 134 1.1 jmcneill void 135 1.1 jmcneill acpi_pci_graviton_init(struct acpi_pci_context *ap) 136 1.1 jmcneill { 137 1.1 jmcneill ACPI_STATUS rv; 138 1.1 jmcneill 139 1.1 jmcneill rv = AcpiGetDevices(__UNCONST("AMZN0001"), acpi_pci_graviton_map, ap, NULL); 140 1.1 jmcneill if (ACPI_FAILURE(rv)) 141 1.1 jmcneill return; 142 1.1 jmcneill } 143