clock.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <kern/atomic.h>
  20. #include <kern/clock.h>
  21. #include <kern/clock_i.h>
  22. #include <kern/init.h>
  23. #include <kern/percpu.h>
  24. #include <kern/rcu.h>
  25. #include <kern/sref.h>
  26. #include <kern/syscnt.h>
  27. #include <kern/thread.h>
  28. #include <kern/timer.h>
  29. #include <kern/work.h>
  30. #include <machine/boot.h>
  31. #include <machine/cpu.h>
  32. struct clock_cpu_data {
  33. struct syscnt sc_tick_intrs;
  34. };
  35. static struct clock_cpu_data clock_cpu_data __percpu;
  36. union clock_global_time clock_global_time;
  37. static inline void __init
  38. clock_cpu_data_init(struct clock_cpu_data *cpu_data, unsigned int cpu)
  39. {
  40. char name[SYSCNT_NAME_SIZE];
  41. snprintf(name, sizeof(name), "clock_tick_intrs/%u", cpu);
  42. syscnt_register(&cpu_data->sc_tick_intrs, name);
  43. }
  44. static int __init
  45. clock_setup(void)
  46. {
  47. for (unsigned int cpu = 0; cpu < cpu_count(); cpu++) {
  48. clock_cpu_data_init(percpu_ptr(clock_cpu_data, cpu), cpu);
  49. }
  50. return 0;
  51. }
  52. INIT_OP_DEFINE(clock_setup,
  53. INIT_OP_DEP(cpu_mp_probe, true),
  54. INIT_OP_DEP(syscnt_setup, true));
  55. void clock_tick_intr(void)
  56. {
  57. struct clock_cpu_data *cpu_data;
  58. assert(thread_check_intr_context());
  59. if (cpu_id() == 0) {
  60. #ifdef ATOMIC_HAVE_64B_OPS
  61. atomic_add(&clock_global_time.ticks, 1ULL, ATOMIC_RELAXED);
  62. #else /* ATOMIC_HAVE_64B_OPS */
  63. union clock_global_time t;
  64. t.ticks = clock_global_time.ticks;
  65. t.ticks++;
  66. atomic_store(&clock_global_time.high2, t.high1, ATOMIC_RELAXED);
  67. atomic_store(&clock_global_time.low, t.low, ATOMIC_RELEASE);
  68. atomic_store(&clock_global_time.high1, t.high1, ATOMIC_RELEASE);
  69. #endif /* ATOMIC_HAVE_64B_OPS */
  70. }
  71. timer_report_periodic_event();
  72. rcu_report_periodic_event();
  73. sref_report_periodic_event();
  74. work_report_periodic_event();
  75. thread_report_periodic_event();
  76. cpu_data = cpu_local_ptr(clock_cpu_data);
  77. syscnt_inc(&cpu_data->sc_tick_intrs);
  78. }