1 /* $NetBSD: ofw_pci_subr.c,v 1.3 2022/01/22 11:49:18 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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: ofw_pci_subr.c,v 1.3 2022/01/22 11:49:18 thorpej Exp $");
34
35 #include <sys/types.h>
36 #include <sys/device.h>
37 #include <sys/endian.h>
38 #include <sys/errno.h>
39
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pci_calls.h>
42
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_pci.h>
45
46 static int
ofw_pci_bus_get_child_devhandle(device_t dev,devhandle_t call_handle,void * v)47 ofw_pci_bus_get_child_devhandle(device_t dev, devhandle_t call_handle, void *v)
48 {
49 struct pci_bus_get_child_devhandle_args *args = v;
50 int phandle = devhandle_to_of(call_handle);
51 struct ofw_pci_register opr;
52 int d, f, len;
53 uint32_t phys_hi;
54
55 /*
56 * No need to compare the bus number; we are searching
57 * only direct children of the specified node. Skipping
58 * the bus number comparison allows us to dodge a slight
59 * difference between the OpenFirmware and FDT PCI bindings
60 * as it relates to PCI-PCI bridges.
61 */
62
63 pci_decompose_tag(args->pc, args->tag, NULL, &d, &f);
64
65 for (phandle = OF_child(phandle); phandle != 0;
66 phandle = OF_peer(phandle)) {
67 len = OF_getprop(phandle, "reg", &opr, sizeof(opr));
68 if (len < sizeof(opr)) {
69 continue;
70 }
71
72 phys_hi = be32toh(opr.phys_hi);
73
74 if (d != OFW_PCI_PHYS_HI_DEVICE(phys_hi) ||
75 f != OFW_PCI_PHYS_HI_FUNCTION(phys_hi)) {
76 continue;
77 }
78
79 /* Found it! */
80 args->devhandle = devhandle_from_of(call_handle, phandle);
81 return 0;
82 }
83
84 return ENODEV;
85 }
86 OF_DEVICE_CALL_REGISTER(PCI_BUS_GET_CHILD_DEVHANDLE_STR,
87 ofw_pci_bus_get_child_devhandle)
88