summaryrefslogtreecommitdiff
path: root/kern/clock.c
blob: 27fb9a233a0b7cf4a0e3ce608b12b2b7334cab0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>

#include <kern/atomic.h>
#include <kern/clock.h>
#include <kern/clock_i.h>
#include <kern/init.h>
#include <kern/percpu.h>
#include <kern/rcu.h>
#include <kern/sref.h>
#include <kern/syscnt.h>
#include <kern/thread.h>
#include <kern/timer.h>
#include <kern/work.h>
#include <machine/boot.h>
#include <machine/cpu.h>

struct clock_cpu_data {
    struct syscnt sc_tick_intrs;
};

static struct clock_cpu_data clock_cpu_data __percpu;

union clock_global_time clock_global_time;

static inline void __init
clock_cpu_data_init(struct clock_cpu_data *cpu_data, unsigned int cpu)
{
    char name[SYSCNT_NAME_SIZE];

    snprintf(name, sizeof(name), "clock_tick_intrs/%u", cpu);
    syscnt_register(&cpu_data->sc_tick_intrs, name);
}

static int __init
clock_setup(void)
{
    for (unsigned int cpu = 0; cpu < cpu_count(); cpu++) {
        clock_cpu_data_init(percpu_ptr(clock_cpu_data, cpu), cpu);
    }

    return 0;
}

INIT_OP_DEFINE(clock_setup,
               INIT_OP_DEP(cpu_mp_probe, true),
               INIT_OP_DEP(syscnt_setup, true));

void clock_tick_intr(void)
{
    struct clock_cpu_data *cpu_data;

    assert(thread_check_intr_context());

    if (cpu_id() == 0) {
#ifdef ATOMIC_HAVE_64B_OPS

        atomic_add(&clock_global_time.ticks, 1, ATOMIC_RELAXED);

#else /* ATOMIC_HAVE_64B_OPS */

        union clock_global_time t;

        t.ticks = clock_global_time.ticks;
        t.ticks++;

        atomic_store(&clock_global_time.high2, t.high1, ATOMIC_RELAXED);
        atomic_store(&clock_global_time.low, t.low, ATOMIC_RELEASE);
        atomic_store(&clock_global_time.high1, t.high1, ATOMIC_RELEASE);

#endif /* ATOMIC_HAVE_64B_OPS */
    }

    timer_report_periodic_event();
    rcu_report_periodic_event();
    sref_report_periodic_event();
    work_report_periodic_event();
    thread_report_periodic_event();

    cpu_data = cpu_local_ptr(clock_cpu_data);
    syscnt_inc(&cpu_data->sc_tick_intrs);
}