1817ba5c0SNathan Whitehorn /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni *
4817ba5c0SNathan Whitehorn * Copyright (c) 2011 Nathan Whitehorn
5817ba5c0SNathan Whitehorn * All rights reserved.
6817ba5c0SNathan Whitehorn *
7817ba5c0SNathan Whitehorn * Redistribution and use in source and binary forms, with or without
8817ba5c0SNathan Whitehorn * modification, are permitted provided that the following conditions
9817ba5c0SNathan Whitehorn * are met:
10817ba5c0SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
11817ba5c0SNathan Whitehorn * notice, this list of conditions and the following disclaimer.
12817ba5c0SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
13817ba5c0SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the
14817ba5c0SNathan Whitehorn * documentation and/or other materials provided with the distribution.
15817ba5c0SNathan Whitehorn *
16817ba5c0SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17817ba5c0SNathan Whitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18817ba5c0SNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19817ba5c0SNathan Whitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20817ba5c0SNathan Whitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21817ba5c0SNathan Whitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22817ba5c0SNathan Whitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23817ba5c0SNathan Whitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24817ba5c0SNathan Whitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25817ba5c0SNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26817ba5c0SNathan Whitehorn * SUCH DAMAGE.
27817ba5c0SNathan Whitehorn */
28817ba5c0SNathan Whitehorn
29817ba5c0SNathan Whitehorn #include <sys/param.h>
30817ba5c0SNathan Whitehorn #include <sys/bus.h>
31817ba5c0SNathan Whitehorn #include <sys/kernel.h>
32817ba5c0SNathan Whitehorn #include <sys/libkern.h>
33817ba5c0SNathan Whitehorn #include <sys/module.h>
34817ba5c0SNathan Whitehorn #include <sys/pciio.h>
35817ba5c0SNathan Whitehorn
36817ba5c0SNathan Whitehorn #include <dev/ofw/openfirm.h>
37817ba5c0SNathan Whitehorn
38817ba5c0SNathan Whitehorn #include <dev/pci/pcivar.h>
39817ba5c0SNathan Whitehorn #include <dev/pci/pcireg.h>
40817ba5c0SNathan Whitehorn #include <dev/pci/pci_private.h>
41817ba5c0SNathan Whitehorn
42817ba5c0SNathan Whitehorn #include <machine/bus.h>
43817ba5c0SNathan Whitehorn #include <machine/rtas.h>
44817ba5c0SNathan Whitehorn
45817ba5c0SNathan Whitehorn #include <powerpc/ofw/ofw_pcibus.h>
46817ba5c0SNathan Whitehorn #include <powerpc/pseries/plpar_iommu.h>
47817ba5c0SNathan Whitehorn
48817ba5c0SNathan Whitehorn #include "pci_if.h"
49817ba5c0SNathan Whitehorn #include "iommu_if.h"
50817ba5c0SNathan Whitehorn
51817ba5c0SNathan Whitehorn static int plpar_pcibus_probe(device_t);
52817ba5c0SNathan Whitehorn static bus_dma_tag_t plpar_pcibus_get_dma_tag(device_t dev, device_t child);
53817ba5c0SNathan Whitehorn
54817ba5c0SNathan Whitehorn /*
55817ba5c0SNathan Whitehorn * Driver methods.
56817ba5c0SNathan Whitehorn */
57817ba5c0SNathan Whitehorn static device_method_t plpar_pcibus_methods[] = {
58817ba5c0SNathan Whitehorn /* Device interface */
59817ba5c0SNathan Whitehorn DEVMETHOD(device_probe, plpar_pcibus_probe),
60817ba5c0SNathan Whitehorn
61817ba5c0SNathan Whitehorn /* IOMMU functions */
62817ba5c0SNathan Whitehorn DEVMETHOD(bus_get_dma_tag, plpar_pcibus_get_dma_tag),
63817ba5c0SNathan Whitehorn DEVMETHOD(iommu_map, phyp_iommu_map),
64817ba5c0SNathan Whitehorn DEVMETHOD(iommu_unmap, phyp_iommu_unmap),
65817ba5c0SNathan Whitehorn
66817ba5c0SNathan Whitehorn DEVMETHOD_END
67817ba5c0SNathan Whitehorn };
68817ba5c0SNathan Whitehorn
69817ba5c0SNathan Whitehorn DEFINE_CLASS_1(pci, plpar_pcibus_driver, plpar_pcibus_methods,
70817ba5c0SNathan Whitehorn sizeof(struct pci_softc), ofw_pcibus_driver);
719b9a5327SJohn Baldwin DRIVER_MODULE(plpar_pcibus, pcib, plpar_pcibus_driver, 0, 0);
72817ba5c0SNathan Whitehorn
73817ba5c0SNathan Whitehorn static int
plpar_pcibus_probe(device_t dev)74817ba5c0SNathan Whitehorn plpar_pcibus_probe(device_t dev)
75817ba5c0SNathan Whitehorn {
76817ba5c0SNathan Whitehorn phandle_t rtas;
77817ba5c0SNathan Whitehorn
78817ba5c0SNathan Whitehorn if (ofw_bus_get_node(dev) == -1 || !rtas_exists())
79817ba5c0SNathan Whitehorn return (ENXIO);
80817ba5c0SNathan Whitehorn
81817ba5c0SNathan Whitehorn rtas = OF_finddevice("/rtas");
82817ba5c0SNathan Whitehorn if (!OF_hasprop(rtas, "ibm,hypertas-functions"))
83817ba5c0SNathan Whitehorn return (ENXIO);
84817ba5c0SNathan Whitehorn
85817ba5c0SNathan Whitehorn device_set_desc(dev, "POWER Hypervisor PCI bus");
86817ba5c0SNathan Whitehorn
87817ba5c0SNathan Whitehorn return (BUS_PROBE_SPECIFIC);
88817ba5c0SNathan Whitehorn }
89817ba5c0SNathan Whitehorn
90817ba5c0SNathan Whitehorn static bus_dma_tag_t
plpar_pcibus_get_dma_tag(device_t dev,device_t child)91817ba5c0SNathan Whitehorn plpar_pcibus_get_dma_tag(device_t dev, device_t child)
92817ba5c0SNathan Whitehorn {
93817ba5c0SNathan Whitehorn struct ofw_pcibus_devinfo *dinfo;
94817ba5c0SNathan Whitehorn
95817ba5c0SNathan Whitehorn while (device_get_parent(child) != dev)
96817ba5c0SNathan Whitehorn child = device_get_parent(child);
97817ba5c0SNathan Whitehorn
98817ba5c0SNathan Whitehorn dinfo = device_get_ivars(child);
99817ba5c0SNathan Whitehorn
100817ba5c0SNathan Whitehorn if (dinfo->opd_dma_tag != NULL)
101817ba5c0SNathan Whitehorn return (dinfo->opd_dma_tag);
102817ba5c0SNathan Whitehorn
103817ba5c0SNathan Whitehorn bus_dma_tag_create(bus_get_dma_tag(dev),
104817ba5c0SNathan Whitehorn 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
105817ba5c0SNathan Whitehorn NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
106817ba5c0SNathan Whitehorn BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->opd_dma_tag);
107817ba5c0SNathan Whitehorn phyp_iommu_set_dma_tag(dev, child, dinfo->opd_dma_tag);
108817ba5c0SNathan Whitehorn
109817ba5c0SNathan Whitehorn return (dinfo->opd_dma_tag);
110817ba5c0SNathan Whitehorn }
111