WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / host1x / bus.c
blob347fb962b6c936ecb25f59807b1a673c385262ed
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Avionic Design GmbH
4 * Copyright (C) 2012-2013, NVIDIA Corporation
5 */
7 #include <linux/debugfs.h>
8 #include <linux/host1x.h>
9 #include <linux/of.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
12 #include <linux/of_device.h>
14 #include "bus.h"
15 #include "dev.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;
32 /**
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
36 * @np: device node
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;
44 int err;
46 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
47 if (!subdev)
48 return -ENOMEM;
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);
62 if (err < 0) {
63 /* XXX cleanup? */
64 of_node_put(child);
65 return err;
70 return 0;
73 /**
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);
81 kfree(subdev);
84 /**
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;
93 int err;
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);
99 if (err < 0) {
100 of_node_put(np);
101 return err;
106 return 0;
109 static void host1x_subdev_register(struct host1x_device *device,
110 struct host1x_subdev *subdev,
111 struct host1x_client *client)
113 int err;
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);
131 if (err < 0)
132 dev_err(&device->dev, "failed to add: %d\n", err);
133 else
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;
160 client->host = 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;
195 int err;
197 mutex_lock(&device->clients_lock);
199 list_for_each_entry(client, &device->clients, list) {
200 if (client->ops && client->ops->init) {
201 err = client->ops->init(client);
202 if (err < 0) {
203 dev_err(&device->dev,
204 "failed to initialize %s: %d\n",
205 dev_name(client->dev), err);
206 goto teardown;
211 mutex_unlock(&device->clients_lock);
213 return 0;
215 teardown:
216 list_for_each_entry_continue_reverse(client, &device->clients, list)
217 if (client->ops->exit)
218 client->ops->exit(client);
220 mutex_unlock(&device->clients_lock);
221 return err;
223 EXPORT_SYMBOL(host1x_device_init);
226 * host1x_device_exit() - uninitialize host1x logical device
227 * @device: host1x logical device
229 * When the driver for a host1x logical device is unloaded, it can call this
230 * function to tear down each of its clients. Typically this is done after a
231 * subsystem-specific data structure is removed and the functionality can no
232 * longer be used.
234 int host1x_device_exit(struct host1x_device *device)
236 struct host1x_client *client;
237 int err;
239 mutex_lock(&device->clients_lock);
241 list_for_each_entry_reverse(client, &device->clients, list) {
242 if (client->ops && client->ops->exit) {
243 err = client->ops->exit(client);
244 if (err < 0) {
245 dev_err(&device->dev,
246 "failed to cleanup %s: %d\n",
247 dev_name(client->dev), err);
248 mutex_unlock(&device->clients_lock);
249 return err;
254 mutex_unlock(&device->clients_lock);
256 return 0;
258 EXPORT_SYMBOL(host1x_device_exit);
260 static int host1x_add_client(struct host1x *host1x,
261 struct host1x_client *client)
263 struct host1x_device *device;
264 struct host1x_subdev *subdev;
266 mutex_lock(&host1x->devices_lock);
268 list_for_each_entry(device, &host1x->devices, list) {
269 list_for_each_entry(subdev, &device->subdevs, list) {
270 if (subdev->np == client->dev->of_node) {
271 host1x_subdev_register(device, subdev, client);
272 mutex_unlock(&host1x->devices_lock);
273 return 0;
278 mutex_unlock(&host1x->devices_lock);
279 return -ENODEV;
282 static int host1x_del_client(struct host1x *host1x,
283 struct host1x_client *client)
285 struct host1x_device *device, *dt;
286 struct host1x_subdev *subdev;
288 mutex_lock(&host1x->devices_lock);
290 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
291 list_for_each_entry(subdev, &device->active, list) {
292 if (subdev->client == client) {
293 host1x_subdev_unregister(device, subdev);
294 mutex_unlock(&host1x->devices_lock);
295 return 0;
300 mutex_unlock(&host1x->devices_lock);
301 return -ENODEV;
304 static int host1x_device_match(struct device *dev, struct device_driver *drv)
306 return strcmp(dev_name(dev), drv->name) == 0;
309 static int host1x_device_uevent(struct device *dev,
310 struct kobj_uevent_env *env)
312 struct device_node *np = dev->parent->of_node;
313 unsigned int count = 0;
314 struct property *p;
315 const char *compat;
318 * This duplicates most of of_device_uevent(), but the latter cannot
319 * be called from modules and operates on dev->of_node, which is not
320 * available in this case.
322 * Note that this is really only needed for backwards compatibility
323 * with libdrm, which parses this information from sysfs and will
324 * fail if it can't find the OF_FULLNAME, specifically.
326 add_uevent_var(env, "OF_NAME=%pOFn", np);
327 add_uevent_var(env, "OF_FULLNAME=%pOF", np);
329 of_property_for_each_string(np, "compatible", p, compat) {
330 add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
331 count++;
334 add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
336 return 0;
339 static int host1x_dma_configure(struct device *dev)
341 return of_dma_configure(dev, dev->of_node, true);
344 static const struct dev_pm_ops host1x_device_pm_ops = {
345 .suspend = pm_generic_suspend,
346 .resume = pm_generic_resume,
347 .freeze = pm_generic_freeze,
348 .thaw = pm_generic_thaw,
349 .poweroff = pm_generic_poweroff,
350 .restore = pm_generic_restore,
353 struct bus_type host1x_bus_type = {
354 .name = "host1x",
355 .match = host1x_device_match,
356 .uevent = host1x_device_uevent,
357 .dma_configure = host1x_dma_configure,
358 .pm = &host1x_device_pm_ops,
361 static void __host1x_device_del(struct host1x_device *device)
363 struct host1x_subdev *subdev, *sd;
364 struct host1x_client *client, *cl;
366 mutex_lock(&device->subdevs_lock);
368 /* unregister subdevices */
369 list_for_each_entry_safe(subdev, sd, &device->active, list) {
371 * host1x_subdev_unregister() will remove the client from
372 * any lists, so we'll need to manually add it back to the
373 * list of idle clients.
375 * XXX: Alternatively, perhaps don't remove the client from
376 * any lists in host1x_subdev_unregister() and instead do
377 * that explicitly from host1x_unregister_client()?
379 client = subdev->client;
381 __host1x_subdev_unregister(device, subdev);
383 /* add the client to the list of idle clients */
384 mutex_lock(&clients_lock);
385 list_add_tail(&client->list, &clients);
386 mutex_unlock(&clients_lock);
389 /* remove subdevices */
390 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
391 host1x_subdev_del(subdev);
393 mutex_unlock(&device->subdevs_lock);
395 /* move clients to idle list */
396 mutex_lock(&clients_lock);
397 mutex_lock(&device->clients_lock);
399 list_for_each_entry_safe(client, cl, &device->clients, list)
400 list_move_tail(&client->list, &clients);
402 mutex_unlock(&device->clients_lock);
403 mutex_unlock(&clients_lock);
405 /* finally remove the device */
406 list_del_init(&device->list);
409 static void host1x_device_release(struct device *dev)
411 struct host1x_device *device = to_host1x_device(dev);
413 __host1x_device_del(device);
414 kfree(device);
417 static int host1x_device_add(struct host1x *host1x,
418 struct host1x_driver *driver)
420 struct host1x_client *client, *tmp;
421 struct host1x_subdev *subdev;
422 struct host1x_device *device;
423 int err;
425 device = kzalloc(sizeof(*device), GFP_KERNEL);
426 if (!device)
427 return -ENOMEM;
429 device_initialize(&device->dev);
431 mutex_init(&device->subdevs_lock);
432 INIT_LIST_HEAD(&device->subdevs);
433 INIT_LIST_HEAD(&device->active);
434 mutex_init(&device->clients_lock);
435 INIT_LIST_HEAD(&device->clients);
436 INIT_LIST_HEAD(&device->list);
437 device->driver = driver;
439 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
440 device->dev.dma_mask = &device->dev.coherent_dma_mask;
441 dev_set_name(&device->dev, "%s", driver->driver.name);
442 device->dev.release = host1x_device_release;
443 device->dev.bus = &host1x_bus_type;
444 device->dev.parent = host1x->dev;
446 of_dma_configure(&device->dev, host1x->dev->of_node, true);
448 device->dev.dma_parms = &device->dma_parms;
449 dma_set_max_seg_size(&device->dev, UINT_MAX);
451 err = host1x_device_parse_dt(device, driver);
452 if (err < 0) {
453 kfree(device);
454 return err;
457 list_add_tail(&device->list, &host1x->devices);
459 mutex_lock(&clients_lock);
461 list_for_each_entry_safe(client, tmp, &clients, list) {
462 list_for_each_entry(subdev, &device->subdevs, list) {
463 if (subdev->np == client->dev->of_node) {
464 host1x_subdev_register(device, subdev, client);
465 break;
470 mutex_unlock(&clients_lock);
472 return 0;
476 * Removes a device by first unregistering any subdevices and then removing
477 * itself from the list of devices.
479 * This function must be called with the host1x->devices_lock held.
481 static void host1x_device_del(struct host1x *host1x,
482 struct host1x_device *device)
484 if (device->registered) {
485 device->registered = false;
486 device_del(&device->dev);
489 put_device(&device->dev);
492 static void host1x_attach_driver(struct host1x *host1x,
493 struct host1x_driver *driver)
495 struct host1x_device *device;
496 int err;
498 mutex_lock(&host1x->devices_lock);
500 list_for_each_entry(device, &host1x->devices, list) {
501 if (device->driver == driver) {
502 mutex_unlock(&host1x->devices_lock);
503 return;
507 err = host1x_device_add(host1x, driver);
508 if (err < 0)
509 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
511 mutex_unlock(&host1x->devices_lock);
514 static void host1x_detach_driver(struct host1x *host1x,
515 struct host1x_driver *driver)
517 struct host1x_device *device, *tmp;
519 mutex_lock(&host1x->devices_lock);
521 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
522 if (device->driver == driver)
523 host1x_device_del(host1x, device);
525 mutex_unlock(&host1x->devices_lock);
528 static int host1x_devices_show(struct seq_file *s, void *data)
530 struct host1x *host1x = s->private;
531 struct host1x_device *device;
533 mutex_lock(&host1x->devices_lock);
535 list_for_each_entry(device, &host1x->devices, list) {
536 struct host1x_subdev *subdev;
538 seq_printf(s, "%s\n", dev_name(&device->dev));
540 mutex_lock(&device->subdevs_lock);
542 list_for_each_entry(subdev, &device->active, list)
543 seq_printf(s, " %pOFf: %s\n", subdev->np,
544 dev_name(subdev->client->dev));
546 list_for_each_entry(subdev, &device->subdevs, list)
547 seq_printf(s, " %pOFf:\n", subdev->np);
549 mutex_unlock(&device->subdevs_lock);
552 mutex_unlock(&host1x->devices_lock);
554 return 0;
556 DEFINE_SHOW_ATTRIBUTE(host1x_devices);
559 * host1x_register() - register a host1x controller
560 * @host1x: host1x controller
562 * The host1x controller driver uses this to register a host1x controller with
563 * the infrastructure. Note that all Tegra SoC generations have only ever come
564 * with a single host1x instance, so this function is somewhat academic.
566 int host1x_register(struct host1x *host1x)
568 struct host1x_driver *driver;
570 mutex_lock(&devices_lock);
571 list_add_tail(&host1x->list, &devices);
572 mutex_unlock(&devices_lock);
574 mutex_lock(&drivers_lock);
576 list_for_each_entry(driver, &drivers, list)
577 host1x_attach_driver(host1x, driver);
579 mutex_unlock(&drivers_lock);
581 debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
582 &host1x_devices_fops);
584 return 0;
588 * host1x_unregister() - unregister a host1x controller
589 * @host1x: host1x controller
591 * The host1x controller driver uses this to remove a host1x controller from
592 * the infrastructure.
594 int host1x_unregister(struct host1x *host1x)
596 struct host1x_driver *driver;
598 mutex_lock(&drivers_lock);
600 list_for_each_entry(driver, &drivers, list)
601 host1x_detach_driver(host1x, driver);
603 mutex_unlock(&drivers_lock);
605 mutex_lock(&devices_lock);
606 list_del_init(&host1x->list);
607 mutex_unlock(&devices_lock);
609 return 0;
612 static int host1x_device_probe(struct device *dev)
614 struct host1x_driver *driver = to_host1x_driver(dev->driver);
615 struct host1x_device *device = to_host1x_device(dev);
617 if (driver->probe)
618 return driver->probe(device);
620 return 0;
623 static int host1x_device_remove(struct device *dev)
625 struct host1x_driver *driver = to_host1x_driver(dev->driver);
626 struct host1x_device *device = to_host1x_device(dev);
628 if (driver->remove)
629 return driver->remove(device);
631 return 0;
634 static void host1x_device_shutdown(struct device *dev)
636 struct host1x_driver *driver = to_host1x_driver(dev->driver);
637 struct host1x_device *device = to_host1x_device(dev);
639 if (driver->shutdown)
640 driver->shutdown(device);
644 * host1x_driver_register_full() - register a host1x driver
645 * @driver: host1x driver
646 * @owner: owner module
648 * Drivers for host1x logical devices call this function to register a driver
649 * with the infrastructure. Note that since these drive logical devices, the
650 * registration of the driver actually triggers tho logical device creation.
651 * A logical device will be created for each host1x instance.
653 int host1x_driver_register_full(struct host1x_driver *driver,
654 struct module *owner)
656 struct host1x *host1x;
658 INIT_LIST_HEAD(&driver->list);
660 mutex_lock(&drivers_lock);
661 list_add_tail(&driver->list, &drivers);
662 mutex_unlock(&drivers_lock);
664 mutex_lock(&devices_lock);
666 list_for_each_entry(host1x, &devices, list)
667 host1x_attach_driver(host1x, driver);
669 mutex_unlock(&devices_lock);
671 driver->driver.bus = &host1x_bus_type;
672 driver->driver.owner = owner;
673 driver->driver.probe = host1x_device_probe;
674 driver->driver.remove = host1x_device_remove;
675 driver->driver.shutdown = host1x_device_shutdown;
677 return driver_register(&driver->driver);
679 EXPORT_SYMBOL(host1x_driver_register_full);
682 * host1x_driver_unregister() - unregister a host1x driver
683 * @driver: host1x driver
685 * Unbinds the driver from each of the host1x logical devices that it is
686 * bound to, effectively removing the subsystem devices that they represent.
688 void host1x_driver_unregister(struct host1x_driver *driver)
690 struct host1x *host1x;
692 driver_unregister(&driver->driver);
694 mutex_lock(&devices_lock);
696 list_for_each_entry(host1x, &devices, list)
697 host1x_detach_driver(host1x, driver);
699 mutex_unlock(&devices_lock);
701 mutex_lock(&drivers_lock);
702 list_del_init(&driver->list);
703 mutex_unlock(&drivers_lock);
705 EXPORT_SYMBOL(host1x_driver_unregister);
708 * host1x_client_register() - register a host1x client
709 * @client: host1x client
711 * Registers a host1x client with each host1x controller instance. Note that
712 * each client will only match their parent host1x controller and will only be
713 * associated with that instance. Once all clients have been registered with
714 * their parent host1x controller, the infrastructure will set up the logical
715 * device and call host1x_device_init(), which will in turn call each client's
716 * &host1x_client_ops.init implementation.
718 int host1x_client_register(struct host1x_client *client)
720 struct host1x *host1x;
721 int err;
723 INIT_LIST_HEAD(&client->list);
724 mutex_init(&client->lock);
725 client->usecount = 0;
727 mutex_lock(&devices_lock);
729 list_for_each_entry(host1x, &devices, list) {
730 err = host1x_add_client(host1x, client);
731 if (!err) {
732 mutex_unlock(&devices_lock);
733 return 0;
737 mutex_unlock(&devices_lock);
739 mutex_lock(&clients_lock);
740 list_add_tail(&client->list, &clients);
741 mutex_unlock(&clients_lock);
743 return 0;
745 EXPORT_SYMBOL(host1x_client_register);
748 * host1x_client_unregister() - unregister a host1x client
749 * @client: host1x client
751 * Removes a host1x client from its host1x controller instance. If a logical
752 * device has already been initialized, it will be torn down.
754 int host1x_client_unregister(struct host1x_client *client)
756 struct host1x_client *c;
757 struct host1x *host1x;
758 int err;
760 mutex_lock(&devices_lock);
762 list_for_each_entry(host1x, &devices, list) {
763 err = host1x_del_client(host1x, client);
764 if (!err) {
765 mutex_unlock(&devices_lock);
766 return 0;
770 mutex_unlock(&devices_lock);
771 mutex_lock(&clients_lock);
773 list_for_each_entry(c, &clients, list) {
774 if (c == client) {
775 list_del_init(&c->list);
776 break;
780 mutex_unlock(&clients_lock);
782 return 0;
784 EXPORT_SYMBOL(host1x_client_unregister);
786 int host1x_client_suspend(struct host1x_client *client)
788 int err = 0;
790 mutex_lock(&client->lock);
792 if (client->usecount == 1) {
793 if (client->ops && client->ops->suspend) {
794 err = client->ops->suspend(client);
795 if (err < 0)
796 goto unlock;
800 client->usecount--;
801 dev_dbg(client->dev, "use count: %u\n", client->usecount);
803 if (client->parent) {
804 err = host1x_client_suspend(client->parent);
805 if (err < 0)
806 goto resume;
809 goto unlock;
811 resume:
812 if (client->usecount == 0)
813 if (client->ops && client->ops->resume)
814 client->ops->resume(client);
816 client->usecount++;
817 unlock:
818 mutex_unlock(&client->lock);
819 return err;
821 EXPORT_SYMBOL(host1x_client_suspend);
823 int host1x_client_resume(struct host1x_client *client)
825 int err = 0;
827 mutex_lock(&client->lock);
829 if (client->parent) {
830 err = host1x_client_resume(client->parent);
831 if (err < 0)
832 goto unlock;
835 if (client->usecount == 0) {
836 if (client->ops && client->ops->resume) {
837 err = client->ops->resume(client);
838 if (err < 0)
839 goto suspend;
843 client->usecount++;
844 dev_dbg(client->dev, "use count: %u\n", client->usecount);
846 goto unlock;
848 suspend:
849 if (client->parent)
850 host1x_client_suspend(client->parent);
851 unlock:
852 mutex_unlock(&client->lock);
853 return err;
855 EXPORT_SYMBOL(host1x_client_resume);