2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013, NVIDIA Corporation
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.
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
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/>.
18 #include <linux/debugfs.h>
19 #include <linux/host1x.h>
21 #include <linux/seq_file.h>
22 #include <linux/slab.h>
23 #include <linux/of_device.h>
28 static DEFINE_MUTEX(clients_lock
);
29 static LIST_HEAD(clients
);
31 static DEFINE_MUTEX(drivers_lock
);
32 static LIST_HEAD(drivers
);
34 static DEFINE_MUTEX(devices_lock
);
35 static LIST_HEAD(devices
);
37 struct host1x_subdev
{
38 struct host1x_client
*client
;
39 struct device_node
*np
;
40 struct list_head list
;
44 * host1x_subdev_add() - add a new subdevice with an associated device node
45 * @device: host1x device to add the subdevice to
48 static int host1x_subdev_add(struct host1x_device
*device
,
49 struct host1x_driver
*driver
,
50 struct device_node
*np
)
52 struct host1x_subdev
*subdev
;
53 struct device_node
*child
;
56 subdev
= kzalloc(sizeof(*subdev
), GFP_KERNEL
);
60 INIT_LIST_HEAD(&subdev
->list
);
61 subdev
->np
= of_node_get(np
);
63 mutex_lock(&device
->subdevs_lock
);
64 list_add_tail(&subdev
->list
, &device
->subdevs
);
65 mutex_unlock(&device
->subdevs_lock
);
67 /* recursively add children */
68 for_each_child_of_node(np
, child
) {
69 if (of_match_node(driver
->subdevs
, child
) &&
70 of_device_is_available(child
)) {
71 err
= host1x_subdev_add(device
, driver
, child
);
84 * host1x_subdev_del() - remove subdevice
85 * @subdev: subdevice to remove
87 static void host1x_subdev_del(struct host1x_subdev
*subdev
)
89 list_del(&subdev
->list
);
90 of_node_put(subdev
->np
);
95 * host1x_device_parse_dt() - scan device tree and add matching subdevices
96 * @device: host1x logical device
97 * @driver: host1x driver
99 static int host1x_device_parse_dt(struct host1x_device
*device
,
100 struct host1x_driver
*driver
)
102 struct device_node
*np
;
105 for_each_child_of_node(device
->dev
.parent
->of_node
, np
) {
106 if (of_match_node(driver
->subdevs
, np
) &&
107 of_device_is_available(np
)) {
108 err
= host1x_subdev_add(device
, driver
, np
);
119 static void host1x_subdev_register(struct host1x_device
*device
,
120 struct host1x_subdev
*subdev
,
121 struct host1x_client
*client
)
126 * Move the subdevice to the list of active (registered) subdevices
127 * and associate it with a client. At the same time, associate the
128 * client with its parent device.
130 mutex_lock(&device
->subdevs_lock
);
131 mutex_lock(&device
->clients_lock
);
132 list_move_tail(&client
->list
, &device
->clients
);
133 list_move_tail(&subdev
->list
, &device
->active
);
134 client
->parent
= &device
->dev
;
135 subdev
->client
= client
;
136 mutex_unlock(&device
->clients_lock
);
137 mutex_unlock(&device
->subdevs_lock
);
139 if (list_empty(&device
->subdevs
)) {
140 err
= device_add(&device
->dev
);
142 dev_err(&device
->dev
, "failed to add: %d\n", err
);
144 device
->registered
= true;
148 static void __host1x_subdev_unregister(struct host1x_device
*device
,
149 struct host1x_subdev
*subdev
)
151 struct host1x_client
*client
= subdev
->client
;
154 * If all subdevices have been activated, we're about to remove the
155 * first active subdevice, so unload the driver first.
157 if (list_empty(&device
->subdevs
)) {
158 if (device
->registered
) {
159 device
->registered
= false;
160 device_del(&device
->dev
);
165 * Move the subdevice back to the list of idle subdevices and remove
166 * it from list of clients.
168 mutex_lock(&device
->clients_lock
);
169 subdev
->client
= NULL
;
170 client
->parent
= NULL
;
171 list_move_tail(&subdev
->list
, &device
->subdevs
);
173 * XXX: Perhaps don't do this here, but rather explicitly remove it
174 * when the device is about to be deleted.
176 * This is somewhat complicated by the fact that this function is
177 * used to remove the subdevice when a client is unregistered but
178 * also when the composite device is about to be removed.
180 list_del_init(&client
->list
);
181 mutex_unlock(&device
->clients_lock
);
184 static void host1x_subdev_unregister(struct host1x_device
*device
,
185 struct host1x_subdev
*subdev
)
187 mutex_lock(&device
->subdevs_lock
);
188 __host1x_subdev_unregister(device
, subdev
);
189 mutex_unlock(&device
->subdevs_lock
);
193 * host1x_device_init() - initialize a host1x logical device
194 * @device: host1x logical device
196 * The driver for the host1x logical device can call this during execution of
197 * its &host1x_driver.probe implementation to initialize each of its clients.
198 * The client drivers access the subsystem specific driver data using the
199 * &host1x_client.parent field and driver data associated with it (usually by
200 * calling dev_get_drvdata()).
202 int host1x_device_init(struct host1x_device
*device
)
204 struct host1x_client
*client
;
207 mutex_lock(&device
->clients_lock
);
209 list_for_each_entry(client
, &device
->clients
, list
) {
210 if (client
->ops
&& client
->ops
->init
) {
211 err
= client
->ops
->init(client
);
213 dev_err(&device
->dev
,
214 "failed to initialize %s: %d\n",
215 dev_name(client
->dev
), err
);
221 mutex_unlock(&device
->clients_lock
);
226 list_for_each_entry_continue_reverse(client
, &device
->clients
, list
)
227 if (client
->ops
->exit
)
228 client
->ops
->exit(client
);
230 mutex_unlock(&device
->clients_lock
);
233 EXPORT_SYMBOL(host1x_device_init
);
236 * host1x_device_exit() - uninitialize host1x logical device
237 * @device: host1x logical device
239 * When the driver for a host1x logical device is unloaded, it can call this
240 * function to tear down each of its clients. Typically this is done after a
241 * subsystem-specific data structure is removed and the functionality can no
244 int host1x_device_exit(struct host1x_device
*device
)
246 struct host1x_client
*client
;
249 mutex_lock(&device
->clients_lock
);
251 list_for_each_entry_reverse(client
, &device
->clients
, list
) {
252 if (client
->ops
&& client
->ops
->exit
) {
253 err
= client
->ops
->exit(client
);
255 dev_err(&device
->dev
,
256 "failed to cleanup %s: %d\n",
257 dev_name(client
->dev
), err
);
258 mutex_unlock(&device
->clients_lock
);
264 mutex_unlock(&device
->clients_lock
);
268 EXPORT_SYMBOL(host1x_device_exit
);
270 static int host1x_add_client(struct host1x
*host1x
,
271 struct host1x_client
*client
)
273 struct host1x_device
*device
;
274 struct host1x_subdev
*subdev
;
276 mutex_lock(&host1x
->devices_lock
);
278 list_for_each_entry(device
, &host1x
->devices
, list
) {
279 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
280 if (subdev
->np
== client
->dev
->of_node
) {
281 host1x_subdev_register(device
, subdev
, client
);
282 mutex_unlock(&host1x
->devices_lock
);
288 mutex_unlock(&host1x
->devices_lock
);
292 static int host1x_del_client(struct host1x
*host1x
,
293 struct host1x_client
*client
)
295 struct host1x_device
*device
, *dt
;
296 struct host1x_subdev
*subdev
;
298 mutex_lock(&host1x
->devices_lock
);
300 list_for_each_entry_safe(device
, dt
, &host1x
->devices
, list
) {
301 list_for_each_entry(subdev
, &device
->active
, list
) {
302 if (subdev
->client
== client
) {
303 host1x_subdev_unregister(device
, subdev
);
304 mutex_unlock(&host1x
->devices_lock
);
310 mutex_unlock(&host1x
->devices_lock
);
314 static int host1x_device_match(struct device
*dev
, struct device_driver
*drv
)
316 return strcmp(dev_name(dev
), drv
->name
) == 0;
319 static int host1x_dma_configure(struct device
*dev
)
321 return of_dma_configure(dev
, dev
->of_node
, true);
324 static const struct dev_pm_ops host1x_device_pm_ops
= {
325 .suspend
= pm_generic_suspend
,
326 .resume
= pm_generic_resume
,
327 .freeze
= pm_generic_freeze
,
328 .thaw
= pm_generic_thaw
,
329 .poweroff
= pm_generic_poweroff
,
330 .restore
= pm_generic_restore
,
333 struct bus_type host1x_bus_type
= {
335 .match
= host1x_device_match
,
336 .dma_configure
= host1x_dma_configure
,
337 .pm
= &host1x_device_pm_ops
,
340 static void __host1x_device_del(struct host1x_device
*device
)
342 struct host1x_subdev
*subdev
, *sd
;
343 struct host1x_client
*client
, *cl
;
345 mutex_lock(&device
->subdevs_lock
);
347 /* unregister subdevices */
348 list_for_each_entry_safe(subdev
, sd
, &device
->active
, list
) {
350 * host1x_subdev_unregister() will remove the client from
351 * any lists, so we'll need to manually add it back to the
352 * list of idle clients.
354 * XXX: Alternatively, perhaps don't remove the client from
355 * any lists in host1x_subdev_unregister() and instead do
356 * that explicitly from host1x_unregister_client()?
358 client
= subdev
->client
;
360 __host1x_subdev_unregister(device
, subdev
);
362 /* add the client to the list of idle clients */
363 mutex_lock(&clients_lock
);
364 list_add_tail(&client
->list
, &clients
);
365 mutex_unlock(&clients_lock
);
368 /* remove subdevices */
369 list_for_each_entry_safe(subdev
, sd
, &device
->subdevs
, list
)
370 host1x_subdev_del(subdev
);
372 mutex_unlock(&device
->subdevs_lock
);
374 /* move clients to idle list */
375 mutex_lock(&clients_lock
);
376 mutex_lock(&device
->clients_lock
);
378 list_for_each_entry_safe(client
, cl
, &device
->clients
, list
)
379 list_move_tail(&client
->list
, &clients
);
381 mutex_unlock(&device
->clients_lock
);
382 mutex_unlock(&clients_lock
);
384 /* finally remove the device */
385 list_del_init(&device
->list
);
388 static void host1x_device_release(struct device
*dev
)
390 struct host1x_device
*device
= to_host1x_device(dev
);
392 __host1x_device_del(device
);
396 static int host1x_device_add(struct host1x
*host1x
,
397 struct host1x_driver
*driver
)
399 struct host1x_client
*client
, *tmp
;
400 struct host1x_subdev
*subdev
;
401 struct host1x_device
*device
;
404 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
408 device_initialize(&device
->dev
);
410 mutex_init(&device
->subdevs_lock
);
411 INIT_LIST_HEAD(&device
->subdevs
);
412 INIT_LIST_HEAD(&device
->active
);
413 mutex_init(&device
->clients_lock
);
414 INIT_LIST_HEAD(&device
->clients
);
415 INIT_LIST_HEAD(&device
->list
);
416 device
->driver
= driver
;
418 device
->dev
.coherent_dma_mask
= host1x
->dev
->coherent_dma_mask
;
419 device
->dev
.dma_mask
= &device
->dev
.coherent_dma_mask
;
420 dev_set_name(&device
->dev
, "%s", driver
->driver
.name
);
421 device
->dev
.release
= host1x_device_release
;
422 device
->dev
.of_node
= host1x
->dev
->of_node
;
423 device
->dev
.bus
= &host1x_bus_type
;
424 device
->dev
.parent
= host1x
->dev
;
426 of_dma_configure(&device
->dev
, host1x
->dev
->of_node
, true);
428 err
= host1x_device_parse_dt(device
, driver
);
434 list_add_tail(&device
->list
, &host1x
->devices
);
436 mutex_lock(&clients_lock
);
438 list_for_each_entry_safe(client
, tmp
, &clients
, list
) {
439 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
440 if (subdev
->np
== client
->dev
->of_node
) {
441 host1x_subdev_register(device
, subdev
, client
);
447 mutex_unlock(&clients_lock
);
453 * Removes a device by first unregistering any subdevices and then removing
454 * itself from the list of devices.
456 * This function must be called with the host1x->devices_lock held.
458 static void host1x_device_del(struct host1x
*host1x
,
459 struct host1x_device
*device
)
461 if (device
->registered
) {
462 device
->registered
= false;
463 device_del(&device
->dev
);
466 put_device(&device
->dev
);
469 static void host1x_attach_driver(struct host1x
*host1x
,
470 struct host1x_driver
*driver
)
472 struct host1x_device
*device
;
475 mutex_lock(&host1x
->devices_lock
);
477 list_for_each_entry(device
, &host1x
->devices
, list
) {
478 if (device
->driver
== driver
) {
479 mutex_unlock(&host1x
->devices_lock
);
484 err
= host1x_device_add(host1x
, driver
);
486 dev_err(host1x
->dev
, "failed to allocate device: %d\n", err
);
488 mutex_unlock(&host1x
->devices_lock
);
491 static void host1x_detach_driver(struct host1x
*host1x
,
492 struct host1x_driver
*driver
)
494 struct host1x_device
*device
, *tmp
;
496 mutex_lock(&host1x
->devices_lock
);
498 list_for_each_entry_safe(device
, tmp
, &host1x
->devices
, list
)
499 if (device
->driver
== driver
)
500 host1x_device_del(host1x
, device
);
502 mutex_unlock(&host1x
->devices_lock
);
505 static int host1x_devices_show(struct seq_file
*s
, void *data
)
507 struct host1x
*host1x
= s
->private;
508 struct host1x_device
*device
;
510 mutex_lock(&host1x
->devices_lock
);
512 list_for_each_entry(device
, &host1x
->devices
, list
) {
513 struct host1x_subdev
*subdev
;
515 seq_printf(s
, "%s\n", dev_name(&device
->dev
));
517 mutex_lock(&device
->subdevs_lock
);
519 list_for_each_entry(subdev
, &device
->active
, list
)
520 seq_printf(s
, " %pOFf: %s\n", subdev
->np
,
521 dev_name(subdev
->client
->dev
));
523 list_for_each_entry(subdev
, &device
->subdevs
, list
)
524 seq_printf(s
, " %pOFf:\n", subdev
->np
);
526 mutex_unlock(&device
->subdevs_lock
);
529 mutex_unlock(&host1x
->devices_lock
);
533 DEFINE_SHOW_ATTRIBUTE(host1x_devices
);
536 * host1x_register() - register a host1x controller
537 * @host1x: host1x controller
539 * The host1x controller driver uses this to register a host1x controller with
540 * the infrastructure. Note that all Tegra SoC generations have only ever come
541 * with a single host1x instance, so this function is somewhat academic.
543 int host1x_register(struct host1x
*host1x
)
545 struct host1x_driver
*driver
;
547 mutex_lock(&devices_lock
);
548 list_add_tail(&host1x
->list
, &devices
);
549 mutex_unlock(&devices_lock
);
551 mutex_lock(&drivers_lock
);
553 list_for_each_entry(driver
, &drivers
, list
)
554 host1x_attach_driver(host1x
, driver
);
556 mutex_unlock(&drivers_lock
);
558 debugfs_create_file("devices", S_IRUGO
, host1x
->debugfs
, host1x
,
559 &host1x_devices_fops
);
565 * host1x_unregister() - unregister a host1x controller
566 * @host1x: host1x controller
568 * The host1x controller driver uses this to remove a host1x controller from
569 * the infrastructure.
571 int host1x_unregister(struct host1x
*host1x
)
573 struct host1x_driver
*driver
;
575 mutex_lock(&drivers_lock
);
577 list_for_each_entry(driver
, &drivers
, list
)
578 host1x_detach_driver(host1x
, driver
);
580 mutex_unlock(&drivers_lock
);
582 mutex_lock(&devices_lock
);
583 list_del_init(&host1x
->list
);
584 mutex_unlock(&devices_lock
);
589 static int host1x_device_probe(struct device
*dev
)
591 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
592 struct host1x_device
*device
= to_host1x_device(dev
);
595 return driver
->probe(device
);
600 static int host1x_device_remove(struct device
*dev
)
602 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
603 struct host1x_device
*device
= to_host1x_device(dev
);
606 return driver
->remove(device
);
611 static void host1x_device_shutdown(struct device
*dev
)
613 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
614 struct host1x_device
*device
= to_host1x_device(dev
);
616 if (driver
->shutdown
)
617 driver
->shutdown(device
);
621 * host1x_driver_register_full() - register a host1x driver
622 * @driver: host1x driver
623 * @owner: owner module
625 * Drivers for host1x logical devices call this function to register a driver
626 * with the infrastructure. Note that since these drive logical devices, the
627 * registration of the driver actually triggers tho logical device creation.
628 * A logical device will be created for each host1x instance.
630 int host1x_driver_register_full(struct host1x_driver
*driver
,
631 struct module
*owner
)
633 struct host1x
*host1x
;
635 INIT_LIST_HEAD(&driver
->list
);
637 mutex_lock(&drivers_lock
);
638 list_add_tail(&driver
->list
, &drivers
);
639 mutex_unlock(&drivers_lock
);
641 mutex_lock(&devices_lock
);
643 list_for_each_entry(host1x
, &devices
, list
)
644 host1x_attach_driver(host1x
, driver
);
646 mutex_unlock(&devices_lock
);
648 driver
->driver
.bus
= &host1x_bus_type
;
649 driver
->driver
.owner
= owner
;
650 driver
->driver
.probe
= host1x_device_probe
;
651 driver
->driver
.remove
= host1x_device_remove
;
652 driver
->driver
.shutdown
= host1x_device_shutdown
;
654 return driver_register(&driver
->driver
);
656 EXPORT_SYMBOL(host1x_driver_register_full
);
659 * host1x_driver_unregister() - unregister a host1x driver
660 * @driver: host1x driver
662 * Unbinds the driver from each of the host1x logical devices that it is
663 * bound to, effectively removing the subsystem devices that they represent.
665 void host1x_driver_unregister(struct host1x_driver
*driver
)
667 driver_unregister(&driver
->driver
);
669 mutex_lock(&drivers_lock
);
670 list_del_init(&driver
->list
);
671 mutex_unlock(&drivers_lock
);
673 EXPORT_SYMBOL(host1x_driver_unregister
);
676 * host1x_client_register() - register a host1x client
677 * @client: host1x client
679 * Registers a host1x client with each host1x controller instance. Note that
680 * each client will only match their parent host1x controller and will only be
681 * associated with that instance. Once all clients have been registered with
682 * their parent host1x controller, the infrastructure will set up the logical
683 * device and call host1x_device_init(), which will in turn call each client's
684 * &host1x_client_ops.init implementation.
686 int host1x_client_register(struct host1x_client
*client
)
688 struct host1x
*host1x
;
691 mutex_lock(&devices_lock
);
693 list_for_each_entry(host1x
, &devices
, list
) {
694 err
= host1x_add_client(host1x
, client
);
696 mutex_unlock(&devices_lock
);
701 mutex_unlock(&devices_lock
);
703 mutex_lock(&clients_lock
);
704 list_add_tail(&client
->list
, &clients
);
705 mutex_unlock(&clients_lock
);
709 EXPORT_SYMBOL(host1x_client_register
);
712 * host1x_client_unregister() - unregister a host1x client
713 * @client: host1x client
715 * Removes a host1x client from its host1x controller instance. If a logical
716 * device has already been initialized, it will be torn down.
718 int host1x_client_unregister(struct host1x_client
*client
)
720 struct host1x_client
*c
;
721 struct host1x
*host1x
;
724 mutex_lock(&devices_lock
);
726 list_for_each_entry(host1x
, &devices
, list
) {
727 err
= host1x_del_client(host1x
, client
);
729 mutex_unlock(&devices_lock
);
734 mutex_unlock(&devices_lock
);
735 mutex_lock(&clients_lock
);
737 list_for_each_entry(c
, &clients
, list
) {
739 list_del_init(&c
->list
);
744 mutex_unlock(&clients_lock
);
748 EXPORT_SYMBOL(host1x_client_unregister
);