1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Avionic Design GmbH
4 * Copyright (C) 2012-2013, NVIDIA Corporation
7 #include <linux/debugfs.h>
8 #include <linux/host1x.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
12 #include <linux/of_device.h>
17 static DEFINE_MUTEX(clients_lock);
18 static LIST_HEAD(clients);
20 static DEFINE_MUTEX(drivers_lock);
21 static LIST_HEAD(drivers);
23 static DEFINE_MUTEX(devices_lock);
24 static LIST_HEAD(devices);
26 struct host1x_subdev {
27 struct host1x_client *client;
28 struct device_node *np;
29 struct list_head list;
33 * host1x_subdev_add() - add a new subdevice with an associated device node
34 * @device: host1x device to add the subdevice to
37 static int host1x_subdev_add(struct host1x_device *device,
38 struct host1x_driver *driver,
39 struct device_node *np)
41 struct host1x_subdev *subdev;
42 struct device_node *child;
45 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
49 INIT_LIST_HEAD(&subdev->list);
50 subdev->np = of_node_get(np);
52 mutex_lock(&device->subdevs_lock);
53 list_add_tail(&subdev->list, &device->subdevs);
54 mutex_unlock(&device->subdevs_lock);
56 /* recursively add children */
57 for_each_child_of_node(np, child) {
58 if (of_match_node(driver->subdevs, child) &&
59 of_device_is_available(child)) {
60 err = host1x_subdev_add(device, driver, child);
73 * host1x_subdev_del() - remove subdevice
74 * @subdev: subdevice to remove
76 static void host1x_subdev_del(struct host1x_subdev *subdev)
78 list_del(&subdev->list);
79 of_node_put(subdev->np);
84 * host1x_device_parse_dt() - scan device tree and add matching subdevices
85 * @device: host1x logical device
86 * @driver: host1x driver
88 static int host1x_device_parse_dt(struct host1x_device *device,
89 struct host1x_driver *driver)
91 struct device_node *np;
94 for_each_child_of_node(device->dev.parent->of_node, np) {
95 if (of_match_node(driver->subdevs, np) &&
96 of_device_is_available(np)) {
97 err = host1x_subdev_add(device, driver, np);
108 static void host1x_subdev_register(struct host1x_device *device,
109 struct host1x_subdev *subdev,
110 struct host1x_client *client)
115 * Move the subdevice to the list of active (registered) subdevices
116 * and associate it with a client. At the same time, associate the
117 * client with its parent device.
119 mutex_lock(&device->subdevs_lock);
120 mutex_lock(&device->clients_lock);
121 list_move_tail(&client->list, &device->clients);
122 list_move_tail(&subdev->list, &device->active);
123 client->host = &device->dev;
124 subdev->client = client;
125 mutex_unlock(&device->clients_lock);
126 mutex_unlock(&device->subdevs_lock);
128 if (list_empty(&device->subdevs)) {
129 err = device_add(&device->dev);
131 dev_err(&device->dev, "failed to add: %d\n", err);
133 device->registered = true;
137 static void __host1x_subdev_unregister(struct host1x_device *device,
138 struct host1x_subdev *subdev)
140 struct host1x_client *client = subdev->client;
143 * If all subdevices have been activated, we're about to remove the
144 * first active subdevice, so unload the driver first.
146 if (list_empty(&device->subdevs)) {
147 if (device->registered) {
148 device->registered = false;
149 device_del(&device->dev);
154 * Move the subdevice back to the list of idle subdevices and remove
155 * it from list of clients.
157 mutex_lock(&device->clients_lock);
158 subdev->client = NULL;
160 list_move_tail(&subdev->list, &device->subdevs);
162 * XXX: Perhaps don't do this here, but rather explicitly remove it
163 * when the device is about to be deleted.
165 * This is somewhat complicated by the fact that this function is
166 * used to remove the subdevice when a client is unregistered but
167 * also when the composite device is about to be removed.
169 list_del_init(&client->list);
170 mutex_unlock(&device->clients_lock);
173 static void host1x_subdev_unregister(struct host1x_device *device,
174 struct host1x_subdev *subdev)
176 mutex_lock(&device->subdevs_lock);
177 __host1x_subdev_unregister(device, subdev);
178 mutex_unlock(&device->subdevs_lock);
182 * host1x_device_init() - initialize a host1x logical device
183 * @device: host1x logical device
185 * The driver for the host1x logical device can call this during execution of
186 * its &host1x_driver.probe implementation to initialize each of its clients.
187 * The client drivers access the subsystem specific driver data using the
188 * &host1x_client.parent field and driver data associated with it (usually by
189 * calling dev_get_drvdata()).
191 int host1x_device_init(struct host1x_device *device)
193 struct host1x_client *client;
196 mutex_lock(&device->clients_lock);
198 list_for_each_entry(client, &device->clients, list) {
199 if (client->ops && client->ops->init) {
200 err = client->ops->init(client);
202 dev_err(&device->dev,
203 "failed to initialize %s: %d\n",
204 dev_name(client->dev), err);
210 mutex_unlock(&device->clients_lock);
215 list_for_each_entry_continue_reverse(client, &device->clients, list)
216 if (client->ops->exit)
217 client->ops->exit(client);
219 mutex_unlock(&device->clients_lock);
222 EXPORT_SYMBOL(host1x_device_init);
225 * host1x_device_exit() - uninitialize host1x logical device
226 * @device: host1x logical device
228 * When the driver for a host1x logical device is unloaded, it can call this
229 * function to tear down each of its clients. Typically this is done after a
230 * subsystem-specific data structure is removed and the functionality can no
233 int host1x_device_exit(struct host1x_device *device)
235 struct host1x_client *client;
238 mutex_lock(&device->clients_lock);
240 list_for_each_entry_reverse(client, &device->clients, list) {
241 if (client->ops && client->ops->exit) {
242 err = client->ops->exit(client);
244 dev_err(&device->dev,
245 "failed to cleanup %s: %d\n",
246 dev_name(client->dev), err);
247 mutex_unlock(&device->clients_lock);
253 mutex_unlock(&device->clients_lock);
257 EXPORT_SYMBOL(host1x_device_exit);
259 static int host1x_add_client(struct host1x *host1x,
260 struct host1x_client *client)
262 struct host1x_device *device;
263 struct host1x_subdev *subdev;
265 mutex_lock(&host1x->devices_lock);
267 list_for_each_entry(device, &host1x->devices, list) {
268 list_for_each_entry(subdev, &device->subdevs, list) {
269 if (subdev->np == client->dev->of_node) {
270 host1x_subdev_register(device, subdev, client);
271 mutex_unlock(&host1x->devices_lock);
277 mutex_unlock(&host1x->devices_lock);
281 static int host1x_del_client(struct host1x *host1x,
282 struct host1x_client *client)
284 struct host1x_device *device, *dt;
285 struct host1x_subdev *subdev;
287 mutex_lock(&host1x->devices_lock);
289 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
290 list_for_each_entry(subdev, &device->active, list) {
291 if (subdev->client == client) {
292 host1x_subdev_unregister(device, subdev);
293 mutex_unlock(&host1x->devices_lock);
299 mutex_unlock(&host1x->devices_lock);
303 static int host1x_device_match(struct device *dev, struct device_driver *drv)
305 return strcmp(dev_name(dev), drv->name) == 0;
308 static int host1x_device_uevent(struct device *dev,
309 struct kobj_uevent_env *env)
311 struct device_node *np = dev->parent->of_node;
312 unsigned int count = 0;
317 * This duplicates most of of_device_uevent(), but the latter cannot
318 * be called from modules and operates on dev->of_node, which is not
319 * available in this case.
321 * Note that this is really only needed for backwards compatibility
322 * with libdrm, which parses this information from sysfs and will
323 * fail if it can't find the OF_FULLNAME, specifically.
325 add_uevent_var(env, "OF_NAME=%pOFn", np);
326 add_uevent_var(env, "OF_FULLNAME=%pOF", np);
328 of_property_for_each_string(np, "compatible", p, compat) {
329 add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
333 add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
338 static const struct dev_pm_ops host1x_device_pm_ops = {
339 .suspend = pm_generic_suspend,
340 .resume = pm_generic_resume,
341 .freeze = pm_generic_freeze,
342 .thaw = pm_generic_thaw,
343 .poweroff = pm_generic_poweroff,
344 .restore = pm_generic_restore,
347 struct bus_type host1x_bus_type = {
349 .match = host1x_device_match,
350 .uevent = host1x_device_uevent,
351 .pm = &host1x_device_pm_ops,
354 static void __host1x_device_del(struct host1x_device *device)
356 struct host1x_subdev *subdev, *sd;
357 struct host1x_client *client, *cl;
359 mutex_lock(&device->subdevs_lock);
361 /* unregister subdevices */
362 list_for_each_entry_safe(subdev, sd, &device->active, list) {
364 * host1x_subdev_unregister() will remove the client from
365 * any lists, so we'll need to manually add it back to the
366 * list of idle clients.
368 * XXX: Alternatively, perhaps don't remove the client from
369 * any lists in host1x_subdev_unregister() and instead do
370 * that explicitly from host1x_unregister_client()?
372 client = subdev->client;
374 __host1x_subdev_unregister(device, subdev);
376 /* add the client to the list of idle clients */
377 mutex_lock(&clients_lock);
378 list_add_tail(&client->list, &clients);
379 mutex_unlock(&clients_lock);
382 /* remove subdevices */
383 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
384 host1x_subdev_del(subdev);
386 mutex_unlock(&device->subdevs_lock);
388 /* move clients to idle list */
389 mutex_lock(&clients_lock);
390 mutex_lock(&device->clients_lock);
392 list_for_each_entry_safe(client, cl, &device->clients, list)
393 list_move_tail(&client->list, &clients);
395 mutex_unlock(&device->clients_lock);
396 mutex_unlock(&clients_lock);
398 /* finally remove the device */
399 list_del_init(&device->list);
402 static void host1x_device_release(struct device *dev)
404 struct host1x_device *device = to_host1x_device(dev);
406 __host1x_device_del(device);
410 static int host1x_device_add(struct host1x *host1x,
411 struct host1x_driver *driver)
413 struct host1x_client *client, *tmp;
414 struct host1x_subdev *subdev;
415 struct host1x_device *device;
418 device = kzalloc(sizeof(*device), GFP_KERNEL);
422 device_initialize(&device->dev);
424 mutex_init(&device->subdevs_lock);
425 INIT_LIST_HEAD(&device->subdevs);
426 INIT_LIST_HEAD(&device->active);
427 mutex_init(&device->clients_lock);
428 INIT_LIST_HEAD(&device->clients);
429 INIT_LIST_HEAD(&device->list);
430 device->driver = driver;
432 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
433 device->dev.dma_mask = &device->dev.coherent_dma_mask;
434 dev_set_name(&device->dev, "%s", driver->driver.name);
435 device->dev.release = host1x_device_release;
436 device->dev.bus = &host1x_bus_type;
437 device->dev.parent = host1x->dev;
439 device->dev.dma_parms = &device->dma_parms;
440 dma_set_max_seg_size(&device->dev, UINT_MAX);
442 err = host1x_device_parse_dt(device, driver);
448 list_add_tail(&device->list, &host1x->devices);
450 mutex_lock(&clients_lock);
452 list_for_each_entry_safe(client, tmp, &clients, list) {
453 list_for_each_entry(subdev, &device->subdevs, list) {
454 if (subdev->np == client->dev->of_node) {
455 host1x_subdev_register(device, subdev, client);
461 mutex_unlock(&clients_lock);
467 * Removes a device by first unregistering any subdevices and then removing
468 * itself from the list of devices.
470 * This function must be called with the host1x->devices_lock held.
472 static void host1x_device_del(struct host1x *host1x,
473 struct host1x_device *device)
475 if (device->registered) {
476 device->registered = false;
477 device_del(&device->dev);
480 put_device(&device->dev);
483 static void host1x_attach_driver(struct host1x *host1x,
484 struct host1x_driver *driver)
486 struct host1x_device *device;
489 mutex_lock(&host1x->devices_lock);
491 list_for_each_entry(device, &host1x->devices, list) {
492 if (device->driver == driver) {
493 mutex_unlock(&host1x->devices_lock);
498 err = host1x_device_add(host1x, driver);
500 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
502 mutex_unlock(&host1x->devices_lock);
505 static void host1x_detach_driver(struct host1x *host1x,
506 struct host1x_driver *driver)
508 struct host1x_device *device, *tmp;
510 mutex_lock(&host1x->devices_lock);
512 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
513 if (device->driver == driver)
514 host1x_device_del(host1x, device);
516 mutex_unlock(&host1x->devices_lock);
519 static int host1x_devices_show(struct seq_file *s, void *data)
521 struct host1x *host1x = s->private;
522 struct host1x_device *device;
524 mutex_lock(&host1x->devices_lock);
526 list_for_each_entry(device, &host1x->devices, list) {
527 struct host1x_subdev *subdev;
529 seq_printf(s, "%s\n", dev_name(&device->dev));
531 mutex_lock(&device->subdevs_lock);
533 list_for_each_entry(subdev, &device->active, list)
534 seq_printf(s, " %pOFf: %s\n", subdev->np,
535 dev_name(subdev->client->dev));
537 list_for_each_entry(subdev, &device->subdevs, list)
538 seq_printf(s, " %pOFf:\n", subdev->np);
540 mutex_unlock(&device->subdevs_lock);
543 mutex_unlock(&host1x->devices_lock);
547 DEFINE_SHOW_ATTRIBUTE(host1x_devices);
550 * host1x_register() - register a host1x controller
551 * @host1x: host1x controller
553 * The host1x controller driver uses this to register a host1x controller with
554 * the infrastructure. Note that all Tegra SoC generations have only ever come
555 * with a single host1x instance, so this function is somewhat academic.
557 int host1x_register(struct host1x *host1x)
559 struct host1x_driver *driver;
561 mutex_lock(&devices_lock);
562 list_add_tail(&host1x->list, &devices);
563 mutex_unlock(&devices_lock);
565 mutex_lock(&drivers_lock);
567 list_for_each_entry(driver, &drivers, list)
568 host1x_attach_driver(host1x, driver);
570 mutex_unlock(&drivers_lock);
572 debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
573 &host1x_devices_fops);
579 * host1x_unregister() - unregister a host1x controller
580 * @host1x: host1x controller
582 * The host1x controller driver uses this to remove a host1x controller from
583 * the infrastructure.
585 int host1x_unregister(struct host1x *host1x)
587 struct host1x_driver *driver;
589 mutex_lock(&drivers_lock);
591 list_for_each_entry(driver, &drivers, list)
592 host1x_detach_driver(host1x, driver);
594 mutex_unlock(&drivers_lock);
596 mutex_lock(&devices_lock);
597 list_del_init(&host1x->list);
598 mutex_unlock(&devices_lock);
603 static int host1x_device_probe(struct device *dev)
605 struct host1x_driver *driver = to_host1x_driver(dev->driver);
606 struct host1x_device *device = to_host1x_device(dev);
609 return driver->probe(device);
614 static int host1x_device_remove(struct device *dev)
616 struct host1x_driver *driver = to_host1x_driver(dev->driver);
617 struct host1x_device *device = to_host1x_device(dev);
620 return driver->remove(device);
625 static void host1x_device_shutdown(struct device *dev)
627 struct host1x_driver *driver = to_host1x_driver(dev->driver);
628 struct host1x_device *device = to_host1x_device(dev);
630 if (driver->shutdown)
631 driver->shutdown(device);
635 * host1x_driver_register_full() - register a host1x driver
636 * @driver: host1x driver
637 * @owner: owner module
639 * Drivers for host1x logical devices call this function to register a driver
640 * with the infrastructure. Note that since these drive logical devices, the
641 * registration of the driver actually triggers tho logical device creation.
642 * A logical device will be created for each host1x instance.
644 int host1x_driver_register_full(struct host1x_driver *driver,
645 struct module *owner)
647 struct host1x *host1x;
649 INIT_LIST_HEAD(&driver->list);
651 mutex_lock(&drivers_lock);
652 list_add_tail(&driver->list, &drivers);
653 mutex_unlock(&drivers_lock);
655 mutex_lock(&devices_lock);
657 list_for_each_entry(host1x, &devices, list)
658 host1x_attach_driver(host1x, driver);
660 mutex_unlock(&devices_lock);
662 driver->driver.bus = &host1x_bus_type;
663 driver->driver.owner = owner;
664 driver->driver.probe = host1x_device_probe;
665 driver->driver.remove = host1x_device_remove;
666 driver->driver.shutdown = host1x_device_shutdown;
668 return driver_register(&driver->driver);
670 EXPORT_SYMBOL(host1x_driver_register_full);
673 * host1x_driver_unregister() - unregister a host1x driver
674 * @driver: host1x driver
676 * Unbinds the driver from each of the host1x logical devices that it is
677 * bound to, effectively removing the subsystem devices that they represent.
679 void host1x_driver_unregister(struct host1x_driver *driver)
681 struct host1x *host1x;
683 driver_unregister(&driver->driver);
685 mutex_lock(&devices_lock);
687 list_for_each_entry(host1x, &devices, list)
688 host1x_detach_driver(host1x, driver);
690 mutex_unlock(&devices_lock);
692 mutex_lock(&drivers_lock);
693 list_del_init(&driver->list);
694 mutex_unlock(&drivers_lock);
696 EXPORT_SYMBOL(host1x_driver_unregister);
699 * __host1x_client_init() - initialize a host1x client
700 * @client: host1x client
701 * @key: lock class key for the client-specific mutex
703 void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key)
705 INIT_LIST_HEAD(&client->list);
706 __mutex_init(&client->lock, "host1x client lock", key);
707 client->usecount = 0;
709 EXPORT_SYMBOL(__host1x_client_init);
712 * host1x_client_exit() - uninitialize a host1x client
713 * @client: host1x client
715 void host1x_client_exit(struct host1x_client *client)
717 mutex_destroy(&client->lock);
719 EXPORT_SYMBOL(host1x_client_exit);
722 * __host1x_client_register() - register a host1x client
723 * @client: host1x client
724 * @key: lock class key for the client-specific mutex
726 * Registers a host1x client with each host1x controller instance. Note that
727 * each client will only match their parent host1x controller and will only be
728 * associated with that instance. Once all clients have been registered with
729 * their parent host1x controller, the infrastructure will set up the logical
730 * device and call host1x_device_init(), which will in turn call each client's
731 * &host1x_client_ops.init implementation.
733 int __host1x_client_register(struct host1x_client *client)
735 struct host1x *host1x;
738 mutex_lock(&devices_lock);
740 list_for_each_entry(host1x, &devices, list) {
741 err = host1x_add_client(host1x, client);
743 mutex_unlock(&devices_lock);
748 mutex_unlock(&devices_lock);
750 mutex_lock(&clients_lock);
751 list_add_tail(&client->list, &clients);
752 mutex_unlock(&clients_lock);
756 EXPORT_SYMBOL(__host1x_client_register);
759 * host1x_client_unregister() - unregister a host1x client
760 * @client: host1x client
762 * Removes a host1x client from its host1x controller instance. If a logical
763 * device has already been initialized, it will be torn down.
765 int host1x_client_unregister(struct host1x_client *client)
767 struct host1x_client *c;
768 struct host1x *host1x;
771 mutex_lock(&devices_lock);
773 list_for_each_entry(host1x, &devices, list) {
774 err = host1x_del_client(host1x, client);
776 mutex_unlock(&devices_lock);
781 mutex_unlock(&devices_lock);
782 mutex_lock(&clients_lock);
784 list_for_each_entry(c, &clients, list) {
786 list_del_init(&c->list);
791 mutex_unlock(&clients_lock);
795 EXPORT_SYMBOL(host1x_client_unregister);
797 int host1x_client_suspend(struct host1x_client *client)
801 mutex_lock(&client->lock);
803 if (client->usecount == 1) {
804 if (client->ops && client->ops->suspend) {
805 err = client->ops->suspend(client);
812 dev_dbg(client->dev, "use count: %u\n", client->usecount);
814 if (client->parent) {
815 err = host1x_client_suspend(client->parent);
823 if (client->usecount == 0)
824 if (client->ops && client->ops->resume)
825 client->ops->resume(client);
829 mutex_unlock(&client->lock);
832 EXPORT_SYMBOL(host1x_client_suspend);
834 int host1x_client_resume(struct host1x_client *client)
838 mutex_lock(&client->lock);
840 if (client->parent) {
841 err = host1x_client_resume(client->parent);
846 if (client->usecount == 0) {
847 if (client->ops && client->ops->resume) {
848 err = client->ops->resume(client);
855 dev_dbg(client->dev, "use count: %u\n", client->usecount);
861 host1x_client_suspend(client->parent);
863 mutex_unlock(&client->lock);
866 EXPORT_SYMBOL(host1x_client_resume);