blob: c2e7fba370bb155bfc2c4a550450be9e370b2bce [file] [log] [blame]
Thierry Reding776dc382013-10-14 14:43:22 +02001/*
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
Thierry Redingd24b2892013-11-08 13:20:23 +010022#include "bus.h"
Thierry Reding776dc382013-10-14 14:43:22 +020023#include "dev.h"
24
25static DEFINE_MUTEX(clients_lock);
26static LIST_HEAD(clients);
27
28static DEFINE_MUTEX(drivers_lock);
29static LIST_HEAD(drivers);
30
31static DEFINE_MUTEX(devices_lock);
32static LIST_HEAD(devices);
33
34struct host1x_subdev {
35 struct host1x_client *client;
36 struct device_node *np;
37 struct list_head list;
38};
39
40/**
41 * host1x_subdev_add() - add a new subdevice with an associated device node
42 */
43static int host1x_subdev_add(struct host1x_device *device,
44 struct device_node *np)
45{
46 struct host1x_subdev *subdev;
47
48 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
49 if (!subdev)
50 return -ENOMEM;
51
52 INIT_LIST_HEAD(&subdev->list);
53 subdev->np = of_node_get(np);
54
55 mutex_lock(&device->subdevs_lock);
56 list_add_tail(&subdev->list, &device->subdevs);
57 mutex_unlock(&device->subdevs_lock);
58
59 return 0;
60}
61
62/**
63 * host1x_subdev_del() - remove subdevice
64 */
65static void host1x_subdev_del(struct host1x_subdev *subdev)
66{
67 list_del(&subdev->list);
68 of_node_put(subdev->np);
69 kfree(subdev);
70}
71
72/**
73 * host1x_device_parse_dt() - scan device tree and add matching subdevices
74 */
Thierry Redingf4c5cf82014-12-18 15:29:14 +010075static int host1x_device_parse_dt(struct host1x_device *device,
76 struct host1x_driver *driver)
Thierry Reding776dc382013-10-14 14:43:22 +020077{
78 struct device_node *np;
79 int err;
80
81 for_each_child_of_node(device->dev.parent->of_node, np) {
Thierry Redingf4c5cf82014-12-18 15:29:14 +010082 if (of_match_node(driver->subdevs, np) &&
Thierry Reding776dc382013-10-14 14:43:22 +020083 of_device_is_available(np)) {
84 err = host1x_subdev_add(device, np);
Amitoj Kaur Chawla93ec3022016-01-24 22:02:10 +053085 if (err < 0) {
86 of_node_put(np);
Thierry Reding776dc382013-10-14 14:43:22 +020087 return err;
Amitoj Kaur Chawla93ec3022016-01-24 22:02:10 +053088 }
Thierry Reding776dc382013-10-14 14:43:22 +020089 }
90 }
91
92 return 0;
93}
94
95static void host1x_subdev_register(struct host1x_device *device,
96 struct host1x_subdev *subdev,
97 struct host1x_client *client)
98{
99 int err;
100
101 /*
102 * Move the subdevice to the list of active (registered) subdevices
103 * and associate it with a client. At the same time, associate the
104 * client with its parent device.
105 */
106 mutex_lock(&device->subdevs_lock);
107 mutex_lock(&device->clients_lock);
108 list_move_tail(&client->list, &device->clients);
109 list_move_tail(&subdev->list, &device->active);
110 client->parent = &device->dev;
111 subdev->client = client;
112 mutex_unlock(&device->clients_lock);
113 mutex_unlock(&device->subdevs_lock);
114
Thierry Reding776dc382013-10-14 14:43:22 +0200115 if (list_empty(&device->subdevs)) {
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100116 err = device_add(&device->dev);
Thierry Reding776dc382013-10-14 14:43:22 +0200117 if (err < 0)
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100118 dev_err(&device->dev, "failed to add: %d\n", err);
Thierry Reding536e1712014-11-05 11:43:26 +0100119 else
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100120 device->registered = true;
Thierry Reding776dc382013-10-14 14:43:22 +0200121 }
122}
123
124static void __host1x_subdev_unregister(struct host1x_device *device,
125 struct host1x_subdev *subdev)
126{
127 struct host1x_client *client = subdev->client;
Thierry Reding776dc382013-10-14 14:43:22 +0200128
129 /*
130 * If all subdevices have been activated, we're about to remove the
131 * first active subdevice, so unload the driver first.
132 */
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100133 if (list_empty(&device->subdevs)) {
134 if (device->registered) {
135 device->registered = false;
136 device_del(&device->dev);
137 }
Thierry Reding776dc382013-10-14 14:43:22 +0200138 }
139
140 /*
141 * Move the subdevice back to the list of idle subdevices and remove
142 * it from list of clients.
143 */
144 mutex_lock(&device->clients_lock);
145 subdev->client = NULL;
146 client->parent = NULL;
147 list_move_tail(&subdev->list, &device->subdevs);
148 /*
149 * XXX: Perhaps don't do this here, but rather explicitly remove it
150 * when the device is about to be deleted.
151 *
152 * This is somewhat complicated by the fact that this function is
153 * used to remove the subdevice when a client is unregistered but
154 * also when the composite device is about to be removed.
155 */
156 list_del_init(&client->list);
157 mutex_unlock(&device->clients_lock);
158}
159
160static void host1x_subdev_unregister(struct host1x_device *device,
161 struct host1x_subdev *subdev)
162{
163 mutex_lock(&device->subdevs_lock);
164 __host1x_subdev_unregister(device, subdev);
165 mutex_unlock(&device->subdevs_lock);
166}
167
168int host1x_device_init(struct host1x_device *device)
169{
170 struct host1x_client *client;
171 int err;
172
173 mutex_lock(&device->clients_lock);
174
175 list_for_each_entry(client, &device->clients, list) {
176 if (client->ops && client->ops->init) {
177 err = client->ops->init(client);
178 if (err < 0) {
179 dev_err(&device->dev,
180 "failed to initialize %s: %d\n",
181 dev_name(client->dev), err);
182 mutex_unlock(&device->clients_lock);
183 return err;
184 }
185 }
186 }
187
188 mutex_unlock(&device->clients_lock);
189
190 return 0;
191}
Thierry Redingfae798a2013-11-08 11:41:42 +0100192EXPORT_SYMBOL(host1x_device_init);
Thierry Reding776dc382013-10-14 14:43:22 +0200193
194int host1x_device_exit(struct host1x_device *device)
195{
196 struct host1x_client *client;
197 int err;
198
199 mutex_lock(&device->clients_lock);
200
201 list_for_each_entry_reverse(client, &device->clients, list) {
202 if (client->ops && client->ops->exit) {
203 err = client->ops->exit(client);
204 if (err < 0) {
205 dev_err(&device->dev,
206 "failed to cleanup %s: %d\n",
207 dev_name(client->dev), err);
208 mutex_unlock(&device->clients_lock);
209 return err;
210 }
211 }
212 }
213
214 mutex_unlock(&device->clients_lock);
215
216 return 0;
217}
Thierry Redingfae798a2013-11-08 11:41:42 +0100218EXPORT_SYMBOL(host1x_device_exit);
Thierry Reding776dc382013-10-14 14:43:22 +0200219
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200220static int host1x_add_client(struct host1x *host1x,
221 struct host1x_client *client)
Thierry Reding776dc382013-10-14 14:43:22 +0200222{
223 struct host1x_device *device;
224 struct host1x_subdev *subdev;
225
226 mutex_lock(&host1x->devices_lock);
227
228 list_for_each_entry(device, &host1x->devices, list) {
229 list_for_each_entry(subdev, &device->subdevs, list) {
230 if (subdev->np == client->dev->of_node) {
231 host1x_subdev_register(device, subdev, client);
232 mutex_unlock(&host1x->devices_lock);
233 return 0;
234 }
235 }
236 }
237
238 mutex_unlock(&host1x->devices_lock);
239 return -ENODEV;
240}
241
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200242static int host1x_del_client(struct host1x *host1x,
243 struct host1x_client *client)
Thierry Reding776dc382013-10-14 14:43:22 +0200244{
245 struct host1x_device *device, *dt;
246 struct host1x_subdev *subdev;
247
248 mutex_lock(&host1x->devices_lock);
249
250 list_for_each_entry_safe(device, dt, &host1x->devices, list) {
251 list_for_each_entry(subdev, &device->active, list) {
252 if (subdev->client == client) {
253 host1x_subdev_unregister(device, subdev);
254 mutex_unlock(&host1x->devices_lock);
255 return 0;
256 }
257 }
258 }
259
260 mutex_unlock(&host1x->devices_lock);
261 return -ENODEV;
262}
263
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100264static int host1x_device_match(struct device *dev, struct device_driver *drv)
265{
266 return strcmp(dev_name(dev), drv->name) == 0;
267}
268
269static int host1x_device_probe(struct device *dev)
270{
271 struct host1x_driver *driver = to_host1x_driver(dev->driver);
272 struct host1x_device *device = to_host1x_device(dev);
273
274 if (driver->probe)
275 return driver->probe(device);
276
277 return 0;
278}
279
280static int host1x_device_remove(struct device *dev)
281{
282 struct host1x_driver *driver = to_host1x_driver(dev->driver);
283 struct host1x_device *device = to_host1x_device(dev);
284
285 if (driver->remove)
286 return driver->remove(device);
287
288 return 0;
289}
290
291static void host1x_device_shutdown(struct device *dev)
292{
293 struct host1x_driver *driver = to_host1x_driver(dev->driver);
294 struct host1x_device *device = to_host1x_device(dev);
295
296 if (driver->shutdown)
297 driver->shutdown(device);
298}
299
300static const struct dev_pm_ops host1x_device_pm_ops = {
301 .suspend = pm_generic_suspend,
302 .resume = pm_generic_resume,
303 .freeze = pm_generic_freeze,
304 .thaw = pm_generic_thaw,
305 .poweroff = pm_generic_poweroff,
306 .restore = pm_generic_restore,
Thierry Reding776dc382013-10-14 14:43:22 +0200307};
308
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100309struct bus_type host1x_bus_type = {
310 .name = "host1x",
311 .match = host1x_device_match,
312 .probe = host1x_device_probe,
313 .remove = host1x_device_remove,
314 .shutdown = host1x_device_shutdown,
315 .pm = &host1x_device_pm_ops,
316};
Thierry Reding776dc382013-10-14 14:43:22 +0200317
Thierry Reding99d2cd82014-12-18 15:26:02 +0100318static void __host1x_device_del(struct host1x_device *device)
319{
320 struct host1x_subdev *subdev, *sd;
321 struct host1x_client *client, *cl;
322
323 mutex_lock(&device->subdevs_lock);
324
325 /* unregister subdevices */
326 list_for_each_entry_safe(subdev, sd, &device->active, list) {
327 /*
328 * host1x_subdev_unregister() will remove the client from
329 * any lists, so we'll need to manually add it back to the
330 * list of idle clients.
331 *
332 * XXX: Alternatively, perhaps don't remove the client from
333 * any lists in host1x_subdev_unregister() and instead do
334 * that explicitly from host1x_unregister_client()?
335 */
336 client = subdev->client;
337
338 __host1x_subdev_unregister(device, subdev);
339
340 /* add the client to the list of idle clients */
341 mutex_lock(&clients_lock);
342 list_add_tail(&client->list, &clients);
343 mutex_unlock(&clients_lock);
344 }
345
346 /* remove subdevices */
347 list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
348 host1x_subdev_del(subdev);
349
350 mutex_unlock(&device->subdevs_lock);
351
352 /* move clients to idle list */
353 mutex_lock(&clients_lock);
354 mutex_lock(&device->clients_lock);
355
356 list_for_each_entry_safe(client, cl, &device->clients, list)
357 list_move_tail(&client->list, &clients);
358
359 mutex_unlock(&device->clients_lock);
360 mutex_unlock(&clients_lock);
361
362 /* finally remove the device */
363 list_del_init(&device->list);
364}
365
Thierry Reding776dc382013-10-14 14:43:22 +0200366static void host1x_device_release(struct device *dev)
367{
368 struct host1x_device *device = to_host1x_device(dev);
369
Thierry Reding99d2cd82014-12-18 15:26:02 +0100370 __host1x_device_del(device);
Thierry Reding776dc382013-10-14 14:43:22 +0200371 kfree(device);
372}
373
374static int host1x_device_add(struct host1x *host1x,
375 struct host1x_driver *driver)
376{
377 struct host1x_client *client, *tmp;
378 struct host1x_subdev *subdev;
379 struct host1x_device *device;
380 int err;
381
382 device = kzalloc(sizeof(*device), GFP_KERNEL);
383 if (!device)
384 return -ENOMEM;
385
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100386 device_initialize(&device->dev);
387
Thierry Reding776dc382013-10-14 14:43:22 +0200388 mutex_init(&device->subdevs_lock);
389 INIT_LIST_HEAD(&device->subdevs);
390 INIT_LIST_HEAD(&device->active);
391 mutex_init(&device->clients_lock);
392 INIT_LIST_HEAD(&device->clients);
393 INIT_LIST_HEAD(&device->list);
394 device->driver = driver;
395
396 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
397 device->dev.dma_mask = &device->dev.coherent_dma_mask;
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100398 dev_set_name(&device->dev, "%s", driver->driver.name);
Thierry Reding776dc382013-10-14 14:43:22 +0200399 device->dev.release = host1x_device_release;
Thierry Reding776dc382013-10-14 14:43:22 +0200400 device->dev.bus = &host1x_bus_type;
401 device->dev.parent = host1x->dev;
402
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100403 err = host1x_device_parse_dt(device, driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200404 if (err < 0) {
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100405 kfree(device);
Thierry Reding776dc382013-10-14 14:43:22 +0200406 return err;
407 }
408
Thierry Reding776dc382013-10-14 14:43:22 +0200409 list_add_tail(&device->list, &host1x->devices);
Thierry Reding776dc382013-10-14 14:43:22 +0200410
411 mutex_lock(&clients_lock);
412
413 list_for_each_entry_safe(client, tmp, &clients, list) {
414 list_for_each_entry(subdev, &device->subdevs, list) {
415 if (subdev->np == client->dev->of_node) {
416 host1x_subdev_register(device, subdev, client);
417 break;
418 }
419 }
420 }
421
422 mutex_unlock(&clients_lock);
423
424 return 0;
425}
426
427/*
428 * Removes a device by first unregistering any subdevices and then removing
429 * itself from the list of devices.
430 *
431 * This function must be called with the host1x->devices_lock held.
432 */
433static void host1x_device_del(struct host1x *host1x,
434 struct host1x_device *device)
435{
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100436 if (device->registered) {
437 device->registered = false;
438 device_del(&device->dev);
439 }
440
441 put_device(&device->dev);
Thierry Reding776dc382013-10-14 14:43:22 +0200442}
443
444static void host1x_attach_driver(struct host1x *host1x,
445 struct host1x_driver *driver)
446{
447 struct host1x_device *device;
448 int err;
449
450 mutex_lock(&host1x->devices_lock);
451
452 list_for_each_entry(device, &host1x->devices, list) {
453 if (device->driver == driver) {
454 mutex_unlock(&host1x->devices_lock);
455 return;
456 }
457 }
458
Thierry Reding776dc382013-10-14 14:43:22 +0200459 err = host1x_device_add(host1x, driver);
460 if (err < 0)
461 dev_err(host1x->dev, "failed to allocate device: %d\n", err);
Thierry Reding38d98de2014-12-18 15:06:56 +0100462
463 mutex_unlock(&host1x->devices_lock);
Thierry Reding776dc382013-10-14 14:43:22 +0200464}
465
466static void host1x_detach_driver(struct host1x *host1x,
467 struct host1x_driver *driver)
468{
469 struct host1x_device *device, *tmp;
470
471 mutex_lock(&host1x->devices_lock);
472
473 list_for_each_entry_safe(device, tmp, &host1x->devices, list)
474 if (device->driver == driver)
475 host1x_device_del(host1x, device);
476
477 mutex_unlock(&host1x->devices_lock);
478}
479
480int host1x_register(struct host1x *host1x)
481{
482 struct host1x_driver *driver;
483
484 mutex_lock(&devices_lock);
485 list_add_tail(&host1x->list, &devices);
486 mutex_unlock(&devices_lock);
487
488 mutex_lock(&drivers_lock);
489
490 list_for_each_entry(driver, &drivers, list)
491 host1x_attach_driver(host1x, driver);
492
493 mutex_unlock(&drivers_lock);
494
495 return 0;
496}
497
498int host1x_unregister(struct host1x *host1x)
499{
500 struct host1x_driver *driver;
501
502 mutex_lock(&drivers_lock);
503
504 list_for_each_entry(driver, &drivers, list)
505 host1x_detach_driver(host1x, driver);
506
507 mutex_unlock(&drivers_lock);
508
509 mutex_lock(&devices_lock);
510 list_del_init(&host1x->list);
511 mutex_unlock(&devices_lock);
512
513 return 0;
514}
515
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100516int host1x_driver_register_full(struct host1x_driver *driver,
517 struct module *owner)
Thierry Reding776dc382013-10-14 14:43:22 +0200518{
519 struct host1x *host1x;
520
521 INIT_LIST_HEAD(&driver->list);
522
523 mutex_lock(&drivers_lock);
524 list_add_tail(&driver->list, &drivers);
525 mutex_unlock(&drivers_lock);
526
527 mutex_lock(&devices_lock);
528
529 list_for_each_entry(host1x, &devices, list)
530 host1x_attach_driver(host1x, driver);
531
532 mutex_unlock(&devices_lock);
533
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100534 driver->driver.bus = &host1x_bus_type;
535 driver->driver.owner = owner;
536
537 return driver_register(&driver->driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200538}
Thierry Redingf4c5cf82014-12-18 15:29:14 +0100539EXPORT_SYMBOL(host1x_driver_register_full);
Thierry Reding776dc382013-10-14 14:43:22 +0200540
541void host1x_driver_unregister(struct host1x_driver *driver)
542{
Thierry Redinge3e70812015-08-24 14:51:04 +0200543 driver_unregister(&driver->driver);
544
Thierry Reding776dc382013-10-14 14:43:22 +0200545 mutex_lock(&drivers_lock);
546 list_del_init(&driver->list);
547 mutex_unlock(&drivers_lock);
548}
549EXPORT_SYMBOL(host1x_driver_unregister);
550
551int host1x_client_register(struct host1x_client *client)
552{
553 struct host1x *host1x;
554 int err;
555
556 mutex_lock(&devices_lock);
557
558 list_for_each_entry(host1x, &devices, list) {
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200559 err = host1x_add_client(host1x, client);
Thierry Reding776dc382013-10-14 14:43:22 +0200560 if (!err) {
561 mutex_unlock(&devices_lock);
562 return 0;
563 }
564 }
565
566 mutex_unlock(&devices_lock);
567
568 mutex_lock(&clients_lock);
569 list_add_tail(&client->list, &clients);
570 mutex_unlock(&clients_lock);
571
572 return 0;
573}
574EXPORT_SYMBOL(host1x_client_register);
575
576int host1x_client_unregister(struct host1x_client *client)
577{
578 struct host1x_client *c;
579 struct host1x *host1x;
580 int err;
581
582 mutex_lock(&devices_lock);
583
584 list_for_each_entry(host1x, &devices, list) {
Thierry Reding0c7dfd32014-05-22 11:12:17 +0200585 err = host1x_del_client(host1x, client);
Thierry Reding776dc382013-10-14 14:43:22 +0200586 if (!err) {
587 mutex_unlock(&devices_lock);
588 return 0;
589 }
590 }
591
592 mutex_unlock(&devices_lock);
593 mutex_lock(&clients_lock);
594
595 list_for_each_entry(c, &clients, list) {
596 if (c == client) {
597 list_del_init(&c->list);
598 break;
599 }
600 }
601
602 mutex_unlock(&clients_lock);
603
604 return 0;
605}
606EXPORT_SYMBOL(host1x_client_unregister);