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
35 * @driver: host1x driver containing the subdevices
38 static int host1x_subdev_add(struct host1x_device *device,
39 struct host1x_driver *driver,
40 struct device_node *np)
42 struct host1x_subdev *subdev;
43 struct device_node *child;
46 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
50 INIT_LIST_HEAD(&subdev->list);
51 subdev->np = of_node_get(np);
53 mutex_lock(&device->subdevs_lock);
54 list_add_tail(&subdev->list, &device->subdevs);
55 mutex_unlock(&device->subdevs_lock);
57 /* recursively add children */
58 for_each_child_of_node(np, child) {
59 if (of_match_node(driver->subdevs, child) &&
60 of_device_is_available(child)) {
61 err = host1x_subdev_add(device, driver, child);
74 * host1x_subdev_del() - remove subdevice
75 * @subdev: subdevice to remove
77 static void host1x_subdev_del(struct host1x_subdev *subdev)
79 list_del(&subdev->list);
80 of_node_put(subdev->np);
85 * host1x_device_parse_dt() - scan device tree and add matching subdevices
86 * @device: host1x logical device
87 * @driver: host1x driver
89 static int host1x_device_parse_dt(struct host1x_device *device,
90 struct host1x_driver *driver)
92 struct device_node *np;
95 for_each_child_of_node(device->dev.parent->of_node, np) {
96 if (of_match_node(driver->subdevs, np) &&
97 of_device_is_available(np)) {
98 err = host1x_subdev_add(device, driver, np);
109 static void host1x_subdev_register(struct host1x_device *device,
110 struct host1x_subdev *subdev,
111 struct host1x_client *client)
116 * Move the subdevice to the list of active (registered) subdevices
117 * and associate it with a client. At the same time, associate the
118 * client with its parent device.
120 mutex_lock(&device->subdevs_lock);
121 mutex_lock(&device->clients_lock);
122 list_move_tail(&client->list, &device->clients);
123 list_move_tail(&subdev->list, &device->active);
124 client->host = &device->dev;
125 subdev->client = client;
126 mutex_unlock(&device->clients_lock);
127 mutex_unlock(&device->subdevs_lock);
129 if (list_empty(&device->subdevs)) {
130 err = device_add(&device->dev);
132 dev_err(&device->dev, "failed to add: %d\n", err);
134 device->registered = true;
138 static void __host1x_subdev_unregister(struct host1x_device *device,
139 struct host1x_subdev *subdev)
141 struct host1x_client *client = subdev->client;
144 * If all subdevices have been activated, we're about to remove the
145 * first active subdevice, so unload the driver first.
147 if (list_empty(&device->subdevs)) {
148 if (device->registered) {
149 device->registered = false;
150 device_del(&device->dev);
155 * Move the subdevice back to the list of idle subdevices and remove
156 * it from list of clients.
158 mutex_lock(&device->clients_lock);
159 subdev->client = NULL;
161 list_move_tail(&subdev->list, &device->subdevs);
163 * XXX: Perhaps don't do this here, but rather explicitly remove it
164 * when the device is about to be deleted.
166 * This is somewhat complicated by the fact that this function is
167 * used to remove the subdevice when a client is unregistered but
168 * also when the composite device is about to be removed.
170 list_del_init(&client->list);
171 mutex_unlock(&device->clients_lock);
174 static void host1x_subdev_unregister(struct host1x_device *device,
175 struct host1x_subdev *subdev)
177 mutex_lock(&device->subdevs_lock);
178 __host1x_subdev_unregister(device, subdev);
179 mutex_unlock(&device->subdevs_lock);
183 * host1x_device_init() - initialize a host1x logical device
184 * @device: host1x logical device
186 * The driver for the host1x logical device can call this during execution of
187 * its &host1x_driver.probe implementation to initialize each of its clients.
188 * The client drivers access the subsystem specific driver data using the
189 * &host1x_client.parent field and driver data associated with it (usually by
190 * calling dev_get_drvdata()).
192 int host1x_device_init(struct host1x_device *device)
194 struct host1x_client *client;
197 mutex_lock(&device->clients_lock);
199 list_for_each_entry(client, &device->clients, list) {
200 if (client->ops && client->ops->early_init) {
201 err = client->ops->early_init(client);
203 dev_err(&device->dev, "failed to early initialize %s: %d\n",
204 dev_name(client->dev), err);
210 list_for_each_entry(client, &device->clients, list) {
211 if (client->ops && client->ops->init) {
212 err = client->ops->init(client);
214 dev_err(&device->dev,
215 "failed to initialize %s: %d\n",
216 dev_name(client->dev), err);
222 mutex_unlock(&device->clients_lock);
227 list_for_each_entry_continue_reverse(client, &device->clients, list)
228 if (client->ops->exit)
229 client->ops->exit(client);
231 /* reset client to end of list for late teardown */
232 client = list_entry(&device->clients, struct host1x_client, list);
235 list_for_each_entry_continue_reverse(client, &device->clients, list)
236 if (client->ops->late_exit)
237 client->ops->late_exit(client);
239 mutex_unlock(&device->clients_lock);
242 EXPORT_SYMBOL(host1x_device_init);
245 * host1x_device_exit() - uninitialize host1x logical device
246 * @device: host1x logical device
248 * When the driver for a host1x logical device is unloaded, it can call this
249 * function to tear down each of its clients. Typically this is done after a
250 * subsystem-specific data structure is removed and the functionality can no
253 int host1x_device_exit(struct host1x_device *device)
255 struct host1x_client *client;
258 mutex_lock(&device->clients_lock);
260 list_for_each_entry_reverse(client, &device->clients, list) {
261 if (client->ops && client->ops->exit) {
262 err = client->ops->exit(client);
264 dev_err(&device->dev,
265 "failed to cleanup %s: %d\n",
266 dev_name(client->dev), err);
267 mutex_unlock(&device->clients_lock);
273 list_for_each_entry_reverse(client, &device->clients, list) {
274 if (client->ops && client->ops->late_exit) {
275 err = client->ops->late_exit(client);
277 dev_err(&device->dev, "failed to late cleanup %s: %d\n",
278 dev_name(client->dev), err);
279 mutex_unlock(&device->clients_lock);
285 mutex_unlock(&device->clients_lock);
289 EXPORT_SYMBOL(host1x_device_exit);
291 static int host1x_add_client(struct host1x *host1x,
292 struct host1x_client *client)
294 struct host1x_device *device;
295 struct host1x_subdev *subdev;
297 mutex_lock(&host1x->devices_lock);
299 list_for_each_entry(device, &host1x->devices, list) {
300 list_for_each_entry(subdev, &device->subdevs, list) {
301 if (subdev->np == client->dev->of_node) {
302 host1x_subdev_register(device, subdev, client);
303 mutex_unlock(&host1x->devices_lock);
309 mutex_unlock(&host1x->devices_lock);
313 static int host1x_del_client(struct host1x *host1x,
314 struct host1x_client *client)
316 struct host1x_device *device, *dt;
317 struct host1x_subdev *subdev;
319 mutex_lock(&host1x->devices_lock);
321 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
322 list_for_each_entry(subdev, &device->active, list) {
323 if (subdev->client == client) {
324 host1x_subdev_unregister(device, subdev);
325 mutex_unlock(&host1x->devices_lock);
331 mutex_unlock(&host1x->devices_lock);
335 static int host1x_device_match(struct device *dev, struct device_driver *drv)
337 return strcmp(dev_name(dev), drv->name) == 0;
340 static int host1x_device_uevent(struct device *dev,
341 struct kobj_uevent_env *env)
343 struct device_node *np = dev->parent->of_node;
344 unsigned int count = 0;
349 * This duplicates most of of_device_uevent(), but the latter cannot
350 * be called from modules and operates on dev->of_node, which is not
351 * available in this case.
353 * Note that this is really only needed for backwards compatibility
354 * with libdrm, which parses this information from sysfs and will
355 * fail if it can't find the OF_FULLNAME, specifically.
357 add_uevent_var(env, "OF_NAME=%pOFn", np);
358 add_uevent_var(env, "OF_FULLNAME=%pOF", np);
360 of_property_for_each_string(np, "compatible", p, compat) {
361 add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
365 add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
370 static int host1x_dma_configure(struct device *dev)
372 return of_dma_configure(dev, dev->of_node, true);
375 static const struct dev_pm_ops host1x_device_pm_ops = {
376 .suspend = pm_generic_suspend,
377 .resume = pm_generic_resume,
378 .freeze = pm_generic_freeze,
379 .thaw = pm_generic_thaw,
380 .poweroff = pm_generic_poweroff,
381 .restore = pm_generic_restore,
384 struct bus_type host1x_bus_type = {
386 .match = host1x_device_match,
387 .uevent = host1x_device_uevent,
388 .dma_configure = host1x_dma_configure,
389 .pm = &host1x_device_pm_ops,
392 static void __host1x_device_del(struct host1x_device *device)
394 struct host1x_subdev *subdev, *sd;
395 struct host1x_client *client, *cl;
397 mutex_lock(&device->subdevs_lock);
399 /* unregister subdevices */
400 list_for_each_entry_safe(subdev, sd, &device->active, list) {
402 * host1x_subdev_unregister() will remove the client from
403 * any lists, so we'll need to manually add it back to the
404 * list of idle clients.
406 * XXX: Alternatively, perhaps don't remove the client from
407 * any lists in host1x_subdev_unregister() and instead do
408 * that explicitly from host1x_unregister_client()?
410 client = subdev->client;
412 __host1x_subdev_unregister(device, subdev);
414 /* add the client to the list of idle clients */
415 mutex_lock(&clients_lock);
416 list_add_tail(&client->list, &clients);
417 mutex_unlock(&clients_lock);
420 /* remove subdevices */
421 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
422 host1x_subdev_del(subdev);
424 mutex_unlock(&device->subdevs_lock);
426 /* move clients to idle list */
427 mutex_lock(&clients_lock);
428 mutex_lock(&device->clients_lock);
430 list_for_each_entry_safe(client, cl, &device->clients, list)
431 list_move_tail(&client->list, &clients);
433 mutex_unlock(&device->clients_lock);
434 mutex_unlock(&clients_lock);
436 /* finally remove the device */
437 list_del_init(&device->list);
440 static void host1x_device_release(struct device *dev)
442 struct host1x_device *device = to_host1x_device(dev);
444 __host1x_device_del(device);
448 static int host1x_device_add(struct host1x *host1x,
449 struct host1x_driver *driver)
451 struct host1x_client *client, *tmp;
452 struct host1x_subdev *subdev;
453 struct host1x_device *device;
456 device = kzalloc(sizeof(*device), GFP_KERNEL);
460 device_initialize(&device->dev);
462 mutex_init(&device->subdevs_lock);
463 INIT_LIST_HEAD(&device->subdevs);
464 INIT_LIST_HEAD(&device->active);
465 mutex_init(&device->clients_lock);
466 INIT_LIST_HEAD(&device->clients);
467 INIT_LIST_HEAD(&device->list);
468 device->driver = driver;
470 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
471 device->dev.dma_mask = &device->dev.coherent_dma_mask;
472 dev_set_name(&device->dev, "%s", driver->driver.name);
473 device->dev.release = host1x_device_release;
474 device->dev.bus = &host1x_bus_type;
475 device->dev.parent = host1x->dev;
477 of_dma_configure(&device->dev, host1x->dev->of_node, true);
479 device->dev.dma_parms = &device->dma_parms;
480 dma_set_max_seg_size(&device->dev, UINT_MAX);
482 err = host1x_device_parse_dt(device, driver);
488 list_add_tail(&device->list, &host1x->devices);
490 mutex_lock(&clients_lock);
492 list_for_each_entry_safe(client, tmp, &clients, list) {
493 list_for_each_entry(subdev, &device->subdevs, list) {
494 if (subdev->np == client->dev->of_node) {
495 host1x_subdev_register(device, subdev, client);
501 mutex_unlock(&clients_lock);
507 * Removes a device by first unregistering any subdevices and then removing
508 * itself from the list of devices.
510 * This function must be called with the host1x->devices_lock held.
512 static void host1x_device_del(struct host1x *host1x,
513 struct host1x_device *device)
515 if (device->registered) {
516 device->registered = false;
517 device_del(&device->dev);
520 put_device(&device->dev);
523 static void host1x_attach_driver(struct host1x *host1x,
524 struct host1x_driver *driver)
526 struct host1x_device *device;
529 mutex_lock(&host1x->devices_lock);
531 list_for_each_entry(device, &host1x->devices, list) {
532 if (device->driver == driver) {
533 mutex_unlock(&host1x->devices_lock);
538 err = host1x_device_add(host1x, driver);
540 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
542 mutex_unlock(&host1x->devices_lock);
545 static void host1x_detach_driver(struct host1x *host1x,
546 struct host1x_driver *driver)
548 struct host1x_device *device, *tmp;
550 mutex_lock(&host1x->devices_lock);
552 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
553 if (device->driver == driver)
554 host1x_device_del(host1x, device);
556 mutex_unlock(&host1x->devices_lock);
559 static int host1x_devices_show(struct seq_file *s, void *data)
561 struct host1x *host1x = s->private;
562 struct host1x_device *device;
564 mutex_lock(&host1x->devices_lock);
566 list_for_each_entry(device, &host1x->devices, list) {
567 struct host1x_subdev *subdev;
569 seq_printf(s, "%s\n", dev_name(&device->dev));
571 mutex_lock(&device->subdevs_lock);
573 list_for_each_entry(subdev, &device->active, list)
574 seq_printf(s, " %pOFf: %s\n", subdev->np,
575 dev_name(subdev->client->dev));
577 list_for_each_entry(subdev, &device->subdevs, list)
578 seq_printf(s, " %pOFf:\n", subdev->np);
580 mutex_unlock(&device->subdevs_lock);
583 mutex_unlock(&host1x->devices_lock);
587 DEFINE_SHOW_ATTRIBUTE(host1x_devices);
590 * host1x_register() - register a host1x controller
591 * @host1x: host1x controller
593 * The host1x controller driver uses this to register a host1x controller with
594 * the infrastructure. Note that all Tegra SoC generations have only ever come
595 * with a single host1x instance, so this function is somewhat academic.
597 int host1x_register(struct host1x *host1x)
599 struct host1x_driver *driver;
601 mutex_lock(&devices_lock);
602 list_add_tail(&host1x->list, &devices);
603 mutex_unlock(&devices_lock);
605 mutex_lock(&drivers_lock);
607 list_for_each_entry(driver, &drivers, list)
608 host1x_attach_driver(host1x, driver);
610 mutex_unlock(&drivers_lock);
612 debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
613 &host1x_devices_fops);
619 * host1x_unregister() - unregister a host1x controller
620 * @host1x: host1x controller
622 * The host1x controller driver uses this to remove a host1x controller from
623 * the infrastructure.
625 int host1x_unregister(struct host1x *host1x)
627 struct host1x_driver *driver;
629 mutex_lock(&drivers_lock);
631 list_for_each_entry(driver, &drivers, list)
632 host1x_detach_driver(host1x, driver);
634 mutex_unlock(&drivers_lock);
636 mutex_lock(&devices_lock);
637 list_del_init(&host1x->list);
638 mutex_unlock(&devices_lock);
643 static int host1x_device_probe(struct device *dev)
645 struct host1x_driver *driver = to_host1x_driver(dev->driver);
646 struct host1x_device *device = to_host1x_device(dev);
649 return driver->probe(device);
654 static int host1x_device_remove(struct device *dev)
656 struct host1x_driver *driver = to_host1x_driver(dev->driver);
657 struct host1x_device *device = to_host1x_device(dev);
660 return driver->remove(device);
665 static void host1x_device_shutdown(struct device *dev)
667 struct host1x_driver *driver = to_host1x_driver(dev->driver);
668 struct host1x_device *device = to_host1x_device(dev);
670 if (driver->shutdown)
671 driver->shutdown(device);
675 * host1x_driver_register_full() - register a host1x driver
676 * @driver: host1x driver
677 * @owner: owner module
679 * Drivers for host1x logical devices call this function to register a driver
680 * with the infrastructure. Note that since these drive logical devices, the
681 * registration of the driver actually triggers tho logical device creation.
682 * A logical device will be created for each host1x instance.
684 int host1x_driver_register_full(struct host1x_driver *driver,
685 struct module *owner)
687 struct host1x *host1x;
689 INIT_LIST_HEAD(&driver->list);
691 mutex_lock(&drivers_lock);
692 list_add_tail(&driver->list, &drivers);
693 mutex_unlock(&drivers_lock);
695 mutex_lock(&devices_lock);
697 list_for_each_entry(host1x, &devices, list)
698 host1x_attach_driver(host1x, driver);
700 mutex_unlock(&devices_lock);
702 driver->driver.bus = &host1x_bus_type;
703 driver->driver.owner = owner;
704 driver->driver.probe = host1x_device_probe;
705 driver->driver.remove = host1x_device_remove;
706 driver->driver.shutdown = host1x_device_shutdown;
708 return driver_register(&driver->driver);
710 EXPORT_SYMBOL(host1x_driver_register_full);
713 * host1x_driver_unregister() - unregister a host1x driver
714 * @driver: host1x driver
716 * Unbinds the driver from each of the host1x logical devices that it is
717 * bound to, effectively removing the subsystem devices that they represent.
719 void host1x_driver_unregister(struct host1x_driver *driver)
721 struct host1x *host1x;
723 driver_unregister(&driver->driver);
725 mutex_lock(&devices_lock);
727 list_for_each_entry(host1x, &devices, list)
728 host1x_detach_driver(host1x, driver);
730 mutex_unlock(&devices_lock);
732 mutex_lock(&drivers_lock);
733 list_del_init(&driver->list);
734 mutex_unlock(&drivers_lock);
736 EXPORT_SYMBOL(host1x_driver_unregister);
739 * __host1x_client_init() - initialize a host1x client
740 * @client: host1x client
741 * @key: lock class key for the client-specific mutex
743 void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key)
745 INIT_LIST_HEAD(&client->list);
746 __mutex_init(&client->lock, "host1x client lock", key);
747 client->usecount = 0;
749 EXPORT_SYMBOL(__host1x_client_init);
752 * host1x_client_exit() - uninitialize a host1x client
753 * @client: host1x client
755 void host1x_client_exit(struct host1x_client *client)
757 mutex_destroy(&client->lock);
759 EXPORT_SYMBOL(host1x_client_exit);
762 * __host1x_client_register() - register a host1x client
763 * @client: host1x client
764 * @key: lock class key for the client-specific mutex
766 * Registers a host1x client with each host1x controller instance. Note that
767 * each client will only match their parent host1x controller and will only be
768 * associated with that instance. Once all clients have been registered with
769 * their parent host1x controller, the infrastructure will set up the logical
770 * device and call host1x_device_init(), which will in turn call each client's
771 * &host1x_client_ops.init implementation.
773 int __host1x_client_register(struct host1x_client *client)
775 struct host1x *host1x;
778 mutex_lock(&devices_lock);
780 list_for_each_entry(host1x, &devices, list) {
781 err = host1x_add_client(host1x, client);
783 mutex_unlock(&devices_lock);
788 mutex_unlock(&devices_lock);
790 mutex_lock(&clients_lock);
791 list_add_tail(&client->list, &clients);
792 mutex_unlock(&clients_lock);
796 EXPORT_SYMBOL(__host1x_client_register);
799 * host1x_client_unregister() - unregister a host1x client
800 * @client: host1x client
802 * Removes a host1x client from its host1x controller instance. If a logical
803 * device has already been initialized, it will be torn down.
805 int host1x_client_unregister(struct host1x_client *client)
807 struct host1x_client *c;
808 struct host1x *host1x;
811 mutex_lock(&devices_lock);
813 list_for_each_entry(host1x, &devices, list) {
814 err = host1x_del_client(host1x, client);
816 mutex_unlock(&devices_lock);
821 mutex_unlock(&devices_lock);
822 mutex_lock(&clients_lock);
824 list_for_each_entry(c, &clients, list) {
826 list_del_init(&c->list);
831 mutex_unlock(&clients_lock);
835 EXPORT_SYMBOL(host1x_client_unregister);
837 int host1x_client_suspend(struct host1x_client *client)
841 mutex_lock(&client->lock);
843 if (client->usecount == 1) {
844 if (client->ops && client->ops->suspend) {
845 err = client->ops->suspend(client);
852 dev_dbg(client->dev, "use count: %u\n", client->usecount);
854 if (client->parent) {
855 err = host1x_client_suspend(client->parent);
863 if (client->usecount == 0)
864 if (client->ops && client->ops->resume)
865 client->ops->resume(client);
869 mutex_unlock(&client->lock);
872 EXPORT_SYMBOL(host1x_client_suspend);
874 int host1x_client_resume(struct host1x_client *client)
878 mutex_lock(&client->lock);
880 if (client->parent) {
881 err = host1x_client_resume(client->parent);
886 if (client->usecount == 0) {
887 if (client->ops && client->ops->resume) {
888 err = client->ops->resume(client);
895 dev_dbg(client->dev, "use count: %u\n", client->usecount);
901 host1x_client_suspend(client->parent);
903 mutex_unlock(&client->lock);
906 EXPORT_SYMBOL(host1x_client_resume);