xref: /openbmc/linux/drivers/gpu/host1x/bus.c (revision 31e67366)
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/host1x.h>
9 #include <linux/of.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
12 #include <linux/of_device.h>
13 
14 #include "bus.h"
15 #include "dev.h"
16 
17 static DEFINE_MUTEX(clients_lock);
18 static LIST_HEAD(clients);
19 
20 static DEFINE_MUTEX(drivers_lock);
21 static LIST_HEAD(drivers);
22 
23 static DEFINE_MUTEX(devices_lock);
24 static LIST_HEAD(devices);
25 
26 struct host1x_subdev {
27 	struct host1x_client *client;
28 	struct device_node *np;
29 	struct list_head list;
30 };
31 
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
37  */
38 static int host1x_subdev_add(struct host1x_device *device,
39 			     struct host1x_driver *driver,
40 			     struct device_node *np)
41 {
42 	struct host1x_subdev *subdev;
43 	struct device_node *child;
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(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;
66 			}
67 		}
68 	}
69 
70 	return 0;
71 }
72 
73 /**
74  * host1x_subdev_del() - remove subdevice
75  * @subdev: subdevice to remove
76  */
77 static void host1x_subdev_del(struct host1x_subdev *subdev)
78 {
79 	list_del(&subdev->list);
80 	of_node_put(subdev->np);
81 	kfree(subdev);
82 }
83 
84 /**
85  * host1x_device_parse_dt() - scan device tree and add matching subdevices
86  * @device: host1x logical device
87  * @driver: host1x driver
88  */
89 static int host1x_device_parse_dt(struct host1x_device *device,
90 				  struct host1x_driver *driver)
91 {
92 	struct device_node *np;
93 	int err;
94 
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;
102 			}
103 		}
104 	}
105 
106 	return 0;
107 }
108 
109 static void host1x_subdev_register(struct host1x_device *device,
110 				   struct host1x_subdev *subdev,
111 				   struct host1x_client *client)
112 {
113 	int err;
114 
115 	/*
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.
119 	 */
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);
128 
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;
135 	}
136 }
137 
138 static void __host1x_subdev_unregister(struct host1x_device *device,
139 				       struct host1x_subdev *subdev)
140 {
141 	struct host1x_client *client = subdev->client;
142 
143 	/*
144 	 * If all subdevices have been activated, we're about to remove the
145 	 * first active subdevice, so unload the driver first.
146 	 */
147 	if (list_empty(&device->subdevs)) {
148 		if (device->registered) {
149 			device->registered = false;
150 			device_del(&device->dev);
151 		}
152 	}
153 
154 	/*
155 	 * Move the subdevice back to the list of idle subdevices and remove
156 	 * it from list of clients.
157 	 */
158 	mutex_lock(&device->clients_lock);
159 	subdev->client = NULL;
160 	client->host = NULL;
161 	list_move_tail(&subdev->list, &device->subdevs);
162 	/*
163 	 * XXX: Perhaps don't do this here, but rather explicitly remove it
164 	 * when the device is about to be deleted.
165 	 *
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.
169 	 */
170 	list_del_init(&client->list);
171 	mutex_unlock(&device->clients_lock);
172 }
173 
174 static void host1x_subdev_unregister(struct host1x_device *device,
175 				     struct host1x_subdev *subdev)
176 {
177 	mutex_lock(&device->subdevs_lock);
178 	__host1x_subdev_unregister(device, subdev);
179 	mutex_unlock(&device->subdevs_lock);
180 }
181 
182 /**
183  * host1x_device_init() - initialize a host1x logical device
184  * @device: host1x logical device
185  *
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()).
191  */
192 int host1x_device_init(struct host1x_device *device)
193 {
194 	struct host1x_client *client;
195 	int err;
196 
197 	mutex_lock(&device->clients_lock);
198 
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;
207 			}
208 		}
209 	}
210 
211 	mutex_unlock(&device->clients_lock);
212 
213 	return 0;
214 
215 teardown:
216 	list_for_each_entry_continue_reverse(client, &device->clients, list)
217 		if (client->ops->exit)
218 			client->ops->exit(client);
219 
220 	mutex_unlock(&device->clients_lock);
221 	return err;
222 }
223 EXPORT_SYMBOL(host1x_device_init);
224 
225 /**
226  * host1x_device_exit() - uninitialize host1x logical device
227  * @device: host1x logical device
228  *
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.
233  */
234 int host1x_device_exit(struct host1x_device *device)
235 {
236 	struct host1x_client *client;
237 	int err;
238 
239 	mutex_lock(&device->clients_lock);
240 
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;
250 			}
251 		}
252 	}
253 
254 	mutex_unlock(&device->clients_lock);
255 
256 	return 0;
257 }
258 EXPORT_SYMBOL(host1x_device_exit);
259 
260 static int host1x_add_client(struct host1x *host1x,
261 			     struct host1x_client *client)
262 {
263 	struct host1x_device *device;
264 	struct host1x_subdev *subdev;
265 
266 	mutex_lock(&host1x->devices_lock);
267 
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;
274 			}
275 		}
276 	}
277 
278 	mutex_unlock(&host1x->devices_lock);
279 	return -ENODEV;
280 }
281 
282 static int host1x_del_client(struct host1x *host1x,
283 			     struct host1x_client *client)
284 {
285 	struct host1x_device *device, *dt;
286 	struct host1x_subdev *subdev;
287 
288 	mutex_lock(&host1x->devices_lock);
289 
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;
296 			}
297 		}
298 	}
299 
300 	mutex_unlock(&host1x->devices_lock);
301 	return -ENODEV;
302 }
303 
304 static int host1x_device_match(struct device *dev, struct device_driver *drv)
305 {
306 	return strcmp(dev_name(dev), drv->name) == 0;
307 }
308 
309 static int host1x_device_uevent(struct device *dev,
310 				struct kobj_uevent_env *env)
311 {
312 	struct device_node *np = dev->parent->of_node;
313 	unsigned int count = 0;
314 	struct property *p;
315 	const char *compat;
316 
317 	/*
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.
321 	 *
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.
325 	 */
326 	add_uevent_var(env, "OF_NAME=%pOFn", np);
327 	add_uevent_var(env, "OF_FULLNAME=%pOF", np);
328 
329 	of_property_for_each_string(np, "compatible", p, compat) {
330 		add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
331 		count++;
332 	}
333 
334 	add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
335 
336 	return 0;
337 }
338 
339 static int host1x_dma_configure(struct device *dev)
340 {
341 	return of_dma_configure(dev, dev->of_node, true);
342 }
343 
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,
351 };
352 
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,
359 };
360 
361 static void __host1x_device_del(struct host1x_device *device)
362 {
363 	struct host1x_subdev *subdev, *sd;
364 	struct host1x_client *client, *cl;
365 
366 	mutex_lock(&device->subdevs_lock);
367 
368 	/* unregister subdevices */
369 	list_for_each_entry_safe(subdev, sd, &device->active, list) {
370 		/*
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.
374 		 *
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()?
378 		 */
379 		client = subdev->client;
380 
381 		__host1x_subdev_unregister(device, subdev);
382 
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);
387 	}
388 
389 	/* remove subdevices */
390 	list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
391 		host1x_subdev_del(subdev);
392 
393 	mutex_unlock(&device->subdevs_lock);
394 
395 	/* move clients to idle list */
396 	mutex_lock(&clients_lock);
397 	mutex_lock(&device->clients_lock);
398 
399 	list_for_each_entry_safe(client, cl, &device->clients, list)
400 		list_move_tail(&client->list, &clients);
401 
402 	mutex_unlock(&device->clients_lock);
403 	mutex_unlock(&clients_lock);
404 
405 	/* finally remove the device */
406 	list_del_init(&device->list);
407 }
408 
409 static void host1x_device_release(struct device *dev)
410 {
411 	struct host1x_device *device = to_host1x_device(dev);
412 
413 	__host1x_device_del(device);
414 	kfree(device);
415 }
416 
417 static int host1x_device_add(struct host1x *host1x,
418 			     struct host1x_driver *driver)
419 {
420 	struct host1x_client *client, *tmp;
421 	struct host1x_subdev *subdev;
422 	struct host1x_device *device;
423 	int err;
424 
425 	device = kzalloc(sizeof(*device), GFP_KERNEL);
426 	if (!device)
427 		return -ENOMEM;
428 
429 	device_initialize(&device->dev);
430 
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;
438 
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;
445 
446 	of_dma_configure(&device->dev, host1x->dev->of_node, true);
447 
448 	device->dev.dma_parms = &device->dma_parms;
449 	dma_set_max_seg_size(&device->dev, UINT_MAX);
450 
451 	err = host1x_device_parse_dt(device, driver);
452 	if (err < 0) {
453 		kfree(device);
454 		return err;
455 	}
456 
457 	list_add_tail(&device->list, &host1x->devices);
458 
459 	mutex_lock(&clients_lock);
460 
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;
466 			}
467 		}
468 	}
469 
470 	mutex_unlock(&clients_lock);
471 
472 	return 0;
473 }
474 
475 /*
476  * Removes a device by first unregistering any subdevices and then removing
477  * itself from the list of devices.
478  *
479  * This function must be called with the host1x->devices_lock held.
480  */
481 static void host1x_device_del(struct host1x *host1x,
482 			      struct host1x_device *device)
483 {
484 	if (device->registered) {
485 		device->registered = false;
486 		device_del(&device->dev);
487 	}
488 
489 	put_device(&device->dev);
490 }
491 
492 static void host1x_attach_driver(struct host1x *host1x,
493 				 struct host1x_driver *driver)
494 {
495 	struct host1x_device *device;
496 	int err;
497 
498 	mutex_lock(&host1x->devices_lock);
499 
500 	list_for_each_entry(device, &host1x->devices, list) {
501 		if (device->driver == driver) {
502 			mutex_unlock(&host1x->devices_lock);
503 			return;
504 		}
505 	}
506 
507 	err = host1x_device_add(host1x, driver);
508 	if (err < 0)
509 		dev_err(host1x->dev, "failed to allocate device: %d\n", err);
510 
511 	mutex_unlock(&host1x->devices_lock);
512 }
513 
514 static void host1x_detach_driver(struct host1x *host1x,
515 				 struct host1x_driver *driver)
516 {
517 	struct host1x_device *device, *tmp;
518 
519 	mutex_lock(&host1x->devices_lock);
520 
521 	list_for_each_entry_safe(device, tmp, &host1x->devices, list)
522 		if (device->driver == driver)
523 			host1x_device_del(host1x, device);
524 
525 	mutex_unlock(&host1x->devices_lock);
526 }
527 
528 static int host1x_devices_show(struct seq_file *s, void *data)
529 {
530 	struct host1x *host1x = s->private;
531 	struct host1x_device *device;
532 
533 	mutex_lock(&host1x->devices_lock);
534 
535 	list_for_each_entry(device, &host1x->devices, list) {
536 		struct host1x_subdev *subdev;
537 
538 		seq_printf(s, "%s\n", dev_name(&device->dev));
539 
540 		mutex_lock(&device->subdevs_lock);
541 
542 		list_for_each_entry(subdev, &device->active, list)
543 			seq_printf(s, "  %pOFf: %s\n", subdev->np,
544 				   dev_name(subdev->client->dev));
545 
546 		list_for_each_entry(subdev, &device->subdevs, list)
547 			seq_printf(s, "  %pOFf:\n", subdev->np);
548 
549 		mutex_unlock(&device->subdevs_lock);
550 	}
551 
552 	mutex_unlock(&host1x->devices_lock);
553 
554 	return 0;
555 }
556 DEFINE_SHOW_ATTRIBUTE(host1x_devices);
557 
558 /**
559  * host1x_register() - register a host1x controller
560  * @host1x: host1x controller
561  *
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.
565  */
566 int host1x_register(struct host1x *host1x)
567 {
568 	struct host1x_driver *driver;
569 
570 	mutex_lock(&devices_lock);
571 	list_add_tail(&host1x->list, &devices);
572 	mutex_unlock(&devices_lock);
573 
574 	mutex_lock(&drivers_lock);
575 
576 	list_for_each_entry(driver, &drivers, list)
577 		host1x_attach_driver(host1x, driver);
578 
579 	mutex_unlock(&drivers_lock);
580 
581 	debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
582 			    &host1x_devices_fops);
583 
584 	return 0;
585 }
586 
587 /**
588  * host1x_unregister() - unregister a host1x controller
589  * @host1x: host1x controller
590  *
591  * The host1x controller driver uses this to remove a host1x controller from
592  * the infrastructure.
593  */
594 int host1x_unregister(struct host1x *host1x)
595 {
596 	struct host1x_driver *driver;
597 
598 	mutex_lock(&drivers_lock);
599 
600 	list_for_each_entry(driver, &drivers, list)
601 		host1x_detach_driver(host1x, driver);
602 
603 	mutex_unlock(&drivers_lock);
604 
605 	mutex_lock(&devices_lock);
606 	list_del_init(&host1x->list);
607 	mutex_unlock(&devices_lock);
608 
609 	return 0;
610 }
611 
612 static int host1x_device_probe(struct device *dev)
613 {
614 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
615 	struct host1x_device *device = to_host1x_device(dev);
616 
617 	if (driver->probe)
618 		return driver->probe(device);
619 
620 	return 0;
621 }
622 
623 static int host1x_device_remove(struct device *dev)
624 {
625 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
626 	struct host1x_device *device = to_host1x_device(dev);
627 
628 	if (driver->remove)
629 		return driver->remove(device);
630 
631 	return 0;
632 }
633 
634 static void host1x_device_shutdown(struct device *dev)
635 {
636 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
637 	struct host1x_device *device = to_host1x_device(dev);
638 
639 	if (driver->shutdown)
640 		driver->shutdown(device);
641 }
642 
643 /**
644  * host1x_driver_register_full() - register a host1x driver
645  * @driver: host1x driver
646  * @owner: owner module
647  *
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.
652  */
653 int host1x_driver_register_full(struct host1x_driver *driver,
654 				struct module *owner)
655 {
656 	struct host1x *host1x;
657 
658 	INIT_LIST_HEAD(&driver->list);
659 
660 	mutex_lock(&drivers_lock);
661 	list_add_tail(&driver->list, &drivers);
662 	mutex_unlock(&drivers_lock);
663 
664 	mutex_lock(&devices_lock);
665 
666 	list_for_each_entry(host1x, &devices, list)
667 		host1x_attach_driver(host1x, driver);
668 
669 	mutex_unlock(&devices_lock);
670 
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;
676 
677 	return driver_register(&driver->driver);
678 }
679 EXPORT_SYMBOL(host1x_driver_register_full);
680 
681 /**
682  * host1x_driver_unregister() - unregister a host1x driver
683  * @driver: host1x driver
684  *
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.
687  */
688 void host1x_driver_unregister(struct host1x_driver *driver)
689 {
690 	struct host1x *host1x;
691 
692 	driver_unregister(&driver->driver);
693 
694 	mutex_lock(&devices_lock);
695 
696 	list_for_each_entry(host1x, &devices, list)
697 		host1x_detach_driver(host1x, driver);
698 
699 	mutex_unlock(&devices_lock);
700 
701 	mutex_lock(&drivers_lock);
702 	list_del_init(&driver->list);
703 	mutex_unlock(&drivers_lock);
704 }
705 EXPORT_SYMBOL(host1x_driver_unregister);
706 
707 /**
708  * host1x_client_register() - register a host1x client
709  * @client: host1x client
710  *
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.
717  */
718 int host1x_client_register(struct host1x_client *client)
719 {
720 	struct host1x *host1x;
721 	int err;
722 
723 	INIT_LIST_HEAD(&client->list);
724 	mutex_init(&client->lock);
725 	client->usecount = 0;
726 
727 	mutex_lock(&devices_lock);
728 
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;
734 		}
735 	}
736 
737 	mutex_unlock(&devices_lock);
738 
739 	mutex_lock(&clients_lock);
740 	list_add_tail(&client->list, &clients);
741 	mutex_unlock(&clients_lock);
742 
743 	return 0;
744 }
745 EXPORT_SYMBOL(host1x_client_register);
746 
747 /**
748  * host1x_client_unregister() - unregister a host1x client
749  * @client: host1x client
750  *
751  * Removes a host1x client from its host1x controller instance. If a logical
752  * device has already been initialized, it will be torn down.
753  */
754 int host1x_client_unregister(struct host1x_client *client)
755 {
756 	struct host1x_client *c;
757 	struct host1x *host1x;
758 	int err;
759 
760 	mutex_lock(&devices_lock);
761 
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;
767 		}
768 	}
769 
770 	mutex_unlock(&devices_lock);
771 	mutex_lock(&clients_lock);
772 
773 	list_for_each_entry(c, &clients, list) {
774 		if (c == client) {
775 			list_del_init(&c->list);
776 			break;
777 		}
778 	}
779 
780 	mutex_unlock(&clients_lock);
781 
782 	return 0;
783 }
784 EXPORT_SYMBOL(host1x_client_unregister);
785 
786 int host1x_client_suspend(struct host1x_client *client)
787 {
788 	int err = 0;
789 
790 	mutex_lock(&client->lock);
791 
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;
797 		}
798 	}
799 
800 	client->usecount--;
801 	dev_dbg(client->dev, "use count: %u\n", client->usecount);
802 
803 	if (client->parent) {
804 		err = host1x_client_suspend(client->parent);
805 		if (err < 0)
806 			goto resume;
807 	}
808 
809 	goto unlock;
810 
811 resume:
812 	if (client->usecount == 0)
813 		if (client->ops && client->ops->resume)
814 			client->ops->resume(client);
815 
816 	client->usecount++;
817 unlock:
818 	mutex_unlock(&client->lock);
819 	return err;
820 }
821 EXPORT_SYMBOL(host1x_client_suspend);
822 
823 int host1x_client_resume(struct host1x_client *client)
824 {
825 	int err = 0;
826 
827 	mutex_lock(&client->lock);
828 
829 	if (client->parent) {
830 		err = host1x_client_resume(client->parent);
831 		if (err < 0)
832 			goto unlock;
833 	}
834 
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;
840 		}
841 	}
842 
843 	client->usecount++;
844 	dev_dbg(client->dev, "use count: %u\n", client->usecount);
845 
846 	goto unlock;
847 
848 suspend:
849 	if (client->parent)
850 		host1x_client_suspend(client->parent);
851 unlock:
852 	mutex_unlock(&client->lock);
853 	return err;
854 }
855 EXPORT_SYMBOL(host1x_client_resume);
856