[353] | 1 | /*
|
---|
| 2 | * TOPPERS/SSP Kernel
|
---|
| 3 | * Smallest Set Profile Kernel
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2013-2015 by Embedded and Real-Time Systems Laboratory
|
---|
| 6 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 7 | * Copyright (C) 2018 by Naoki Saito
|
---|
| 8 | * Nagoya Municipal Industrial Research Institute, JAPAN
|
---|
| 9 | *
|
---|
| 10 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 11 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 12 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 13 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 14 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 15 | * スコード中に含まれていること.
|
---|
| 16 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 17 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 18 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 19 | * の無保証規定を掲載すること.
|
---|
| 20 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 21 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 22 | * と.
|
---|
| 23 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 24 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 25 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 26 | * 報告すること.
|
---|
| 27 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 28 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 29 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 30 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 31 | * 免責すること.
|
---|
| 32 | *
|
---|
| 33 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 34 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 35 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 36 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 37 | * の責任を負わない.
|
---|
| 38 | *
|
---|
| 39 | * $Id$
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | /*
|
---|
| 43 | * タイマドライバ(Generic Timer)
|
---|
| 44 | */
|
---|
| 45 | #include "kernel_impl.h"
|
---|
| 46 | #include "time_event.h"
|
---|
| 47 | #include "target_timer.h"
|
---|
| 48 | #include <sil.h>
|
---|
| 49 |
|
---|
| 50 | /*
|
---|
| 51 | * 仮想タイマの制御レジスタからの読み出し
|
---|
| 52 | */
|
---|
| 53 | Inline uint64_t
|
---|
| 54 | clock_read_ctrl(void) {
|
---|
| 55 | uint64_t cntv_ctrl;
|
---|
| 56 |
|
---|
| 57 | Asm ("isb; mrs %0, cntv_ctl_el0; isb;"
|
---|
| 58 | : "=r"(cntv_ctrl) :: "memory");
|
---|
| 59 |
|
---|
| 60 | return cntv_ctrl;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /*
|
---|
| 64 | * 仮想タイマの制御レジスタへの書き込み
|
---|
| 65 | */
|
---|
| 66 | Inline void
|
---|
| 67 | clock_write_ctrl(uint64_t cntv_ctrl) {
|
---|
| 68 |
|
---|
| 69 | Asm ("isb; msr cntv_ctl_el0, %0; isb;"
|
---|
| 70 | :: "r"(cntv_ctrl) : "memory");
|
---|
| 71 | return ;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | * 仮想タイマ(フリーカウンタ)のカウント値の読み出し
|
---|
| 76 | */
|
---|
| 77 | Inline uint64_t
|
---|
| 78 | clock_read_counter(void){
|
---|
| 79 | uint64_t cval;
|
---|
| 80 |
|
---|
| 81 | Asm ("mrs %0, cntvct_el0" : "=r" (cval));
|
---|
| 82 |
|
---|
| 83 | return cval;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /*
|
---|
| 87 | * 仮想タイマの値の読み出し
|
---|
| 88 | */
|
---|
| 89 | Inline uint64_t
|
---|
| 90 | clock_read_tval(void){
|
---|
| 91 | uint64_t cntv_tval;
|
---|
| 92 |
|
---|
| 93 | Asm ("isb; mrs %0, cntv_tval_el0; isb;" : "=r"(cntv_tval)
|
---|
| 94 | :: "memory");
|
---|
| 95 | return cntv_tval;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /*
|
---|
| 99 | * 仮想タイマの値の書き込み
|
---|
| 100 | */
|
---|
| 101 | Inline void
|
---|
| 102 | clock_write_tval(uint64_t cntv_tval){
|
---|
| 103 |
|
---|
| 104 | Asm ("isb; msr cntv_tval_el0, %0; isb;" :: "r"(cntv_tval)
|
---|
| 105 | : "memory");
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /*
|
---|
| 109 | * システムカウンタのクロック周波数の取得
|
---|
| 110 | */
|
---|
| 111 | Inline uint64_t
|
---|
| 112 | get_timer_freq(void) {
|
---|
| 113 | uint64_t freq_hz;
|
---|
| 114 |
|
---|
| 115 | Asm ("mrs %0, cntfrq_el0; isb; " : "=r"(freq_hz) :: "memory");
|
---|
| 116 | return freq_hz;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /*
|
---|
| 120 | * タイマの開始
|
---|
| 121 | */
|
---|
| 122 | static void
|
---|
| 123 | target_timer_start(void)
|
---|
| 124 | {
|
---|
| 125 | uint64_t cntv_ctl;
|
---|
| 126 |
|
---|
| 127 | cntv_ctl = clock_read_ctrl();
|
---|
| 128 | cntv_ctl |= ( CNTV_CTL_ENABLE | CNTV_CTL_START );
|
---|
| 129 | cntv_ctl &= ~CNTV_CTL_MASK; //mask
|
---|
| 130 | clock_write_ctrl(cntv_ctl);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /*
|
---|
| 134 | * タイマの停止
|
---|
| 135 | */
|
---|
| 136 | static void
|
---|
| 137 | target_timer_stop(void)
|
---|
| 138 | {
|
---|
| 139 | uint64_t cntv_ctl;
|
---|
| 140 |
|
---|
| 141 | cntv_ctl = clock_read_ctrl();
|
---|
| 142 | cntv_ctl &= ~( CNTV_CTL_ENABLE | CNTV_CTL_START );
|
---|
| 143 | cntv_ctl |= CNTV_CTL_MASK;
|
---|
| 144 | clock_write_ctrl(cntv_ctl);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /*
|
---|
| 148 | * タイマ割込み要求のクリア
|
---|
| 149 | */
|
---|
| 150 | static void
|
---|
| 151 | target_timer_int_clear(void){
|
---|
| 152 |
|
---|
| 153 | gicd_clear_pending(INTNO_TIMER);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /*
|
---|
| 157 | * 次のタイマ割込み発生までのオフセット
|
---|
| 158 | */
|
---|
| 159 | static uint64_t next_tval;
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * タイマの起動処理
|
---|
| 163 | */
|
---|
| 164 | void
|
---|
| 165 | target_timer_initialize(intptr_t exinf)
|
---|
| 166 | {
|
---|
| 167 | target_timer_stop();
|
---|
| 168 | target_timer_int_clear();
|
---|
| 169 |
|
---|
| 170 | // 次回のタイマ割込み発生までのオフセット算出
|
---|
| 171 | next_tval = get_timer_freq() * 1000 / 1000000UL; /* 1ms */
|
---|
| 172 | clock_write_tval(next_tval);
|
---|
| 173 |
|
---|
| 174 | target_timer_start();
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /*
|
---|
| 178 | * タイマの停止処理
|
---|
| 179 | */
|
---|
| 180 | void
|
---|
| 181 | target_timer_terminate(intptr_t exinf)
|
---|
| 182 | {
|
---|
| 183 | target_timer_stop();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * タイマ割込みハンドラ
|
---|
| 188 | */
|
---|
| 189 | void
|
---|
| 190 | target_timer_handler(void)
|
---|
| 191 | {
|
---|
| 192 | target_timer_stop();
|
---|
| 193 | target_timer_int_clear();
|
---|
| 194 | clock_write_tval(next_tval); // 次回の割込み
|
---|
| 195 | target_timer_start();
|
---|
| 196 |
|
---|
| 197 | i_begin_int(INTNO_TIMER);
|
---|
| 198 | signal_time();
|
---|
| 199 | i_end_int(INTNO_TIMER);
|
---|
| 200 | }
|
---|