Subversion Repositories HelenOS

Rev

Rev 3577 | Rev 3779 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
173 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2005 Jakub Jermar
173 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1888 jermar 29
/** @addtogroup ia64   
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1888 jermar 35
#ifndef KERN_ia64_ASM_H_
36
#define KERN_ia64_ASM_H_
173 jermar 37
 
747 jermar 38
#include <config.h>
173 jermar 39
#include <arch/types.h>
432 jermar 40
#include <arch/register.h>
173 jermar 41
 
3577 vana 42
typedef uint64_t ioport_t;
2515 vana 43
 
2726 vana 44
#define IA64_IOSPACE_ADDRESS 0xE001000000000000ULL
2515 vana 45
 
3774 jermar 46
static inline void  outb(ioport_t port, uint8_t v)
2515 vana 47
{
3774 jermar 48
    *((uint8_t *)(IA64_IOSPACE_ADDRESS +
49
        ((port & 0xfff) | ((port >> 2) << 12)))) = v;
2726 vana 50
 
2515 vana 51
    asm volatile ("mf\n" ::: "memory");
52
}
53
 
3774 jermar 54
static inline void  outw(ioport_t port, uint16_t v)
3490 vana 55
{
3774 jermar 56
    *((uint16_t *)(IA64_IOSPACE_ADDRESS +
57
        ((port & 0xfff) | ((port >> 2) << 12)))) = v;
2515 vana 58
 
3490 vana 59
    asm volatile ("mf\n" ::: "memory");
60
}
61
 
3774 jermar 62
static inline void  outl(ioport_t port, uint32_t v)
3490 vana 63
{
3774 jermar 64
    *((uint32_t *)(IA64_IOSPACE_ADDRESS +
65
        ((port & 0xfff) | ((port >> 2) << 12)))) = v;
3490 vana 66
 
67
    asm volatile ("mf\n" ::: "memory");
68
}
69
 
3577 vana 70
static inline uint8_t inb(ioport_t port)
2515 vana 71
{
72
    asm volatile ("mf\n" ::: "memory");
2726 vana 73
 
3774 jermar 74
    return *((uint8_t *)(IA64_IOSPACE_ADDRESS +
75
        ((port & 0xfff) | ((port >> 2) << 12))));
2515 vana 76
}
77
 
3577 vana 78
static inline uint16_t inw(ioport_t port)
3490 vana 79
{
80
    asm volatile ("mf\n" ::: "memory");
2515 vana 81
 
3774 jermar 82
    return *((uint16_t *)(IA64_IOSPACE_ADDRESS +
83
        ((port & 0xffE) | ((port >> 2) << 12))));
3490 vana 84
}
2515 vana 85
 
3577 vana 86
static inline uint32_t inl(ioport_t port)
3490 vana 87
{
88
    asm volatile ("mf\n" ::: "memory");
89
 
3774 jermar 90
    return *((uint32_t *)(IA64_IOSPACE_ADDRESS +
91
        ((port & 0xfff) | ((port >> 2) << 12))));
3490 vana 92
}
93
 
180 jermar 94
/** Return base address of current stack
95
 *
96
 * Return the base address of the current stack.
97
 * The stack is assumed to be STACK_SIZE long.
98
 * The stack must start on page boundary.
99
 */
1780 jermar 100
static inline uintptr_t get_stack_base(void)
173 jermar 101
{
1780 jermar 102
    uint64_t v;
180 jermar 103
 
3577 vana 104
    //I'm not sure why but this code bad inlines in scheduler, 
105
    //so THE shifts about 16B and causes kernel panic
106
    //asm volatile ("and %0 = %1, r12" : "=r" (v) : "r" (~(STACK_SIZE-1)));
107
    //return v;
180 jermar 108
 
3577 vana 109
    //this code have the same meaning but inlines well
110
    asm volatile ("mov %0 = r12" : "=r" (v)  );
111
    return v & (~(STACK_SIZE-1));
173 jermar 112
}
113
 
919 jermar 114
/** Return Processor State Register.
115
 *
116
 * @return PSR.
117
 */
