intr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Interrupt Management
  4. *
  5. * Copyright (c) 2010-2021, NVIDIA Corporation.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/interrupt.h>
  9. #include "dev.h"
  10. #include "fence.h"
  11. #include "intr.h"
  12. static void host1x_intr_add_fence_to_list(struct host1x_fence_list *list,
  13. struct host1x_syncpt_fence *fence)
  14. {
  15. struct host1x_syncpt_fence *fence_in_list;
  16. list_for_each_entry_reverse(fence_in_list, &list->list, list) {
  17. if ((s32)(fence_in_list->threshold - fence->threshold) <= 0) {
  18. /* Fence in list is before us, we can insert here */
  19. list_add(&fence->list, &fence_in_list->list);
  20. return;
  21. }
  22. }
  23. /* Add as first in list */
  24. list_add(&fence->list, &list->list);
  25. }
  26. static void host1x_intr_update_hw_state(struct host1x *host, struct host1x_syncpt *sp)
  27. {
  28. struct host1x_syncpt_fence *fence;
  29. if (!list_empty(&sp->fences.list)) {
  30. fence = list_first_entry(&sp->fences.list, struct host1x_syncpt_fence, list);
  31. host1x_hw_intr_set_syncpt_threshold(host, sp->id, fence->threshold);
  32. host1x_hw_intr_enable_syncpt_intr(host, sp->id);
  33. } else {
  34. host1x_hw_intr_disable_syncpt_intr(host, sp->id);
  35. }
  36. }
  37. void host1x_intr_add_fence_locked(struct host1x *host, struct host1x_syncpt_fence *fence)
  38. {
  39. struct host1x_fence_list *fence_list = &fence->sp->fences;
  40. INIT_LIST_HEAD(&fence->list);
  41. host1x_intr_add_fence_to_list(fence_list, fence);
  42. host1x_intr_update_hw_state(host, fence->sp);
  43. }
  44. bool host1x_intr_remove_fence(struct host1x *host, struct host1x_syncpt_fence *fence)
  45. {
  46. struct host1x_fence_list *fence_list = &fence->sp->fences;
  47. unsigned long irqflags;
  48. spin_lock_irqsave(&fence_list->lock, irqflags);
  49. if (list_empty(&fence->list)) {
  50. spin_unlock_irqrestore(&fence_list->lock, irqflags);
  51. return false;
  52. }
  53. list_del_init(&fence->list);
  54. host1x_intr_update_hw_state(host, fence->sp);
  55. spin_unlock_irqrestore(&fence_list->lock, irqflags);
  56. return true;
  57. }
  58. void host1x_intr_handle_interrupt(struct host1x *host, unsigned int id)
  59. {
  60. struct host1x_syncpt *sp = &host->syncpt[id];
  61. struct host1x_syncpt_fence *fence, *tmp;
  62. unsigned int value;
  63. value = host1x_syncpt_load(sp);
  64. spin_lock(&sp->fences.lock);
  65. list_for_each_entry_safe(fence, tmp, &sp->fences.list, list) {
  66. if (((value - fence->threshold) & 0x80000000U) != 0U) {
  67. /* Fence is not yet expired, we are done */
  68. break;
  69. }
  70. list_del_init(&fence->list);
  71. host1x_fence_signal(fence);
  72. }
  73. /* Re-enable interrupt if necessary */
  74. host1x_intr_update_hw_state(host, sp);
  75. spin_unlock(&sp->fences.lock);
  76. }
  77. int host1x_intr_init(struct host1x *host)
  78. {
  79. struct host1x_intr_irq_data *irq_data;
  80. unsigned int id;
  81. int i, err;
  82. for (id = 0; id < host1x_syncpt_nb_pts(host); ++id) {
  83. struct host1x_syncpt *syncpt = &host->syncpt[id];
  84. spin_lock_init(&syncpt->fences.lock);
  85. INIT_LIST_HEAD(&syncpt->fences.list);
  86. }
  87. irq_data = devm_kcalloc(host->dev, host->num_syncpt_irqs, sizeof(irq_data[0]), GFP_KERNEL);
  88. if (!irq_data)
  89. return -ENOMEM;
  90. host1x_hw_intr_disable_all_syncpt_intrs(host);
  91. for (i = 0; i < host->num_syncpt_irqs; i++) {
  92. irq_data[i].host = host;
  93. irq_data[i].offset = i;
  94. err = devm_request_irq(host->dev, host->syncpt_irqs[i],
  95. host->intr_op->isr, IRQF_SHARED,
  96. "host1x_syncpt", &irq_data[i]);
  97. if (err < 0)
  98. return err;
  99. }
  100. return 0;
  101. }
  102. void host1x_intr_deinit(struct host1x *host)
  103. {
  104. }
  105. void host1x_intr_start(struct host1x *host)
  106. {
  107. u32 hz = clk_get_rate(host->clk);
  108. int err;
  109. mutex_lock(&host->intr_mutex);
  110. err = host1x_hw_intr_init_host_sync(host, DIV_ROUND_UP(hz, 1000000));
  111. if (err) {
  112. mutex_unlock(&host->intr_mutex);
  113. return;
  114. }
  115. mutex_unlock(&host->intr_mutex);
  116. }
  117. void host1x_intr_stop(struct host1x *host)
  118. {
  119. host1x_hw_intr_disable_all_syncpt_intrs(host);
  120. }