amdgpu_ih.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_ih.h"
  26. /**
  27. * amdgpu_ih_ring_init - initialize the IH state
  28. *
  29. * @adev: amdgpu_device pointer
  30. * @ih: ih ring to initialize
  31. * @ring_size: ring size to allocate
  32. * @use_bus_addr: true when we can use dma_alloc_coherent
  33. *
  34. * Initializes the IH state and allocates a buffer
  35. * for the IH ring buffer.
  36. * Returns 0 for success, errors for failure.
  37. */
  38. int amdgpu_ih_ring_init(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
  39. unsigned ring_size, bool use_bus_addr)
  40. {
  41. u32 rb_bufsz;
  42. int r;
  43. /* Align ring size */
  44. rb_bufsz = order_base_2(ring_size / 4);
  45. ring_size = (1 << rb_bufsz) * 4;
  46. ih->ring_size = ring_size;
  47. ih->ptr_mask = ih->ring_size - 1;
  48. ih->rptr = 0;
  49. ih->use_bus_addr = use_bus_addr;
  50. if (use_bus_addr) {
  51. if (ih->ring)
  52. return 0;
  53. /* add 8 bytes for the rptr/wptr shadows and
  54. * add them to the end of the ring allocation.
  55. */
  56. ih->ring = dma_alloc_coherent(adev->dev, ih->ring_size + 8,
  57. &ih->rb_dma_addr, GFP_KERNEL);
  58. if (ih->ring == NULL)
  59. return -ENOMEM;
  60. memset((void *)ih->ring, 0, ih->ring_size + 8);
  61. ih->wptr_offs = (ih->ring_size / 4) + 0;
  62. ih->rptr_offs = (ih->ring_size / 4) + 1;
  63. } else {
  64. r = amdgpu_device_wb_get(adev, &ih->wptr_offs);
  65. if (r)
  66. return r;
  67. r = amdgpu_device_wb_get(adev, &ih->rptr_offs);
  68. if (r) {
  69. amdgpu_device_wb_free(adev, ih->wptr_offs);
  70. return r;
  71. }
  72. r = amdgpu_bo_create_kernel(adev, ih->ring_size, PAGE_SIZE,
  73. AMDGPU_GEM_DOMAIN_GTT,
  74. &ih->ring_obj, &ih->gpu_addr,
  75. (void **)&ih->ring);
  76. if (r) {
  77. amdgpu_device_wb_free(adev, ih->rptr_offs);
  78. amdgpu_device_wb_free(adev, ih->wptr_offs);
  79. return r;
  80. }
  81. }
  82. return 0;
  83. }
  84. /**
  85. * amdgpu_ih_ring_fini - tear down the IH state
  86. *
  87. * @adev: amdgpu_device pointer
  88. * @ih: ih ring to tear down
  89. *
  90. * Tears down the IH state and frees buffer
  91. * used for the IH ring buffer.
  92. */
  93. void amdgpu_ih_ring_fini(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
  94. {
  95. if (ih->use_bus_addr) {
  96. if (!ih->ring)
  97. return;
  98. /* add 8 bytes for the rptr/wptr shadows and
  99. * add them to the end of the ring allocation.
  100. */
  101. dma_free_coherent(adev->dev, ih->ring_size + 8,
  102. (void *)ih->ring, ih->rb_dma_addr);
  103. ih->ring = NULL;
  104. } else {
  105. amdgpu_bo_free_kernel(&ih->ring_obj, &ih->gpu_addr,
  106. (void **)&ih->ring);
  107. amdgpu_device_wb_free(adev, ih->wptr_offs);
  108. amdgpu_device_wb_free(adev, ih->rptr_offs);
  109. }
  110. }
  111. /**
  112. * amdgpu_ih_process - interrupt handler
  113. *
  114. * @adev: amdgpu_device pointer
  115. * @ih: ih ring to process
  116. *
  117. * Interrupt hander (VI), walk the IH ring.
  118. * Returns irq process return code.
  119. */
  120. int amdgpu_ih_process(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
  121. void (*callback)(struct amdgpu_device *adev,
  122. struct amdgpu_ih_ring *ih))
  123. {
  124. u32 wptr;
  125. if (!ih->enabled || adev->shutdown)
  126. return IRQ_NONE;
  127. wptr = amdgpu_ih_get_wptr(adev);
  128. restart_ih:
  129. /* is somebody else already processing irqs? */
  130. if (atomic_xchg(&ih->lock, 1))
  131. return IRQ_NONE;
  132. DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, ih->rptr, wptr);
  133. /* Order reading of wptr vs. reading of IH ring data */
  134. rmb();
  135. while (ih->rptr != wptr) {
  136. callback(adev, ih);
  137. ih->rptr &= ih->ptr_mask;
  138. }
  139. amdgpu_ih_set_rptr(adev);
  140. atomic_set(&ih->lock, 0);
  141. /* make sure wptr hasn't changed while processing */
  142. wptr = amdgpu_ih_get_wptr(adev);
  143. if (wptr != ih->rptr)
  144. goto restart_ih;
  145. return IRQ_HANDLED;
  146. }