driver core: add SPDX identifiers to all driver core files
[linux-block.git] / drivers / base / component.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Componentized device handling.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This is work in progress.  We gather up the component devices into a list,
10  * and bind them when instructed.  At the moment, we're specific to the DRM
11  * subsystem, and only handles one master device, but this doesn't have to be
12  * the case.
13  */
14 #include <linux/component.h>
15 #include <linux/device.h>
16 #include <linux/kref.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21
22 struct component;
23
24 struct component_match_array {
25         void *data;
26         int (*compare)(struct device *, void *);
27         void (*release)(struct device *, void *);
28         struct component *component;
29         bool duplicate;
30 };
31
32 struct component_match {
33         size_t alloc;
34         size_t num;
35         struct component_match_array *compare;
36 };
37
38 struct master {
39         struct list_head node;
40         bool bound;
41
42         const struct component_master_ops *ops;
43         struct device *dev;
44         struct component_match *match;
45 };
46
47 struct component {
48         struct list_head node;
49         struct master *master;
50         bool bound;
51
52         const struct component_ops *ops;
53         struct device *dev;
54 };
55
56 static DEFINE_MUTEX(component_mutex);
57 static LIST_HEAD(component_list);
58 static LIST_HEAD(masters);
59
60 static struct master *__master_find(struct device *dev,
61         const struct component_master_ops *ops)
62 {
63         struct master *m;
64
65         list_for_each_entry(m, &masters, node)
66                 if (m->dev == dev && (!ops || m->ops == ops))
67                         return m;
68
69         return NULL;
70 }
71
72 static struct component *find_component(struct master *master,
73         int (*compare)(struct device *, void *), void *compare_data)
74 {
75         struct component *c;
76
77         list_for_each_entry(c, &component_list, node) {
78                 if (c->master && c->master != master)
79                         continue;
80
81                 if (compare(c->dev, compare_data))
82                         return c;
83         }
84
85         return NULL;
86 }
87
88 static int find_components(struct master *master)
89 {
90         struct component_match *match = master->match;
91         size_t i;
92         int ret = 0;
93
94         /*
95          * Scan the array of match functions and attach
96          * any components which are found to this master.
97          */
98         for (i = 0; i < match->num; i++) {
99                 struct component_match_array *mc = &match->compare[i];
100                 struct component *c;
101
102                 dev_dbg(master->dev, "Looking for component %zu\n", i);
103
104                 if (match->compare[i].component)
105                         continue;
106
107                 c = find_component(master, mc->compare, mc->data);
108                 if (!c) {
109                         ret = -ENXIO;
110                         break;
111                 }
112
113                 dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
114
115                 /* Attach this component to the master */
116                 match->compare[i].duplicate = !!c->master;
117                 match->compare[i].component = c;
118                 c->master = master;
119         }
120         return ret;
121 }
122
123 /* Detach component from associated master */
124 static void remove_component(struct master *master, struct component *c)
125 {
126         size_t i;
127
128         /* Detach the component from this master. */
129         for (i = 0; i < master->match->num; i++)
130                 if (master->match->compare[i].component == c)
131                         master->match->compare[i].component = NULL;
132 }
133
134 /*
135  * Try to bring up a master.  If component is NULL, we're interested in
136  * this master, otherwise it's a component which must be present to try
137  * and bring up the master.
138  *
139  * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
140  */
141 static int try_to_bring_up_master(struct master *master,
142         struct component *component)
143 {
144         int ret;
145
146         dev_dbg(master->dev, "trying to bring up master\n");
147
148         if (find_components(master)) {
149                 dev_dbg(master->dev, "master has incomplete components\n");
150                 return 0;
151         }
152
153         if (component && component->master != master) {
154                 dev_dbg(master->dev, "master is not for this component (%s)\n",
155                         dev_name(component->dev));
156                 return 0;
157         }
158
159         if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
160                 return -ENOMEM;
161
162         /* Found all components */
163         ret = master->ops->bind(master->dev);
164         if (ret < 0) {
165                 devres_release_group(master->dev, NULL);
166                 dev_info(master->dev, "master bind failed: %d\n", ret);
167                 return ret;
168         }
169
170         master->bound = true;
171         return 1;
172 }
173
174 static int try_to_bring_up_masters(struct component *component)
175 {
176         struct master *m;
177         int ret = 0;
178
179         list_for_each_entry(m, &masters, node) {
180                 if (!m->bound) {
181                         ret = try_to_bring_up_master(m, component);
182                         if (ret != 0)
183                                 break;
184                 }
185         }
186
187         return ret;
188 }
189
190 static void take_down_master(struct master *master)
191 {
192         if (master->bound) {
193                 master->ops->unbind(master->dev);
194                 devres_release_group(master->dev, NULL);
195                 master->bound = false;
196         }
197 }
198
199 static void component_match_release(struct device *master,
200         struct component_match *match)
201 {
202         unsigned int i;
203
204         for (i = 0; i < match->num; i++) {
205                 struct component_match_array *mc = &match->compare[i];
206
207                 if (mc->release)
208                         mc->release(master, mc->data);
209         }
210
211         kfree(match->compare);
212 }
213
214 static void devm_component_match_release(struct device *dev, void *res)
215 {
216         component_match_release(dev, res);
217 }
218
219 static int component_match_realloc(struct device *dev,
220         struct component_match *match, size_t num)
221 {
222         struct component_match_array *new;
223
224         if (match->alloc == num)
225                 return 0;
226
227         new = kmalloc_array(num, sizeof(*new), GFP_KERNEL);
228         if (!new)
229                 return -ENOMEM;
230
231         if (match->compare) {
232                 memcpy(new, match->compare, sizeof(*new) *
233                                             min(match->num, num));
234                 kfree(match->compare);
235         }
236         match->compare = new;
237         match->alloc = num;
238
239         return 0;
240 }
241
242 /*
243  * Add a component to be matched, with a release function.
244  *
245  * The match array is first created or extended if necessary.
246  */
247 void component_match_add_release(struct device *master,
248         struct component_match **matchptr,
249         void (*release)(struct device *, void *),
250         int (*compare)(struct device *, void *), void *compare_data)
251 {
252         struct component_match *match = *matchptr;
253
254         if (IS_ERR(match))
255                 return;
256
257         if (!match) {
258                 match = devres_alloc(devm_component_match_release,
259                                      sizeof(*match), GFP_KERNEL);
260                 if (!match) {
261                         *matchptr = ERR_PTR(-ENOMEM);
262                         return;
263                 }
264
265                 devres_add(master, match);
266
267                 *matchptr = match;
268         }
269
270         if (match->num == match->alloc) {
271                 size_t new_size = match->alloc + 16;
272                 int ret;
273
274                 ret = component_match_realloc(master, match, new_size);
275                 if (ret) {
276                         *matchptr = ERR_PTR(ret);
277                         return;
278                 }
279         }
280
281         match->compare[match->num].compare = compare;
282         match->compare[match->num].release = release;
283         match->compare[match->num].data = compare_data;
284         match->compare[match->num].component = NULL;
285         match->num++;
286 }
287 EXPORT_SYMBOL(component_match_add_release);
288
289 static void free_master(struct master *master)
290 {
291         struct component_match *match = master->match;
292         int i;
293
294         list_del(&master->node);
295
296         if (match) {
297                 for (i = 0; i < match->num; i++) {
298                         struct component *c = match->compare[i].component;
299                         if (c)
300                                 c->master = NULL;
301                 }
302         }
303
304         kfree(master);
305 }
306
307 int component_master_add_with_match(struct device *dev,
308         const struct component_master_ops *ops,
309         struct component_match *match)
310 {
311         struct master *master;
312         int ret;
313
314         /* Reallocate the match array for its true size */
315         ret = component_match_realloc(dev, match, match->num);
316         if (ret)
317                 return ret;
318
319         master = kzalloc(sizeof(*master), GFP_KERNEL);
320         if (!master)
321                 return -ENOMEM;
322
323         master->dev = dev;
324         master->ops = ops;
325         master->match = match;
326
327         /* Add to the list of available masters. */
328         mutex_lock(&component_mutex);
329         list_add(&master->node, &masters);
330
331         ret = try_to_bring_up_master(master, NULL);
332
333         if (ret < 0)
334                 free_master(master);
335
336         mutex_unlock(&component_mutex);
337
338         return ret < 0 ? ret : 0;
339 }
340 EXPORT_SYMBOL_GPL(component_master_add_with_match);
341
342 void component_master_del(struct device *dev,
343         const struct component_master_ops *ops)
344 {
345         struct master *master;
346
347         mutex_lock(&component_mutex);
348         master = __master_find(dev, ops);
349         if (master) {
350                 take_down_master(master);
351                 free_master(master);
352         }
353         mutex_unlock(&component_mutex);
354 }
355 EXPORT_SYMBOL_GPL(component_master_del);
356
357 static void component_unbind(struct component *component,
358         struct master *master, void *data)
359 {
360         WARN_ON(!component->bound);
361
362         component->ops->unbind(component->dev, master->dev, data);
363         component->bound = false;
364
365         /* Release all resources claimed in the binding of this component */
366         devres_release_group(component->dev, component);
367 }
368
369 void component_unbind_all(struct device *master_dev, void *data)
370 {
371         struct master *master;
372         struct component *c;
373         size_t i;
374
375         WARN_ON(!mutex_is_locked(&component_mutex));
376
377         master = __master_find(master_dev, NULL);
378         if (!master)
379                 return;
380
381         /* Unbind components in reverse order */
382         for (i = master->match->num; i--; )
383                 if (!master->match->compare[i].duplicate) {
384                         c = master->match->compare[i].component;
385                         component_unbind(c, master, data);
386                 }
387 }
388 EXPORT_SYMBOL_GPL(component_unbind_all);
389
390 static int component_bind(struct component *component, struct master *master,
391         void *data)
392 {
393         int ret;
394
395         /*
396          * Each component initialises inside its own devres group.
397          * This allows us to roll-back a failed component without
398          * affecting anything else.
399          */
400         if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
401                 return -ENOMEM;
402
403         /*
404          * Also open a group for the device itself: this allows us
405          * to release the resources claimed against the sub-device
406          * at the appropriate moment.
407          */
408         if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
409                 devres_release_group(master->dev, NULL);
410                 return -ENOMEM;
411         }
412
413         dev_dbg(master->dev, "binding %s (ops %ps)\n",
414                 dev_name(component->dev), component->ops);
415
416         ret = component->ops->bind(component->dev, master->dev, data);
417         if (!ret) {
418                 component->bound = true;
419
420                 /*
421                  * Close the component device's group so that resources
422                  * allocated in the binding are encapsulated for removal
423                  * at unbind.  Remove the group on the DRM device as we
424                  * can clean those resources up independently.
425                  */
426                 devres_close_group(component->dev, NULL);
427                 devres_remove_group(master->dev, NULL);
428
429                 dev_info(master->dev, "bound %s (ops %ps)\n",
430                          dev_name(component->dev), component->ops);
431         } else {
432                 devres_release_group(component->dev, NULL);
433                 devres_release_group(master->dev, NULL);
434
435                 dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
436                         dev_name(component->dev), component->ops, ret);
437         }
438
439         return ret;
440 }
441
442 int component_bind_all(struct device *master_dev, void *data)
443 {
444         struct master *master;
445         struct component *c;
446         size_t i;
447         int ret = 0;
448
449         WARN_ON(!mutex_is_locked(&component_mutex));
450
451         master = __master_find(master_dev, NULL);
452         if (!master)
453                 return -EINVAL;
454
455         /* Bind components in match order */
456         for (i = 0; i < master->match->num; i++)
457                 if (!master->match->compare[i].duplicate) {
458                         c = master->match->compare[i].component;
459                         ret = component_bind(c, master, data);
460                         if (ret)
461                                 break;
462                 }
463
464         if (ret != 0) {
465                 for (; i--; )
466                         if (!master->match->compare[i].duplicate) {
467                                 c = master->match->compare[i].component;
468                                 component_unbind(c, master, data);
469                         }
470         }
471
472         return ret;
473 }
474 EXPORT_SYMBOL_GPL(component_bind_all);
475
476 int component_add(struct device *dev, const struct component_ops *ops)
477 {
478         struct component *component;
479         int ret;
480
481         component = kzalloc(sizeof(*component), GFP_KERNEL);
482         if (!component)
483                 return -ENOMEM;
484
485         component->ops = ops;
486         component->dev = dev;
487
488         dev_dbg(dev, "adding component (ops %ps)\n", ops);
489
490         mutex_lock(&component_mutex);
491         list_add_tail(&component->node, &component_list);
492
493         ret = try_to_bring_up_masters(component);
494         if (ret < 0) {
495                 if (component->master)
496                         remove_component(component->master, component);
497                 list_del(&component->node);
498
499                 kfree(component);
500         }
501         mutex_unlock(&component_mutex);
502
503         return ret < 0 ? ret : 0;
504 }
505 EXPORT_SYMBOL_GPL(component_add);
506
507 void component_del(struct device *dev, const struct component_ops *ops)
508 {
509         struct component *c, *component = NULL;
510
511         mutex_lock(&component_mutex);
512         list_for_each_entry(c, &component_list, node)
513                 if (c->dev == dev && c->ops == ops) {
514                         list_del(&c->node);
515                         component = c;
516                         break;
517                 }
518
519         if (component && component->master) {
520                 take_down_master(component->master);
521                 remove_component(component->master, component);
522         }
523
524         mutex_unlock(&component_mutex);
525
526         WARN_ON(!component);
527         kfree(component);
528 }
529 EXPORT_SYMBOL_GPL(component_del);
530
531 MODULE_LICENSE("GPL v2");