xref: /netbsd-src/sys/dev/ofw/ofw_pci_subr.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: ofw_pci_subr.c,v 1.1 2021/05/12 23:22:33 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.1 2021/05/12 23:22:33 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 
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_pci.h>
44 
45 static int
46 ofw_pci_bus_get_child_devhandle(device_t dev, devhandle_t call_handle, void *v)
47 {
48 	struct pci_bus_get_child_devhandle_args *args = v;
49 	int phandle = devhandle_to_of(call_handle);
50 	struct ofw_pci_register opr;
51 	int d, f, len;
52 	uint32_t phys_hi;
53 
54 	/*
55 	 * No need to compare the bus number; we are searching
56 	 * only direct children of the specified node.  Skipping
57 	 * the bus number comparison allows us to dodge a slight
58 	 * difference between the OpenFirmware and FDT PCI bindings
59 	 * as it relates to PCI-PCI bridges.
60 	 */
61 
62 	pci_decompose_tag(args->pc, args->tag, NULL, &d, &f);
63 
64 	for (phandle = OF_child(phandle); phandle != 0;
65 	     phandle = OF_peer(phandle)) {
66 		len = OF_getprop(phandle, "reg", &opr, sizeof(opr));
67 		if (len < sizeof(opr)) {
68 			continue;
69 		}
70 
71 		phys_hi = be32toh(opr.phys_hi);
72 
73 		if (d != OFW_PCI_PHYS_HI_DEVICE(phys_hi) ||
74 		    f != OFW_PCI_PHYS_HI_FUNCTION(phys_hi)) {
75 			continue;
76 		}
77 
78 		/* Found it! */
79 		args->devhandle = devhandle_from_of(phandle);
80 		return 0;
81 	}
82 
83 	return ENODEV;
84 }
85 OF_DEVICE_CALL_REGISTER("pci-bus-get-child-devhandle",
86 			ofw_pci_bus_get_child_devhandle)
87