bus.c 18 KB

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