1780 jermar 118
static inline uint64_t psr_read(void)
919 jermar 119
{
1780 jermar 120
    uint64_t v;
919 jermar 121
 
2082 decky 122
    asm volatile ("mov %0 = psr\n" : "=r" (v));
919 jermar 123
 
124
    return v;
125
}
126
 
470 jermar 127
/** Read IVA (Interruption Vector Address).
128
 *
129
 * @return Return location of interruption vector table.
130
 */
1780 jermar 131
static inline uint64_t iva_read(void)
470 jermar 132
{
1780 jermar 133
    uint64_t v;
470 jermar 134
 
2082 decky 135
    asm volatile ("mov %0 = cr.iva\n" : "=r" (v));
470 jermar 136
 
137
    return v;
138
}
139
 
140
/** Write IVA (Interruption Vector Address) register.
141
 *
1708 jermar 142
 * @param v New location of interruption vector table.
470 jermar 143
 */
1780 jermar 144
static inline void iva_write(uint64_t v)
470 jermar 145
{
2082 decky 146
    asm volatile ("mov cr.iva = %0\n" : : "r" (v));
470 jermar 147
}
148
 
149
 
432 jermar 150
/** Read IVR (External Interrupt Vector Register).
431 jermar 151
 *
152
 * @return Highest priority, pending, unmasked external interrupt vector.
153
 */
1780 jermar 154
static inline uint64_t ivr_read(void)
431 jermar 155
{
1780 jermar 156
    uint64_t v;
431 jermar 157
 
2082 decky 158
    asm volatile ("mov %0 = cr.ivr\n" : "=r" (v));
431 jermar 159
 
432 jermar 160
    return v;
431 jermar 161
}
195 vana 162
 
3577 vana 163
static inline uint64_t cr64_read(void)
164
{
165
    uint64_t v;
166
 
167
    asm volatile ("mov %0 = cr64\n" : "=r" (v));
168
 
169
    return v;
170
}
171
 
172
 
432 jermar 173
/** Write ITC (Interval Timer Counter) register.
174
 *
1708 jermar 175
 * @param v New counter value.
432 jermar 176
 */
1780 jermar 177
static inline void itc_write(uint64_t v)
432 jermar 178
{
2082 decky 179
    asm volatile ("mov ar.itc = %0\n" : : "r" (v));
432 jermar 180
}
431 jermar 181
 
432 jermar 182
/** Read ITC (Interval Timer Counter) register.
183
 *
184
 * @return Current counter value.
185
 */
1780 jermar 186
static inline uint64_t itc_read(void)
432 jermar 187
{
1780 jermar 188
    uint64_t v;
432 jermar 189
 
2082 decky 190
    asm volatile ("mov %0 = ar.itc\n" : "=r" (v));
432 jermar 191
 
192
    return v;
193
}
195 vana 194
 
432 jermar 195
/** Write ITM (Interval Timer Match) register.
196
 *
1708 jermar 197
 * @param v New match value.
432 jermar 198
 */
1780 jermar 199
static inline void itm_write(uint64_t v)
432 jermar 200
{
2082 decky 201
    asm volatile ("mov cr.itm = %0\n" : : "r" (v));
432 jermar 202
}
195 vana 203
 
1488 vana 204
/** Read ITM (Interval Timer Match) register.
205
 *
206
 * @return Match value.
207
 */
1780 jermar 208
static inline uint64_t itm_read(void)
1488 vana 209
{
1780 jermar 210
    uint64_t v;
1488 vana 211
 
2082 decky 212
    asm volatile ("mov %0 = cr.itm\n" : "=r" (v));
1488 vana 213
 
214
    return v;
215
}
216
 
433 jermar 217
/** Read ITV (Interval Timer Vector) register.
218
 *
219
 * @return Current vector and mask bit.
220
 */
1780 jermar 221
static inline uint64_t itv_read(void)
433 jermar 222
{
1780 jermar 223
    uint64_t v;
433 jermar 224
 
2082 decky 225
    asm volatile ("mov %0 = cr.itv\n" : "=r" (v));
433 jermar 226
 
227
    return v;
228
}
229
 
432 jermar 230
/** Write ITV (Interval Timer Vector) register.
231
 *
1708 jermar 232
 * @param v New vector and mask bit.
432 jermar 233
 */
