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