Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <device.h> |
Anas Nashif | e1e05a2 | 2019-06-25 12:25:32 -0400 | [diff] [blame] | 9 | #include <sys/atomic.h> |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 10 | #include <syscall_handler.h> |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 11 | |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 12 | extern const struct init_entry __init_start[]; |
| 13 | extern const struct init_entry __init_PRE_KERNEL_1_start[]; |
| 14 | extern const struct init_entry __init_PRE_KERNEL_2_start[]; |
| 15 | extern const struct init_entry __init_POST_KERNEL_start[]; |
| 16 | extern const struct init_entry __init_APPLICATION_start[]; |
| 17 | extern const struct init_entry __init_end[]; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 18 | |
Daniel Leung | 4e1637b | 2020-01-15 08:57:29 -0800 | [diff] [blame] | 19 | #ifdef CONFIG_SMP |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 20 | extern const struct init_entry __init_SMP_start[]; |
Daniel Leung | 4e1637b | 2020-01-15 08:57:29 -0800 | [diff] [blame] | 21 | #endif |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 22 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 23 | extern const struct device __device_start[]; |
| 24 | extern const struct device __device_end[]; |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 25 | |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 26 | extern uint32_t __device_init_status_start[]; |
| 27 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 28 | /** |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 29 | * @brief Initialize state for all static devices. |
| 30 | * |
| 31 | * The state object is always zero-initialized, but this may not be |
| 32 | * sufficient. |
| 33 | */ |
| 34 | void z_device_state_init(void) |
| 35 | { |
| 36 | const struct device *dev = __device_start; |
| 37 | |
| 38 | while (dev < __device_end) { |
| 39 | z_object_init(dev); |
| 40 | ++dev; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 45 | * @brief Execute all the init entry initialization functions at a given level |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 46 | * |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 47 | * @details Invokes the initialization routine for each init entry object |
| 48 | * created by the INIT_ENTRY_DEFINE() macro using the specified level. |
| 49 | * The linker script places the init entry objects in memory in the order |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 50 | * they need to be invoked, with symbols indicating where one level leaves |
| 51 | * off and the next one begins. |
| 52 | * |
| 53 | * @param level init level to run. |
| 54 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 55 | void z_sys_init_run_level(int32_t level) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 56 | { |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 57 | static const struct init_entry *levels[] = { |
| 58 | __init_PRE_KERNEL_1_start, |
| 59 | __init_PRE_KERNEL_2_start, |
| 60 | __init_POST_KERNEL_start, |
| 61 | __init_APPLICATION_start, |
Daniel Leung | 4e1637b | 2020-01-15 08:57:29 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_SMP |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 63 | __init_SMP_start, |
Daniel Leung | 4e1637b | 2020-01-15 08:57:29 -0800 | [diff] [blame] | 64 | #endif |
Flavio Ceolin | ac14685 | 2018-11-01 17:42:07 -0700 | [diff] [blame] | 65 | /* End marker */ |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 66 | __init_end, |
Flavio Ceolin | ac14685 | 2018-11-01 17:42:07 -0700 | [diff] [blame] | 67 | }; |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 68 | const struct init_entry *entry; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 69 | |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 70 | for (entry = levels[level]; entry < levels[level+1]; entry++) { |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 71 | const struct device *dev = entry->dev; |
Peter Bigot | 65eee5c | 2021-02-02 10:23:55 -0600 | [diff] [blame] | 72 | int rc = entry->init(dev); |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 73 | |
Peter Bigot | 65eee5c | 2021-02-02 10:23:55 -0600 | [diff] [blame] | 74 | if (dev != NULL) { |
| 75 | /* Mark device initialized. If initialization |
| 76 | * failed, record the error condition. |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 77 | */ |
Peter Bigot | 65eee5c | 2021-02-02 10:23:55 -0600 | [diff] [blame] | 78 | if (rc != 0) { |
| 79 | if (rc < 0) { |
| 80 | rc = -rc; |
| 81 | } |
| 82 | if (rc > UINT8_MAX) { |
| 83 | rc = UINT8_MAX; |
| 84 | } |
| 85 | dev->state->init_res = rc; |
| 86 | } |
| 87 | dev->state->initialized = true; |
Andrew Boie | a68120d | 2018-12-07 13:12:21 -0800 | [diff] [blame] | 88 | } |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 92 | const struct device *z_impl_device_get_binding(const char *name) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 93 | { |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 94 | const struct device *dev; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 95 | |
Peter Bigot | 5b36a01 | 2021-02-12 06:19:08 -0600 | [diff] [blame] | 96 | /* A null string identifies no device. So does an empty |
| 97 | * string. |
| 98 | */ |
Anas Nashif | 0630452 | 2021-03-29 17:13:18 -0400 | [diff] [blame] | 99 | if ((name == NULL) || (name[0] == '\0')) { |
Peter Bigot | 5b36a01 | 2021-02-12 06:19:08 -0600 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 103 | /* Split the search into two loops: in the common scenario, where |
| 104 | * device names are stored in ROM (and are referenced by the user |
| 105 | * with CONFIG_* macros), only cheap pointer comparisons will be |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 106 | * performed. Reserve string comparisons for a fallback. |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 107 | */ |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 108 | for (dev = __device_start; dev != __device_end; dev++) { |
Peter Bigot | d8b86cb | 2020-06-22 10:01:39 -0500 | [diff] [blame] | 109 | if (z_device_ready(dev) && (dev->name == name)) { |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 110 | return dev; |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 114 | for (dev = __device_start; dev != __device_end; dev++) { |
Peter Bigot | d8b86cb | 2020-06-22 10:01:39 -0500 | [diff] [blame] | 115 | if (z_device_ready(dev) && (strcmp(name, dev->name) == 0)) { |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 116 | return dev; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 123 | #ifdef CONFIG_USERSPACE |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 124 | static inline const struct device *z_vrfy_device_get_binding(const char *name) |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 125 | { |
| 126 | char name_copy[Z_DEVICE_MAX_NAME_LEN]; |
| 127 | |
| 128 | if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy)) |
| 129 | != 0) { |
Daniel Leung | 0773441 | 2021-04-27 11:39:33 -0700 | [diff] [blame] | 130 | return NULL; |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 133 | return z_impl_device_get_binding(name_copy); |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 134 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 135 | #include <syscalls/device_get_binding_mrsh.c> |
Andrzej Głąbek | 6de16d0 | 2021-03-12 16:25:59 +0100 | [diff] [blame] | 136 | |
| 137 | static inline int z_vrfy_device_usable_check(const struct device *dev) |
| 138 | { |
| 139 | Z_OOPS(Z_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY)); |
| 140 | |
| 141 | return z_impl_device_usable_check(dev); |
| 142 | } |
| 143 | #include <syscalls/device_usable_check_mrsh.c> |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 144 | #endif /* CONFIG_USERSPACE */ |
| 145 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 146 | size_t z_device_get_all_static(struct device const **devices) |
Peter Bigot | 219a3ca | 2020-06-22 08:55:37 -0500 | [diff] [blame] | 147 | { |
| 148 | *devices = __device_start; |
| 149 | return __device_end - __device_start; |
| 150 | } |
| 151 | |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 152 | bool z_device_ready(const struct device *dev) |
| 153 | { |
Armando Visconti | 4b4068c | 2021-05-13 18:24:25 +0200 | [diff] [blame] | 154 | /* |
| 155 | * if an invalid device pointer is passed as argument, this call |
| 156 | * reports the `device` as not ready for usage. |
| 157 | */ |
| 158 | if (dev == NULL) { |
| 159 | return false; |
| 160 | } |
| 161 | |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 162 | return dev->state->initialized && (dev->state->init_res == 0U); |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Martí Bolívar | b94a11f | 2021-09-24 12:25:40 -0700 | [diff] [blame] | 165 | int device_required_foreach(const struct device *dev, |
| 166 | device_visitor_callback_t visitor_cb, |
| 167 | void *context) |
Peter Bigot | b29abe3 | 2021-02-22 11:42:08 -0600 | [diff] [blame] | 168 | { |
Martí Bolívar | b94a11f | 2021-09-24 12:25:40 -0700 | [diff] [blame] | 169 | size_t handle_count = 0; |
| 170 | const device_handle_t *handles = |
| 171 | device_required_handles_get(dev, &handle_count); |
| 172 | |
Peter Bigot | b29abe3 | 2021-02-22 11:42:08 -0600 | [diff] [blame] | 173 | /* Iterate over fixed devices */ |
| 174 | for (size_t i = 0; i < handle_count; ++i) { |
| 175 | device_handle_t dh = handles[i]; |
| 176 | const struct device *rdev = device_from_handle(dh); |
| 177 | int rc = visitor_cb(rdev, context); |
| 178 | |
| 179 | if (rc < 0) { |
| 180 | return rc; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return handle_count; |
| 185 | } |