xref: /freebsd/sys/powerpc/pseries/plpar_pcibus.c (revision 61e21613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/libkern.h>
33 #include <sys/module.h>
34 #include <sys/pciio.h>
35 
36 #include <dev/ofw/openfirm.h>
37 
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pci_private.h>
41 
42 #include <machine/bus.h>
43 #include <machine/rtas.h>
44 
45 #include <powerpc/ofw/ofw_pcibus.h>
46 #include <powerpc/pseries/plpar_iommu.h>
47 
48 #include "pci_if.h"
49 #include "iommu_if.h"
50 
51 static int		plpar_pcibus_probe(device_t);
52 static bus_dma_tag_t	plpar_pcibus_get_dma_tag(device_t dev, device_t child);
53 
54 /*
55  * Driver methods.
56  */
57 static device_method_t	plpar_pcibus_methods[] = {
58 	/* Device interface */
59 	DEVMETHOD(device_probe,		plpar_pcibus_probe),
60 
61 	/* IOMMU functions */
62 	DEVMETHOD(bus_get_dma_tag,	plpar_pcibus_get_dma_tag),
63 	DEVMETHOD(iommu_map,		phyp_iommu_map),
64 	DEVMETHOD(iommu_unmap,		phyp_iommu_unmap),
65 
66 	DEVMETHOD_END
67 };
68 
69 DEFINE_CLASS_1(pci, plpar_pcibus_driver, plpar_pcibus_methods,
70     sizeof(struct pci_softc), ofw_pcibus_driver);
71 DRIVER_MODULE(plpar_pcibus, pcib, plpar_pcibus_driver, 0, 0);
72 
73 static int
74 plpar_pcibus_probe(device_t dev)
75 {
76 	phandle_t rtas;
77 
78 	if (ofw_bus_get_node(dev) == -1 || !rtas_exists())
79 		return (ENXIO);
80 
81 	rtas = OF_finddevice("/rtas");
82 	if (!OF_hasprop(rtas, "ibm,hypertas-functions"))
83 		return (ENXIO);
84 
85 	device_set_desc(dev, "POWER Hypervisor PCI bus");
86 
87 	return (BUS_PROBE_SPECIFIC);
88 }
89 
90 static bus_dma_tag_t
91 plpar_pcibus_get_dma_tag(device_t dev, device_t child)
92 {
93 	struct ofw_pcibus_devinfo *dinfo;
94 
95 	while (device_get_parent(child) != dev)
96 		child = device_get_parent(child);
97 
98 	dinfo = device_get_ivars(child);
99 
100 	if (dinfo->opd_dma_tag != NULL)
101 		return (dinfo->opd_dma_tag);
102 
103 	bus_dma_tag_create(bus_get_dma_tag(dev),
104 	    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
105 	    NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
106 	    BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->opd_dma_tag);
107 	phyp_iommu_set_dma_tag(dev, child, dinfo->opd_dma_tag);
108 
109 	return (dinfo->opd_dma_tag);
110 }
111