1 | /*
|
---|
2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @addtogroup sun4v sun4v
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <as.h>
|
---|
38 | #include <assert.h>
|
---|
39 | #include <ddf/driver.h>
|
---|
40 | #include <ddf/log.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <str_error.h>
|
---|
43 | #include <ops/hw_res.h>
|
---|
44 | #include <ops/pio_window.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <sysinfo.h>
|
---|
48 |
|
---|
49 | #define NAME "sun4v"
|
---|
50 |
|
---|
51 | typedef struct sun4v_fun {
|
---|
52 | hw_resource_list_t hw_resources;
|
---|
53 | pio_window_t pio_window;
|
---|
54 | } sun4v_fun_t;
|
---|
55 |
|
---|
56 | static errno_t sun4v_dev_add(ddf_dev_t *dev);
|
---|
57 |
|
---|
58 | static driver_ops_t sun4v_ops = {
|
---|
59 | .dev_add = &sun4v_dev_add
|
---|
60 | };
|
---|
61 |
|
---|
62 | static driver_t sun4v_driver = {
|
---|
63 | .name = NAME,
|
---|
64 | .driver_ops = &sun4v_ops
|
---|
65 | };
|
---|
66 |
|
---|
67 | static hw_resource_t console_res[] = {
|
---|
68 | {
|
---|
69 | .type = MEM_RANGE,
|
---|
70 | .res.mem_range = {
|
---|
71 | .address = 0,
|
---|
72 | .size = PAGE_SIZE,
|
---|
73 | .relative = true,
|
---|
74 | .endianness = LITTLE_ENDIAN
|
---|
75 | }
|
---|
76 | },
|
---|
77 | {
|
---|
78 | .type = MEM_RANGE,
|
---|
79 | .res.mem_range = {
|
---|
80 | .address = 0,
|
---|
81 | .size = PAGE_SIZE,
|
---|
82 | .relative = true,
|
---|
83 | .endianness = LITTLE_ENDIAN
|
---|
84 | }
|
---|
85 | },
|
---|
86 | };
|
---|
87 |
|
---|
88 | static sun4v_fun_t console_data = {
|
---|
89 | .hw_resources = {
|
---|
90 | sizeof(console_res) / sizeof(console_res[0]),
|
---|
91 | console_res
|
---|
92 | },
|
---|
93 | .pio_window = {
|
---|
94 | .mem = {
|
---|
95 | .base = 0,
|
---|
96 | .size = PAGE_SIZE
|
---|
97 | }
|
---|
98 | }
|
---|
99 | };
|
---|
100 |
|
---|
101 | /** Obtain function soft-state from DDF function node */
|
---|
102 | static sun4v_fun_t *sun4v_fun(ddf_fun_t *fnode)
|
---|
103 | {
|
---|
104 | return ddf_fun_data_get(fnode);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static hw_resource_list_t *sun4v_get_resources(ddf_fun_t *fnode)
|
---|
108 | {
|
---|
109 | sun4v_fun_t *fun = sun4v_fun(fnode);
|
---|
110 |
|
---|
111 | assert(fun != NULL);
|
---|
112 | return &fun->hw_resources;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static errno_t sun4v_enable_interrupt(ddf_fun_t *fun, int irq)
|
---|
116 | {
|
---|
117 | return EOK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static pio_window_t *sun4v_get_pio_window(ddf_fun_t *fnode)
|
---|
121 | {
|
---|
122 | sun4v_fun_t *fun = sun4v_fun(fnode);
|
---|
123 |
|
---|
124 | assert(fun != NULL);
|
---|
125 | return &fun->pio_window;
|
---|
126 | }
|
---|
127 |
|
---|
128 | static hw_res_ops_t fun_hw_res_ops = {
|
---|
129 | .get_resource_list = &sun4v_get_resources,
|
---|
130 | .enable_interrupt = &sun4v_enable_interrupt,
|
---|
131 | };
|
---|
132 |
|
---|
133 | static pio_window_ops_t fun_pio_window_ops = {
|
---|
134 | .get_pio_window = &sun4v_get_pio_window
|
---|
135 | };
|
---|
136 |
|
---|
137 | static ddf_dev_ops_t sun4v_fun_ops;
|
---|
138 |
|
---|
139 | static errno_t sun4v_add_fun(ddf_dev_t *dev, const char *name,
|
---|
140 | const char *str_match_id, sun4v_fun_t *fun_proto)
|
---|
141 | {
|
---|
142 | ddf_msg(LVL_NOTE, "Adding function '%s'.", name);
|
---|
143 |
|
---|
144 | ddf_fun_t *fnode = NULL;
|
---|
145 | errno_t rc;
|
---|
146 |
|
---|
147 | /* Create new device. */
|
---|
148 | fnode = ddf_fun_create(dev, fun_inner, name);
|
---|
149 | if (fnode == NULL) {
|
---|
150 | ddf_msg(LVL_ERROR, "Error creating function '%s'", name);
|
---|
151 | rc = ENOMEM;
|
---|
152 | goto error;
|
---|
153 | }
|
---|
154 |
|
---|
155 | sun4v_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(sun4v_fun_t));
|
---|
156 | if (fun == NULL) {
|
---|
157 | rc = ENOMEM;
|
---|
158 | goto error;
|
---|
159 | }
|
---|
160 |
|
---|
161 | *fun = *fun_proto;
|
---|
162 |
|
---|
163 | /* Add match ID */
|
---|
164 | rc = ddf_fun_add_match_id(fnode, str_match_id, 100);
|
---|
165 | if (rc != EOK) {
|
---|
166 | ddf_msg(LVL_ERROR, "Error adding match ID");
|
---|
167 | goto error;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /* Set provided operations to the device. */
|
---|
171 | ddf_fun_set_ops(fnode, &sun4v_fun_ops);
|
---|
172 |
|
---|
173 | /* Register function. */
|
---|
174 | rc = ddf_fun_bind(fnode);
|
---|
175 | if (rc != EOK) {
|
---|
176 | ddf_msg(LVL_ERROR, "Failed binding function %s.", name);
|
---|
177 | goto error;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return EOK;
|
---|
181 |
|
---|
182 | error:
|
---|
183 | if (fnode != NULL)
|
---|
184 | ddf_fun_destroy(fnode);
|
---|
185 |
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static errno_t sun4v_add_functions(ddf_dev_t *dev)
|
---|
190 | {
|
---|
191 | errno_t rc;
|
---|
192 |
|
---|
193 | rc = sun4v_add_fun(dev, "console", "sun4v/console", &console_data);
|
---|
194 | if (rc != EOK)
|
---|
195 | return rc;
|
---|
196 |
|
---|
197 | return EOK;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Add device. */
|
---|
201 | static errno_t sun4v_dev_add(ddf_dev_t *dev)
|
---|
202 | {
|
---|
203 | ddf_msg(LVL_DEBUG, "sun4v_dev_add, device handle = %d",
|
---|
204 | (int)ddf_dev_get_handle(dev));
|
---|
205 |
|
---|
206 | /* Register functions. */
|
---|
207 | if (sun4v_add_functions(dev)) {
|
---|
208 | ddf_msg(LVL_ERROR, "Failed to add functions for sun4v platform.");
|
---|
209 | }
|
---|
210 |
|
---|
211 | return EOK;
|
---|
212 | }
|
---|
213 |
|
---|
214 | static errno_t sun4v_init(void)
|
---|
215 | {
|
---|
216 | errno_t rc;
|
---|
217 | sysarg_t paddr;
|
---|
218 |
|
---|
219 | sun4v_fun_ops.interfaces[HW_RES_DEV_IFACE] = &fun_hw_res_ops;
|
---|
220 | sun4v_fun_ops.interfaces[PIO_WINDOW_DEV_IFACE] = &fun_pio_window_ops;
|
---|
221 |
|
---|
222 | rc = ddf_log_init(NAME);
|
---|
223 | if (rc != EOK) {
|
---|
224 | printf(NAME ": Failed initializing logging service\n");
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | rc = sysinfo_get_value("niagara.inbuf.address", &paddr);
|
---|
229 | if (rc != EOK) {
|
---|
230 | ddf_msg(LVL_ERROR, "niagara.inbuf.address not set: %s", str_error(rc));
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 |
|
---|
234 | console_res[0].res.mem_range.address = paddr;
|
---|
235 |
|
---|
236 | rc = sysinfo_get_value("niagara.outbuf.address", &paddr);
|
---|
237 | if (rc != EOK) {
|
---|
238 | ddf_msg(LVL_ERROR, "niagara.outbuf.address not set: %s", str_error(rc));
|
---|
239 | return rc;
|
---|
240 | }
|
---|
241 |
|
---|
242 | console_res[1].res.mem_range.address = paddr;
|
---|
243 | return EOK;
|
---|
244 | }
|
---|
245 |
|
---|
246 | int main(int argc, char *argv[])
|
---|
247 | {
|
---|
248 | errno_t rc;
|
---|
249 |
|
---|
250 | printf(NAME ": Sun4v platform driver\n");
|
---|
251 |
|
---|
252 | rc = sun4v_init();
|
---|
253 | if (rc != EOK)
|
---|
254 | return 1;
|
---|
255 |
|
---|
256 | return ddf_driver_main(&sun4v_driver);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * @}
|
---|
261 | */
|
---|