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 const struct dev_pm_ops host1x_device_pm_ops = {
  229. .suspend = pm_generic_suspend,
  230. .resume = pm_generic_resume,
  231. .freeze = pm_generic_freeze,
  232. .thaw = pm_generic_thaw,
  233. .poweroff = pm_generic_poweroff,
  234. .restore = pm_generic_restore,
  235. };
  236. struct bus_type host1x_bus_type = {
  237. .name = "host1x",
  238. .match = host1x_device_match,
  239. .pm = &host1x_device_pm_ops,
  240. };
  241. static void __host1x_device_del(struct host1x_device *device)
  242. {
  243. struct host1x_subdev *subdev, *sd;
  244. struct host1x_client *client, *cl;
  245. mutex_lock(&device->subdevs_lock);
  246. /* unregister subdevices */
  247. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  248. /*
  249. * host1x_subdev_unregister() will remove the client from
  250. * any lists, so we'll need to manually add it back to the
  251. * list of idle clients.
  252. *
  253. * XXX: Alternatively, perhaps don't remove the client from
  254. * any lists in host1x_subdev_unregister() and instead do
  255. * that explicitly from host1x_unregister_client()?
  256. */
  257. client = subdev->client;
  258. __host1x_subdev_unregister(device, subdev);
  259. /* add the client to the list of idle clients */
  260. mutex_lock(&clients_lock);
  261. list_add_tail(&client->list, &clients);
  262. mutex_unlock(&clients_lock);
  263. }
  264. /* remove subdevices */
  265. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  266. host1x_subdev_del(subdev);
  267. mutex_unlock(&device->subdevs_lock);
  268. /* move clients to idle list */
  269. mutex_lock(&clients_lock);
  270. mutex_lock(&device->clients_lock);
  271. list_for_each_entry_safe(client, cl, &device->clients, list)
  272. list_move_tail(&client->list, &clients);
  273. mutex_unlock(&device->clients_lock);
  274. mutex_unlock(&clients_lock);
  275. /* finally remove the device */
  276. list_del_init(&device->list);
  277. }
  278. static void host1x_device_release(struct device *dev)
  279. {
  280. struct host1x_device *device = to_host1x_device(dev);
  281. __host1x_device_del(device);
  282. kfree(device);
  283. }
  284. static int host1x_device_add(struct host1x *host1x,
  285. struct host1x_driver *driver)
  286. {
  287. struct host1x_client *client, *tmp;
  288. struct host1x_subdev *subdev;
  289. struct host1x_device *device;
  290. int err;
  291. device = kzalloc(sizeof(*device), GFP_KERNEL);
  292. if (!device)
  293. return -ENOMEM;
  294. device_initialize(&device->dev);
  295. mutex_init(&device->subdevs_lock);
  296. INIT_LIST_HEAD(&device->subdevs);
  297. INIT_LIST_HEAD(&device->active);
  298. mutex_init(&device->clients_lock);
  299. INIT_LIST_HEAD(&device->clients);
  300. INIT_LIST_HEAD(&device->list);
  301. device->driver = driver;
  302. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  303. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  304. dev_set_name(&device->dev, "%s", driver->driver.name);
  305. of_dma_configure(&device->dev, host1x->dev->of_node);
  306. device->dev.release = host1x_device_release;
  307. device->dev.of_node = host1x->dev->of_node;
  308. device->dev.bus = &host1x_bus_type;
  309. device->dev.parent = host1x->dev;
  310. err = host1x_device_parse_dt(device, driver);
  311. if (err < 0) {
  312. kfree(device);
  313. return err;
  314. }
  315. list_add_tail(&device->list, &host1x->devices);
  316. mutex_lock(&clients_lock);
  317. list_for_each_entry_safe(client, tmp, &clients, list) {
  318. list_for_each_entry(subdev, &device->subdevs, list) {
  319. if (subdev->np == client->dev->of_node) {
  320. host1x_subdev_register(device, subdev, client);
  321. break;
  322. }
  323. }
  324. }
  325. mutex_unlock(&clients_lock);
  326. return 0;
  327. }
  328. /*
  329. * Removes a device by first unregistering any subdevices and then removing
  330. * itself from the list of devices.
  331. *
  332. * This function must be called with the host1x->devices_lock held.
  333. */
  334. static void host1x_device_del(struct host1x *host1x,
  335. struct host1x_device *device)
  336. {
  337. if (device->registered) {
  338. device->registered = false;
  339. device_del(&device->dev);
  340. }
  341. put_device(&device->dev);
  342. }
  343. static void host1x_attach_driver(struct host1x *host1x,
  344. struct host1x_driver *driver)
  345. {
  346. struct host1x_device *device;
  347. int err;
  348. mutex_lock(&host1x->devices_lock);
  349. list_for_each_entry(device, &host1x->devices, list) {
  350. if (device->driver == driver) {
  351. mutex_unlock(&host1x->devices_lock);
  352. return;
  353. }
  354. }
  355. err = host1x_device_add(host1x, driver);
  356. if (err < 0)
  357. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  358. mutex_unlock(&host1x->devices_lock);
  359. }
  360. static void host1x_detach_driver(struct host1x *host1x,
  361. struct host1x_driver *driver)
  362. {
  363. struct host1x_device *device, *tmp;
  364. mutex_lock(&host1x->devices_lock);
  365. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  366. if (device->driver == driver)
  367. host1x_device_del(host1x, device);
  368. mutex_unlock(&host1x->devices_lock);
  369. }
  370. int host1x_register(struct host1x *host1x)
  371. {
  372. struct host1x_driver *driver;
  373. mutex_lock(&devices_lock);
  374. list_add_tail(&host1x->list, &devices);
  375. mutex_unlock(&devices_lock);
  376. mutex_lock(&drivers_lock);
  377. list_for_each_entry(driver, &drivers, list)
  378. host1x_attach_driver(host1x, driver);
  379. mutex_unlock(&drivers_lock);
  380. return 0;
  381. }
  382. int host1x_unregister(struct host1x *host1x)
  383. {
  384. struct host1x_driver *driver;
  385. mutex_lock(&drivers_lock);
  386. list_for_each_entry(driver, &drivers, list)
  387. host1x_detach_driver(host1x, driver);
  388. mutex_unlock(&drivers_lock);
  389. mutex_lock(&devices_lock);
  390. list_del_init(&host1x->list);
  391. mutex_unlock(&devices_lock);
  392. return 0;
  393. }
  394. static int host1x_device_probe(struct device *dev)
  395. {
  396. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  397. struct host1x_device *device = to_host1x_device(dev);
  398. if (driver->probe)
  399. return driver->probe(device);
  400. return 0;
  401. }
  402. static int host1x_device_remove(struct device *dev)
  403. {
  404. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  405. struct host1x_device *device = to_host1x_device(dev);
  406. if (driver->remove)
  407. return driver->remove(device);
  408. return 0;
  409. }
  410. static void host1x_device_shutdown(struct device *dev)
  411. {
  412. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  413. struct host1x_device *device = to_host1x_device(dev);
  414. if (driver->shutdown)
  415. driver->shutdown(device);
  416. }
  417. int host1x_driver_register_full(struct host1x_driver *driver,
  418. struct module *owner)
  419. {
  420. struct host1x *host1x;
  421. INIT_LIST_HEAD(&driver->list);
  422. mutex_lock(&drivers_lock);
  423. list_add_tail(&driver->list, &drivers);
  424. mutex_unlock(&drivers_lock);
  425. mutex_lock(&devices_lock);
  426. list_for_each_entry(host1x, &devices, list)
  427. host1x_attach_driver(host1x, driver);
  428. mutex_unlock(&devices_lock);
  429. driver->driver.bus = &host1x_bus_type;
  430. driver->driver.owner = owner;
  431. driver->driver.probe = host1x_device_probe;
  432. driver->driver.remove = host1x_device_remove;
  433. driver->driver.shutdown = host1x_device_shutdown;
  434. return driver_register(&driver->driver);
  435. }
  436. EXPORT_SYMBOL(host1x_driver_register_full);
  437. void host1x_driver_unregister(struct host1x_driver *driver)
  438. {
  439. driver_unregister(&driver->driver);
  440. mutex_lock(&drivers_lock);
  441. list_del_init(&driver->list);
  442. mutex_unlock(&drivers_lock);
  443. }
  444. EXPORT_SYMBOL(host1x_driver_unregister);
  445. int host1x_client_register(struct host1x_client *client)
  446. {
  447. struct host1x *host1x;
  448. int err;
  449. mutex_lock(&devices_lock);
  450. list_for_each_entry(host1x, &devices, list) {
  451. err = host1x_add_client(host1x, client);
  452. if (!err) {
  453. mutex_unlock(&devices_lock);
  454. return 0;
  455. }
  456. }
  457. mutex_unlock(&devices_lock);
  458. mutex_lock(&clients_lock);
  459. list_add_tail(&client->list, &clients);
  460. mutex_unlock(&clients_lock);
  461. return 0;
  462. }
  463. EXPORT_SYMBOL(host1x_client_register);
  464. int host1x_client_unregister(struct host1x_client *client)
  465. {
  466. struct host1x_client *c;
  467. struct host1x *host1x;
  468. int err;
  469. mutex_lock(&devices_lock);
  470. list_for_each_entry(host1x, &devices, list) {
  471. err = host1x_del_client(host1x, client);
  472. if (!err) {
  473. mutex_unlock(&devices_lock);
  474. return 0;
  475. }
  476. }
  477. mutex_unlock(&devices_lock);
  478. mutex_lock(&clients_lock);
  479. list_for_each_entry(c, &clients, list) {
  480. if (c == client) {
  481. list_del_init(&c->list);
  482. break;
  483. }
  484. }
  485. mutex_unlock(&clients_lock);
  486. return 0;
  487. }
  488. EXPORT_SYMBOL(host1x_client_unregister);