channel.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 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. static void release_channel(struct kref *kref)
  67. {
  68. struct host1x_channel *channel =
  69. container_of(kref, struct host1x_channel, refcount);
  70. struct host1x *host = dev_get_drvdata(channel->dev->parent);
  71. struct host1x_channel_list *chlist = &host->channel_list;
  72. host1x_hw_cdma_stop(host, &channel->cdma);
  73. host1x_cdma_deinit(&channel->cdma);
  74. clear_bit(channel->id, chlist->allocated_channels);
  75. }
  76. void host1x_channel_put(struct host1x_channel *channel)
  77. {
  78. kref_put(&channel->refcount, release_channel);
  79. }
  80. EXPORT_SYMBOL(host1x_channel_put);
  81. static struct host1x_channel *acquire_unused_channel(struct host1x *host)
  82. {
  83. struct host1x_channel_list *chlist = &host->channel_list;
  84. unsigned int max_channels = host->info->nb_channels;
  85. unsigned int index;
  86. index = find_first_zero_bit(chlist->allocated_channels, max_channels);
  87. if (index >= max_channels) {
  88. dev_err(host->dev, "failed to find free channel\n");
  89. return NULL;
  90. }
  91. chlist->channels[index].id = index;
  92. set_bit(index, chlist->allocated_channels);
  93. return &chlist->channels[index];
  94. }
  95. /**
  96. * host1x_channel_request() - Allocate a channel
  97. * @client: Host1x client this channel will be used to send commands to
  98. *
  99. * Allocates a new host1x channel for @client. May return NULL if CDMA
  100. * initialization fails.
  101. */
  102. struct host1x_channel *host1x_channel_request(struct host1x_client *client)
  103. {
  104. struct host1x *host = dev_get_drvdata(client->dev->parent);
  105. struct host1x_channel_list *chlist = &host->channel_list;
  106. struct host1x_channel *channel;
  107. int err;
  108. channel = acquire_unused_channel(host);
  109. if (!channel)
  110. return NULL;
  111. kref_init(&channel->refcount);
  112. mutex_init(&channel->submitlock);
  113. channel->client = client;
  114. channel->dev = client->dev;
  115. err = host1x_hw_channel_init(host, channel, channel->id);
  116. if (err < 0)
  117. goto fail;
  118. err = host1x_cdma_init(&channel->cdma);
  119. if (err < 0)
  120. goto fail;
  121. return channel;
  122. fail:
  123. clear_bit(channel->id, chlist->allocated_channels);
  124. dev_err(client->dev, "failed to initialize channel\n");
  125. return NULL;
  126. }
  127. EXPORT_SYMBOL(host1x_channel_request);