channel.c 4.4 KB

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