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/dma-mapping.h> 9 #include <linux/host1x.h> 10 #include <linux/of.h> 11 #include <linux/seq_file.h> 12 #include <linux/slab.h> 13 #include <linux/of_device.h> 14 15 #include "bus.h" 16 #include "dev.h" 17 18 static DEFINE_MUTEX(clients_lock); 19 static LIST_HEAD(clients); 20 21 static DEFINE_MUTEX(drivers_lock); 22 static LIST_HEAD(drivers); 23 24 static DEFINE_MUTEX(devices_lock); 25 static LIST_HEAD(devices); 26 27 struct host1x_subdev { 28 struct host1x_client *client; 29 struct device_node *np; 30 struct list_head list; 31 }; 32 33 /** 34 * host1x_subdev_add() - add a new subdevice with an associated device node 35 * @device: host1x device to add the subdevice to 36 * @driver: host1x driver containing the subdevices 37 * @np: device node 38 */ 39 static int host1x_subdev_add(struct host1x_device *device, 40 struct host1x_driver *driver, 41 struct device_node *np) 42 { 43 struct host1x_subdev *subdev; 44 int err; 45 46 subdev = kzalloc(sizeof(*subdev), GFP_KERNEL); 47 if (!subdev) 48 return -ENOMEM; 49 50 INIT_LIST_HEAD(&subdev->list); 51 subdev->np = of_node_get(np); 52 53 mutex_lock(&device->subdevs_lock); 54 list_add_tail(&subdev->list, &device->subdevs); 55 mutex_unlock(&device->subdevs_lock); 56 57 /* recursively add children */ 58 for_each_child_of_node_scoped(np, child) { 59 if (of_match_node(driver->subdevs, child) && 60 of_device_is_available(child)) { 61 err = host1x_subdev_add(device, driver, child); 62 if (err < 0) { 63 /* XXX cleanup? */ 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 */ 76 static 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 */ 88 static int host1x_device_parse_dt(struct host1x_device *device, 89 struct host1x_driver *driver) 90 { 91 int err; 92 93 for_each_child_of_node_scoped(device->dev.parent->of_node, np) { 94 if (of_match_node(driver->subdevs, np) && 95 of_device_is_available(np)) { 96 err = host1x_subdev_add(device, driver, np); 97 if (err < 0) 98 return err; 99 } 100 } 101 102 return 0; 103 } 104 105 static void host1x_subdev_register(struct host1x_device *device, 106 struct host1x_subdev *subdev, 107 struct host1x_client *client) 108 { 109 int err; 110 111 /* 112 * Move the subdevice to the list of active (registered) subdevices 113 * and associate it with a client. At the same time, associate the 114 * client with its parent device. 115 */ 116 mutex_lock(&device->subdevs_lock); 117 mutex_lock(&device->clients_lock); 118 list_move_tail(&client->list, &device->clients); 119 list_move_tail(&subdev->list, &device->active); 120 client->host = &device->dev; 121 subdev->client = client; 122 mutex_unlock(&device->clients_lock); 123 mutex_unlock(&device->subdevs_lock); 124 125 if (list_empty(&device->subdevs)) { 126 err = device_add(&device->dev); 127 if (err < 0) 128 dev_err(&device->dev, "failed to add: %d\n", err); 129 else 130 device->registered = true; 131 } 132 } 133 134 static void __host1x_subdev_unregister(struct host1x_device *device, 135 struct host1x_subdev *subdev) 136 { 137 struct host1x_client *client = subdev->client; 138 139 /* 140 * If all subdevices have been activated, we're about to remove the 141 * first active subdevice, so unload the driver first. 142 */ 143 if (list_empty(&device->subdevs)) { 144 if (device->registered) { 145 device->registered = false; 146 device_del(&device->dev); 147 } 148 } 149 150 /* 151 * Move the subdevice back to the list of idle subdevices and remove 152 * it from list of clients. 153 */ 154 mutex_lock(&device->clients_lock); 155 subdev->client = NULL; 156 client->host = NULL; 157 list_move_tail(&subdev->list, &device->subdevs); 158 /* 159 * XXX: Perhaps don't do this here, but rather explicitly remove it 160 * when the device is about to be deleted. 161 * 162 * This is somewhat complicated by the fact that this function is 163 * used to remove the subdevice when a client is unregistered but 164 * also when the composite device is about to be removed. 165 */ 166 list_del_init(&client->list); 167 mutex_unlock(&device->clients_lock); 168 } 169 170 static void host1x_subdev_unregister(struct host1x_device *device, 171 struct host1x_subdev *subdev) 172 { 173 mutex_lock(&device->subdevs_lock); 174 __host1x_subdev_unregister(device, subdev); 175 mutex_unlock(&device->subdevs_lock); 176 } 177 178 /** 179 * host1x_device_init() - initialize a host1x logical device 180 * @device: host1x logical device 181 * 182 * The driver for the host1x logical device can call this during execution of 183 * its &host1x_driver.probe implementation to initialize each of its clients. 184 * The client drivers access the subsystem specific driver data using the 185 * &host1x_client.parent field and driver data associated with it (usually by 186 * calling dev_get_drvdata()). 187 */ 188 int host1x_device_init(struct host1x_device *device) 189 { 190 struct host1x_client *client; 191 int err; 192 193 mutex_lock(&device->clients_lock); 194 195 list_for_each_entry(client, &device->clients, list) { 196 if (client->ops && client->ops->early_init) { 197 err = client->ops->early_init(client); 198 if (err < 0) { 199 dev_err(&device->dev, "failed to early initialize %s: %d\n", 200 dev_name(client->dev), err); 201 goto teardown_late; 202 } 203 } 204 } 205 206 list_for_each_entry(client, &device->clients, list) { 207 if (client->ops && client->ops->init) { 208 err = client->ops->init(client); 209 if (err < 0) { 210 dev_err(&device->dev, 211 "failed to initialize %s: %d\n", 212 dev_name(client->dev), err); 213 goto teardown; 214 } 215 } 216 } 217 218 mutex_unlock(&device->clients_lock); 219 220 return 0; 221 222 teardown: 223 list_for_each_entry_continue_reverse(client, &device->clients, list) 224 if (client->ops->exit) 225 client->ops->exit(client); 226 227 /* reset client to end of list for late teardown */ 228 client = list_entry(&device->clients, struct host1x_client, list); 229 230 teardown_late: 231 list_for_each_entry_continue_reverse(client, &device->clients, list) 232 if (client->ops->late_exit) 233 client->ops->late_exit(client); 234 235 mutex_unlock(&device->clients_lock); 236 return err; 237 } 238 EXPORT_SYMBOL(host1x_device_init); 239 240 /** 241 * host1x_device_exit() - uninitialize host1x logical device 242 * @device: host1x logical device 243 * 244 * When the driver for a host1x logical device is unloaded, it can call this 245 * function to tear down each of its clients. Typically this is done after a 246 * subsystem-specific data structure is removed and the functionality can no 247 * longer be used. 248 */ 249 int host1x_device_exit(struct host1x_device *device) 250 { 251 struct host1x_client *client; 252 int err; 253 254 mutex_lock(&device->clients_lock); 255 256 list_for_each_entry_reverse(client, &device->clients, list) { 257 if (client->ops && client->ops->exit) { 258 err = client->ops->exit(client); 259 if (err < 0) { 260 dev_err(&device->dev, 261 "failed to cleanup %s: %d\n", 262 dev_name(client->dev), err); 263 mutex_unlock(&device->clients_lock); 264 return err; 265 } 266 } 267 } 268 269 list_for_each_entry_reverse(client, &device->clients, list) { 270 if (client->ops && client->ops->late_exit) { 271 err = client->ops->late_exit(client); 272 if (err < 0) { 273 dev_err(&device->dev, "failed to late cleanup %s: %d\n", 274 dev_name(client->dev), err); 275 mutex_unlock(&device->clients_lock); 276 return err; 277 } 278 } 279 } 280 281 mutex_unlock(&device->clients_lock); 282 283 return 0; 284 } 285 EXPORT_SYMBOL(host1x_device_exit); 286 287 static int host1x_add_client(struct host1x *host1x, 288 struct host1x_client *client) 289 { 290 struct host1x_device *device; 291 struct host1x_subdev *subdev; 292 293 mutex_lock(&host1x->devices_lock); 294 295 list_for_each_entry(device, &host1x->devices, list) { 296 list_for_each_entry(subdev, &device->subdevs, list) { 297 if (subdev->np == client->dev->of_node) { 298 host1x_subdev_register(device, subdev, client); 299 mutex_unlock(&host1x->devices_lock); 300 return 0; 301 } 302 } 303 } 304 305 mutex_unlock(&host1x->devices_lock); 306 return -ENODEV; 307 } 308 309 static int host1x_del_client(struct host1x *host1x, 310 struct host1x_client *client) 311 { 312 struct host1x_device *device, *dt; 313 struct host1x_subdev *subdev; 314 315 mutex_lock(&host1x->devices_lock); 316 317 list_for_each_entry_safe(device, dt, &host1x->devices, list) { 318 list_for_each_entry(subdev, &device->active, list) { 319 if (subdev->client == client) { 320 host1x_subdev_unregister(device, subdev); 321 mutex_unlock(&host1x->devices_lock); 322 return 0; 323 } 324 } 325 } 326 327 mutex_unlock(&host1x->devices_lock); 328 return -ENODEV; 329 } 330 331 static int host1x_device_match(struct device *dev, const struct device_driver *drv) 332 { 333 return strcmp(dev_name(dev), drv->name) == 0; 334 } 335 336 /* 337 * Note that this is really only needed for backwards compatibility 338 * with libdrm, which parses this information from sysfs and will 339 * fail if it can't find the OF_FULLNAME, specifically. 340 */ 341 static int host1x_device_uevent(const struct device *dev, 342 struct kobj_uevent_env *env) 343 { 344 of_device_uevent(dev->parent, env); 345 346 return 0; 347 } 348 349 static int host1x_device_probe(struct device *dev) 350 { 351 struct host1x_driver *driver = to_host1x_driver(dev->driver); 352 struct host1x_device *device = to_host1x_device(dev); 353 354 if (driver->probe) 355 return driver->probe(device); 356 357 return 0; 358 } 359 360 static void host1x_device_remove(struct device *dev) 361 { 362 struct host1x_driver *driver = to_host1x_driver(dev->driver); 363 struct host1x_device *device = to_host1x_device(dev); 364 365 if (driver->remove) 366 driver->remove(device); 367 } 368 369 static void host1x_device_shutdown(struct device *dev) 370 { 371 struct host1x_driver *driver = to_host1x_driver(dev->driver); 372 struct host1x_device *device = to_host1x_device(dev); 373 374 if (dev->driver && driver->shutdown) 375 driver->shutdown(device); 376 } 377 378 379 static const struct dev_pm_ops host1x_device_pm_ops = { 380 .suspend = pm_generic_suspend, 381 .resume = pm_generic_resume, 382 .freeze = pm_generic_freeze, 383 .thaw = pm_generic_thaw, 384 .poweroff = pm_generic_poweroff, 385 .restore = pm_generic_restore, 386 }; 387 388 const struct bus_type host1x_bus_type = { 389 .name = "host1x", 390 .match = host1x_device_match, 391 .uevent = host1x_device_uevent, 392 .probe = host1x_device_probe, 393 .remove = host1x_device_remove, 394 .shutdown = host1x_device_shutdown, 395 .pm = &host1x_device_pm_ops, 396 }; 397 398 static void __host1x_device_del(struct host1x_device *device) 399 { 400 struct host1x_subdev *subdev, *sd; 401 struct host1x_client *client, *cl; 402 403 mutex_lock(&device->subdevs_lock); 404 405 /* unregister subdevices */ 406 list_for_each_entry_safe(subdev, sd, &device->active, list) { 407 /* 408 * host1x_subdev_unregister() will remove the client from 409 * any lists, so we'll need to manually add it back to the 410 * list of idle clients. 411 * 412 * XXX: Alternatively, perhaps don't remove the client from 413 * any lists in host1x_subdev_unregister() and instead do 414 * that explicitly from host1x_unregister_client()? 415 */ 416 client = subdev->client; 417 418 __host1x_subdev_unregister(device, subdev); 419 420 /* add the client to the list of idle clients */ 421 mutex_lock(&clients_lock); 422 list_add_tail(&client->list, &clients); 423 mutex_unlock(&clients_lock); 424 } 425 426 /* remove subdevices */ 427 list_for_each_entry_safe(subdev, sd, &device->subdevs, list) 428 host1x_subdev_del(subdev); 429 430 mutex_unlock(&device->subdevs_lock); 431 432 /* move clients to idle list */ 433 mutex_lock(&clients_lock); 434 mutex_lock(&device->clients_lock); 435 436 list_for_each_entry_safe(client, cl, &device->clients, list) 437 list_move_tail(&client->list, &clients); 438 439 mutex_unlock(&device->clients_lock); 440 mutex_unlock(&clients_lock); 441 442 /* finally remove the device */ 443 list_del_init(&device->list); 444 } 445 446 static void host1x_device_release(struct device *dev) 447 { 448 struct host1x_device *device = to_host1x_device(dev); 449 450 __host1x_device_del(device); 451 kfree(device); 452 } 453 454 static int host1x_device_add(struct host1x *host1x, 455 struct host1x_driver *driver) 456 { 457 struct host1x_client *client, *tmp; 458 struct host1x_subdev *subdev; 459 struct host1x_device *device; 460 int err; 461 462 device = kzalloc(sizeof(*device), GFP_KERNEL); 463 if (!device) 464 return -ENOMEM; 465 466 device_initialize(&device->dev); 467 468 mutex_init(&device->subdevs_lock); 469 INIT_LIST_HEAD(&device->subdevs); 470 INIT_LIST_HEAD(&device->active); 471 mutex_init(&device->clients_lock); 472 INIT_LIST_HEAD(&device->clients); 473 INIT_LIST_HEAD(&device->list); 474 device->driver = driver; 475 476 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask; 477 device->dev.dma_mask = &device->dev.coherent_dma_mask; 478 dev_set_name(&device->dev, "%s", driver->driver.name); 479 device->dev.release = host1x_device_release; 480 device->dev.bus = &host1x_bus_type; 481 device->dev.parent = host1x->dev; 482 483 device->dev.dma_parms = &device->dma_parms; 484 dma_set_max_seg_size(&device->dev, UINT_MAX); 485 486 err = host1x_device_parse_dt(device, driver); 487 if (err < 0) { 488 kfree(device); 489 return err; 490 } 491 492 list_add_tail(&device->list, &host1x->devices); 493 494 mutex_lock(&clients_lock); 495 496 list_for_each_entry_safe(client, tmp, &clients, list) { 497 list_for_each_entry(subdev, &device->subdevs, list) { 498 if (subdev->np == client->dev->of_node) { 499 host1x_subdev_register(device, subdev, client); 500 break; 501 } 502 } 503 } 504 505 mutex_unlock(&clients_lock); 506 507 /* 508 * Add device even if there are no subdevs to ensure syncpoint functionality 509 * is available regardless of whether any engine subdevices are present 510 */ 511 if (list_empty(&device->subdevs)) { 512 err = device_add(&device->dev); 513 if (err < 0) 514 dev_err(&device->dev, "failed to add device: %d\n", err); 515 else 516 device->registered = true; 517 } 518 519 return 0; 520 } 521 522 /* 523 * Removes a device by first unregistering any subdevices and then removing 524 * itself from the list of devices. 525 * 526 * This function must be called with the host1x->devices_lock held. 527 */ 528 static void host1x_device_del(struct host1x *host1x, 529 struct host1x_device *device) 530 { 531 if (device->registered) { 532 device->registered = false; 533 device_del(&device->dev); 534 } 535 536 put_device(&device->dev); 537 } 538 539 static void host1x_attach_driver(struct host1x *host1x, 540 struct host1x_driver *driver) 541 { 542 struct host1x_device *device; 543 int err; 544 545 mutex_lock(&host1x->devices_lock); 546 547 list_for_each_entry(device, &host1x->devices, list) { 548 if (device->driver == driver) { 549 mutex_unlock(&host1x->devices_lock); 550 return; 551 } 552 } 553 554 err = host1x_device_add(host1x, driver); 555 if (err < 0) 556 dev_err(host1x->dev, "failed to allocate device: %d\n", err); 557 558 mutex_unlock(&host1x->devices_lock); 559 } 560 561 static void host1x_detach_driver(struct host1x *host1x, 562 struct host1x_driver *driver) 563 { 564 struct host1x_device *device, *tmp; 565 566 mutex_lock(&host1x->devices_lock); 567 568 list_for_each_entry_safe(device, tmp, &host1x->devices, list) 569 if (device->driver == driver) 570 host1x_device_del(host1x, device); 571 572 mutex_unlock(&host1x->devices_lock); 573 } 574 575 static int host1x_devices_show(struct seq_file *s, void *data) 576 { 577 struct host1x *host1x = s->private; 578 struct host1x_device *device; 579 580 mutex_lock(&host1x->devices_lock); 581 582 list_for_each_entry(device, &host1x->devices, list) { 583 struct host1x_subdev *subdev; 584 585 seq_printf(s, "%s\n", dev_name(&device->dev)); 586 587 mutex_lock(&device->subdevs_lock); 588 589 list_for_each_entry(subdev, &device->active, list) 590 seq_printf(s, " %pOFf: %s\n", subdev->np, 591 dev_name(subdev->client->dev)); 592 593 list_for_each_entry(subdev, &device->subdevs, list) 594 seq_printf(s, " %pOFf:\n", subdev->np); 595 596 mutex_unlock(&device->subdevs_lock); 597 } 598 599 mutex_unlock(&host1x->devices_lock); 600 601 return 0; 602 } 603 DEFINE_SHOW_ATTRIBUTE(host1x_devices); 604 605 /** 606 * host1x_register() - register a host1x controller 607 * @host1x: host1x controller 608 * 609 * The host1x controller driver uses this to register a host1x controller with 610 * the infrastructure. Note that all Tegra SoC generations have only ever come 611 * with a single host1x instance, so this function is somewhat academic. 612 */ 613 int host1x_register(struct host1x *host1x) 614 { 615 struct host1x_driver *driver; 616 617 mutex_lock(&devices_lock); 618 list_add_tail(&host1x->list, &devices); 619 mutex_unlock(&devices_lock); 620 621 mutex_lock(&drivers_lock); 622 623 list_for_each_entry(driver, &drivers, list) 624 host1x_attach_driver(host1x, driver); 625 626 mutex_unlock(&drivers_lock); 627 628 debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x, 629 &host1x_devices_fops); 630 631 return 0; 632 } 633 634 /** 635 * host1x_unregister() - unregister a host1x controller 636 * @host1x: host1x controller 637 * 638 * The host1x controller driver uses this to remove a host1x controller from 639 * the infrastructure. 640 */ 641 int host1x_unregister(struct host1x *host1x) 642 { 643 struct host1x_driver *driver; 644 645 mutex_lock(&drivers_lock); 646 647 list_for_each_entry(driver, &drivers, list) 648 host1x_detach_driver(host1x, driver); 649 650 mutex_unlock(&drivers_lock); 651 652 mutex_lock(&devices_lock); 653 list_del_init(&host1x->list); 654 mutex_unlock(&devices_lock); 655 656 return 0; 657 } 658 659 /** 660 * host1x_driver_register_full() - register a host1x driver 661 * @driver: host1x driver 662 * @owner: owner module 663 * 664 * Drivers for host1x logical devices call this function to register a driver 665 * with the infrastructure. Note that since these drive logical devices, the 666 * registration of the driver actually triggers tho logical device creation. 667 * A logical device will be created for each host1x instance. 668 */ 669 int host1x_driver_register_full(struct host1x_driver *driver, 670 struct module *owner) 671 { 672 struct host1x *host1x; 673 674 INIT_LIST_HEAD(&driver->list); 675 676 mutex_lock(&drivers_lock); 677 list_add_tail(&driver->list, &drivers); 678 mutex_unlock(&drivers_lock); 679 680 mutex_lock(&devices_lock); 681 682 list_for_each_entry(host1x, &devices, list) 683 host1x_attach_driver(host1x, driver); 684 685 mutex_unlock(&devices_lock); 686 687 driver->driver.bus = &host1x_bus_type; 688 driver->driver.owner = owner; 689 690 return driver_register(&driver->driver); 691 } 692 EXPORT_SYMBOL(host1x_driver_register_full); 693 694 /** 695 * host1x_driver_unregister() - unregister a host1x driver 696 * @driver: host1x driver 697 * 698 * Unbinds the driver from each of the host1x logical devices that it is 699 * bound to, effectively removing the subsystem devices that they represent. 700 */ 701 void host1x_driver_unregister(struct host1x_driver *driver) 702 { 703 struct host1x *host1x; 704 705 driver_unregister(&driver->driver); 706 707 mutex_lock(&devices_lock); 708 709 list_for_each_entry(host1x, &devices, list) 710 host1x_detach_driver(host1x, driver); 711 712 mutex_unlock(&devices_lock); 713 714 mutex_lock(&drivers_lock); 715 list_del_init(&driver->list); 716 mutex_unlock(&drivers_lock); 717 } 718 EXPORT_SYMBOL(host1x_driver_unregister); 719 720 /** 721 * __host1x_client_init() - initialize a host1x client 722 * @client: host1x client 723 * @key: lock class key for the client-specific mutex 724 */ 725 void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key) 726 { 727 host1x_bo_cache_init(&client->cache); 728 INIT_LIST_HEAD(&client->list); 729 __mutex_init(&client->lock, "host1x client lock", key); 730 client->usecount = 0; 731 } 732 EXPORT_SYMBOL(__host1x_client_init); 733 734 /** 735 * host1x_client_exit() - uninitialize a host1x client 736 * @client: host1x client 737 */ 738 void host1x_client_exit(struct host1x_client *client) 739 { 740 mutex_destroy(&client->lock); 741 } 742 EXPORT_SYMBOL(host1x_client_exit); 743 744 /** 745 * __host1x_client_register() - register a host1x client 746 * @client: host1x client 747 * 748 * Registers a host1x client with each host1x controller instance. Note that 749 * each client will only match their parent host1x controller and will only be 750 * associated with that instance. Once all clients have been registered with 751 * their parent host1x controller, the infrastructure will set up the logical 752 * device and call host1x_device_init(), which will in turn call each client's 753 * &host1x_client_ops.init implementation. 754 */ 755 int __host1x_client_register(struct host1x_client *client) 756 { 757 struct host1x *host1x; 758 int err; 759 760 mutex_lock(&devices_lock); 761 762 list_for_each_entry(host1x, &devices, list) { 763 err = host1x_add_client(host1x, client); 764 if (!err) { 765 mutex_unlock(&devices_lock); 766 return 0; 767 } 768 } 769 770 mutex_unlock(&devices_lock); 771 772 mutex_lock(&clients_lock); 773 list_add_tail(&client->list, &clients); 774 mutex_unlock(&clients_lock); 775 776 return 0; 777 } 778 EXPORT_SYMBOL(__host1x_client_register); 779 780 /** 781 * host1x_client_unregister() - unregister a host1x client 782 * @client: host1x client 783 * 784 * Removes a host1x client from its host1x controller instance. If a logical 785 * device has already been initialized, it will be torn down. 786 */ 787 void host1x_client_unregister(struct host1x_client *client) 788 { 789 struct host1x_client *c; 790 struct host1x *host1x; 791 int err; 792 793 mutex_lock(&devices_lock); 794 795 list_for_each_entry(host1x, &devices, list) { 796 err = host1x_del_client(host1x, client); 797 if (!err) { 798 mutex_unlock(&devices_lock); 799 return; 800 } 801 } 802 803 mutex_unlock(&devices_lock); 804 mutex_lock(&clients_lock); 805 806 list_for_each_entry(c, &clients, list) { 807 if (c == client) { 808 list_del_init(&c->list); 809 break; 810 } 811 } 812 813 mutex_unlock(&clients_lock); 814 815 host1x_bo_cache_destroy(&client->cache); 816 } 817 EXPORT_SYMBOL(host1x_client_unregister); 818 819 int host1x_client_suspend(struct host1x_client *client) 820 { 821 int err = 0; 822 823 mutex_lock(&client->lock); 824 825 if (client->usecount == 1) { 826 if (client->ops && client->ops->suspend) { 827 err = client->ops->suspend(client); 828 if (err < 0) 829 goto unlock; 830 } 831 } 832 833 client->usecount--; 834 dev_dbg(client->dev, "use count: %u\n", client->usecount); 835 836 if (client->parent) { 837 err = host1x_client_suspend(client->parent); 838 if (err < 0) 839 goto resume; 840 } 841 842 goto unlock; 843 844 resume: 845 if (client->usecount == 0) 846 if (client->ops && client->ops->resume) 847 client->ops->resume(client); 848 849 client->usecount++; 850 unlock: 851 mutex_unlock(&client->lock); 852 return err; 853 } 854 EXPORT_SYMBOL(host1x_client_suspend); 855 856 int host1x_client_resume(struct host1x_client *client) 857 { 858 int err = 0; 859 860 mutex_lock(&client->lock); 861 862 if (client->parent) { 863 err = host1x_client_resume(client->parent); 864 if (err < 0) 865 goto unlock; 866 } 867 868 if (client->usecount == 0) { 869 if (client->ops && client->ops->resume) { 870 err = client->ops->resume(client); 871 if (err < 0) 872 goto suspend; 873 } 874 } 875 876 client->usecount++; 877 dev_dbg(client->dev, "use count: %u\n", client->usecount); 878 879 goto unlock; 880 881 suspend: 882 if (client->parent) 883 host1x_client_suspend(client->parent); 884 unlock: 885 mutex_unlock(&client->lock); 886 return err; 887 } 888 EXPORT_SYMBOL(host1x_client_resume); 889 890 struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo, 891 enum dma_data_direction dir, 892 struct host1x_bo_cache *cache) 893 { 894 struct host1x_bo_mapping *mapping; 895 896 if (cache) { 897 mutex_lock(&cache->lock); 898 899 list_for_each_entry(mapping, &cache->mappings, entry) { 900 if (mapping->bo == bo && mapping->direction == dir) { 901 kref_get(&mapping->ref); 902 goto unlock; 903 } 904 } 905 } 906 907 mapping = bo->ops->pin(dev, bo, dir); 908 if (IS_ERR(mapping)) 909 goto unlock; 910 911 spin_lock(&mapping->bo->lock); 912 list_add_tail(&mapping->list, &bo->mappings); 913 spin_unlock(&mapping->bo->lock); 914 915 if (cache) { 916 INIT_LIST_HEAD(&mapping->entry); 917 mapping->cache = cache; 918 919 list_add_tail(&mapping->entry, &cache->mappings); 920 921 /* bump reference count to track the copy in the cache */ 922 kref_get(&mapping->ref); 923 } 924 925 unlock: 926 if (cache) 927 mutex_unlock(&cache->lock); 928 929 return mapping; 930 } 931 EXPORT_SYMBOL(host1x_bo_pin); 932 933 static void __host1x_bo_unpin(struct kref *ref) 934 { 935 struct host1x_bo_mapping *mapping = to_host1x_bo_mapping(ref); 936 937 /* 938 * When the last reference of the mapping goes away, make sure to remove the mapping from 939 * the cache. 940 */ 941 if (mapping->cache) 942 list_del(&mapping->entry); 943 944 spin_lock(&mapping->bo->lock); 945 list_del(&mapping->list); 946 spin_unlock(&mapping->bo->lock); 947 948 mapping->bo->ops->unpin(mapping); 949 } 950 951 void host1x_bo_unpin(struct host1x_bo_mapping *mapping) 952 { 953 struct host1x_bo_cache *cache = mapping->cache; 954 955 if (cache) 956 mutex_lock(&cache->lock); 957 958 kref_put(&mapping->ref, __host1x_bo_unpin); 959 960 if (cache) 961 mutex_unlock(&cache->lock); 962 } 963 EXPORT_SYMBOL(host1x_bo_unpin); 964