blob: 0121fe7a4548dbbfbc6a24ba21667db8f0ff93e7 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013, NVIDIA Corporation
4 *
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.
8 *
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
12 * more details.
13 *
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/>.
16 */
17
18#include <linux/host1x.h>
19#include <linux/of.h>
20#include <linux/slab.h>
21#include <linux/of_device.h>
22
23#include "bus.h"
24#include "dev.h"
25
26static DEFINE_MUTEX(clients_lock);
27static LIST_HEAD(clients);
28
29static DEFINE_MUTEX(drivers_lock);
30static LIST_HEAD(drivers);
31
32static DEFINE_MUTEX(devices_lock);
33static LIST_HEAD(devices);
34
35struct host1x_subdev {
36 struct host1x_client *client;
37 struct device_node *np;
38 struct list_head list;
39};
40
41/**
42 * host1x_subdev_add() - add a new subdevice with an associated device node
43 * @device: host1x device to add the subdevice to
44 * @np: device node
45 */
46static int host1x_subdev_add(struct host1x_device *device,
47 struct host1x_driver *driver,
48 struct device_node *np)
49{
50 struct host1x_subdev *subdev;
51 struct device_node *child;
52 int err;
53
54 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
55 if (!subdev)
56 return -ENOMEM;
57
58 INIT_LIST_HEAD(&subdev->list);
59 subdev->np = of_node_get(np);
60
61 mutex_lock(&device->subdevs_lock);
62 list_add_tail(&subdev->list, &device->subdevs);
63 mutex_unlock(&device->subdevs_lock);
64
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);
70 if (err < 0) {
71 /* XXX cleanup? */
72 of_node_put(child);
73 return err;
74 }
75 }
76 }
77
78 return 0;
79}
80
81/**
82 * host1x_subdev_del() - remove subdevice
83 * @subdev: subdevice to remove
84 */
85static void host1x_subdev_del(struct host1x_subdev *subdev)
86{
87 list_del(&subdev->list);
88 of_node_put(subdev->np);
89 kfree(subdev);
90}
91
92/**
93 * host1x_device_parse_dt() - scan device tree and add matching subdevices
94 * @device: host1x logical device
95 * @driver: host1x driver
96 */
97static int host1x_device_parse_dt(struct host1x_device *device,
98 struct host1x_driver *driver)
99{
100 struct device_node *np;
101 int err;
102
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);
107 if (err < 0) {
108 of_node_put(np);
109 return err;
110 }
111 }
112 }
113
114 return 0;
115}
116
117static void host1x_subdev_register(struct host1x_device *device,
118 struct host1x_subdev *subdev,
119 struct host1x_client *client)
120{
121 int err;
122
123 /*
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.
127 */
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);
136
137 if (list_empty(&device->subdevs)) {
138 err = device_add(&device->dev);
139 if (err < 0)
140 dev_err(&device->dev, "failed to add: %d\n", err);
141 else
142 device->registered = true;
143 }
144}
145
146static void __host1x_subdev_unregister(struct host1x_device *device,
147 struct host1x_subdev *subdev)
148{
149 struct host1x_client *client = subdev->client;
150
151 /*
152 * If all subdevices have been activated, we're about to remove the
153 * first active subdevice, so unload the driver first.
154 */
155 if (list_empty(&device->subdevs)) {
156 if (device->registered) {
157 device->registered = false;
158 device_del(&device->dev);
159 }
160 }
161
162 /*
163 * Move the subdevice back to the list of idle subdevices and remove
164 * it from list of clients.
165 */
166 mutex_lock(&device->clients_lock);
167 subdev->client = NULL;
168 client->parent = NULL;
169 list_move_tail(&subdev->list, &device->subdevs);
170 /*
171 * XXX: Perhaps don't do this here, but rather explicitly remove it
172 * when the device is about to be deleted.
173 *
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.
177 */
178 list_del_init(&client->list);
179 mutex_unlock(&device->clients_lock);
180}
181
182static void host1x_subdev_unregister(struct host1x_device *device,
183 struct host1x_subdev *subdev)
184{
185 mutex_lock(&device->subdevs_lock);
186 __host1x_subdev_unregister(device, subdev);
187 mutex_unlock(&device->subdevs_lock);
188}
189
190/**
191 * host1x_device_init() - initialize a host1x logical device
192 * @device: host1x logical device
193 *
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()).
199 */
200int host1x_device_init(struct host1x_device *device)
201{
202 struct host1x_client *client;
203 int err;
204
205 mutex_lock(&device->clients_lock);
206
207 list_for_each_entry(client, &device->clients, list) {
208 if (client->ops && client->ops->init) {
209 err = client->ops->init(client);
210 if (err < 0) {
211 dev_err(&device->dev,
212 "failed to initialize %s: %d\n",
213 dev_name(client->dev), err);
214 goto teardown;
215 }
216 }
217 }
218
219 mutex_unlock(&device->clients_lock);
220
221 return 0;
222
223teardown:
224 list_for_each_entry_continue_reverse(client, &device->clients, list)
225 if (client->ops->exit)
226 client->ops->exit(client);
227
228 mutex_unlock(&device->clients_lock);
229 return err;
230}
231EXPORT_SYMBOL(host1x_device_init);
232
233/**
234 * host1x_device_exit() - uninitialize host1x logical device
235 * @device: host1x logical device
236 *
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
240 * longer be used.
241 */
242int host1x_device_exit(struct host1x_device *device)
243{
244 struct host1x_client *client;
245 int err;
246
247 mutex_lock(&device->clients_lock);
248
249 list_for_each_entry_reverse(client, &device->clients, list) {
250 if (client->ops && client->ops->exit) {
251 err = client->ops->exit(client);
252 if (err < 0) {
253 dev_err(&device->dev,
254 "failed to cleanup %s: %d\n",
255 dev_name(client->dev), err);
256 mutex_unlock(&device->clients_lock);
257 return err;
258 }
259 }
260 }
261
262 mutex_unlock(&device->clients_lock);
263
264 return 0;
265}
266EXPORT_SYMBOL(host1x_device_exit);
267
268static int host1x_add_client(struct host1x *host1x,
269 struct host1x_client *client)
270{
271 struct host1x_device *device;
272 struct host1x_subdev *subdev;
273
274 mutex_lock(&host1x->devices_lock);
275
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);
281 return 0;
282 }
283 }
284 }
285
286 mutex_unlock(&host1x->devices_lock);
287 return -ENODEV;
288}
289
290static int host1x_del_client(struct host1x *host1x,
291 struct host1x_client *client)
292{
293 struct host1x_device *device, *dt;
294 struct host1x_subdev *subdev;
295
296 mutex_lock(&host1x->devices_lock);
297
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);
303 return 0;
304 }
305 }
306 }
307
308 mutex_unlock(&host1x->devices_lock);
309 return -ENODEV;
310}
311
312static int host1x_device_match(struct device *dev, struct device_driver *drv)
313{
314 return strcmp(dev_name(dev), drv->name) == 0;
315}
316
317static int host1x_dma_configure(struct device *dev)
318{
319 return of_dma_configure(dev, dev->of_node, true);
320}
321
322static const struct dev_pm_ops host1x_device_pm_ops = {
323 .suspend = pm_generic_suspend,
324 .resume = pm_generic_resume,
325 .freeze = pm_generic_freeze,
326 .thaw = pm_generic_thaw,
327 .poweroff = pm_generic_poweroff,
328 .restore = pm_generic_restore,
329};
330
331struct bus_type host1x_bus_type = {
332 .name = "host1x",
333 .match = host1x_device_match,
334 .dma_configure = host1x_dma_configure,
335 .pm = &host1x_device_pm_ops,
336};
337
338static void __host1x_device_del(struct host1x_device *device)
339{
340 struct host1x_subdev *subdev, *sd;
341 struct host1x_client *client, *cl;
342
343 mutex_lock(&device->subdevs_lock);
344
345 /* unregister subdevices */
346 list_for_each_entry_safe(subdev, sd, &device->active, list) {
347 /*
348 * host1x_subdev_unregister() will remove the client from
349 * any lists, so we'll need to manually add it back to the
350 * list of idle clients.
351 *
352 * XXX: Alternatively, perhaps don't remove the client from
353 * any lists in host1x_subdev_unregister() and instead do
354 * that explicitly from host1x_unregister_client()?
355 */
356 client = subdev->client;
357
358 __host1x_subdev_unregister(device, subdev);
359
360 /* add the client to the list of idle clients */
361 mutex_lock(&clients_lock);
362 list_add_tail(&client->list, &clients);
363 mutex_unlock(&clients_lock);
364 }
365
366 /* remove subdevices */
367 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
368 host1x_subdev_del(subdev);
369
370 mutex_unlock(&device->subdevs_lock);
371
372 /* move clients to idle list */
373 mutex_lock(&clients_lock);
374 mutex_lock(&device->clients_lock);
375
376 list_for_each_entry_safe(client, cl, &device->clients, list)
377 list_move_tail(&client->list, &clients);
378
379 mutex_unlock(&device->clients_lock);
380 mutex_unlock(&clients_lock);
381
382 /* finally remove the device */
383 list_del_init(&device->list);
384}
385
386static void host1x_device_release(struct device *dev)
387{
388 struct host1x_device *device = to_host1x_device(dev);
389
390 __host1x_device_del(device);
391 kfree(device);
392}
393
394static int host1x_device_add(struct host1x *host1x,
395 struct host1x_driver *driver)
396{
397 struct host1x_client *client, *tmp;
398 struct host1x_subdev *subdev;
399 struct host1x_device *device;
400 int err;
401
402 device = kzalloc(sizeof(*device), GFP_KERNEL);
403 if (!device)
404 return -ENOMEM;
405
406 device_initialize(&device->dev);
407
408 mutex_init(&device->subdevs_lock);
409 INIT_LIST_HEAD(&device->subdevs);
410 INIT_LIST_HEAD(&device->active);
411 mutex_init(&device->clients_lock);
412 INIT_LIST_HEAD(&device->clients);
413 INIT_LIST_HEAD(&device->list);
414 device->driver = driver;
415
416 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
417 device->dev.dma_mask = &device->dev.coherent_dma_mask;
418 dev_set_name(&device->dev, "%s", driver->driver.name);
419 device->dev.release = host1x_device_release;
420 device->dev.of_node = host1x->dev->of_node;
421 device->dev.bus = &host1x_bus_type;
422 device->dev.parent = host1x->dev;
423
424 of_dma_configure(&device->dev, host1x->dev->of_node, true);
425
426 device->dev.dma_parms = &device->dma_parms;
427 dma_set_max_seg_size(&device->dev, SZ_4M);
428
429 err = host1x_device_parse_dt(device, driver);
430 if (err < 0) {
431 kfree(device);
432 return err;
433 }
434
435 list_add_tail(&device->list, &host1x->devices);
436
437 mutex_lock(&clients_lock);
438
439 list_for_each_entry_safe(client, tmp, &clients, list) {
440 list_for_each_entry(subdev, &device->subdevs, list) {
441 if (subdev->np == client->dev->of_node) {
442 host1x_subdev_register(device, subdev, client);
443 break;
444 }
445 }
446 }
447
448 mutex_unlock(&clients_lock);
449
450 return 0;
451}
452
453/*
454 * Removes a device by first unregistering any subdevices and then removing
455 * itself from the list of devices.
456 *
457 * This function must be called with the host1x->devices_lock held.
458 */
459static void host1x_device_del(struct host1x *host1x,
460 struct host1x_device *device)
461{
462 if (device->registered) {
463 device->registered = false;
464 device_del(&device->dev);
465 }
466
467 put_device(&device->dev);
468}
469
470static void host1x_attach_driver(struct host1x *host1x,
471 struct host1x_driver *driver)
472{
473 struct host1x_device *device;
474 int err;
475
476 mutex_lock(&host1x->devices_lock);
477
478 list_for_each_entry(device, &host1x->devices, list) {
479 if (device->driver == driver) {
480 mutex_unlock(&host1x->devices_lock);
481 return;
482 }
483 }
484
485 err = host1x_device_add(host1x, driver);
486 if (err < 0)
487 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
488
489 mutex_unlock(&host1x->devices_lock);
490}
491
492static void host1x_detach_driver(struct host1x *host1x,
493 struct host1x_driver *driver)
494{
495 struct host1x_device *device, *tmp;
496
497 mutex_lock(&host1x->devices_lock);
498
499 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
500 if (device->driver == driver)
501 host1x_device_del(host1x, device);
502
503 mutex_unlock(&host1x->devices_lock);
504}
505
506/**
507 * host1x_register() - register a host1x controller
508 * @host1x: host1x controller
509 *
510 * The host1x controller driver uses this to register a host1x controller with
511 * the infrastructure. Note that all Tegra SoC generations have only ever come
512 * with a single host1x instance, so this function is somewhat academic.
513 */
514int host1x_register(struct host1x *host1x)
515{
516 struct host1x_driver *driver;
517
518 mutex_lock(&devices_lock);
519 list_add_tail(&host1x->list, &devices);
520 mutex_unlock(&devices_lock);
521
522 mutex_lock(&drivers_lock);
523
524 list_for_each_entry(driver, &drivers, list)
525 host1x_attach_driver(host1x, driver);
526
527 mutex_unlock(&drivers_lock);
528
529 return 0;
530}
531
532/**
533 * host1x_unregister() - unregister a host1x controller
534 * @host1x: host1x controller
535 *
536 * The host1x controller driver uses this to remove a host1x controller from
537 * the infrastructure.
538 */
539int host1x_unregister(struct host1x *host1x)
540{
541 struct host1x_driver *driver;
542
543 mutex_lock(&drivers_lock);
544
545 list_for_each_entry(driver, &drivers, list)
546 host1x_detach_driver(host1x, driver);
547
548 mutex_unlock(&drivers_lock);
549
550 mutex_lock(&devices_lock);
551 list_del_init(&host1x->list);
552 mutex_unlock(&devices_lock);
553
554 return 0;
555}
556
557static int host1x_device_probe(struct device *dev)
558{
559 struct host1x_driver *driver = to_host1x_driver(dev->driver);
560 struct host1x_device *device = to_host1x_device(dev);
561
562 if (driver->probe)
563 return driver->probe(device);
564
565 return 0;
566}
567
568static int host1x_device_remove(struct device *dev)
569{
570 struct host1x_driver *driver = to_host1x_driver(dev->driver);
571 struct host1x_device *device = to_host1x_device(dev);
572
573 if (driver->remove)
574 return driver->remove(device);
575
576 return 0;
577}
578
579static void host1x_device_shutdown(struct device *dev)
580{
581 struct host1x_driver *driver = to_host1x_driver(dev->driver);
582 struct host1x_device *device = to_host1x_device(dev);
583
584 if (driver->shutdown)
585 driver->shutdown(device);
586}
587
588/**
589 * host1x_driver_register_full() - register a host1x driver
590 * @driver: host1x driver
591 * @owner: owner module
592 *
593 * Drivers for host1x logical devices call this function to register a driver
594 * with the infrastructure. Note that since these drive logical devices, the
595 * registration of the driver actually triggers tho logical device creation.
596 * A logical device will be created for each host1x instance.
597 */
598int host1x_driver_register_full(struct host1x_driver *driver,
599 struct module *owner)
600{
601 struct host1x *host1x;
602
603 INIT_LIST_HEAD(&driver->list);
604
605 mutex_lock(&drivers_lock);
606 list_add_tail(&driver->list, &drivers);
607 mutex_unlock(&drivers_lock);
608
609 mutex_lock(&devices_lock);
610
611 list_for_each_entry(host1x, &devices, list)
612 host1x_attach_driver(host1x, driver);
613
614 mutex_unlock(&devices_lock);
615
616 driver->driver.bus = &host1x_bus_type;
617 driver->driver.owner = owner;
618 driver->driver.probe = host1x_device_probe;
619 driver->driver.remove = host1x_device_remove;
620 driver->driver.shutdown = host1x_device_shutdown;
621
622 return driver_register(&driver->driver);
623}
624EXPORT_SYMBOL(host1x_driver_register_full);
625
626/**
627 * host1x_driver_unregister() - unregister a host1x driver
628 * @driver: host1x driver
629 *
630 * Unbinds the driver from each of the host1x logical devices that it is
631 * bound to, effectively removing the subsystem devices that they represent.
632 */
633void host1x_driver_unregister(struct host1x_driver *driver)
634{
635 driver_unregister(&driver->driver);
636
637 mutex_lock(&drivers_lock);
638 list_del_init(&driver->list);
639 mutex_unlock(&drivers_lock);
640}
641EXPORT_SYMBOL(host1x_driver_unregister);
642
643/**
644 * host1x_client_register() - register a host1x client
645 * @client: host1x client
646 *
647 * Registers a host1x client with each host1x controller instance. Note that
648 * each client will only match their parent host1x controller and will only be
649 * associated with that instance. Once all clients have been registered with
650 * their parent host1x controller, the infrastructure will set up the logical
651 * device and call host1x_device_init(), which will in turn call each client's
652 * &host1x_client_ops.init implementation.
653 */
654int host1x_client_register(struct host1x_client *client)
655{
656 struct host1x *host1x;
657 int err;
658
659 mutex_lock(&devices_lock);
660
661 list_for_each_entry(host1x, &devices, list) {
662 err = host1x_add_client(host1x, client);
663 if (!err) {
664 mutex_unlock(&devices_lock);
665 return 0;
666 }
667 }
668
669 mutex_unlock(&devices_lock);
670
671 mutex_lock(&clients_lock);
672 list_add_tail(&client->list, &clients);
673 mutex_unlock(&clients_lock);
674
675 return 0;
676}
677EXPORT_SYMBOL(host1x_client_register);
678
679/**
680 * host1x_client_unregister() - unregister a host1x client
681 * @client: host1x client
682 *
683 * Removes a host1x client from its host1x controller instance. If a logical
684 * device has already been initialized, it will be torn down.
685 */
686int host1x_client_unregister(struct host1x_client *client)
687{
688 struct host1x_client *c;
689 struct host1x *host1x;
690 int err;
691
692 mutex_lock(&devices_lock);
693
694 list_for_each_entry(host1x, &devices, list) {
695 err = host1x_del_client(host1x, client);
696 if (!err) {
697 mutex_unlock(&devices_lock);
698 return 0;
699 }
700 }
701
702 mutex_unlock(&devices_lock);
703 mutex_lock(&clients_lock);
704
705 list_for_each_entry(c, &clients, list) {
706 if (c == client) {
707 list_del_init(&c->list);
708 break;
709 }
710 }
711
712 mutex_unlock(&clients_lock);
713
714 return 0;
715}
716EXPORT_SYMBOL(host1x_client_unregister);