bus.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #include <linux/host1x.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. #include <linux/of_device.h>
  21. #include "bus.h"
  22. #include "dev.h"
  23. static DEFINE_MUTEX(clients_lock);
  24. static LIST_HEAD(clients);
  25. static DEFINE_MUTEX(drivers_lock);
  26. static LIST_HEAD(drivers);
  27. static DEFINE_MUTEX(devices_lock);
  28. static LIST_HEAD(devices);
  29. struct host1x_subdev {
  30. struct host1x_client *client;
  31. struct device_node *np;
  32. struct list_head list;
  33. };
  34. /**
  35. * host1x_subdev_add() - add a new subdevice with an associated device node
  36. * @device: host1x device to add the subdevice to
  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. struct device_node *child;
  45. int err;
  46. subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
  47. if (!subdev)
  48. return -ENOMEM;
  49. INIT_LIST_HEAD(&subdev->list);
  50. subdev->np = of_node_get(np);
  51. mutex_lock(&device->subdevs_lock);
  52. list_add_tail(&subdev->list, &device->subdevs);
  53. mutex_unlock(&device->subdevs_lock);
  54. /* recursively add children */
  55. for_each_child_of_node(np, child) {
  56. if (of_match_node(driver->subdevs, child) &&
  57. of_device_is_available(child)) {
  58. err = host1x_subdev_add(device, driver, child);
  59. if (err < 0) {
  60. /* XXX cleanup? */
  61. of_node_put(child);
  62. return err;
  63. }
  64. }
  65. }
  66. return 0;
  67. }
  68. /**
  69. * host1x_subdev_del() - remove subdevice
  70. * @subdev: subdevice to remove
  71. */
  72. static void host1x_subdev_del(struct host1x_subdev *subdev)
  73. {
  74. list_del(&subdev->list);
  75. of_node_put(subdev->np);
  76. kfree(subdev);
  77. }
  78. /**
  79. * host1x_device_parse_dt() - scan device tree and add matching subdevices
  80. * @device: host1x logical device
  81. * @driver: host1x driver
  82. */
  83. static int host1x_device_parse_dt(struct host1x_device *device,
  84. struct host1x_driver *driver)
  85. {
  86. struct device_node *np;
  87. int err;
  88. for_each_child_of_node(device->dev.parent->of_node, np) {
  89. if (of_match_node(driver->subdevs, np) &&
  90. of_device_is_available(np)) {
  91. err = host1x_subdev_add(device, driver, np);
  92. if (err < 0) {
  93. of_node_put(np);
  94. return err;
  95. }
  96. }
  97. }
  98. return 0;
  99. }
  100. static void host1x_subdev_register(struct host1x_device *device,
  101. struct host1x_subdev *subdev,
  102. struct host1x_client *client)
  103. {
  104. int err;
  105. /*
  106. * Move the subdevice to the list of active (registered) subdevices
  107. * and associate it with a client. At the same time, associate the
  108. * client with its parent device.
  109. */
  110. mutex_lock(&device->subdevs_lock);
  111. mutex_lock(&device->clients_lock);
  112. list_move_tail(&client->list, &device->clients);
  113. list_move_tail(&subdev->list, &device->active);
  114. client->parent = &device->dev;
  115. subdev->client = client;
  116. mutex_unlock(&device->clients_lock);
  117. mutex_unlock(&device->subdevs_lock);
  118. if (list_empty(&device->subdevs)) {
  119. err = device_add(&device->dev);
  120. if (err < 0)
  121. dev_err(&device->dev, "failed to add: %d\n", err);
  122. else
  123. device->registered = true;
  124. }
  125. }
  126. static void __host1x_subdev_unregister(struct host1x_device *device,
  127. struct host1x_subdev *subdev)
  128. {
  129. struct host1x_client *client = subdev->client;
  130. /*
  131. * If all subdevices have been activated, we're about to remove the
  132. * first active subdevice, so unload the driver first.
  133. */
  134. if (list_empty(&device->subdevs)) {
  135. if (device->registered) {
  136. device->registered = false;
  137. device_del(&device->dev);
  138. }
  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. static void host1x_subdev_unregister(struct host1x_device *device,
  160. struct host1x_subdev *subdev)
  161. {
  162. mutex_lock(&device->subdevs_lock);
  163. __host1x_subdev_unregister(device, subdev);
  164. mutex_unlock(&device->subdevs_lock);
  165. }
  166. /**
  167. * host1x_device_init() - initialize a host1x logical device
  168. * @device: host1x logical device
  169. *
  170. * The driver for the host1x logical device can call this during execution of
  171. * its &host1x_driver.probe implementation to initialize each of its clients.
  172. * The client drivers access the subsystem specific driver data using the
  173. * &host1x_client.parent field and driver data associated with it (usually by
  174. * calling dev_get_drvdata()).
  175. */
  176. int host1x_device_init(struct host1x_device *device)
  177. {
  178. struct host1x_client *client;
  179. int err;
  180. mutex_lock(&device->clients_lock);
  181. list_for_each_entry(client, &device->clients, list) {
  182. if (client->ops && client->ops->init) {
  183. err = client->ops->init(client);
  184. if (err < 0) {
  185. dev_err(&device->dev,
  186. "failed to initialize %s: %d\n",
  187. dev_name(client->dev), err);
  188. mutex_unlock(&device->clients_lock);
  189. return err;
  190. }
  191. }
  192. }
  193. mutex_unlock(&device->clients_lock);
  194. return 0;
  195. }
  196. EXPORT_SYMBOL(host1x_device_init);
  197. /**
  198. * host1x_device_exit() - uninitialize host1x logical device
  199. * @device: host1x logical device
  200. *
  201. * When the driver for a host1x logical device is unloaded, it can call this
  202. * function to tear down each of its clients. Typically this is done after a
  203. * subsystem-specific data structure is removed and the functionality can no
  204. * longer be used.
  205. */
  206. int host1x_device_exit(struct host1x_device *device)
  207. {
  208. struct host1x_client *client;
  209. int err;
  210. mutex_lock(&device->clients_lock);
  211. list_for_each_entry_reverse(client, &device->clients, list) {
  212. if (client->ops && client->ops->exit) {
  213. err = client->ops->exit(client);
  214. if (err < 0) {
  215. dev_err(&device->dev,
  216. "failed to cleanup %s: %d\n",
  217. dev_name(client->dev), err);
  218. mutex_unlock(&device->clients_lock);
  219. return err;
  220. }
  221. }
  222. }
  223. mutex_unlock(&device->clients_lock);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(host1x_device_exit);
  227. static int host1x_add_client(struct host1x *host1x,
  228. struct host1x_client *client)
  229. {
  230. struct host1x_device *device;
  231. struct host1x_subdev *subdev;
  232. mutex_lock(&host1x->devices_lock);
  233. list_for_each_entry(device, &host1x->devices, list) {
  234. list_for_each_entry(subdev, &device->subdevs, list) {
  235. if (subdev->np == client->dev->of_node) {
  236. host1x_subdev_register(device, subdev, client);
  237. mutex_unlock(&host1x->devices_lock);
  238. return 0;
  239. }
  240. }
  241. }
  242. mutex_unlock(&host1x->devices_lock);
  243. return -ENODEV;
  244. }
  245. static int host1x_del_client(struct host1x *host1x,
  246. struct host1x_client *client)
  247. {
  248. struct host1x_device *device, *dt;
  249. struct host1x_subdev *subdev;
  250. mutex_lock(&host1x->devices_lock);
  251. list_for_each_entry_safe(device, dt, &host1x->devices, list) {
  252. list_for_each_entry(subdev, &device->active, list) {
  253. if (subdev->client == client) {
  254. host1x_subdev_unregister(device, subdev);
  255. mutex_unlock(&host1x->devices_lock);
  256. return 0;
  257. }
  258. }
  259. }
  260. mutex_unlock(&host1x->devices_lock);
  261. return -ENODEV;
  262. }
  263. static int host1x_device_match(struct device *dev, struct device_driver *drv)
  264. {
  265. return strcmp(dev_name(dev), drv->name) == 0;
  266. }
  267. static const struct dev_pm_ops host1x_device_pm_ops = {
  268. .suspend = pm_generic_suspend,
  269. .resume = pm_generic_resume,
  270. .freeze = pm_generic_freeze,
  271. .thaw = pm_generic_thaw,
  272. .poweroff = pm_generic_poweroff,
  273. .restore = pm_generic_restore,
  274. };
  275. struct bus_type host1x_bus_type = {
  276. .name = "host1x",
  277. .match = host1x_device_match,
  278. .pm = &host1x_device_pm_ops,
  279. };
  280. static void __host1x_device_del(struct host1x_device *device)
  281. {
  282. struct host1x_subdev *subdev, *sd;
  283. struct host1x_client *client, *cl;
  284. mutex_lock(&device->subdevs_lock);
  285. /* unregister subdevices */
  286. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  287. /*
  288. * host1x_subdev_unregister() will remove the client from
  289. * any lists, so we'll need to manually add it back to the
  290. * list of idle clients.
  291. *
  292. * XXX: Alternatively, perhaps don't remove the client from
  293. * any lists in host1x_subdev_unregister() and instead do
  294. * that explicitly from host1x_unregister_client()?
  295. */
  296. client = subdev->client;
  297. __host1x_subdev_unregister(device, subdev);
  298. /* add the client to the list of idle clients */
  299. mutex_lock(&clients_lock);
  300. list_add_tail(&client->list, &clients);
  301. mutex_unlock(&clients_lock);
  302. }
  303. /* remove subdevices */
  304. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  305. host1x_subdev_del(subdev);
  306. mutex_unlock(&device->subdevs_lock);
  307. /* move clients to idle list */
  308. mutex_lock(&clients_lock);
  309. mutex_lock(&device->clients_lock);
  310. list_for_each_entry_safe(client, cl, &device->clients, list)
  311. list_move_tail(&client->list, &clients);
  312. mutex_unlock(&device->clients_lock);
  313. mutex_unlock(&clients_lock);
  314. /* finally remove the device */
  315. list_del_init(&device->list);
  316. }
  317. static void host1x_device_release(struct device *dev)
  318. {
  319. struct host1x_device *device = to_host1x_device(dev);
  320. __host1x_device_del(device);
  321. kfree(device);
  322. }
  323. static int host1x_device_add(struct host1x *host1x,
  324. struct host1x_driver *driver)
  325. {
  326. struct host1x_client *client, *tmp;
  327. struct host1x_subdev *subdev;
  328. struct host1x_device *device;
  329. int err;
  330. device = kzalloc(sizeof(*device), GFP_KERNEL);
  331. if (!device)
  332. return -ENOMEM;
  333. device_initialize(&device->dev);
  334. mutex_init(&device->subdevs_lock);
  335. INIT_LIST_HEAD(&device->subdevs);
  336. INIT_LIST_HEAD(&device->active);
  337. mutex_init(&device->clients_lock);
  338. INIT_LIST_HEAD(&device->clients);
  339. INIT_LIST_HEAD(&device->list);
  340. device->driver = driver;
  341. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  342. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  343. dev_set_name(&device->dev, "%s", driver->driver.name);
  344. of_dma_configure(&device->dev, host1x->dev->of_node);
  345. device->dev.release = host1x_device_release;
  346. device->dev.of_node = host1x->dev->of_node;
  347. device->dev.bus = &host1x_bus_type;
  348. device->dev.parent = host1x->dev;
  349. err = host1x_device_parse_dt(device, driver);
  350. if (err < 0) {
  351. kfree(device);
  352. return err;
  353. }
  354. list_add_tail(&device->list, &host1x->devices);
  355. mutex_lock(&clients_lock);
  356. list_for_each_entry_safe(client, tmp, &clients, list) {
  357. list_for_each_entry(subdev, &device->subdevs, list) {
  358. if (subdev->np == client->dev->of_node) {
  359. host1x_subdev_register(device, subdev, client);
  360. break;
  361. }
  362. }
  363. }
  364. mutex_unlock(&clients_lock);
  365. return 0;
  366. }
  367. /*
  368. * Removes a device by first unregistering any subdevices and then removing
  369. * itself from the list of devices.
  370. *
  371. * This function must be called with the host1x->devices_lock held.
  372. */
  373. static void host1x_device_del(struct host1x *host1x,
  374. struct host1x_device *device)
  375. {
  376. if (device->registered) {
  377. device->registered = false;
  378. device_del(&device->dev);
  379. }
  380. put_device(&device->dev);
  381. }
  382. static void host1x_attach_driver(struct host1x *host1x,
  383. struct host1x_driver *driver)
  384. {
  385. struct host1x_device *device;
  386. int err;
  387. mutex_lock(&host1x->devices_lock);
  388. list_for_each_entry(device, &host1x->devices, list) {
  389. if (device->driver == driver) {
  390. mutex_unlock(&host1x->devices_lock);
  391. return;
  392. }
  393. }
  394. err = host1x_device_add(host1x, driver);
  395. if (err < 0)
  396. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  397. mutex_unlock(&host1x->devices_lock);
  398. }
  399. static void host1x_detach_driver(struct host1x *host1x,
  400. struct host1x_driver *driver)
  401. {
  402. struct host1x_device *device, *tmp;
  403. mutex_lock(&host1x->devices_lock);
  404. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  405. if (device->driver == driver)
  406. host1x_device_del(host1x, device);
  407. mutex_unlock(&host1x->devices_lock);
  408. }
  409. /**
  410. * host1x_register() - register a host1x controller
  411. * @host1x: host1x controller
  412. *
  413. * The host1x controller driver uses this to register a host1x controller with
  414. * the infrastructure. Note that all Tegra SoC generations have only ever come
  415. * with a single host1x instance, so this function is somewhat academic.
  416. */
  417. int host1x_register(struct host1x *host1x)
  418. {
  419. struct host1x_driver *driver;
  420. mutex_lock(&devices_lock);
  421. list_add_tail(&host1x->list, &devices);
  422. mutex_unlock(&devices_lock);
  423. mutex_lock(&drivers_lock);
  424. list_for_each_entry(driver, &drivers, list)
  425. host1x_attach_driver(host1x, driver);
  426. mutex_unlock(&drivers_lock);
  427. return 0;
  428. }
  429. /**
  430. * host1x_unregister() - unregister a host1x controller
  431. * @host1x: host1x controller
  432. *
  433. * The host1x controller driver uses this to remove a host1x controller from
  434. * the infrastructure.
  435. */
  436. int host1x_unregister(struct host1x *host1x)
  437. {
  438. struct host1x_driver *driver;
  439. mutex_lock(&drivers_lock);
  440. list_for_each_entry(driver, &drivers, list)
  441. host1x_detach_driver(host1x, driver);
  442. mutex_unlock(&drivers_lock);
  443. mutex_lock(&devices_lock);
  444. list_del_init(&host1x->list);
  445. mutex_unlock(&devices_lock);
  446. return 0;
  447. }
  448. static int host1x_device_probe(struct device *dev)
  449. {
  450. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  451. struct host1x_device *device = to_host1x_device(dev);
  452. if (driver->probe)
  453. return driver->probe(device);
  454. return 0;
  455. }
  456. static int host1x_device_remove(struct device *dev)
  457. {
  458. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  459. struct host1x_device *device = to_host1x_device(dev);
  460. if (driver->remove)
  461. return driver->remove(device);
  462. return 0;
  463. }
  464. static void host1x_device_shutdown(struct device *dev)
  465. {
  466. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  467. struct host1x_device *device = to_host1x_device(dev);
  468. if (driver->shutdown)
  469. driver->shutdown(device);
  470. }
  471. /**
  472. * host1x_driver_register_full() - register a host1x driver
  473. * @driver: host1x driver
  474. * @owner: owner module
  475. *
  476. * Drivers for host1x logical devices call this function to register a driver
  477. * with the infrastructure. Note that since these drive logical devices, the
  478. * registration of the driver actually triggers tho logical device creation.
  479. * A logical device will be created for each host1x instance.
  480. */
  481. int host1x_driver_register_full(struct host1x_driver *driver,
  482. struct module *owner)
  483. {
  484. struct host1x *host1x;
  485. INIT_LIST_HEAD(&driver->list);
  486. mutex_lock(&drivers_lock);
  487. list_add_tail(&driver->list, &drivers);
  488. mutex_unlock(&drivers_lock);
  489. mutex_lock(&devices_lock);
  490. list_for_each_entry(host1x, &devices, list)
  491. host1x_attach_driver(host1x, driver);
  492. mutex_unlock(&devices_lock);
  493. driver->driver.bus = &host1x_bus_type;
  494. driver->driver.owner = owner;
  495. driver->driver.probe = host1x_device_probe;
  496. driver->driver.remove = host1x_device_remove;
  497. driver->driver.shutdown = host1x_device_shutdown;
  498. return driver_register(&driver->driver);
  499. }
  500. EXPORT_SYMBOL(host1x_driver_register_full);
  501. /**
  502. * host1x_driver_unregister() - unregister a host1x driver
  503. * @driver: host1x driver
  504. *
  505. * Unbinds the driver from each of the host1x logical devices that it is
  506. * bound to, effectively removing the subsystem devices that they represent.
  507. */
  508. void host1x_driver_unregister(struct host1x_driver *driver)
  509. {
  510. driver_unregister(&driver->driver);
  511. mutex_lock(&drivers_lock);
  512. list_del_init(&driver->list);
  513. mutex_unlock(&drivers_lock);
  514. }
  515. EXPORT_SYMBOL(host1x_driver_unregister);
  516. /**
  517. * host1x_client_register() - register a host1x client
  518. * @client: host1x client
  519. *
  520. * Registers a host1x client with each host1x controller instance. Note that
  521. * each client will only match their parent host1x controller and will only be
  522. * associated with that instance. Once all clients have been registered with
  523. * their parent host1x controller, the infrastructure will set up the logical
  524. * device and call host1x_device_init(), which will in turn call each client's
  525. * &host1x_client_ops.init implementation.
  526. */
  527. int host1x_client_register(struct host1x_client *client)
  528. {
  529. struct host1x *host1x;
  530. int err;
  531. mutex_lock(&devices_lock);
  532. list_for_each_entry(host1x, &devices, list) {
  533. err = host1x_add_client(host1x, client);
  534. if (!err) {
  535. mutex_unlock(&devices_lock);
  536. return 0;
  537. }
  538. }
  539. mutex_unlock(&devices_lock);
  540. mutex_lock(&clients_lock);
  541. list_add_tail(&client->list, &clients);
  542. mutex_unlock(&clients_lock);
  543. return 0;
  544. }
  545. EXPORT_SYMBOL(host1x_client_register);
  546. /**
  547. * host1x_client_unregister() - unregister a host1x client
  548. * @client: host1x client
  549. *
  550. * Removes a host1x client from its host1x controller instance. If a logical
  551. * device has already been initialized, it will be torn down.
  552. */
  553. int host1x_client_unregister(struct host1x_client *client)
  554. {
  555. struct host1x_client *c;
  556. struct host1x *host1x;
  557. int err;
  558. mutex_lock(&devices_lock);
  559. list_for_each_entry(host1x, &devices, list) {
  560. err = host1x_del_client(host1x, client);
  561. if (!err) {
  562. mutex_unlock(&devices_lock);
  563. return 0;
  564. }
  565. }
  566. mutex_unlock(&devices_lock);
  567. mutex_lock(&clients_lock);
  568. list_for_each_entry(c, &clients, list) {
  569. if (c == client) {
  570. list_del_init(&c->list);
  571. break;
  572. }
  573. }
  574. mutex_unlock(&clients_lock);
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(host1x_client_unregister);