bus.c 18 KB

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