xref: /freebsd/sys/dev/cfe/cfe_resource.c (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Bruce M. Simpson.
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 /*
30  * Driver to swallow up memory ranges reserved by CFE platform firmware.
31  * CFE on Sentry5 doesn't specify reserved ranges, so this is not useful
32  * at the present time.
33  * TODO: Don't attach this off nexus.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 
44 #include <sys/module.h>
45 #include <sys/bus.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/rman.h>
50 
51 #include <dev/cfe/cfe_api.h>
52 #include <dev/cfe/cfe_error.h>
53 
54 #define MAX_CFE_RESERVATIONS 16
55 
56 struct cferes_softc {
57 	int		 rnum;
58 	int		 rid[MAX_CFE_RESERVATIONS];
59 	struct resource	*res[MAX_CFE_RESERVATIONS];
60 };
61 
62 static int
63 cferes_probe(device_t dev)
64 {
65 
66 	return (BUS_PROBE_NOWILDCARD);
67 }
68 
69 static int
70 cferes_attach(device_t dev)
71 {
72 
73 	return (0);
74 }
75 
76 static void
77 cferes_identify(driver_t* driver, device_t parent)
78 {
79 	device_t		 child;
80 	int			 i;
81 	struct resource		*res;
82 	int			 result;
83 	int			 rid;
84 	struct cferes_softc	*sc;
85 	uint64_t		 addr, len, type;
86 
87 	child = BUS_ADD_CHILD(parent, 100, "cferes", -1);
88 	device_set_driver(child, driver);
89 	sc = device_get_softc(child);
90 
91 	sc->rnum = 0;
92 	for (i = 0; i < ~0U; i++) {
93 		result = cfe_enummem(i, CFE_FLG_FULL_ARENA, &addr, &len, &type);
94 		if (result < 0)
95 			break;
96 		if (type != CFE_MI_RESERVED) {
97 			if (bootverbose)
98 			printf("%s: skipping non reserved range 0x%0jx(%jd)\n",
99 			    device_getnameunit(child),
100 			    (uintmax_t)addr, (uintmax_t)len);
101 			continue;
102 		}
103 
104 		bus_set_resource(child, SYS_RES_MEMORY, sc->rnum, addr, len);
105 		rid = sc->rnum;
106 		res = bus_alloc_resource_any(child, SYS_RES_MEMORY, &rid, 0);
107 		if (res == NULL) {
108 			bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
109 			continue;
110 		}
111 		sc->rid[sc->rnum] = rid;
112 		sc->res[sc->rnum] = res;
113 
114 		sc->rnum++;
115 		if (sc->rnum == MAX_CFE_RESERVATIONS)
116 			break;
117 	}
118 
119 	if (sc->rnum == 0) {
120 		device_delete_child(parent, child);
121 		return;
122 	}
123 
124 	device_set_desc(child, "CFE reserved memory");
125 }
126 
127 static int
128 cferes_detach(device_t dev)
129 {
130 	int			i;
131 	struct cferes_softc	*sc = device_get_softc(dev);
132 
133 	for (i = 0; i < sc->rnum; i++) {
134 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid[i],
135 		    sc->res[i]);
136 	}
137 
138 	return (0);
139 }
140 
141 static device_method_t cferes_methods[] = {
142 	/* Device interface */
143 	DEVMETHOD(device_identify,	cferes_identify),
144 	DEVMETHOD(device_probe,		cferes_probe),
145 	DEVMETHOD(device_attach,	cferes_attach),
146 	DEVMETHOD(device_detach,	cferes_detach),
147 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
148 	DEVMETHOD(device_suspend,	bus_generic_suspend),
149 	DEVMETHOD(device_resume,	bus_generic_resume),
150 	{ 0, 0 }
151 };
152 
153 static driver_t cferes_driver = {
154 	"cferes",
155 	cferes_methods,
156 	sizeof (struct cferes_softc)
157 };
158 
159 static devclass_t cferes_devclass;
160 
161 DRIVER_MODULE(cfe, nexus, cferes_driver, cferes_devclass, 0, 0);
162