bus.c 14 KB

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