1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
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
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
72 of_node_put(child);
73 return err;
74 }
75 }
76 }
77
78 return 0;
79}
80
81
82
83
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
94
95
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
125
126
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
153
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
164
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
172
173
174
175
176
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
192
193
194
195
196
197
198
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
235
236
237
238
239
240
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
346 list_for_each_entry_safe(subdev, sd, &device->active, list) {
347
348
349
350
351
352
353
354
355
356 client = subdev->client;
357
358 __host1x_subdev_unregister(device, subdev);
359
360
361 mutex_lock(&clients_lock);
362 list_add_tail(&client->list, &clients);
363 mutex_unlock(&clients_lock);
364 }
365
366
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
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
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 err = host1x_device_parse_dt(device, driver);
427 if (err < 0) {
428 kfree(device);
429 return err;
430 }
431
432 list_add_tail(&device->list, &host1x->devices);
433
434 mutex_lock(&clients_lock);
435
436 list_for_each_entry_safe(client, tmp, &clients, list) {
437 list_for_each_entry(subdev, &device->subdevs, list) {
438 if (subdev->np == client->dev->of_node) {
439 host1x_subdev_register(device, subdev, client);
440 break;
441 }
442 }
443 }
444
445 mutex_unlock(&clients_lock);
446
447 return 0;
448}
449
450
451
452
453
454
455
456static void host1x_device_del(struct host1x *host1x,
457 struct host1x_device *device)
458{
459 if (device->registered) {
460 device->registered = false;
461 device_del(&device->dev);
462 }
463
464 put_device(&device->dev);
465}
466
467static void host1x_attach_driver(struct host1x *host1x,
468 struct host1x_driver *driver)
469{
470 struct host1x_device *device;
471 int err;
472
473 mutex_lock(&host1x->devices_lock);
474
475 list_for_each_entry(device, &host1x->devices, list) {
476 if (device->driver == driver) {
477 mutex_unlock(&host1x->devices_lock);
478 return;
479 }
480 }
481
482 err = host1x_device_add(host1x, driver);
483 if (err < 0)
484 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
485
486 mutex_unlock(&host1x->devices_lock);
487}
488
489static void host1x_detach_driver(struct host1x *host1x,
490 struct host1x_driver *driver)
491{
492 struct host1x_device *device, *tmp;
493
494 mutex_lock(&host1x->devices_lock);
495
496 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
497 if (device->driver == driver)
498 host1x_device_del(host1x, device);
499
500 mutex_unlock(&host1x->devices_lock);
501}
502
503
504
505
506
507
508
509
510
511int host1x_register(struct host1x *host1x)
512{
513 struct host1x_driver *driver;
514
515 mutex_lock(&devices_lock);
516 list_add_tail(&host1x->list, &devices);
517 mutex_unlock(&devices_lock);
518
519 mutex_lock(&drivers_lock);
520
521 list_for_each_entry(driver, &drivers, list)
522 host1x_attach_driver(host1x, driver);
523
524 mutex_unlock(&drivers_lock);
525
526 return 0;
527}
528
529
530
531
532
533
534
535
536int host1x_unregister(struct host1x *host1x)
537{
538 struct host1x_driver *driver;
539
540 mutex_lock(&drivers_lock);
541
542 list_for_each_entry(driver, &drivers, list)
543 host1x_detach_driver(host1x, driver);
544
545 mutex_unlock(&drivers_lock);
546
547 mutex_lock(&devices_lock);
548 list_del_init(&host1x->list);
549 mutex_unlock(&devices_lock);
550
551 return 0;
552}
553
554static int host1x_device_probe(struct device *dev)
555{
556 struct host1x_driver *driver = to_host1x_driver(dev->driver);
557 struct host1x_device *device = to_host1x_device(dev);
558
559 if (driver->probe)
560 return driver->probe(device);
561
562 return 0;
563}
564
565static int host1x_device_remove(struct device *dev)
566{
567 struct host1x_driver *driver = to_host1x_driver(dev->driver);
568 struct host1x_device *device = to_host1x_device(dev);
569
570 if (driver->remove)
571 return driver->remove(device);
572
573 return 0;
574}
575
576static void host1x_device_shutdown(struct device *dev)
577{
578 struct host1x_driver *driver = to_host1x_driver(dev->driver);
579 struct host1x_device *device = to_host1x_device(dev);
580
581 if (driver->shutdown)
582 driver->shutdown(device);
583}
584
585
586
587
588
589
590
591
592
593
594
595int host1x_driver_register_full(struct host1x_driver *driver,
596 struct module *owner)
597{
598 struct host1x *host1x;
599
600 INIT_LIST_HEAD(&driver->list);
601
602 mutex_lock(&drivers_lock);
603 list_add_tail(&driver->list, &drivers);
604 mutex_unlock(&drivers_lock);
605
606 mutex_lock(&devices_lock);
607
608 list_for_each_entry(host1x, &devices, list)
609 host1x_attach_driver(host1x, driver);
610
611 mutex_unlock(&devices_lock);
612
613 driver->driver.bus = &host1x_bus_type;
614 driver->driver.owner = owner;
615 driver->driver.probe = host1x_device_probe;
616 driver->driver.remove = host1x_device_remove;
617 driver->driver.shutdown = host1x_device_shutdown;
618
619 return driver_register(&driver->driver);
620}
621EXPORT_SYMBOL(host1x_driver_register_full);
622
623
624
625
626
627
628
629
630void host1x_driver_unregister(struct host1x_driver *driver)
631{
632 driver_unregister(&driver->driver);
633
634 mutex_lock(&drivers_lock);
635 list_del_init(&driver->list);
636 mutex_unlock(&drivers_lock);
637}
638EXPORT_SYMBOL(host1x_driver_unregister);
639
640
641
642
643
644
645
646
647
648
649
650
651int host1x_client_register(struct host1x_client *client)
652{
653 struct host1x *host1x;
654 int err;
655
656 mutex_lock(&devices_lock);
657
658 list_for_each_entry(host1x, &devices, list) {
659 err = host1x_add_client(host1x, client);
660 if (!err) {
661 mutex_unlock(&devices_lock);
662 return 0;
663 }
664 }
665
666 mutex_unlock(&devices_lock);
667
668 mutex_lock(&clients_lock);
669 list_add_tail(&client->list, &clients);
670 mutex_unlock(&clients_lock);
671
672 return 0;
673}
674EXPORT_SYMBOL(host1x_client_register);
675
676
677
678
679
680
681
682
683int host1x_client_unregister(struct host1x_client *client)
684{
685 struct host1x_client *c;
686 struct host1x *host1x;
687 int err;
688
689 mutex_lock(&devices_lock);
690
691 list_for_each_entry(host1x, &devices, list) {
692 err = host1x_del_client(host1x, client);
693 if (!err) {
694 mutex_unlock(&devices_lock);
695 return 0;
696 }
697 }
698
699 mutex_unlock(&devices_lock);
700 mutex_lock(&clients_lock);
701
702 list_for_each_entry(c, &clients, list) {
703 if (c == client) {
704 list_del_init(&c->list);
705 break;
706 }
707 }
708
709 mutex_unlock(&clients_lock);
710
711 return 0;
712}
713EXPORT_SYMBOL(host1x_client_unregister);
714