base.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /* move any due alarms off the pending list */
  37. spin_lock_irqsave(&tmr->lock, flags);
  38. list_for_each_entry_safe(alarm, atemp, &tmr->alarms, head) {
  39. if (alarm->timestamp <= nvkm_timer_read(tmr))
  40. list_move_tail(&alarm->head, &exec);
  41. }
  42. /* reschedule interrupt for next alarm time */
  43. if (!list_empty(&tmr->alarms)) {
  44. alarm = list_first_entry(&tmr->alarms, typeof(*alarm), head);
  45. tmr->func->alarm_init(tmr, alarm->timestamp);
  46. } else {
  47. tmr->func->alarm_fini(tmr);
  48. }
  49. spin_unlock_irqrestore(&tmr->lock, flags);
  50. /* execute any pending alarm handlers */
  51. list_for_each_entry_safe(alarm, atemp, &exec, head) {
  52. list_del_init(&alarm->head);
  53. alarm->func(alarm);
  54. }
  55. }
  56. void
  57. nvkm_timer_alarm(struct nvkm_timer *tmr, u32 nsec, struct nvkm_alarm *alarm)
  58. {
  59. struct nvkm_alarm *list;
  60. unsigned long flags;
  61. alarm->timestamp = nvkm_timer_read(tmr) + nsec;
  62. /* append new alarm to list, in soonest-alarm-first order */
  63. spin_lock_irqsave(&tmr->lock, flags);
  64. if (!nsec) {
  65. if (!list_empty(&alarm->head))
  66. list_del(&alarm->head);
  67. } else {
  68. list_for_each_entry(list, &tmr->alarms, head) {
  69. if (list->timestamp > alarm->timestamp)
  70. break;
  71. }
  72. list_add_tail(&alarm->head, &list->head);
  73. }
  74. spin_unlock_irqrestore(&tmr->lock, flags);
  75. /* process pending alarms */
  76. nvkm_timer_alarm_trigger(tmr);
  77. }
  78. void
  79. nvkm_timer_alarm_cancel(struct nvkm_timer *tmr, struct nvkm_alarm *alarm)
  80. {
  81. unsigned long flags;
  82. spin_lock_irqsave(&tmr->lock, flags);
  83. list_del_init(&alarm->head);
  84. spin_unlock_irqrestore(&tmr->lock, flags);
  85. }
  86. static void
  87. nvkm_timer_intr(struct nvkm_subdev *subdev)
  88. {
  89. struct nvkm_timer *tmr = nvkm_timer(subdev);
  90. tmr->func->intr(tmr);
  91. }
  92. static int
  93. nvkm_timer_fini(struct nvkm_subdev *subdev, bool suspend)
  94. {
  95. struct nvkm_timer *tmr = nvkm_timer(subdev);
  96. tmr->func->alarm_fini(tmr);
  97. return 0;
  98. }
  99. static int
  100. nvkm_timer_init(struct nvkm_subdev *subdev)
  101. {
  102. struct nvkm_timer *tmr = nvkm_timer(subdev);
  103. if (tmr->func->init)
  104. tmr->func->init(tmr);
  105. tmr->func->time(tmr, ktime_to_ns(ktime_get()));
  106. nvkm_timer_alarm_trigger(tmr);
  107. return 0;
  108. }
  109. static void *
  110. nvkm_timer_dtor(struct nvkm_subdev *subdev)
  111. {
  112. return nvkm_timer(subdev);
  113. }
  114. static const struct nvkm_subdev_func
  115. nvkm_timer = {
  116. .dtor = nvkm_timer_dtor,
  117. .init = nvkm_timer_init,
  118. .fini = nvkm_timer_fini,
  119. .intr = nvkm_timer_intr,
  120. };
  121. int
  122. nvkm_timer_new_(const struct nvkm_timer_func *func, struct nvkm_device *device,
  123. int index, struct nvkm_timer **ptmr)
  124. {
  125. struct nvkm_timer *tmr;
  126. if (!(tmr = *ptmr = kzalloc(sizeof(*tmr), GFP_KERNEL)))
  127. return -ENOMEM;
  128. nvkm_subdev_ctor(&nvkm_timer, device, index, &tmr->subdev);
  129. tmr->func = func;
  130. INIT_LIST_HEAD(&tmr->alarms);
  131. spin_lock_init(&tmr->lock);
  132. return 0;
  133. }