| 1 | /* | 
|---|
| 2 | * drivers/base/devres.c - device resource management | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (c) 2006  SUSE Linux Products GmbH | 
|---|
| 5 | * Copyright (c) 2006  Tejun Heo <teheo@suse.de> | 
|---|
| 6 | * | 
|---|
| 7 | * This file is released under the GPLv2. | 
|---|
| 8 | */ | 
|---|
| 9 |  | 
|---|
| 10 | #include <linux/device.h> | 
|---|
| 11 | #include <linux/module.h> | 
|---|
| 12 | #include <linux/slab.h> | 
|---|
| 13 | #include <linux/gfp.h> | 
|---|
| 14 | #include <linux/errno.h> | 
|---|
| 15 |  | 
|---|
| 16 | struct devres_node { | 
|---|
| 17 | struct list_head                entry; | 
|---|
| 18 | dr_release_t                    release; | 
|---|
| 19 | #ifdef CONFIG_DEBUG_DEVRES | 
|---|
| 20 | const char                      *name; | 
|---|
| 21 | size_t                          size; | 
|---|
| 22 | #endif | 
|---|
| 23 | }; | 
|---|
| 24 |  | 
|---|
| 25 | struct devres { | 
|---|
| 26 | struct devres_node              node; | 
|---|
| 27 | /* -- 3 pointers */ | 
|---|
| 28 | unsigned long long              data[]; /* guarantee ull alignment */ | 
|---|
| 29 | }; | 
|---|
| 30 |  | 
|---|
| 31 | struct devres_group { | 
|---|
| 32 | struct devres_node              node[2]; | 
|---|
| 33 | void                            *id; | 
|---|
| 34 | int                             color; | 
|---|
| 35 | /* -- 8 pointers */ | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | #define devres_log(dev, node, op)       do {} while (0) | 
|---|
| 39 |  | 
|---|
| 40 | /* | 
|---|
| 41 | * Release functions for devres group.  These callbacks are used only | 
|---|
| 42 | * for identification. | 
|---|
| 43 | */ | 
|---|
| 44 | static void group_open_release(struct device *dev, void *res) | 
|---|
| 45 | { | 
|---|
| 46 | /* noop */ | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | static void group_close_release(struct device *dev, void *res) | 
|---|
| 50 | { | 
|---|
| 51 | /* noop */ | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | static struct devres_group * node_to_group(struct devres_node *node) | 
|---|
| 55 | { | 
|---|
| 56 | if (node->release == &group_open_release) | 
|---|
| 57 | return container_of(node, struct devres_group, node[0]); | 
|---|
| 58 | if (node->release == &group_close_release) | 
|---|
| 59 | return container_of(node, struct devres_group, node[1]); | 
|---|
| 60 | return NULL; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | static inline struct devres * alloc_dr(dr_release_t release, | 
|---|
| 64 | size_t size, gfp_t gfp, int nid) | 
|---|
| 65 | { | 
|---|
| 66 | size_t tot_size = sizeof(struct devres) + size; | 
|---|
| 67 | struct devres *dr; | 
|---|
| 68 |  | 
|---|
| 69 | dr = kmalloc_node_track_caller(tot_size, gfp, nid); | 
|---|
| 70 |  | 
|---|
| 71 | memset(dr, 0, offsetof(struct devres, data)); | 
|---|
| 72 |  | 
|---|
| 73 | INIT_LIST_HEAD(&dr->node.entry); | 
|---|
| 74 | dr->node.release = release; | 
|---|
| 75 | return dr; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | #define devres_log(dev, node, op)       do {} while (0) | 
|---|
| 79 |  | 
|---|
| 80 | static void add_dr(struct device *dev, struct devres_node *node) | 
|---|
| 81 | { | 
|---|
| 82 | devres_log(dev, node, "ADD"); | 
|---|
| 83 | BUG_ON(!list_empty(&node->entry)); | 
|---|
| 84 | //#ifndef TARGET_OS2 | 
|---|
| 85 | /* Traps here on OS/2 */ | 
|---|
| 86 | list_add_tail(&node->entry, &dev->devres_head); | 
|---|
| 87 | //#endif | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | /** | 
|---|
| 91 | * devres_add - Register device resource | 
|---|
| 92 | * @dev: Device to add resource to | 
|---|
| 93 | * @res: Resource to register | 
|---|
| 94 | * | 
|---|
| 95 | * Register devres @res to @dev.  @res should have been allocated | 
|---|
| 96 | * using devres_alloc().  On driver detach, the associated release | 
|---|
| 97 | * function will be invoked and devres will be freed automatically. | 
|---|
| 98 | */ | 
|---|
| 99 | void devres_add(struct device *dev, void *res) | 
|---|
| 100 | { | 
|---|
| 101 | /* Traps here on OS/2 */ | 
|---|
| 102 | struct devres *dr = container_of(res, struct devres, data); | 
|---|
| 103 | unsigned long flags; | 
|---|
| 104 | spin_lock_irqsave(&dev->devres_lock, flags); | 
|---|
| 105 | add_dr(dev, &dr->node); | 
|---|
| 106 | spin_unlock_irqrestore(&dev->devres_lock, flags); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | /** | 
|---|
| 110 | * devres_alloc - Allocate device resource data | 
|---|
| 111 | * @release: Release function devres will be associated with | 
|---|
| 112 | * @size: Allocation size | 
|---|
| 113 | * @gfp: Allocation flags | 
|---|
| 114 | * @nid: NUMA node | 
|---|
| 115 | * | 
|---|
| 116 | * Allocate devres of @size bytes.  The allocated area is zeroed, then | 
|---|
| 117 | * associated with @release.  The returned pointer can be passed to | 
|---|
| 118 | * other devres_*() functions. | 
|---|
| 119 | * | 
|---|
| 120 | * RETURNS: | 
|---|
| 121 | * Pointer to allocated devres on success, NULL on failure. | 
|---|
| 122 | */ | 
|---|
| 123 | void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid) | 
|---|
| 124 | { | 
|---|
| 125 | struct devres *dr; | 
|---|
| 126 |  | 
|---|
| 127 | dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid); | 
|---|
| 128 | return dr->data; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 | * devres_free - Free device resource data | 
|---|
| 133 | * @res: Pointer to devres data to free | 
|---|
| 134 | * | 
|---|
| 135 | * Free devres created with devres_alloc(). | 
|---|
| 136 | */ | 
|---|
| 137 | void devres_free(void *res) | 
|---|
| 138 | { | 
|---|
| 139 | if (res) { | 
|---|
| 140 | struct devres *dr = container_of(res, struct devres, data); | 
|---|
| 141 |  | 
|---|
| 142 | BUG_ON(!list_empty(&dr->node.entry)); | 
|---|
| 143 | kfree(dr); | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | static int remove_nodes(struct device *dev, | 
|---|
| 148 | struct list_head *first, struct list_head *end, | 
|---|
| 149 | struct list_head *todo) | 
|---|
| 150 | { | 
|---|
| 151 | int cnt = 0, nr_groups = 0; | 
|---|
| 152 | struct list_head *cur; | 
|---|
| 153 |  | 
|---|
| 154 | /* First pass - move normal devres entries to @todo and clear | 
|---|
| 155 | * devres_group colors. | 
|---|
| 156 | */ | 
|---|
| 157 | cur = first; | 
|---|
| 158 | while (cur != end) { | 
|---|
| 159 | struct devres_node *node; | 
|---|
| 160 | struct devres_group *grp; | 
|---|
| 161 |  | 
|---|
| 162 | node = list_entry(cur, struct devres_node, entry); | 
|---|
| 163 | cur = cur->next; | 
|---|
| 164 |  | 
|---|
| 165 | grp = node_to_group(node); | 
|---|
| 166 | if (grp) { | 
|---|
| 167 | /* clear color of group markers in the first pass */ | 
|---|
| 168 | grp->color = 0; | 
|---|
| 169 | nr_groups++; | 
|---|
| 170 | } else { | 
|---|
| 171 | /* regular devres entry */ | 
|---|
| 172 | if (&node->entry == first) | 
|---|
| 173 | first = first->next; | 
|---|
| 174 | list_move_tail(&node->entry, todo); | 
|---|
| 175 | cnt++; | 
|---|
| 176 | } | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | if (!nr_groups) | 
|---|
| 180 | return cnt; | 
|---|
| 181 |  | 
|---|
| 182 | /* Second pass - Scan groups and color them.  A group gets | 
|---|
| 183 | * color value of two iff the group is wholly contained in | 
|---|
| 184 | * [cur, end).  That is, for a closed group, both opening and | 
|---|
| 185 | * closing markers should be in the range, while just the | 
|---|
| 186 | * opening marker is enough for an open group. | 
|---|
| 187 | */ | 
|---|
| 188 | cur = first; | 
|---|
| 189 | while (cur != end) { | 
|---|
| 190 | struct devres_node *node; | 
|---|
| 191 | struct devres_group *grp; | 
|---|
| 192 |  | 
|---|
| 193 | node = list_entry(cur, struct devres_node, entry); | 
|---|
| 194 | cur = cur->next; | 
|---|
| 195 |  | 
|---|
| 196 | grp = node_to_group(node); | 
|---|
| 197 | BUG_ON(!grp || list_empty(&grp->node[0].entry)); | 
|---|
| 198 |  | 
|---|
| 199 | grp->color++; | 
|---|
| 200 | if (list_empty(&grp->node[1].entry)) | 
|---|
| 201 | grp->color++; | 
|---|
| 202 |  | 
|---|
| 203 | BUG_ON(grp->color <= 0 || grp->color > 2); | 
|---|
| 204 | if (grp->color == 2) { | 
|---|
| 205 | /* No need to update cur or end.  The removed | 
|---|
| 206 | * nodes are always before both. | 
|---|
| 207 | */ | 
|---|
| 208 | list_move_tail(&grp->node[0].entry, todo); | 
|---|
| 209 | list_del_init(&grp->node[1].entry); | 
|---|
| 210 | } | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | return cnt; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | static int release_nodes(struct device *dev, struct list_head *first, | 
|---|
| 217 | struct list_head *end, unsigned long flags) | 
|---|
| 218 | { | 
|---|
| 219 | //      LIST_HEAD(todo); | 
|---|
| 220 | struct list_head todo; | 
|---|
| 221 |  | 
|---|
| 222 | int cnt; | 
|---|
| 223 | struct devres *dr, *tmp; | 
|---|
| 224 |  | 
|---|
| 225 | cnt = remove_nodes(dev, first, end, &todo); | 
|---|
| 226 |  | 
|---|
| 227 | spin_unlock_irqrestore(&dev->devres_lock, flags); | 
|---|
| 228 |  | 
|---|
| 229 | /* Release.  Note that both devres and devres_group are | 
|---|
| 230 | * handled as devres in the following loop.  This is safe. | 
|---|
| 231 | */ | 
|---|
| 232 | list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry, struct devres) { | 
|---|
| 233 | devres_log(dev, &dr->node, "REL"); | 
|---|
| 234 | dr->node.release(dev, dr->data); | 
|---|
| 235 | kfree(dr); | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | return cnt; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /** | 
|---|
| 242 | * devres_release_all - Release all managed resources | 
|---|
| 243 | * @dev: Device to release resources for | 
|---|
| 244 | * | 
|---|
| 245 | * Release all resources associated with @dev.  This function is | 
|---|
| 246 | * called on driver detach. | 
|---|
| 247 | */ | 
|---|
| 248 | int devres_release_all(struct device *dev) | 
|---|
| 249 | { | 
|---|
| 250 | unsigned long flags; | 
|---|
| 251 |  | 
|---|
| 252 | /* Looks like an uninitialized device structure */ | 
|---|
| 253 | if (WARN_ON(dev->devres_head.next == NULL)) | 
|---|
| 254 | return -ENODEV; | 
|---|
| 255 | spin_lock_irqsave(&dev->devres_lock, flags); | 
|---|
| 256 | return release_nodes(dev, dev->devres_head.next, &dev->devres_head, | 
|---|
| 257 | flags); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | static struct devres *find_dr(struct device *dev, dr_release_t release, | 
|---|
| 261 | dr_match_t match, void *match_data) | 
|---|
| 262 | { | 
|---|
| 263 | struct devres_node *node; | 
|---|
| 264 |  | 
|---|
| 265 | list_for_each_entry_reverse(node, &dev->devres_head, entry, struct devres_node) { | 
|---|
| 266 | struct devres *dr = container_of(node, struct devres, node); | 
|---|
| 267 |  | 
|---|
| 268 | if (node->release != release) | 
|---|
| 269 | continue; | 
|---|
| 270 | if (match && !match(dev, dr->data, match_data)) | 
|---|
| 271 | continue; | 
|---|
| 272 | return dr; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | return NULL; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | /** | 
|---|
| 279 | * devres_find - Find device resource | 
|---|
| 280 | * @dev: Device to lookup resource from | 
|---|
| 281 | * @release: Look for resources associated with this release function | 
|---|
| 282 | * @match: Match function (optional) | 
|---|
| 283 | * @match_data: Data for the match function | 
|---|
| 284 | * | 
|---|
| 285 | * Find the latest devres of @dev which is associated with @release | 
|---|
| 286 | * and for which @match returns 1.  If @match is NULL, it's considered | 
|---|
| 287 | * to match all. | 
|---|
| 288 | * | 
|---|
| 289 | * RETURNS: | 
|---|
| 290 | * Pointer to found devres, NULL if not found. | 
|---|
| 291 | */ | 
|---|
| 292 | void * devres_find(struct device *dev, dr_release_t release, | 
|---|
| 293 | dr_match_t match, void *match_data) | 
|---|
| 294 | { | 
|---|
| 295 | struct devres *dr; | 
|---|
| 296 | unsigned long flags; | 
|---|
| 297 |  | 
|---|
| 298 | spin_lock_irqsave(&dev->devres_lock, flags); | 
|---|
| 299 | dr = find_dr(dev, release, match, match_data); | 
|---|
| 300 | spin_unlock_irqrestore(&dev->devres_lock, flags); | 
|---|
| 301 |  | 
|---|
| 302 | if (dr) | 
|---|
| 303 | return dr->data; | 
|---|
| 304 | return NULL; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | /** | 
|---|
| 308 | * devm_add_action() - add a custom action to list of managed resources | 
|---|
| 309 | * @dev: Device that owns the action | 
|---|
| 310 | * @action: Function that should be called | 
|---|
| 311 | * @data: Pointer to data passed to @action implementation | 
|---|
| 312 | * | 
|---|
| 313 | * This adds a custom action to the list of managed resources so that | 
|---|
| 314 | * it gets executed as part of standard resource unwinding. | 
|---|
| 315 | */ | 
|---|
| 316 | int devm_add_action(struct device *dev, void (*action)(void *), void *data) | 
|---|
| 317 | { | 
|---|
| 318 | return 0; | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | /** | 
|---|
| 322 | * devm_remove_action() - removes previously added custom action | 
|---|
| 323 | * @dev: Device that owns the action | 
|---|
| 324 | * @action: Function implementing the action | 
|---|
| 325 | * @data: Pointer to data passed to @action implementation | 
|---|
| 326 | * | 
|---|
| 327 | * Removes instance of @action previously added by devm_add_action(). | 
|---|
| 328 | * Both action and data should match one of the existing entries. | 
|---|
| 329 | */ | 
|---|
| 330 | void devm_remove_action(struct device *dev, void (*action)(void *), void *data) | 
|---|
| 331 | { | 
|---|
| 332 | } | 
|---|