channel.c 3.7 KB

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