channel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright © 2012, 2013 Thierry Reding
  3. * Copyright © 2013 Erik Faye-Lund
  4. * Copyright © 2014-2021 NVIDIA Corporation
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. # include "config.h"
  26. #endif
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <sys/ioctl.h>
  30. #include "private.h"
  31. drm_public int
  32. drm_tegra_channel_open(struct drm_tegra *drm,
  33. enum drm_tegra_class client,
  34. struct drm_tegra_channel **channelp)
  35. {
  36. struct drm_tegra_channel_open args;
  37. struct drm_tegra_channel *channel;
  38. enum host1x_class class;
  39. int err;
  40. switch (client) {
  41. case DRM_TEGRA_HOST1X:
  42. class = HOST1X_CLASS_HOST1X;
  43. break;
  44. case DRM_TEGRA_GR2D:
  45. class = HOST1X_CLASS_GR2D;
  46. break;
  47. case DRM_TEGRA_GR3D:
  48. class = HOST1X_CLASS_GR3D;
  49. break;
  50. case DRM_TEGRA_VIC:
  51. class = HOST1X_CLASS_VIC;
  52. break;
  53. default:
  54. return -EINVAL;
  55. }
  56. channel = calloc(1, sizeof(*channel));
  57. if (!channel)
  58. return -ENOMEM;
  59. channel->drm = drm;
  60. memset(&args, 0, sizeof(args));
  61. args.host1x_class = class;
  62. err = ioctl(drm->fd, DRM_IOCTL_TEGRA_CHANNEL_OPEN, &args);
  63. if (err < 0) {
  64. free(channel);
  65. return -errno;
  66. }
  67. channel->context = args.context;
  68. channel->version = args.version;
  69. channel->capabilities = args.capabilities;
  70. channel->class = class;
  71. switch (channel->version) {
  72. case 0x20:
  73. case 0x30:
  74. case 0x35:
  75. case 0x40:
  76. case 0x21:
  77. channel->cond_shift = 8;
  78. break;
  79. case 0x18:
  80. case 0x19:
  81. channel->cond_shift = 10;
  82. break;
  83. default:
  84. return -ENOTSUP;
  85. }
  86. *channelp = channel;
  87. return 0;
  88. }
  89. drm_public int drm_tegra_channel_close(struct drm_tegra_channel *channel)
  90. {
  91. struct drm_tegra_channel_close args;
  92. struct drm_tegra *drm;
  93. int err;
  94. if (!channel)
  95. return -EINVAL;
  96. drm = channel->drm;
  97. memset(&args, 0, sizeof(args));
  98. args.context = channel->context;
  99. err = ioctl(drm->fd, DRM_IOCTL_TEGRA_CHANNEL_CLOSE, &args);
  100. if (err < 0)
  101. return -errno;
  102. free(channel);
  103. return 0;
  104. }
  105. drm_public unsigned int
  106. drm_tegra_channel_get_version(struct drm_tegra_channel *channel)
  107. {
  108. return channel->version;
  109. }
  110. drm_public int
  111. drm_tegra_channel_map(struct drm_tegra_channel *channel,
  112. struct drm_tegra_bo *bo, uint32_t flags,
  113. struct drm_tegra_mapping **mapp)
  114. {
  115. struct drm_tegra *drm = channel->drm;
  116. struct drm_tegra_channel_map args;
  117. struct drm_tegra_mapping *map;
  118. int err;
  119. if (!drm || !bo || !mapp)
  120. return -EINVAL;
  121. map = calloc(1, sizeof(*map));
  122. if (!map)
  123. return -ENOMEM;
  124. memset(&args, 0, sizeof(args));
  125. args.context = channel->context;
  126. args.handle = bo->handle;
  127. args.flags = flags;
  128. err = ioctl(drm->fd, DRM_IOCTL_TEGRA_CHANNEL_MAP, &args);
  129. if (err < 0) {
  130. free(map);
  131. return -errno;
  132. }
  133. map->channel = channel;
  134. map->id = args.mapping;
  135. *mapp = map;
  136. return 0;
  137. }
  138. drm_public int
  139. drm_tegra_channel_unmap(struct drm_tegra_mapping *map)
  140. {
  141. struct drm_tegra_channel *channel = map->channel;
  142. struct drm_tegra *drm = channel->drm;
  143. struct drm_tegra_channel_unmap args;
  144. int err;
  145. if (!channel || !map)
  146. return -EINVAL;
  147. memset(&args, 0, sizeof(args));
  148. args.context = channel->context;
  149. args.mapping = map->id;
  150. err = ioctl(drm->fd, DRM_IOCTL_TEGRA_CHANNEL_UNMAP, &args);
  151. if (err < 0)
  152. return -errno;
  153. free(map);
  154. return 0;
  155. }