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