bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. goto teardown;
  189. }
  190. }
  191. }
  192. mutex_unlock(&device->clients_lock);
  193. return 0;
  194. teardown:
  195. list_for_each_entry_continue_reverse(client, &device->clients, list)
  196. if (client->ops->exit)
  197. client->ops->exit(client);
  198. mutex_unlock(&device->clients_lock);
  199. return err;
  200. }
  201. EXPORT_SYMBOL(host1x_device_init);
  202. /**
  203. * host1x_device_exit() - uninitialize host1x logical device
  204. * @device: host1x logical device
  205. *
  206. * When the driver for a host1x logical device is unloaded, it can call this
  207. * function to tear down each of its clients. Typically this is done after a
  208. * subsystem-specific data structure is removed and the functionality can no
  209. * longer be used.
  210. */
  211. int host1x_device_exit(struct host1x_device *device)
  212. {
  213. struct host1x_client *client;
  214. int err;
  215. mutex_lock(&device->clients_lock);
  216. list_for_each_entry_reverse(client, &device->clients, list) {
  217. if (client->ops && client->ops->exit) {
  218. err = client->ops->exit(client);
  219. if (err < 0) {
  220. dev_err(&device->dev,
  221. "failed to cleanup %s: %d\n",
  222. dev_name(client->dev), err);
  223. mutex_unlock(&device->clients_lock);
  224. return err;
  225. }
  226. }
  227. }
  228. mutex_unlock(&device->clients_lock);
  229. return 0;
  230. }
  231. EXPORT_SYMBOL(host1x_device_exit);
  232. static int host1x_add_client(struct host1x *host1x,
  233. struct host1x_client *client)
  234. {
  235. struct host1x_device *device;
  236. struct host1x_subdev *subdev;
  237. mutex_lock(&host1x->devices_lock);
  238. list_for_each_entry(device, &host1x->devices, list) {
  239. list_for_each_entry(subdev, &device->subdevs, list) {
  240. if (subdev->np == client->dev->of_node) {
  241. host1x_subdev_register(device, subdev, client);
  242. mutex_unlock(&host1x->devices_lock);
  243. return 0;
  244. }
  245. }
  246. }
  247. mutex_unlock(&host1x->devices_lock);
  248. return -ENODEV;
  249. }
  250. static int host1x_del_client(struct host1x *host1x,
  251. struct host1x_client *client)
  252. {
  253. struct host1x_device *device, *dt;
  254. struct host1x_subdev *subdev;
  255. mutex_lock(&host1x->devices_lock);
  256. list_for_each_entry_safe(device, dt, &host1x->devices, list) {
  257. list_for_each_entry(subdev, &device->active, list) {
  258. if (subdev->client == client) {
  259. host1x_subdev_unregister(device, subdev);
  260. mutex_unlock(&host1x->devices_lock);
  261. return 0;
  262. }
  263. }
  264. }
  265. mutex_unlock(&host1x->devices_lock);
  266. return -ENODEV;
  267. }
  268. static int host1x_device_match(struct device *dev, struct device_driver *drv)
  269. {
  270. return strcmp(dev_name(dev), drv->name) == 0;
  271. }
  272. static int host1x_dma_configure(struct device *dev)
  273. {
  274. return of_dma_configure(dev, dev->of_node, true);
  275. }
  276. static const struct dev_pm_ops host1x_device_pm_ops = {
  277. .suspend = pm_generic_suspend,
  278. .resume = pm_generic_resume,
  279. .freeze = pm_generic_freeze,
  280. .thaw = pm_generic_thaw,
  281. .poweroff = pm_generic_poweroff,
  282. .restore = pm_generic_restore,
  283. };
  284. struct bus_type host1x_bus_type = {
  285. .name = "host1x",
  286. .match = host1x_device_match,
  287. .dma_configure = host1x_dma_configure,
  288. .pm = &host1x_device_pm_ops,
  289. };
  290. static void __host1x_device_del(struct host1x_device *device)
  291. {
  292. struct host1x_subdev *subdev, *sd;
  293. struct host1x_client *client, *cl;
  294. mutex_lock(&device->subdevs_lock);
  295. /* unregister subdevices */
  296. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  297. /*
  298. * host1x_subdev_unregister() will remove the client from
  299. * any lists, so we'll need to manually add it back to the
  300. * list of idle clients.
  301. *
  302. * XXX: Alternatively, perhaps don't remove the client from
  303. * any lists in host1x_subdev_unregister() and instead do
  304. * that explicitly from host1x_unregister_client()?
  305. */
  306. client = subdev->client;
  307. __host1x_subdev_unregister(device, subdev);
  308. /* add the client to the list of idle clients */
  309. mutex_lock(&clients_lock);
  310. list_add_tail(&client->list, &clients);
  311. mutex_unlock(&clients_lock);
  312. }
  313. /* remove subdevices */
  314. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  315. host1x_subdev_del(subdev);
  316. mutex_unlock(&device->subdevs_lock);
  317. /* move clients to idle list */
  318. mutex_lock(&clients_lock);
  319. mutex_lock(&device->clients_lock);
  320. list_for_each_entry_safe(client, cl, &device->clients, list)
  321. list_move_tail(&client->list, &clients);
  322. mutex_unlock(&device->clients_lock);
  323. mutex_unlock(&clients_lock);
  324. /* finally remove the device */
  325. list_del_init(&device->list);
  326. }
  327. static void host1x_device_release(struct device *dev)
  328. {
  329. struct host1x_device *device = to_host1x_device(dev);
  330. __host1x_device_del(device);
  331. kfree(device);
  332. }
  333. static int host1x_device_add(struct host1x *host1x,
  334. struct host1x_driver *driver)
  335. {
  336. struct host1x_client *client, *tmp;
  337. struct host1x_subdev *subdev;
  338. struct host1x_device *device;
  339. int err;
  340. device = kzalloc(sizeof(*device), GFP_KERNEL);
  341. if (!device)
  342. return -ENOMEM;
  343. device_initialize(&device->dev);
  344. mutex_init(&device->subdevs_lock);
  345. INIT_LIST_HEAD(&device->subdevs);
  346. INIT_LIST_HEAD(&device->active);
  347. mutex_init(&device->clients_lock);
  348. INIT_LIST_HEAD(&device->clients);
  349. INIT_LIST_HEAD(&device->list);
  350. device->driver = driver;
  351. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  352. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  353. dev_set_name(&device->dev, "%s", driver->driver.name);
  354. device->dev.release = host1x_device_release;
  355. device->dev.of_node = host1x->dev->of_node;
  356. device->dev.bus = &host1x_bus_type;
  357. device->dev.parent = host1x->dev;
  358. of_dma_configure(&device->dev, host1x->dev->of_node, true);
  359. err = host1x_device_parse_dt(device, driver);
  360. if (err < 0) {
  361. kfree(device);
  362. return err;
  363. }
  364. list_add_tail(&device->list, &host1x->devices);
  365. mutex_lock(&clients_lock);
  366. list_for_each_entry_safe(client, tmp, &clients, list) {
  367. list_for_each_entry(subdev, &device->subdevs, list) {
  368. if (subdev->np == client->dev->of_node) {
  369. host1x_subdev_register(device, subdev, client);
  370. break;
  371. }
  372. }
  373. }
  374. mutex_unlock(&clients_lock);
  375. return 0;
  376. }
  377. /*
  378. * Removes a device by first unregistering any subdevices and then removing
  379. * itself from the list of devices.
  380. *
  381. * This function must be called with the host1x->devices_lock held.
  382. */
  383. static void host1x_device_del(struct host1x *host1x,
  384. struct host1x_device *device)
  385. {
  386. if (device->registered) {
  387. device->registered = false;
  388. device_del(&device->dev);
  389. }
  390. put_device(&device->dev);
  391. }
  392. static void host1x_attach_driver(struct host1x *host1x,
  393. struct host1x_driver *driver)
  394. {
  395. struct host1x_device *device;
  396. int err;
  397. mutex_lock(&host1x->devices_lock);
  398. list_for_each_entry(device, &host1x->devices, list) {
  399. if (device->driver == driver) {
  400. mutex_unlock(&host1x->devices_lock);
  401. return;
  402. }
  403. }
  404. err = host1x_device_add(host1x, driver);
  405. if (err < 0)
  406. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  407. mutex_unlock(&host1x->devices_lock);
  408. }
  409. static void host1x_detach_driver(struct host1x *host1x,
  410. struct host1x_driver *driver)
  411. {
  412. struct host1x_device *device, *tmp;
  413. mutex_lock(&host1x->devices_lock);
  414. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  415. if (device->driver == driver)
  416. host1x_device_del(host1x, device);
  417. mutex_unlock(&host1x->devices_lock);
  418. }
  419. /**
  420. * host1x_register() - register a host1x controller
  421. * @host1x: host1x controller
  422. *
  423. * The host1x controller driver uses this to register a host1x controller with
  424. * the infrastructure. Note that all Tegra SoC generations have only ever come
  425. * with a single host1x instance, so this function is somewhat academic.
  426. */
  427. int host1x_register(struct host1x *host1x)
  428. {
  429. struct host1x_driver *driver;
  430. mutex_lock(&devices_lock);
  431. list_add_tail(&host1x->list, &devices);
  432. mutex_unlock(&devices_lock);
  433. mutex_lock(&drivers_lock);
  434. list_for_each_entry(driver, &drivers, list)
  435. host1x_attach_driver(host1x, driver);
  436. mutex_unlock(&drivers_lock);
  437. return 0;
  438. }
  439. /**
  440. * host1x_unregister() - unregister a host1x controller
  441. * @host1x: host1x controller
  442. *
  443. * The host1x controller driver uses this to remove a host1x controller from
  444. * the infrastructure.
  445. */
  446. int host1x_unregister(struct host1x *host1x)
  447. {
  448. struct host1x_driver *driver;
  449. mutex_lock(&drivers_lock);
  450. list_for_each_entry(driver, &drivers, list)
  451. host1x_detach_driver(host1x, driver);
  452. mutex_unlock(&drivers_lock);
  453. mutex_lock(&devices_lock);
  454. list_del_init(&host1x->list);
  455. mutex_unlock(&devices_lock);
  456. return 0;
  457. }
  458. static int host1x_device_probe(struct device *dev)
  459. {
  460. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  461. struct host1x_device *device = to_host1x_device(dev);
  462. if (driver->probe)
  463. return driver->probe(device);
  464. return 0;
  465. }
  466. static int host1x_device_remove(struct device *dev)
  467. {
  468. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  469. struct host1x_device *device = to_host1x_device(dev);
  470. if (driver->remove)
  471. return driver->remove(device);
  472. return 0;
  473. }
  474. static void host1x_device_shutdown(struct device *dev)
  475. {
  476. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  477. struct host1x_device *device = to_host1x_device(dev);
  478. if (driver->shutdown)
  479. driver->shutdown(device);
  480. }
  481. /**
  482. * host1x_driver_register_full() - register a host1x driver
  483. * @driver: host1x driver
  484. * @owner: owner module
  485. *
  486. * Drivers for host1x logical devices call this function to register a driver
  487. * with the infrastructure. Note that since these drive logical devices, the
  488. * registration of the driver actually triggers tho logical device creation.
  489. * A logical device will be created for each host1x instance.
  490. */
  491. int host1x_driver_register_full(struct host1x_driver *driver,
  492. struct module *owner)
  493. {
  494. struct host1x *host1x;
  495. INIT_LIST_HEAD(&driver->list);
  496. mutex_lock(&drivers_lock);
  497. list_add_tail(&driver->list, &drivers);
  498. mutex_unlock(&drivers_lock);
  499. mutex_lock(&devices_lock);
  500. list_for_each_entry(host1x, &devices, list)
  501. host1x_attach_driver(host1x, driver);
  502. mutex_unlock(&devices_lock);
  503. driver->driver.bus = &host1x_bus_type;
  504. driver->driver.owner = owner;
  505. driver->driver.probe = host1x_device_probe;
  506. driver->driver.remove = host1x_device_remove;
  507. driver->driver.shutdown = host1x_device_shutdown;
  508. return driver_register(&driver->driver);
  509. }
  510. EXPORT_SYMBOL(host1x_driver_register_full);
  511. /**
  512. * host1x_driver_unregister() - unregister a host1x driver
  513. * @driver: host1x driver
  514. *
  515. * Unbinds the driver from each of the host1x logical devices that it is
  516. * bound to, effectively removing the subsystem devices that they represent.
  517. */
  518. void host1x_driver_unregister(struct host1x_driver *driver)
  519. {
  520. driver_unregister(&driver->driver);
  521. mutex_lock(&drivers_lock);
  522. list_del_init(&driver->list);
  523. mutex_unlock(&drivers_lock);
  524. }
  525. EXPORT_SYMBOL(host1x_driver_unregister);
  526. /**
  527. * host1x_client_register() - register a host1x client
  528. * @client: host1x client
  529. *
  530. * Registers a host1x client with each host1x controller instance. Note that
  531. * each client will only match their parent host1x controller and will only be
  532. * associated with that instance. Once all clients have been registered with
  533. * their parent host1x controller, the infrastructure will set up the logical
  534. * device and call host1x_device_init(), which will in turn call each client's
  535. * &host1x_client_ops.init implementation.
  536. */
  537. int host1x_client_register(struct host1x_client *client)
  538. {
  539. struct host1x *host1x;
  540. int err;
  541. mutex_lock(&devices_lock);
  542. list_for_each_entry(host1x, &devices, list) {
  543. err = host1x_add_client(host1x, client);
  544. if (!err) {
  545. mutex_unlock(&devices_lock);
  546. return 0;
  547. }
  548. }
  549. mutex_unlock(&devices_lock);
  550. mutex_lock(&clients_lock);
  551. list_add_tail(&client->list, &clients);
  552. mutex_unlock(&clients_lock);
  553. return 0;
  554. }
  555. EXPORT_SYMBOL(host1x_client_register);
  556. /**
  557. * host1x_client_unregister() - unregister a host1x client
  558. * @client: host1x client
  559. *
  560. * Removes a host1x client from its host1x controller instance. If a logical
  561. * device has already been initialized, it will be torn down.
  562. */
  563. int host1x_client_unregister(struct host1x_client *client)
  564. {
  565. struct host1x_client *c;
  566. struct host1x *host1x;
  567. int err;
  568. mutex_lock(&devices_lock);
  569. list_for_each_entry(host1x, &devices, list) {
  570. err = host1x_del_client(host1x, client);
  571. if (!err) {
  572. mutex_unlock(&devices_lock);
  573. return 0;
  574. }
  575. }
  576. mutex_unlock(&devices_lock);
  577. mutex_lock(&clients_lock);
  578. list_for_each_entry(c, &clients, list) {
  579. if (c == client) {
  580. list_del_init(&c->list);
  581. break;
  582. }
  583. }
  584. mutex_unlock(&clients_lock);
  585. return 0;
  586. }
  587. EXPORT_SYMBOL(host1x_client_unregister);