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