ofw_pci_subr.c revision 1.1
11.1Sthorpej/*	$NetBSD: ofw_pci_subr.c,v 1.1 2021/05/12 23:22:33 thorpej Exp $	*/
21.1Sthorpej
31.1Sthorpej/*-
41.1Sthorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * This code is derived from software contributed to The NetBSD Foundation
81.1Sthorpej * by Jason R. Thorpe.
91.1Sthorpej *
101.1Sthorpej * Redistribution and use in source and binary forms, with or without
111.1Sthorpej * modification, are permitted provided that the following conditions
121.1Sthorpej * are met:
131.1Sthorpej * 1. Redistributions of source code must retain the above copyright
141.1Sthorpej *    notice, this list of conditions and the following disclaimer.
151.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
161.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
171.1Sthorpej *    documentation and/or other materials provided with the distribution.
181.1Sthorpej *
191.1Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sthorpej * POSSIBILITY OF SUCH DAMAGE.
301.1Sthorpej */
311.1Sthorpej
321.1Sthorpej#include <sys/cdefs.h>
331.1Sthorpej__KERNEL_RCSID(0, "$NetBSD: ofw_pci_subr.c,v 1.1 2021/05/12 23:22:33 thorpej Exp $");
341.1Sthorpej
351.1Sthorpej#include <sys/types.h>
361.1Sthorpej#include <sys/device.h>
371.1Sthorpej#include <sys/endian.h>
381.1Sthorpej#include <sys/errno.h>
391.1Sthorpej
401.1Sthorpej#include <dev/pci/pcivar.h>
411.1Sthorpej
421.1Sthorpej#include <dev/ofw/openfirm.h>
431.1Sthorpej#include <dev/ofw/ofw_pci.h>
441.1Sthorpej
451.1Sthorpejstatic int
461.1Sthorpejofw_pci_bus_get_child_devhandle(device_t dev, devhandle_t call_handle, void *v)
471.1Sthorpej{
481.1Sthorpej	struct pci_bus_get_child_devhandle_args *args = v;
491.1Sthorpej	int phandle = devhandle_to_of(call_handle);
501.1Sthorpej	struct ofw_pci_register opr;
511.1Sthorpej	int d, f, len;
521.1Sthorpej	uint32_t phys_hi;
531.1Sthorpej
541.1Sthorpej	/*
551.1Sthorpej	 * No need to compare the bus number; we are searching
561.1Sthorpej	 * only direct children of the specified node.  Skipping
571.1Sthorpej	 * the bus number comparison allows us to dodge a slight
581.1Sthorpej	 * difference between the OpenFirmware and FDT PCI bindings
591.1Sthorpej	 * as it relates to PCI-PCI bridges.
601.1Sthorpej	 */
611.1Sthorpej
621.1Sthorpej	pci_decompose_tag(args->pc, args->tag, NULL, &d, &f);
631.1Sthorpej
641.1Sthorpej	for (phandle = OF_child(phandle); phandle != 0;
651.1Sthorpej	     phandle = OF_peer(phandle)) {
661.1Sthorpej		len = OF_getprop(phandle, "reg", &opr, sizeof(opr));
671.1Sthorpej		if (len < sizeof(opr)) {
681.1Sthorpej			continue;
691.1Sthorpej		}
701.1Sthorpej
711.1Sthorpej		phys_hi = be32toh(opr.phys_hi);
721.1Sthorpej
731.1Sthorpej		if (d != OFW_PCI_PHYS_HI_DEVICE(phys_hi) ||
741.1Sthorpej		    f != OFW_PCI_PHYS_HI_FUNCTION(phys_hi)) {
751.1Sthorpej			continue;
761.1Sthorpej		}
771.1Sthorpej
781.1Sthorpej		/* Found it! */
791.1Sthorpej		args->devhandle = devhandle_from_of(phandle);
801.1Sthorpej		return 0;
811.1Sthorpej	}
821.1Sthorpej
831.1Sthorpej	return ENODEV;
841.1Sthorpej}
851.1SthorpejOF_DEVICE_CALL_REGISTER("pci-bus-get-child-devhandle",
861.1Sthorpej			ofw_pci_bus_get_child_devhandle)
87