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