bus.c 23 KB

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