xref: /netbsd-src/sys/dev/ofw/ofw_pci_subr.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1*3944ff70Sthorpej /*	$NetBSD: ofw_pci_subr.c,v 1.3 2022/01/22 11:49:18 thorpej Exp $	*/
24c5901a8Sthorpej 
34c5901a8Sthorpej /*-
44c5901a8Sthorpej  * Copyright (c) 2021 The NetBSD Foundation, Inc.
54c5901a8Sthorpej  * All rights reserved.
64c5901a8Sthorpej  *
74c5901a8Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
84c5901a8Sthorpej  * by Jason R. Thorpe.
94c5901a8Sthorpej  *
104c5901a8Sthorpej  * Redistribution and use in source and binary forms, with or without
114c5901a8Sthorpej  * modification, are permitted provided that the following conditions
124c5901a8Sthorpej  * are met:
134c5901a8Sthorpej  * 1. Redistributions of source code must retain the above copyright
144c5901a8Sthorpej  *    notice, this list of conditions and the following disclaimer.
154c5901a8Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
164c5901a8Sthorpej  *    notice, this list of conditions and the following disclaimer in the
174c5901a8Sthorpej  *    documentation and/or other materials provided with the distribution.
184c5901a8Sthorpej  *
194c5901a8Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204c5901a8Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214c5901a8Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224c5901a8Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234c5901a8Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244c5901a8Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254c5901a8Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264c5901a8Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274c5901a8Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284c5901a8Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294c5901a8Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
304c5901a8Sthorpej  */
314c5901a8Sthorpej 
324c5901a8Sthorpej #include <sys/cdefs.h>
33*3944ff70Sthorpej __KERNEL_RCSID(0, "$NetBSD: ofw_pci_subr.c,v 1.3 2022/01/22 11:49:18 thorpej Exp $");
344c5901a8Sthorpej 
354c5901a8Sthorpej #include <sys/types.h>
364c5901a8Sthorpej #include <sys/device.h>
374c5901a8Sthorpej #include <sys/endian.h>
384c5901a8Sthorpej #include <sys/errno.h>
394c5901a8Sthorpej 
404c5901a8Sthorpej #include <dev/pci/pcivar.h>
414ecce2b2Sthorpej #include <dev/pci/pci_calls.h>
424c5901a8Sthorpej 
434c5901a8Sthorpej #include <dev/ofw/openfirm.h>
444c5901a8Sthorpej #include <dev/ofw/ofw_pci.h>
454c5901a8Sthorpej 
464c5901a8Sthorpej static int
ofw_pci_bus_get_child_devhandle(device_t dev,devhandle_t call_handle,void * v)474c5901a8Sthorpej ofw_pci_bus_get_child_devhandle(device_t dev, devhandle_t call_handle, void *v)
484c5901a8Sthorpej {
494c5901a8Sthorpej 	struct pci_bus_get_child_devhandle_args *args = v;
504c5901a8Sthorpej 	int phandle = devhandle_to_of(call_handle);
514c5901a8Sthorpej 	struct ofw_pci_register opr;
524c5901a8Sthorpej 	int d, f, len;
534c5901a8Sthorpej 	uint32_t phys_hi;
544c5901a8Sthorpej 
554c5901a8Sthorpej 	/*
564c5901a8Sthorpej 	 * No need to compare the bus number; we are searching
574c5901a8Sthorpej 	 * only direct children of the specified node.  Skipping
584c5901a8Sthorpej 	 * the bus number comparison allows us to dodge a slight
594c5901a8Sthorpej 	 * difference between the OpenFirmware and FDT PCI bindings
604c5901a8Sthorpej 	 * as it relates to PCI-PCI bridges.
614c5901a8Sthorpej 	 */
624c5901a8Sthorpej 
634c5901a8Sthorpej 	pci_decompose_tag(args->pc, args->tag, NULL, &d, &f);
644c5901a8Sthorpej 
654c5901a8Sthorpej 	for (phandle = OF_child(phandle); phandle != 0;
664c5901a8Sthorpej 	     phandle = OF_peer(phandle)) {
674c5901a8Sthorpej 		len = OF_getprop(phandle, "reg", &opr, sizeof(opr));
684c5901a8Sthorpej 		if (len < sizeof(opr)) {
694c5901a8Sthorpej 			continue;
704c5901a8Sthorpej 		}
714c5901a8Sthorpej 
724c5901a8Sthorpej 		phys_hi = be32toh(opr.phys_hi);
734c5901a8Sthorpej 
744c5901a8Sthorpej 		if (d != OFW_PCI_PHYS_HI_DEVICE(phys_hi) ||
754c5901a8Sthorpej 		    f != OFW_PCI_PHYS_HI_FUNCTION(phys_hi)) {
764c5901a8Sthorpej 			continue;
774c5901a8Sthorpej 		}
784c5901a8Sthorpej 
794c5901a8Sthorpej 		/* Found it! */
80*3944ff70Sthorpej 		args->devhandle = devhandle_from_of(call_handle, phandle);
814c5901a8Sthorpej 		return 0;
824c5901a8Sthorpej 	}
834c5901a8Sthorpej 
844c5901a8Sthorpej 	return ENODEV;
854c5901a8Sthorpej }
864ecce2b2Sthorpej OF_DEVICE_CALL_REGISTER(PCI_BUS_GET_CHILD_DEVHANDLE_STR,
874c5901a8Sthorpej 			ofw_pci_bus_get_child_devhandle)
88