1255643Snwhitehorn/*-
2255643Snwhitehorn * Copyright (c) 2011 Nathan Whitehorn
3255643Snwhitehorn * All rights reserved.
4255643Snwhitehorn *
5255643Snwhitehorn * Redistribution and use in source and binary forms, with or without
6255643Snwhitehorn * modification, are permitted provided that the following conditions
7255643Snwhitehorn * are met:
8255643Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9255643Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10255643Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11255643Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12255643Snwhitehorn *    documentation and/or other materials provided with the distribution.
13255643Snwhitehorn *
14255643Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15255643Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16255643Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17255643Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18255643Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19255643Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20255643Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21255643Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22255643Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23255643Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24255643Snwhitehorn * SUCH DAMAGE.
25255643Snwhitehorn */
26255643Snwhitehorn
27255643Snwhitehorn#include <sys/cdefs.h>
28255643Snwhitehorn__FBSDID("$FreeBSD$");
29255643Snwhitehorn
30255643Snwhitehorn#include <sys/param.h>
31255643Snwhitehorn#include <sys/systm.h>
32255643Snwhitehorn#include <sys/kernel.h>
33255643Snwhitehorn#include <sys/module.h>
34255643Snwhitehorn#include <sys/malloc.h>
35255643Snwhitehorn#include <sys/bus.h>
36255643Snwhitehorn#include <sys/cpu.h>
37255643Snwhitehorn#include <machine/bus.h>
38255643Snwhitehorn#include <machine/intr_machdep.h>
39255643Snwhitehorn
40255643Snwhitehorn#include <dev/ofw/openfirm.h>
41255643Snwhitehorn#include <dev/ofw/ofw_bus.h>
42255643Snwhitehorn#include <dev/ofw/ofw_bus_subr.h>
43255643Snwhitehorn
44255927Snwhitehorn#include <powerpc/pseries/plpar_iommu.h>
45255927Snwhitehorn
46255927Snwhitehorn#include "iommu_if.h"
47255927Snwhitehorn
48255643Snwhitehornstatic int	vdevice_probe(device_t);
49255643Snwhitehornstatic int	vdevice_attach(device_t);
50255643Snwhitehornstatic const struct ofw_bus_devinfo *vdevice_get_devinfo(device_t dev,
51255643Snwhitehorn    device_t child);
52255643Snwhitehornstatic int	vdevice_print_child(device_t dev, device_t child);
53255927Snwhitehornstatic struct resource_list *vdevice_get_resource_list(device_t, device_t);
54255927Snwhitehornstatic bus_dma_tag_t vdevice_get_dma_tag(device_t dev, device_t child);
55255643Snwhitehorn
56255643Snwhitehorn/*
57255643Snwhitehorn * VDevice devinfo
58255643Snwhitehorn */
59255643Snwhitehornstruct vdevice_devinfo {
60255643Snwhitehorn	struct ofw_bus_devinfo mdi_obdinfo;
61255643Snwhitehorn	struct resource_list mdi_resources;
62255927Snwhitehorn	bus_dma_tag_t mdi_dma_tag;
63255643Snwhitehorn};
64255643Snwhitehorn
65255643Snwhitehornstatic device_method_t vdevice_methods[] = {
66255643Snwhitehorn	/* Device interface */
67255643Snwhitehorn	DEVMETHOD(device_probe,		vdevice_probe),
68255643Snwhitehorn	DEVMETHOD(device_attach,	vdevice_attach),
69255643Snwhitehorn
70255643Snwhitehorn	/* Bus interface */
71255643Snwhitehorn	DEVMETHOD(bus_add_child,	bus_generic_add_child),
72255643Snwhitehorn	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
73255643Snwhitehorn	DEVMETHOD(bus_print_child,	vdevice_print_child),
74255643Snwhitehorn	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
75255643Snwhitehorn	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
76255643Snwhitehorn	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
77255643Snwhitehorn	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
78255643Snwhitehorn	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
79255643Snwhitehorn	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
80255643Snwhitehorn	DEVMETHOD(bus_get_resource_list, vdevice_get_resource_list),
81255643Snwhitehorn
82255643Snwhitehorn	/* ofw_bus interface */
83255643Snwhitehorn	DEVMETHOD(ofw_bus_get_devinfo,	vdevice_get_devinfo),
84255643Snwhitehorn	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
85255643Snwhitehorn	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
86255643Snwhitehorn	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
87255643Snwhitehorn	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
88255643Snwhitehorn	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
89255643Snwhitehorn
90255927Snwhitehorn	/* IOMMU interface */
91255927Snwhitehorn	DEVMETHOD(bus_get_dma_tag,	vdevice_get_dma_tag),
92255927Snwhitehorn	DEVMETHOD(iommu_map,		phyp_iommu_map),
93255927Snwhitehorn	DEVMETHOD(iommu_unmap,		phyp_iommu_unmap),
94255927Snwhitehorn
95255643Snwhitehorn	DEVMETHOD_END
96255643Snwhitehorn};
97255643Snwhitehorn
98255643Snwhitehornstatic driver_t vdevice_driver = {
99255643Snwhitehorn	"vdevice",
100255643Snwhitehorn	vdevice_methods,
101255643Snwhitehorn	0
102255643Snwhitehorn};
103255643Snwhitehorn
104255643Snwhitehornstatic devclass_t vdevice_devclass;
105255643Snwhitehorn
106261513SnwhitehornDRIVER_MODULE(vdevice, ofwbus, vdevice_driver, vdevice_devclass, 0, 0);
107255643Snwhitehorn
108255643Snwhitehornstatic int
109255643Snwhitehornvdevice_probe(device_t dev)
110255643Snwhitehorn{
111255643Snwhitehorn	const char	*name;
112255643Snwhitehorn
113255643Snwhitehorn	name = ofw_bus_get_name(dev);
114255643Snwhitehorn
115255643Snwhitehorn	if (name == NULL || strcmp(name, "vdevice") != 0)
116255643Snwhitehorn		return (ENXIO);
117255643Snwhitehorn
118255643Snwhitehorn	if (!ofw_bus_is_compatible(dev, "IBM,vdevice"))
119255643Snwhitehorn		return (ENXIO);
120255643Snwhitehorn
121255643Snwhitehorn	device_set_desc(dev, "POWER Hypervisor Virtual Device Root");
122255643Snwhitehorn
123255643Snwhitehorn	return (0);
124255643Snwhitehorn}
125255643Snwhitehorn
126255643Snwhitehornstatic int
127255643Snwhitehornvdevice_attach(device_t dev)
128255643Snwhitehorn{
129255643Snwhitehorn	phandle_t root, child;
130255643Snwhitehorn	device_t cdev;
131255643Snwhitehorn	struct vdevice_devinfo *dinfo;
132255643Snwhitehorn
133255643Snwhitehorn	root = ofw_bus_get_node(dev);
134255643Snwhitehorn
135276726Snwhitehorn	/* The XICP (root PIC) will handle all our interrupts */
136276726Snwhitehorn	powerpc_register_pic(root_pic, OF_xref_from_node(root),
137276726Snwhitehorn	    1 << 24 /* 24-bit XIRR field */, 1 /* Number of IPIs */, FALSE);
138276726Snwhitehorn
139255643Snwhitehorn	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
140255643Snwhitehorn		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
141255643Snwhitehorn
142255643Snwhitehorn                if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo,
143255643Snwhitehorn		    child) != 0) {
144255643Snwhitehorn                        free(dinfo, M_DEVBUF);
145255643Snwhitehorn                        continue;
146255643Snwhitehorn                }
147255643Snwhitehorn		resource_list_init(&dinfo->mdi_resources);
148255643Snwhitehorn
149282972Sbr		ofw_bus_intr_to_rl(dev, child, &dinfo->mdi_resources, NULL);
150255643Snwhitehorn
151255643Snwhitehorn                cdev = device_add_child(dev, NULL, -1);
152255643Snwhitehorn                if (cdev == NULL) {
153255643Snwhitehorn                        device_printf(dev, "<%s>: device_add_child failed\n",
154255643Snwhitehorn                            dinfo->mdi_obdinfo.obd_name);
155255643Snwhitehorn                        ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
156255643Snwhitehorn                        free(dinfo, M_DEVBUF);
157255643Snwhitehorn                        continue;
158255643Snwhitehorn                }
159255643Snwhitehorn		device_set_ivars(cdev, dinfo);
160255643Snwhitehorn	}
161255643Snwhitehorn
162255643Snwhitehorn	return (bus_generic_attach(dev));
163255643Snwhitehorn}
164255643Snwhitehorn
165255643Snwhitehornstatic const struct ofw_bus_devinfo *
166255643Snwhitehornvdevice_get_devinfo(device_t dev, device_t child)
167255643Snwhitehorn{
168255643Snwhitehorn	return (device_get_ivars(child));
169255643Snwhitehorn}
170255643Snwhitehorn
171255643Snwhitehornstatic int
172255643Snwhitehornvdevice_print_child(device_t dev, device_t child)
173255643Snwhitehorn{
174255643Snwhitehorn	struct vdevice_devinfo *dinfo;
175255643Snwhitehorn	struct resource_list *rl;
176255643Snwhitehorn	int retval = 0;
177255643Snwhitehorn
178255643Snwhitehorn	dinfo = device_get_ivars(child);
179255643Snwhitehorn	rl = &dinfo->mdi_resources;
180255643Snwhitehorn
181255643Snwhitehorn	retval += bus_print_child_header(dev, child);
182255643Snwhitehorn
183297199Sjhibbits	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
184255643Snwhitehorn
185255643Snwhitehorn	retval += bus_print_child_footer(dev, child);
186255643Snwhitehorn
187255643Snwhitehorn	return (retval);
188255643Snwhitehorn}
189255643Snwhitehorn
190255643Snwhitehornstatic struct resource_list *
191255643Snwhitehornvdevice_get_resource_list (device_t dev, device_t child)
192255643Snwhitehorn{
193255643Snwhitehorn        struct vdevice_devinfo *dinfo;
194255643Snwhitehorn
195255643Snwhitehorn        dinfo = device_get_ivars(child);
196255643Snwhitehorn        return (&dinfo->mdi_resources);
197255643Snwhitehorn}
198255643Snwhitehorn
199255927Snwhitehornstatic bus_dma_tag_t
200255927Snwhitehornvdevice_get_dma_tag(device_t dev, device_t child)
201255927Snwhitehorn{
202255927Snwhitehorn	struct vdevice_devinfo *dinfo;
203255927Snwhitehorn	while (child != NULL && device_get_parent(child) != dev)
204255927Snwhitehorn		child = device_get_parent(child);
205255927Snwhitehorn        dinfo = device_get_ivars(child);
206255927Snwhitehorn
207255927Snwhitehorn	if (dinfo->mdi_dma_tag == NULL) {
208255927Snwhitehorn		bus_dma_tag_create(bus_get_dma_tag(dev),
209255927Snwhitehorn		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
210255927Snwhitehorn		    NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
211255927Snwhitehorn		    BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->mdi_dma_tag);
212255927Snwhitehorn		phyp_iommu_set_dma_tag(dev, child, dinfo->mdi_dma_tag);
213255927Snwhitehorn	}
214255927Snwhitehorn
215255927Snwhitehorn        return (dinfo->mdi_dma_tag);
216255927Snwhitehorn}
217255927Snwhitehorn
218