source: trunk/hal/tsar_mips32/core/hal_special.c @ 110

Last change on this file since 110 was 101, checked in by alain, 7 years ago

euh...

File size: 5.2 KB
Line 
1/*
2 * hal_special.c - implementation of Generic Special Register Access API for TSAR-MIPS32
3 *
4 * Author    Alain Greiner (2016,2017)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH..
9 *
10 * ALMOS-MKH. is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH. is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25#include <hal_types.h>
26#include <hal_special.h>
27#include <core.h>
28#include <thread.h>
29
30/****  Forward declarations ****/
31
32struct thread_s;
33
34
35///////////////////
36gid_t hal_get_gid()
37{
38        uint32_t proc_id;
39
40        asm volatile ("mfc0    %0,  $15, 1" : "=&r" (proc_id));
41
42        return (proc_id & 0x3FF);  // 4/4/2 format for TSAR
43}
44
45/////////////////////////
46uint64_t hal_get_cycles()
47{
48        uint64_t cycles;                // absolute time to be returned
49    uint32_t last_count;            // last registered cycles count
50    uint32_t current_count;         // current cycles count
51        uint32_t elapsed;
52
53    core_t * core = CURRENT_THREAD->core;
54
55    // get last registered time stamp
56        last_count = core->time_stamp;
57
58    // get current time stamp from hardware register
59        asm volatile ("mfc0   %0,  $9  " : "=&r" (current_count));
60
61        // compute number of elapsed cycles, taking into account 32 bits register wrap
62        if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count;
63        else                           elapsed = current_count - last_count;
64
65    // compute absolute time
66        cycles = core->cycles + elapsed;
67
68        // update core time
69        core->time_stamp = current_count;
70        core->cycles     = cycles;
71
72        hal_wbflush();
73
74        return cycles;
75}
76
77//////////////////////////////////////////
78struct thread_s * hal_get_current_thread()
79{
80        void * thread_ptr;
81 
82        asm volatile
83    ( "mfc0    %0,  $4,  2  \n"
84      : "=&r" (thread_ptr)  );
85
86        return thread_ptr;
87}
88
89///////////////////////////////////////////////////////
90void hal_set_current_thread( struct thread_s * thread )
91{ 
92        asm volatile
93    ( "mtc0    %0,  $4,  2  \n"
94      : : "r" (thread) );
95}
96
97/////////////////////
98void hal_fpu_enable()
99{
100        asm volatile 
101        ( ".set noat                         \n"
102      "lui    $27,    0x2000             \n"
103      "mfc0   $1,     $12                \n"
104      "or     $27,    $1,    $27         \n"
105      "mtc0   $27,    $12                \n"
106      ".set at                           \n" );
107}
108
109//////////////////////
110void hal_fpu_disable()
111{
112        asm volatile 
113        ( ".set noat                         \n"
114      "lui    $27,    0xDFFF             \n"
115          "ori    $27,    $27,   0xFFFF      \n"
116      "mfc0   $1,     $12                \n"
117      "and    $27,    $1,    $27         \n"
118      "mtc0   $27,    $12                \n"
119          ".set at                           \n");
120}
121
122////////////////////////
123uint32_t hal_get_stack()
124{
125        register uint32_t sp;
126 
127        asm volatile
128        ( "or    %0,   $0,   $29  \n" 
129       : "=&r" (sp) );
130 
131        return sp;
132}
133
134////////////////////////////////////////
135uint32_t hal_set_stack( void * new_val )
136{
137        register uint32_t sp;
138 
139        asm volatile
140        ( "or    %0,   $0,      $29   \n"
141          "or    $29,  $0,      %1    \n"
142          : "=&r" (sp) : "r" (new_val)  );
143 
144        return sp;
145}
146
147////////////////////////////
148uint32_t hal_get_bad_vaddr()
149{
150        register uint32_t bad_va;
151
152        asm volatile
153    ( "mfc0    %0,  $8  \n"
154      : "=&r" (bad_va) );
155
156        return bad_va;
157}
158
159////////////////////////////////////////////
160uint32_t hal_uncached_read( uint32_t * ptr )
161{
162        register uint32_t val;
163
164        asm volatile
165        ( "ll    %0,     (%1)  \n"
166      : "=&r"(val) : "r" (ptr) );
167
168        return val;
169}
170
171//////////////////////////////////////////
172void hal_invalid_dcache_line( void * ptr )
173{
174        asm volatile
175        ( "cache    %0,     (%1)              \n"
176          "sync                               \n"
177          : : "i" (0x11) , "r" (ptr) );
178}
179
180//////////////////
181void hal_wbflush()
182{
183        asm volatile
184        ( "sync    \n":: );
185}
186
187////////////////
188void hal_rdbar()
189{
190        asm volatile( "" ::: "memory" );
191}
192
193/////////////////////
194void hal_core_sleep()
195{
196        asm volatile
197        ("wait   \n"::);
198}
199
200//////////////////////////////////////
201void hal_fixed_delay( uint32_t delay )
202{ 
203    asm volatile
204    ( "1:                    \n"
205      "or    $27,  %0,  $0   \n"
206      "addi  $27, $27,  -1   \n"
207      "bne   $27,  $0,  1b   \n"
208      "nop                   \n"
209      : : "r" (delay) : "$27" );
210}
211
212//////////////////////////////////////////////////
213void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code,
214                       intptr_t * mmu_ins_bad_vaddr,
215                       intptr_t * mmu_dat_excp_code,
216                       intptr_t * mmu_dat_bad_vaddr )
217{
218    asm volatile
219    ( "mfc2   %0,    $11        \n"
220      "mfc2   %1,    $13        \n"
221      "mfc2   %2,    $12        \n"
222      "mfc2   %3,    $14        \n"
223      : "=&r"(mmu_ins_excp_code),
224        "=&r"(mmu_ins_bad_vaddr),
225        "=&r"(mmu_dat_excp_code),
226        "=&r"(mmu_dat_bad_vaddr) );
227}
Note: See TracBrowser for help on using the repository browser.