channel.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Tegra host1x Channel
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include "channel.h"
  21. #include "dev.h"
  22. #include "job.h"
  23. /* Constructor for the host1x device list */
  24. int host1x_channel_list_init(struct host1x *host)
  25. {
  26. INIT_LIST_HEAD(&host->chlist.list);
  27. mutex_init(&host->chlist_mutex);
  28. if (host->info->nb_channels > BITS_PER_LONG) {
  29. WARN(1, "host1x hardware has more channels than supported by the driver\n");
  30. return -ENOSYS;
  31. }
  32. return 0;
  33. }
  34. int host1x_job_submit(struct host1x_job *job)
  35. {
  36. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  37. return host1x_hw_channel_submit(host, job);
  38. }
  39. EXPORT_SYMBOL(host1x_job_submit);
  40. struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
  41. {
  42. int err = 0;
  43. mutex_lock(&channel->reflock);
  44. if (channel->refcount == 0)
  45. err = host1x_cdma_init(&channel->cdma);
  46. if (!err)
  47. channel->refcount++;
  48. mutex_unlock(&channel->reflock);
  49. return err ? NULL : channel;
  50. }
  51. EXPORT_SYMBOL(host1x_channel_get);
  52. void host1x_channel_put(struct host1x_channel *channel)
  53. {
  54. mutex_lock(&channel->reflock);
  55. if (channel->refcount == 1) {
  56. struct host1x *host = dev_get_drvdata(channel->dev->parent);
  57. host1x_hw_cdma_stop(host, &channel->cdma);
  58. host1x_cdma_deinit(&channel->cdma);
  59. }
  60. channel->refcount--;
  61. mutex_unlock(&channel->reflock);
  62. }
  63. EXPORT_SYMBOL(host1x_channel_put);
  64. struct host1x_channel *host1x_channel_request(struct device *dev)
  65. {
  66. struct host1x *host = dev_get_drvdata(dev->parent);
  67. int max_channels = host->info->nb_channels;
  68. struct host1x_channel *channel = NULL;
  69. int index, err;
  70. mutex_lock(&host->chlist_mutex);
  71. index = find_first_zero_bit(&host->allocated_channels, max_channels);
  72. if (index >= max_channels)
  73. goto fail;
  74. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  75. if (!channel)
  76. goto fail;
  77. err = host1x_hw_channel_init(host, channel, index);
  78. if (err < 0)
  79. goto fail;
  80. /* Link device to host1x_channel */
  81. channel->dev = dev;
  82. /* Add to channel list */
  83. list_add_tail(&channel->list, &host->chlist.list);
  84. host->allocated_channels |= BIT(index);
  85. mutex_unlock(&host->chlist_mutex);
  86. return channel;
  87. fail:
  88. dev_err(dev, "failed to init channel\n");
  89. kfree(channel);
  90. mutex_unlock(&host->chlist_mutex);
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL(host1x_channel_request);
  94. void host1x_channel_free(struct host1x_channel *channel)
  95. {
  96. struct host1x *host = dev_get_drvdata(channel->dev->parent);
  97. host->allocated_channels &= ~BIT(channel->id);
  98. list_del(&channel->list);
  99. kfree(channel);
  100. }
  101. EXPORT_SYMBOL(host1x_channel_free);