Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 |
| 4 | * Texas Instruments Incorporated - http://www.ti.com/ |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 5 | */ |
| 6 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 7 | #include <common.h> |
| 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <remoteproc.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm.h> |
| 16 | #include <dm/uclass.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | /** |
| 22 | * for_each_remoteproc_device() - iterate through the list of rproc devices |
| 23 | * @fn: check function to call per match, if this function returns fail, |
| 24 | * iteration is aborted with the resultant error value |
| 25 | * @skip_dev: Device to skip calling the callback about. |
| 26 | * @data: Data to pass to the callback function |
| 27 | * |
| 28 | * Return: 0 if none of the callback returned a non 0 result, else returns the |
| 29 | * result from the callback function |
| 30 | */ |
| 31 | static int for_each_remoteproc_device(int (*fn) (struct udevice *dev, |
| 32 | struct dm_rproc_uclass_pdata *uc_pdata, |
| 33 | const void *data), |
| 34 | struct udevice *skip_dev, |
| 35 | const void *data) |
| 36 | { |
| 37 | struct udevice *dev; |
| 38 | struct dm_rproc_uclass_pdata *uc_pdata; |
| 39 | int ret; |
| 40 | |
| 41 | for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev; |
| 42 | ret = uclass_find_next_device(&dev)) { |
| 43 | if (ret || dev == skip_dev) |
| 44 | continue; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 45 | uc_pdata = dev_get_uclass_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 46 | ret = fn(dev, uc_pdata, data); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * _rproc_name_is_unique() - iteration helper to check if rproc name is unique |
| 56 | * @dev: device that we are checking name for |
| 57 | * @uc_pdata: uclass platform data |
| 58 | * @data: compare data (this is the name we want to ensure is unique) |
| 59 | * |
| 60 | * Return: 0 is there is no match(is unique); if there is a match(we dont |
| 61 | * have a unique name), return -EINVAL. |
| 62 | */ |
| 63 | static int _rproc_name_is_unique(struct udevice *dev, |
| 64 | struct dm_rproc_uclass_pdata *uc_pdata, |
| 65 | const void *data) |
| 66 | { |
| 67 | const char *check_name = data; |
| 68 | |
| 69 | /* devices not yet populated with data - so skip them */ |
Nishanth Menon | 3ab6a4e | 2015-11-30 22:05:58 -0600 | [diff] [blame] | 70 | if (!uc_pdata->name || !check_name) |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 71 | return 0; |
| 72 | |
| 73 | /* Return 0 to search further if we dont match */ |
| 74 | if (strlen(uc_pdata->name) != strlen(check_name)) |
| 75 | return 0; |
| 76 | |
| 77 | if (!strcmp(uc_pdata->name, check_name)) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * rproc_name_is_unique() - Check if the rproc name is unique |
| 85 | * @check_dev: Device we are attempting to ensure is unique |
| 86 | * @check_name: Name we are trying to ensure is unique. |
| 87 | * |
| 88 | * Return: true if we have a unique name, false if name is not unique. |
| 89 | */ |
| 90 | static bool rproc_name_is_unique(struct udevice *check_dev, |
| 91 | const char *check_name) |
| 92 | { |
| 93 | int ret; |
| 94 | |
| 95 | ret = for_each_remoteproc_device(_rproc_name_is_unique, |
| 96 | check_dev, check_name); |
| 97 | return ret ? false : true; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * rproc_pre_probe() - Pre probe accessor for the uclass |
| 102 | * @dev: device for which we are preprobing |
| 103 | * |
| 104 | * Parses and fills up the uclass pdata for use as needed by core and |
| 105 | * remote proc drivers. |
| 106 | * |
| 107 | * Return: 0 if all wernt ok, else appropriate error value. |
| 108 | */ |
| 109 | static int rproc_pre_probe(struct udevice *dev) |
| 110 | { |
| 111 | struct dm_rproc_uclass_pdata *uc_pdata; |
| 112 | const struct dm_rproc_ops *ops; |
| 113 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 114 | uc_pdata = dev_get_uclass_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 115 | |
| 116 | /* See if we need to populate via fdt */ |
| 117 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 118 | if (!dev_get_plat(dev)) { |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 119 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 120 | int node = dev_of_offset(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 121 | const void *blob = gd->fdt_blob; |
| 122 | bool tmp; |
| 123 | if (!blob) { |
| 124 | debug("'%s' no dt?\n", dev->name); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | debug("'%s': using fdt\n", dev->name); |
| 128 | uc_pdata->name = fdt_getprop(blob, node, |
| 129 | "remoteproc-name", NULL); |
| 130 | |
| 131 | /* Default is internal memory mapped */ |
| 132 | uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED; |
| 133 | tmp = fdtdec_get_bool(blob, node, |
| 134 | "remoteproc-internal-memory-mapped"); |
| 135 | if (tmp) |
| 136 | uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED; |
| 137 | #else |
| 138 | /* Nothing much we can do about this, can we? */ |
| 139 | return -EINVAL; |
| 140 | #endif |
| 141 | |
| 142 | } else { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 143 | struct dm_rproc_uclass_pdata *pdata = dev_get_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 144 | |
| 145 | debug("'%s': using legacy data\n", dev->name); |
| 146 | if (pdata->name) |
| 147 | uc_pdata->name = pdata->name; |
| 148 | uc_pdata->mem_type = pdata->mem_type; |
| 149 | uc_pdata->driver_plat_data = pdata->driver_plat_data; |
| 150 | } |
| 151 | |
| 152 | /* Else try using device Name */ |
| 153 | if (!uc_pdata->name) |
| 154 | uc_pdata->name = dev->name; |
| 155 | if (!uc_pdata->name) { |
| 156 | debug("Unnamed device!"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | if (!rproc_name_is_unique(dev, uc_pdata->name)) { |
| 161 | debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name); |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
| 165 | ops = rproc_get_ops(dev); |
| 166 | if (!ops) { |
| 167 | debug("%s driver has no ops?\n", dev->name); |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
| 171 | if (!ops->load || !ops->start) { |
| 172 | debug("%s driver has missing mandatory ops?\n", dev->name); |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * rproc_post_probe() - post probe accessor for the uclass |
| 181 | * @dev: deivce we finished probing |
| 182 | * |
| 183 | * initiate init function after the probe is completed. This allows |
| 184 | * the remote processor drivers to split up the initializations between |
| 185 | * probe and init as needed. |
| 186 | * |
| 187 | * Return: if the remote proc driver has a init routine, invokes it and |
| 188 | * hands over the return value. overall, 0 if all went well, else appropriate |
| 189 | * error value. |
| 190 | */ |
| 191 | static int rproc_post_probe(struct udevice *dev) |
| 192 | { |
| 193 | const struct dm_rproc_ops *ops; |
| 194 | |
| 195 | ops = rproc_get_ops(dev); |
| 196 | if (!ops) { |
| 197 | debug("%s driver has no ops?\n", dev->name); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | if (ops->init) |
| 202 | return ops->init(dev); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | UCLASS_DRIVER(rproc) = { |
| 208 | .id = UCLASS_REMOTEPROC, |
| 209 | .name = "remoteproc", |
| 210 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 211 | .pre_probe = rproc_pre_probe, |
| 212 | .post_probe = rproc_post_probe, |
Simon Glass | 33b2efb | 2020-12-03 16:55:22 -0700 | [diff] [blame] | 213 | .per_device_plat_auto = sizeof(struct dm_rproc_uclass_pdata), |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | /* Remoteproc subsystem access functions */ |
| 217 | /** |
| 218 | * _rproc_probe_dev() - iteration helper to probe a rproc device |
| 219 | * @dev: device to probe |
| 220 | * @uc_pdata: uclass data allocated for the device |
| 221 | * @data: unused |
| 222 | * |
| 223 | * Return: 0 if all ok, else appropriate error value. |
| 224 | */ |
| 225 | static int _rproc_probe_dev(struct udevice *dev, |
| 226 | struct dm_rproc_uclass_pdata *uc_pdata, |
| 227 | const void *data) |
| 228 | { |
| 229 | int ret; |
| 230 | |
| 231 | ret = device_probe(dev); |
| 232 | |
| 233 | if (ret) |
| 234 | debug("%s: Failed to initialize - %d\n", dev->name, ret); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * _rproc_dev_is_probed() - check if the device has been probed |
| 240 | * @dev: device to check |
| 241 | * @uc_pdata: unused |
| 242 | * @data: unused |
| 243 | * |
| 244 | * Return: -EAGAIN if not probed else return 0 |
| 245 | */ |
| 246 | static int _rproc_dev_is_probed(struct udevice *dev, |
| 247 | struct dm_rproc_uclass_pdata *uc_pdata, |
| 248 | const void *data) |
| 249 | { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 250 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 251 | return 0; |
| 252 | |
| 253 | return -EAGAIN; |
| 254 | } |
| 255 | |
| 256 | bool rproc_is_initialized(void) |
| 257 | { |
| 258 | int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL); |
| 259 | return ret ? false : true; |
| 260 | } |
| 261 | |
| 262 | int rproc_init(void) |
| 263 | { |
| 264 | int ret; |
| 265 | |
| 266 | if (rproc_is_initialized()) { |
| 267 | debug("Already initialized\n"); |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
| 271 | ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL); |
| 272 | return ret; |
| 273 | } |
| 274 | |
Lokesh Vutla | ddca80e | 2018-08-27 15:57:50 +0530 | [diff] [blame] | 275 | int rproc_dev_init(int id) |
| 276 | { |
| 277 | struct udevice *dev = NULL; |
| 278 | int ret; |
| 279 | |
| 280 | ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev); |
| 281 | if (ret) { |
| 282 | debug("Unknown remote processor id '%d' requested(%d)\n", |
| 283 | id, ret); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | ret = device_probe(dev); |
| 288 | if (ret) |
| 289 | debug("%s: Failed to initialize - %d\n", dev->name, ret); |
| 290 | |
| 291 | return ret; |
| 292 | } |
| 293 | |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 294 | int rproc_load(int id, ulong addr, ulong size) |
| 295 | { |
| 296 | struct udevice *dev = NULL; |
| 297 | struct dm_rproc_uclass_pdata *uc_pdata; |
| 298 | const struct dm_rproc_ops *ops; |
| 299 | int ret; |
| 300 | |
| 301 | ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev); |
| 302 | if (ret) { |
| 303 | debug("Unknown remote processor id '%d' requested(%d)\n", |
| 304 | id, ret); |
| 305 | return ret; |
| 306 | } |
| 307 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 308 | uc_pdata = dev_get_uclass_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 309 | |
| 310 | ops = rproc_get_ops(dev); |
| 311 | if (!ops) { |
| 312 | debug("%s driver has no ops?\n", dev->name); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
| 316 | debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n", |
| 317 | uc_pdata->name, addr, size); |
| 318 | if (ops->load) |
| 319 | return ops->load(dev, addr, size); |
| 320 | |
| 321 | debug("%s: data corruption?? mandatory function is missing!\n", |
| 322 | dev->name); |
| 323 | |
| 324 | return -EINVAL; |
| 325 | }; |
| 326 | |
| 327 | /* |
| 328 | * Completely internal helper enums.. |
| 329 | * Keeping this isolated helps this code evolve independent of other |
| 330 | * parts.. |
| 331 | */ |
| 332 | enum rproc_ops { |
| 333 | RPROC_START, |
| 334 | RPROC_STOP, |
| 335 | RPROC_RESET, |
| 336 | RPROC_PING, |
| 337 | RPROC_RUNNING, |
| 338 | }; |
| 339 | |
| 340 | /** |
| 341 | * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback |
| 342 | * @id: id of the remote processor |
| 343 | * @op: one of rproc_ops that indicate what operation to invoke |
| 344 | * |
| 345 | * Most of the checks and verification for remoteproc operations are more |
| 346 | * or less same for almost all operations. This allows us to put a wrapper |
| 347 | * and use the common checks to allow the driver to function appropriately. |
| 348 | * |
| 349 | * Return: 0 if all ok, else appropriate error value. |
| 350 | */ |
| 351 | static int _rproc_ops_wrapper(int id, enum rproc_ops op) |
| 352 | { |
| 353 | struct udevice *dev = NULL; |
| 354 | struct dm_rproc_uclass_pdata *uc_pdata; |
| 355 | const struct dm_rproc_ops *ops; |
| 356 | int (*fn)(struct udevice *dev); |
| 357 | bool mandatory = false; |
| 358 | char *op_str; |
| 359 | int ret; |
| 360 | |
| 361 | ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev); |
| 362 | if (ret) { |
| 363 | debug("Unknown remote processor id '%d' requested(%d)\n", |
| 364 | id, ret); |
| 365 | return ret; |
| 366 | } |
| 367 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 368 | uc_pdata = dev_get_uclass_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 369 | |
| 370 | ops = rproc_get_ops(dev); |
| 371 | if (!ops) { |
| 372 | debug("%s driver has no ops?\n", dev->name); |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | switch (op) { |
| 376 | case RPROC_START: |
| 377 | fn = ops->start; |
| 378 | mandatory = true; |
| 379 | op_str = "Starting"; |
| 380 | break; |
| 381 | case RPROC_STOP: |
| 382 | fn = ops->stop; |
| 383 | op_str = "Stopping"; |
| 384 | break; |
| 385 | case RPROC_RESET: |
| 386 | fn = ops->reset; |
| 387 | op_str = "Resetting"; |
| 388 | break; |
| 389 | case RPROC_RUNNING: |
| 390 | fn = ops->is_running; |
| 391 | op_str = "Checking if running:"; |
| 392 | break; |
| 393 | case RPROC_PING: |
| 394 | fn = ops->ping; |
| 395 | op_str = "Pinging"; |
| 396 | break; |
| 397 | default: |
| 398 | debug("what is '%d' operation??\n", op); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | debug("%s %s...\n", op_str, uc_pdata->name); |
| 403 | if (fn) |
| 404 | return fn(dev); |
| 405 | |
| 406 | if (mandatory) |
| 407 | debug("%s: data corruption?? mandatory function is missing!\n", |
| 408 | dev->name); |
| 409 | |
| 410 | return -ENOSYS; |
| 411 | } |
| 412 | |
| 413 | int rproc_start(int id) |
| 414 | { |
| 415 | return _rproc_ops_wrapper(id, RPROC_START); |
| 416 | }; |
| 417 | |
| 418 | int rproc_stop(int id) |
| 419 | { |
| 420 | return _rproc_ops_wrapper(id, RPROC_STOP); |
| 421 | }; |
| 422 | |
| 423 | int rproc_reset(int id) |
| 424 | { |
| 425 | return _rproc_ops_wrapper(id, RPROC_RESET); |
| 426 | }; |
| 427 | |
| 428 | int rproc_ping(int id) |
| 429 | { |
| 430 | return _rproc_ops_wrapper(id, RPROC_PING); |
| 431 | }; |
| 432 | |
| 433 | int rproc_is_running(int id) |
| 434 | { |
| 435 | return _rproc_ops_wrapper(id, RPROC_RUNNING); |
| 436 | }; |