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 | */ |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | |
| 7 | #define LOG_CATEGORY UCLASS_REMOTEPROC |
| 8 | |
Nishanth Menon | 08b9dc2 | 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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <remoteproc.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm.h> |
| 18 | #include <dm/uclass.h> |
| 19 | #include <dm/uclass-internal.h> |
| 20 | |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 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) |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 120 | bool tmp; |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 121 | debug("'%s': using fdt\n", dev->name); |
Patrick Delaunay | c249ae5 | 2021-09-20 17:56:06 +0200 | [diff] [blame] | 122 | uc_pdata->name = dev_read_string(dev, "remoteproc-name"); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 123 | |
| 124 | /* Default is internal memory mapped */ |
| 125 | uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED; |
Patrick Delaunay | c249ae5 | 2021-09-20 17:56:06 +0200 | [diff] [blame] | 126 | tmp = dev_read_bool(dev, "remoteproc-internal-memory-mapped"); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 127 | if (tmp) |
| 128 | uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED; |
| 129 | #else |
| 130 | /* Nothing much we can do about this, can we? */ |
| 131 | return -EINVAL; |
| 132 | #endif |
| 133 | |
| 134 | } else { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 135 | struct dm_rproc_uclass_pdata *pdata = dev_get_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 136 | |
| 137 | debug("'%s': using legacy data\n", dev->name); |
| 138 | if (pdata->name) |
| 139 | uc_pdata->name = pdata->name; |
| 140 | uc_pdata->mem_type = pdata->mem_type; |
| 141 | uc_pdata->driver_plat_data = pdata->driver_plat_data; |
| 142 | } |
| 143 | |
| 144 | /* Else try using device Name */ |
| 145 | if (!uc_pdata->name) |
| 146 | uc_pdata->name = dev->name; |
| 147 | if (!uc_pdata->name) { |
| 148 | debug("Unnamed device!"); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | if (!rproc_name_is_unique(dev, uc_pdata->name)) { |
| 153 | debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | ops = rproc_get_ops(dev); |
| 158 | if (!ops) { |
| 159 | debug("%s driver has no ops?\n", dev->name); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | if (!ops->load || !ops->start) { |
| 164 | debug("%s driver has missing mandatory ops?\n", dev->name); |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * rproc_post_probe() - post probe accessor for the uclass |
| 173 | * @dev: deivce we finished probing |
| 174 | * |
| 175 | * initiate init function after the probe is completed. This allows |
| 176 | * the remote processor drivers to split up the initializations between |
| 177 | * probe and init as needed. |
| 178 | * |
| 179 | * Return: if the remote proc driver has a init routine, invokes it and |
| 180 | * hands over the return value. overall, 0 if all went well, else appropriate |
| 181 | * error value. |
| 182 | */ |
| 183 | static int rproc_post_probe(struct udevice *dev) |
| 184 | { |
| 185 | const struct dm_rproc_ops *ops; |
| 186 | |
| 187 | ops = rproc_get_ops(dev); |
| 188 | if (!ops) { |
| 189 | debug("%s driver has no ops?\n", dev->name); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | if (ops->init) |
| 194 | return ops->init(dev); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | UCLASS_DRIVER(rproc) = { |
| 200 | .id = UCLASS_REMOTEPROC, |
| 201 | .name = "remoteproc", |
| 202 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 203 | .pre_probe = rproc_pre_probe, |
| 204 | .post_probe = rproc_post_probe, |
Simon Glass | 33b2efb | 2020-12-03 16:55:22 -0700 | [diff] [blame] | 205 | .per_device_plat_auto = sizeof(struct dm_rproc_uclass_pdata), |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | /* Remoteproc subsystem access functions */ |
| 209 | /** |
| 210 | * _rproc_probe_dev() - iteration helper to probe a rproc device |
| 211 | * @dev: device to probe |
| 212 | * @uc_pdata: uclass data allocated for the device |
| 213 | * @data: unused |
| 214 | * |
| 215 | * Return: 0 if all ok, else appropriate error value. |
| 216 | */ |
| 217 | static int _rproc_probe_dev(struct udevice *dev, |
| 218 | struct dm_rproc_uclass_pdata *uc_pdata, |
| 219 | const void *data) |
| 220 | { |
| 221 | int ret; |
| 222 | |
| 223 | ret = device_probe(dev); |
| 224 | |
| 225 | if (ret) |
| 226 | debug("%s: Failed to initialize - %d\n", dev->name, ret); |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * _rproc_dev_is_probed() - check if the device has been probed |
| 232 | * @dev: device to check |
| 233 | * @uc_pdata: unused |
| 234 | * @data: unused |
| 235 | * |
| 236 | * Return: -EAGAIN if not probed else return 0 |
| 237 | */ |
| 238 | static int _rproc_dev_is_probed(struct udevice *dev, |
| 239 | struct dm_rproc_uclass_pdata *uc_pdata, |
| 240 | const void *data) |
| 241 | { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 242 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 243 | return 0; |
| 244 | |
| 245 | return -EAGAIN; |
| 246 | } |
| 247 | |
| 248 | bool rproc_is_initialized(void) |
| 249 | { |
| 250 | int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL); |
| 251 | return ret ? false : true; |
| 252 | } |
| 253 | |
| 254 | int rproc_init(void) |
| 255 | { |
| 256 | int ret; |
| 257 | |
| 258 | if (rproc_is_initialized()) { |
| 259 | debug("Already initialized\n"); |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | |
| 263 | ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL); |
| 264 | return ret; |
| 265 | } |
| 266 | |
Lokesh Vutla | ddca80e | 2018-08-27 15:57:50 +0530 | [diff] [blame] | 267 | int rproc_dev_init(int id) |
| 268 | { |
| 269 | struct udevice *dev = NULL; |
| 270 | int ret; |
| 271 | |
| 272 | ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev); |
| 273 | if (ret) { |
| 274 | debug("Unknown remote processor id '%d' requested(%d)\n", |
| 275 | id, ret); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | ret = device_probe(dev); |
| 280 | if (ret) |
| 281 | debug("%s: Failed to initialize - %d\n", dev->name, ret); |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 286 | int rproc_load(int id, ulong addr, ulong size) |
| 287 | { |
| 288 | struct udevice *dev = NULL; |
| 289 | struct dm_rproc_uclass_pdata *uc_pdata; |
| 290 | const struct dm_rproc_ops *ops; |
| 291 | int ret; |
| 292 | |
| 293 | ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev); |
| 294 | if (ret) { |
| 295 | debug("Unknown remote processor id '%d' requested(%d)\n", |
| 296 | id, ret); |
| 297 | return ret; |
| 298 | } |
| 299 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 300 | uc_pdata = dev_get_uclass_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 301 | |
| 302 | ops = rproc_get_ops(dev); |
| 303 | if (!ops) { |
| 304 | debug("%s driver has no ops?\n", dev->name); |
| 305 | return -EINVAL; |
| 306 | } |
| 307 | |
| 308 | debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n", |
| 309 | uc_pdata->name, addr, size); |
| 310 | if (ops->load) |
| 311 | return ops->load(dev, addr, size); |
| 312 | |
| 313 | debug("%s: data corruption?? mandatory function is missing!\n", |
| 314 | dev->name); |
| 315 | |
| 316 | return -EINVAL; |
| 317 | }; |
| 318 | |
| 319 | /* |
| 320 | * Completely internal helper enums.. |
| 321 | * Keeping this isolated helps this code evolve independent of other |
| 322 | * parts.. |
| 323 | */ |
| 324 | enum rproc_ops { |
| 325 | RPROC_START, |
| 326 | RPROC_STOP, |
| 327 | RPROC_RESET, |
| 328 | RPROC_PING, |
| 329 | RPROC_RUNNING, |
| 330 | }; |
| 331 | |
| 332 | /** |
| 333 | * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback |
| 334 | * @id: id of the remote processor |
| 335 | * @op: one of rproc_ops that indicate what operation to invoke |
| 336 | * |
| 337 | * Most of the checks and verification for remoteproc operations are more |
| 338 | * or less same for almost all operations. This allows us to put a wrapper |
| 339 | * and use the common checks to allow the driver to function appropriately. |
| 340 | * |
| 341 | * Return: 0 if all ok, else appropriate error value. |
| 342 | */ |
| 343 | static int _rproc_ops_wrapper(int id, enum rproc_ops op) |
| 344 | { |
| 345 | struct udevice *dev = NULL; |
| 346 | struct dm_rproc_uclass_pdata *uc_pdata; |
| 347 | const struct dm_rproc_ops *ops; |
| 348 | int (*fn)(struct udevice *dev); |
| 349 | bool mandatory = false; |
| 350 | char *op_str; |
| 351 | int ret; |
| 352 | |
| 353 | ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev); |
| 354 | if (ret) { |
| 355 | debug("Unknown remote processor id '%d' requested(%d)\n", |
| 356 | id, ret); |
| 357 | return ret; |
| 358 | } |
| 359 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 360 | uc_pdata = dev_get_uclass_plat(dev); |
Nishanth Menon | 08b9dc2 | 2015-09-17 15:42:39 -0500 | [diff] [blame] | 361 | |
| 362 | ops = rproc_get_ops(dev); |
| 363 | if (!ops) { |
| 364 | debug("%s driver has no ops?\n", dev->name); |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | switch (op) { |
| 368 | case RPROC_START: |
| 369 | fn = ops->start; |
| 370 | mandatory = true; |
| 371 | op_str = "Starting"; |
| 372 | break; |
| 373 | case RPROC_STOP: |
| 374 | fn = ops->stop; |
| 375 | op_str = "Stopping"; |
| 376 | break; |
| 377 | case RPROC_RESET: |
| 378 | fn = ops->reset; |
| 379 | op_str = "Resetting"; |
| 380 | break; |
| 381 | case RPROC_RUNNING: |
| 382 | fn = ops->is_running; |
| 383 | op_str = "Checking if running:"; |
| 384 | break; |
| 385 | case RPROC_PING: |
| 386 | fn = ops->ping; |
| 387 | op_str = "Pinging"; |
| 388 | break; |
| 389 | default: |
| 390 | debug("what is '%d' operation??\n", op); |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | |
| 394 | debug("%s %s...\n", op_str, uc_pdata->name); |
| 395 | if (fn) |
| 396 | return fn(dev); |
| 397 | |
| 398 | if (mandatory) |
| 399 | debug("%s: data corruption?? mandatory function is missing!\n", |
| 400 | dev->name); |
| 401 | |
| 402 | return -ENOSYS; |
| 403 | } |
| 404 | |
| 405 | int rproc_start(int id) |
| 406 | { |
| 407 | return _rproc_ops_wrapper(id, RPROC_START); |
| 408 | }; |
| 409 | |
| 410 | int rproc_stop(int id) |
| 411 | { |
| 412 | return _rproc_ops_wrapper(id, RPROC_STOP); |
| 413 | }; |
| 414 | |
| 415 | int rproc_reset(int id) |
| 416 | { |
| 417 | return _rproc_ops_wrapper(id, RPROC_RESET); |
| 418 | }; |
| 419 | |
| 420 | int rproc_ping(int id) |
| 421 | { |
| 422 | return _rproc_ops_wrapper(id, RPROC_PING); |
| 423 | }; |
| 424 | |
| 425 | int rproc_is_running(int id) |
| 426 | { |
| 427 | return _rproc_ops_wrapper(id, RPROC_RUNNING); |
| 428 | }; |