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