xref: /freebsd/sys/powerpc/pseries/vdevice.c (revision 069ac184)
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/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/malloc.h>
34 #include <sys/bus.h>
35 #include <sys/cpu.h>
36 #include <machine/bus.h>
37 #include <machine/intr_machdep.h>
38 
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <powerpc/pseries/plpar_iommu.h>
44 
45 #include "iommu_if.h"
46 
47 static int	vdevice_probe(device_t);
48 static int	vdevice_attach(device_t);
49 static const struct ofw_bus_devinfo *vdevice_get_devinfo(device_t dev,
50     device_t child);
51 static int	vdevice_print_child(device_t dev, device_t child);
52 static struct resource_list *vdevice_get_resource_list(device_t, device_t);
53 static bus_dma_tag_t vdevice_get_dma_tag(device_t dev, device_t child);
54 
55 /*
56  * VDevice devinfo
57  */
58 struct vdevice_devinfo {
59 	struct ofw_bus_devinfo mdi_obdinfo;
60 	struct resource_list mdi_resources;
61 	bus_dma_tag_t mdi_dma_tag;
62 };
63 
64 static device_method_t vdevice_methods[] = {
65 	/* Device interface */
66 	DEVMETHOD(device_probe,		vdevice_probe),
67 	DEVMETHOD(device_attach,	vdevice_attach),
68 
69 	/* Bus interface */
70 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
71 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
72 	DEVMETHOD(bus_print_child,	vdevice_print_child),
73 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
74 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
75 	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
76 	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
77 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
78 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
79 	DEVMETHOD(bus_get_resource_list, vdevice_get_resource_list),
80 
81 	/* ofw_bus interface */
82 	DEVMETHOD(ofw_bus_get_devinfo,	vdevice_get_devinfo),
83 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
84 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
85 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
86 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
87 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
88 
89 	/* IOMMU interface */
90 	DEVMETHOD(bus_get_dma_tag,	vdevice_get_dma_tag),
91 	DEVMETHOD(iommu_map,		phyp_iommu_map),
92 	DEVMETHOD(iommu_unmap,		phyp_iommu_unmap),
93 
94 	DEVMETHOD_END
95 };
96 
97 static driver_t vdevice_driver = {
98 	"vdevice",
99 	vdevice_methods,
100 	0
101 };
102 
103 DRIVER_MODULE(vdevice, ofwbus, vdevice_driver, 0, 0);
104 
105 static int
106 vdevice_probe(device_t dev)
107 {
108 	const char	*name;
109 
110 	name = ofw_bus_get_name(dev);
111 
112 	if (name == NULL || strcmp(name, "vdevice") != 0)
113 		return (ENXIO);
114 
115 	if (!ofw_bus_is_compatible(dev, "IBM,vdevice"))
116 		return (ENXIO);
117 
118 	device_set_desc(dev, "POWER Hypervisor Virtual Device Root");
119 
120 	return (0);
121 }
122 
123 static int
124 vdevice_attach(device_t dev)
125 {
126 	phandle_t root, child;
127 	device_t cdev;
128 	struct vdevice_devinfo *dinfo;
129 
130 	root = ofw_bus_get_node(dev);
131 
132 	/* The XICP (root PIC) will handle all our interrupts */
133 	powerpc_register_pic(root_pic, OF_xref_from_node(root),
134 	    1 << 24 /* 24-bit XIRR field */, 1 /* Number of IPIs */, FALSE);
135 
136 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
137 		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
138 
139                 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo,
140 		    child) != 0) {
141                         free(dinfo, M_DEVBUF);
142                         continue;
143                 }
144 		resource_list_init(&dinfo->mdi_resources);
145 
146 		ofw_bus_intr_to_rl(dev, child, &dinfo->mdi_resources, NULL);
147 
148                 cdev = device_add_child(dev, NULL, -1);
149                 if (cdev == NULL) {
150                         device_printf(dev, "<%s>: device_add_child failed\n",
151                             dinfo->mdi_obdinfo.obd_name);
152                         ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
153                         free(dinfo, M_DEVBUF);
154                         continue;
155                 }
156 		device_set_ivars(cdev, dinfo);
157 	}
158 
159 	return (bus_generic_attach(dev));
160 }
161 
162 static const struct ofw_bus_devinfo *
163 vdevice_get_devinfo(device_t dev, device_t child)
164 {
165 	return (device_get_ivars(child));
166 }
167 
168 static int
169 vdevice_print_child(device_t dev, device_t child)
170 {
171 	struct vdevice_devinfo *dinfo;
172 	struct resource_list *rl;
173 	int retval = 0;
174 
175 	dinfo = device_get_ivars(child);
176 	rl = &dinfo->mdi_resources;
177 
178 	retval += bus_print_child_header(dev, child);
179 
180 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
181 
182 	retval += bus_print_child_footer(dev, child);
183 
184 	return (retval);
185 }
186 
187 static struct resource_list *
188 vdevice_get_resource_list (device_t dev, device_t child)
189 {
190         struct vdevice_devinfo *dinfo;
191 
192         dinfo = device_get_ivars(child);
193         return (&dinfo->mdi_resources);
194 }
195 
196 static bus_dma_tag_t
197 vdevice_get_dma_tag(device_t dev, device_t child)
198 {
199 	struct vdevice_devinfo *dinfo;
200 	while (child != NULL && device_get_parent(child) != dev)
201 		child = device_get_parent(child);
202         dinfo = device_get_ivars(child);
203 
204 	if (dinfo->mdi_dma_tag == NULL) {
205 		bus_dma_tag_create(bus_get_dma_tag(dev),
206 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
207 		    NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
208 		    BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->mdi_dma_tag);
209 		phyp_iommu_set_dma_tag(dev, child, dinfo->mdi_dma_tag);
210 	}
211 
212         return (dinfo->mdi_dma_tag);
213 }
214