bus.c 17 KB

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