base.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_move_tail(&alarm->head, &exec);
  50. }
  51. /* Shut down interrupt if no more pending alarms. */
  52. if (list_empty(&tmr->alarms))
  53. tmr->func->alarm_fini(tmr);
  54. spin_unlock_irqrestore(&tmr->lock, flags);
  55. /* Execute completed callbacks. */
  56. list_for_each_entry_safe(alarm, atemp, &exec, head) {
  57. list_del_init(&alarm->head);
  58. alarm->func(alarm);
  59. }
  60. }
  61. void
  62. nvkm_timer_alarm(struct nvkm_timer *tmr, u32 nsec, struct nvkm_alarm *alarm)
  63. {
  64. struct nvkm_alarm *list;
  65. unsigned long flags;
  66. /* Remove alarm from pending list.
  67. *
  68. * This both protects against the corruption of the list,
  69. * and implements alarm rescheduling/cancellation.
  70. */
  71. spin_lock_irqsave(&tmr->lock, flags);
  72. list_del_init(&alarm->head);
  73. if (nsec) {
  74. /* Insert into pending list, ordered earliest to latest. */
  75. alarm->timestamp = nvkm_timer_read(tmr) + nsec;
  76. list_for_each_entry(list, &tmr->alarms, head) {
  77. if (list->timestamp > alarm->timestamp)
  78. break;
  79. }
  80. list_add_tail(&alarm->head, &list->head);
  81. /* Update HW if this is now the earliest alarm. */
  82. list = list_first_entry(&tmr->alarms, typeof(*list), head);
  83. if (list == alarm) {
  84. tmr->func->alarm_init(tmr, alarm->timestamp);
  85. /* This shouldn't happen if callers aren't stupid.
  86. *
  87. * Worst case scenario is that it'll take roughly
  88. * 4 seconds for the next alarm to trigger.
  89. */
  90. WARN_ON(alarm->timestamp <= nvkm_timer_read(tmr));
  91. }
  92. }
  93. spin_unlock_irqrestore(&tmr->lock, flags);
  94. }
  95. void
  96. nvkm_timer_alarm_cancel(struct nvkm_timer *tmr, struct nvkm_alarm *alarm)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&tmr->lock, flags);
  100. list_del_init(&alarm->head);
  101. spin_unlock_irqrestore(&tmr->lock, flags);
  102. }
  103. static void
  104. nvkm_timer_intr(struct nvkm_subdev *subdev)
  105. {
  106. struct nvkm_timer *tmr = nvkm_timer(subdev);
  107. tmr->func->intr(tmr);
  108. }
  109. static int
  110. nvkm_timer_fini(struct nvkm_subdev *subdev, bool suspend)
  111. {
  112. struct nvkm_timer *tmr = nvkm_timer(subdev);
  113. tmr->func->alarm_fini(tmr);
  114. return 0;
  115. }
  116. static int
  117. nvkm_timer_init(struct nvkm_subdev *subdev)
  118. {
  119. struct nvkm_timer *tmr = nvkm_timer(subdev);
  120. if (tmr->func->init)
  121. tmr->func->init(tmr);
  122. tmr->func->time(tmr, ktime_to_ns(ktime_get()));
  123. nvkm_timer_alarm_trigger(tmr);
  124. return 0;
  125. }
  126. static void *
  127. nvkm_timer_dtor(struct nvkm_subdev *subdev)
  128. {
  129. return nvkm_timer(subdev);
  130. }
  131. static const struct nvkm_subdev_func
  132. nvkm_timer = {
  133. .dtor = nvkm_timer_dtor,
  134. .init = nvkm_timer_init,
  135. .fini = nvkm_timer_fini,
  136. .intr = nvkm_timer_intr,
  137. };
  138. int
  139. nvkm_timer_new_(const struct nvkm_timer_func *func, struct nvkm_device *device,
  140. int index, struct nvkm_timer **ptmr)
  141. {
  142. struct nvkm_timer *tmr;
  143. if (!(tmr = *ptmr = kzalloc(sizeof(*tmr), GFP_KERNEL)))
  144. return -ENOMEM;
  145. nvkm_subdev_ctor(&nvkm_timer, device, index, &tmr->subdev);
  146. tmr->func = func;
  147. INIT_LIST_HEAD(&tmr->alarms);
  148. spin_lock_init(&tmr->lock);
  149. return 0;
  150. }