bus.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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, struct device_driver *drv)
  289. {
  290. return strcmp(dev_name(dev), drv->name) == 0;
  291. }
  292. static int host1x_device_uevent(struct device *dev,
  293. struct kobj_uevent_env *env)
  294. {
  295. struct device_node *np = dev->parent->of_node;
  296. unsigned int count = 0;
  297. struct property *p;
  298. const char *compat;
  299. /*
  300. * This duplicates most of of_device_uevent(), but the latter cannot
  301. * be called from modules and operates on dev->of_node, which is not
  302. * available in this case.
  303. *
  304. * Note that this is really only needed for backwards compatibility
  305. * with libdrm, which parses this information from sysfs and will
  306. * fail if it can't find the OF_FULLNAME, specifically.
  307. */
  308. add_uevent_var(env, "OF_NAME=%pOFn", np);
  309. add_uevent_var(env, "OF_FULLNAME=%pOF", np);
  310. of_property_for_each_string(np, "compatible", p, compat) {
  311. add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
  312. count++;
  313. }
  314. add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
  315. return 0;
  316. }
  317. static int host1x_dma_configure(struct device *dev)
  318. {
  319. return of_dma_configure(dev, dev->of_node, true);
  320. }
  321. static const struct dev_pm_ops host1x_device_pm_ops = {
  322. .suspend = pm_generic_suspend,
  323. .resume = pm_generic_resume,
  324. .freeze = pm_generic_freeze,
  325. .thaw = pm_generic_thaw,
  326. .poweroff = pm_generic_poweroff,
  327. .restore = pm_generic_restore,
  328. };
  329. struct bus_type host1x_bus_type = {
  330. .name = "host1x",
  331. .match = host1x_device_match,
  332. .uevent = host1x_device_uevent,
  333. .dma_configure = host1x_dma_configure,
  334. .pm = &host1x_device_pm_ops,
  335. };
  336. static void __host1x_device_del(struct host1x_device *device)
  337. {
  338. struct host1x_subdev *subdev, *sd;
  339. struct host1x_client *client, *cl;
  340. mutex_lock(&device->subdevs_lock);
  341. /* unregister subdevices */
  342. list_for_each_entry_safe(subdev, sd, &device->active, list) {
  343. /*
  344. * host1x_subdev_unregister() will remove the client from
  345. * any lists, so we'll need to manually add it back to the
  346. * list of idle clients.
  347. *
  348. * XXX: Alternatively, perhaps don't remove the client from
  349. * any lists in host1x_subdev_unregister() and instead do
  350. * that explicitly from host1x_unregister_client()?
  351. */
  352. client = subdev->client;
  353. __host1x_subdev_unregister(device, subdev);
  354. /* add the client to the list of idle clients */
  355. mutex_lock(&clients_lock);
  356. list_add_tail(&client->list, &clients);
  357. mutex_unlock(&clients_lock);
  358. }
  359. /* remove subdevices */
  360. list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
  361. host1x_subdev_del(subdev);
  362. mutex_unlock(&device->subdevs_lock);
  363. /* move clients to idle list */
  364. mutex_lock(&clients_lock);
  365. mutex_lock(&device->clients_lock);
  366. list_for_each_entry_safe(client, cl, &device->clients, list)
  367. list_move_tail(&client->list, &clients);
  368. mutex_unlock(&device->clients_lock);
  369. mutex_unlock(&clients_lock);
  370. /* finally remove the device */
  371. list_del_init(&device->list);
  372. }
  373. static void host1x_device_release(struct device *dev)
  374. {
  375. struct host1x_device *device = to_host1x_device(dev);
  376. __host1x_device_del(device);
  377. kfree(device);
  378. }
  379. static int host1x_device_add(struct host1x *host1x,
  380. struct host1x_driver *driver)
  381. {
  382. struct host1x_client *client, *tmp;
  383. struct host1x_subdev *subdev;
  384. struct host1x_device *device;
  385. int err;
  386. device = kzalloc(sizeof(*device), GFP_KERNEL);
  387. if (!device)
  388. return -ENOMEM;
  389. device_initialize(&device->dev);
  390. mutex_init(&device->subdevs_lock);
  391. INIT_LIST_HEAD(&device->subdevs);
  392. INIT_LIST_HEAD(&device->active);
  393. mutex_init(&device->clients_lock);
  394. INIT_LIST_HEAD(&device->clients);
  395. INIT_LIST_HEAD(&device->list);
  396. device->driver = driver;
  397. device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
  398. device->dev.dma_mask = &device->dev.coherent_dma_mask;
  399. dev_set_name(&device->dev, "%s", driver->driver.name);
  400. device->dev.release = host1x_device_release;
  401. device->dev.bus = &host1x_bus_type;
  402. device->dev.parent = host1x->dev;
  403. of_dma_configure(&device->dev, host1x->dev->of_node, true);
  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. return 0;
  423. }
  424. /*
  425. * Removes a device by first unregistering any subdevices and then removing
  426. * itself from the list of devices.
  427. *
  428. * This function must be called with the host1x->devices_lock held.
  429. */
  430. static void host1x_device_del(struct host1x *host1x,
  431. struct host1x_device *device)
  432. {
  433. if (device->registered) {
  434. device->registered = false;
  435. device_del(&device->dev);
  436. }
  437. put_device(&device->dev);
  438. }
  439. static void host1x_attach_driver(struct host1x *host1x,
  440. struct host1x_driver *driver)
  441. {
  442. struct host1x_device *device;
  443. int err;
  444. mutex_lock(&host1x->devices_lock);
  445. list_for_each_entry(device, &host1x->devices, list) {
  446. if (device->driver == driver) {
  447. mutex_unlock(&host1x->devices_lock);
  448. return;
  449. }
  450. }
  451. err = host1x_device_add(host1x, driver);
  452. if (err < 0)
  453. dev_err(host1x->dev, "failed to allocate device: %d\n", err);
  454. mutex_unlock(&host1x->devices_lock);
  455. }
  456. static void host1x_detach_driver(struct host1x *host1x,
  457. struct host1x_driver *driver)
  458. {
  459. struct host1x_device *device, *tmp;
  460. mutex_lock(&host1x->devices_lock);
  461. list_for_each_entry_safe(device, tmp, &host1x->devices, list)
  462. if (device->driver == driver)
  463. host1x_device_del(host1x, device);
  464. mutex_unlock(&host1x->devices_lock);
  465. }
  466. static int host1x_devices_show(struct seq_file *s, void *data)
  467. {
  468. struct host1x *host1x = s->private;
  469. struct host1x_device *device;
  470. mutex_lock(&host1x->devices_lock);
  471. list_for_each_entry(device, &host1x->devices, list) {
  472. struct host1x_subdev *subdev;
  473. seq_printf(s, "%s\n", dev_name(&device->dev));
  474. mutex_lock(&device->subdevs_lock);
  475. list_for_each_entry(subdev, &device->active, list)
  476. seq_printf(s, " %pOFf: %s\n", subdev->np,
  477. dev_name(subdev->client->dev));
  478. list_for_each_entry(subdev, &device->subdevs, list)
  479. seq_printf(s, " %pOFf:\n", subdev->np);
  480. mutex_unlock(&device->subdevs_lock);
  481. }
  482. mutex_unlock(&host1x->devices_lock);
  483. return 0;
  484. }
  485. DEFINE_SHOW_ATTRIBUTE(host1x_devices);
  486. /**
  487. * host1x_register() - register a host1x controller
  488. * @host1x: host1x controller
  489. *
  490. * The host1x controller driver uses this to register a host1x controller with
  491. * the infrastructure. Note that all Tegra SoC generations have only ever come
  492. * with a single host1x instance, so this function is somewhat academic.
  493. */
  494. int host1x_register(struct host1x *host1x)
  495. {
  496. struct host1x_driver *driver;
  497. mutex_lock(&devices_lock);
  498. list_add_tail(&host1x->list, &devices);
  499. mutex_unlock(&devices_lock);
  500. mutex_lock(&drivers_lock);
  501. list_for_each_entry(driver, &drivers, list)
  502. host1x_attach_driver(host1x, driver);
  503. mutex_unlock(&drivers_lock);
  504. debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
  505. &host1x_devices_fops);
  506. return 0;
  507. }
  508. /**
  509. * host1x_unregister() - unregister a host1x controller
  510. * @host1x: host1x controller
  511. *
  512. * The host1x controller driver uses this to remove a host1x controller from
  513. * the infrastructure.
  514. */
  515. int host1x_unregister(struct host1x *host1x)
  516. {
  517. struct host1x_driver *driver;
  518. mutex_lock(&drivers_lock);
  519. list_for_each_entry(driver, &drivers, list)
  520. host1x_detach_driver(host1x, driver);
  521. mutex_unlock(&drivers_lock);
  522. mutex_lock(&devices_lock);
  523. list_del_init(&host1x->list);
  524. mutex_unlock(&devices_lock);
  525. return 0;
  526. }
  527. static int host1x_device_probe(struct device *dev)
  528. {
  529. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  530. struct host1x_device *device = to_host1x_device(dev);
  531. if (driver->probe)
  532. return driver->probe(device);
  533. return 0;
  534. }
  535. static int host1x_device_remove(struct device *dev)
  536. {
  537. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  538. struct host1x_device *device = to_host1x_device(dev);
  539. if (driver->remove)
  540. return driver->remove(device);
  541. return 0;
  542. }
  543. static void host1x_device_shutdown(struct device *dev)
  544. {
  545. struct host1x_driver *driver = to_host1x_driver(dev->driver);
  546. struct host1x_device *device = to_host1x_device(dev);
  547. if (driver->shutdown)
  548. driver->shutdown(device);
  549. }
  550. /**
  551. * host1x_driver_register_full() - register a host1x driver
  552. * @driver: host1x driver
  553. * @owner: owner module
  554. *
  555. * Drivers for host1x logical devices call this function to register a driver
  556. * with the infrastructure. Note that since these drive logical devices, the
  557. * registration of the driver actually triggers tho logical device creation.
  558. * A logical device will be created for each host1x instance.
  559. */
  560. int host1x_driver_register_full(struct host1x_driver *driver,
  561. struct module *owner)
  562. {
  563. struct host1x *host1x;
  564. INIT_LIST_HEAD(&driver->list);
  565. mutex_lock(&drivers_lock);
  566. list_add_tail(&driver->list, &drivers);
  567. mutex_unlock(&drivers_lock);
  568. mutex_lock(&devices_lock);
  569. list_for_each_entry(host1x, &devices, list)
  570. host1x_attach_driver(host1x, driver);
  571. mutex_unlock(&devices_lock);
  572. driver->driver.bus = &host1x_bus_type;
  573. driver->driver.owner = owner;
  574. driver->driver.probe = host1x_device_probe;
  575. driver->driver.remove = host1x_device_remove;
  576. driver->driver.shutdown = host1x_device_shutdown;
  577. return driver_register(&driver->driver);
  578. }
  579. EXPORT_SYMBOL(host1x_driver_register_full);
  580. /**
  581. * host1x_driver_unregister() - unregister a host1x driver
  582. * @driver: host1x driver
  583. *
  584. * Unbinds the driver from each of the host1x logical devices that it is
  585. * bound to, effectively removing the subsystem devices that they represent.
  586. */
  587. void host1x_driver_unregister(struct host1x_driver *driver)
  588. {
  589. struct host1x *host1x;
  590. driver_unregister(&driver->driver);
  591. mutex_lock(&devices_lock);
  592. list_for_each_entry(host1x, &devices, list)
  593. host1x_detach_driver(host1x, driver);
  594. mutex_unlock(&devices_lock);
  595. mutex_lock(&drivers_lock);
  596. list_del_init(&driver->list);
  597. mutex_unlock(&drivers_lock);
  598. }
  599. EXPORT_SYMBOL(host1x_driver_unregister);
  600. /**
  601. * __host1x_client_init() - initialize a host1x client
  602. * @client: host1x client
  603. * @key: lock class key for the client-specific mutex
  604. */
  605. void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key)
  606. {
  607. host1x_bo_cache_init(&client->cache);
  608. INIT_LIST_HEAD(&client->list);
  609. __mutex_init(&client->lock, "host1x client lock", key);
  610. client->usecount = 0;
  611. }
  612. EXPORT_SYMBOL(__host1x_client_init);
  613. /**
  614. * host1x_client_exit() - uninitialize a host1x client
  615. * @client: host1x client
  616. */
  617. void host1x_client_exit(struct host1x_client *client)
  618. {
  619. mutex_destroy(&client->lock);
  620. }
  621. EXPORT_SYMBOL(host1x_client_exit);
  622. /**
  623. * __host1x_client_register() - register a host1x client
  624. * @client: host1x client
  625. *
  626. * Registers a host1x client with each host1x controller instance. Note that
  627. * each client will only match their parent host1x controller and will only be
  628. * associated with that instance. Once all clients have been registered with
  629. * their parent host1x controller, the infrastructure will set up the logical
  630. * device and call host1x_device_init(), which will in turn call each client's
  631. * &host1x_client_ops.init implementation.
  632. */
  633. int __host1x_client_register(struct host1x_client *client)
  634. {
  635. struct host1x *host1x;
  636. int err;
  637. mutex_lock(&devices_lock);
  638. list_for_each_entry(host1x, &devices, list) {
  639. err = host1x_add_client(host1x, client);
  640. if (!err) {
  641. mutex_unlock(&devices_lock);
  642. return 0;
  643. }
  644. }
  645. mutex_unlock(&devices_lock);
  646. mutex_lock(&clients_lock);
  647. list_add_tail(&client->list, &clients);
  648. mutex_unlock(&clients_lock);
  649. return 0;
  650. }
  651. EXPORT_SYMBOL(__host1x_client_register);
  652. /**
  653. * host1x_client_unregister() - unregister a host1x client
  654. * @client: host1x client
  655. *
  656. * Removes a host1x client from its host1x controller instance. If a logical
  657. * device has already been initialized, it will be torn down.
  658. */
  659. int host1x_client_unregister(struct host1x_client *client)
  660. {
  661. struct host1x_client *c;
  662. struct host1x *host1x;
  663. int err;
  664. mutex_lock(&devices_lock);
  665. list_for_each_entry(host1x, &devices, list) {
  666. err = host1x_del_client(host1x, client);
  667. if (!err) {
  668. mutex_unlock(&devices_lock);
  669. return 0;
  670. }
  671. }
  672. mutex_unlock(&devices_lock);
  673. mutex_lock(&clients_lock);
  674. list_for_each_entry(c, &clients, list) {
  675. if (c == client) {
  676. list_del_init(&c->list);
  677. break;
  678. }
  679. }
  680. mutex_unlock(&clients_lock);
  681. host1x_bo_cache_destroy(&client->cache);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(host1x_client_unregister);
  685. int host1x_client_suspend(struct host1x_client *client)
  686. {
  687. int err = 0;
  688. mutex_lock(&client->lock);
  689. if (client->usecount == 1) {
  690. if (client->ops && client->ops->suspend) {
  691. err = client->ops->suspend(client);
  692. if (err < 0)
  693. goto unlock;
  694. }
  695. }
  696. client->usecount--;
  697. dev_dbg(client->dev, "use count: %u\n", client->usecount);
  698. if (client->parent) {
  699. err = host1x_client_suspend(client->parent);
  700. if (err < 0)
  701. goto resume;
  702. }
  703. goto unlock;
  704. resume:
  705. if (client->usecount == 0)
  706. if (client->ops && client->ops->resume)
  707. client->ops->resume(client);
  708. client->usecount++;
  709. unlock:
  710. mutex_unlock(&client->lock);
  711. return err;
  712. }
  713. EXPORT_SYMBOL(host1x_client_suspend);
  714. int host1x_client_resume(struct host1x_client *client)
  715. {
  716. int err = 0;
  717. mutex_lock(&client->lock);
  718. if (client->parent) {
  719. err = host1x_client_resume(client->parent);
  720. if (err < 0)
  721. goto unlock;
  722. }
  723. if (client->usecount == 0) {
  724. if (client->ops && client->ops->resume) {
  725. err = client->ops->resume(client);
  726. if (err < 0)
  727. goto suspend;
  728. }
  729. }
  730. client->usecount++;
  731. dev_dbg(client->dev, "use count: %u\n", client->usecount);
  732. goto unlock;
  733. suspend:
  734. if (client->parent)
  735. host1x_client_suspend(client->parent);
  736. unlock:
  737. mutex_unlock(&client->lock);
  738. return err;
  739. }
  740. EXPORT_SYMBOL(host1x_client_resume);
  741. struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo,
  742. enum dma_data_direction dir,
  743. struct host1x_bo_cache *cache)
  744. {
  745. struct host1x_bo_mapping *mapping;
  746. if (cache) {
  747. mutex_lock(&cache->lock);
  748. list_for_each_entry(mapping, &cache->mappings, entry) {
  749. if (mapping->bo == bo && mapping->direction == dir) {
  750. kref_get(&mapping->ref);
  751. goto unlock;
  752. }
  753. }
  754. }
  755. mapping = bo->ops->pin(dev, bo, dir);
  756. if (IS_ERR(mapping))
  757. goto unlock;
  758. spin_lock(&mapping->bo->lock);
  759. list_add_tail(&mapping->list, &bo->mappings);
  760. spin_unlock(&mapping->bo->lock);
  761. if (cache) {
  762. INIT_LIST_HEAD(&mapping->entry);
  763. mapping->cache = cache;
  764. list_add_tail(&mapping->entry, &cache->mappings);
  765. /* bump reference count to track the copy in the cache */
  766. kref_get(&mapping->ref);
  767. }
  768. unlock:
  769. if (cache)
  770. mutex_unlock(&cache->lock);
  771. return mapping;
  772. }
  773. EXPORT_SYMBOL(host1x_bo_pin);
  774. static void __host1x_bo_unpin(struct kref *ref)
  775. {
  776. struct host1x_bo_mapping *mapping = to_host1x_bo_mapping(ref);
  777. /*
  778. * When the last reference of the mapping goes away, make sure to remove the mapping from
  779. * the cache.
  780. */
  781. if (mapping->cache)
  782. list_del(&mapping->entry);
  783. spin_lock(&mapping->bo->lock);
  784. list_del(&mapping->list);
  785. spin_unlock(&mapping->bo->lock);
  786. mapping->bo->ops->unpin(mapping);
  787. }
  788. void host1x_bo_unpin(struct host1x_bo_mapping *mapping)
  789. {
  790. struct host1x_bo_cache *cache = mapping->cache;
  791. if (cache)
  792. mutex_lock(&cache->lock);
  793. kref_put(&mapping->ref, __host1x_bo_unpin);
  794. if (cache)
  795. mutex_unlock(&cache->lock);
  796. }
  797. EXPORT_SYMBOL(host1x_bo_unpin);