1 /*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 /*
32 * This code implements a `root nexus' for Arm Architecture
33 * machines. The function of the root nexus is to serve as an
34 * attachment point for both processors and buses, and to manage
35 * resources which are common to all of them. In particular,
36 * this code implements the core resource managers for interrupt
37 * requests and I/O memory address space.
38 */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/kobj.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include "device_if.h"
47 #include "bus_if.h"
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #ifdef LOSCFG_DRIVERS_HDF_USB
51 #include "hdf_usb.h"
52 #endif
53
54 struct devclass_res {
55 TAILQ_ENTRY(devclass_res) link;
56 struct resource_list nx_resources;
57 char* devclass_name;
58 };
59 TAILQ_HEAD(devclass_res_list, devclass_res);
60 static struct devclass_res_list devclass_resources;
61
62 static int nexus_probe(device_t);
63 static int nexus_attach(device_t);
64 static int nexus_print_child(device_t, device_t);
65 static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
66 rman_res_t, rman_res_t, rman_res_t, u_int);
67
68 static device_method_t nexus_methods[] = {
69 /* Device interface */
70 DEVMETHOD(device_probe, nexus_probe),
71 DEVMETHOD(device_attach, nexus_attach),
72 /* Bus interface */
73 DEVMETHOD(bus_print_child, nexus_print_child),
74 DEVMETHOD(bus_add_child, bus_generic_add_child),
75 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
76
77 { 0, 0 }
78 };
79
80 static devclass_t nexus_devclass;
81 static driver_t nexus_driver = {
82 "nexus",
83 nexus_methods,
84 1 /* no softc */
85 };
86 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
87 BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
88
get_resource_list(const char * devclass_name)89 static struct resource_list *get_resource_list(const char *devclass_name)
90 {
91 struct devclass_res *dr = NULL;
92 if (devclass_name == NULL) {
93 return (NULL);
94 }
95 TAILQ_FOREACH(dr, &devclass_resources, link) {
96 if (!strcmp(dr->devclass_name, devclass_name)) {
97 break;
98 }
99 }
100 if (dr == NULL) {
101 dr = malloc(sizeof(struct devclass_res) + strlen(devclass_name) + 1);
102 if (dr == NULL) {
103 return (NULL);
104 }
105 dr->devclass_name = (char *)(dr + 1);
106 (void)strcpy_s(dr->devclass_name, strlen(devclass_name) + 1, devclass_name);
107 resource_list_init(&dr->nx_resources);
108 TAILQ_INSERT_TAIL(&devclass_resources, dr, link);
109 }
110 return (&dr->nx_resources);
111 }
112
add_resource(const char * devclass_name,int type,int unit,rman_res_t start,rman_res_t end,rman_res_t count)113 static void add_resource(const char *devclass_name, int type, int unit, rman_res_t start, rman_res_t end,
114 rman_res_t count)
115 {
116 struct resource_list *res_list = get_resource_list(devclass_name);
117 if (res_list == NULL) {
118 return;
119 }
120 resource_list_add(res_list, type, unit, start, end, count);
121 }
122
123 int
nexus_init(void)124 nexus_init(void)
125 {
126 TAILQ_INIT(&devclass_resources);
127 #ifdef LOSCFG_DRIVERS_HDF_USB
128 UsbResourceInit(nexus, add_resource);
129 #endif
130 machine_resource_init(add_resource);
131
132 return driver_module_handler(NULL, MOD_LOAD, &nexus_root_driver_mod);
133 }
134
135 static int
nexus_probe(device_t dev)136 nexus_probe(device_t dev)
137 {
138 device_quiet(dev); /* suppress attach message for neatness */
139
140 return (BUS_PROBE_DEFAULT);
141 }
142
143 static int
nexus_attach(device_t dev)144 nexus_attach(device_t dev)
145 {
146 /*
147 * First, deal with the children we know about already
148 */
149 bus_generic_probe(dev);
150 bus_generic_attach(dev);
151
152 return (0);
153 }
154
155 static int
nexus_print_child(device_t bus,device_t child)156 nexus_print_child(device_t bus, device_t child)
157 {
158 int retval = 0;
159
160 retval += bus_print_child_header(bus, child);
161 retval += printf("\n");
162
163 return (retval);
164 }
165
166 /*
167 * Allocate a resource on behalf of child. NB: child is usually going to be a
168 * child of one of our descendants, not a direct child of nexus0.
169 * (Exceptions include footbridge.)
170 */
171 static struct resource *
nexus_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)172 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
173 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
174 {
175 devclass_t dc = device_get_devclass(child);
176 struct resource_list *res_list = NULL;
177 if (dc == NULL || rid == NULL) {
178 return (NULL);
179 }
180 res_list = get_resource_list(devclass_get_name(dc));
181 if (res_list == NULL) {
182 return (NULL);
183 }
184 struct resource_list_entry *rle = resource_list_find(res_list, type, *rid);
185 if (rle == NULL) {
186 return(NULL);
187 }
188 return (rle->res);
189 }
190