bus.c 13 KB

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