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/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 26 static DEFINE_MUTEX(clients_lock); 27 static LIST_HEAD(clients); 28 29 static DEFINE_MUTEX(drivers_lock); 30 static LIST_HEAD(drivers); 31 32 static DEFINE_MUTEX(devices_lock); 33 static LIST_HEAD(devices); 34 35 struct host1x_subdev { 36 struct host1x_client *client; 37 struct device_node *np; 38 struct list_head list; 39 }; 40 41 /** 42 * host1x_subdev_add() - add a new subdevice with an associated device node 43 * @device: host1x device to add the subdevice to 44 * @np: device node 45 */ 46 static 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 /* recursively add children */ 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 /* XXX cleanup? */ 72 of_node_put(child); 73 return err; 74 } 75 } 76 } 77 78 return 0; 79 } 80 81 /** 82 * host1x_subdev_del() - remove subdevice 83 * @subdev: subdevice to remove 84 */ 85 static 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 * host1x_device_parse_dt() - scan device tree and add matching subdevices 94 * @device: host1x logical device 95 * @driver: host1x driver 96 */ 97 static 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 117 static 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 * Move the subdevice to the list of active (registered) subdevices 125 * and associate it with a client. At the same time, associate the 126 * client with its parent device. 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 146 static void __host1x_subdev_unregister(struct host1x_device *device, 147 struct host1x_subdev *subdev) 148 { 149 struct host1x_client *client = subdev->client; 150 151 /* 152 * If all subdevices have been activated, we're about to remove the 153 * first active subdevice, so unload the driver first. 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 * Move the subdevice back to the list of idle subdevices and remove 164 * it from list of clients. 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 * XXX: Perhaps don't do this here, but rather explicitly remove it 172 * when the device is about to be deleted. 173 * 174 * This is somewhat complicated by the fact that this function is 175 * used to remove the subdevice when a client is unregistered but 176 * also when the composite device is about to be removed. 177 */ 178 list_del_init(&client->list); 179 mutex_unlock(&device->clients_lock); 180 } 181 182 static 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 * host1x_device_init() - initialize a host1x logical device 192 * @device: host1x logical device 193 * 194 * The driver for the host1x logical device can call this during execution of 195 * its &host1x_driver.probe implementation to initialize each of its clients. 196 * The client drivers access the subsystem specific driver data using the 197 * &host1x_client.parent field and driver data associated with it (usually by 198 * calling dev_get_drvdata()). 199 */ 200 int 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 mutex_unlock(&device->clients_lock); 215 return err; 216 } 217 } 218 } 219 220 mutex_unlock(&device->clients_lock); 221 222 return 0; 223 } 224 EXPORT_SYMBOL(host1x_device_init); 225 226 /** 227 * host1x_device_exit() - uninitialize host1x logical device 228 * @device: host1x logical device 229 * 230 * When the driver for a host1x logical device is unloaded, it can call this 231 * function to tear down each of its clients. Typically this is done after a 232 * subsystem-specific data structure is removed and the functionality can no 233 * longer be used. 234 */ 235 int host1x_device_exit(struct host1x_device *device) 236 { 237 struct host1x_client *client; 238 int err; 239 240 mutex_lock(&device->clients_lock); 241 242 list_for_each_entry_reverse(client, &device->clients, list) { 243 if (client->ops && client->ops->exit) { 244 err = client->ops->exit(client); 245 if (err < 0) { 246 dev_err(&device->dev, 247 "failed to cleanup %s: %d\n", 248 dev_name(client->dev), err); 249 mutex_unlock(&device->clients_lock); 250 return err; 251 } 252 } 253 } 254 255 mutex_unlock(&device->clients_lock); 256 257 return 0; 258 } 259 EXPORT_SYMBOL(host1x_device_exit); 260 261 static int host1x_add_client(struct host1x *host1x, 262 struct host1x_client *client) 263 { 264 struct host1x_device *device; 265 struct host1x_subdev *subdev; 266 267 mutex_lock(&host1x->devices_lock); 268 269 list_for_each_entry(device, &host1x->devices, list) { 270 list_for_each_entry(subdev, &device->subdevs, list) { 271 if (subdev->np == client->dev->of_node) { 272 host1x_subdev_register(device, subdev, client); 273 mutex_unlock(&host1x->devices_lock); 274 return 0; 275 } 276 } 277 } 278 279 mutex_unlock(&host1x->devices_lock); 280 return -ENODEV; 281 } 282 283 static int host1x_del_client(struct host1x *host1x, 284 struct host1x_client *client) 285 { 286 struct host1x_device *device, *dt; 287 struct host1x_subdev *subdev; 288 289 mutex_lock(&host1x->devices_lock); 290 291 list_for_each_entry_safe(device, dt, &host1x->devices, list) { 292 list_for_each_entry(subdev, &device->active, list) { 293 if (subdev->client == client) { 294 host1x_subdev_unregister(device, subdev); 295 mutex_unlock(&host1x->devices_lock); 296 return 0; 297 } 298 } 299 } 300 301 mutex_unlock(&host1x->devices_lock); 302 return -ENODEV; 303 } 304 305 static int host1x_device_match(struct device *dev, struct device_driver *drv) 306 { 307 return strcmp(dev_name(dev), drv->name) == 0; 308 } 309 310 static const struct dev_pm_ops host1x_device_pm_ops = { 311 .suspend = pm_generic_suspend, 312 .resume = pm_generic_resume, 313 .freeze = pm_generic_freeze, 314 .thaw = pm_generic_thaw, 315 .poweroff = pm_generic_poweroff, 316 .restore = pm_generic_restore, 317 }; 318 319 struct bus_type host1x_bus_type = { 320 .name = "host1x", 321 .match = host1x_device_match, 322 .pm = &host1x_device_pm_ops, 323 .force_dma = true, 324 }; 325 326 static void __host1x_device_del(struct host1x_device *device) 327 { 328 struct host1x_subdev *subdev, *sd; 329 struct host1x_client *client, *cl; 330 331 mutex_lock(&device->subdevs_lock); 332 333 /* unregister subdevices */ 334 list_for_each_entry_safe(subdev, sd, &device->active, list) { 335 /* 336 * host1x_subdev_unregister() will remove the client from 337 * any lists, so we'll need to manually add it back to the 338 * list of idle clients. 339 * 340 * XXX: Alternatively, perhaps don't remove the client from 341 * any lists in host1x_subdev_unregister() and instead do 342 * that explicitly from host1x_unregister_client()? 343 */ 344 client = subdev->client; 345 346 __host1x_subdev_unregister(device, subdev); 347 348 /* add the client to the list of idle clients */ 349 mutex_lock(&clients_lock); 350 list_add_tail(&client->list, &clients); 351 mutex_unlock(&clients_lock); 352 } 353 354 /* remove subdevices */ 355 list_for_each_entry_safe(subdev, sd, &device->subdevs, list) 356 host1x_subdev_del(subdev); 357 358 mutex_unlock(&device->subdevs_lock); 359 360 /* move clients to idle list */ 361 mutex_lock(&clients_lock); 362 mutex_lock(&device->clients_lock); 363 364 list_for_each_entry_safe(client, cl, &device->clients, list) 365 list_move_tail(&client->list, &clients); 366 367 mutex_unlock(&device->clients_lock); 368 mutex_unlock(&clients_lock); 369 370 /* finally remove the device */ 371 list_del_init(&device->list); 372 } 373 374 static void host1x_device_release(struct device *dev) 375 { 376 struct host1x_device *device = to_host1x_device(dev); 377 378 __host1x_device_del(device); 379 kfree(device); 380 } 381 382 static int host1x_device_add(struct host1x *host1x, 383 struct host1x_driver *driver) 384 { 385 struct host1x_client *client, *tmp; 386 struct host1x_subdev *subdev; 387 struct host1x_device *device; 388 int err; 389 390 device = kzalloc(sizeof(*device), GFP_KERNEL); 391 if (!device) 392 return -ENOMEM; 393 394 device_initialize(&device->dev); 395 396 mutex_init(&device->subdevs_lock); 397 INIT_LIST_HEAD(&device->subdevs); 398 INIT_LIST_HEAD(&device->active); 399 mutex_init(&device->clients_lock); 400 INIT_LIST_HEAD(&device->clients); 401 INIT_LIST_HEAD(&device->list); 402 device->driver = driver; 403 404 device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask; 405 device->dev.dma_mask = &device->dev.coherent_dma_mask; 406 dev_set_name(&device->dev, "%s", driver->driver.name); 407 of_dma_configure(&device->dev, host1x->dev->of_node); 408 device->dev.release = host1x_device_release; 409 device->dev.of_node = host1x->dev->of_node; 410 device->dev.bus = &host1x_bus_type; 411 device->dev.parent = host1x->dev; 412 413 err = host1x_device_parse_dt(device, driver); 414 if (err < 0) { 415 kfree(device); 416 return err; 417 } 418 419 list_add_tail(&device->list, &host1x->devices); 420 421 mutex_lock(&clients_lock); 422 423 list_for_each_entry_safe(client, tmp, &clients, list) { 424 list_for_each_entry(subdev, &device->subdevs, list) { 425 if (subdev->np == client->dev->of_node) { 426 host1x_subdev_register(device, subdev, client); 427 break; 428 } 429 } 430 } 431 432 mutex_unlock(&clients_lock); 433 434 return 0; 435 } 436 437 /* 438 * Removes a device by first unregistering any subdevices and then removing 439 * itself from the list of devices. 440 * 441 * This function must be called with the host1x->devices_lock held. 442 */ 443 static void host1x_device_del(struct host1x *host1x, 444 struct host1x_device *device) 445 { 446 if (device->registered) { 447 device->registered = false; 448 device_del(&device->dev); 449 } 450 451 put_device(&device->dev); 452 } 453 454 static void host1x_attach_driver(struct host1x *host1x, 455 struct host1x_driver *driver) 456 { 457 struct host1x_device *device; 458 int err; 459 460 mutex_lock(&host1x->devices_lock); 461 462 list_for_each_entry(device, &host1x->devices, list) { 463 if (device->driver == driver) { 464 mutex_unlock(&host1x->devices_lock); 465 return; 466 } 467 } 468 469 err = host1x_device_add(host1x, driver); 470 if (err < 0) 471 dev_err(host1x->dev, "failed to allocate device: %d\n", err); 472 473 mutex_unlock(&host1x->devices_lock); 474 } 475 476 static void host1x_detach_driver(struct host1x *host1x, 477 struct host1x_driver *driver) 478 { 479 struct host1x_device *device, *tmp; 480 481 mutex_lock(&host1x->devices_lock); 482 483 list_for_each_entry_safe(device, tmp, &host1x->devices, list) 484 if (device->driver == driver) 485 host1x_device_del(host1x, device); 486 487 mutex_unlock(&host1x->devices_lock); 488 } 489 490 /** 491 * host1x_register() - register a host1x controller 492 * @host1x: host1x controller 493 * 494 * The host1x controller driver uses this to register a host1x controller with 495 * the infrastructure. Note that all Tegra SoC generations have only ever come 496 * with a single host1x instance, so this function is somewhat academic. 497 */ 498 int host1x_register(struct host1x *host1x) 499 { 500 struct host1x_driver *driver; 501 502 mutex_lock(&devices_lock); 503 list_add_tail(&host1x->list, &devices); 504 mutex_unlock(&devices_lock); 505 506 mutex_lock(&drivers_lock); 507 508 list_for_each_entry(driver, &drivers, list) 509 host1x_attach_driver(host1x, driver); 510 511 mutex_unlock(&drivers_lock); 512 513 return 0; 514 } 515 516 /** 517 * host1x_unregister() - unregister a host1x controller 518 * @host1x: host1x controller 519 * 520 * The host1x controller driver uses this to remove a host1x controller from 521 * the infrastructure. 522 */ 523 int host1x_unregister(struct host1x *host1x) 524 { 525 struct host1x_driver *driver; 526 527 mutex_lock(&drivers_lock); 528 529 list_for_each_entry(driver, &drivers, list) 530 host1x_detach_driver(host1x, driver); 531 532 mutex_unlock(&drivers_lock); 533 534 mutex_lock(&devices_lock); 535 list_del_init(&host1x->list); 536 mutex_unlock(&devices_lock); 537 538 return 0; 539 } 540 541 static int host1x_device_probe(struct device *dev) 542 { 543 struct host1x_driver *driver = to_host1x_driver(dev->driver); 544 struct host1x_device *device = to_host1x_device(dev); 545 546 if (driver->probe) 547 return driver->probe(device); 548 549 return 0; 550 } 551 552 static int host1x_device_remove(struct device *dev) 553 { 554 struct host1x_driver *driver = to_host1x_driver(dev->driver); 555 struct host1x_device *device = to_host1x_device(dev); 556 557 if (driver->remove) 558 return driver->remove(device); 559 560 return 0; 561 } 562 563 static void host1x_device_shutdown(struct device *dev) 564 { 565 struct host1x_driver *driver = to_host1x_driver(dev->driver); 566 struct host1x_device *device = to_host1x_device(dev); 567 568 if (driver->shutdown) 569 driver->shutdown(device); 570 } 571 572 /** 573 * host1x_driver_register_full() - register a host1x driver 574 * @driver: host1x driver 575 * @owner: owner module 576 * 577 * Drivers for host1x logical devices call this function to register a driver 578 * with the infrastructure. Note that since these drive logical devices, the 579 * registration of the driver actually triggers tho logical device creation. 580 * A logical device will be created for each host1x instance. 581 */ 582 int host1x_driver_register_full(struct host1x_driver *driver, 583 struct module *owner) 584 { 585 struct host1x *host1x; 586 587 INIT_LIST_HEAD(&driver->list); 588 589 mutex_lock(&drivers_lock); 590 list_add_tail(&driver->list, &drivers); 591 mutex_unlock(&drivers_lock); 592 593 mutex_lock(&devices_lock); 594 595 list_for_each_entry(host1x, &devices, list) 596 host1x_attach_driver(host1x, driver); 597 598 mutex_unlock(&devices_lock); 599 600 driver->driver.bus = &host1x_bus_type; 601 driver->driver.owner = owner; 602 driver->driver.probe = host1x_device_probe; 603 driver->driver.remove = host1x_device_remove; 604 driver->driver.shutdown = host1x_device_shutdown; 605 606 return driver_register(&driver->driver); 607 } 608 EXPORT_SYMBOL(host1x_driver_register_full); 609 610 /** 611 * host1x_driver_unregister() - unregister a host1x driver 612 * @driver: host1x driver 613 * 614 * Unbinds the driver from each of the host1x logical devices that it is 615 * bound to, effectively removing the subsystem devices that they represent. 616 */ 617 void host1x_driver_unregister(struct host1x_driver *driver) 618 { 619 driver_unregister(&driver->driver); 620 621 mutex_lock(&drivers_lock); 622 list_del_init(&driver->list); 623 mutex_unlock(&drivers_lock); 624 } 625 EXPORT_SYMBOL(host1x_driver_unregister); 626 627 /** 628 * host1x_client_register() - register a host1x client 629 * @client: host1x client 630 * 631 * Registers a host1x client with each host1x controller instance. Note that 632 * each client will only match their parent host1x controller and will only be 633 * associated with that instance. Once all clients have been registered with 634 * their parent host1x controller, the infrastructure will set up the logical 635 * device and call host1x_device_init(), which will in turn call each client's 636 * &host1x_client_ops.init implementation. 637 */ 638 int host1x_client_register(struct host1x_client *client) 639 { 640 struct host1x *host1x; 641 int err; 642 643 mutex_lock(&devices_lock); 644 645 list_for_each_entry(host1x, &devices, list) { 646 err = host1x_add_client(host1x, client); 647 if (!err) { 648 mutex_unlock(&devices_lock); 649 return 0; 650 } 651 } 652 653 mutex_unlock(&devices_lock); 654 655 mutex_lock(&clients_lock); 656 list_add_tail(&client->list, &clients); 657 mutex_unlock(&clients_lock); 658 659 return 0; 660 } 661 EXPORT_SYMBOL(host1x_client_register); 662 663 /** 664 * host1x_client_unregister() - unregister a host1x client 665 * @client: host1x client 666 * 667 * Removes a host1x client from its host1x controller instance. If a logical 668 * device has already been initialized, it will be torn down. 669 */ 670 int host1x_client_unregister(struct host1x_client *client) 671 { 672 struct host1x_client *c; 673 struct host1x *host1x; 674 int err; 675 676 mutex_lock(&devices_lock); 677 678 list_for_each_entry(host1x, &devices, list) { 679 err = host1x_del_client(host1x, client); 680 if (!err) { 681 mutex_unlock(&devices_lock); 682 return 0; 683 } 684 } 685 686 mutex_unlock(&devices_lock); 687 mutex_lock(&clients_lock); 688 689 list_for_each_entry(c, &clients, list) { 690 if (c == client) { 691 list_del_init(&c->list); 692 break; 693 } 694 } 695 696 mutex_unlock(&clients_lock); 697 698 return 0; 699 } 700 EXPORT_SYMBOL(host1x_client_unregister); 701