channel.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Channel
  4. *
  5. * Copyright (c) 2010-2013, NVIDIA Corporation.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include "channel.h"
  10. #include "dev.h"
  11. #include "job.h"
  12. /* Constructor for the host1x device list */
  13. int host1x_channel_list_init(struct host1x_channel_list *chlist,
  14. unsigned int num_channels)
  15. {
  16. chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
  17. GFP_KERNEL);
  18. if (!chlist->channels)
  19. return -ENOMEM;
  20. chlist->allocated_channels = bitmap_zalloc(num_channels, GFP_KERNEL);
  21. if (!chlist->allocated_channels) {
  22. kfree(chlist->channels);
  23. return -ENOMEM;
  24. }
  25. mutex_init(&chlist->lock);
  26. return 0;
  27. }
  28. void host1x_channel_list_free(struct host1x_channel_list *chlist)
  29. {
  30. bitmap_free(chlist->allocated_channels);
  31. kfree(chlist->channels);
  32. }
  33. int host1x_job_submit(struct host1x_job *job)
  34. {
  35. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  36. return host1x_hw_channel_submit(host, job);
  37. }
  38. EXPORT_SYMBOL(host1x_job_submit);
  39. struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
  40. {
  41. kref_get(&channel->refcount);
  42. return channel;
  43. }
  44. EXPORT_SYMBOL(host1x_channel_get);
  45. /**
  46. * host1x_channel_get_index() - Attempt to get channel reference by index
  47. * @host: Host1x device object
  48. * @index: Index of channel
  49. *
  50. * If channel number @index is currently allocated, increase its refcount
  51. * and return a pointer to it. Otherwise, return NULL.
  52. */
  53. struct host1x_channel *host1x_channel_get_index(struct host1x *host,
  54. unsigned int index)
  55. {
  56. struct host1x_channel *ch = &host->channel_list.channels[index];
  57. if (!kref_get_unless_zero(&ch->refcount))
  58. return NULL;
  59. return ch;
  60. }
  61. void host1x_channel_stop(struct host1x_channel *channel)
  62. {
  63. struct host1x *host = dev_get_drvdata(channel->dev->parent);
  64. host1x_hw_cdma_stop(host, &channel->cdma);
  65. }
  66. EXPORT_SYMBOL(host1x_channel_stop);
  67. /**
  68. * host1x_channel_stop_all() - disable CDMA on allocated channels
  69. * @host: host1x instance
  70. *
  71. * Stop CDMA on allocated channels
  72. */
  73. void host1x_channel_stop_all(struct host1x *host)
  74. {
  75. struct host1x_channel_list *chlist = &host->channel_list;
  76. int bit;
  77. mutex_lock(&chlist->lock);
  78. for_each_set_bit(bit, chlist->allocated_channels, host->info->nb_channels)
  79. host1x_channel_stop(&chlist->channels[bit]);
  80. mutex_unlock(&chlist->lock);
  81. }
  82. static void release_channel(struct kref *kref)
  83. {
  84. struct host1x_channel *channel =
  85. container_of(kref, struct host1x_channel, refcount);
  86. struct host1x *host = dev_get_drvdata(channel->dev->parent);
  87. struct host1x_channel_list *chlist = &host->channel_list;
  88. host1x_hw_cdma_stop(host, &channel->cdma);
  89. host1x_cdma_deinit(&channel->cdma);
  90. clear_bit(channel->id, chlist->allocated_channels);
  91. }
  92. void host1x_channel_put(struct host1x_channel *channel)
  93. {
  94. kref_put(&channel->refcount, release_channel);
  95. }
  96. EXPORT_SYMBOL(host1x_channel_put);
  97. static struct host1x_channel *acquire_unused_channel(struct host1x *host)
  98. {
  99. struct host1x_channel_list *chlist = &host->channel_list;
  100. unsigned int max_channels = host->info->nb_channels;
  101. unsigned int index;
  102. mutex_lock(&chlist->lock);
  103. index = find_first_zero_bit(chlist->allocated_channels, max_channels);
  104. if (index >= max_channels) {
  105. mutex_unlock(&chlist->lock);
  106. dev_err(host->dev, "failed to find free channel\n");
  107. return NULL;
  108. }
  109. chlist->channels[index].id = index;
  110. set_bit(index, chlist->allocated_channels);
  111. mutex_unlock(&chlist->lock);
  112. return &chlist->channels[index];
  113. }
  114. /**
  115. * host1x_channel_request() - Allocate a channel
  116. * @client: Host1x client this channel will be used to send commands to
  117. *
  118. * Allocates a new host1x channel for @client. May return NULL if CDMA
  119. * initialization fails.
  120. */
  121. struct host1x_channel *host1x_channel_request(struct host1x_client *client)
  122. {
  123. struct host1x *host = dev_get_drvdata(client->dev->parent);
  124. struct host1x_channel_list *chlist = &host->channel_list;
  125. struct host1x_channel *channel;
  126. int err;
  127. channel = acquire_unused_channel(host);
  128. if (!channel)
  129. return NULL;
  130. kref_init(&channel->refcount);
  131. mutex_init(&channel->submitlock);
  132. channel->client = client;
  133. channel->dev = client->dev;
  134. err = host1x_hw_channel_init(host, channel, channel->id);
  135. if (err < 0)
  136. goto fail;
  137. err = host1x_cdma_init(&channel->cdma);
  138. if (err < 0)
  139. goto fail;
  140. return channel;
  141. fail:
  142. clear_bit(channel->id, chlist->allocated_channels);
  143. dev_err(client->dev, "failed to initialize channel\n");
  144. return NULL;
  145. }
  146. EXPORT_SYMBOL(host1x_channel_request);