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