Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Avionic Design GmbH
4 * Copyright (C) 2012-2013, NVIDIA Corporation
5 */
6
7#include <linux/debugfs.h>
8#include <linux/dma-mapping.h>
9#include <linux/host1x.h>
10#include <linux/of.h>
11#include <linux/seq_file.h>
12#include <linux/slab.h>
13#include <linux/of_device.h>
14
15#include "bus.h"
16#include "dev.h"
17
18static DEFINE_MUTEX(clients_lock);
19static LIST_HEAD(clients);
20
21static DEFINE_MUTEX(drivers_lock);
22static LIST_HEAD(drivers);
23
24static DEFINE_MUTEX(devices_lock);
25static LIST_HEAD(devices);
26
27struct host1x_subdev {
28 struct host1x_client *client;
29 struct device_node *np;
30 struct list_head list;
31};
32
33/**
34 * host1x_subdev_add() - add a new subdevice with an associated device node
35 * @device: host1x device to add the subdevice to
36 * @driver: host1x driver containing the subdevices
37 * @np: device node
38 */
39static int host1x_subdev_add(struct host1x_device *device,
40 struct host1x_driver *driver,
41 struct device_node *np)
42{
43 struct host1x_subdev *subdev;
44 int err;
45
46 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
47 if (!subdev)
48 return -ENOMEM;
49
50 INIT_LIST_HEAD(&subdev->list);
51 subdev->np = of_node_get(np);
52
53 mutex_lock(&device->subdevs_lock);
54 list_add_tail(&subdev->list, &device->subdevs);
55 mutex_unlock(&device->subdevs_lock);
56
57 /* recursively add children */
58 for_each_child_of_node_scoped(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 return err;
65 }
66 }
67 }
68
69 return 0;
70}
71
72/**
73 * host1x_subdev_del() - remove subdevice
74 * @subdev: subdevice to remove
75 */
76static void host1x_subdev_del(struct host1x_subdev *subdev)
77{
78 list_del(&subdev->list);
79 of_node_put(subdev->np);
80 kfree(subdev);
81}
82
83/**
84 * host1x_device_parse_dt() - scan device tree and add matching subdevices
85 * @device: host1x logical device
86 * @driver: host1x driver
87 */
88static int host1x_device_parse_dt(struct host1x_device *device,
89 struct host1x_driver *driver)
90{
91 int err;
92
93 for_each_child_of_node_scoped(device->dev.parent->of_node, np) {
94 if (of_match_node(driver->subdevs, np) &&
95 of_device_is_available(np)) {
96 err = host1x_subdev_add(device, driver, np);
97 if (err < 0)
98 return err;
99 }
100 }
101
102 return 0;
103}
104
105static void host1x_subdev_register(struct host1x_device *device,
106 struct host1x_subdev *subdev,
107 struct host1x_client *client)
108{
109 int err;
110
111 /*
112 * Move the subdevice to the list of active (registered) subdevices
113 * and associate it with a client. At the same time, associate the
114 * client with its parent device.
115 */
116 mutex_lock(&device->subdevs_lock);
117 mutex_lock(&device->clients_lock);
118 list_move_tail(&client->list, &device->clients);
119 list_move_tail(&subdev->list, &device->active);
120 client->host = &device->dev;
121 subdev->client = client;
122 mutex_unlock(&device->clients_lock);
123 mutex_unlock(&device->subdevs_lock);
124
125 if (list_empty(&device->subdevs)) {
126 err = device_add(&device->dev);
127 if (err < 0)
128 dev_err(&device->dev, "failed to add: %d\n", err);
129 else
130 device->registered = true;
131 }
132}
133
134static void __host1x_subdev_unregister(struct host1x_device *device,
135 struct host1x_subdev *subdev)
136{
137 struct host1x_client *client = subdev->client;
138
139 /*
140 * If all subdevices have been activated, we're about to remove the
141 * first active subdevice, so unload the driver first.
142 */
143 if (list_empty(&device->subdevs)) {
144 if (device->registered) {
145 device->registered = false;
146 device_del(&device->dev);
147 }
148 }
149
150 /*
151 * Move the subdevice back to the list of idle subdevices and remove
152 * it from list of clients.
153 */
154 mutex_lock(&device->clients_lock);
155 subdev->client = NULL;
156 client->host = NULL;
157 list_move_tail(&subdev->list, &device->subdevs);
158 /*
159 * XXX: Perhaps don't do this here, but rather explicitly remove it
160 * when the device is about to be deleted.
161 *
162 * This is somewhat complicated by the fact that this function is
163 * used to remove the subdevice when a client is unregistered but
164 * also when the composite device is about to be removed.
165 */
166 list_del_init(&client->list);
167 mutex_unlock(&device->clients_lock);
168}
169
170static void host1x_subdev_unregister(struct host1x_device *device,
171 struct host1x_subdev *subdev)
172{
173 mutex_lock(&device->subdevs_lock);
174 __host1x_subdev_unregister(device, subdev);
175 mutex_unlock(&device->subdevs_lock);
176}
177
178/**
179 * host1x_device_init() - initialize a host1x logical device
180 * @device: host1x logical device
181 *
182 * The driver for the host1x logical device can call this during execution of
183 * its &host1x_driver.probe implementation to initialize each of its clients.
184 * The client drivers access the subsystem specific driver data using the
185 * &host1x_client.parent field and driver data associated with it (usually by
186 * calling dev_get_drvdata()).
187 */
188int host1x_device_init(struct host1x_device *device)
189{
190 struct host1x_client *client;
191 int err;
192
193 mutex_lock(&device->clients_lock);
194
195 list_for_each_entry(client, &device->clients, list) {
196 if (client->ops && client->ops->early_init) {
197 err = client->ops->early_init(client);
198 if (err < 0) {
199 dev_err(&device->dev, "failed to early initialize %s: %d\n",
200 dev_name(client->dev), err);
201 goto teardown_late;
202 }
203 }
204 }
205
206 list_for_each_entry(client, &device->clients, list) {
207 if (client->ops && client->ops->init) {
208 err = client->ops->init(client);
209 if (err < 0) {
210 dev_err(&device->dev,
211 "failed to initialize %s: %d\n",
212 dev_name(client->dev), err);
213 goto teardown;
214 }
215 }
216 }
217
218 mutex_unlock(&device->clients_lock);
219
220 return 0;
221
222teardown:
223 list_for_each_entry_continue_reverse(client, &device->clients, list)
224 if (client->ops->exit)
225 client->ops->exit(client);
226
227 /* reset client to end of list for late teardown */
228 client = list_entry(&device->clients, struct host1x_client, list);
229
230teardown_late:
231 list_for_each_entry_continue_reverse(client, &device->clients, list)
232 if (client->ops->late_exit)
233 client->ops->late_exit(client);
234
235 mutex_unlock(&device->clients_lock);
236 return err;
237}
238EXPORT_SYMBOL(host1x_device_init);
239
240/**
241 * host1x_device_exit() - uninitialize host1x logical device
242 * @device: host1x logical device
243 *
244 * When the driver for a host1x logical device is unloaded, it can call this
245 * function to tear down each of its clients. Typically this is done after a
246 * subsystem-specific data structure is removed and the functionality can no
247 * longer be used.
248 */
249int host1x_device_exit(struct host1x_device *device)
250{
251 struct host1x_client *client;
252 int err;
253
254 mutex_lock(&device->clients_lock);
255
256 list_for_each_entry_reverse(client, &device->clients, list) {
257 if (client->ops && client->ops->exit) {
258 err = client->ops->exit(client);
259 if (err < 0) {
260 dev_err(&device->dev,
261 "failed to cleanup %s: %d\n",
262 dev_name(client->dev), err);
263 mutex_unlock(&device->clients_lock);
264 return err;
265 }
266 }
267 }
268
269 list_for_each_entry_reverse(client, &device->clients, list) {
270 if (client->ops && client->ops->late_exit) {
271 err = client->ops->late_exit(client);
272 if (err < 0) {
273 dev_err(&device->dev, "failed to late cleanup %s: %d\n",
274 dev_name(client->dev), err);
275 mutex_unlock(&device->clients_lock);
276 return err;
277 }
278 }
279 }
280
281 mutex_unlock(&device->clients_lock);
282
283 return 0;
284}
285EXPORT_SYMBOL(host1x_device_exit);
286
287static int host1x_add_client(struct host1x *host1x,
288 struct host1x_client *client)
289{
290 struct host1x_device *device;
291 struct host1x_subdev *subdev;
292
293 mutex_lock(&host1x->devices_lock);
294
295 list_for_each_entry(device, &host1x->devices, list) {
296 list_for_each_entry(subdev, &device->subdevs, list) {
297 if (subdev->np == client->dev->of_node) {
298 host1x_subdev_register(device, subdev, client);
299 mutex_unlock(&host1x->devices_lock);
300 return 0;
301 }
302 }
303 }
304
305 mutex_unlock(&host1x->devices_lock);
306 return -ENODEV;
307}
308
309static int host1x_del_client(struct host1x *host1x,
310 struct host1x_client *client)
311{
312 struct host1x_device *device, *dt;
313 struct host1x_subdev *subdev;
314
315 mutex_lock(&host1x->devices_lock);
316
317 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
318 list_for_each_entry(subdev, &device->active, list) {
319 if (subdev->client == client) {
320 host1x_subdev_unregister(device, subdev);
321 mutex_unlock(&host1x->devices_lock);
322 return 0;
323 }
324 }
325 }
326
327 mutex_unlock(&host1x->devices_lock);
328 return -ENODEV;
329}
330
331static int host1x_device_match(struct device *dev, const struct device_driver *drv)
332{
333 return strcmp(dev_name(dev), drv->name) == 0;
334}
335
336/*
337 * Note that this is really only needed for backwards compatibility
338 * with libdrm, which parses this information from sysfs and will
339 * fail if it can't find the OF_FULLNAME, specifically.
340 */
341static int host1x_device_uevent(const struct device *dev,
342 struct kobj_uevent_env *env)
343{
344 of_device_uevent(dev->parent, env);
345
346 return 0;
347}
348
349static const struct dev_pm_ops host1x_device_pm_ops = {
350 .suspend = pm_generic_suspend,
351 .resume = pm_generic_resume,
352 .freeze = pm_generic_freeze,
353 .thaw = pm_generic_thaw,
354 .poweroff = pm_generic_poweroff,
355 .restore = pm_generic_restore,
356};
357
358const struct bus_type host1x_bus_type = {
359 .name = "host1x",
360 .match = host1x_device_match,
361 .uevent = host1x_device_uevent,
362 .pm = &host1x_device_pm_ops,
363};
364
365static void __host1x_device_del(struct host1x_device *device)
366{
367 struct host1x_subdev *subdev, *sd;
368 struct host1x_client *client, *cl;
369
370 mutex_lock(&device->subdevs_lock);
371
372 /* unregister subdevices */
373 list_for_each_entry_safe(subdev, sd, &device->active, list) {
374 /*
375 * host1x_subdev_unregister() will remove the client from
376 * any lists, so we'll need to manually add it back to the
377 * list of idle clients.
378 *
379 * XXX: Alternatively, perhaps don't remove the client from
380 * any lists in host1x_subdev_unregister() and instead do
381 * that explicitly from host1x_unregister_client()?
382 */
383 client = subdev->client;
384
385 __host1x_subdev_unregister(device, subdev);
386
387 /* add the client to the list of idle clients */
388 mutex_lock(&clients_lock);
389 list_add_tail(&client->list, &clients);
390 mutex_unlock(&clients_lock);
391 }
392
393 /* remove subdevices */
394 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
395 host1x_subdev_del(subdev);
396
397 mutex_unlock(&device->subdevs_lock);
398
399 /* move clients to idle list */
400 mutex_lock(&clients_lock);
401 mutex_lock(&device->clients_lock);
402
403 list_for_each_entry_safe(client, cl, &device->clients, list)
404 list_move_tail(&client->list, &clients);
405
406 mutex_unlock(&device->clients_lock);
407 mutex_unlock(&clients_lock);
408
409 /* finally remove the device */
410 list_del_init(&device->list);
411}
412
413static void host1x_device_release(struct device *dev)
414{
415 struct host1x_device *device = to_host1x_device(dev);
416
417 __host1x_device_del(device);
418 kfree(device);
419}
420
421static int host1x_device_add(struct host1x *host1x,
422 struct host1x_driver *driver)
423{
424 struct host1x_client *client, *tmp;
425 struct host1x_subdev *subdev;
426 struct host1x_device *device;
427 int err;
428
429 device = kzalloc(sizeof(*device), GFP_KERNEL);
430 if (!device)
431 return -ENOMEM;
432
433 device_initialize(&device->dev);
434
435 mutex_init(&device->subdevs_lock);
436 INIT_LIST_HEAD(&device->subdevs);
437 INIT_LIST_HEAD(&device->active);
438 mutex_init(&device->clients_lock);
439 INIT_LIST_HEAD(&device->clients);
440 INIT_LIST_HEAD(&device->list);
441 device->driver = driver;
442
443 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
444 device->dev.dma_mask = &device->dev.coherent_dma_mask;
445 dev_set_name(&device->dev, "%s", driver->driver.name);
446 device->dev.release = host1x_device_release;
447 device->dev.bus = &host1x_bus_type;
448 device->dev.parent = host1x->dev;
449
450 device->dev.dma_parms = &device->dma_parms;
451 dma_set_max_seg_size(&device->dev, UINT_MAX);
452
453 err = host1x_device_parse_dt(device, driver);
454 if (err < 0) {
455 kfree(device);
456 return err;
457 }
458
459 list_add_tail(&device->list, &host1x->devices);
460
461 mutex_lock(&clients_lock);
462
463 list_for_each_entry_safe(client, tmp, &clients, list) {
464 list_for_each_entry(subdev, &device->subdevs, list) {
465 if (subdev->np == client->dev->of_node) {
466 host1x_subdev_register(device, subdev, client);
467 break;
468 }
469 }
470 }
471
472 mutex_unlock(&clients_lock);
473
474 /*
475 * Add device even if there are no subdevs to ensure syncpoint functionality
476 * is available regardless of whether any engine subdevices are present
477 */
478 if (list_empty(&device->subdevs)) {
479 err = device_add(&device->dev);
480 if (err < 0)
481 dev_err(&device->dev, "failed to add device: %d\n", err);
482 else
483 device->registered = true;
484 }
485
486 return 0;
487}
488
489/*
490 * Removes a device by first unregistering any subdevices and then removing
491 * itself from the list of devices.
492 *
493 * This function must be called with the host1x->devices_lock held.
494 */
495static void host1x_device_del(struct host1x *host1x,
496 struct host1x_device *device)
497{
498 if (device->registered) {
499 device->registered = false;
500 device_del(&device->dev);
501 }
502
503 put_device(&device->dev);
504}
505
506static void host1x_attach_driver(struct host1x *host1x,
507 struct host1x_driver *driver)
508{
509 struct host1x_device *device;
510 int err;
511
512 mutex_lock(&host1x->devices_lock);
513
514 list_for_each_entry(device, &host1x->devices, list) {
515 if (device->driver == driver) {
516 mutex_unlock(&host1x->devices_lock);
517 return;
518 }
519 }
520
521 err = host1x_device_add(host1x, driver);
522 if (err < 0)
523 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
524
525 mutex_unlock(&host1x->devices_lock);
526}
527
528static void host1x_detach_driver(struct host1x *host1x,
529 struct host1x_driver *driver)
530{
531 struct host1x_device *device, *tmp;
532
533 mutex_lock(&host1x->devices_lock);
534
535 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
536 if (device->driver == driver)
537 host1x_device_del(host1x, device);
538
539 mutex_unlock(&host1x->devices_lock);
540}
541
542static int host1x_devices_show(struct seq_file *s, void *data)
543{
544 struct host1x *host1x = s->private;
545 struct host1x_device *device;
546
547 mutex_lock(&host1x->devices_lock);
548
549 list_for_each_entry(device, &host1x->devices, list) {
550 struct host1x_subdev *subdev;
551
552 seq_printf(s, "%s\n", dev_name(&device->dev));
553
554 mutex_lock(&device->subdevs_lock);
555
556 list_for_each_entry(subdev, &device->active, list)
557 seq_printf(s, " %pOFf: %s\n", subdev->np,
558 dev_name(subdev->client->dev));
559
560 list_for_each_entry(subdev, &device->subdevs, list)
561 seq_printf(s, " %pOFf:\n", subdev->np);
562
563 mutex_unlock(&device->subdevs_lock);
564 }
565
566 mutex_unlock(&host1x->devices_lock);
567
568 return 0;
569}
570DEFINE_SHOW_ATTRIBUTE(host1x_devices);
571
572/**
573 * host1x_register() - register a host1x controller
574 * @host1x: host1x controller
575 *
576 * The host1x controller driver uses this to register a host1x controller with
577 * the infrastructure. Note that all Tegra SoC generations have only ever come
578 * with a single host1x instance, so this function is somewhat academic.
579 */
580int host1x_register(struct host1x *host1x)
581{
582 struct host1x_driver *driver;
583
584 mutex_lock(&devices_lock);
585 list_add_tail(&host1x->list, &devices);
586 mutex_unlock(&devices_lock);
587
588 mutex_lock(&drivers_lock);
589
590 list_for_each_entry(driver, &drivers, list)
591 host1x_attach_driver(host1x, driver);
592
593 mutex_unlock(&drivers_lock);
594
595 debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
596 &host1x_devices_fops);
597
598 return 0;
599}
600
601/**
602 * host1x_unregister() - unregister a host1x controller
603 * @host1x: host1x controller
604 *
605 * The host1x controller driver uses this to remove a host1x controller from
606 * the infrastructure.
607 */
608int host1x_unregister(struct host1x *host1x)
609{
610 struct host1x_driver *driver;
611
612 mutex_lock(&drivers_lock);
613
614 list_for_each_entry(driver, &drivers, list)
615 host1x_detach_driver(host1x, driver);
616
617 mutex_unlock(&drivers_lock);
618
619 mutex_lock(&devices_lock);
620 list_del_init(&host1x->list);
621 mutex_unlock(&devices_lock);
622
623 return 0;
624}
625
626static int host1x_device_probe(struct device *dev)
627{
628 struct host1x_driver *driver = to_host1x_driver(dev->driver);
629 struct host1x_device *device = to_host1x_device(dev);
630
631 if (driver->probe)
632 return driver->probe(device);
633
634 return 0;
635}
636
637static int host1x_device_remove(struct device *dev)
638{
639 struct host1x_driver *driver = to_host1x_driver(dev->driver);
640 struct host1x_device *device = to_host1x_device(dev);
641
642 if (driver->remove)
643 return driver->remove(device);
644
645 return 0;
646}
647
648static void host1x_device_shutdown(struct device *dev)
649{
650 struct host1x_driver *driver = to_host1x_driver(dev->driver);
651 struct host1x_device *device = to_host1x_device(dev);
652
653 if (driver->shutdown)
654 driver->shutdown(device);
655}
656
657/**
658 * host1x_driver_register_full() - register a host1x driver
659 * @driver: host1x driver
660 * @owner: owner module
661 *
662 * Drivers for host1x logical devices call this function to register a driver
663 * with the infrastructure. Note that since these drive logical devices, the
664 * registration of the driver actually triggers tho logical device creation.
665 * A logical device will be created for each host1x instance.
666 */
667int host1x_driver_register_full(struct host1x_driver *driver,
668 struct module *owner)
669{
670 struct host1x *host1x;
671
672 INIT_LIST_HEAD(&driver->list);
673
674 mutex_lock(&drivers_lock);
675 list_add_tail(&driver->list, &drivers);
676 mutex_unlock(&drivers_lock);
677
678 mutex_lock(&devices_lock);
679
680 list_for_each_entry(host1x, &devices, list)
681 host1x_attach_driver(host1x, driver);
682
683 mutex_unlock(&devices_lock);
684
685 driver->driver.bus = &host1x_bus_type;
686 driver->driver.owner = owner;
687 driver->driver.probe = host1x_device_probe;
688 driver->driver.remove = host1x_device_remove;
689 driver->driver.shutdown = host1x_device_shutdown;
690
691 return driver_register(&driver->driver);
692}
693EXPORT_SYMBOL(host1x_driver_register_full);
694
695/**
696 * host1x_driver_unregister() - unregister a host1x driver
697 * @driver: host1x driver
698 *
699 * Unbinds the driver from each of the host1x logical devices that it is
700 * bound to, effectively removing the subsystem devices that they represent.
701 */
702void host1x_driver_unregister(struct host1x_driver *driver)
703{
704 struct host1x *host1x;
705
706 driver_unregister(&driver->driver);
707
708 mutex_lock(&devices_lock);
709
710 list_for_each_entry(host1x, &devices, list)
711 host1x_detach_driver(host1x, driver);
712
713 mutex_unlock(&devices_lock);
714
715 mutex_lock(&drivers_lock);
716 list_del_init(&driver->list);
717 mutex_unlock(&drivers_lock);
718}
719EXPORT_SYMBOL(host1x_driver_unregister);
720
721/**
722 * __host1x_client_init() - initialize a host1x client
723 * @client: host1x client
724 * @key: lock class key for the client-specific mutex
725 */
726void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key)
727{
728 host1x_bo_cache_init(&client->cache);
729 INIT_LIST_HEAD(&client->list);
730 __mutex_init(&client->lock, "host1x client lock", key);
731 client->usecount = 0;
732}
733EXPORT_SYMBOL(__host1x_client_init);
734
735/**
736 * host1x_client_exit() - uninitialize a host1x client
737 * @client: host1x client
738 */
739void host1x_client_exit(struct host1x_client *client)
740{
741 mutex_destroy(&client->lock);
742}
743EXPORT_SYMBOL(host1x_client_exit);
744
745/**
746 * __host1x_client_register() - register a host1x client
747 * @client: host1x client
748 *
749 * Registers a host1x client with each host1x controller instance. Note that
750 * each client will only match their parent host1x controller and will only be
751 * associated with that instance. Once all clients have been registered with
752 * their parent host1x controller, the infrastructure will set up the logical
753 * device and call host1x_device_init(), which will in turn call each client's
754 * &host1x_client_ops.init implementation.
755 */
756int __host1x_client_register(struct host1x_client *client)
757{
758 struct host1x *host1x;
759 int err;
760
761 mutex_lock(&devices_lock);
762
763 list_for_each_entry(host1x, &devices, list) {
764 err = host1x_add_client(host1x, client);
765 if (!err) {
766 mutex_unlock(&devices_lock);
767 return 0;
768 }
769 }
770
771 mutex_unlock(&devices_lock);
772
773 mutex_lock(&clients_lock);
774 list_add_tail(&client->list, &clients);
775 mutex_unlock(&clients_lock);
776
777 return 0;
778}
779EXPORT_SYMBOL(__host1x_client_register);
780
781/**
782 * host1x_client_unregister() - unregister a host1x client
783 * @client: host1x client
784 *
785 * Removes a host1x client from its host1x controller instance. If a logical
786 * device has already been initialized, it will be torn down.
787 */
788void host1x_client_unregister(struct host1x_client *client)
789{
790 struct host1x_client *c;
791 struct host1x *host1x;
792 int err;
793
794 mutex_lock(&devices_lock);
795
796 list_for_each_entry(host1x, &devices, list) {
797 err = host1x_del_client(host1x, client);
798 if (!err) {
799 mutex_unlock(&devices_lock);
800 return;
801 }
802 }
803
804 mutex_unlock(&devices_lock);
805 mutex_lock(&clients_lock);
806
807 list_for_each_entry(c, &clients, list) {
808 if (c == client) {
809 list_del_init(&c->list);
810 break;
811 }
812 }
813
814 mutex_unlock(&clients_lock);
815
816 host1x_bo_cache_destroy(&client->cache);
817}
818EXPORT_SYMBOL(host1x_client_unregister);
819
820int host1x_client_suspend(struct host1x_client *client)
821{
822 int err = 0;
823
824 mutex_lock(&client->lock);
825
826 if (client->usecount == 1) {
827 if (client->ops && client->ops->suspend) {
828 err = client->ops->suspend(client);
829 if (err < 0)
830 goto unlock;
831 }
832 }
833
834 client->usecount--;
835 dev_dbg(client->dev, "use count: %u\n", client->usecount);
836
837 if (client->parent) {
838 err = host1x_client_suspend(client->parent);
839 if (err < 0)
840 goto resume;
841 }
842
843 goto unlock;
844
845resume:
846 if (client->usecount == 0)
847 if (client->ops && client->ops->resume)
848 client->ops->resume(client);
849
850 client->usecount++;
851unlock:
852 mutex_unlock(&client->lock);
853 return err;
854}
855EXPORT_SYMBOL(host1x_client_suspend);
856
857int host1x_client_resume(struct host1x_client *client)
858{
859 int err = 0;
860
861 mutex_lock(&client->lock);
862
863 if (client->parent) {
864 err = host1x_client_resume(client->parent);
865 if (err < 0)
866 goto unlock;
867 }
868
869 if (client->usecount == 0) {
870 if (client->ops && client->ops->resume) {
871 err = client->ops->resume(client);
872 if (err < 0)
873 goto suspend;
874 }
875 }
876
877 client->usecount++;
878 dev_dbg(client->dev, "use count: %u\n", client->usecount);
879
880 goto unlock;
881
882suspend:
883 if (client->parent)
884 host1x_client_suspend(client->parent);
885unlock:
886 mutex_unlock(&client->lock);
887 return err;
888}
889EXPORT_SYMBOL(host1x_client_resume);
890
891struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo,
892 enum dma_data_direction dir,
893 struct host1x_bo_cache *cache)
894{
895 struct host1x_bo_mapping *mapping;
896
897 if (cache) {
898 mutex_lock(&cache->lock);
899
900 list_for_each_entry(mapping, &cache->mappings, entry) {
901 if (mapping->bo == bo && mapping->direction == dir) {
902 kref_get(&mapping->ref);
903 goto unlock;
904 }
905 }
906 }
907
908 mapping = bo->ops->pin(dev, bo, dir);
909 if (IS_ERR(mapping))
910 goto unlock;
911
912 spin_lock(&mapping->bo->lock);
913 list_add_tail(&mapping->list, &bo->mappings);
914 spin_unlock(&mapping->bo->lock);
915
916 if (cache) {
917 INIT_LIST_HEAD(&mapping->entry);
918 mapping->cache = cache;
919
920 list_add_tail(&mapping->entry, &cache->mappings);
921
922 /* bump reference count to track the copy in the cache */
923 kref_get(&mapping->ref);
924 }
925
926unlock:
927 if (cache)
928 mutex_unlock(&cache->lock);
929
930 return mapping;
931}
932EXPORT_SYMBOL(host1x_bo_pin);
933
934static void __host1x_bo_unpin(struct kref *ref)
935{
936 struct host1x_bo_mapping *mapping = to_host1x_bo_mapping(ref);
937
938 /*
939 * When the last reference of the mapping goes away, make sure to remove the mapping from
940 * the cache.
941 */
942 if (mapping->cache)
943 list_del(&mapping->entry);
944
945 spin_lock(&mapping->bo->lock);
946 list_del(&mapping->list);
947 spin_unlock(&mapping->bo->lock);
948
949 mapping->bo->ops->unpin(mapping);
950}
951
952void host1x_bo_unpin(struct host1x_bo_mapping *mapping)
953{
954 struct host1x_bo_cache *cache = mapping->cache;
955
956 if (cache)
957 mutex_lock(&cache->lock);
958
959 kref_put(&mapping->ref, __host1x_bo_unpin);
960
961 if (cache)
962 mutex_unlock(&cache->lock);
963}
964EXPORT_SYMBOL(host1x_bo_unpin);