acpi_pci_graviton.c revision 1.2 1 /* $NetBSD: acpi_pci_graviton.c,v 1.2 2020/06/15 18:57:39 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2018, 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_graviton.c,v 1.2 2020/06/15 18:57:39 ad 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 #include <sys/cpu.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pciconf.h>
48
49 #include <dev/acpi/acpivar.h>
50 #include <dev/acpi/acpi_pci.h>
51 #include <dev/acpi/acpi_mcfg.h>
52
53 #include <arm/acpi/acpi_pci_machdep.h>
54
55 static int
56 acpi_pci_graviton_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
57 {
58 struct acpi_pci_context *ap = pc->pc_conf_v;
59 int b, d, f;
60
61 pci_decompose_tag(pc, tag, &b, &d, &f);
62
63 if (ap->ap_bus == b) {
64 if (d > 0 || f > 0) {
65 *data = -1;
66 return EINVAL;
67 }
68 *data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg);
69 return 0;
70 }
71
72 return acpimcfg_conf_read(pc, tag, reg, data);
73 }
74
75 static int
76 acpi_pci_graviton_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
77 {
78 struct acpi_pci_context *ap = pc->pc_conf_v;
79 int b, d, f;
80
81 pci_decompose_tag(pc, tag, &b, &d, &f);
82
83 if (ap->ap_bus == b) {
84 if (d > 0 || f > 0) {
85 return EINVAL;
86 }
87 bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data);
88 return 0;
89 }
90
91 return acpimcfg_conf_write(pc, tag, reg, data);
92 }
93
94 static ACPI_STATUS
95 acpi_pci_graviton_map(ACPI_HANDLE handle, UINT32 level, void *ctx, void **retval)
96 {
97 struct acpi_pci_context *ap = ctx;
98 struct acpi_resources res;
99 struct acpi_mem *mem;
100 ACPI_HANDLE parent;
101 ACPI_INTEGER seg;
102 ACPI_STATUS rv;
103 int error;
104
105 rv = AcpiGetParent(handle, &parent);
106 if (ACPI_FAILURE(rv))
107 return rv;
108 rv = acpi_eval_integer(parent, "_SEG", &seg);
109 if (ACPI_FAILURE(rv))
110 seg = 0;
111 if (ap->ap_seg != seg)
112 return AE_OK;
113
114 rv = acpi_resource_parse(ap->ap_dev, handle, "_CRS", &res, &acpi_resource_parse_ops_quiet);
115 if (ACPI_FAILURE(rv))
116 return rv;
117
118 mem = acpi_res_mem(&res, 0);
119 if (mem == NULL) {
120 acpi_resource_cleanup(&res);
121 return AE_NOT_FOUND;
122 }
123
124 error = bus_space_map(ap->ap_bst, mem->ar_base, mem->ar_length,
125 _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &ap->ap_conf_bsh);
126 if (error != 0)
127 return AE_NO_MEMORY;
128
129 ap->ap_conf_read = acpi_pci_graviton_conf_read;
130 ap->ap_conf_write = acpi_pci_graviton_conf_write;
131
132 return AE_CTRL_TERMINATE;
133 }
134
135 void
136 acpi_pci_graviton_init(struct acpi_pci_context *ap)
137 {
138 ACPI_STATUS rv;
139
140 rv = AcpiGetDevices(__UNCONST("AMZN0001"), acpi_pci_graviton_map, ap, NULL);
141 if (ACPI_FAILURE(rv))
142 return;
143 }
144