channel.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_channel_list *chlist,
  25. unsigned int num_channels)
  26. {
  27. chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
  28. GFP_KERNEL);
  29. if (!chlist->channels)
  30. return -ENOMEM;
  31. chlist->allocated_channels =
  32. kcalloc(BITS_TO_LONGS(num_channels), sizeof(unsigned long),
  33. GFP_KERNEL);
  34. if (!chlist->allocated_channels) {
  35. kfree(chlist->channels);
  36. return -ENOMEM;
  37. }
  38. bitmap_zero(chlist->allocated_channels, num_channels);
  39. return 0;
  40. }
  41. void host1x_channel_list_free(struct host1x_channel_list *chlist)
  42. {
  43. kfree(chlist->allocated_channels);
  44. kfree(chlist->channels);
  45. }
  46. int host1x_job_submit(struct host1x_job *job)
  47. {
  48. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  49. return host1x_hw_channel_submit(host, job);
  50. }
  51. EXPORT_SYMBOL(host1x_job_submit);
  52. struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
  53. {
  54. kref_get(&channel->refcount);
  55. return channel;
  56. }
  57. EXPORT_SYMBOL(host1x_channel_get);
  58. /**
  59. * host1x_channel_get_index() - Attempt to get channel reference by index
  60. * @host: Host1x device object
  61. * @index: Index of channel
  62. *
  63. * If channel number @index is currently allocated, increase its refcount
  64. * and return a pointer to it. Otherwise, return NULL.
  65. */
  66. struct host1x_channel *host1x_channel_get_index(struct host1x *host,
  67. unsigned int index)
  68. {
  69. struct host1x_channel *ch = &host->channel_list.channels[index];
  70. if (!kref_get_unless_zero(&ch->refcount))
  71. return NULL;
  72. return ch;
  73. }
  74. static void release_channel(struct kref *kref)
  75. {
  76. struct host1x_channel *channel =
  77. container_of(kref, struct host1x_channel, refcount);
  78. struct host1x *host = dev_get_drvdata(channel->dev->parent);
  79. struct host1x_channel_list *chlist = &host->channel_list;
  80. host1x_hw_cdma_stop(host, &channel->cdma);
  81. host1x_cdma_deinit(&channel->cdma);
  82. clear_bit(channel->id, chlist->allocated_channels);
  83. }
  84. void host1x_channel_put(struct host1x_channel *channel)
  85. {
  86. kref_put(&channel->refcount, release_channel);
  87. }
  88. EXPORT_SYMBOL(host1x_channel_put);
  89. static struct host1x_channel *acquire_unused_channel(struct host1x *host)
  90. {
  91. struct host1x_channel_list *chlist = &host->channel_list;
  92. unsigned int max_channels = host->info->nb_channels;
  93. unsigned int index;
  94. index = find_first_zero_bit(chlist->allocated_channels, max_channels);
  95. if (index >= max_channels) {
  96. dev_err(host->dev, "failed to find free channel\n");
  97. return NULL;
  98. }
  99. chlist->channels[index].id = index;
  100. set_bit(index, chlist->allocated_channels);
  101. return &chlist->channels[index];
  102. }
  103. /**
  104. * host1x_channel_request() - Allocate a channel
  105. * @device: Host1x unit this channel will be used to send commands to
  106. *
  107. * Allocates a new host1x channel for @device. May return NULL if CDMA
  108. * initialization fails.
  109. */
  110. struct host1x_channel *host1x_channel_request(struct device *dev)
  111. {
  112. struct host1x *host = dev_get_drvdata(dev->parent);
  113. struct host1x_channel_list *chlist = &host->channel_list;
  114. struct host1x_channel *channel;
  115. int err;
  116. channel = acquire_unused_channel(host);
  117. if (!channel)
  118. return NULL;
  119. kref_init(&channel->refcount);
  120. mutex_init(&channel->submitlock);
  121. channel->dev = dev;
  122. err = host1x_hw_channel_init(host, channel, channel->id);
  123. if (err < 0)
  124. goto fail;
  125. err = host1x_cdma_init(&channel->cdma);
  126. if (err < 0)
  127. goto fail;
  128. return channel;
  129. fail:
  130. clear_bit(channel->id, chlist->allocated_channels);
  131. dev_err(dev, "failed to initialize channel\n");
  132. return NULL;
  133. }
  134. EXPORT_SYMBOL(host1x_channel_request);