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
46 static int host1x_subdev_add(struct host1x_device
*device
,
47 struct host1x_driver
*driver
,
48 struct device_node
*np
)
50 struct host1x_subdev
*subdev
;
51 struct device_node
*child
;
54 subdev
= kzalloc(sizeof(*subdev
), GFP_KERNEL
);
58 INIT_LIST_HEAD(&subdev
->list
);
59 subdev
->np
= of_node_get(np
);
61 mutex_lock(&device
->subdevs_lock
);
62 list_add_tail(&subdev
->list
, &device
->subdevs
);
63 mutex_unlock(&device
->subdevs_lock
);
65 /* recursively add children */
66 for_each_child_of_node(np
, child
) {
67 if (of_match_node(driver
->subdevs
, child
) &&
68 of_device_is_available(child
)) {
69 err
= host1x_subdev_add(device
, driver
, child
);
82 * host1x_subdev_del() - remove subdevice
83 * @subdev: subdevice to remove
85 static void host1x_subdev_del(struct host1x_subdev
*subdev
)
87 list_del(&subdev
->list
);
88 of_node_put(subdev
->np
);
93 * host1x_device_parse_dt() - scan device tree and add matching subdevices
94 * @device: host1x logical device
95 * @driver: host1x driver
97 static int host1x_device_parse_dt(struct host1x_device
*device
,
98 struct host1x_driver
*driver
)
100 struct device_node
*np
;
103 for_each_child_of_node(device
->dev
.parent
->of_node
, np
) {
104 if (of_match_node(driver
->subdevs
, np
) &&
105 of_device_is_available(np
)) {
106 err
= host1x_subdev_add(device
, driver
, np
);
117 static void host1x_subdev_register(struct host1x_device
*device
,
118 struct host1x_subdev
*subdev
,
119 struct host1x_client
*client
)
124 * Move the subdevice to the list of active (registered) subdevices
125 * and associate it with a client. At the same time, associate the
126 * client with its parent device.
128 mutex_lock(&device
->subdevs_lock
);
129 mutex_lock(&device
->clients_lock
);
130 list_move_tail(&client
->list
, &device
->clients
);
131 list_move_tail(&subdev
->list
, &device
->active
);
132 client
->parent
= &device
->dev
;
133 subdev
->client
= client
;
134 mutex_unlock(&device
->clients_lock
);
135 mutex_unlock(&device
->subdevs_lock
);
137 if (list_empty(&device
->subdevs
)) {
138 err
= device_add(&device
->dev
);
140 dev_err(&device
->dev
, "failed to add: %d\n", err
);
142 device
->registered
= true;
146 static void __host1x_subdev_unregister(struct host1x_device
*device
,
147 struct host1x_subdev
*subdev
)
149 struct host1x_client
*client
= subdev
->client
;
152 * If all subdevices have been activated, we're about to remove the
153 * first active subdevice, so unload the driver first.
155 if (list_empty(&device
->subdevs
)) {
156 if (device
->registered
) {
157 device
->registered
= false;
158 device_del(&device
->dev
);
163 * Move the subdevice back to the list of idle subdevices and remove
164 * it from list of clients.
166 mutex_lock(&device
->clients_lock
);
167 subdev
->client
= NULL
;
168 client
->parent
= NULL
;
169 list_move_tail(&subdev
->list
, &device
->subdevs
);
171 * XXX: Perhaps don't do this here, but rather explicitly remove it
172 * when the device is about to be deleted.
174 * This is somewhat complicated by the fact that this function is
175 * used to remove the subdevice when a client is unregistered but
176 * also when the composite device is about to be removed.
178 list_del_init(&client
->list
);
179 mutex_unlock(&device
->clients_lock
);
182 static void host1x_subdev_unregister(struct host1x_device
*device
,
183 struct host1x_subdev
*subdev
)
185 mutex_lock(&device
->subdevs_lock
);
186 __host1x_subdev_unregister(device
, subdev
);
187 mutex_unlock(&device
->subdevs_lock
);
191 * host1x_device_init() - initialize a host1x logical device
192 * @device: host1x logical device
194 * The driver for the host1x logical device can call this during execution of
195 * its &host1x_driver.probe implementation to initialize each of its clients.
196 * The client drivers access the subsystem specific driver data using the
197 * &host1x_client.parent field and driver data associated with it (usually by
198 * calling dev_get_drvdata()).
200 int host1x_device_init(struct host1x_device
*device
)
202 struct host1x_client
*client
;
205 mutex_lock(&device
->clients_lock
);
207 list_for_each_entry(client
, &device
->clients
, list
) {
208 if (client
->ops
&& client
->ops
->init
) {
209 err
= client
->ops
->init(client
);
211 dev_err(&device
->dev
,
212 "failed to initialize %s: %d\n",
213 dev_name(client
->dev
), err
);
219 mutex_unlock(&device
->clients_lock
);
224 list_for_each_entry_continue_reverse(client
, &device
->clients
, list
)
225 if (client
->ops
->exit
)
226 client
->ops
->exit(client
);
228 mutex_unlock(&device
->clients_lock
);
231 EXPORT_SYMBOL(host1x_device_init
);
234 * host1x_device_exit() - uninitialize host1x logical device
235 * @device: host1x logical device
237 * When the driver for a host1x logical device is unloaded, it can call this
238 * function to tear down each of its clients. Typically this is done after a
239 * subsystem-specific data structure is removed and the functionality can no
242 int host1x_device_exit(struct host1x_device
*device
)
244 struct host1x_client
*client
;
247 mutex_lock(&device
->clients_lock
);
249 list_for_each_entry_reverse(client
, &device
->clients
, list
) {
250 if (client
->ops
&& client
->ops
->exit
) {
251 err
= client
->ops
->exit(client
);
253 dev_err(&device
->dev
,
254 "failed to cleanup %s: %d\n",
255 dev_name(client
->dev
), err
);
256 mutex_unlock(&device
->clients_lock
);
262 mutex_unlock(&device
->clients_lock
);
266 EXPORT_SYMBOL(host1x_device_exit
);
268 static int host1x_add_client(struct host1x
*host1x
,
269 struct host1x_client
*client
)
271 struct host1x_device
*device
;
272 struct host1x_subdev
*subdev
;
274 mutex_lock(&host1x
->devices_lock
);
276 list_for_each_entry(device
, &host1x
->devices
, list
) {
277 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
278 if (subdev
->np
== client
->dev
->of_node
) {
279 host1x_subdev_register(device
, subdev
, client
);
280 mutex_unlock(&host1x
->devices_lock
);
286 mutex_unlock(&host1x
->devices_lock
);
290 static int host1x_del_client(struct host1x
*host1x
,
291 struct host1x_client
*client
)
293 struct host1x_device
*device
, *dt
;
294 struct host1x_subdev
*subdev
;
296 mutex_lock(&host1x
->devices_lock
);
298 list_for_each_entry_safe(device
, dt
, &host1x
->devices
, list
) {
299 list_for_each_entry(subdev
, &device
->active
, list
) {
300 if (subdev
->client
== client
) {
301 host1x_subdev_unregister(device
, subdev
);
302 mutex_unlock(&host1x
->devices_lock
);
308 mutex_unlock(&host1x
->devices_lock
);
312 static int host1x_device_match(struct device
*dev
, struct device_driver
*drv
)
314 return strcmp(dev_name(dev
), drv
->name
) == 0;
317 static const struct dev_pm_ops host1x_device_pm_ops
= {
318 .suspend
= pm_generic_suspend
,
319 .resume
= pm_generic_resume
,
320 .freeze
= pm_generic_freeze
,
321 .thaw
= pm_generic_thaw
,
322 .poweroff
= pm_generic_poweroff
,
323 .restore
= pm_generic_restore
,
326 struct bus_type host1x_bus_type
= {
328 .match
= host1x_device_match
,
329 .pm
= &host1x_device_pm_ops
,
333 static void __host1x_device_del(struct host1x_device
*device
)
335 struct host1x_subdev
*subdev
, *sd
;
336 struct host1x_client
*client
, *cl
;
338 mutex_lock(&device
->subdevs_lock
);
340 /* unregister subdevices */
341 list_for_each_entry_safe(subdev
, sd
, &device
->active
, list
) {
343 * host1x_subdev_unregister() will remove the client from
344 * any lists, so we'll need to manually add it back to the
345 * list of idle clients.
347 * XXX: Alternatively, perhaps don't remove the client from
348 * any lists in host1x_subdev_unregister() and instead do
349 * that explicitly from host1x_unregister_client()?
351 client
= subdev
->client
;
353 __host1x_subdev_unregister(device
, subdev
);
355 /* add the client to the list of idle clients */
356 mutex_lock(&clients_lock
);
357 list_add_tail(&client
->list
, &clients
);
358 mutex_unlock(&clients_lock
);
361 /* remove subdevices */
362 list_for_each_entry_safe(subdev
, sd
, &device
->subdevs
, list
)
363 host1x_subdev_del(subdev
);
365 mutex_unlock(&device
->subdevs_lock
);
367 /* move clients to idle list */
368 mutex_lock(&clients_lock
);
369 mutex_lock(&device
->clients_lock
);
371 list_for_each_entry_safe(client
, cl
, &device
->clients
, list
)
372 list_move_tail(&client
->list
, &clients
);
374 mutex_unlock(&device
->clients_lock
);
375 mutex_unlock(&clients_lock
);
377 /* finally remove the device */
378 list_del_init(&device
->list
);
381 static void host1x_device_release(struct device
*dev
)
383 struct host1x_device
*device
= to_host1x_device(dev
);
385 __host1x_device_del(device
);
389 static int host1x_device_add(struct host1x
*host1x
,
390 struct host1x_driver
*driver
)
392 struct host1x_client
*client
, *tmp
;
393 struct host1x_subdev
*subdev
;
394 struct host1x_device
*device
;
397 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
401 device_initialize(&device
->dev
);
403 mutex_init(&device
->subdevs_lock
);
404 INIT_LIST_HEAD(&device
->subdevs
);
405 INIT_LIST_HEAD(&device
->active
);
406 mutex_init(&device
->clients_lock
);
407 INIT_LIST_HEAD(&device
->clients
);
408 INIT_LIST_HEAD(&device
->list
);
409 device
->driver
= driver
;
411 device
->dev
.coherent_dma_mask
= host1x
->dev
->coherent_dma_mask
;
412 device
->dev
.dma_mask
= &device
->dev
.coherent_dma_mask
;
413 dev_set_name(&device
->dev
, "%s", driver
->driver
.name
);
414 device
->dev
.release
= host1x_device_release
;
415 device
->dev
.of_node
= host1x
->dev
->of_node
;
416 device
->dev
.bus
= &host1x_bus_type
;
417 device
->dev
.parent
= host1x
->dev
;
419 of_dma_configure(&device
->dev
, host1x
->dev
->of_node
);
421 err
= host1x_device_parse_dt(device
, driver
);
427 list_add_tail(&device
->list
, &host1x
->devices
);
429 mutex_lock(&clients_lock
);
431 list_for_each_entry_safe(client
, tmp
, &clients
, list
) {
432 list_for_each_entry(subdev
, &device
->subdevs
, list
) {
433 if (subdev
->np
== client
->dev
->of_node
) {
434 host1x_subdev_register(device
, subdev
, client
);
440 mutex_unlock(&clients_lock
);
446 * Removes a device by first unregistering any subdevices and then removing
447 * itself from the list of devices.
449 * This function must be called with the host1x->devices_lock held.
451 static void host1x_device_del(struct host1x
*host1x
,
452 struct host1x_device
*device
)
454 if (device
->registered
) {
455 device
->registered
= false;
456 device_del(&device
->dev
);
459 put_device(&device
->dev
);
462 static void host1x_attach_driver(struct host1x
*host1x
,
463 struct host1x_driver
*driver
)
465 struct host1x_device
*device
;
468 mutex_lock(&host1x
->devices_lock
);
470 list_for_each_entry(device
, &host1x
->devices
, list
) {
471 if (device
->driver
== driver
) {
472 mutex_unlock(&host1x
->devices_lock
);
477 err
= host1x_device_add(host1x
, driver
);
479 dev_err(host1x
->dev
, "failed to allocate device: %d\n", err
);
481 mutex_unlock(&host1x
->devices_lock
);
484 static void host1x_detach_driver(struct host1x
*host1x
,
485 struct host1x_driver
*driver
)
487 struct host1x_device
*device
, *tmp
;
489 mutex_lock(&host1x
->devices_lock
);
491 list_for_each_entry_safe(device
, tmp
, &host1x
->devices
, list
)
492 if (device
->driver
== driver
)
493 host1x_device_del(host1x
, device
);
495 mutex_unlock(&host1x
->devices_lock
);
499 * host1x_register() - register a host1x controller
500 * @host1x: host1x controller
502 * The host1x controller driver uses this to register a host1x controller with
503 * the infrastructure. Note that all Tegra SoC generations have only ever come
504 * with a single host1x instance, so this function is somewhat academic.
506 int host1x_register(struct host1x
*host1x
)
508 struct host1x_driver
*driver
;
510 mutex_lock(&devices_lock
);
511 list_add_tail(&host1x
->list
, &devices
);
512 mutex_unlock(&devices_lock
);
514 mutex_lock(&drivers_lock
);
516 list_for_each_entry(driver
, &drivers
, list
)
517 host1x_attach_driver(host1x
, driver
);
519 mutex_unlock(&drivers_lock
);
525 * host1x_unregister() - unregister a host1x controller
526 * @host1x: host1x controller
528 * The host1x controller driver uses this to remove a host1x controller from
529 * the infrastructure.
531 int host1x_unregister(struct host1x
*host1x
)
533 struct host1x_driver
*driver
;
535 mutex_lock(&drivers_lock
);
537 list_for_each_entry(driver
, &drivers
, list
)
538 host1x_detach_driver(host1x
, driver
);
540 mutex_unlock(&drivers_lock
);
542 mutex_lock(&devices_lock
);
543 list_del_init(&host1x
->list
);
544 mutex_unlock(&devices_lock
);
549 static int host1x_device_probe(struct device
*dev
)
551 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
552 struct host1x_device
*device
= to_host1x_device(dev
);
555 return driver
->probe(device
);
560 static int host1x_device_remove(struct device
*dev
)
562 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
563 struct host1x_device
*device
= to_host1x_device(dev
);
566 return driver
->remove(device
);
571 static void host1x_device_shutdown(struct device
*dev
)
573 struct host1x_driver
*driver
= to_host1x_driver(dev
->driver
);
574 struct host1x_device
*device
= to_host1x_device(dev
);
576 if (driver
->shutdown
)
577 driver
->shutdown(device
);
581 * host1x_driver_register_full() - register a host1x driver
582 * @driver: host1x driver
583 * @owner: owner module
585 * Drivers for host1x logical devices call this function to register a driver
586 * with the infrastructure. Note that since these drive logical devices, the
587 * registration of the driver actually triggers tho logical device creation.
588 * A logical device will be created for each host1x instance.
590 int host1x_driver_register_full(struct host1x_driver
*driver
,
591 struct module
*owner
)
593 struct host1x
*host1x
;
595 INIT_LIST_HEAD(&driver
->list
);
597 mutex_lock(&drivers_lock
);
598 list_add_tail(&driver
->list
, &drivers
);
599 mutex_unlock(&drivers_lock
);
601 mutex_lock(&devices_lock
);
603 list_for_each_entry(host1x
, &devices
, list
)
604 host1x_attach_driver(host1x
, driver
);
606 mutex_unlock(&devices_lock
);
608 driver
->driver
.bus
= &host1x_bus_type
;
609 driver
->driver
.owner
= owner
;
610 driver
->driver
.probe
= host1x_device_probe
;
611 driver
->driver
.remove
= host1x_device_remove
;
612 driver
->driver
.shutdown
= host1x_device_shutdown
;
614 return driver_register(&driver
->driver
);
616 EXPORT_SYMBOL(host1x_driver_register_full
);
619 * host1x_driver_unregister() - unregister a host1x driver
620 * @driver: host1x driver
622 * Unbinds the driver from each of the host1x logical devices that it is
623 * bound to, effectively removing the subsystem devices that they represent.
625 void host1x_driver_unregister(struct host1x_driver
*driver
)
627 driver_unregister(&driver
->driver
);
629 mutex_lock(&drivers_lock
);
630 list_del_init(&driver
->list
);
631 mutex_unlock(&drivers_lock
);
633 EXPORT_SYMBOL(host1x_driver_unregister
);
636 * host1x_client_register() - register a host1x client
637 * @client: host1x client
639 * Registers a host1x client with each host1x controller instance. Note that
640 * each client will only match their parent host1x controller and will only be
641 * associated with that instance. Once all clients have been registered with
642 * their parent host1x controller, the infrastructure will set up the logical
643 * device and call host1x_device_init(), which will in turn call each client's
644 * &host1x_client_ops.init implementation.
646 int host1x_client_register(struct host1x_client
*client
)
648 struct host1x
*host1x
;
651 mutex_lock(&devices_lock
);
653 list_for_each_entry(host1x
, &devices
, list
) {
654 err
= host1x_add_client(host1x
, client
);
656 mutex_unlock(&devices_lock
);
661 mutex_unlock(&devices_lock
);
663 mutex_lock(&clients_lock
);
664 list_add_tail(&client
->list
, &clients
);
665 mutex_unlock(&clients_lock
);
669 EXPORT_SYMBOL(host1x_client_register
);
672 * host1x_client_unregister() - unregister a host1x client
673 * @client: host1x client
675 * Removes a host1x client from its host1x controller instance. If a logical
676 * device has already been initialized, it will be torn down.
678 int host1x_client_unregister(struct host1x_client
*client
)
680 struct host1x_client
*c
;
681 struct host1x
*host1x
;
684 mutex_lock(&devices_lock
);
686 list_for_each_entry(host1x
, &devices
, list
) {
687 err
= host1x_del_client(host1x
, client
);
689 mutex_unlock(&devices_lock
);
694 mutex_unlock(&devices_lock
);
695 mutex_lock(&clients_lock
);
697 list_for_each_entry(c
, &clients
, list
) {
699 list_del_init(&c
->list
);
704 mutex_unlock(&clients_lock
);
708 EXPORT_SYMBOL(host1x_client_unregister
);