xref: /openbmc/linux/drivers/gpu/host1x/bus.c (revision cfbb9be8)
1 /*
2  * Copyright (C) 2012 Avionic Design GmbH
3  * Copyright (C) 2012-2013, NVIDIA Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <linux/host1x.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21 #include <linux/of_device.h>
22 
23 #include "bus.h"
24 #include "dev.h"
25 
26 static DEFINE_MUTEX(clients_lock);
27 static LIST_HEAD(clients);
28 
29 static DEFINE_MUTEX(drivers_lock);
30 static LIST_HEAD(drivers);
31 
32 static DEFINE_MUTEX(devices_lock);
33 static LIST_HEAD(devices);
34 
35 struct host1x_subdev {
36 	struct host1x_client *client;
37 	struct device_node *np;
38 	struct list_head list;
39 };
40 
41 /**
42  * host1x_subdev_add() - add a new subdevice with an associated device node
43  * @device: host1x device to add the subdevice to
44  * @np: device node
45  */
46 static int host1x_subdev_add(struct host1x_device *device,
47 			     struct host1x_driver *driver,
48 			     struct device_node *np)
49 {
50 	struct host1x_subdev *subdev;
51 	struct device_node *child;
52 	int err;
53 
54 	subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
55 	if (!subdev)
56 		return -ENOMEM;
57 
58 	INIT_LIST_HEAD(&subdev->list);
59 	subdev->np = of_node_get(np);
60 
61 	mutex_lock(&device->subdevs_lock);
62 	list_add_tail(&subdev->list, &device->subdevs);
63 	mutex_unlock(&device->subdevs_lock);
64 
65 	/* recursively add children */
66 	for_each_child_of_node(np, child) {
67 		if (of_match_node(driver->subdevs, child) &&
68 		    of_device_is_available(child)) {
69 			err = host1x_subdev_add(device, driver, child);
70 			if (err < 0) {
71 				/* XXX cleanup? */
72 				of_node_put(child);
73 				return err;
74 			}
75 		}
76 	}
77 
78 	return 0;
79 }
80 
81 /**
82  * host1x_subdev_del() - remove subdevice
83  * @subdev: subdevice to remove
84  */
85 static void host1x_subdev_del(struct host1x_subdev *subdev)
86 {
87 	list_del(&subdev->list);
88 	of_node_put(subdev->np);
89 	kfree(subdev);
90 }
91 
92 /**
93  * host1x_device_parse_dt() - scan device tree and add matching subdevices
94  * @device: host1x logical device
95  * @driver: host1x driver
96  */
97 static int host1x_device_parse_dt(struct host1x_device *device,
98 				  struct host1x_driver *driver)
99 {
100 	struct device_node *np;
101 	int err;
102 
103 	for_each_child_of_node(device->dev.parent->of_node, np) {
104 		if (of_match_node(driver->subdevs, np) &&
105 		    of_device_is_available(np)) {
106 			err = host1x_subdev_add(device, driver, np);
107 			if (err < 0) {
108 				of_node_put(np);
109 				return err;
110 			}
111 		}
112 	}
113 
114 	return 0;
115 }
116 
117 static void host1x_subdev_register(struct host1x_device *device,
118 				   struct host1x_subdev *subdev,
119 				   struct host1x_client *client)
120 {
121 	int err;
122 
123 	/*
124 	 * Move the subdevice to the list of active (registered) subdevices
125 	 * and associate it with a client. At the same time, associate the
126 	 * client with its parent device.
127 	 */
128 	mutex_lock(&device->subdevs_lock);
129 	mutex_lock(&device->clients_lock);
130 	list_move_tail(&client->list, &device->clients);
131 	list_move_tail(&subdev->list, &device->active);
132 	client->parent = &device->dev;
133 	subdev->client = client;
134 	mutex_unlock(&device->clients_lock);
135 	mutex_unlock(&device->subdevs_lock);
136 
137 	if (list_empty(&device->subdevs)) {
138 		err = device_add(&device->dev);
139 		if (err < 0)
140 			dev_err(&device->dev, "failed to add: %d\n", err);
141 		else
142 			device->registered = true;
143 	}
144 }
145 
146 static void __host1x_subdev_unregister(struct host1x_device *device,
147 				       struct host1x_subdev *subdev)
148 {
149 	struct host1x_client *client = subdev->client;
150 
151 	/*
152 	 * If all subdevices have been activated, we're about to remove the
153 	 * first active subdevice, so unload the driver first.
154 	 */
155 	if (list_empty(&device->subdevs)) {
156 		if (device->registered) {
157 			device->registered = false;
158 			device_del(&device->dev);
159 		}
160 	}
161 
162 	/*
163 	 * Move the subdevice back to the list of idle subdevices and remove
164 	 * it from list of clients.
165 	 */
166 	mutex_lock(&device->clients_lock);
167 	subdev->client = NULL;
168 	client->parent = NULL;
169 	list_move_tail(&subdev->list, &device->subdevs);
170 	/*
171 	 * XXX: Perhaps don't do this here, but rather explicitly remove it
172 	 * when the device is about to be deleted.
173 	 *
174 	 * This is somewhat complicated by the fact that this function is
175 	 * used to remove the subdevice when a client is unregistered but
176 	 * also when the composite device is about to be removed.
177 	 */
178 	list_del_init(&client->list);
179 	mutex_unlock(&device->clients_lock);
180 }
181 
182 static void host1x_subdev_unregister(struct host1x_device *device,
183 				     struct host1x_subdev *subdev)
184 {
185 	mutex_lock(&device->subdevs_lock);
186 	__host1x_subdev_unregister(device, subdev);
187 	mutex_unlock(&device->subdevs_lock);
188 }
189 
190 /**
191  * host1x_device_init() - initialize a host1x logical device
192  * @device: host1x logical device
193  *
194  * The driver for the host1x logical device can call this during execution of
195  * its &host1x_driver.probe implementation to initialize each of its clients.
196  * The client drivers access the subsystem specific driver data using the
197  * &host1x_client.parent field and driver data associated with it (usually by
198  * calling dev_get_drvdata()).
199  */
200 int host1x_device_init(struct host1x_device *device)
201 {
202 	struct host1x_client *client;
203 	int err;
204 
205 	mutex_lock(&device->clients_lock);
206 
207 	list_for_each_entry(client, &device->clients, list) {
208 		if (client->ops && client->ops->init) {
209 			err = client->ops->init(client);
210 			if (err < 0) {
211 				dev_err(&device->dev,
212 					"failed to initialize %s: %d\n",
213 					dev_name(client->dev), err);
214 				goto teardown;
215 			}
216 		}
217 	}
218 
219 	mutex_unlock(&device->clients_lock);
220 
221 	return 0;
222 
223 teardown:
224 	list_for_each_entry_continue_reverse(client, &device->clients, list)
225 		if (client->ops->exit)
226 			client->ops->exit(client);
227 
228 	mutex_unlock(&device->clients_lock);
229 	return err;
230 }
231 EXPORT_SYMBOL(host1x_device_init);
232 
233 /**
234  * host1x_device_exit() - uninitialize host1x logical device
235  * @device: host1x logical device
236  *
237  * When the driver for a host1x logical device is unloaded, it can call this
238  * function to tear down each of its clients. Typically this is done after a
239  * subsystem-specific data structure is removed and the functionality can no
240  * longer be used.
241  */
242 int host1x_device_exit(struct host1x_device *device)
243 {
244 	struct host1x_client *client;
245 	int err;
246 
247 	mutex_lock(&device->clients_lock);
248 
249 	list_for_each_entry_reverse(client, &device->clients, list) {
250 		if (client->ops && client->ops->exit) {
251 			err = client->ops->exit(client);
252 			if (err < 0) {
253 				dev_err(&device->dev,
254 					"failed to cleanup %s: %d\n",
255 					dev_name(client->dev), err);
256 				mutex_unlock(&device->clients_lock);
257 				return err;
258 			}
259 		}
260 	}
261 
262 	mutex_unlock(&device->clients_lock);
263 
264 	return 0;
265 }
266 EXPORT_SYMBOL(host1x_device_exit);
267 
268 static int host1x_add_client(struct host1x *host1x,
269 			     struct host1x_client *client)
270 {
271 	struct host1x_device *device;
272 	struct host1x_subdev *subdev;
273 
274 	mutex_lock(&host1x->devices_lock);
275 
276 	list_for_each_entry(device, &host1x->devices, list) {
277 		list_for_each_entry(subdev, &device->subdevs, list) {
278 			if (subdev->np == client->dev->of_node) {
279 				host1x_subdev_register(device, subdev, client);
280 				mutex_unlock(&host1x->devices_lock);
281 				return 0;
282 			}
283 		}
284 	}
285 
286 	mutex_unlock(&host1x->devices_lock);
287 	return -ENODEV;
288 }
289 
290 static int host1x_del_client(struct host1x *host1x,
291 			     struct host1x_client *client)
292 {
293 	struct host1x_device *device, *dt;
294 	struct host1x_subdev *subdev;
295 
296 	mutex_lock(&host1x->devices_lock);
297 
298 	list_for_each_entry_safe(device, dt, &host1x->devices, list) {
299 		list_for_each_entry(subdev, &device->active, list) {
300 			if (subdev->client == client) {
301 				host1x_subdev_unregister(device, subdev);
302 				mutex_unlock(&host1x->devices_lock);
303 				return 0;
304 			}
305 		}
306 	}
307 
308 	mutex_unlock(&host1x->devices_lock);
309 	return -ENODEV;
310 }
311 
312 static int host1x_device_match(struct device *dev, struct device_driver *drv)
313 {
314 	return strcmp(dev_name(dev), drv->name) == 0;
315 }
316 
317 static const struct dev_pm_ops host1x_device_pm_ops = {
318 	.suspend = pm_generic_suspend,
319 	.resume = pm_generic_resume,
320 	.freeze = pm_generic_freeze,
321 	.thaw = pm_generic_thaw,
322 	.poweroff = pm_generic_poweroff,
323 	.restore = pm_generic_restore,
324 };
325 
326 struct bus_type host1x_bus_type = {
327 	.name = "host1x",
328 	.match = host1x_device_match,
329 	.pm = &host1x_device_pm_ops,
330 	.force_dma = true,
331 };
332 
333 static void __host1x_device_del(struct host1x_device *device)
334 {
335 	struct host1x_subdev *subdev, *sd;
336 	struct host1x_client *client, *cl;
337 
338 	mutex_lock(&device->subdevs_lock);
339 
340 	/* unregister subdevices */
341 	list_for_each_entry_safe(subdev, sd, &device->active, list) {
342 		/*
343 		 * host1x_subdev_unregister() will remove the client from
344 		 * any lists, so we'll need to manually add it back to the
345 		 * list of idle clients.
346 		 *
347 		 * XXX: Alternatively, perhaps don't remove the client from
348 		 * any lists in host1x_subdev_unregister() and instead do
349 		 * that explicitly from host1x_unregister_client()?
350 		 */
351 		client = subdev->client;
352 
353 		__host1x_subdev_unregister(device, subdev);
354 
355 		/* add the client to the list of idle clients */
356 		mutex_lock(&clients_lock);
357 		list_add_tail(&client->list, &clients);
358 		mutex_unlock(&clients_lock);
359 	}
360 
361 	/* remove subdevices */
362 	list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
363 		host1x_subdev_del(subdev);
364 
365 	mutex_unlock(&device->subdevs_lock);
366 
367 	/* move clients to idle list */
368 	mutex_lock(&clients_lock);
369 	mutex_lock(&device->clients_lock);
370 
371 	list_for_each_entry_safe(client, cl, &device->clients, list)
372 		list_move_tail(&client->list, &clients);
373 
374 	mutex_unlock(&device->clients_lock);
375 	mutex_unlock(&clients_lock);
376 
377 	/* finally remove the device */
378 	list_del_init(&device->list);
379 }
380 
381 static void host1x_device_release(struct device *dev)
382 {
383 	struct host1x_device *device = to_host1x_device(dev);
384 
385 	__host1x_device_del(device);
386 	kfree(device);
387 }
388 
389 static int host1x_device_add(struct host1x *host1x,
390 			     struct host1x_driver *driver)
391 {
392 	struct host1x_client *client, *tmp;
393 	struct host1x_subdev *subdev;
394 	struct host1x_device *device;
395 	int err;
396 
397 	device = kzalloc(sizeof(*device), GFP_KERNEL);
398 	if (!device)
399 		return -ENOMEM;
400 
401 	device_initialize(&device->dev);
402 
403 	mutex_init(&device->subdevs_lock);
404 	INIT_LIST_HEAD(&device->subdevs);
405 	INIT_LIST_HEAD(&device->active);
406 	mutex_init(&device->clients_lock);
407 	INIT_LIST_HEAD(&device->clients);
408 	INIT_LIST_HEAD(&device->list);
409 	device->driver = driver;
410 
411 	device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
412 	device->dev.dma_mask = &device->dev.coherent_dma_mask;
413 	dev_set_name(&device->dev, "%s", driver->driver.name);
414 	device->dev.release = host1x_device_release;
415 	device->dev.of_node = host1x->dev->of_node;
416 	device->dev.bus = &host1x_bus_type;
417 	device->dev.parent = host1x->dev;
418 
419 	of_dma_configure(&device->dev, host1x->dev->of_node);
420 
421 	err = host1x_device_parse_dt(device, driver);
422 	if (err < 0) {
423 		kfree(device);
424 		return err;
425 	}
426 
427 	list_add_tail(&device->list, &host1x->devices);
428 
429 	mutex_lock(&clients_lock);
430 
431 	list_for_each_entry_safe(client, tmp, &clients, list) {
432 		list_for_each_entry(subdev, &device->subdevs, list) {
433 			if (subdev->np == client->dev->of_node) {
434 				host1x_subdev_register(device, subdev, client);
435 				break;
436 			}
437 		}
438 	}
439 
440 	mutex_unlock(&clients_lock);
441 
442 	return 0;
443 }
444 
445 /*
446  * Removes a device by first unregistering any subdevices and then removing
447  * itself from the list of devices.
448  *
449  * This function must be called with the host1x->devices_lock held.
450  */
451 static void host1x_device_del(struct host1x *host1x,
452 			      struct host1x_device *device)
453 {
454 	if (device->registered) {
455 		device->registered = false;
456 		device_del(&device->dev);
457 	}
458 
459 	put_device(&device->dev);
460 }
461 
462 static void host1x_attach_driver(struct host1x *host1x,
463 				 struct host1x_driver *driver)
464 {
465 	struct host1x_device *device;
466 	int err;
467 
468 	mutex_lock(&host1x->devices_lock);
469 
470 	list_for_each_entry(device, &host1x->devices, list) {
471 		if (device->driver == driver) {
472 			mutex_unlock(&host1x->devices_lock);
473 			return;
474 		}
475 	}
476 
477 	err = host1x_device_add(host1x, driver);
478 	if (err < 0)
479 		dev_err(host1x->dev, "failed to allocate device: %d\n", err);
480 
481 	mutex_unlock(&host1x->devices_lock);
482 }
483 
484 static void host1x_detach_driver(struct host1x *host1x,
485 				 struct host1x_driver *driver)
486 {
487 	struct host1x_device *device, *tmp;
488 
489 	mutex_lock(&host1x->devices_lock);
490 
491 	list_for_each_entry_safe(device, tmp, &host1x->devices, list)
492 		if (device->driver == driver)
493 			host1x_device_del(host1x, device);
494 
495 	mutex_unlock(&host1x->devices_lock);
496 }
497 
498 /**
499  * host1x_register() - register a host1x controller
500  * @host1x: host1x controller
501  *
502  * The host1x controller driver uses this to register a host1x controller with
503  * the infrastructure. Note that all Tegra SoC generations have only ever come
504  * with a single host1x instance, so this function is somewhat academic.
505  */
506 int host1x_register(struct host1x *host1x)
507 {
508 	struct host1x_driver *driver;
509 
510 	mutex_lock(&devices_lock);
511 	list_add_tail(&host1x->list, &devices);
512 	mutex_unlock(&devices_lock);
513 
514 	mutex_lock(&drivers_lock);
515 
516 	list_for_each_entry(driver, &drivers, list)
517 		host1x_attach_driver(host1x, driver);
518 
519 	mutex_unlock(&drivers_lock);
520 
521 	return 0;
522 }
523 
524 /**
525  * host1x_unregister() - unregister a host1x controller
526  * @host1x: host1x controller
527  *
528  * The host1x controller driver uses this to remove a host1x controller from
529  * the infrastructure.
530  */
531 int host1x_unregister(struct host1x *host1x)
532 {
533 	struct host1x_driver *driver;
534 
535 	mutex_lock(&drivers_lock);
536 
537 	list_for_each_entry(driver, &drivers, list)
538 		host1x_detach_driver(host1x, driver);
539 
540 	mutex_unlock(&drivers_lock);
541 
542 	mutex_lock(&devices_lock);
543 	list_del_init(&host1x->list);
544 	mutex_unlock(&devices_lock);
545 
546 	return 0;
547 }
548 
549 static int host1x_device_probe(struct device *dev)
550 {
551 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
552 	struct host1x_device *device = to_host1x_device(dev);
553 
554 	if (driver->probe)
555 		return driver->probe(device);
556 
557 	return 0;
558 }
559 
560 static int host1x_device_remove(struct device *dev)
561 {
562 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
563 	struct host1x_device *device = to_host1x_device(dev);
564 
565 	if (driver->remove)
566 		return driver->remove(device);
567 
568 	return 0;
569 }
570 
571 static void host1x_device_shutdown(struct device *dev)
572 {
573 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
574 	struct host1x_device *device = to_host1x_device(dev);
575 
576 	if (driver->shutdown)
577 		driver->shutdown(device);
578 }
579 
580 /**
581  * host1x_driver_register_full() - register a host1x driver
582  * @driver: host1x driver
583  * @owner: owner module
584  *
585  * Drivers for host1x logical devices call this function to register a driver
586  * with the infrastructure. Note that since these drive logical devices, the
587  * registration of the driver actually triggers tho logical device creation.
588  * A logical device will be created for each host1x instance.
589  */
590 int host1x_driver_register_full(struct host1x_driver *driver,
591 				struct module *owner)
592 {
593 	struct host1x *host1x;
594 
595 	INIT_LIST_HEAD(&driver->list);
596 
597 	mutex_lock(&drivers_lock);
598 	list_add_tail(&driver->list, &drivers);
599 	mutex_unlock(&drivers_lock);
600 
601 	mutex_lock(&devices_lock);
602 
603 	list_for_each_entry(host1x, &devices, list)
604 		host1x_attach_driver(host1x, driver);
605 
606 	mutex_unlock(&devices_lock);
607 
608 	driver->driver.bus = &host1x_bus_type;
609 	driver->driver.owner = owner;
610 	driver->driver.probe = host1x_device_probe;
611 	driver->driver.remove = host1x_device_remove;
612 	driver->driver.shutdown = host1x_device_shutdown;
613 
614 	return driver_register(&driver->driver);
615 }
616 EXPORT_SYMBOL(host1x_driver_register_full);
617 
618 /**
619  * host1x_driver_unregister() - unregister a host1x driver
620  * @driver: host1x driver
621  *
622  * Unbinds the driver from each of the host1x logical devices that it is
623  * bound to, effectively removing the subsystem devices that they represent.
624  */
625 void host1x_driver_unregister(struct host1x_driver *driver)
626 {
627 	driver_unregister(&driver->driver);
628 
629 	mutex_lock(&drivers_lock);
630 	list_del_init(&driver->list);
631 	mutex_unlock(&drivers_lock);
632 }
633 EXPORT_SYMBOL(host1x_driver_unregister);
634 
635 /**
636  * host1x_client_register() - register a host1x client
637  * @client: host1x client
638  *
639  * Registers a host1x client with each host1x controller instance. Note that
640  * each client will only match their parent host1x controller and will only be
641  * associated with that instance. Once all clients have been registered with
642  * their parent host1x controller, the infrastructure will set up the logical
643  * device and call host1x_device_init(), which will in turn call each client's
644  * &host1x_client_ops.init implementation.
645  */
646 int host1x_client_register(struct host1x_client *client)
647 {
648 	struct host1x *host1x;
649 	int err;
650 
651 	mutex_lock(&devices_lock);
652 
653 	list_for_each_entry(host1x, &devices, list) {
654 		err = host1x_add_client(host1x, client);
655 		if (!err) {
656 			mutex_unlock(&devices_lock);
657 			return 0;
658 		}
659 	}
660 
661 	mutex_unlock(&devices_lock);
662 
663 	mutex_lock(&clients_lock);
664 	list_add_tail(&client->list, &clients);
665 	mutex_unlock(&clients_lock);
666 
667 	return 0;
668 }
669 EXPORT_SYMBOL(host1x_client_register);
670 
671 /**
672  * host1x_client_unregister() - unregister a host1x client
673  * @client: host1x client
674  *
675  * Removes a host1x client from its host1x controller instance. If a logical
676  * device has already been initialized, it will be torn down.
677  */
678 int host1x_client_unregister(struct host1x_client *client)
679 {
680 	struct host1x_client *c;
681 	struct host1x *host1x;
682 	int err;
683 
684 	mutex_lock(&devices_lock);
685 
686 	list_for_each_entry(host1x, &devices, list) {
687 		err = host1x_del_client(host1x, client);
688 		if (!err) {
689 			mutex_unlock(&devices_lock);
690 			return 0;
691 		}
692 	}
693 
694 	mutex_unlock(&devices_lock);
695 	mutex_lock(&clients_lock);
696 
697 	list_for_each_entry(c, &clients, list) {
698 		if (c == client) {
699 			list_del_init(&c->list);
700 			break;
701 		}
702 	}
703 
704 	mutex_unlock(&clients_lock);
705 
706 	return 0;
707 }
708 EXPORT_SYMBOL(host1x_client_unregister);
709