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

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

hal_special: replace hal_time_stamp() by hal_get_cycles()
hal_remote : remove hal_remove_unc()

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