bus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 "bus.h"
  21. #include "dev.h"
  22. static DEFINE_MUTEX(clients_lock);
  23. static LIST_HEAD(clients);
  24. static DEFINE_MUTEX(drivers_lock);
  25. static LIST_HEAD(drivers);
  26. static DEFINE_MUTEX(devices_lock);
  27. static LIST_HEAD(devices);
  28. struct host1x_subdev {
  29. struct host1x_client *client;
  30. struct device_node *np;
  31. struct list_head list;
  32. };
  33. /**
  34. * host1x_subdev_add() - add a new subdevice with an associated device node
  35. */
  36. static int host1x_subdev_add(struct host1x_device *device,
  37. struct device_node *np)
  38. {
  39. struct host1x_subdev *subdev;
  40. subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
  41. if (!subdev)
  42. return -ENOMEM;
  43. INIT_LIST_HEAD(&subdev->list);
  44. subdev->np = of_node_get(np);
  45. mutex_lock(&device->subdevs_lock);
  46. list_add_tail(&subdev->list, &device->subdevs);
  47. mutex_unlock(&device->subdevs_lock);
  48. return 0;
  49. }
  50. /**
  51. * host1x_subdev_del() - remove subdevice
  52. */
  53. static void host1x_subdev_del(struct host1x_subdev *subdev)
  54. {
  55. list_del(&subdev->list);
  56. of_node_put(subdev->np);
  57. kfree(subdev);
  58. }
  59. /**
  60. * host1x_device_parse_dt() - scan device tree and add matching subdevices
  61. */
  62. static int host1x_device_parse_dt(struct host1x_device *device)
  63. {
  64. struct device_node *np;
  65. int err;
  66. for_each_child_of_node(device->dev.parent->of_node, np) {
  67. if (of_match_node(device->driver->subdevs, np) &&
  68. of_device_is_available(np)) {
  69. err = host1x_subdev_add(device, np);
  70. if (err < 0)
  71. return err;
  72. }
  73. }
  74. return 0;
  75. }
  76. static void host1x_subdev_register(struct host1x_device *device,
  77. struct host1x_subdev *subdev,
  78. struct host1x_client *client)
  79. {
  80. int err;
  81. /*
  82. * Move the subdevice to the list of active (registered) subdevices
  83. * and associate it with a client. At the same time, associate the
  84. * client with its parent device.
  85. */
  86. mutex_lock(&device->subdevs_lock);
  87. mutex_lock(&device->clients_lock);
  88. list_move_tail(&client->list, &device->clients);
  89. list_move_tail(&subdev->list, &device->active);
  90. client->parent = &device->dev;
  91. subdev->client = client;
  92. mutex_unlock(&device->clients_lock);
  93. mutex_unlock(&device->subdevs_lock);
  94. /*
  95. * When all subdevices have been registered, the composite device is
  96. * ready to be probed.
  97. */
  98. if (list_empty(&device->subdevs)) {
  99. err = device->driver->probe(device);
  100. if (err < 0)
  101. dev_err(&device->dev, "probe failed: %d\n", err);
  102. }
  103. }
  104. static void __host1x_subdev_unregister(struct host1x_device *device,
  105. struct host1x_subdev *subdev)
  106. {
  107. struct host1x_client *client = subdev->client;
  108. int err;
  109. /*
  110. * If all subdevices have been activated, we're about to remove the
  111. * first active subdevice, so unload the driver first.
  112. */
  113. if (list_empty(&device->subdevs)) {
  114. err = device->driver->remove(device);
  115. if (err < 0)
  116. dev_err(&device->dev, "remove failed: %d\n", err);
  117. }
  118. /*
  119. * Move the subdevice back to the list of idle subdevices and remove
  120. * it from list of clients.
  121. */
  122. mutex_lock(&device->clients_lock);
  123. subdev->client = NULL;
  124. client->parent = NULL;
  125. list_move_tail(&subdev->list, &device->subdevs);
  126. /*
  127. * XXX: Perhaps don't do this here, but rather explicitly remove it
  128. * when the device is about to be deleted.
  129. *
  130. * This is somewhat complicated by the fact that this function is
  131. * used to remove the subdevice when a client is unregistered but
  132. * also when the composite device is about to be removed.
  133. */
  134. list_del_init(&client->list);
  135. mutex_unlock(&device->clients_lock);
  136. }
  137. static void host1x_subdev_unregister(struct host1x_device *device,
  138. struct host1x_subdev *subdev)
  139. {
  140. mutex_lock(&device->subdevs_lock);
  141. __host1x_subdev_unregister(device, subdev);
  142. mutex_unlock(&device->subdevs_lock);
  143. }
  144. int host1x_device_init(struct host1x_device *device)
  145. {
  146. struct host1x_client *client;
  147. int err;
  148. mutex_lock(&device->clients_lock);
  149. list_for_each_entry(client, &device->clients, list) {
  150. if (client->ops && client->ops->init) {
  151. err = client->ops->init(client);
  152. if (err < 0) {
  153. dev_err(&device->dev,
  154. "failed to initialize %s: %d\n",
  155. dev_name(client->dev), err);
  156. mutex_unlock(&device->clients_lock);
  157. return err;
  158. }
  159. }
  160. }
  161. mutex_unlock(&device->clients_lock);
  162. return 0;
  163. }
  164. EXPORT_SYMBOL(host1x_device_init);
  165. int host1x_device_exit(struct host1x_device *device)
  166. {
  167. struct host1x_client *client;
  168. int err;
  169. mutex_lock(&device->clients_lock);
  170. list_for_each_entry_reverse(client, &device->clients, list) {
  171. if (client->ops && client->ops->exit) {
  172. err = client->ops->exit(client);
  173. if (err < 0) {
  174. dev_err(&device->dev,
  175. "failed to cleanup %s: %d\n",
  176. dev_name(client->dev), err);
  177. mutex_unlock(&device->clients_lock);
  178. return err;
  179. }
  180. }
  181. }
  182. mutex_unlock(&device->clients_lock);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL(host1x_device_exit);
  186. static int host1x_add_client(struct host1x *host1x,
  187. struct host1x_client *client)
  188. {
  189. struct host1x_device *device;
  190. struct host1x_subdev *subdev;
  191. mutex_lock(&host1x->devices_lock);
  192. list_for_each_entry(device, &host1x->devices, list) {
  193. list_for_each_entry(subdev, &device->subdevs, list) {
  194. if (subdev->np == client->dev->of_node) {
  195. host1x_subdev_register(device, subdev, client);
  196. mutex_unlock(&host1x->devices_lock);
  197. return 0;
  198. }
  199. }
  200. }
  201. mutex_unlock(&host1x->devices_lock);
  202. return -ENODEV;
  203. }
  204. static int host1x_del_client(struct host1x *host1x,
  205. struct host1x_client *client)
  206. {
  207. struct host1x_device *device, *dt;
  208. struct host1x_subdev *subdev;
  209. mutex_lock(&host1x->devices_lock);
  210. list_for_each_entry_safe(device, dt, &host1x->devices, list) {
  211. list_for_each_entry(subdev, &device->active, list) {
  212. if (subdev->client == client) {
  213. host1x_subdev_unregister(device, subdev);
  214. mutex_unlock(&host1x->devices_lock);
  215. return 0;
  216. }
  217. }
  218. }
  219. mutex_unlock(&host1x->devices_lock);
  220. return -ENODEV;
  221. }
  222. static struct bus_type host1x_bus_type = {
  223. .name = "host1x",
  224. };
  225. int host1x_bus_init(void)
  226. {
  227. return bus_register(&host1x_bus_type);
  228. }
  229. void host1x_bus_exit(void)
  230. {
  231. bus_unregister(&host1x_bus_type);
  232. }
  233. static void host1x_device_release(struct device *dev)
  234. {
  235. struct host1x_device *device = to_host1x_device(dev);
  236. kfree(device);
  237. }
  238. static int host1x_device_add(struct host1x *host1x,
  239. struct host1x_driver *driver)
  240. {
  241. struct host1x_client *client, *tmp;
  242. struct host1x_subdev *subdev;
  243. struct host1x_device *device;
  244. int err;
  245. device = kzalloc(sizeof(*device), GFP_KERNEL);
  246. if (!device)
  247. return -ENOMEM;
  248. mutex_init(&device->subdevs_lock);
  249. INIT_LIST_HEAD(&device->subdevs);
  250. INIT_LIST_HEAD(&device->active);
  251. mutex_init(&device->clients_lock);
  252. INIT_LIST_HEAD(&device->clients);
  253. INIT_LIST_HEAD(&device->list);
  254. device->driver = driver;
  255. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  256. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  257. device->dev.release = host1x_device_release;
  258. dev_set_name(&device->dev, "%s", driver->name);
  259. device->dev.bus = &host1x_bus_type;
  260. device->dev.parent = host1x->dev;
  261. err = device_register(&device->dev);
  262. if (err < 0)
  263. return err;
  264. err = host1x_device_parse_dt(device);
  265. if (err < 0) {
  266. device_unregister(&device->dev);
  267. return err;
  268. }
  269. mutex_lock(&host1x->devices_lock);
  270. list_add_tail(&device->list, &host1x->devices);
  271. mutex_unlock(&host1x->devices_lock);
  272. mutex_lock(&clients_lock);
  273. list_for_each_entry_safe(client, tmp, &clients, list) {
  274. list_for_each_entry(subdev, &device->subdevs, list) {
  275. if (subdev->np == client->dev->of_node) {
  276. host1x_subdev_register(device, subdev, client);
  277. break;
  278. }
  279. }
  280. }
  281. mutex_unlock(&clients_lock);
  282. return 0;
  283. }
  284. /*
  285. * Removes a device by first unregistering any subdevices and then removing
  286. * itself from the list of devices.
  287. *
  288. * This function must be called with the host1x->devices_lock held.
  289. */
  290. static void host1x_device_del(struct host1x *host1x,
  291. struct host1x_device *device)
  292. {
  293. struct host1x_subdev *subdev, *sd;
  294. struct host1x_client *client, *cl;
  295. mutex_lock(&device->subdevs_lock);
  296. /* unregister subdevices */
  297. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  298. /*
  299. * host1x_subdev_unregister() will remove the client from
  300. * any lists, so we'll need to manually add it back to the
  301. * list of idle clients.
  302. *
  303. * XXX: Alternatively, perhaps don't remove the client from
  304. * any lists in host1x_subdev_unregister() and instead do
  305. * that explicitly from host1x_unregister_client()?
  306. */
  307. client = subdev->client;
  308. __host1x_subdev_unregister(device, subdev);
  309. /* add the client to the list of idle clients */
  310. mutex_lock(&clients_lock);
  311. list_add_tail(&client->list, &clients);
  312. mutex_unlock(&clients_lock);
  313. }
  314. /* remove subdevices */
  315. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  316. host1x_subdev_del(subdev);
  317. mutex_unlock(&device->subdevs_lock);
  318. /* move clients to idle list */
  319. mutex_lock(&clients_lock);
  320. mutex_lock(&device->clients_lock);
  321. list_for_each_entry_safe(client, cl, &device->clients, list)
  322. list_move_tail(&client->list, &clients);
  323. mutex_unlock(&device->clients_lock);
  324. mutex_unlock(&clients_lock);
  325. /* finally remove the device */
  326. list_del_init(&device->list);
  327. device_unregister(&device->dev);
  328. }
  329. static void host1x_attach_driver(struct host1x *host1x,
  330. struct host1x_driver *driver)
  331. {
  332. struct host1x_device *device;
  333. int err;
  334. mutex_lock(&host1x->devices_lock);
  335. list_for_each_entry(device, &host1x->devices, list) {
  336. if (device->driver == driver) {
  337. mutex_unlock(&host1x->devices_lock);
  338. return;
  339. }
  340. }
  341. mutex_unlock(&host1x->devices_lock);
  342. err = host1x_device_add(host1x, driver);
  343. if (err < 0)
  344. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  345. }
  346. static void host1x_detach_driver(struct host1x *host1x,
  347. struct host1x_driver *driver)
  348. {
  349. struct host1x_device *device, *tmp;
  350. mutex_lock(&host1x->devices_lock);
  351. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  352. if (device->driver == driver)
  353. host1x_device_del(host1x, device);
  354. mutex_unlock(&host1x->devices_lock);
  355. }
  356. int host1x_register(struct host1x *host1x)
  357. {
  358. struct host1x_driver *driver;
  359. mutex_lock(&devices_lock);
  360. list_add_tail(&host1x->list, &devices);
  361. mutex_unlock(&devices_lock);
  362. mutex_lock(&drivers_lock);
  363. list_for_each_entry(driver, &drivers, list)
  364. host1x_attach_driver(host1x, driver);
  365. mutex_unlock(&drivers_lock);
  366. return 0;
  367. }
  368. int host1x_unregister(struct host1x *host1x)
  369. {
  370. struct host1x_driver *driver;
  371. mutex_lock(&drivers_lock);
  372. list_for_each_entry(driver, &drivers, list)
  373. host1x_detach_driver(host1x, driver);
  374. mutex_unlock(&drivers_lock);
  375. mutex_lock(&devices_lock);
  376. list_del_init(&host1x->list);
  377. mutex_unlock(&devices_lock);
  378. return 0;
  379. }
  380. int host1x_driver_register(struct host1x_driver *driver)
  381. {
  382. struct host1x *host1x;
  383. INIT_LIST_HEAD(&driver->list);
  384. mutex_lock(&drivers_lock);
  385. list_add_tail(&driver->list, &drivers);
  386. mutex_unlock(&drivers_lock);
  387. mutex_lock(&devices_lock);
  388. list_for_each_entry(host1x, &devices, list)
  389. host1x_attach_driver(host1x, driver);
  390. mutex_unlock(&devices_lock);
  391. return 0;
  392. }
  393. EXPORT_SYMBOL(host1x_driver_register);
  394. void host1x_driver_unregister(struct host1x_driver *driver)
  395. {
  396. mutex_lock(&drivers_lock);
  397. list_del_init(&driver->list);
  398. mutex_unlock(&drivers_lock);
  399. }
  400. EXPORT_SYMBOL(host1x_driver_unregister);
  401. int host1x_client_register(struct host1x_client *client)
  402. {
  403. struct host1x *host1x;
  404. int err;
  405. mutex_lock(&devices_lock);
  406. list_for_each_entry(host1x, &devices, list) {
  407. err = host1x_add_client(host1x, client);
  408. if (!err) {
  409. mutex_unlock(&devices_lock);
  410. return 0;
  411. }
  412. }
  413. mutex_unlock(&devices_lock);
  414. mutex_lock(&clients_lock);
  415. list_add_tail(&client->list, &clients);
  416. mutex_unlock(&clients_lock);
  417. return 0;
  418. }
  419. EXPORT_SYMBOL(host1x_client_register);
  420. int host1x_client_unregister(struct host1x_client *client)
  421. {
  422. struct host1x_client *c;
  423. struct host1x *host1x;
  424. int err;
  425. mutex_lock(&devices_lock);
  426. list_for_each_entry(host1x, &devices, list) {
  427. err = host1x_del_client(host1x, client);
  428. if (!err) {
  429. mutex_unlock(&devices_lock);
  430. return 0;
  431. }
  432. }
  433. mutex_unlock(&devices_lock);
  434. mutex_lock(&clients_lock);
  435. list_for_each_entry(c, &clients, list) {
  436. if (c == client) {
  437. list_del_init(&c->list);
  438. break;
  439. }
  440. }
  441. mutex_unlock(&clients_lock);
  442. return 0;
  443. }
  444. EXPORT_SYMBOL(host1x_client_unregister);