bus.c 23 KB

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