1780 jermar 234
static inline void itv_write(uint64_t v)
432 jermar 235
{
2082 decky 236
    asm volatile ("mov cr.itv = %0\n" : : "r" (v));
432 jermar 237
}
238 vana 238
 
432 jermar 239
/** Write EOI (End Of Interrupt) register.
240
 *
1708 jermar 241
 * @param v This value is ignored.
432 jermar 242
 */
1780 jermar 243
static inline void eoi_write(uint64_t v)
432 jermar 244
{
2082 decky 245
    asm volatile ("mov cr.eoi = %0\n" : : "r" (v));
432 jermar 246
}
247
 
248
/** Read TPR (Task Priority Register).
249
 *
250
 * @return Current value of TPR.
251
 */
1780 jermar 252
static inline uint64_t tpr_read(void)
432 jermar 253
{
1780 jermar 254
    uint64_t v;
432 jermar 255
 
2082 decky 256
    asm volatile ("mov %0 = cr.tpr\n"  : "=r" (v));
432 jermar 257
 
258
    return v;
259
}
260
 
261
/** Write TPR (Task Priority Register).
262
 *
1708 jermar 263
 * @param v New value of TPR.
432 jermar 264
 */
1780 jermar 265
static inline void tpr_write(uint64_t v)
432 jermar 266
{
2082 decky 267
    asm volatile ("mov cr.tpr = %0\n" : : "r" (v));
432 jermar 268
}
269
 
270
/** Disable interrupts.
271
 *
272
 * Disable interrupts and return previous
273
 * value of PSR.
274
 *
275
 * @return Old interrupt priority level.
276
 */
277
static ipl_t interrupts_disable(void)
278
{
1780 jermar 279
    uint64_t v;
432 jermar 280
 
2082 decky 281
    asm volatile (
432 jermar 282
        "mov %0 = psr\n"
283
        "rsm %1\n"
284
        : "=r" (v)
285
        : "i" (PSR_I_MASK)
286
    );
287
 
288
    return (ipl_t) v;
289
}
290
 
291
/** Enable interrupts.
292
 *
293
 * Enable interrupts and return previous
294
 * value of PSR.
295
 *
296
 * @return Old interrupt priority level.
297
 */
298
static ipl_t interrupts_enable(void)
299
{
1780 jermar 300
    uint64_t v;
432 jermar 301
 
2082 decky 302
    asm volatile (
432 jermar 303
        "mov %0 = psr\n"
304
        "ssm %1\n"
305
        ";;\n"
306
        "srlz.d\n"
307
        : "=r" (v)
308
        : "i" (PSR_I_MASK)
309
    );
310
 
311
    return (ipl_t) v;
312
}
313
 
314
/** Restore interrupt priority level.
315
 *
316
 * Restore PSR.
317
 *
318
 * @param ipl Saved interrupt priority level.
319
 */
320
static inline void interrupts_restore(ipl_t ipl)
321
{
472 jermar 322
    if (ipl & PSR_I_MASK)
323
        (void) interrupts_enable();
324
    else
325
        (void) interrupts_disable();
432 jermar 326
}
327
 
328
/** Return interrupt priority level.
329
 *
330
 * @return PSR.
331
 */
332
static inline ipl_t interrupts_read(void)
333
{
919 jermar 334
    return (ipl_t) psr_read();
432 jermar 335
}
336
 
746 jermar 337
/** Disable protection key checking. */
338
static inline void pk_disable(void)
339
{
2082 decky 340
    asm volatile ("rsm %0\n" : : "i" (PSR_PK_MASK));
746 jermar 341
}
342
 
432 jermar 343
extern void cpu_halt(void);
344
extern void cpu_sleep(void);
1780 jermar 345
extern void asm_delay_loop(uint32_t t);
238 vana 346
 
3774 jermar 347
extern void switch_to_userspace(uintptr_t, uintptr_t, uintptr_t, uintptr_t,
348
    uint64_t, uint64_t);
919 jermar 349
 
173 jermar 350
#endif
1702 cejka 351
 
1888 jermar 352
/** @}
1702 cejka 353
 */