bus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. int host1x_device_exit(struct host1x_device *device)
  165. {
  166. struct host1x_client *client;
  167. int err;
  168. mutex_lock(&device->clients_lock);
  169. list_for_each_entry_reverse(client, &device->clients, list) {
  170. if (client->ops && client->ops->exit) {
  171. err = client->ops->exit(client);
  172. if (err < 0) {
  173. dev_err(&device->dev,
  174. "failed to cleanup %s: %d\n",
  175. dev_name(client->dev), err);
  176. mutex_unlock(&device->clients_lock);
  177. return err;
  178. }
  179. }
  180. }
  181. mutex_unlock(&device->clients_lock);
  182. return 0;
  183. }
  184. static int host1x_register_client(struct host1x *host1x,
  185. struct host1x_client *client)
  186. {
  187. struct host1x_device *device;
  188. struct host1x_subdev *subdev;
  189. mutex_lock(&host1x->devices_lock);
  190. list_for_each_entry(device, &host1x->devices, list) {
  191. list_for_each_entry(subdev, &device->subdevs, list) {
  192. if (subdev->np == client->dev->of_node) {
  193. host1x_subdev_register(device, subdev, client);
  194. mutex_unlock(&host1x->devices_lock);
  195. return 0;
  196. }
  197. }
  198. }
  199. mutex_unlock(&host1x->devices_lock);
  200. return -ENODEV;
  201. }
  202. static int host1x_unregister_client(struct host1x *host1x,
  203. struct host1x_client *client)
  204. {
  205. struct host1x_device *device, *dt;
  206. struct host1x_subdev *subdev;
  207. mutex_lock(&host1x->devices_lock);
  208. list_for_each_entry_safe(device, dt, &host1x->devices, list) {
  209. list_for_each_entry(subdev, &device->active, list) {
  210. if (subdev->client == client) {
  211. host1x_subdev_unregister(device, subdev);
  212. mutex_unlock(&host1x->devices_lock);
  213. return 0;
  214. }
  215. }
  216. }
  217. mutex_unlock(&host1x->devices_lock);
  218. return -ENODEV;
  219. }
  220. static struct bus_type host1x_bus_type = {
  221. .name = "host1x",
  222. };
  223. int host1x_bus_init(void)
  224. {
  225. return bus_register(&host1x_bus_type);
  226. }
  227. void host1x_bus_exit(void)
  228. {
  229. bus_unregister(&host1x_bus_type);
  230. }
  231. static void host1x_device_release(struct device *dev)
  232. {
  233. struct host1x_device *device = to_host1x_device(dev);
  234. kfree(device);
  235. }
  236. static int host1x_device_add(struct host1x *host1x,
  237. struct host1x_driver *driver)
  238. {
  239. struct host1x_client *client, *tmp;
  240. struct host1x_subdev *subdev;
  241. struct host1x_device *device;
  242. int err;
  243. device = kzalloc(sizeof(*device), GFP_KERNEL);
  244. if (!device)
  245. return -ENOMEM;
  246. mutex_init(&device->subdevs_lock);
  247. INIT_LIST_HEAD(&device->subdevs);
  248. INIT_LIST_HEAD(&device->active);
  249. mutex_init(&device->clients_lock);
  250. INIT_LIST_HEAD(&device->clients);
  251. INIT_LIST_HEAD(&device->list);
  252. device->driver = driver;
  253. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  254. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  255. device->dev.release = host1x_device_release;
  256. dev_set_name(&device->dev, "%s", driver->name);
  257. device->dev.bus = &host1x_bus_type;
  258. device->dev.parent = host1x->dev;
  259. err = device_register(&device->dev);
  260. if (err < 0)
  261. return err;
  262. err = host1x_device_parse_dt(device);
  263. if (err < 0) {
  264. device_unregister(&device->dev);
  265. return err;
  266. }
  267. mutex_lock(&host1x->devices_lock);
  268. list_add_tail(&device->list, &host1x->devices);
  269. mutex_unlock(&host1x->devices_lock);
  270. mutex_lock(&clients_lock);
  271. list_for_each_entry_safe(client, tmp, &clients, list) {
  272. list_for_each_entry(subdev, &device->subdevs, list) {
  273. if (subdev->np == client->dev->of_node) {
  274. host1x_subdev_register(device, subdev, client);
  275. break;
  276. }
  277. }
  278. }
  279. mutex_unlock(&clients_lock);
  280. return 0;
  281. }
  282. /*
  283. * Removes a device by first unregistering any subdevices and then removing
  284. * itself from the list of devices.
  285. *
  286. * This function must be called with the host1x->devices_lock held.
  287. */
  288. static void host1x_device_del(struct host1x *host1x,
  289. struct host1x_device *device)
  290. {
  291. struct host1x_subdev *subdev, *sd;
  292. struct host1x_client *client, *cl;
  293. mutex_lock(&device->subdevs_lock);
  294. /* unregister subdevices */
  295. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  296. /*
  297. * host1x_subdev_unregister() will remove the client from
  298. * any lists, so we'll need to manually add it back to the
  299. * list of idle clients.
  300. *
  301. * XXX: Alternatively, perhaps don't remove the client from
  302. * any lists in host1x_subdev_unregister() and instead do
  303. * that explicitly from host1x_unregister_client()?
  304. */
  305. client = subdev->client;
  306. __host1x_subdev_unregister(device, subdev);
  307. /* add the client to the list of idle clients */
  308. mutex_lock(&clients_lock);
  309. list_add_tail(&client->list, &clients);
  310. mutex_unlock(&clients_lock);
  311. }
  312. /* remove subdevices */
  313. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  314. host1x_subdev_del(subdev);
  315. mutex_unlock(&device->subdevs_lock);
  316. /* move clients to idle list */
  317. mutex_lock(&clients_lock);
  318. mutex_lock(&device->clients_lock);
  319. list_for_each_entry_safe(client, cl, &device->clients, list)
  320. list_move_tail(&client->list, &clients);
  321. mutex_unlock(&device->clients_lock);
  322. mutex_unlock(&clients_lock);
  323. /* finally remove the device */
  324. list_del_init(&device->list);
  325. device_unregister(&device->dev);
  326. }
  327. static void host1x_attach_driver(struct host1x *host1x,
  328. struct host1x_driver *driver)
  329. {
  330. struct host1x_device *device;
  331. int err;
  332. mutex_lock(&host1x->devices_lock);
  333. list_for_each_entry(device, &host1x->devices, list) {
  334. if (device->driver == driver) {
  335. mutex_unlock(&host1x->devices_lock);
  336. return;
  337. }
  338. }
  339. mutex_unlock(&host1x->devices_lock);
  340. err = host1x_device_add(host1x, driver);
  341. if (err < 0)
  342. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  343. }
  344. static void host1x_detach_driver(struct host1x *host1x,
  345. struct host1x_driver *driver)
  346. {
  347. struct host1x_device *device, *tmp;
  348. mutex_lock(&host1x->devices_lock);
  349. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  350. if (device->driver == driver)
  351. host1x_device_del(host1x, device);
  352. mutex_unlock(&host1x->devices_lock);
  353. }
  354. int host1x_register(struct host1x *host1x)
  355. {
  356. struct host1x_driver *driver;
  357. mutex_lock(&devices_lock);
  358. list_add_tail(&host1x->list, &devices);
  359. mutex_unlock(&devices_lock);
  360. mutex_lock(&drivers_lock);
  361. list_for_each_entry(driver, &drivers, list)
  362. host1x_attach_driver(host1x, driver);
  363. mutex_unlock(&drivers_lock);
  364. return 0;
  365. }
  366. int host1x_unregister(struct host1x *host1x)
  367. {
  368. struct host1x_driver *driver;
  369. mutex_lock(&drivers_lock);
  370. list_for_each_entry(driver, &drivers, list)
  371. host1x_detach_driver(host1x, driver);
  372. mutex_unlock(&drivers_lock);
  373. mutex_lock(&devices_lock);
  374. list_del_init(&host1x->list);
  375. mutex_unlock(&devices_lock);
  376. return 0;
  377. }
  378. int host1x_driver_register(struct host1x_driver *driver)
  379. {
  380. struct host1x *host1x;
  381. INIT_LIST_HEAD(&driver->list);
  382. mutex_lock(&drivers_lock);
  383. list_add_tail(&driver->list, &drivers);
  384. mutex_unlock(&drivers_lock);
  385. mutex_lock(&devices_lock);
  386. list_for_each_entry(host1x, &devices, list)
  387. host1x_attach_driver(host1x, driver);
  388. mutex_unlock(&devices_lock);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(host1x_driver_register);
  392. void host1x_driver_unregister(struct host1x_driver *driver)
  393. {
  394. mutex_lock(&drivers_lock);
  395. list_del_init(&driver->list);
  396. mutex_unlock(&drivers_lock);
  397. }
  398. EXPORT_SYMBOL(host1x_driver_unregister);
  399. int host1x_client_register(struct host1x_client *client)
  400. {
  401. struct host1x *host1x;
  402. int err;
  403. mutex_lock(&devices_lock);
  404. list_for_each_entry(host1x, &devices, list) {
  405. err = host1x_register_client(host1x, client);
  406. if (!err) {
  407. mutex_unlock(&devices_lock);
  408. return 0;
  409. }
  410. }
  411. mutex_unlock(&devices_lock);
  412. mutex_lock(&clients_lock);
  413. list_add_tail(&client->list, &clients);
  414. mutex_unlock(&clients_lock);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL(host1x_client_register);
  418. int host1x_client_unregister(struct host1x_client *client)
  419. {
  420. struct host1x_client *c;
  421. struct host1x *host1x;
  422. int err;
  423. mutex_lock(&devices_lock);
  424. list_for_each_entry(host1x, &devices, list) {
  425. err = host1x_unregister_client(host1x, client);
  426. if (!err) {
  427. mutex_unlock(&devices_lock);
  428. return 0;
  429. }
  430. }
  431. mutex_unlock(&devices_lock);
  432. mutex_lock(&clients_lock);
  433. list_for_each_entry(c, &clients, list) {
  434. if (c == client) {
  435. list_del_init(&c->list);
  436. break;
  437. }
  438. }
  439. mutex_unlock(&clients_lock);
  440. return 0;
  441. }
  442. EXPORT_SYMBOL(host1x_client_unregister);