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