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

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

This version executed successfully the user "init" process on a mono-processor TSAR architecture.

File size: 5.6 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/////////////////////////////////
35void hal_set_ebase( reg_t  base )
36{ 
37        asm volatile ("mtc0    %0,  $15,  1" : : "r" (base));
38}
39
40//////////////////////////
41inline gid_t hal_get_gid()
42{
43        uint32_t proc_id;
44
45        asm volatile ("mfc0    %0,  $15, 1" : "=&r" (proc_id));
46
47        return (proc_id & 0x3FF);  // 4/4/2 format for TSAR
48}
49
50/////////////////////////////
51inline reg_t hal_time_stamp()
52{
53    reg_t count;
54
55        asm volatile ("mfc0   %0,  $9" : "=&r" (count));
56
57    return count;
58}
59
60/////////////////////////
61inline reg_t hal_get_sr()
62{
63    reg_t sr;
64
65        asm volatile ("mfc0    %0,    $12" : "=&r" (sr));
66
67        return sr;
68}
69
70/////////////////////////
71uint64_t hal_get_cycles()
72{
73        uint64_t cycles;                // absolute time to be returned
74    uint32_t last_count;            // last registered cycles count
75    uint32_t current_count;         // current cycles count
76        uint32_t elapsed;
77
78    core_t * core = CURRENT_THREAD->core;
79
80    // get last registered time stamp
81        last_count = core->time_stamp;
82
83    // get current time stamp from hardware register
84        current_count = hal_time_stamp();
85
86        // compute number of elapsed cycles, taking into account 32 bits register wrap
87        if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count;
88        else                           elapsed = current_count - last_count;
89
90    // compute absolute time
91        cycles = core->cycles + elapsed;
92
93        // update core time
94        core->time_stamp = current_count;
95        core->cycles     = cycles;
96
97        hal_fence();
98
99        return cycles;
100}
101
102/////////////////////////////////////////////////
103inline struct thread_s * hal_get_current_thread()
104{
105        void * thread_ptr;
106 
107        asm volatile ("mfc0    %0,  $4,  2" : "=&r" (thread_ptr));
108
109        return thread_ptr;
110}
111
112///////////////////////////////////////////////////////
113void hal_set_current_thread( struct thread_s * thread )
114{ 
115        asm volatile ("mtc0    %0,  $4,  2" : : "r" (thread));
116}
117
118/////////////////////
119void hal_fpu_enable()
120{
121        asm volatile 
122        ( ".set noat                         \n"
123      "lui    $27,    0x2000             \n"
124      "mfc0   $1,     $12                \n"
125      "or     $27,    $1,    $27         \n"
126      "mtc0   $27,    $12                \n"
127      ".set at                           \n" );
128}
129
130//////////////////////
131void hal_fpu_disable()
132{
133        asm volatile 
134        ( ".set noat                         \n"
135      "lui    $27,    0xDFFF             \n"
136          "ori    $27,    $27,   0xFFFF      \n"
137      "mfc0   $1,     $12                \n"
138      "and    $27,    $1,    $27         \n"
139      "mtc0   $27,    $12                \n"
140          ".set at                           \n");
141}
142
143////////////////////////
144uint32_t hal_get_stack()
145{
146        register uint32_t sp;
147 
148        asm volatile ("or    %0,   $0,   $29" : "=&r" (sp));
149 
150        return sp;
151}
152
153////////////////////////////////////////
154uint32_t hal_set_stack( void * new_val )
155{
156        register uint32_t sp;
157 
158        asm volatile
159        ( "or    %0,   $0,      $29   \n"
160          "or    $29,  $0,      %1    \n"
161          : "=&r" (sp) : "r" (new_val)  );
162 
163        return sp;
164}
165
166////////////////////////////
167uint32_t hal_get_bad_vaddr()
168{
169        register uint32_t bad_va;
170
171        asm volatile
172    ( "mfc0    %0,  $8  \n"
173      : "=&r" (bad_va) );
174
175        return bad_va;
176}
177
178////////////////////////////////////////////
179uint32_t hal_uncached_read( uint32_t * ptr )
180{
181        register uint32_t val;
182
183        asm volatile
184        ( "ll    %0,     (%1)  \n"
185      : "=&r"(val) : "r" (ptr) );
186
187        return val;
188}
189
190//////////////////////////////////////////
191void hal_invalid_dcache_line( void * ptr )
192{
193        asm volatile
194        ( "cache    %0,     (%1)              \n"
195          "sync                               \n"
196          : : "i" (0x11) , "r" (ptr) );
197}
198
199////////////////
200void hal_fence()
201{
202        asm volatile
203        ( "sync    \n":: );
204}
205
206////////////////
207void hal_rdbar()
208{
209        asm volatile( "" ::: "memory" );
210}
211
212/////////////////////
213void hal_core_sleep()
214{
215        asm volatile
216        ("wait   \n"::);
217}
218
219//////////////////////////////////////
220void hal_fixed_delay( uint32_t delay )
221{ 
222    asm volatile
223    ( "1:                    \n"
224      "or    $27,  %0,  $0   \n"
225      "addi  $27, $27,  -1   \n"
226      "bne   $27,  $0,  1b   \n"
227      "nop                   \n"
228      : : "r" (delay) : "$27" );
229}
230
231//////////////////////////////////////////////////
232void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code,
233                       intptr_t * mmu_ins_bad_vaddr,
234                       intptr_t * mmu_dat_excp_code,
235                       intptr_t * mmu_dat_bad_vaddr )
236{
237    asm volatile
238    ( "mfc2   %0,    $11        \n"
239      "mfc2   %1,    $13        \n"
240      "mfc2   %2,    $12        \n"
241      "mfc2   %3,    $14        \n"
242      : "=&r"(*mmu_ins_excp_code),
243        "=&r"(*mmu_ins_bad_vaddr),
244        "=&r"(*mmu_dat_excp_code),
245        "=&r"(*mmu_dat_bad_vaddr) );
246}
247
Note: See TracBrowser for help on using the repository browser.