1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreWatchdog |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _Watchdog_Do_tickle() and _Watchdog_Tick(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (C) 2015, 2016 embedded brains GmbH & Co. KG |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifdef HAVE_CONFIG_H |
---|
38 | #include "config.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <rtems/score/watchdogimpl.h> |
---|
42 | #include <rtems/score/schedulerimpl.h> |
---|
43 | #include <rtems/score/threaddispatch.h> |
---|
44 | #include <rtems/score/timecounter.h> |
---|
45 | |
---|
46 | void _Watchdog_Do_tickle( |
---|
47 | Watchdog_Header *header, |
---|
48 | Watchdog_Control *first, |
---|
49 | uint64_t now, |
---|
50 | #ifdef RTEMS_SMP |
---|
51 | ISR_lock_Control *lock, |
---|
52 | #endif |
---|
53 | ISR_lock_Context *lock_context |
---|
54 | ) |
---|
55 | { |
---|
56 | do { |
---|
57 | if ( first->expire <= now ) { |
---|
58 | Watchdog_Service_routine_entry routine; |
---|
59 | |
---|
60 | _Watchdog_Next_first( header, first ); |
---|
61 | _RBTree_Extract( &header->Watchdogs, &first->Node.RBTree ); |
---|
62 | _Watchdog_Set_state( first, WATCHDOG_INACTIVE ); |
---|
63 | routine = first->routine; |
---|
64 | |
---|
65 | _ISR_lock_Release_and_ISR_enable( lock, lock_context ); |
---|
66 | ( *routine )( first ); |
---|
67 | _ISR_lock_ISR_disable_and_acquire( lock, lock_context ); |
---|
68 | } else { |
---|
69 | break; |
---|
70 | } |
---|
71 | |
---|
72 | first = _Watchdog_Header_first( header ); |
---|
73 | } while ( first != NULL ); |
---|
74 | } |
---|
75 | |
---|
76 | void _Watchdog_Tick( Per_CPU_Control *cpu ) |
---|
77 | { |
---|
78 | ISR_lock_Context lock_context; |
---|
79 | Watchdog_Header *header; |
---|
80 | Watchdog_Control *first; |
---|
81 | uint64_t ticks; |
---|
82 | struct timespec now; |
---|
83 | Thread_Control *executing; |
---|
84 | const Thread_CPU_budget_operations *cpu_budget_operations; |
---|
85 | |
---|
86 | #ifdef RTEMS_SMP |
---|
87 | if ( _Per_CPU_Is_boot_processor( cpu ) ) { |
---|
88 | #endif |
---|
89 | ++_Watchdog_Ticks_since_boot; |
---|
90 | #ifdef RTEMS_SMP |
---|
91 | } |
---|
92 | #endif |
---|
93 | |
---|
94 | _ISR_lock_ISR_disable_and_acquire( &cpu->Watchdog.Lock, &lock_context ); |
---|
95 | |
---|
96 | ticks = cpu->Watchdog.ticks; |
---|
97 | _Assert( ticks < UINT64_MAX ); |
---|
98 | ++ticks; |
---|
99 | cpu->Watchdog.ticks = ticks; |
---|
100 | |
---|
101 | header = &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_TICKS ]; |
---|
102 | first = _Watchdog_Header_first( header ); |
---|
103 | |
---|
104 | if ( first != NULL ) { |
---|
105 | _Watchdog_Tickle( |
---|
106 | header, |
---|
107 | first, |
---|
108 | ticks, |
---|
109 | &cpu->Watchdog.Lock, |
---|
110 | &lock_context |
---|
111 | ); |
---|
112 | } |
---|
113 | |
---|
114 | header = &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_MONOTONIC ]; |
---|
115 | first = _Watchdog_Header_first( header ); |
---|
116 | |
---|
117 | if ( first != NULL ) { |
---|
118 | _Timecounter_Getnanouptime( &now ); |
---|
119 | _Watchdog_Tickle( |
---|
120 | header, |
---|
121 | first, |
---|
122 | _Watchdog_Ticks_from_timespec( &now ), |
---|
123 | &cpu->Watchdog.Lock, |
---|
124 | &lock_context |
---|
125 | ); |
---|
126 | } |
---|
127 | |
---|
128 | header = &cpu->Watchdog.Header[ PER_CPU_WATCHDOG_REALTIME ]; |
---|
129 | first = _Watchdog_Header_first( header ); |
---|
130 | |
---|
131 | if ( first != NULL ) { |
---|
132 | _Timecounter_Getnanotime( &now ); |
---|
133 | _Watchdog_Tickle( |
---|
134 | header, |
---|
135 | first, |
---|
136 | _Watchdog_Ticks_from_timespec( &now ), |
---|
137 | &cpu->Watchdog.Lock, |
---|
138 | &lock_context |
---|
139 | ); |
---|
140 | } |
---|
141 | |
---|
142 | _ISR_lock_Release_and_ISR_enable( &cpu->Watchdog.Lock, &lock_context ); |
---|
143 | |
---|
144 | /* |
---|
145 | * Each online processor has at least an idle thread as the executing thread |
---|
146 | * even in case it has currently no scheduler assigned. Clock interrupts on |
---|
147 | * processors which are not online would be a severe bug of the Clock Driver. |
---|
148 | */ |
---|
149 | executing = _Per_CPU_Get_executing( cpu ); |
---|
150 | _Assert( executing != NULL ); |
---|
151 | cpu_budget_operations = executing->CPU_budget.operations; |
---|
152 | |
---|
153 | if ( cpu_budget_operations != NULL ) { |
---|
154 | ( *cpu_budget_operations->at_tick )( executing ); |
---|
155 | } |
---|
156 | } |
---|