base.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2012 Red Hat 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. * Authors: Ben Skeggs
  23. */
  24. #include "priv.h"
  25. u64
  26. nvkm_timer_read(struct nvkm_timer *tmr)
  27. {
  28. return tmr->func->read(tmr);
  29. }
  30. void
  31. nvkm_timer_alarm_trigger(struct nvkm_timer *tmr)
  32. {
  33. struct nvkm_alarm *alarm, *atemp;
  34. unsigned long flags;
  35. LIST_HEAD(exec);
  36. /* Process pending alarms. */
  37. spin_lock_irqsave(&tmr->lock, flags);
  38. list_for_each_entry_safe(alarm, atemp, &tmr->alarms, head) {
  39. /* Have we hit the earliest alarm that hasn't gone off? */
  40. if (alarm->timestamp > nvkm_timer_read(tmr)) {
  41. /* Schedule it. If we didn't race, we're done. */
  42. tmr->func->alarm_init(tmr, alarm->timestamp);
  43. if (alarm->timestamp > nvkm_timer_read(tmr))
  44. break;
  45. }
  46. /* Move to completed list. We'll drop the lock before
  47. * executing the callback so it can reschedule itself.
  48. */
  49. list_del_init(&alarm->head);
  50. list_add(&alarm->exec, &exec);
  51. }
  52. /* Shut down interrupt if no more pending alarms. */
  53. if (list_empty(&tmr->alarms))
  54. tmr->func->alarm_fini(tmr);
  55. spin_unlock_irqrestore(&tmr->lock, flags);
  56. /* Execute completed callbacks. */
  57. list_for_each_entry_safe(alarm, atemp, &exec, exec) {
  58. list_del(&alarm->exec);
  59. alarm->func(alarm);
  60. }
  61. }
  62. void
  63. nvkm_timer_alarm(struct nvkm_timer *tmr, u32 nsec, struct nvkm_alarm *alarm)
  64. {
  65. struct nvkm_alarm *list;
  66. unsigned long flags;
  67. /* Remove alarm from pending list.
  68. *
  69. * This both protects against the corruption of the list,
  70. * and implements alarm rescheduling/cancellation.
  71. */
  72. spin_lock_irqsave(&tmr->lock, flags);
  73. list_del_init(&alarm->head);
  74. if (nsec) {
  75. /* Insert into pending list, ordered earliest to latest. */
  76. alarm->timestamp = nvkm_timer_read(tmr) + nsec;
  77. list_for_each_entry(list, &tmr->alarms, head) {
  78. if (list->timestamp > alarm->timestamp)
  79. break;
  80. }
  81. list_add_tail(&alarm->head, &list->head);
  82. /* Update HW if this is now the earliest alarm. */
  83. list = list_first_entry(&tmr->alarms, typeof(*list), head);
  84. if (list == alarm) {
  85. tmr->func->alarm_init(tmr, alarm->timestamp);
  86. /* This shouldn't happen if callers aren't stupid.
  87. *
  88. * Worst case scenario is that it'll take roughly
  89. * 4 seconds for the next alarm to trigger.
  90. */
  91. WARN_ON(alarm->timestamp <= nvkm_timer_read(tmr));
  92. }
  93. }
  94. spin_unlock_irqrestore(&tmr->lock, flags);
  95. }
  96. void
  97. nvkm_timer_alarm_cancel(struct nvkm_timer *tmr, struct nvkm_alarm *alarm)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&tmr->lock, flags);
  101. list_del_init(&alarm->head);
  102. spin_unlock_irqrestore(&tmr->lock, flags);
  103. }
  104. static void
  105. nvkm_timer_intr(struct nvkm_subdev *subdev)
  106. {
  107. struct nvkm_timer *tmr = nvkm_timer(subdev);
  108. tmr->func->intr(tmr);
  109. }
  110. static int
  111. nvkm_timer_fini(struct nvkm_subdev *subdev, bool suspend)
  112. {
  113. struct nvkm_timer *tmr = nvkm_timer(subdev);
  114. tmr->func->alarm_fini(tmr);
  115. return 0;
  116. }
  117. static int
  118. nvkm_timer_init(struct nvkm_subdev *subdev)
  119. {
  120. struct nvkm_timer *tmr = nvkm_timer(subdev);
  121. if (tmr->func->init)
  122. tmr->func->init(tmr);
  123. tmr->func->time(tmr, ktime_to_ns(ktime_get()));
  124. nvkm_timer_alarm_trigger(tmr);
  125. return 0;
  126. }
  127. static void *
  128. nvkm_timer_dtor(struct nvkm_subdev *subdev)
  129. {
  130. return nvkm_timer(subdev);
  131. }
  132. static const struct nvkm_subdev_func
  133. nvkm_timer = {
  134. .dtor = nvkm_timer_dtor,
  135. .init = nvkm_timer_init,
  136. .fini = nvkm_timer_fini,
  137. .intr = nvkm_timer_intr,
  138. };
  139. int
  140. nvkm_timer_new_(const struct nvkm_timer_func *func, struct nvkm_device *device,
  141. int index, struct nvkm_timer **ptmr)
  142. {
  143. struct nvkm_timer *tmr;
  144. if (!(tmr = *ptmr = kzalloc(sizeof(*tmr), GFP_KERNEL)))
  145. return -ENOMEM;
  146. nvkm_subdev_ctor(&nvkm_timer, device, index, &tmr->subdev);
  147. tmr->func = func;
  148. INIT_LIST_HEAD(&tmr->alarms);
  149. spin_lock_init(&tmr->lock);
  150. return 0;
  151. }