bus.c 14 KB

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