bus.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Avionic Design GmbH
  4. * Copyright (C) 2012-2013, NVIDIA Corporation
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/host1x.h>
  8. #include <linux/of.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/slab.h>
  11. #include <linux/of_device.h>
  12. #include "bus.h"
  13. #include "dev.h"
  14. static DEFINE_MUTEX(clients_lock);
  15. static LIST_HEAD(clients);
  16. static DEFINE_MUTEX(drivers_lock);
  17. static LIST_HEAD(drivers);
  18. static DEFINE_MUTEX(devices_lock);
  19. static LIST_HEAD(devices);
  20. struct host1x_subdev {
  21. struct host1x_client *client;
  22. struct device_node *np;
  23. struct list_head list;
  24. };
  25. /**
  26. * host1x_subdev_add() - add a new subdevice with an associated device node
  27. * @device: host1x device to add the subdevice to
  28. * @np: device node
  29. */
  30. static int host1x_subdev_add(struct host1x_device *device,
  31. struct host1x_driver *driver,
  32. struct device_node *np)
  33. {
  34. struct host1x_subdev *subdev;
  35. struct device_node *child;
  36. int err;
  37. subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
  38. if (!subdev)
  39. return -ENOMEM;
  40. INIT_LIST_HEAD(&subdev->list);
  41. subdev->np = of_node_get(np);
  42. mutex_lock(&device->subdevs_lock);
  43. list_add_tail(&subdev->list, &device->subdevs);
  44. mutex_unlock(&device->subdevs_lock);
  45. /* recursively add children */
  46. for_each_child_of_node(np, child) {
  47. if (of_match_node(driver->subdevs, child) &&
  48. of_device_is_available(child)) {
  49. err = host1x_subdev_add(device, driver, child);
  50. if (err < 0) {
  51. /* XXX cleanup? */
  52. of_node_put(child);
  53. return err;
  54. }
  55. }
  56. }
  57. return 0;
  58. }
  59. /**
  60. * host1x_subdev_del() - remove subdevice
  61. * @subdev: subdevice to remove
  62. */
  63. static void host1x_subdev_del(struct host1x_subdev *subdev)
  64. {
  65. list_del(&subdev->list);
  66. of_node_put(subdev->np);
  67. kfree(subdev);
  68. }
  69. /**
  70. * host1x_device_parse_dt() - scan device tree and add matching subdevices
  71. * @device: host1x logical device
  72. * @driver: host1x driver
  73. */
  74. static int host1x_device_parse_dt(struct host1x_device *device,
  75. struct host1x_driver *driver)
  76. {
  77. struct device_node *np;
  78. int err;
  79. for_each_child_of_node(device->dev.parent->of_node, np) {
  80. if (of_match_node(driver->subdevs, np) &&
  81. of_device_is_available(np)) {
  82. err = host1x_subdev_add(device, driver, np);
  83. if (err < 0) {
  84. of_node_put(np);
  85. return err;
  86. }
  87. }
  88. }
  89. return 0;
  90. }
  91. static void host1x_subdev_register(struct host1x_device *device,
  92. struct host1x_subdev *subdev,
  93. struct host1x_client *client)
  94. {
  95. int err;
  96. /*
  97. * Move the subdevice to the list of active (registered) subdevices
  98. * and associate it with a client. At the same time, associate the
  99. * client with its parent device.
  100. */
  101. mutex_lock(&device->subdevs_lock);
  102. mutex_lock(&device->clients_lock);
  103. list_move_tail(&client->list, &device->clients);
  104. list_move_tail(&subdev->list, &device->active);
  105. client->host = &device->dev;
  106. subdev->client = client;
  107. mutex_unlock(&device->clients_lock);
  108. mutex_unlock(&device->subdevs_lock);
  109. if (list_empty(&device->subdevs)) {
  110. err = device_add(&device->dev);
  111. if (err < 0)
  112. dev_err(&device->dev, "failed to add: %d\n", err);
  113. else
  114. device->registered = true;
  115. }
  116. }
  117. static void __host1x_subdev_unregister(struct host1x_device *device,
  118. struct host1x_subdev *subdev)
  119. {
  120. struct host1x_client *client = subdev->client;
  121. /*
  122. * If all subdevices have been activated, we're about to remove the
  123. * first active subdevice, so unload the driver first.
  124. */
  125. if (list_empty(&device->subdevs)) {
  126. if (device->registered) {
  127. device->registered = false;
  128. device_del(&device->dev);
  129. }
  130. }
  131. /*
  132. * Move the subdevice back to the list of idle subdevices and remove
  133. * it from list of clients.
  134. */
  135. mutex_lock(&device->clients_lock);
  136. subdev->client = NULL;
  137. client->host = NULL;
  138. list_move_tail(&subdev->list, &device->subdevs);
  139. /*
  140. * XXX: Perhaps don't do this here, but rather explicitly remove it
  141. * when the device is about to be deleted.
  142. *
  143. * This is somewhat complicated by the fact that this function is
  144. * used to remove the subdevice when a client is unregistered but
  145. * also when the composite device is about to be removed.
  146. */
  147. list_del_init(&client->list);
  148. mutex_unlock(&device->clients_lock);
  149. }
  150. static void host1x_subdev_unregister(struct host1x_device *device,
  151. struct host1x_subdev *subdev)
  152. {
  153. mutex_lock(&device->subdevs_lock);
  154. __host1x_subdev_unregister(device, subdev);
  155. mutex_unlock(&device->subdevs_lock);
  156. }
  157. /**
  158. * host1x_device_init() - initialize a host1x logical device
  159. * @device: host1x logical device
  160. *
  161. * The driver for the host1x logical device can call this during execution of
  162. * its &host1x_driver.probe implementation to initialize each of its clients.
  163. * The client drivers access the subsystem specific driver data using the
  164. * &host1x_client.parent field and driver data associated with it (usually by
  165. * calling dev_get_drvdata()).
  166. */
  167. int host1x_device_init(struct host1x_device *device)
  168. {
  169. struct host1x_client *client;
  170. int err;
  171. mutex_lock(&device->clients_lock);
  172. list_for_each_entry(client, &device->clients, list) {
  173. if (client->ops && client->ops->init) {
  174. err = client->ops->init(client);
  175. if (err < 0) {
  176. dev_err(&device->dev,
  177. "failed to initialize %s: %d\n",
  178. dev_name(client->dev), err);
  179. goto teardown;
  180. }
  181. }
  182. }
  183. mutex_unlock(&device->clients_lock);
  184. return 0;
  185. teardown:
  186. list_for_each_entry_continue_reverse(client, &device->clients, list)
  187. if (client->ops->exit)
  188. client->ops->exit(client);
  189. mutex_unlock(&device->clients_lock);
  190. return err;
  191. }
  192. EXPORT_SYMBOL(host1x_device_init);
  193. /**
  194. * host1x_device_exit() - uninitialize host1x logical device
  195. * @device: host1x logical device
  196. *
  197. * When the driver for a host1x logical device is unloaded, it can call this
  198. * function to tear down each of its clients. Typically this is done after a
  199. * subsystem-specific data structure is removed and the functionality can no
  200. * longer be used.
  201. */
  202. int host1x_device_exit(struct host1x_device *device)
  203. {
  204. struct host1x_client *client;
  205. int err;
  206. mutex_lock(&device->clients_lock);
  207. list_for_each_entry_reverse(client, &device->clients, list) {
  208. if (client->ops && client->ops->exit) {
  209. err = client->ops->exit(client);
  210. if (err < 0) {
  211. dev_err(&device->dev,
  212. "failed to cleanup %s: %d\n",
  213. dev_name(client->dev), err);
  214. mutex_unlock(&device->clients_lock);
  215. return err;
  216. }
  217. }
  218. }
  219. mutex_unlock(&device->clients_lock);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL(host1x_device_exit);
  223. static int host1x_add_client(struct host1x *host1x,
  224. struct host1x_client *client)
  225. {
  226. struct host1x_device *device;
  227. struct host1x_subdev *subdev;
  228. mutex_lock(&host1x->devices_lock);
  229. list_for_each_entry(device, &host1x->devices, list) {
  230. list_for_each_entry(subdev, &device->subdevs, list) {
  231. if (subdev->np == client->dev->of_node) {
  232. host1x_subdev_register(device, subdev, client);
  233. mutex_unlock(&host1x->devices_lock);
  234. return 0;
  235. }
  236. }
  237. }
  238. mutex_unlock(&host1x->devices_lock);
  239. return -ENODEV;
  240. }
  241. static int host1x_del_client(struct host1x *host1x,
  242. struct host1x_client *client)
  243. {
  244. struct host1x_device *device, *dt;
  245. struct host1x_subdev *subdev;
  246. mutex_lock(&host1x->devices_lock);
  247. list_for_each_entry_safe(device, dt, &host1x->devices, list) {
  248. list_for_each_entry(subdev, &device->active, list) {
  249. if (subdev->client == client) {
  250. host1x_subdev_unregister(device, subdev);
  251. mutex_unlock(&host1x->devices_lock);
  252. return 0;
  253. }
  254. }
  255. }
  256. mutex_unlock(&host1x->devices_lock);
  257. return -ENODEV;
  258. }
  259. static int host1x_device_match(struct device *dev, struct device_driver *drv)
  260. {
  261. return strcmp(dev_name(dev), drv->name) == 0;
  262. }
  263. static int host1x_device_uevent(struct device *dev,
  264. struct kobj_uevent_env *env)
  265. {
  266. struct device_node *np = dev->parent->of_node;
  267. unsigned int count = 0;
  268. struct property *p;
  269. const char *compat;
  270. /*
  271. * This duplicates most of of_device_uevent(), but the latter cannot
  272. * be called from modules and operates on dev->of_node, which is not
  273. * available in this case.
  274. *
  275. * Note that this is really only needed for backwards compatibility
  276. * with libdrm, which parses this information from sysfs and will
  277. * fail if it can't find the OF_FULLNAME, specifically.
  278. */
  279. add_uevent_var(env, "OF_NAME=%pOFn", np);
  280. add_uevent_var(env, "OF_FULLNAME=%pOF", np);
  281. of_property_for_each_string(np, "compatible", p, compat) {
  282. add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
  283. count++;
  284. }
  285. add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
  286. return 0;
  287. }
  288. static int host1x_dma_configure(struct device *dev)
  289. {
  290. return of_dma_configure(dev, dev->of_node, true);
  291. }
  292. static const struct dev_pm_ops host1x_device_pm_ops = {
  293. .suspend = pm_generic_suspend,
  294. .resume = pm_generic_resume,
  295. .freeze = pm_generic_freeze,
  296. .thaw = pm_generic_thaw,
  297. .poweroff = pm_generic_poweroff,
  298. .restore = pm_generic_restore,
  299. };
  300. struct bus_type host1x_bus_type = {
  301. .name = "host1x",
  302. .match = host1x_device_match,
  303. .uevent = host1x_device_uevent,
  304. .dma_configure = host1x_dma_configure,
  305. .pm = &host1x_device_pm_ops,
  306. };
  307. static void __host1x_device_del(struct host1x_device *device)
  308. {
  309. struct host1x_subdev *subdev, *sd;
  310. struct host1x_client *client, *cl;
  311. mutex_lock(&device->subdevs_lock);
  312. /* unregister subdevices */
  313. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  314. /*
  315. * host1x_subdev_unregister() will remove the client from
  316. * any lists, so we'll need to manually add it back to the
  317. * list of idle clients.
  318. *
  319. * XXX: Alternatively, perhaps don't remove the client from
  320. * any lists in host1x_subdev_unregister() and instead do
  321. * that explicitly from host1x_unregister_client()?
  322. */
  323. client = subdev->client;
  324. __host1x_subdev_unregister(device, subdev);
  325. /* add the client to the list of idle clients */
  326. mutex_lock(&clients_lock);
  327. list_add_tail(&client->list, &clients);
  328. mutex_unlock(&clients_lock);
  329. }
  330. /* remove subdevices */
  331. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  332. host1x_subdev_del(subdev);
  333. mutex_unlock(&device->subdevs_lock);
  334. /* move clients to idle list */
  335. mutex_lock(&clients_lock);
  336. mutex_lock(&device->clients_lock);
  337. list_for_each_entry_safe(client, cl, &device->clients, list)
  338. list_move_tail(&client->list, &clients);
  339. mutex_unlock(&device->clients_lock);
  340. mutex_unlock(&clients_lock);
  341. /* finally remove the device */
  342. list_del_init(&device->list);
  343. }
  344. static void host1x_device_release(struct device *dev)
  345. {
  346. struct host1x_device *device = to_host1x_device(dev);
  347. __host1x_device_del(device);
  348. kfree(device);
  349. }
  350. static int host1x_device_add(struct host1x *host1x,
  351. struct host1x_driver *driver)
  352. {
  353. struct host1x_client *client, *tmp;
  354. struct host1x_subdev *subdev;
  355. struct host1x_device *device;
  356. int err;
  357. device = kzalloc(sizeof(*device), GFP_KERNEL);
  358. if (!device)
  359. return -ENOMEM;
  360. device_initialize(&device->dev);
  361. mutex_init(&device->subdevs_lock);
  362. INIT_LIST_HEAD(&device->subdevs);
  363. INIT_LIST_HEAD(&device->active);
  364. mutex_init(&device->clients_lock);
  365. INIT_LIST_HEAD(&device->clients);
  366. INIT_LIST_HEAD(&device->list);
  367. device->driver = driver;
  368. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  369. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  370. dev_set_name(&device->dev, "%s", driver->driver.name);
  371. device->dev.release = host1x_device_release;
  372. device->dev.bus = &host1x_bus_type;
  373. device->dev.parent = host1x->dev;
  374. of_dma_configure(&device->dev, host1x->dev->of_node, true);
  375. device->dev.dma_parms = &device->dma_parms;
  376. dma_set_max_seg_size(&device->dev, UINT_MAX);
  377. err = host1x_device_parse_dt(device, driver);
  378. if (err < 0) {
  379. kfree(device);
  380. return err;
  381. }
  382. list_add_tail(&device->list, &host1x->devices);
  383. mutex_lock(&clients_lock);
  384. list_for_each_entry_safe(client, tmp, &clients, list) {
  385. list_for_each_entry(subdev, &device->subdevs, list) {
  386. if (subdev->np == client->dev->of_node) {
  387. host1x_subdev_register(device, subdev, client);
  388. break;
  389. }
  390. }
  391. }
  392. mutex_unlock(&clients_lock);
  393. return 0;
  394. }
  395. /*
  396. * Removes a device by first unregistering any subdevices and then removing
  397. * itself from the list of devices.
  398. *
  399. * This function must be called with the host1x->devices_lock held.
  400. */
  401. static void host1x_device_del(struct host1x *host1x,
  402. struct host1x_device *device)
  403. {
  404. if (device->registered) {
  405. device->registered = false;
  406. device_del(&device->dev);
  407. }
  408. put_device(&device->dev);
  409. }
  410. static void host1x_attach_driver(struct host1x *host1x,
  411. struct host1x_driver *driver)
  412. {
  413. struct host1x_device *device;
  414. int err;
  415. mutex_lock(&host1x->devices_lock);
  416. list_for_each_entry(device, &host1x->devices, list) {
  417. if (device->driver == driver) {
  418. mutex_unlock(&host1x->devices_lock);
  419. return;
  420. }
  421. }
  422. err = host1x_device_add(host1x, driver);
  423. if (err < 0)
  424. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  425. mutex_unlock(&host1x->devices_lock);
  426. }
  427. static void host1x_detach_driver(struct host1x *host1x,
  428. struct host1x_driver *driver)
  429. {
  430. struct host1x_device *device, *tmp;
  431. mutex_lock(&host1x->devices_lock);
  432. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  433. if (device->driver == driver)
  434. host1x_device_del(host1x, device);
  435. mutex_unlock(&host1x->devices_lock);
  436. }
  437. static int host1x_devices_show(struct seq_file *s, void *data)
  438. {
  439. struct host1x *host1x = s->private;
  440. struct host1x_device *device;
  441. mutex_lock(&host1x->devices_lock);
  442. list_for_each_entry(device, &host1x->devices, list) {
  443. struct host1x_subdev *subdev;
  444. seq_printf(s, "%s\n", dev_name(&device->dev));
  445. mutex_lock(&device->subdevs_lock);
  446. list_for_each_entry(subdev, &device->active, list)
  447. seq_printf(s, " %pOFf: %s\n", subdev->np,
  448. dev_name(subdev->client->dev));
  449. list_for_each_entry(subdev, &device->subdevs, list)
  450. seq_printf(s, " %pOFf:\n", subdev->np);
  451. mutex_unlock(&device->subdevs_lock);
  452. }
  453. mutex_unlock(&host1x->devices_lock);
  454. return 0;
  455. }
  456. DEFINE_SHOW_ATTRIBUTE(host1x_devices);
  457. /**
  458. * host1x_register() - register a host1x controller
  459. * @host1x: host1x controller
  460. *
  461. * The host1x controller driver uses this to register a host1x controller with
  462. * the infrastructure. Note that all Tegra SoC generations have only ever come
  463. * with a single host1x instance, so this function is somewhat academic.
  464. */
  465. int host1x_register(struct host1x *host1x)
  466. {
  467. struct host1x_driver *driver;
  468. mutex_lock(&devices_lock);
  469. list_add_tail(&host1x->list, &devices);
  470. mutex_unlock(&devices_lock);
  471. mutex_lock(&drivers_lock);
  472. list_for_each_entry(driver, &drivers, list)
  473. host1x_attach_driver(host1x, driver);
  474. mutex_unlock(&drivers_lock);
  475. debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
  476. &host1x_devices_fops);
  477. return 0;
  478. }
  479. /**
  480. * host1x_unregister() - unregister a host1x controller
  481. * @host1x: host1x controller
  482. *
  483. * The host1x controller driver uses this to remove a host1x controller from
  484. * the infrastructure.
  485. */
  486. int host1x_unregister(struct host1x *host1x)
  487. {
  488. struct host1x_driver *driver;
  489. mutex_lock(&drivers_lock);
  490. list_for_each_entry(driver, &drivers, list)
  491. host1x_detach_driver(host1x, driver);
  492. mutex_unlock(&drivers_lock);
  493. mutex_lock(&devices_lock);
  494. list_del_init(&host1x->list);
  495. mutex_unlock(&devices_lock);
  496. return 0;
  497. }
  498. static int host1x_device_probe(struct device *dev)
  499. {
  500. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  501. struct host1x_device *device = to_host1x_device(dev);
  502. if (driver->probe)
  503. return driver->probe(device);
  504. return 0;
  505. }
  506. static int host1x_device_remove(struct device *dev)
  507. {
  508. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  509. struct host1x_device *device = to_host1x_device(dev);
  510. if (driver->remove)
  511. return driver->remove(device);
  512. return 0;
  513. }
  514. static void host1x_device_shutdown(struct device *dev)
  515. {
  516. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  517. struct host1x_device *device = to_host1x_device(dev);
  518. if (driver->shutdown)
  519. driver->shutdown(device);
  520. }
  521. /**
  522. * host1x_driver_register_full() - register a host1x driver
  523. * @driver: host1x driver
  524. * @owner: owner module
  525. *
  526. * Drivers for host1x logical devices call this function to register a driver
  527. * with the infrastructure. Note that since these drive logical devices, the
  528. * registration of the driver actually triggers tho logical device creation.
  529. * A logical device will be created for each host1x instance.
  530. */
  531. int host1x_driver_register_full(struct host1x_driver *driver,
  532. struct module *owner)
  533. {
  534. struct host1x *host1x;
  535. INIT_LIST_HEAD(&driver->list);
  536. mutex_lock(&drivers_lock);
  537. list_add_tail(&driver->list, &drivers);
  538. mutex_unlock(&drivers_lock);
  539. mutex_lock(&devices_lock);
  540. list_for_each_entry(host1x, &devices, list)
  541. host1x_attach_driver(host1x, driver);
  542. mutex_unlock(&devices_lock);
  543. driver->driver.bus = &host1x_bus_type;
  544. driver->driver.owner = owner;
  545. driver->driver.probe = host1x_device_probe;
  546. driver->driver.remove = host1x_device_remove;
  547. driver->driver.shutdown = host1x_device_shutdown;
  548. return driver_register(&driver->driver);
  549. }
  550. EXPORT_SYMBOL(host1x_driver_register_full);
  551. /**
  552. * host1x_driver_unregister() - unregister a host1x driver
  553. * @driver: host1x driver
  554. *
  555. * Unbinds the driver from each of the host1x logical devices that it is
  556. * bound to, effectively removing the subsystem devices that they represent.
  557. */
  558. void host1x_driver_unregister(struct host1x_driver *driver)
  559. {
  560. struct host1x *host1x;
  561. driver_unregister(&driver->driver);
  562. mutex_lock(&devices_lock);
  563. list_for_each_entry(host1x, &devices, list)
  564. host1x_detach_driver(host1x, driver);
  565. mutex_unlock(&devices_lock);
  566. mutex_lock(&drivers_lock);
  567. list_del_init(&driver->list);
  568. mutex_unlock(&drivers_lock);
  569. }
  570. EXPORT_SYMBOL(host1x_driver_unregister);
  571. /**
  572. * __host1x_client_init() - initialize a host1x client
  573. * @client: host1x client
  574. * @key: lock class key for the client-specific mutex
  575. */
  576. void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key)
  577. {
  578. INIT_LIST_HEAD(&client->list);
  579. __mutex_init(&client->lock, "host1x client lock", key);
  580. client->usecount = 0;
  581. }
  582. EXPORT_SYMBOL(__host1x_client_init);
  583. /**
  584. * host1x_client_exit() - uninitialize a host1x client
  585. * @client: host1x client
  586. */
  587. void host1x_client_exit(struct host1x_client *client)
  588. {
  589. mutex_destroy(&client->lock);
  590. }
  591. EXPORT_SYMBOL(host1x_client_exit);
  592. /**
  593. * __host1x_client_register() - register a host1x client
  594. * @client: host1x client
  595. * @key: lock class key for the client-specific mutex
  596. *
  597. * Registers a host1x client with each host1x controller instance. Note that
  598. * each client will only match their parent host1x controller and will only be
  599. * associated with that instance. Once all clients have been registered with
  600. * their parent host1x controller, the infrastructure will set up the logical
  601. * device and call host1x_device_init(), which will in turn call each client's
  602. * &host1x_client_ops.init implementation.
  603. */
  604. int __host1x_client_register(struct host1x_client *client)
  605. {
  606. struct host1x *host1x;
  607. int err;
  608. mutex_lock(&devices_lock);
  609. list_for_each_entry(host1x, &devices, list) {
  610. err = host1x_add_client(host1x, client);
  611. if (!err) {
  612. mutex_unlock(&devices_lock);
  613. return 0;
  614. }
  615. }
  616. mutex_unlock(&devices_lock);
  617. mutex_lock(&clients_lock);
  618. list_add_tail(&client->list, &clients);
  619. mutex_unlock(&clients_lock);
  620. return 0;
  621. }
  622. EXPORT_SYMBOL(__host1x_client_register);
  623. /**
  624. * host1x_client_unregister() - unregister a host1x client
  625. * @client: host1x client
  626. *
  627. * Removes a host1x client from its host1x controller instance. If a logical
  628. * device has already been initialized, it will be torn down.
  629. */
  630. int host1x_client_unregister(struct host1x_client *client)
  631. {
  632. struct host1x_client *c;
  633. struct host1x *host1x;
  634. int err;
  635. mutex_lock(&devices_lock);
  636. list_for_each_entry(host1x, &devices, list) {
  637. err = host1x_del_client(host1x, client);
  638. if (!err) {
  639. mutex_unlock(&devices_lock);
  640. return 0;
  641. }
  642. }
  643. mutex_unlock(&devices_lock);
  644. mutex_lock(&clients_lock);
  645. list_for_each_entry(c, &clients, list) {
  646. if (c == client) {
  647. list_del_init(&c->list);
  648. break;
  649. }
  650. }
  651. mutex_unlock(&clients_lock);
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(host1x_client_unregister);
  655. int host1x_client_suspend(struct host1x_client *client)
  656. {
  657. int err = 0;
  658. mutex_lock(&client->lock);
  659. if (client->usecount == 1) {
  660. if (client->ops && client->ops->suspend) {
  661. err = client->ops->suspend(client);
  662. if (err < 0)
  663. goto unlock;
  664. }
  665. }
  666. client->usecount--;
  667. dev_dbg(client->dev, "use count: %u\n", client->usecount);
  668. if (client->parent) {
  669. err = host1x_client_suspend(client->parent);
  670. if (err < 0)
  671. goto resume;
  672. }
  673. goto unlock;
  674. resume:
  675. if (client->usecount == 0)
  676. if (client->ops && client->ops->resume)
  677. client->ops->resume(client);
  678. client->usecount++;
  679. unlock:
  680. mutex_unlock(&client->lock);
  681. return err;
  682. }
  683. EXPORT_SYMBOL(host1x_client_suspend);
  684. int host1x_client_resume(struct host1x_client *client)
  685. {
  686. int err = 0;
  687. mutex_lock(&client->lock);
  688. if (client->parent) {
  689. err = host1x_client_resume(client->parent);
  690. if (err < 0)
  691. goto unlock;
  692. }
  693. if (client->usecount == 0) {
  694. if (client->ops && client->ops->resume) {
  695. err = client->ops->resume(client);
  696. if (err < 0)
  697. goto suspend;
  698. }
  699. }
  700. client->usecount++;
  701. dev_dbg(client->dev, "use count: %u\n", client->usecount);
  702. goto unlock;
  703. suspend:
  704. if (client->parent)
  705. host1x_client_suspend(client->parent);
  706. unlock:
  707. mutex_unlock(&client->lock);
  708. return err;
  709. }
  710. EXPORT_SYMBOL(host1x_client_resume);