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/host1x.h>
20 #include <linux/slab.h>
21 #include <linux/of_device.h>
26 static DEFINE_MUTEX(clients_lock
);
27 static LIST_HEAD(clients
);
29 static DEFINE_MUTEX(drivers_lock
);
30 static LIST_HEAD(drivers
);
32 static DEFINE_MUTEX(devices_lock
);
33 static LIST_HEAD(devices
);
35 struct host1x_subdev
{
36 struct host1x_client
*client
;
37 struct device_node
*np
;
38 struct list_head list
;
42 * host1x_subdev_add() - add a new subdevice with an associated device node
43 * @device: host1x device to add the subdevice to
44 * @driver: host1x driver
47 static int host1x_subdev_add(struct host1x_device
*device
,
48 struct device_node
*np
)
50 struct host1x_subdev
*subdev
;
52 subdev
= kzalloc(sizeof(*subdev
), GFP_KERNEL
);
56 INIT_LIST_HEAD(&subdev
->list
);
57 subdev
->np
= of_node_get(np
);
59 mutex_lock(&device
->subdevs_lock
);
60 list_add_tail(&subdev
->list
, &device
->subdevs
);
61 mutex_unlock(&device
->subdevs_lock
);
67 * host1x_subdev_del() - remove subdevice
68 * @subdev: subdevice to remove
70 static void host1x_subdev_del(struct host1x_subdev
*subdev
)
72 list_del(&subdev
->list
);
73 of_node_put(subdev
->np
);
78 * host1x_device_parse_dt() - scan device tree and add matching subdevices
79 * @device: host1x logical device
80 * @driver: host1x driver
82 static int host1x_device_parse_dt(struct host1x_device
*device
,
83 struct host1x_driver
*driver
)
85 struct device_node
*np
;
88 for_each_child_of_node(device
->dev
.parent
->of_node
, np
) {
89 if (of_match_node(driver
->subdevs
, np
) &&
90 of_device_is_available(np
)) {
91 err
= host1x_subdev_add(device
, np
);
102 static void host1x_subdev_register(struct host1x_device
*device
,
103 struct host1x_subdev
*subdev
,
104 struct host1x_client
*client
)
109 * Move the subdevice to the list of active (registered) subdevices
110 * and associate it with a client. At the same time, associate the
111 * client with its parent device.
113 mutex_lock(&device
->subdevs_lock
);
114 mutex_lock(&device
->clients_lock
);
115 list_move_tail(&client
->list
, &device
->clients
);
116 list_move_tail(&subdev
->list
, &device
->active
);
117 client
->parent
= &device
->dev
;
118 subdev
->client
= client
;
119 mutex_unlock(&device
->clients_lock
);
120 mutex_unlock(&device
->subdevs_lock
);
122 if (list_empty(&device
->subdevs
)) {
123 err
= device_add(&device
->dev
);
125 dev_err(&device
->dev
, "failed to add: %d\n", err
);
127 device
->registered
= true;
131 static void __host1x_subdev_unregister(struct host1x_device
*device
,
132 struct host1x_subdev
*subdev
)
134 struct host1x_client
*client
= subdev
->client
;
137 * If all subdevices have been activated, we're about to remove the
138 * first active subdevice, so unload the driver first.
140 if (list_empty(&device
->subdevs
)) {
141 if (device
->registered
) {
142 device
->registered
= false;
143 device_del(&device
->dev
);
148 * Move the subdevice back to the list of idle subdevices and remove
149 * it from list of clients.
151 mutex_lock(&device
->clients_lock
);
152 subdev
->client
= NULL
;
153 client
->parent
= NULL
;
154 list_move_tail(&subdev
->list
, &device
->subdevs
);
156 * XXX: Perhaps don't do this here, but rather explicitly remove it
157 * when the device is about to be deleted.
159 * This is somewhat complicated by the fact that this function is
160 * used to remove the subdevice when a client is unregistered but
161 * also when the composite device is about to be removed.
163 list_del_init(&client
->list
);
164 mutex_unlock(&device
->clients_lock
);
167 static void host1x_subdev_unregister(struct host1x_device
*device
,
168 struct host1x_subdev
*subdev
)
170 mutex_lock(&device
->subdevs_lock
);
171 __host1x_subdev_unregister(device
, subdev
);
172 mutex_unlock(&device
->subdevs_lock
);
176 * host1x_device_init() - initialize a host1x logical device
177 * @device: host1x logical device
179 * The driver for the host1x logical device can call this during execution of
180 * its &host1x_driver.probe implementation to initialize each of its clients.
181 * The client drivers access the subsystem specific driver data using the
182 * &host1x_client.parent field and driver data associated with it (usually by
183 * calling dev_get_drvdata()).
185 int host1x_device_init(struct host1x_device
*device
)
187 struct host1x_client
*client
;
190 mutex_lock(&device
->clients_lock
);
192 list_for_each_entry(client
, &device
->clients
, list
) {
193 if (client
->ops
&& client
->ops
->init
) {
194 err
= client
->ops
->init(client
);
196 dev_err(&device
->dev
,
197 "failed to initialize %s: %d\n",
198 dev_name(client
->dev
), err
);
199 mutex_unlock(&device
->clients_lock
);
205 mutex_unlock(&device
->clients_lock
);
209 EXPORT_SYMBOL(host1x_device_init
);
212 * host1x_device_exit() - uninitialize host1x logical device
213 * @device: host1x logical device
215 * When the driver for a host1x logical device is unloaded, it can call this
216 * function to tear down each of its clients. Typically this is done after a
217 * subsystem-specific data structure is removed and the functionality can no
220 int host1x_device_exit(struct host1x_device
*device
)
222 struct host1x_client
*client
;
225 mutex_lock(&device
->clients_lock
);
227 list_for_each_entry_reverse(client
, &device
->clients
, list
) {
228 if (client
->ops
&& client
->ops
->exit
) {
229 err
= client
->ops
->exit(client
);
231 dev_err(&device
->dev
,
232 "failed to cleanup %s: %d\n",
233 dev_name(client
->dev
), err
);
234 mutex_unlock(&device
->clients_lock
);
240 mutex_unlock(&device
->clients_lock
);
244 EXPORT_SYMBOL(host1x_device_exit
);
246 static int host1x_add_client(struct host1x
*host1x
,
247 struct host1x_client
*client
)
249 struct host1x_device
*device
;
250 struct host1x_subdev
*subdev
;
252 mutex_lock(&host1x
->devices_lock
);
254 list_for_each_entry(device
, &host1x
->devices
, list
) {
255 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
256 if (subdev
->np
== client
->dev
->of_node
) {
257 host1x_subdev_register(device
, subdev
, client
);
258 mutex_unlock(&host1x
->devices_lock
);
264 mutex_unlock(&host1x
->devices_lock
);
268 static int host1x_del_client(struct host1x
*host1x
,
269 struct host1x_client
*client
)
271 struct host1x_device
*device
, *dt
;
272 struct host1x_subdev
*subdev
;
274 mutex_lock(&host1x
->devices_lock
);
276 list_for_each_entry_safe(device
, dt
, &host1x
->devices
, list
) {
277 list_for_each_entry(subdev
, &device
->active
, list
) {
278 if (subdev
->client
== client
) {
279 host1x_subdev_unregister(device
, subdev
);
280 mutex_unlock(&host1x
->devices_lock
);
286 mutex_unlock(&host1x
->devices_lock
);
290 static int host1x_device_match(struct device
*dev
, struct device_driver
*drv
)
292 return strcmp(dev_name(dev
), drv
->name
) == 0;
295 static const struct dev_pm_ops host1x_device_pm_ops
= {
296 .suspend
= pm_generic_suspend
,
297 .resume
= pm_generic_resume
,
298 .freeze
= pm_generic_freeze
,
299 .thaw
= pm_generic_thaw
,
300 .poweroff
= pm_generic_poweroff
,
301 .restore
= pm_generic_restore
,
304 struct bus_type host1x_bus_type
= {
306 .match
= host1x_device_match
,
307 .pm
= &host1x_device_pm_ops
,
310 static void __host1x_device_del(struct host1x_device
*device
)
312 struct host1x_subdev
*subdev
, *sd
;
313 struct host1x_client
*client
, *cl
;
315 mutex_lock(&device
->subdevs_lock
);
317 /* unregister subdevices */
318 list_for_each_entry_safe(subdev
, sd
, &device
->active
, list
) {
320 * host1x_subdev_unregister() will remove the client from
321 * any lists, so we'll need to manually add it back to the
322 * list of idle clients.
324 * XXX: Alternatively, perhaps don't remove the client from
325 * any lists in host1x_subdev_unregister() and instead do
326 * that explicitly from host1x_unregister_client()?
328 client
= subdev
->client
;
330 __host1x_subdev_unregister(device
, subdev
);
332 /* add the client to the list of idle clients */
333 mutex_lock(&clients_lock
);
334 list_add_tail(&client
->list
, &clients
);
335 mutex_unlock(&clients_lock
);
338 /* remove subdevices */
339 list_for_each_entry_safe(subdev
, sd
, &device
->subdevs
, list
)
340 host1x_subdev_del(subdev
);
342 mutex_unlock(&device
->subdevs_lock
);
344 /* move clients to idle list */
345 mutex_lock(&clients_lock
);
346 mutex_lock(&device
->clients_lock
);
348 list_for_each_entry_safe(client
, cl
, &device
->clients
, list
)
349 list_move_tail(&client
->list
, &clients
);
351 mutex_unlock(&device
->clients_lock
);
352 mutex_unlock(&clients_lock
);
354 /* finally remove the device */
355 list_del_init(&device
->list
);
358 static void host1x_device_release(struct device
*dev
)
360 struct host1x_device
*device
= to_host1x_device(dev
);
362 __host1x_device_del(device
);
366 static int host1x_device_add(struct host1x
*host1x
,
367 struct host1x_driver
*driver
)
369 struct host1x_client
*client
, *tmp
;
370 struct host1x_subdev
*subdev
;
371 struct host1x_device
*device
;
374 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
378 device_initialize(&device
->dev
);
380 mutex_init(&device
->subdevs_lock
);
381 INIT_LIST_HEAD(&device
->subdevs
);
382 INIT_LIST_HEAD(&device
->active
);
383 mutex_init(&device
->clients_lock
);
384 INIT_LIST_HEAD(&device
->clients
);
385 INIT_LIST_HEAD(&device
->list
);
386 device
->driver
= driver
;
388 device
->dev
.coherent_dma_mask
= host1x
->dev
->coherent_dma_mask
;
389 device
->dev
.dma_mask
= &device
->dev
.coherent_dma_mask
;
390 dev_set_name(&device
->dev
, "%s", driver
->driver
.name
);
391 of_dma_configure(&device
->dev
, host1x
->dev
->of_node
);
392 device
->dev
.release
= host1x_device_release
;
393 device
->dev
.of_node
= host1x
->dev
->of_node
;
394 device
->dev
.bus
= &host1x_bus_type
;
395 device
->dev
.parent
= host1x
->dev
;
397 err
= host1x_device_parse_dt(device
, driver
);
403 list_add_tail(&device
->list
, &host1x
->devices
);
405 mutex_lock(&clients_lock
);
407 list_for_each_entry_safe(client
, tmp
, &clients
, list
) {
408 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
409 if (subdev
->np
== client
->dev
->of_node
) {
410 host1x_subdev_register(device
, subdev
, client
);
416 mutex_unlock(&clients_lock
);
422 * Removes a device by first unregistering any subdevices and then removing
423 * itself from the list of devices.
425 * This function must be called with the host1x->devices_lock held.
427 static void host1x_device_del(struct host1x
*host1x
,
428 struct host1x_device
*device
)
430 if (device
->registered
) {
431 device
->registered
= false;
432 device_del(&device
->dev
);
435 put_device(&device
->dev
);
438 static void host1x_attach_driver(struct host1x
*host1x
,
439 struct host1x_driver
*driver
)
441 struct host1x_device
*device
;
444 mutex_lock(&host1x
->devices_lock
);
446 list_for_each_entry(device
, &host1x
->devices
, list
) {
447 if (device
->driver
== driver
) {
448 mutex_unlock(&host1x
->devices_lock
);
453 err
= host1x_device_add(host1x
, driver
);
455 dev_err(host1x
->dev
, "failed to allocate device: %d\n", err
);
457 mutex_unlock(&host1x
->devices_lock
);
460 static void host1x_detach_driver(struct host1x
*host1x
,
461 struct host1x_driver
*driver
)
463 struct host1x_device
*device
, *tmp
;
465 mutex_lock(&host1x
->devices_lock
);
467 list_for_each_entry_safe(device
, tmp
, &host1x
->devices
, list
)
468 if (device
->driver
== driver
)
469 host1x_device_del(host1x
, device
);
471 mutex_unlock(&host1x
->devices_lock
);
475 * host1x_register() - register a host1x controller
476 * @host1x: host1x controller
478 * The host1x controller driver uses this to register a host1x controller with
479 * the infrastructure. Note that all Tegra SoC generations have only ever come
480 * with a single host1x instance, so this function is somewhat academic.
482 int host1x_register(struct host1x
*host1x
)
484 struct host1x_driver
*driver
;
486 mutex_lock(&devices_lock
);
487 list_add_tail(&host1x
->list
, &devices
);
488 mutex_unlock(&devices_lock
);
490 mutex_lock(&drivers_lock
);
492 list_for_each_entry(driver
, &drivers
, list
)
493 host1x_attach_driver(host1x
, driver
);
495 mutex_unlock(&drivers_lock
);
501 * host1x_unregister() - unregister a host1x controller
502 * @host1x: host1x controller
504 * The host1x controller driver uses this to remove a host1x controller from
505 * the infrastructure.
507 int host1x_unregister(struct host1x
*host1x
)
509 struct host1x_driver
*driver
;
511 mutex_lock(&drivers_lock
);
513 list_for_each_entry(driver
, &drivers
, list
)
514 host1x_detach_driver(host1x
, driver
);
516 mutex_unlock(&drivers_lock
);
518 mutex_lock(&devices_lock
);
519 list_del_init(&host1x
->list
);
520 mutex_unlock(&devices_lock
);
525 static int host1x_device_probe(struct device
*dev
)
527 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
528 struct host1x_device
*device
= to_host1x_device(dev
);
531 return driver
->probe(device
);
536 static int host1x_device_remove(struct device
*dev
)
538 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
539 struct host1x_device
*device
= to_host1x_device(dev
);
542 return driver
->remove(device
);
547 static void host1x_device_shutdown(struct device
*dev
)
549 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
550 struct host1x_device
*device
= to_host1x_device(dev
);
552 if (driver
->shutdown
)
553 driver
->shutdown(device
);
557 * host1x_driver_register_full() - register a host1x driver
558 * @driver: host1x driver
559 * @owner: owner module
561 * Drivers for host1x logical devices call this function to register a driver
562 * with the infrastructure. Note that since these drive logical devices, the
563 * registration of the driver actually triggers tho logical device creation.
564 * A logical device will be created for each host1x instance.
566 int host1x_driver_register_full(struct host1x_driver
*driver
,
567 struct module
*owner
)
569 struct host1x
*host1x
;
571 INIT_LIST_HEAD(&driver
->list
);
573 mutex_lock(&drivers_lock
);
574 list_add_tail(&driver
->list
, &drivers
);
575 mutex_unlock(&drivers_lock
);
577 mutex_lock(&devices_lock
);
579 list_for_each_entry(host1x
, &devices
, list
)
580 host1x_attach_driver(host1x
, driver
);
582 mutex_unlock(&devices_lock
);
584 driver
->driver
.bus
= &host1x_bus_type
;
585 driver
->driver
.owner
= owner
;
586 driver
->driver
.probe
= host1x_device_probe
;
587 driver
->driver
.remove
= host1x_device_remove
;
588 driver
->driver
.shutdown
= host1x_device_shutdown
;
590 return driver_register(&driver
->driver
);
592 EXPORT_SYMBOL(host1x_driver_register_full
);
595 * host1x_driver_unregister() - unregister a host1x driver
596 * @driver: host1x driver
598 * Unbinds the driver from each of the host1x logical devices that it is
599 * bound to, effectively removing the subsystem devices that they represent.
601 void host1x_driver_unregister(struct host1x_driver
*driver
)
603 driver_unregister(&driver
->driver
);
605 mutex_lock(&drivers_lock
);
606 list_del_init(&driver
->list
);
607 mutex_unlock(&drivers_lock
);
609 EXPORT_SYMBOL(host1x_driver_unregister
);
612 * host1x_client_register() - register a host1x client
613 * @client: host1x client
615 * Registers a host1x client with each host1x controller instance. Note that
616 * each client will only match their parent host1x controller and will only be
617 * associated with that instance. Once all clients have been registered with
618 * their parent host1x controller, the infrastructure will set up the logical
619 * device and call host1x_device_init(), which will in turn call each client's
620 * &host1x_client_ops.init implementation.
622 int host1x_client_register(struct host1x_client
*client
)
624 struct host1x
*host1x
;
627 mutex_lock(&devices_lock
);
629 list_for_each_entry(host1x
, &devices
, list
) {
630 err
= host1x_add_client(host1x
, client
);
632 mutex_unlock(&devices_lock
);
637 mutex_unlock(&devices_lock
);
639 mutex_lock(&clients_lock
);
640 list_add_tail(&client
->list
, &clients
);
641 mutex_unlock(&clients_lock
);
645 EXPORT_SYMBOL(host1x_client_register
);
648 * host1x_client_unregister() - unregister a host1x client
649 * @client: host1x client
651 * Removes a host1x client from its host1x controller instance. If a logical
652 * device has already been initialized, it will be torn down.
654 int host1x_client_unregister(struct host1x_client
*client
)
656 struct host1x_client
*c
;
657 struct host1x
*host1x
;
660 mutex_lock(&devices_lock
);
662 list_for_each_entry(host1x
, &devices
, list
) {
663 err
= host1x_del_client(host1x
, client
);
665 mutex_unlock(&devices_lock
);
670 mutex_unlock(&devices_lock
);
671 mutex_lock(&clients_lock
);
673 list_for_each_entry(c
, &clients
, list
) {
675 list_del_init(&c
->list
);
680 mutex_unlock(&clients_lock
);
684 EXPORT_SYMBOL(host1x_client_unregister
);