[7aa94304] | 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 | * @defgroup sun4v platform driver.
|
---|
| 31 | * @brief Sun4v platform driver.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[19d2ce01] | 38 | #include <as.h>
|
---|
[7aa94304] | 39 | #include <assert.h>
|
---|
| 40 | #include <ddf/driver.h>
|
---|
| 41 | #include <ddf/log.h>
|
---|
[19d2ce01] | 42 | #include <errno.h>
|
---|
[c1694b6b] | 43 | #include <str_error.h>
|
---|
[19d2ce01] | 44 | #include <ops/hw_res.h>
|
---|
| 45 | #include <ops/pio_window.h>
|
---|
| 46 | #include <stdio.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include <sysinfo.h>
|
---|
[7aa94304] | 49 |
|
---|
| 50 | #define NAME "sun4v"
|
---|
| 51 |
|
---|
[19d2ce01] | 52 | typedef struct sun4v_fun {
|
---|
| 53 | hw_resource_list_t hw_resources;
|
---|
| 54 | pio_window_t pio_window;
|
---|
| 55 | } sun4v_fun_t;
|
---|
| 56 |
|
---|
[7aa94304] | 57 | static int sun4v_dev_add(ddf_dev_t *dev);
|
---|
| 58 |
|
---|
| 59 | static driver_ops_t sun4v_ops = {
|
---|
| 60 | .dev_add = &sun4v_dev_add
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | static driver_t sun4v_driver = {
|
---|
| 64 | .name = NAME,
|
---|
| 65 | .driver_ops = &sun4v_ops
|
---|
| 66 | };
|
---|
| 67 |
|
---|
[19d2ce01] | 68 | static hw_resource_t console_res[] = {
|
---|
| 69 | {
|
---|
| 70 | .type = MEM_RANGE,
|
---|
| 71 | .res.mem_range = {
|
---|
| 72 | .address = 0,
|
---|
| 73 | .size = PAGE_SIZE,
|
---|
| 74 | .relative = true,
|
---|
| 75 | .endianness = LITTLE_ENDIAN
|
---|
| 76 | }
|
---|
| 77 | },
|
---|
[f4cfd271] | 78 | {
|
---|
| 79 | .type = MEM_RANGE,
|
---|
| 80 | .res.mem_range = {
|
---|
| 81 | .address = 0,
|
---|
| 82 | .size = PAGE_SIZE,
|
---|
| 83 | .relative = true,
|
---|
| 84 | .endianness = LITTLE_ENDIAN
|
---|
| 85 | }
|
---|
| 86 | },
|
---|
[19d2ce01] | 87 | };
|
---|
| 88 |
|
---|
| 89 | static sun4v_fun_t console_data = {
|
---|
| 90 | .hw_resources = {
|
---|
| 91 | sizeof(console_res) / sizeof(console_res[0]),
|
---|
| 92 | console_res
|
---|
| 93 | },
|
---|
| 94 | .pio_window = {
|
---|
| 95 | .mem = {
|
---|
| 96 | .base = 0,
|
---|
| 97 | .size = PAGE_SIZE
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | /** Obtain function soft-state from DDF function node */
|
---|
| 103 | static sun4v_fun_t *sun4v_fun(ddf_fun_t *fnode)
|
---|
| 104 | {
|
---|
| 105 | return ddf_fun_data_get(fnode);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static hw_resource_list_t *sun4v_get_resources(ddf_fun_t *fnode)
|
---|
| 109 | {
|
---|
| 110 | sun4v_fun_t *fun = sun4v_fun(fnode);
|
---|
| 111 |
|
---|
| 112 | assert(fun != NULL);
|
---|
| 113 | return &fun->hw_resources;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | static int sun4v_enable_interrupt(ddf_fun_t *fun, int irq)
|
---|
| 117 | {
|
---|
| 118 | return true;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | static pio_window_t *sun4v_get_pio_window(ddf_fun_t *fnode)
|
---|
| 122 | {
|
---|
| 123 | sun4v_fun_t *fun = sun4v_fun(fnode);
|
---|
| 124 |
|
---|
| 125 | assert(fun != NULL);
|
---|
| 126 | return &fun->pio_window;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | static hw_res_ops_t fun_hw_res_ops = {
|
---|
| 130 | .get_resource_list = &sun4v_get_resources,
|
---|
| 131 | .enable_interrupt = &sun4v_enable_interrupt,
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 | static pio_window_ops_t fun_pio_window_ops = {
|
---|
| 135 | .get_pio_window = &sun4v_get_pio_window
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | static ddf_dev_ops_t sun4v_fun_ops;
|
---|
| 139 |
|
---|
| 140 | static int sun4v_add_fun(ddf_dev_t *dev, const char *name,
|
---|
| 141 | const char *str_match_id, sun4v_fun_t *fun_proto)
|
---|
[7aa94304] | 142 | {
|
---|
| 143 | ddf_msg(LVL_NOTE, "Adding function '%s'.", name);
|
---|
| 144 |
|
---|
| 145 | ddf_fun_t *fnode = NULL;
|
---|
| 146 | int rc;
|
---|
| 147 |
|
---|
| 148 | /* Create new device. */
|
---|
| 149 | fnode = ddf_fun_create(dev, fun_inner, name);
|
---|
| 150 | if (fnode == NULL) {
|
---|
| 151 | ddf_msg(LVL_ERROR, "Error creating function '%s'", name);
|
---|
| 152 | rc = ENOMEM;
|
---|
| 153 | goto error;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[19d2ce01] | 156 | sun4v_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(sun4v_fun_t));
|
---|
| 157 | if (fun == NULL) {
|
---|
| 158 | rc = ENOMEM;
|
---|
| 159 | goto error;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | *fun = *fun_proto;
|
---|
| 163 |
|
---|
[7aa94304] | 164 | /* Add match ID */
|
---|
| 165 | rc = ddf_fun_add_match_id(fnode, str_match_id, 100);
|
---|
| 166 | if (rc != EOK) {
|
---|
| 167 | ddf_msg(LVL_ERROR, "Error adding match ID");
|
---|
| 168 | goto error;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[19d2ce01] | 171 | /* Set provided operations to the device. */
|
---|
| 172 | ddf_fun_set_ops(fnode, &sun4v_fun_ops);
|
---|
| 173 |
|
---|
[7aa94304] | 174 | /* Register function. */
|
---|
[19d2ce01] | 175 | rc = ddf_fun_bind(fnode);
|
---|
| 176 | if (rc != EOK) {
|
---|
[7aa94304] | 177 | ddf_msg(LVL_ERROR, "Failed binding function %s.", name);
|
---|
| 178 | goto error;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | return EOK;
|
---|
| 182 |
|
---|
| 183 | error:
|
---|
| 184 | if (fnode != NULL)
|
---|
| 185 | ddf_fun_destroy(fnode);
|
---|
| 186 |
|
---|
| 187 | return rc;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | static int sun4v_add_functions(ddf_dev_t *dev)
|
---|
| 191 | {
|
---|
| 192 | int rc;
|
---|
| 193 |
|
---|
[19d2ce01] | 194 | rc = sun4v_add_fun(dev, "console", "sun4v/console", &console_data);
|
---|
[7aa94304] | 195 | if (rc != EOK)
|
---|
| 196 | return rc;
|
---|
| 197 |
|
---|
| 198 | return EOK;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Add device. */
|
---|
| 202 | static int sun4v_dev_add(ddf_dev_t *dev)
|
---|
| 203 | {
|
---|
[19d2ce01] | 204 | ddf_msg(LVL_DEBUG, "sun4v_dev_add, device handle = %d",
|
---|
[7aa94304] | 205 | (int)ddf_dev_get_handle(dev));
|
---|
| 206 |
|
---|
| 207 | /* Register functions. */
|
---|
| 208 | if (sun4v_add_functions(dev)) {
|
---|
| 209 | ddf_msg(LVL_ERROR, "Failed to add functions for sun4v platform.");
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | return EOK;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[19d2ce01] | 215 | static int sun4v_init(void)
|
---|
[7aa94304] | 216 | {
|
---|
| 217 | int rc;
|
---|
[19d2ce01] | 218 | sysarg_t paddr;
|
---|
[7aa94304] | 219 |
|
---|
[19d2ce01] | 220 | sun4v_fun_ops.interfaces[HW_RES_DEV_IFACE] = &fun_hw_res_ops;
|
---|
| 221 | sun4v_fun_ops.interfaces[PIO_WINDOW_DEV_IFACE] = &fun_pio_window_ops;
|
---|
[7aa94304] | 222 |
|
---|
| 223 | rc = ddf_log_init(NAME);
|
---|
| 224 | if (rc != EOK) {
|
---|
[19d2ce01] | 225 | printf(NAME ": Failed initializing logging service\n");
|
---|
| 226 | return rc;
|
---|
[7aa94304] | 227 | }
|
---|
| 228 |
|
---|
[19d2ce01] | 229 | rc = sysinfo_get_value("niagara.inbuf.address", &paddr);
|
---|
| 230 | if (rc != EOK) {
|
---|
[c1694b6b] | 231 | ddf_msg(LVL_ERROR, "niagara.inbuf.address not set: %s", str_error(rc));
|
---|
[19d2ce01] | 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[f4cfd271] | 235 | console_res[0].res.mem_range.address = paddr;
|
---|
| 236 |
|
---|
| 237 | rc = sysinfo_get_value("niagara.outbuf.address", &paddr);
|
---|
| 238 | if (rc != EOK) {
|
---|
[c1694b6b] | 239 | ddf_msg(LVL_ERROR, "niagara.outbuf.address not set: %s", str_error(rc));
|
---|
[f4cfd271] | 240 | return rc;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | console_res[1].res.mem_range.address = paddr;
|
---|
[19d2ce01] | 244 | return EOK;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | int main(int argc, char *argv[])
|
---|
| 248 | {
|
---|
| 249 | int rc;
|
---|
| 250 |
|
---|
| 251 | printf(NAME ": Sun4v platform driver\n");
|
---|
| 252 |
|
---|
| 253 | rc = sun4v_init();
|
---|
| 254 | if (rc != EOK)
|
---|
| 255 | return 1;
|
---|
| 256 |
|
---|
[7aa94304] | 257 | return ddf_driver_main(&sun4v_driver);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
| 261 | * @}
|
---|
| 262 | */
|
---|