[361635c] | 1 | /*
|
---|
| 2 | * Copyright (C) 2005 Jakub 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 |
|
---|
[06e1e95] | 29 | /** @addtogroup ia64
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_ia64_ASM_H_
|
---|
| 36 | #define KERN_ia64_ASM_H_
|
---|
[361635c] | 37 |
|
---|
| 38 | #include <config.h>
|
---|
[c2b95d3] | 39 | #include <arch/types.h>
|
---|
[0259524] | 40 | #include <arch/register.h>
|
---|
[361635c] | 41 |
|
---|
[1fbbcd6] | 42 | /** Return base address of current stack
|
---|
| 43 | *
|
---|
| 44 | * Return the base address of the current stack.
|
---|
| 45 | * The stack is assumed to be STACK_SIZE long.
|
---|
| 46 | * The stack must start on page boundary.
|
---|
| 47 | */
|
---|
[7f1c620] | 48 | static inline uintptr_t get_stack_base(void)
|
---|
[361635c] | 49 | {
|
---|
[7f1c620] | 50 | uint64_t v;
|
---|
[1fbbcd6] | 51 |
|
---|
| 52 | __asm__ volatile ("and %0 = %1, r12" : "=r" (v) : "r" (~(STACK_SIZE-1)));
|
---|
| 53 |
|
---|
| 54 | return v;
|
---|
[361635c] | 55 | }
|
---|
| 56 |
|
---|
[b994a60] | 57 | /** Return Processor State Register.
|
---|
| 58 | *
|
---|
| 59 | * @return PSR.
|
---|
| 60 | */
|
---|
[7f1c620] | 61 | static inline uint64_t psr_read(void)
|
---|
[b994a60] | 62 | {
|
---|
[7f1c620] | 63 | uint64_t v;
|
---|
[b994a60] | 64 |
|
---|
| 65 | __asm__ volatile ("mov %0 = psr\n" : "=r" (v));
|
---|
| 66 |
|
---|
| 67 | return v;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[e2ec980f] | 70 | /** Read IVA (Interruption Vector Address).
|
---|
| 71 | *
|
---|
| 72 | * @return Return location of interruption vector table.
|
---|
| 73 | */
|
---|
[7f1c620] | 74 | static inline uint64_t iva_read(void)
|
---|
[e2ec980f] | 75 | {
|
---|
[7f1c620] | 76 | uint64_t v;
|
---|
[e2ec980f] | 77 |
|
---|
| 78 | __asm__ volatile ("mov %0 = cr.iva\n" : "=r" (v));
|
---|
| 79 |
|
---|
| 80 | return v;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Write IVA (Interruption Vector Address) register.
|
---|
| 84 | *
|
---|
[abbc16e] | 85 | * @param v New location of interruption vector table.
|
---|
[e2ec980f] | 86 | */
|
---|
[7f1c620] | 87 | static inline void iva_write(uint64_t v)
|
---|
[e2ec980f] | 88 | {
|
---|
| 89 | __asm__ volatile ("mov cr.iva = %0\n" : : "r" (v));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[0259524] | 93 | /** Read IVR (External Interrupt Vector Register).
|
---|
[dbd1059] | 94 | *
|
---|
| 95 | * @return Highest priority, pending, unmasked external interrupt vector.
|
---|
| 96 | */
|
---|
[7f1c620] | 97 | static inline uint64_t ivr_read(void)
|
---|
[dbd1059] | 98 | {
|
---|
[7f1c620] | 99 | uint64_t v;
|
---|
[dbd1059] | 100 |
|
---|
[0259524] | 101 | __asm__ volatile ("mov %0 = cr.ivr\n" : "=r" (v));
|
---|
[dbd1059] | 102 |
|
---|
[0259524] | 103 | return v;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Write ITC (Interval Timer Counter) register.
|
---|
| 107 | *
|
---|
[abbc16e] | 108 | * @param v New counter value.
|
---|
[0259524] | 109 | */
|
---|
[7f1c620] | 110 | static inline void itc_write(uint64_t v)
|
---|
[0259524] | 111 | {
|
---|
| 112 | __asm__ volatile ("mov ar.itc = %0\n" : : "r" (v));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /** Read ITC (Interval Timer Counter) register.
|
---|
| 116 | *
|
---|
| 117 | * @return Current counter value.
|
---|
| 118 | */
|
---|
[7f1c620] | 119 | static inline uint64_t itc_read(void)
|
---|
[0259524] | 120 | {
|
---|
[7f1c620] | 121 | uint64_t v;
|
---|
[0259524] | 122 |
|
---|
| 123 | __asm__ volatile ("mov %0 = ar.itc\n" : "=r" (v));
|
---|
| 124 |
|
---|
| 125 | return v;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /** Write ITM (Interval Timer Match) register.
|
---|
| 129 | *
|
---|
[abbc16e] | 130 | * @param v New match value.
|
---|
[0259524] | 131 | */
|
---|
[7f1c620] | 132 | static inline void itm_write(uint64_t v)
|
---|
[0259524] | 133 | {
|
---|
| 134 | __asm__ volatile ("mov cr.itm = %0\n" : : "r" (v));
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[98492e8] | 137 | /** Read ITM (Interval Timer Match) register.
|
---|
| 138 | *
|
---|
| 139 | * @return Match value.
|
---|
| 140 | */
|
---|
[7f1c620] | 141 | static inline uint64_t itm_read(void)
|
---|
[98492e8] | 142 | {
|
---|
[7f1c620] | 143 | uint64_t v;
|
---|
[98492e8] | 144 |
|
---|
| 145 | __asm__ volatile ("mov %0 = cr.itm\n" : "=r" (v));
|
---|
| 146 |
|
---|
| 147 | return v;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[05d9dd89] | 150 | /** Read ITV (Interval Timer Vector) register.
|
---|
| 151 | *
|
---|
| 152 | * @return Current vector and mask bit.
|
---|
| 153 | */
|
---|
[7f1c620] | 154 | static inline uint64_t itv_read(void)
|
---|
[05d9dd89] | 155 | {
|
---|
[7f1c620] | 156 | uint64_t v;
|
---|
[05d9dd89] | 157 |
|
---|
| 158 | __asm__ volatile ("mov %0 = cr.itv\n" : "=r" (v));
|
---|
| 159 |
|
---|
| 160 | return v;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[0259524] | 163 | /** Write ITV (Interval Timer Vector) register.
|
---|
| 164 | *
|
---|
[abbc16e] | 165 | * @param v New vector and mask bit.
|
---|
[0259524] | 166 | */
|
---|
[7f1c620] | 167 | static inline void itv_write(uint64_t v)
|
---|
[0259524] | 168 | {
|
---|
| 169 | __asm__ volatile ("mov cr.itv = %0\n" : : "r" (v));
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /** Write EOI (End Of Interrupt) register.
|
---|
| 173 | *
|
---|
[abbc16e] | 174 | * @param v This value is ignored.
|
---|
[0259524] | 175 | */
|
---|
[7f1c620] | 176 | static inline void eoi_write(uint64_t v)
|
---|
[0259524] | 177 | {
|
---|
| 178 | __asm__ volatile ("mov cr.eoi = %0\n" : : "r" (v));
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /** Read TPR (Task Priority Register).
|
---|
| 182 | *
|
---|
| 183 | * @return Current value of TPR.
|
---|
| 184 | */
|
---|
[7f1c620] | 185 | static inline uint64_t tpr_read(void)
|
---|
[0259524] | 186 | {
|
---|
[7f1c620] | 187 | uint64_t v;
|
---|
[0259524] | 188 |
|
---|
| 189 | __asm__ volatile ("mov %0 = cr.tpr\n" : "=r" (v));
|
---|
| 190 |
|
---|
| 191 | return v;
|
---|
[dbd1059] | 192 | }
|
---|
| 193 |
|
---|
[0259524] | 194 | /** Write TPR (Task Priority Register).
|
---|
| 195 | *
|
---|
[abbc16e] | 196 | * @param v New value of TPR.
|
---|
[0259524] | 197 | */
|
---|
[7f1c620] | 198 | static inline void tpr_write(uint64_t v)
|
---|
[0259524] | 199 | {
|
---|
| 200 | __asm__ volatile ("mov cr.tpr = %0\n" : : "r" (v));
|
---|
| 201 | }
|
---|
[9c0a9b3] | 202 |
|
---|
[0259524] | 203 | /** Disable interrupts.
|
---|
| 204 | *
|
---|
| 205 | * Disable interrupts and return previous
|
---|
| 206 | * value of PSR.
|
---|
| 207 | *
|
---|
| 208 | * @return Old interrupt priority level.
|
---|
| 209 | */
|
---|
| 210 | static ipl_t interrupts_disable(void)
|
---|
| 211 | {
|
---|
[7f1c620] | 212 | uint64_t v;
|
---|
[0259524] | 213 |
|
---|
| 214 | __asm__ volatile (
|
---|
| 215 | "mov %0 = psr\n"
|
---|
| 216 | "rsm %1\n"
|
---|
| 217 | : "=r" (v)
|
---|
| 218 | : "i" (PSR_I_MASK)
|
---|
| 219 | );
|
---|
| 220 |
|
---|
| 221 | return (ipl_t) v;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /** Enable interrupts.
|
---|
| 225 | *
|
---|
| 226 | * Enable interrupts and return previous
|
---|
| 227 | * value of PSR.
|
---|
| 228 | *
|
---|
| 229 | * @return Old interrupt priority level.
|
---|
| 230 | */
|
---|
| 231 | static ipl_t interrupts_enable(void)
|
---|
| 232 | {
|
---|
[7f1c620] | 233 | uint64_t v;
|
---|
[0259524] | 234 |
|
---|
| 235 | __asm__ volatile (
|
---|
| 236 | "mov %0 = psr\n"
|
---|
| 237 | "ssm %1\n"
|
---|
| 238 | ";;\n"
|
---|
| 239 | "srlz.d\n"
|
---|
| 240 | : "=r" (v)
|
---|
| 241 | : "i" (PSR_I_MASK)
|
---|
| 242 | );
|
---|
| 243 |
|
---|
| 244 | return (ipl_t) v;
|
---|
| 245 | }
|
---|
[9c0a9b3] | 246 |
|
---|
[0259524] | 247 | /** Restore interrupt priority level.
|
---|
| 248 | *
|
---|
| 249 | * Restore PSR.
|
---|
| 250 | *
|
---|
| 251 | * @param ipl Saved interrupt priority level.
|
---|
| 252 | */
|
---|
| 253 | static inline void interrupts_restore(ipl_t ipl)
|
---|
| 254 | {
|
---|
[2ccd275] | 255 | if (ipl & PSR_I_MASK)
|
---|
| 256 | (void) interrupts_enable();
|
---|
| 257 | else
|
---|
| 258 | (void) interrupts_disable();
|
---|
[0259524] | 259 | }
|
---|
[9c0a9b3] | 260 |
|
---|
[0259524] | 261 | /** Return interrupt priority level.
|
---|
| 262 | *
|
---|
| 263 | * @return PSR.
|
---|
| 264 | */
|
---|
| 265 | static inline ipl_t interrupts_read(void)
|
---|
| 266 | {
|
---|
[b994a60] | 267 | return (ipl_t) psr_read();
|
---|
[0259524] | 268 | }
|
---|
[60f6b7c] | 269 |
|
---|
[2a003d5b] | 270 | /** Disable protection key checking. */
|
---|
| 271 | static inline void pk_disable(void)
|
---|
| 272 | {
|
---|
| 273 | __asm__ volatile ("rsm %0\n" : : "i" (PSR_PK_MASK));
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[0259524] | 276 | extern void cpu_halt(void);
|
---|
| 277 | extern void cpu_sleep(void);
|
---|
[7f1c620] | 278 | extern void asm_delay_loop(uint32_t t);
|
---|
[5e2455a] | 279 |
|
---|
[7f1c620] | 280 | extern void switch_to_userspace(uintptr_t entry, uintptr_t sp, uintptr_t bsp, uintptr_t uspace_uarg, uint64_t ipsr, uint64_t rsc);
|
---|
[b994a60] | 281 |
|
---|
[361635c] | 282 | #endif
|
---|
[b45c443] | 283 |
|
---|
[06e1e95] | 284 | /** @}
|
---|
[b45c443] | 285 | */
|
---|