| 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 |
|
|---|
| 29 | /** @addtogroup ia64
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_ia64_ASM_H_
|
|---|
| 36 | #define KERN_ia64_ASM_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <config.h>
|
|---|
| 39 | #include <typedefs.h>
|
|---|
| 40 | #include <typedefs.h>
|
|---|
| 41 | #include <arch/register.h>
|
|---|
| 42 | #include <trace.h>
|
|---|
| 43 |
|
|---|
| 44 | #define IA64_IOSPACE_ADDRESS 0xE001000000000000ULL
|
|---|
| 45 |
|
|---|
| 46 | NO_TRACE static inline void pio_write_8(ioport8_t *port, uint8_t v)
|
|---|
| 47 | {
|
|---|
| 48 | uintptr_t prt = (uintptr_t) port;
|
|---|
| 49 |
|
|---|
| 50 | *((ioport8_t *) (IA64_IOSPACE_ADDRESS +
|
|---|
| 51 | ((prt & 0xfff) | ((prt >> 2) << 12)))) = v;
|
|---|
| 52 |
|
|---|
| 53 | asm volatile (
|
|---|
| 54 | "mf\n"
|
|---|
| 55 | ::: "memory"
|
|---|
| 56 | );
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | NO_TRACE static inline void pio_write_16(ioport16_t *port, uint16_t v)
|
|---|
| 60 | {
|
|---|
| 61 | uintptr_t prt = (uintptr_t) port;
|
|---|
| 62 |
|
|---|
| 63 | *((ioport16_t *) (IA64_IOSPACE_ADDRESS +
|
|---|
| 64 | ((prt & 0xfff) | ((prt >> 2) << 12)))) = v;
|
|---|
| 65 |
|
|---|
| 66 | asm volatile (
|
|---|
| 67 | "mf\n"
|
|---|
| 68 | ::: "memory"
|
|---|
| 69 | );
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | NO_TRACE static inline void pio_write_32(ioport32_t *port, uint32_t v)
|
|---|
| 73 | {
|
|---|
| 74 | uintptr_t prt = (uintptr_t) port;
|
|---|
| 75 |
|
|---|
| 76 | *((ioport32_t *) (IA64_IOSPACE_ADDRESS +
|
|---|
| 77 | ((prt & 0xfff) | ((prt >> 2) << 12)))) = v;
|
|---|
| 78 |
|
|---|
| 79 | asm volatile (
|
|---|
| 80 | "mf\n"
|
|---|
| 81 | ::: "memory"
|
|---|
| 82 | );
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | NO_TRACE static inline uint8_t pio_read_8(ioport8_t *port)
|
|---|
| 86 | {
|
|---|
| 87 | uintptr_t prt = (uintptr_t) port;
|
|---|
| 88 |
|
|---|
| 89 | asm volatile (
|
|---|
| 90 | "mf\n"
|
|---|
| 91 | ::: "memory"
|
|---|
| 92 | );
|
|---|
| 93 |
|
|---|
| 94 | return *((ioport8_t *) (IA64_IOSPACE_ADDRESS +
|
|---|
| 95 | ((prt & 0xfff) | ((prt >> 2) << 12))));
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | NO_TRACE static inline uint16_t pio_read_16(ioport16_t *port)
|
|---|
| 99 | {
|
|---|
| 100 | uintptr_t prt = (uintptr_t) port;
|
|---|
| 101 |
|
|---|
| 102 | asm volatile (
|
|---|
| 103 | "mf\n"
|
|---|
| 104 | ::: "memory"
|
|---|
| 105 | );
|
|---|
| 106 |
|
|---|
| 107 | return *((ioport16_t *) (IA64_IOSPACE_ADDRESS +
|
|---|
| 108 | ((prt & 0xfff) | ((prt >> 2) << 12))));
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | NO_TRACE static inline uint32_t pio_read_32(ioport32_t *port)
|
|---|
| 112 | {
|
|---|
| 113 | uintptr_t prt = (uintptr_t) port;
|
|---|
| 114 |
|
|---|
| 115 | asm volatile (
|
|---|
| 116 | "mf\n"
|
|---|
| 117 | ::: "memory"
|
|---|
| 118 | );
|
|---|
| 119 |
|
|---|
| 120 | return *((ioport32_t *) (IA64_IOSPACE_ADDRESS +
|
|---|
| 121 | ((prt & 0xfff) | ((prt >> 2) << 12))));
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Return base address of current stack
|
|---|
| 125 | *
|
|---|
| 126 | * Return the base address of the current stack.
|
|---|
| 127 | * The stack is assumed to be STACK_SIZE long.
|
|---|
| 128 | * The stack must start on page boundary.
|
|---|
| 129 | *
|
|---|
| 130 | */
|
|---|
| 131 | NO_TRACE static inline uintptr_t get_stack_base(void)
|
|---|
| 132 | {
|
|---|
| 133 | uint64_t v;
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | * I'm not sure why but this code inlines badly
|
|---|
| 137 | * in scheduler, resulting in THE shifting about
|
|---|
| 138 | * 16B and causing kernel panic.
|
|---|
| 139 | *
|
|---|
| 140 | * asm volatile (
|
|---|
| 141 | * "and %[value] = %[mask], r12"
|
|---|
| 142 | * : [value] "=r" (v)
|
|---|
| 143 | * : [mask] "r" (~(STACK_SIZE - 1))
|
|---|
| 144 | * );
|
|---|
| 145 | * return v;
|
|---|
| 146 | *
|
|---|
| 147 | * The following code has the same semantics but
|
|---|
| 148 | * inlines correctly.
|
|---|
| 149 | *
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | asm volatile (
|
|---|
| 153 | "mov %[value] = r12"
|
|---|
| 154 | : [value] "=r" (v)
|
|---|
| 155 | );
|
|---|
| 156 |
|
|---|
| 157 | return (v & (~(STACK_SIZE - 1)));
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Return Processor State Register.
|
|---|
| 161 | *
|
|---|
| 162 | * @return PSR.
|
|---|
| 163 | *
|
|---|
| 164 | */
|
|---|
| 165 | NO_TRACE static inline uint64_t psr_read(void)
|
|---|
| 166 | {
|
|---|
| 167 | uint64_t v;
|
|---|
| 168 |
|
|---|
| 169 | asm volatile (
|
|---|
| 170 | "mov %[value] = psr\n"
|
|---|
| 171 | : [value] "=r" (v)
|
|---|
| 172 | );
|
|---|
| 173 |
|
|---|
| 174 | return v;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** Read IVA (Interruption Vector Address).
|
|---|
| 178 | *
|
|---|
| 179 | * @return Return location of interruption vector table.
|
|---|
| 180 | *
|
|---|
| 181 | */
|
|---|
| 182 | NO_TRACE static inline uint64_t iva_read(void)
|
|---|
| 183 | {
|
|---|
| 184 | uint64_t v;
|
|---|
| 185 |
|
|---|
| 186 | asm volatile (
|
|---|
| 187 | "mov %[value] = cr.iva\n"
|
|---|
| 188 | : [value] "=r" (v)
|
|---|
| 189 | );
|
|---|
| 190 |
|
|---|
| 191 | return v;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /** Write IVA (Interruption Vector Address) register.
|
|---|
| 195 | *
|
|---|
| 196 | * @param v New location of interruption vector table.
|
|---|
| 197 | *
|
|---|
| 198 | */
|
|---|
| 199 | NO_TRACE static inline void iva_write(uint64_t v)
|
|---|
| 200 | {
|
|---|
| 201 | asm volatile (
|
|---|
| 202 | "mov cr.iva = %[value]\n"
|
|---|
| 203 | :: [value] "r" (v)
|
|---|
| 204 | );
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /** Read IVR (External Interrupt Vector Register).
|
|---|
| 208 | *
|
|---|
| 209 | * @return Highest priority, pending, unmasked external
|
|---|
| 210 | * interrupt vector.
|
|---|
| 211 | *
|
|---|
| 212 | */
|
|---|
| 213 | NO_TRACE static inline uint64_t ivr_read(void)
|
|---|
| 214 | {
|
|---|
| 215 | uint64_t v;
|
|---|
| 216 |
|
|---|
| 217 | asm volatile (
|
|---|
| 218 | "mov %[value] = cr.ivr\n"
|
|---|
| 219 | : [value] "=r" (v)
|
|---|
| 220 | );
|
|---|
| 221 |
|
|---|
| 222 | return v;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | NO_TRACE static inline uint64_t cr64_read(void)
|
|---|
| 226 | {
|
|---|
| 227 | uint64_t v;
|
|---|
| 228 |
|
|---|
| 229 | asm volatile (
|
|---|
| 230 | "mov %[value] = cr64\n"
|
|---|
| 231 | : [value] "=r" (v)
|
|---|
| 232 | );
|
|---|
| 233 |
|
|---|
| 234 | return v;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /** Write ITC (Interval Timer Counter) register.
|
|---|
| 238 | *
|
|---|
| 239 | * @param v New counter value.
|
|---|
| 240 | *
|
|---|
| 241 | */
|
|---|
| 242 | NO_TRACE static inline void itc_write(uint64_t v)
|
|---|
| 243 | {
|
|---|
| 244 | asm volatile (
|
|---|
| 245 | "mov ar.itc = %[value]\n"
|
|---|
| 246 | :: [value] "r" (v)
|
|---|
| 247 | );
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /** Read ITC (Interval Timer Counter) register.
|
|---|
| 251 | *
|
|---|
| 252 | * @return Current counter value.
|
|---|
| 253 | *
|
|---|
| 254 | */
|
|---|
| 255 | NO_TRACE static inline uint64_t itc_read(void)
|
|---|
| 256 | {
|
|---|
| 257 | uint64_t v;
|
|---|
| 258 |
|
|---|
| 259 | asm volatile (
|
|---|
| 260 | "mov %[value] = ar.itc\n"
|
|---|
| 261 | : [value] "=r" (v)
|
|---|
| 262 | );
|
|---|
| 263 |
|
|---|
| 264 | return v;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** Write ITM (Interval Timer Match) register.
|
|---|
| 268 | *
|
|---|
| 269 | * @param v New match value.
|
|---|
| 270 | *
|
|---|
| 271 | */
|
|---|
| 272 | NO_TRACE static inline void itm_write(uint64_t v)
|
|---|
| 273 | {
|
|---|
| 274 | asm volatile (
|
|---|
| 275 | "mov cr.itm = %[value]\n"
|
|---|
| 276 | :: [value] "r" (v)
|
|---|
| 277 | );
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /** Read ITM (Interval Timer Match) register.
|
|---|
| 281 | *
|
|---|
| 282 | * @return Match value.
|
|---|
| 283 | *
|
|---|
| 284 | */
|
|---|
| 285 | NO_TRACE static inline uint64_t itm_read(void)
|
|---|
| 286 | {
|
|---|
| 287 | uint64_t v;
|
|---|
| 288 |
|
|---|
| 289 | asm volatile (
|
|---|
| 290 | "mov %[value] = cr.itm\n"
|
|---|
| 291 | : [value] "=r" (v)
|
|---|
| 292 | );
|
|---|
| 293 |
|
|---|
| 294 | return v;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /** Read ITV (Interval Timer Vector) register.
|
|---|
| 298 | *
|
|---|
| 299 | * @return Current vector and mask bit.
|
|---|
| 300 | *
|
|---|
| 301 | */
|
|---|
| 302 | NO_TRACE static inline uint64_t itv_read(void)
|
|---|
| 303 | {
|
|---|
| 304 | uint64_t v;
|
|---|
| 305 |
|
|---|
| 306 | asm volatile (
|
|---|
| 307 | "mov %[value] = cr.itv\n"
|
|---|
| 308 | : [value] "=r" (v)
|
|---|
| 309 | );
|
|---|
| 310 |
|
|---|
| 311 | return v;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /** Write ITV (Interval Timer Vector) register.
|
|---|
| 315 | *
|
|---|
| 316 | * @param v New vector and mask bit.
|
|---|
| 317 | *
|
|---|
| 318 | */
|
|---|
| 319 | NO_TRACE static inline void itv_write(uint64_t v)
|
|---|
| 320 | {
|
|---|
| 321 | asm volatile (
|
|---|
| 322 | "mov cr.itv = %[value]\n"
|
|---|
| 323 | :: [value] "r" (v)
|
|---|
| 324 | );
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /** Write EOI (End Of Interrupt) register.
|
|---|
| 328 | *
|
|---|
| 329 | * @param v This value is ignored.
|
|---|
| 330 | *
|
|---|
| 331 | */
|
|---|
| 332 | NO_TRACE static inline void eoi_write(uint64_t v)
|
|---|
| 333 | {
|
|---|
| 334 | asm volatile (
|
|---|
| 335 | "mov cr.eoi = %[value]\n"
|
|---|
| 336 | :: [value] "r" (v)
|
|---|
| 337 | );
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /** Read TPR (Task Priority Register).
|
|---|
| 341 | *
|
|---|
| 342 | * @return Current value of TPR.
|
|---|
| 343 | *
|
|---|
| 344 | */
|
|---|
| 345 | NO_TRACE static inline uint64_t tpr_read(void)
|
|---|
| 346 | {
|
|---|
| 347 | uint64_t v;
|
|---|
| 348 |
|
|---|
| 349 | asm volatile (
|
|---|
| 350 | "mov %[value] = cr.tpr\n"
|
|---|
| 351 | : [value] "=r" (v)
|
|---|
| 352 | );
|
|---|
| 353 |
|
|---|
| 354 | return v;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /** Write TPR (Task Priority Register).
|
|---|
| 358 | *
|
|---|
| 359 | * @param v New value of TPR.
|
|---|
| 360 | *
|
|---|
| 361 | */
|
|---|
| 362 | NO_TRACE static inline void tpr_write(uint64_t v)
|
|---|
| 363 | {
|
|---|
| 364 | asm volatile (
|
|---|
| 365 | "mov cr.tpr = %[value]\n"
|
|---|
| 366 | :: [value] "r" (v)
|
|---|
| 367 | );
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | /** Disable interrupts.
|
|---|
| 371 | *
|
|---|
| 372 | * Disable interrupts and return previous
|
|---|
| 373 | * value of PSR.
|
|---|
| 374 | *
|
|---|
| 375 | * @return Old interrupt priority level.
|
|---|
| 376 | *
|
|---|
| 377 | */
|
|---|
| 378 | NO_TRACE static ipl_t interrupts_disable(void)
|
|---|
| 379 | {
|
|---|
| 380 | uint64_t v;
|
|---|
| 381 |
|
|---|
| 382 | asm volatile (
|
|---|
| 383 | "mov %[value] = psr\n"
|
|---|
| 384 | "rsm %[mask]\n"
|
|---|
| 385 | : [value] "=r" (v)
|
|---|
| 386 | : [mask] "i" (PSR_I_MASK)
|
|---|
| 387 | );
|
|---|
| 388 |
|
|---|
| 389 | return (ipl_t) v;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | /** Enable interrupts.
|
|---|
| 393 | *
|
|---|
| 394 | * Enable interrupts and return previous
|
|---|
| 395 | * value of PSR.
|
|---|
| 396 | *
|
|---|
| 397 | * @return Old interrupt priority level.
|
|---|
| 398 | *
|
|---|
| 399 | */
|
|---|
| 400 | NO_TRACE static ipl_t interrupts_enable(void)
|
|---|
| 401 | {
|
|---|
| 402 | uint64_t v;
|
|---|
| 403 |
|
|---|
| 404 | asm volatile (
|
|---|
| 405 | "mov %[value] = psr\n"
|
|---|
| 406 | "ssm %[mask]\n"
|
|---|
| 407 | ";;\n"
|
|---|
| 408 | "srlz.d\n"
|
|---|
| 409 | : [value] "=r" (v)
|
|---|
| 410 | : [mask] "i" (PSR_I_MASK)
|
|---|
| 411 | );
|
|---|
| 412 |
|
|---|
| 413 | return (ipl_t) v;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /** Restore interrupt priority level.
|
|---|
| 417 | *
|
|---|
| 418 | * Restore PSR.
|
|---|
| 419 | *
|
|---|
| 420 | * @param ipl Saved interrupt priority level.
|
|---|
| 421 | *
|
|---|
| 422 | */
|
|---|
| 423 | NO_TRACE static inline void interrupts_restore(ipl_t ipl)
|
|---|
| 424 | {
|
|---|
| 425 | if (ipl & PSR_I_MASK)
|
|---|
| 426 | (void) interrupts_enable();
|
|---|
| 427 | else
|
|---|
| 428 | (void) interrupts_disable();
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | /** Return interrupt priority level.
|
|---|
| 432 | *
|
|---|
| 433 | * @return PSR.
|
|---|
| 434 | *
|
|---|
| 435 | */
|
|---|
| 436 | NO_TRACE static inline ipl_t interrupts_read(void)
|
|---|
| 437 | {
|
|---|
| 438 | return (ipl_t) psr_read();
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | /** Check interrupts state.
|
|---|
| 442 | *
|
|---|
| 443 | * @return True if interrupts are disabled.
|
|---|
| 444 | *
|
|---|
| 445 | */
|
|---|
| 446 | NO_TRACE static inline bool interrupts_disabled(void)
|
|---|
| 447 | {
|
|---|
| 448 | return !(psr_read() & PSR_I_MASK);
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | /** Disable protection key checking. */
|
|---|
| 452 | NO_TRACE static inline void pk_disable(void)
|
|---|
| 453 | {
|
|---|
| 454 | asm volatile (
|
|---|
| 455 | "rsm %[mask]\n"
|
|---|
| 456 | ";;\n"
|
|---|
| 457 | "srlz.d\n"
|
|---|
| 458 | :: [mask] "i" (PSR_PK_MASK)
|
|---|
| 459 | );
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | extern void cpu_halt(void) __attribute__((noreturn));
|
|---|
| 463 | extern void cpu_sleep(void);
|
|---|
| 464 | extern void asm_delay_loop(uint32_t t);
|
|---|
| 465 |
|
|---|
| 466 | extern void switch_to_userspace(uintptr_t, uintptr_t, uintptr_t, uintptr_t,
|
|---|
| 467 | uint64_t, uint64_t);
|
|---|
| 468 |
|
|---|
| 469 | #endif
|
|---|
| 470 |
|
|---|
| 471 | /** @}
|
|---|
| 472 | */
|
|---|