| 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 | /*
|
|---|
| 39 | * Release functions for devres group. These callbacks are used only
|
|---|
| 40 | * for identification.
|
|---|
| 41 | */
|
|---|
| 42 | static void group_open_release(struct device *dev, void *res)
|
|---|
| 43 | {
|
|---|
| 44 | /* noop */
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | static void group_close_release(struct device *dev, void *res)
|
|---|
| 48 | {
|
|---|
| 49 | /* noop */
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | static struct devres_group * node_to_group(struct devres_node *node)
|
|---|
| 53 | {
|
|---|
| 54 | if (node->release == &group_open_release)
|
|---|
| 55 | return container_of(node, struct devres_group, node[0]);
|
|---|
| 56 | if (node->release == &group_close_release)
|
|---|
| 57 | return container_of(node, struct devres_group, node[1]);
|
|---|
| 58 | return NULL;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | static inline struct devres * alloc_dr(dr_release_t release,
|
|---|
| 62 | size_t size, gfp_t gfp, int nid)
|
|---|
| 63 | {
|
|---|
| 64 | size_t tot_size = sizeof(struct devres) + size;
|
|---|
| 65 | struct devres *dr;
|
|---|
| 66 |
|
|---|
| 67 | dr = kmalloc_node_track_caller(tot_size, gfp, nid);
|
|---|
| 68 |
|
|---|
| 69 | memset(dr, 0, offsetof(struct devres, data));
|
|---|
| 70 |
|
|---|
| 71 | INIT_LIST_HEAD(&dr->node.entry);
|
|---|
| 72 | dr->node.release = release;
|
|---|
| 73 | return dr;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | #define devres_log(dev, node, op) do {} while (0)
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * devres_alloc - Allocate device resource data
|
|---|
| 80 | * @release: Release function devres will be associated with
|
|---|
| 81 | * @size: Allocation size
|
|---|
| 82 | * @gfp: Allocation flags
|
|---|
| 83 | * @nid: NUMA node
|
|---|
| 84 | *
|
|---|
| 85 | * Allocate devres of @size bytes. The allocated area is zeroed, then
|
|---|
| 86 | * associated with @release. The returned pointer can be passed to
|
|---|
| 87 | * other devres_*() functions.
|
|---|
| 88 | *
|
|---|
| 89 | * RETURNS:
|
|---|
| 90 | * Pointer to allocated devres on success, NULL on failure.
|
|---|
| 91 | */
|
|---|
| 92 | void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
|
|---|
| 93 | {
|
|---|
| 94 | struct devres *dr;
|
|---|
| 95 |
|
|---|
| 96 | dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
|
|---|
| 97 | return dr->data;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * devres_free - Free device resource data
|
|---|
| 102 | * @res: Pointer to devres data to free
|
|---|
| 103 | *
|
|---|
| 104 | * Free devres created with devres_alloc().
|
|---|
| 105 | */
|
|---|
| 106 | void devres_free(void *res)
|
|---|
| 107 | {
|
|---|
| 108 | if (res) {
|
|---|
| 109 | struct devres *dr = container_of(res, struct devres, data);
|
|---|
| 110 |
|
|---|
| 111 | BUG_ON(!list_empty(&dr->node.entry));
|
|---|
| 112 | kfree(dr);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int remove_nodes(struct device *dev,
|
|---|
| 117 | struct list_head *first, struct list_head *end,
|
|---|
| 118 | struct list_head *todo)
|
|---|
| 119 | {
|
|---|
| 120 | int cnt = 0, nr_groups = 0;
|
|---|
| 121 | struct list_head *cur;
|
|---|
| 122 |
|
|---|
| 123 | /* First pass - move normal devres entries to @todo and clear
|
|---|
| 124 | * devres_group colors.
|
|---|
| 125 | */
|
|---|
| 126 | cur = first;
|
|---|
| 127 | while (cur != end) {
|
|---|
| 128 | struct devres_node *node;
|
|---|
| 129 | struct devres_group *grp;
|
|---|
| 130 |
|
|---|
| 131 | node = list_entry(cur, struct devres_node, entry);
|
|---|
| 132 | cur = cur->next;
|
|---|
| 133 |
|
|---|
| 134 | grp = node_to_group(node);
|
|---|
| 135 | if (grp) {
|
|---|
| 136 | /* clear color of group markers in the first pass */
|
|---|
| 137 | grp->color = 0;
|
|---|
| 138 | nr_groups++;
|
|---|
| 139 | } else {
|
|---|
| 140 | /* regular devres entry */
|
|---|
| 141 | if (&node->entry == first)
|
|---|
| 142 | first = first->next;
|
|---|
| 143 | list_move_tail(&node->entry, todo);
|
|---|
| 144 | cnt++;
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | if (!nr_groups)
|
|---|
| 149 | return cnt;
|
|---|
| 150 |
|
|---|
| 151 | /* Second pass - Scan groups and color them. A group gets
|
|---|
| 152 | * color value of two iff the group is wholly contained in
|
|---|
| 153 | * [cur, end). That is, for a closed group, both opening and
|
|---|
| 154 | * closing markers should be in the range, while just the
|
|---|
| 155 | * opening marker is enough for an open group.
|
|---|
| 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 | BUG_ON(!grp || list_empty(&grp->node[0].entry));
|
|---|
| 167 |
|
|---|
| 168 | grp->color++;
|
|---|
| 169 | if (list_empty(&grp->node[1].entry))
|
|---|
| 170 | grp->color++;
|
|---|
| 171 |
|
|---|
| 172 | BUG_ON(grp->color <= 0 || grp->color > 2);
|
|---|
| 173 | if (grp->color == 2) {
|
|---|
| 174 | /* No need to update cur or end. The removed
|
|---|
| 175 | * nodes are always before both.
|
|---|
| 176 | */
|
|---|
| 177 | list_move_tail(&grp->node[0].entry, todo);
|
|---|
| 178 | list_del_init(&grp->node[1].entry);
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | return cnt;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | static int release_nodes(struct device *dev, struct list_head *first,
|
|---|
| 186 | struct list_head *end, unsigned long flags)
|
|---|
| 187 | {
|
|---|
| 188 | // LIST_HEAD(todo);
|
|---|
| 189 | struct list_head todo;
|
|---|
| 190 |
|
|---|
| 191 | int cnt;
|
|---|
| 192 | struct devres *dr, *tmp;
|
|---|
| 193 |
|
|---|
| 194 | cnt = remove_nodes(dev, first, end, &todo);
|
|---|
| 195 |
|
|---|
| 196 | spin_unlock_irqrestore(&dev->devres_lock, flags);
|
|---|
| 197 |
|
|---|
| 198 | /* Release. Note that both devres and devres_group are
|
|---|
| 199 | * handled as devres in the following loop. This is safe.
|
|---|
| 200 | */
|
|---|
| 201 | list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry, struct devres) {
|
|---|
| 202 | devres_log(dev, &dr->node, "REL");
|
|---|
| 203 | dr->node.release(dev, dr->data);
|
|---|
| 204 | kfree(dr);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | return cnt;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * devres_release_all - Release all managed resources
|
|---|
| 212 | * @dev: Device to release resources for
|
|---|
| 213 | *
|
|---|
| 214 | * Release all resources associated with @dev. This function is
|
|---|
| 215 | * called on driver detach.
|
|---|
| 216 | */
|
|---|
| 217 | int devres_release_all(struct device *dev)
|
|---|
| 218 | {
|
|---|
| 219 | unsigned long flags;
|
|---|
| 220 |
|
|---|
| 221 | /* Looks like an uninitialized device structure */
|
|---|
| 222 | if (WARN_ON(dev->devres_head.next == NULL))
|
|---|
| 223 | return -ENODEV;
|
|---|
| 224 | spin_lock_irqsave(&dev->devres_lock, flags);
|
|---|
| 225 | return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
|
|---|
| 226 | flags);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | static struct devres *find_dr(struct device *dev, dr_release_t release,
|
|---|
| 230 | dr_match_t match, void *match_data)
|
|---|
| 231 | {
|
|---|
| 232 | struct devres_node *node;
|
|---|
| 233 |
|
|---|
| 234 | list_for_each_entry_reverse(node, &dev->devres_head, entry, struct devres_node) {
|
|---|
| 235 | struct devres *dr = container_of(node, struct devres, node);
|
|---|
| 236 |
|
|---|
| 237 | if (node->release != release)
|
|---|
| 238 | continue;
|
|---|
| 239 | if (match && !match(dev, dr->data, match_data))
|
|---|
| 240 | continue;
|
|---|
| 241 | return dr;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | return NULL;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /**
|
|---|
| 248 | * devres_find - Find device resource
|
|---|
| 249 | * @dev: Device to lookup resource from
|
|---|
| 250 | * @release: Look for resources associated with this release function
|
|---|
| 251 | * @match: Match function (optional)
|
|---|
| 252 | * @match_data: Data for the match function
|
|---|
| 253 | *
|
|---|
| 254 | * Find the latest devres of @dev which is associated with @release
|
|---|
| 255 | * and for which @match returns 1. If @match is NULL, it's considered
|
|---|
| 256 | * to match all.
|
|---|
| 257 | *
|
|---|
| 258 | * RETURNS:
|
|---|
| 259 | * Pointer to found devres, NULL if not found.
|
|---|
| 260 | */
|
|---|
| 261 | void * devres_find(struct device *dev, dr_release_t release,
|
|---|
| 262 | dr_match_t match, void *match_data)
|
|---|
| 263 | {
|
|---|
| 264 | struct devres *dr;
|
|---|
| 265 | unsigned long flags;
|
|---|
| 266 |
|
|---|
| 267 | spin_lock_irqsave(&dev->devres_lock, flags);
|
|---|
| 268 | dr = find_dr(dev, release, match, match_data);
|
|---|
| 269 | spin_unlock_irqrestore(&dev->devres_lock, flags);
|
|---|
| 270 |
|
|---|
| 271 | if (dr)
|
|---|
| 272 | return dr->data;
|
|---|
| 273 | return NULL;
|
|---|
| 274 | }
|
|---|