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