[lk][arch] Add spin_cycles

Add an architecture specific function which spins for a specific
number of CPU cycles.  Currently implemented for ARM-M only.

Change-Id: Idbf2a83186cf5ffa239d644dc732fe3d419431c1
Signed-off-by: John Grossman <johngro@google.com>
This commit is contained in:
John Grossman
2015-05-19 11:57:53 -07:00
parent c2b645ef8a
commit 5f804f25fc
3 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
/* vim: set expandtab ts=4 sw=4 tw=100: */
/*
* Copyright (c) 2013 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <compiler.h>
#include <kernel/debug.h>
__ALIGNED(8) __NAKED
void spin_cycles(uint32_t cycles)
{
asm (
/* 4 cycles per loop, subtract out 8 cycles for the overhead of the next
* 4 instructions, plus the call into and return from the function.
* Then, add 3 then >> 2 to round up to the number of loop iterations.
*/
"subs r1, %[cycles], #5\n"
"asrs r1, r1, #2\n"
"ble .Ldone\n"
/* Padding to stay aligned on an 8 byte boundary, also has the added
* advantage of normalizing the overhead (1+1+2 cycles if the branch is
* take, or 1+1+1+1 cycles if the branch is skipped and the nop is
* executed)
*/
"nop\n"
/* Main delay loop.
* sub is 1 cycle
* nop is 1 cycle
* branch is 2 cycles
*/
".Lloop:\n"
"subs r1, r1, #1\n"
"nop\n"
"bne .Lloop\n"
".Ldone:\n"
"bx lr\n"
: /* no output */
: [cycles] "r" (cycles) /* input is cycles */
: "r1" /* r1 gets clobbered */
);
}

View File

@@ -182,6 +182,7 @@ MODULE_SRCS += \
$(LOCAL_DIR)/arm-m/arch.c \
$(LOCAL_DIR)/arm-m/vectab.c \
$(LOCAL_DIR)/arm-m/start.c \
$(LOCAL_DIR)/arm-m/spin_cycles.c \
$(LOCAL_DIR)/arm-m/exceptions.c \
$(LOCAL_DIR)/arm-m/thread.c

View File

@@ -80,6 +80,9 @@ void _panic(void *caller, const char *fmt, ...) __PRINTFLIKE(2, 3) __NO_RETURN;
/* spin the cpu for a period of (short) time */
void spin(uint32_t usecs);
/* spin the cpu for a certain number of cpu cycles */
void spin_cycles(uint32_t usecs);
__END_CDECLS
#endif