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