clock.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/init.h>
  22. #include <kern/percpu.h>
  23. #include <kern/rcu.h>
  24. #include <kern/sref.h>
  25. #include <kern/syscnt.h>
  26. #include <kern/thread.h>
  27. #include <kern/timer.h>
  28. #include <kern/work.h>
  29. #include <machine/boot.h>
  30. #include <machine/cpu.h>
  31. struct clock_cpu_data
  32. {
  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 (uint32_t cpu = 0; cpu < cpu_count (); ++cpu)
  48. clock_cpu_data_init (percpu_ptr (clock_cpu_data, cpu), cpu);
  49. return (0);
  50. }
  51. INIT_OP_DEFINE (clock_setup,
  52. INIT_OP_DEP (cpu_mp_probe, true),
  53. INIT_OP_DEP (syscnt_setup, true));
  54. void clock_tick_intr (void)
  55. {
  56. struct clock_cpu_data *cpu_data;
  57. assert (thread_check_intr_context ());
  58. if (cpu_id () == 0)
  59. {
  60. #ifdef __LP64__
  61. atomic_add_rlx (&clock_global_time.ticks, 1);
  62. #else
  63. union clock_global_time t = { .ticks = clock_global_time.ticks };
  64. ++t.ticks;
  65. atomic_store_rlx (&clock_global_time.high2, t.high1);
  66. atomic_store_rel (&clock_global_time.low, t.low);
  67. atomic_store_rel (&clock_global_time.high1, t.high1);
  68. #endif
  69. }
  70. timer_report_periodic_event ();
  71. rcu_report_periodic_event ();
  72. sref_report_periodic_event ();
  73. work_report_periodic_event ();
  74. thread_report_periodic_event ();
  75. cpu_data = cpu_local_ptr (clock_cpu_data);
  76. syscnt_inc (&cpu_data->sc_tick_intrs);
  77. }